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