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