]> diplodocus.org Git - nmh/blob - uip/mhshow.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / mhshow.c
1 /* mhshow.c -- display 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/fmt_scan.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/mhcachesbr.h>
18 #include "h/done.h"
19 #include <h/utils.h>
20 #include "mhmisc.h"
21 #include "sbr/m_maildir.h"
22 #include "sbr/m_popen.h"
23 #include "mhfree.h"
24 #include "mhshowsbr.h"
25
26 #define MHSHOW_SWITCHES \
27 X("check", 0, CHECKSW) \
28 X("nocheck", 0, NCHECKSW) \
29 X("verbose", 0, VERBSW) \
30 X("noverbose", 0, NVERBSW) \
31 X("concat", 0, CONCATSW) \
32 X("noconcat", 0, NCONCATSW) \
33 X("textonly", 0, TEXTONLYSW) \
34 X("notextonly", 0, NTEXTONLYSW) \
35 X("inlineonly", 0, INLINESW) \
36 X("noinlineonly", 0, NINLINESW) \
37 X("file file", 0, FILESW) \
38 X("form formfile", 0, FORMSW) \
39 X("header", 0, HEADSW) \
40 X("noheader", 0, NHEADSW) \
41 X("headerform formfile", 0, HEADFORMSW) \
42 X("markform formfile", 0, MARKFORMSW) \
43 X("part number", 0, PARTSW) \
44 X("type content", 0, TYPESW) \
45 X("prefer content", 0, PREFERSW) \
46 X("noprefer", 0, NPREFERSW) \
47 X("rcache policy", 0, RCACHESW) \
48 X("wcache policy", 0, WCACHESW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
51 /* \
52 * switches for moreproc/mhlproc \
53 */ \
54 X("moreproc program", -4, PROGSW) \
55 X("nomoreproc", -3, NPROGSW) \
56 X("length lines", -4, LENSW) \
57 X("width columns", -4, WIDTHSW) \
58 /* \
59 * switches for debugging \
60 */ \
61 X("debug", -5, DEBUGSW) \
62
63 #define X(sw, minchars, id) id,
64 DEFINE_SWITCH_ENUM(MHSHOW);
65 #undef X
66
67 #define X(sw, minchars, id) { sw, minchars, id },
68 DEFINE_SWITCH_ARRAY(MHSHOW, switches);
69 #undef X
70
71
72 int debugsw = 0;
73 int verbosw = 0;
74
75 #define quitser pipeser
76
77 /*
78 * static prototypes
79 */
80 static void pipeser (int);
81
82
83 int
84 main (int argc, char **argv)
85 {
86 int msgnum, *icachesw, concatsw = -1, textonly = -1, inlineonly = -1;
87 char *cp, *file = NULL;
88 char *maildir, buf[100], **argp;
89 char **arguments;
90 struct msgs_array msgs = { 0, 0, NULL };
91 struct msgs *mp = NULL;
92 CT ct, *ctp;
93 FILE *fp;
94
95 if (nmh_init(argv[0], true, true)) { return 1; }
96
97 set_done(freects_done);
98
99 arguments = getarguments (invo_name, argc, argv, 1);
100 argp = arguments;
101
102 /*
103 * Parse arguments
104 */
105 while ((cp = *argp++)) {
106 if (*cp == '-') {
107 switch (smatch (++cp, switches)) {
108 case AMBIGSW:
109 ambigsw (cp, switches);
110 done (1);
111 case UNKWNSW:
112 die("-%s unknown", cp);
113
114 case HELPSW:
115 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
116 invo_name);
117 print_help (buf, switches, 1);
118 done (0);
119 case VERSIONSW:
120 print_version(invo_name);
121 done (0);
122
123 case RCACHESW:
124 icachesw = &rcachesw;
125 goto do_cache;
126 case WCACHESW:
127 icachesw = &wcachesw;
128 do_cache:
129 if (!(cp = *argp++) || *cp == '-')
130 die("missing argument to %s", argp[-2]);
131 switch (*icachesw = smatch (cp, cache_policy)) {
132 case AMBIGSW:
133 ambigsw (cp, cache_policy);
134 done (1);
135 case UNKWNSW:
136 die("%s unknown", cp);
137 default:
138 break;
139 }
140 continue;
141
142 case CHECKSW:
143 checksw++;
144 continue;
145 case NCHECKSW:
146 checksw = 0;
147 continue;
148
149 case CONCATSW:
150 concatsw = 1;
151 continue;
152 case NCONCATSW:
153 concatsw = 0;
154 continue;
155 case TEXTONLYSW:
156 textonly = 1;
157 continue;
158 case NTEXTONLYSW:
159 textonly = 0;
160 continue;
161 case INLINESW:
162 inlineonly = 1;
163 continue;
164 case NINLINESW:
165 inlineonly = 0;
166 continue;
167
168 case PARTSW:
169 if (!(cp = *argp++) || *cp == '-')
170 die("missing argument to %s", argp[-2]);
171 if (npart >= NPARTS)
172 die("too many parts (starting with %s), %d max",
173 cp, NPARTS);
174 parts[npart++] = cp;
175 continue;
176
177 case TYPESW:
178 if (!(cp = *argp++) || *cp == '-')
179 die("missing argument to %s", argp[-2]);
180 if (ntype >= NTYPES)
181 die("too many types (starting with %s), %d max",
182 cp, NTYPES);
183 types[ntype++] = cp;
184 continue;
185
186 case PREFERSW:
187 if (!(cp = *argp++) || *cp == '-')
188 die("missing argument to %s", argp[-2]);
189 if (npreferred >= NPREFS)
190 die("too many preferred types (starting with %s), %d max",
191 cp, NPREFS);
192 mime_preference[npreferred].type = cp;
193 cp = strchr(cp, '/');
194 if (cp) *cp++ = '\0';
195 mime_preference[npreferred++].subtype = cp;
196 continue;
197
198 case NPREFERSW:
199 npreferred = 0;
200 continue;
201
202 case FILESW:
203 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204 die("missing argument to %s", argp[-2]);
205 file = *cp == '-' ? cp : path (cp, TFILE);
206 continue;
207
208 case FORMSW:
209 if (!(cp = *argp++) || *cp == '-')
210 die("missing argument to %s", argp[-2]);
211 free(formsw);
212 formsw = mh_xstrdup(etcpath(cp));
213 continue;
214
215 case HEADSW:
216 headersw = 1;
217 continue;
218 case NHEADSW:
219 headersw = 0;
220 continue;
221
222 case HEADFORMSW:
223 if (!(headerform = *argp++) || *headerform == '-')
224 die("missing argument to %s", argp[-2]);
225 continue;
226
227 case MARKFORMSW:
228 if (!(markerform = *argp++) || *markerform == '-')
229 die("missing argument to %s", argp[-2]);
230 continue;
231
232 /*
233 * Switches for moreproc/mhlproc
234 */
235 case PROGSW:
236 if (!(progsw = *argp++) || *progsw == '-')
237 die("missing argument to %s", argp[-2]);
238 continue;
239 case NPROGSW:
240 nomore++;
241 continue;
242
243 case LENSW:
244 case WIDTHSW:
245 if (!(cp = *argp++) || *cp == '-')
246 die("missing argument to %s", argp[-2]);
247 continue;
248
249 case VERBSW:
250 verbosw = 1;
251 continue;
252 case NVERBSW:
253 verbosw = 0;
254 continue;
255 case DEBUGSW:
256 debugsw = 1;
257 continue;
258 }
259 }
260 if (*cp == '+' || *cp == '@') {
261 if (folder)
262 die("only one folder at a time!");
263 folder = pluspath (cp);
264 } else
265 app_msgarg(&msgs, cp);
266 }
267
268 /* null terminate the list of acceptable parts/types */
269 parts[npart] = NULL;
270 types[ntype] = NULL;
271
272 /*
273 * If we had any specific parts or types specified, turn off text only
274 * content.
275 */
276
277 if (npart > 0 || ntype > 0) {
278 if (textonly == -1)
279 textonly = 0;
280 if (inlineonly == -1)
281 inlineonly = 0;
282 }
283
284 /*
285 * Check if we've specified an additional profile
286 */
287 if ((cp = getenv ("MHSHOW"))) {
288 if ((fp = fopen (cp, "r"))) {
289 readconfig(NULL, fp, cp, 0);
290 fclose (fp);
291 } else {
292 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
293 }
294 }
295
296 /*
297 * Read the standard profile setup
298 */
299 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
300 readconfig(NULL, fp, cp, 0);
301 fclose (fp);
302 }
303
304 /* Check for public cache location */
305 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
306 cache_public = NULL;
307
308 /* Check for private cache location */
309 if (!(cache_private = context_find (nmhprivcache)))
310 cache_private = ".cache";
311 cache_private = mh_xstrdup(m_maildir(cache_private));
312
313 if (!context_find ("path"))
314 free (path ("./", TFOLDER));
315
316 if (file && msgs.size)
317 die("cannot specify msg and file at same time!");
318
319 /*
320 * check if message is coming from file
321 */
322 if (file) {
323 cts = mh_xcalloc(2, sizeof *cts);
324 ctp = cts;
325
326 if ((ct = parse_mime (file)))
327 *ctp++ = ct;
328
329 headersw = 0;
330 } else {
331 /*
332 * message(s) are coming from a folder
333 */
334 if (!msgs.size)
335 app_msgarg(&msgs, "cur");
336 if (!folder)
337 folder = getfolder (1);
338 maildir = m_maildir (folder);
339
340 if (chdir (maildir) == NOTOK)
341 adios (maildir, "unable to change directory to");
342
343 /* read folder and create message structure */
344 if (!(mp = folder_read (folder, 1)))
345 die("unable to read folder %s", folder);
346
347 /* check for empty folder */
348 if (mp->nummsg == 0)
349 die("no messages in %s", folder);
350
351 /* parse all the message ranges/sequences and set SELECTED */
352 for (msgnum = 0; msgnum < msgs.size; msgnum++)
353 if (!m_convert (mp, msgs.msgs[msgnum]))
354 done (1);
355
356 /*
357 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
358 * since we will use that as a tag to know which messages
359 * to remove from the "unseen" sequence.
360 */
361 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
362 if (is_selected(mp, msgnum))
363 set_unseen (mp, msgnum);
364
365 seq_setprev (mp); /* set the Previous-Sequence */
366 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
367
368 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
369 ctp = cts;
370
371 /*
372 * Parse all the SELECTED messages.
373 */
374 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
375 if (is_selected(mp, msgnum)) {
376 char *msgnam;
377
378 msgnam = m_name (msgnum);
379 if ((ct = parse_mime (msgnam)))
380 *ctp++ = ct;
381 }
382 }
383 }
384
385 if (!*cts)
386 done (1);
387
388 userrs = true;
389 SIGNAL (SIGQUIT, quitser);
390 SIGNAL (SIGPIPE, pipeser);
391
392 /*
393 * Get the associated umask for the relevant contents.
394 */
395 for (ctp = cts; *ctp; ctp++) {
396 struct stat st;
397
398 ct = *ctp;
399 if (type_ok (ct, 1) && !ct->c_umask) {
400 if (stat (ct->c_file, &st) != NOTOK)
401 ct->c_umask = ~(st.st_mode & 0777);
402 else
403 ct->c_umask = ~m_gmprot();
404 }
405 }
406
407 /* If reading from a folder, do some updating */
408 if (mp) {
409 context_replace (pfolder, folder);/* update current folder */
410 seq_setcur (mp, mp->hghsel); /* update current message */
411 seq_save (mp); /* synchronize sequences */
412 context_save (); /* save the context file */
413 }
414
415 if (concatsw)
416 m_popen(moreproc, 0);
417
418 /*
419 * Show the message content
420 */
421 show_all_messages (cts, concatsw, textonly, inlineonly);
422
423 /* Now free all the structures for the content */
424 for (ctp = cts; *ctp; ctp++)
425 free_content (*ctp);
426
427 free(cts);
428 cts = NULL;
429
430 if (concatsw)
431 m_pclose();
432
433 done (0);
434 return 1;
435 }
436
437
438 static void
439 pipeser (int i)
440 {
441 if (i == SIGQUIT) {
442 fflush (stdout);
443 fprintf (stderr, "\n");
444 fflush (stderr);
445 }
446
447 done (1);
448 /* NOTREACHED */
449 }