2 ** show.c -- show/list messages
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.
13 static struct swit switches[] = {
16 #define NOCHECKMIMESW 1
23 { "form formfile", 0 },
25 { "showproc program", 0 },
27 { "showmimeproc program", 0 },
29 { "file file", -4 }, /* interface from whatnow (listproc) */
40 static int is_mime(char *);
46 char *showproc = "mhl";
47 char *showmimeproc = "mhshow";
50 main(int argc, char **argv)
52 int headersw = 1, msgp = 0;
53 int checkmime = 1, mime;
54 int vecp = 1, procp = 1, mode = SHOW, msgnum;
55 char *cp, *file = NULL, *folder = NULL, *proc;
56 char buf[BUFSIZ], **argp, **arguments;
57 char *msgs[MAXARGS], *vec[MAXARGS];
58 struct msgs *mp = NULL;
61 setlocale(LC_ALL, "");
63 invo_name = mhbasename(argv[0]);
65 /* read user profile/context */
68 if (!mh_strcasecmp(invo_name, "next")) {
70 } else if (!mh_strcasecmp(invo_name, "prev")) {
73 arguments = getarguments(invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch(++cp, switches)) {
80 ambigsw(cp, switches);
87 snprintf(buf, sizeof(buf), "%s [+folder] %s[switches] [switches for showproc]", invo_name, mode == SHOW ? "[msgs] ": "");
88 print_help(buf, switches, 1);
91 print_version(invo_name);
97 adios(NULL, "usage: %s [+folder] [switches] [switches for showproc]", invo_name);
100 adios(NULL, "only one file at a time!");
101 if (!(cp = *argp++) || *cp == '-')
102 adios(NULL, "missing argument to %s", argp[-2]);
103 file = getcpy(expanddir(cp));
115 if (!(cp = *argp++) || *cp == '-')
116 adios(NULL, "missing argument to %s",
118 vec[vecp++] = getcpy(etcpath(cp));
122 if (!(showproc = *argp++) || *showproc == '-')
123 adios(NULL, "missing argument to %s",
128 if (!(showmimeproc = *argp++) ||
129 *showmimeproc == '-')
130 adios(NULL, "missing argument to %s",
141 if (*cp == '+' || *cp == '@') {
143 adios(NULL, "only one folder at a time!");
145 folder = getcpy(expandfol(cp));
146 } else if (mode != SHOW) {
156 adios(NULL, "only one file at a time!");
164 msgs[msgp++] = seq_next;
167 msgs[msgp++] = seq_prev;
170 msgs[msgp++] = seq_cur;
176 folder = getcurfol();
178 /* read folder and create message structure */
179 if (!(mp = folder_read(folder)))
180 adios(NULL, "unable to read folder %s", folder);
182 /* check for empty folder */
184 adios(NULL, "no messages in %s", folder);
186 /* parse all the message ranges/sequences and set SELECTED */
187 for (msgnum = 0; msgnum < msgp; msgnum++)
188 if (!m_convert(mp, msgs[msgnum]))
192 ** Set the SELECT_UNSEEN bit for all the SELECTED messages,
193 ** since we will use that as a tag to know which messages
194 ** to remove from the "unseen" sequence.
196 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
197 if (is_selected(mp, msgnum))
198 set_unseen(mp, msgnum);
200 seq_setprev(mp); /* set the Previous-Sequence */
201 seq_setunseen(mp, 0); /* unset unseen seqs for shown msgs */
203 if (mp->numsel > MAXARGS - 2)
204 adios(NULL, "more than %d messages for show exec",
207 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
208 if (is_selected(mp, msgnum))
209 vec[vecp++] = getcpy(m_name(msgnum));
211 seq_setcur(mp, mp->hghsel); /* update current message */
212 seq_save(mp); /* synchronize sequences */
213 context_replace(curfolder, folder); /* update current folder */
214 context_save(); /* save the context file */
216 if (headersw && vecp == 2)
217 printf("(Message %s:%s)\n", folder, vec[1]);
225 ** Decide which "proc" to use
228 /* check if any messages are non-text MIME messages */
231 if (is_mime(vec[vecp - 1])) {
235 for (msgnum = mp->lowsel; msgnum <= mp->hghsel;
237 if (!is_selected(mp, msgnum)) {
240 snprintf(buf, sizeof buf, "%s/%d",
241 toabsdir(folder), msgnum);
251 proc = (mime) ? showmimeproc : showproc;
253 /* Special-cased because mhshow takes msg not files args. */
254 if (strcmp(mhbasename(proc), "mhshow")==0) {
256 vec[vecp] = vec[vecp - 1];
257 vec[vecp - 1] = "-file";
260 vec[vecp++] = concat("+", folder, NULL);
262 vec[0] = mhbasename(proc);
264 adios(proc, "unable to exec");
267 /* Add the path to the message names. */
269 for (msgnum = procp; msgnum < vecp; msgnum++) {
270 vec[msgnum] = concat(mp->foldpath, "/",
275 vec[0] = mhbasename(proc);
277 adios(proc, "unable to exec");
278 return 0; /* dead code to satisfy the compiler */
282 ** Check if a message or file contains MIME.
285 is_mime(char *msgnam)
288 char buf[BUFSIZ], name[NAMESZ];
291 if ((fp = fopen(msgnam, "r")) == NULL)
295 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
299 /* Does it have a Mime-Version header? */
300 if (mh_strcasecmp(name, VRSN_FIELD)==0) {
305 /* else skip the rest of this header field and go on */
306 while (state == FLDPLUS) {
307 state = m_getfld(state, name, buf, sizeof(buf),
313 /* We've passed the header, so message is just text */