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