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