]> diplodocus.org Git - nmh/blob - uip/show.c
vector.c: Move interface to own file.
[nmh] / uip / show.c
1 /* show.c -- show/list 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/arglist.h"
13 #include "sbr/error.h"
14 #include "h/mime.h"
15 #include "h/done.h"
16 #include "h/utils.h"
17 #include "sbr/m_maildir.h"
18
19 #define SHOW_SWITCHES \
20 X("checkmime", 0, CHECKMIMESW) \
21 X("nocheckmime", 0, NOCHECKMIMESW) \
22 X("header", 0, HEADSW) \
23 X("noheader", 0, NHEADSW) \
24 X("form formfile", 0, FORMSW) \
25 X("moreproc program", 0, PROGSW) \
26 X("nomoreproc", 0, NPROGSW) \
27 X("length lines", 0, LENSW) \
28 X("width columns", 0, WIDTHSW) \
29 X("showproc program", 0, SHOWSW) \
30 X("showmimeproc program", 0, SHOWMIMESW) \
31 X("noshowproc", 0, NSHOWSW) \
32 X("draft", 0, DRFTSW) \
33 X("file file", -4, FILESW) /* interface from showfile */ \
34 X("fmtproc program", 0, FMTPROCSW) \
35 X("nofmtproc", 0, NFMTPROCSW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 /* \
39 * switches for mhlproc \
40 */ \
41 X("concat", 0, CONCATSW) \
42 X("noconcat", 0, NCONCATSW) \
43 /* \
44 * switches for mhshow \
45 */ \
46 X("part number", 0, PARTSW) \
47 X("type content", 0, TYPESW) \
48 X("prefer content", 0, PREFERSW) \
49 X("markform file", 0, MARKFORMSW) \
50 X("rcache policy", 0, RCACHESW) \
51 X("wcache policy", 0, WCACHESW) \
52
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(SHOW);
55 #undef X
56
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(SHOW, switches);
59 #undef X
60
61 /*
62 * static prototypes
63 */
64 static int is_nontext(char *);
65
66 #define SHOW 0
67 #define NEXT 1
68 #define PREV 2
69
70
71 int
72 main (int argc, char **argv)
73 {
74 bool draftsw = false;
75 bool headersw = true;
76 bool nshow = false;
77 bool checkmime = true;
78 bool mime = false;
79 int isdf = 0, mode = SHOW, msgnum;
80 char *cp, *maildir, *file = NULL, *folder = NULL, *proc, *program;
81 char buf[BUFSIZ], **argp, **arguments;
82 struct msgs *mp = NULL;
83 struct msgs_array msgs = { 0, 0, NULL };
84 struct msgs_array vec = { 0, 0, NULL }, non_mhl_vec = { 0, 0, NULL };
85
86 if (nmh_init(argv[0], true, true)) { return 1; }
87
88 if (!strcasecmp (invo_name, "next")) {
89 mode = NEXT;
90 } else if (!strcasecmp (invo_name, "prev")) {
91 mode = PREV;
92 }
93 arguments = getarguments (invo_name, argc, argv, 1);
94 argp = arguments;
95
96 while ((cp = *argp++)) {
97 if (*cp == '-') {
98 switch (smatch (++cp, switches)) {
99 case AMBIGSW:
100 ambigsw (cp, switches);
101 done (1);
102
103 case HEADSW:
104 headersw = true;
105 goto non_mhl_switches;
106 case NHEADSW:
107 headersw = false;
108 /* FALLTHRU */
109 case CONCATSW:
110 case NCONCATSW:
111 non_mhl_switches:
112 /* mhl can't handle these, so keep them separate. */
113 app_msgarg(&non_mhl_vec, --cp);
114 continue;
115
116 case UNKWNSW:
117 case NPROGSW:
118 case NFMTPROCSW:
119 app_msgarg(&vec, --cp);
120 continue;
121
122 case HELPSW:
123 snprintf (buf, sizeof(buf),
124 "%s [+folder] %s[switches] [switches for showproc]",
125 invo_name, mode == SHOW ? "[msgs] ": "");
126 print_help (buf, switches, 1);
127 done (0);
128 case VERSIONSW:
129 print_version(invo_name);
130 done (0);
131
132 case DRFTSW:
133 if (file)
134 die("only one file at a time!");
135 draftsw = true;
136 if (mode == SHOW)
137 continue;
138 usage:
139 die( "usage: %s [+folder] [switches] [switches for showproc]",
140 invo_name);
141 case FILESW:
142 if (mode != SHOW)
143 goto usage;
144 if (draftsw || file)
145 die("only one file at a time!");
146 if (!(cp = *argp++) || *cp == '-')
147 die("missing argument to %s", argp[-2]);
148 file = path (cp, TFILE);
149 continue;
150
151 case FORMSW:
152 app_msgarg(&vec, --cp);
153 if (!(cp = *argp++) || *cp == '-')
154 die("missing argument to %s", argp[-2]);
155 app_msgarg(&vec, mh_xstrdup(etcpath(cp)));
156 continue;
157
158 case PROGSW:
159 case LENSW:
160 case WIDTHSW:
161 case FMTPROCSW:
162 case PARTSW:
163 case TYPESW:
164 case PREFERSW:
165 case MARKFORMSW:
166 case RCACHESW:
167 case WCACHESW:
168 app_msgarg(&vec, --cp);
169 if (!(cp = *argp++) || *cp == '-')
170 die("missing argument to %s", argp[-2]);
171 app_msgarg(&vec, cp);
172 continue;
173
174 case SHOWSW:
175 if (!(showproc = *argp++) || *showproc == '-')
176 die("missing argument to %s", argp[-2]);
177 nshow = false;
178 continue;
179 case NSHOWSW:
180 nshow = true;
181 continue;
182
183 case SHOWMIMESW:
184 if (!(showmimeproc = *argp++) || *showmimeproc == '-')
185 die("missing argument to %s", argp[-2]);
186 nshow = false;
187 continue;
188 case CHECKMIMESW:
189 checkmime = true;
190 continue;
191 case NOCHECKMIMESW:
192 checkmime = false;
193 continue;
194 }
195 }
196 if (*cp == '+' || *cp == '@') {
197 if (folder)
198 die("only one folder at a time!");
199 folder = pluspath (cp);
200 } else {
201 if (mode != SHOW)
202 goto usage;
203 app_msgarg(&msgs, cp);
204 }
205 }
206
207 if (!context_find ("path"))
208 free (path ("./", TFOLDER));
209
210 if (draftsw || file) {
211 if (msgs.size)
212 die("only one file at a time!");
213 if (draftsw)
214 app_msgarg(&vec, mh_xstrdup(m_draft(folder, NULL, 1, &isdf)));
215 else
216 app_msgarg(&vec, file);
217 headersw = false;
218 goto go_to_it;
219 }
220
221 if (!msgs.size) {
222 switch (mode) {
223 case NEXT:
224 app_msgarg(&msgs, "next");
225 break;
226 case PREV:
227 app_msgarg(&msgs, "prev");
228 break;
229 default:
230 app_msgarg(&msgs, "cur");
231 break;
232 }
233 }
234
235 if (!folder)
236 folder = getfolder (1);
237 maildir = m_maildir (folder);
238
239 if (chdir (maildir) == NOTOK)
240 adios (maildir, "unable to change directory to");
241
242 /* read folder and create message structure */
243 if (!(mp = folder_read (folder, 1)))
244 die("unable to read folder %s", folder);
245
246 /* check for empty folder */
247 if (mp->nummsg == 0)
248 die("no messages in %s", folder);
249
250 /* parse all the message ranges/sequences and set SELECTED */
251 for (msgnum = 0; msgnum < msgs.size; msgnum++)
252 if (!m_convert (mp, msgs.msgs[msgnum]))
253 done (1);
254
255 /*
256 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
257 * since we will use that as a tag to know which messages
258 * to remove from the "unseen" sequence.
259 */
260 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
261 if (is_selected(mp, msgnum))
262 set_unseen (mp, msgnum);
263
264 seq_setprev (mp); /* set the Previous-Sequence */
265 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
266
267 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
268 if (is_selected(mp, msgnum))
269 app_msgarg(&vec, mh_xstrdup(m_name (msgnum)));
270
271 seq_setcur (mp, mp->hghsel); /* update current message */
272 seq_save (mp); /* synchronize sequences */
273 context_replace (pfolder, folder); /* update current folder */
274 context_save (); /* save the context file */
275
276 go_to_it: ;
277
278 /*
279 * Decide which "proc" to use
280 */
281 if (nshow) {
282 proc = catproc;
283 } else {
284 /* check if any messages are non-text MIME messages */
285 if (! mime && checkmime) {
286 if (!draftsw && !file) {
287 /* loop through selected messages and check for MIME */
288 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
289 if (is_selected (mp, msgnum) && is_nontext (m_name (msgnum))) {
290 mime = true;
291 break;
292 }
293 } else {
294 /* check the file or draft for MIME */
295 if (is_nontext (vec.msgs[vec.size - 1]))
296 mime = true;
297 }
298 }
299
300 /* Set the "proc" */
301 if (mime)
302 proc = showmimeproc;
303 else
304 proc = showproc;
305 }
306
307 if (folder && !draftsw && !file)
308 setenv("mhfolder", folder, 1);
309
310 if (strcmp (r1bindex (proc, '/'), "cat") == 0) {
311
312 if (headersw && vec.size == 1)
313 printf ("(Message %s:%s)\n", folder, vec.msgs[0]);
314
315 } else if (strcmp (r1bindex (proc, '/'), "mhl") == 0) {
316
317 if (headersw && vec.size == 1)
318 printf ("(Message %s:%s)\n", folder, vec.msgs[0]);
319
320 /* If "mhl", then run it internally */
321 argsplit_insert(&vec, "mhl", &program);
322 app_msgarg(&vec, NULL);
323 mhl (vec.size, vec.msgs);
324 done (0);
325
326 } else {
327 int i;
328 char **mp;
329
330 for (i = 0, mp = non_mhl_vec.msgs; i < non_mhl_vec.size; ++i, ++mp) {
331 if (draftsw || file) {
332 /* Insert the switch before the filename. */
333 app_msgarg(&vec, vec.msgs[vec.size - 1]);
334 vec.msgs[vec.size - 2] = *mp;
335 } else {
336 app_msgarg(&vec, *mp);
337 }
338 }
339
340 if (strcmp (r1bindex (proc, '/'), "mhn") == 0) {
341 /* Add "-file" if showing file or draft, */
342 if (draftsw || file) {
343 app_msgarg(&vec, vec.msgs[vec.size - 1]);
344 vec.msgs[vec.size - 2] = "-file";
345 }
346 /* and add -show for backward compatibility */
347 app_msgarg(&vec, "-show");
348 } else if (strcmp (r1bindex (proc, '/'), "mhshow") == 0) {
349 /* If "mhshow", add "-file" if showing file or draft. */
350 if (draftsw || file) {
351 app_msgarg(&vec, vec.msgs[vec.size - 1]);
352 vec.msgs[vec.size - 2] = "-file";
353 }
354 } else {
355 if (headersw && vec.size == 1)
356 printf ("(Message %s:%s)\n", folder, vec.msgs[0]);
357 }
358 }
359
360 fflush (stdout);
361
362 argsplit_insert(&vec, proc, &program);
363 app_msgarg(&vec, NULL);
364 execvp (program, vec.msgs);
365 adios (proc, "unable to exec");
366 return 0; /* dead code to satisfy the compiler */
367 }
368
369
370 /*
371 * Check if a message or file contains any non-text parts
372 */
373 static int
374 is_nontext (char *msgnam)
375 {
376 int result, state;
377 char *bp, *dp, *cp;
378 char buf[NMH_BUFSIZ], name[NAMESZ];
379 FILE *fp;
380 m_getfld_state_t gstate;
381
382 if ((fp = fopen (msgnam, "r")) == NULL)
383 return 0;
384 gstate = m_getfld_state_init(fp);
385 for (;;) {
386 int bufsz = sizeof buf;
387 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
388 case FLD:
389 case FLDPLUS:
390 /*
391 * Check Content-Type field
392 */
393 if (!strcasecmp (name, TYPE_FIELD)) {
394 int passno;
395 char c;
396
397 cp = mh_xstrdup(buf);
398 while (state == FLDPLUS) {
399 bufsz = sizeof buf;
400 state = m_getfld2(&gstate, name, buf, &bufsz);
401 cp = add (buf, cp);
402 }
403 bp = cp;
404 passno = 1;
405
406 again:
407 for (; isspace ((unsigned char) *bp); bp++)
408 continue;
409 if (*bp == '(') {
410 int i;
411
412 for (bp++, i = 0;;) {
413 switch (*bp++) {
414 case '\0':
415 invalid:
416 result = 0;
417 goto out;
418 case '\\':
419 if (*bp++ == '\0')
420 goto invalid;
421 continue;
422 case '(':
423 i++;
424 continue;
425 default:
426 continue;
427 case ')':
428 if (--i < 0)
429 break;
430 continue;
431 }
432 break;
433 }
434 }
435 if (passno == 2) {
436 if (*bp != '/')
437 goto invalid;
438 bp++;
439 passno = 3;
440 goto again;
441 }
442 for (dp = bp; istoken (*dp); dp++)
443 continue;
444 c = *dp;
445 *dp = '\0';
446 if (!*bp)
447 goto invalid;
448 if (passno > 1) {
449 if ((result = (strcasecmp (bp, "plain") != 0)))
450 goto out;
451 *dp = c;
452 for (dp++; isspace ((unsigned char) *dp); dp++)
453 continue;
454 if (*dp) {
455 if ((result = !uprf (dp, "charset")))
456 goto out;
457 dp += LEN("charset");
458 while (isspace ((unsigned char) *dp))
459 dp++;
460 if (*dp++ != '=')
461 goto invalid;
462 while (isspace ((unsigned char) *dp))
463 dp++;
464 if (*dp == '"') {
465 if ((bp = strchr(++dp, '"')))
466 *bp = '\0';
467 } else {
468 for (bp = dp; *bp; bp++)
469 if (!istoken (*bp)) {
470 *bp = '\0';
471 break;
472 }
473 }
474 } else {
475 /* Default character set */
476 dp = "US-ASCII";
477 }
478 /* Check the character set */
479 result = !check_charset (dp, strlen (dp));
480 } else {
481 if (!(result = (strcasecmp (bp, "text") != 0))) {
482 *dp = c;
483 bp = dp;
484 passno = 2;
485 goto again;
486 }
487 }
488 out:
489 free (cp);
490 if (result) {
491 fclose (fp);
492 m_getfld_state_destroy (&gstate);
493 return result;
494 }
495 break;
496 }
497
498 /*
499 * Check Content-Transfer-Encoding field
500 */
501 if (!strcasecmp (name, ENCODING_FIELD)) {
502 cp = mh_xstrdup(buf);
503 while (state == FLDPLUS) {
504 bufsz = sizeof buf;
505 state = m_getfld2(&gstate, name, buf, &bufsz);
506 cp = add (buf, cp);
507 }
508 for (bp = cp; isspace ((unsigned char) *bp); bp++)
509 continue;
510 for (dp = bp; istoken ((unsigned char) *dp); dp++)
511 continue;
512 *dp = '\0';
513 result = (strcasecmp (bp, "7bit")
514 && strcasecmp (bp, "8bit")
515 && strcasecmp (bp, "binary"));
516
517 free (cp);
518 if (result) {
519 fclose (fp);
520 m_getfld_state_destroy (&gstate);
521 return result;
522 }
523 break;
524 }
525
526 /*
527 * Just skip the rest of this header
528 * field and go to next one.
529 */
530 while (state == FLDPLUS) {
531 bufsz = sizeof buf;
532 state = m_getfld2(&gstate, name, buf, &bufsz);
533 }
534 break;
535
536 /*
537 * We've passed the message header,
538 * so message is just text.
539 */
540 default:
541 fclose (fp);
542 m_getfld_state_destroy (&gstate);
543 return 0;
544 }
545 }
546 }