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