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