]> diplodocus.org Git - nmh/blob - uip/mhlist.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / mhlist.c
1 /* mhlist.c -- list the contents of MIME messages
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 <fcntl.h>
10 #include <h/signals.h>
11 #include <h/md5.h>
12 #include <h/mts.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
17 #include "h/done.h"
18 #include <h/utils.h>
19 #include "mhmisc.h"
20 #include "sbr/m_maildir.h"
21 #include "mhfree.h"
22
23 #define MHLIST_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("realsize", 0, SIZESW) \
29 X("norealsize", 0, NSIZESW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
32 X("disposition", 0, DISPOSW) \
33 X("nodisposition", 0, NDISPOSW) \
34 X("file file", 0, FILESW) \
35 X("part number", 0, PARTSW) \
36 X("type content", 0, TYPESW) \
37 X("prefer content", 0, PREFERSW) \
38 X("noprefer", 0, NPREFERSW) \
39 X("rcache policy", 0, RCACHESW) \
40 X("wcache policy", 0, WCACHESW) \
41 X("changecur", 0, CHGSW) \
42 X("nochangecur", 0, NCHGSW) \
43 X("version", 0, VERSIONSW) \
44 X("help", 0, HELPSW) \
45 X("debug", -5, DEBUGSW) \
46
47 #define X(sw, minchars, id) id,
48 DEFINE_SWITCH_ENUM(MHLIST);
49 #undef X
50
51 #define X(sw, minchars, id) { sw, minchars, id },
52 DEFINE_SWITCH_ARRAY(MHLIST, switches);
53 #undef X
54
55 /*
56 * This is currently needed to keep mhparse happy.
57 * This needs to be changed.
58 */
59 int debugsw = 0;
60
61 #define quitser pipeser
62
63 /*
64 * static prototypes
65 */
66 static void pipeser (int);
67
68
69 int
70 main (int argc, char **argv)
71 {
72 bool sizesw = true;
73 bool headsw = true;
74 bool chgflag = true;
75 bool verbosw = false;
76 bool dispo = false;
77 int msgnum, *icachesw;
78 char *cp, *file = NULL, *folder = NULL;
79 char *maildir, buf[100], **argp;
80 char **arguments;
81 struct msgs_array msgs = { 0, 0, NULL };
82 struct msgs *mp = NULL;
83 CT ct, *ctp;
84
85 if (nmh_init(argv[0], true, true)) { return 1; }
86
87 set_done(freects_done);
88
89 arguments = getarguments (invo_name, argc, argv, 1);
90 argp = arguments;
91
92 /*
93 * Parse arguments
94 */
95 while ((cp = *argp++)) {
96 if (*cp == '-') {
97 switch (smatch (++cp, switches)) {
98 case AMBIGSW:
99 ambigsw (cp, switches);
100 done (1);
101 case UNKWNSW:
102 die("-%s unknown", cp);
103
104 case HELPSW:
105 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
106 invo_name);
107 print_help (buf, switches, 1);
108 done (0);
109 case VERSIONSW:
110 print_version(invo_name);
111 done (0);
112
113 case RCACHESW:
114 icachesw = &rcachesw;
115 goto do_cache;
116 case WCACHESW:
117 icachesw = &wcachesw;
118 do_cache:
119 if (!(cp = *argp++) || *cp == '-')
120 die("missing argument to %s", argp[-2]);
121 switch (*icachesw = smatch (cp, cache_policy)) {
122 case AMBIGSW:
123 ambigsw (cp, cache_policy);
124 done (1);
125 case UNKWNSW:
126 die("%s unknown", cp);
127 default:
128 break;
129 }
130 continue;
131
132 case CHECKSW:
133 checksw++;
134 continue;
135 case NCHECKSW:
136 checksw = 0;
137 continue;
138
139 case HEADSW:
140 headsw = true;
141 continue;
142 case NHEADSW:
143 headsw = false;
144 continue;
145
146 case SIZESW:
147 sizesw = true;
148 continue;
149 case NSIZESW:
150 sizesw = false;
151 continue;
152
153 case PARTSW:
154 if (!(cp = *argp++) || *cp == '-')
155 die("missing argument to %s", argp[-2]);
156 if (npart >= NPARTS)
157 die("too many parts (starting with %s), %d max",
158 cp, NPARTS);
159 parts[npart++] = cp;
160 continue;
161
162 case TYPESW:
163 if (!(cp = *argp++) || *cp == '-')
164 die("missing argument to %s", argp[-2]);
165 if (ntype >= NTYPES)
166 die("too many types (starting with %s), %d max",
167 cp, NTYPES);
168 types[ntype++] = cp;
169 continue;
170
171 case PREFERSW:
172 if (!(cp = *argp++) || *cp == '-')
173 die("missing argument to %s", argp[-2]);
174 if (npreferred >= NPREFS)
175 die("too many preferred types (starting with %s), %d max",
176 cp, NPREFS);
177 mime_preference[npreferred].type = cp;
178 cp = strchr(cp, '/');
179 if (cp) *cp++ = '\0';
180 mime_preference[npreferred++].subtype = cp;
181 continue;
182
183 case NPREFERSW:
184 npreferred = 0;
185 continue;
186
187 case FILESW:
188 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
189 die("missing argument to %s", argp[-2]);
190 file = *cp == '-' ? cp : path (cp, TFILE);
191 continue;
192
193 case CHGSW:
194 chgflag = true;
195 continue;
196 case NCHGSW:
197 chgflag = false;
198 continue;
199
200 case VERBSW:
201 verbosw = true;
202 continue;
203 case NVERBSW:
204 verbosw = false;
205 continue;
206 case DISPOSW:
207 dispo = true;
208 continue;
209 case NDISPOSW:
210 dispo = false;
211 continue;
212 case DEBUGSW:
213 debugsw = 1;
214 continue;
215 }
216 }
217 if (*cp == '+' || *cp == '@') {
218 if (folder)
219 die("only one folder at a time!");
220 folder = pluspath (cp);
221 } else
222 app_msgarg(&msgs, cp);
223 }
224
225 /* null terminate the list of acceptable parts/types */
226 parts[npart] = NULL;
227 types[ntype] = NULL;
228
229 /* Check for public cache location */
230 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
231 cache_public = NULL;
232
233 /* Check for private cache location */
234 if (!(cache_private = context_find (nmhprivcache)))
235 cache_private = ".cache";
236 cache_private = mh_xstrdup(m_maildir(cache_private));
237
238 if (!context_find ("path"))
239 free (path ("./", TFOLDER));
240
241 if (file && msgs.size)
242 die("cannot specify msg and file at same time!");
243
244 /*
245 * check if message is coming from file
246 */
247 if (file) {
248 cts = mh_xcalloc(2, sizeof *cts);
249 ctp = cts;
250
251 if ((ct = parse_mime (file)))
252 *ctp++ = ct;
253 } else {
254 /*
255 * message(s) are coming from a folder
256 */
257 if (!msgs.size)
258 app_msgarg(&msgs, "cur");
259 if (!folder)
260 folder = getfolder (1);
261 maildir = m_maildir (folder);
262
263 if (chdir (maildir) == NOTOK)
264 adios (maildir, "unable to change directory to");
265
266 /* read folder and create message structure */
267 if (!(mp = folder_read (folder, 0)))
268 die("unable to read folder %s", folder);
269
270 /* check for empty folder */
271 if (mp->nummsg == 0)
272 die("no messages in %s", folder);
273
274 /* parse all the message ranges/sequences and set SELECTED */
275 for (msgnum = 0; msgnum < msgs.size; msgnum++)
276 if (!m_convert (mp, msgs.msgs[msgnum]))
277 done (1);
278 seq_setprev (mp); /* set the previous-sequence */
279
280 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
281 ctp = cts;
282
283 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
284 if (is_selected(mp, msgnum)) {
285 char *msgnam;
286
287 msgnam = m_name (msgnum);
288 if ((ct = parse_mime (msgnam)))
289 *ctp++ = ct;
290 }
291 }
292 }
293
294 if (!*cts)
295 done (1);
296
297 userrs = true;
298 SIGNAL (SIGQUIT, quitser);
299 SIGNAL (SIGPIPE, pipeser);
300
301 /*
302 * Get the associated umask for the relevant contents.
303 */
304 for (ctp = cts; *ctp; ctp++) {
305 struct stat st;
306
307 ct = *ctp;
308 if (type_ok (ct, 1) && !ct->c_umask) {
309 if (stat (ct->c_file, &st) != NOTOK)
310 ct->c_umask = ~(st.st_mode & 0777);
311 else
312 ct->c_umask = ~m_gmprot();
313 }
314 }
315
316 /*
317 * List the message content
318 */
319 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
320
321 /* Now free all the structures for the content */
322 for (ctp = cts; *ctp; ctp++)
323 free_content (*ctp);
324
325 free(cts);
326 cts = NULL;
327
328 /* If reading from a folder, do some updating */
329 if (mp) {
330 context_replace (pfolder, folder);/* update current folder */
331 if (chgflag)
332 seq_setcur (mp, mp->hghsel); /* update current message */
333 seq_save (mp); /* synchronize sequences */
334 context_save (); /* save the context file */
335 }
336
337 done (0);
338 return 1;
339 }
340
341
342 static void
343 pipeser (int i)
344 {
345 if (i == SIGQUIT) {
346 fflush (stdout);
347 fprintf (stderr, "\n");
348 fflush (stderr);
349 }
350
351 done (1);
352 /* NOTREACHED */
353 }