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