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