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