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