]> diplodocus.org Git - nmh/blob - uip/mhn.c
picksbr.c: fprintf(3) function pointer using `%p' and union.
[nmh] / uip / mhn.c
1 /* mhn.c -- display, list, cache, or store 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 "mhfree.h"
23 #include "mhshowsbr.h"
24
25 #define MHN_SWITCHES \
26 X("auto", 0, AUTOSW) \
27 X("noauto", 0, NAUTOSW) \
28 X("cache", 0, CACHESW) \
29 X("nocache", 0, NCACHESW) \
30 X("check", 0, CHECKSW) \
31 X("nocheck", 0, NCHECKSW) \
32 X("headers", 0, HEADSW) \
33 X("noheaders", 0, NHEADSW) \
34 X("list", 0, LISTSW) \
35 X("nolist", 0, NLISTSW) \
36 X("realsize", 0, SIZESW) \
37 X("norealsize", 0, NSIZESW) \
38 X("show", 0, SHOWSW) \
39 X("noshow", 0, NSHOWSW) \
40 X("store", 0, STORESW) \
41 X("nostore", 0, NSTORESW) \
42 X("verbose", 0, VERBSW) \
43 X("noverbose", 0, NVERBSW) \
44 X("file file", 0, FILESW) \
45 X("form formfile", 0, FORMSW) \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("rcache policy", 0, RCACHESW) \
49 X("wcache policy", 0, WCACHESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
52 /* \
53 * for debugging \
54 */ \
55 X("debug", -5, DEBUGSW) \
56 /* \
57 * switches for moreproc/mhlproc \
58 */ \
59 X("moreproc program", -4, PROGSW) \
60 X("nomoreproc", -3, NPROGSW) \
61 X("length lines", -4, LENSW) \
62 X("width columns", -4, WIDTHSW) \
63 /* \
64 * switches for mhbuild \
65 */ \
66 X("build", -5, BUILDSW) \
67 X("nobuild", -7, NBUILDSW) \
68 X("rfc934mode", 0, RFC934SW) \
69 X("norfc934mode", 0, NRFC934SW) \
70
71 #define X(sw, minchars, id) id,
72 DEFINE_SWITCH_ENUM(MHN);
73 #undef X
74
75 #define X(sw, minchars, id) { sw, minchars, id },
76 DEFINE_SWITCH_ARRAY(MHN, switches);
77 #undef X
78
79
80 int debugsw = 0;
81 int verbosw = 0;
82
83 /*
84 * variables for mhbuild (mhn -build)
85 */
86 static int buildsw = 0;
87 static int rfc934sw = 0;
88
89 /*
90 * what action to take?
91 */
92 static int cachesw = 0;
93 static int listsw = 0;
94 static int showsw = 0;
95 static int storesw = 0;
96
97 #define quitser pipeser
98
99 /*
100 * static prototypes
101 */
102 static void pipeser (int);
103
104
105 int
106 main (int argc, char **argv)
107 {
108 int sizesw = 1, headsw = 1, autosw = 0;
109 int msgnum, *icachesw;
110 char *cp, *file = NULL, *folder = NULL;
111 char *maildir, buf[100], **argp;
112 char **arguments;
113 char *cwd;
114 struct msgs_array msgs = { 0, 0, NULL };
115 struct msgs *mp = NULL;
116 CT ct, *ctp;
117 FILE *fp;
118 mhstoreinfo_t info;
119
120 if (nmh_init(argv[0], 1)) { return 1; }
121
122 set_done(freects_done);
123
124 arguments = getarguments (invo_name, argc, argv, 1);
125 argp = arguments;
126
127 /*
128 * Parse arguments
129 */
130 while ((cp = *argp++)) {
131 if (*cp == '-') {
132 switch (smatch (++cp, switches)) {
133 case AMBIGSW:
134 ambigsw (cp, switches);
135 done (1);
136 case UNKWNSW:
137 adios (NULL, "-%s unknown", cp);
138
139 case HELPSW:
140 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
141 invo_name);
142 print_help (buf, switches, 1);
143 done (0);
144 case VERSIONSW:
145 print_version(invo_name);
146 done (0);
147
148 case AUTOSW:
149 autosw++;
150 continue;
151 case NAUTOSW:
152 autosw = 0;
153 continue;
154
155 case CACHESW:
156 cachesw++;
157 continue;
158 case NCACHESW:
159 cachesw = 0;
160 continue;
161
162 case RCACHESW:
163 icachesw = &rcachesw;
164 goto do_cache;
165 case WCACHESW:
166 icachesw = &wcachesw;
167 do_cache:
168 if (!(cp = *argp++) || *cp == '-')
169 adios (NULL, "missing argument to %s", argp[-2]);
170 switch (*icachesw = smatch (cp, cache_policy)) {
171 case AMBIGSW:
172 ambigsw (cp, cache_policy);
173 done (1);
174 case UNKWNSW:
175 adios (NULL, "%s unknown", cp);
176 default:
177 break;
178 }
179 continue;
180
181 case CHECKSW:
182 checksw++;
183 continue;
184 case NCHECKSW:
185 checksw = 0;
186 continue;
187
188 case HEADSW:
189 headsw = 1;
190 continue;
191 case NHEADSW:
192 headsw = 0;
193 continue;
194
195 case LISTSW:
196 listsw = 1;
197 continue;
198 case NLISTSW:
199 listsw = 0;
200 continue;
201
202 case SHOWSW:
203 showsw = 1;
204 continue;
205 case NSHOWSW:
206 showsw = 0;
207 continue;
208
209 case SIZESW:
210 sizesw = 1;
211 continue;
212 case NSIZESW:
213 sizesw = 0;
214 continue;
215
216 case STORESW:
217 storesw = 1;
218 continue;
219 case NSTORESW:
220 storesw = 0;
221 continue;
222
223 case PARTSW:
224 if (!(cp = *argp++) || *cp == '-')
225 adios (NULL, "missing argument to %s", argp[-2]);
226 if (npart >= NPARTS)
227 adios (NULL, "too many parts (starting with %s), %d max",
228 cp, NPARTS);
229 parts[npart++] = cp;
230 continue;
231
232 case TYPESW:
233 if (!(cp = *argp++) || *cp == '-')
234 adios (NULL, "missing argument to %s", argp[-2]);
235 if (ntype >= NTYPES)
236 adios (NULL, "too many types (starting with %s), %d max",
237 cp, NTYPES);
238 types[ntype++] = cp;
239 continue;
240
241 case FILESW:
242 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
243 adios (NULL, "missing argument to %s", argp[-2]);
244 file = *cp == '-' ? cp : path (cp, TFILE);
245 continue;
246
247 case FORMSW:
248 if (!(cp = *argp++) || *cp == '-')
249 adios (NULL, "missing argument to %s", argp[-2]);
250 free(formsw);
251 formsw = getcpy (etcpath (cp));
252 continue;
253
254 /*
255 * Switches for moreproc/mhlproc
256 */
257 case PROGSW:
258 if (!(progsw = *argp++) || *progsw == '-')
259 adios (NULL, "missing argument to %s", argp[-2]);
260 continue;
261 case NPROGSW:
262 nomore++;
263 continue;
264
265 case LENSW:
266 case WIDTHSW:
267 if (!(cp = *argp++) || *cp == '-')
268 adios (NULL, "missing argument to %s", argp[-2]);
269 continue;
270
271 /*
272 * Switches for mhbuild
273 */
274 case BUILDSW:
275 buildsw = 1;
276 continue;
277 case NBUILDSW:
278 buildsw = 0;
279 continue;
280 case RFC934SW:
281 rfc934sw = 1;
282 continue;
283 case NRFC934SW:
284 rfc934sw = -1;
285 continue;
286
287 case VERBSW:
288 verbosw = 1;
289 continue;
290 case NVERBSW:
291 verbosw = 0;
292 continue;
293 case DEBUGSW:
294 debugsw = 1;
295 continue;
296 }
297 }
298 if (*cp == '+' || *cp == '@') {
299 if (folder)
300 adios (NULL, "only one folder at a time!");
301 folder = pluspath (cp);
302 } else
303 app_msgarg(&msgs, cp);
304 }
305
306 /* null terminate the list of acceptable parts/types */
307 parts[npart] = NULL;
308 types[ntype] = NULL;
309
310 /*
311 * Check if we've specified an additional profile
312 */
313 if ((cp = getenv ("MHN"))) {
314 if ((fp = fopen (cp, "r"))) {
315 readconfig(NULL, fp, cp, 0);
316 fclose (fp);
317 } else {
318 admonish ("", "unable to read $MHN profile (%s)", cp);
319 }
320 }
321
322 /*
323 * Read the standard profile setup
324 */
325 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
326 readconfig(NULL, fp, cp, 0);
327 fclose (fp);
328 }
329
330 /* Check for public cache location */
331 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
332 cache_public = NULL;
333
334 /* Check for private cache location */
335 if (!(cache_private = context_find (nmhprivcache)))
336 cache_private = ".cache";
337 cache_private = getcpy (m_maildir (cache_private));
338
339 /*
340 * Cache the current directory before we do any chdirs()'s.
341 */
342 cwd = mh_xstrdup(pwd());
343
344 if (!context_find ("path"))
345 free (path ("./", TFOLDER));
346
347 /*
348 * Process a mhn composition file (mhn -build)
349 */
350 if (buildsw) {
351 char *vec[MAXARGS];
352 int vecp;
353
354 if (showsw || storesw || cachesw)
355 adios (NULL, "cannot use -build with -show, -store, -cache");
356 if (msgs.size < 1)
357 adios (NULL, "need to specify a %s composition file", invo_name);
358 if (msgs.size > 1)
359 adios (NULL, "only one %s composition file at a time", invo_name);
360
361 vecp = 0;
362 vec[vecp++] = "mhbuild";
363
364 if (rfc934sw == 1)
365 vec[vecp++] = "-rfc934mode";
366 else if (rfc934sw == -1)
367 vec[vecp++] = "-norfc934mode";
368
369 vec[vecp++] = msgs.msgs[0];
370 vec[vecp] = NULL;
371
372 execvp ("mhbuild", vec);
373 fprintf (stderr, "unable to exec ");
374 _exit(1);
375 }
376
377 /*
378 * Process a mhn composition file (old MH style)
379 */
380 if (msgs.size == 1 && !folder && !npart && !cachesw
381 && !showsw && !storesw && !ntype && !file
382 && (cp = getenv ("mhdraft"))
383 && strcmp (cp, msgs.msgs[0]) == 0) {
384
385 char *vec[MAXARGS];
386 int vecp;
387
388 vecp = 0;
389 vec[vecp++] = "mhbuild";
390
391 if (rfc934sw == 1)
392 vec[vecp++] = "-rfc934mode";
393 else if (rfc934sw == -1)
394 vec[vecp++] = "-norfc934mode";
395
396 vec[vecp++] = cp;
397 vec[vecp] = NULL;
398
399 execvp ("mhbuild", vec);
400 fprintf (stderr, "unable to exec ");
401 _exit(1);
402 }
403
404 if (file && msgs.size)
405 adios (NULL, "cannot specify msg and file at same time!");
406
407 /*
408 * check if message is coming from file
409 */
410 if (file) {
411 cts = mh_xcalloc(2, sizeof *cts);
412 ctp = cts;
413
414 if ((ct = parse_mime (file)))
415 *ctp++ = ct;
416 } else {
417 /*
418 * message(s) are coming from a folder
419 */
420 if (!msgs.size)
421 app_msgarg(&msgs, "cur");
422 if (!folder)
423 folder = getfolder (1);
424 maildir = m_maildir (folder);
425
426 if (chdir (maildir) == NOTOK)
427 adios (maildir, "unable to change directory to");
428
429 /* read folder and create message structure */
430 if (!(mp = folder_read (folder, 1)))
431 adios (NULL, "unable to read folder %s", folder);
432
433 /* check for empty folder */
434 if (mp->nummsg == 0)
435 adios (NULL, "no messages in %s", folder);
436
437 /* parse all the message ranges/sequences and set SELECTED */
438 for (msgnum = 0; msgnum < msgs.size; msgnum++)
439 if (!m_convert (mp, msgs.msgs[msgnum]))
440 done (1);
441 seq_setprev (mp); /* set the previous-sequence */
442
443 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
444 ctp = cts;
445
446 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
447 if (is_selected(mp, msgnum)) {
448 char *msgnam;
449
450 msgnam = m_name (msgnum);
451 if ((ct = parse_mime (msgnam)))
452 *ctp++ = ct;
453 }
454 }
455 }
456
457 if (!*cts)
458 done (1);
459
460 /*
461 * You can't give more than one of these flags
462 * at a time.
463 */
464 if (showsw + listsw + storesw + cachesw > 1)
465 adios (NULL, "can only use one of -show, -list, -store, -cache at same time");
466
467 /* If no action is specified, assume -show */
468 if (!listsw && !showsw && !storesw && !cachesw)
469 showsw = 1;
470
471 userrs = 1;
472 SIGNAL (SIGQUIT, quitser);
473 SIGNAL (SIGPIPE, pipeser);
474
475 /*
476 * Get the associated umask for the relevant contents.
477 */
478 for (ctp = cts; *ctp; ctp++) {
479 struct stat st;
480
481 ct = *ctp;
482 if (type_ok (ct, 1) && !ct->c_umask) {
483 if (stat (ct->c_file, &st) != NOTOK)
484 ct->c_umask = ~(st.st_mode & 0777);
485 else
486 ct->c_umask = ~m_gmprot();
487 }
488 }
489
490 /*
491 * List the message content
492 */
493 if (listsw)
494 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, 0);
495
496 /*
497 * Store the message content
498 */
499 if (storesw) {
500 info = mhstoreinfo_create (cts, cwd, "always", autosw, verbosw);;
501 store_all_messages (info);
502 mhstoreinfo_free (info);
503 }
504
505 /* If reading from a folder, do some updating */
506 if (mp) {
507 context_replace (pfolder, folder);/* update current folder */
508 seq_setcur (mp, mp->hghsel); /* update current message */
509 seq_save (mp); /* synchronize sequences */
510 context_save (); /* save the context file */
511 }
512
513 /*
514 * Cache the message content
515 */
516 if (cachesw)
517 cache_all_messages (cts);
518
519 /*
520 * Show the message content
521 */
522 if (showsw)
523 show_all_messages (cts, 0, 0, 0);
524
525 /* Now free all the structures for the content */
526 for (ctp = cts; *ctp; ctp++)
527 free_content (*ctp);
528
529 free (cts);
530 cts = NULL;
531
532 done (0);
533 return 1;
534 }
535
536
537 static void
538 pipeser (int i)
539 {
540 if (i == SIGQUIT) {
541 fflush (stdout);
542 fprintf (stderr, "\n");
543 fflush (stderr);
544 }
545
546 done (1);
547 /* NOTREACHED */
548 }