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