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