]>
diplodocus.org Git - nmh/blob - uip/show.c
3 * show.c -- show/list messages
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.
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) \
34 #define X(sw, minchars, id) id,
35 DEFINE_SWITCH_ENUM(SHOW
);
38 #define X(sw, minchars, id) { sw, minchars, id },
39 DEFINE_SWITCH_ARRAY(SHOW
, switches
);
45 static int is_nontext(char *);
53 main (int argc
, char **argv
)
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
};
65 setlocale(LC_ALL
, "");
67 invo_name
= r1bindex (argv
[0], '/');
69 app_msgarg(&vec
, NULL
); /* placeholder, filled later with proc name */
71 /* read user profile/context */
74 if (!mh_strcasecmp (invo_name
, "next")) {
76 } else if (!mh_strcasecmp (invo_name
, "prev")) {
79 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 while ((cp
= *argp
++)) {
84 switch (smatch (++cp
, switches
)) {
86 ambigsw (cp
, switches
);
91 app_msgarg(&vec
, --cp
);
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);
101 print_version(invo_name
);
106 adios (NULL
, "only one file at a time!");
112 "usage: %s [+folder] [switches] [switches for showproc]",
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
);
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
)));
142 app_msgarg(&vec
, --cp
);
143 if (!(cp
= *argp
++) || *cp
== '-')
144 adios (NULL
, "missing argument to %s", argp
[-2]);
145 app_msgarg(&vec
, cp
);
149 if (!(showproc
= *argp
++) || *showproc
== '-')
150 adios (NULL
, "missing argument to %s", argp
[-2]);
158 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
159 adios (NULL
, "missing argument to %s", argp
[-2]);
170 if (*cp
== '+' || *cp
== '@') {
172 adios (NULL
, "only one folder at a time!");
174 folder
= pluspath (cp
);
179 app_msgarg(&msgs
, cp
);
183 if (!context_find ("path"))
184 free (path ("./", TFOLDER
));
186 if (draftsw
|| file
) {
188 adios (NULL
, "only one file at a time!");
190 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
192 app_msgarg(&vec
, file
);
197 if (!msgs
.size
&& !folder
&& mode
== SHOW
&& (cp
= getenv ("mhdraft")) && *cp
) {
199 app_msgarg(&vec
, cp
);
207 app_msgarg(&msgs
, "next");
210 app_msgarg(&msgs
, "prev");
213 app_msgarg(&msgs
, "cur");
219 folder
= getfolder (1);
220 maildir
= m_maildir (folder
);
222 if (chdir (maildir
) == NOTOK
)
223 adios (maildir
, "unable to change directory to");
225 /* read folder and create message structure */
226 if (!(mp
= folder_read (folder
)))
227 adios (NULL
, "unable to read folder %s", folder
);
229 /* check for empty folder */
231 adios (NULL
, "no messages in %s", folder
);
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
]))
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.
243 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
244 if (is_selected(mp
, msgnum
))
245 set_unseen (mp
, msgnum
);
247 seq_setprev (mp
); /* set the Previous-Sequence */
248 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
250 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
251 if (is_selected(mp
, msgnum
))
252 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
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 */
259 if (headersw
&& vec
.size
== 2)
260 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[1]);
266 * Decide which "proc" to use
272 /* check if any messages are non-text MIME messages */
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
))) {
282 /* check the file or draft for MIME */
283 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
295 if (folder
&& !draftsw
&& !file
)
296 m_putenv ("mhfolder", folder
);
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";
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";
312 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
313 /* If "mhl", then run it internally */
315 app_msgarg(&vec
, NULL
);
316 mhl (vec
.size
, vec
.msgs
);
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 */
329 * Check if a message or file contains any non-text parts
332 is_nontext (char *msgnam
)
335 unsigned char *bp
, *dp
;
337 char buf
[BUFSIZ
], name
[NAMESZ
];
340 if ((fp
= fopen (msgnam
, "r")) == NULL
)
343 for (state
= FLD
;;) {
344 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
349 * Check Content-Type field
351 if (!mh_strcasecmp (name
, TYPE_FIELD
)) {
355 cp
= add (buf
, NULL
);
356 while (state
== FLDPLUS
) {
357 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
364 for (; isspace (*bp
); bp
++)
369 for (bp
++, i
= 0;;) {
399 for (dp
= bp
; istoken (*dp
); dp
++)
406 if ((result
= (mh_strcasecmp (bp
, "plain") != 0)))
409 for (dp
++; isspace (*dp
); dp
++)
412 if ((result
= !uprf (dp
, "charset")))
414 dp
+= sizeof("charset") - 1;
415 while (isspace (*dp
))
419 while (isspace (*dp
))
422 if ((bp
= strchr(++dp
, '"')))
425 for (bp
= dp
; *bp
; bp
++)
426 if (!istoken (*bp
)) {
432 /* Default character set */
435 /* Check the character set */
436 result
= !check_charset (dp
, strlen (dp
));
438 if (!(result
= (mh_strcasecmp (bp
, "text") != 0))) {
455 * Check Content-Transfer-Encoding field
457 if (!mh_strcasecmp (name
, ENCODING_FIELD
)) {
458 cp
= add (buf
, NULL
);
459 while (state
== FLDPLUS
) {
460 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
463 for (bp
= cp
; isspace (*bp
); bp
++)
465 for (dp
= bp
; istoken (*dp
); dp
++)
468 result
= (mh_strcasecmp (bp
, "7bit")
469 && mh_strcasecmp (bp
, "8bit")
470 && mh_strcasecmp (bp
, "binary"));
481 * Just skip the rest of this header
482 * field and go to next one.
484 while (state
== FLDPLUS
)
485 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
489 * We've passed the message header,
490 * so message is just text.