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