]> diplodocus.org Git - nmh/blob - uip/mhpath.c
ecb805525df7981ee9f8d2a10eb5b30f277738eb
[nmh] / uip / mhpath.c
1 /* mhpath.c -- print full pathnames of nmh messages and folders
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/context_find.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/path.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/error.h"
15 #include "h/done.h"
16 #include "h/utils.h"
17 #include "sbr/m_maildir.h"
18
19 #define MHPATH_SWITCHES \
20 X("version", 0, VERSIONSW) \
21 X("help", 0, HELPSW) \
22
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(MHPATH);
25 #undef X
26
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(MHPATH, switches);
29 #undef X
30
31 int
32 main(int argc, char **argv)
33 {
34 int i;
35 char *cp, *maildir, *folder = NULL;
36 char **argp;
37 char **arguments, buf[BUFSIZ];
38 struct msgs_array msgs = { 0, 0, NULL };
39 struct msgs *mp;
40
41 if (nmh_init(argv[0], true, false)) { return 1; }
42
43 arguments = getarguments (invo_name, argc, argv, 1);
44 argp = arguments;
45
46 /*
47 * Parse arguments
48 */
49 while ((cp = *argp++)) {
50 if (*cp == '-') {
51 switch (smatch (++cp, switches)) {
52 case AMBIGSW:
53 ambigsw (cp, switches);
54 done (1);
55 case UNKWNSW:
56 die("-%s unknown", cp);
57
58 case HELPSW:
59 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
60 invo_name);
61 print_help (buf, switches, 1);
62 done (0);
63 case VERSIONSW:
64 print_version(invo_name);
65 done (0);
66 }
67 }
68 if (*cp == '+' || *cp == '@') {
69 if (folder)
70 die("only one folder at a time!");
71 folder = pluspath (cp);
72 } else
73 app_msgarg(&msgs, cp);
74 }
75
76 if (!context_find ("path"))
77 free (path ("./", TFOLDER));
78
79 if (!folder)
80 folder = getfolder (1);
81 maildir = m_maildir (folder);
82
83 /* If no messages are given, print folder pathname */
84 if (!msgs.size) {
85 puts(maildir);
86 done (0);
87 }
88
89 if (chdir (maildir) == NOTOK)
90 adios (maildir, "unable to change directory to");
91
92 /* read folder and create message structure */
93 if (!(mp = folder_read (folder, 1)))
94 die("unable to read folder %s", folder);
95
96 /*
97 * We need to make sure there is message status space
98 * for all the message numbers from 1 to "new" since
99 * mhpath can select empty slots. If we are adding
100 * space at the end, we go ahead and add 10 slots.
101 */
102 if (mp->hghmsg >= mp->hghoff) {
103 if (!(mp = folder_realloc (mp, 1, mp->hghmsg + 10)))
104 die("unable to allocate folder storage");
105 } else if (mp->lowoff > 1) {
106 if (!(mp = folder_realloc (mp, 1, mp->hghoff)))
107 die("unable to allocate folder storage");
108 }
109
110 mp->msgflags |= ALLOW_NEW; /* allow the "new" sequence */
111
112 /* parse all the message ranges/sequences and set SELECTED */
113 for (i = 0; i < msgs.size; i++)
114 if (!m_convert (mp, msgs.msgs[i]))
115 done (1);
116
117 seq_setprev (mp); /* set the previous-sequence */
118
119 /* print the path of all selected messages */
120 for (i = mp->lowsel; i <= mp->hghsel; i++)
121 if (is_selected (mp, i))
122 printf ("%s/%s\n", mp->foldpath, m_name (i));
123
124 seq_save (mp); /* synchronize message sequences */
125 context_save (); /* save the context file */
126 folder_free (mp); /* free folder/message structure */
127 done (0);
128 return 1;
129 }