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