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