]>
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
, *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
};
64 if (nmh_init(argv
[0], 1)) { return 1; }
66 if (!strcasecmp (invo_name
, "next")) {
68 } else if (!strcasecmp (invo_name
, "prev")) {
71 arguments
= getarguments (invo_name
, argc
, argv
, 1);
74 while ((cp
= *argp
++)) {
76 switch (smatch (++cp
, switches
)) {
78 ambigsw (cp
, switches
);
83 app_msgarg(&vec
, --cp
);
87 snprintf (buf
, sizeof(buf
),
88 "%s [+folder] %s[switches] [switches for showproc]",
89 invo_name
, mode
== SHOW
? "[msgs] ": "");
90 print_help (buf
, switches
, 1);
93 print_version(invo_name
);
98 adios (NULL
, "only one file at a time!");
104 "usage: %s [+folder] [switches] [switches for showproc]",
110 adios (NULL
, "only one file at a time!");
111 if (!(cp
= *argp
++) || *cp
== '-')
112 adios (NULL
, "missing argument to %s", argp
[-2]);
113 file
= path (cp
, TFILE
);
124 app_msgarg(&vec
, --cp
);
125 if (!(cp
= *argp
++) || *cp
== '-')
126 adios (NULL
, "missing argument to %s", argp
[-2]);
127 app_msgarg(&vec
, getcpy (etcpath(cp
)));
134 app_msgarg(&vec
, --cp
);
135 if (!(cp
= *argp
++) || *cp
== '-')
136 adios (NULL
, "missing argument to %s", argp
[-2]);
137 app_msgarg(&vec
, cp
);
141 if (!(showproc
= *argp
++) || *showproc
== '-')
142 adios (NULL
, "missing argument to %s", argp
[-2]);
150 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
151 adios (NULL
, "missing argument to %s", argp
[-2]);
162 if (*cp
== '+' || *cp
== '@') {
164 adios (NULL
, "only one folder at a time!");
166 folder
= pluspath (cp
);
171 app_msgarg(&msgs
, cp
);
175 if (!context_find ("path"))
176 free (path ("./", TFOLDER
));
178 if (draftsw
|| file
) {
180 adios (NULL
, "only one file at a time!");
182 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
184 app_msgarg(&vec
, file
);
191 app_msgarg(&msgs
, "next");
194 app_msgarg(&msgs
, "prev");
197 app_msgarg(&msgs
, "cur");
203 folder
= getfolder (1);
204 maildir
= m_maildir (folder
);
206 if (chdir (maildir
) == NOTOK
)
207 adios (maildir
, "unable to change directory to");
209 /* read folder and create message structure */
210 if (!(mp
= folder_read (folder
, 1)))
211 adios (NULL
, "unable to read folder %s", folder
);
213 /* check for empty folder */
215 adios (NULL
, "no messages in %s", folder
);
217 /* parse all the message ranges/sequences and set SELECTED */
218 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
219 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
223 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
224 * since we will use that as a tag to know which messages
225 * to remove from the "unseen" sequence.
227 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
228 if (is_selected(mp
, msgnum
))
229 set_unseen (mp
, msgnum
);
231 seq_setprev (mp
); /* set the Previous-Sequence */
232 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
234 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
235 if (is_selected(mp
, msgnum
))
236 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
238 seq_setcur (mp
, mp
->hghsel
); /* update current message */
239 seq_save (mp
); /* synchronize sequences */
240 context_replace (pfolder
, folder
); /* update current folder */
241 context_save (); /* save the context file */
243 if (headersw
&& vec
.size
== 1)
244 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
250 * Decide which "proc" to use
256 /* check if any messages are non-text MIME messages */
258 if (!draftsw
&& !file
) {
259 /* loop through selected messages and check for MIME */
260 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
261 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
266 /* check the file or draft for MIME */
267 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
279 if (folder
&& !draftsw
&& !file
)
280 m_putenv ("mhfolder", folder
);
282 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
283 /* Add "-file" if showing file or draft, */
284 if (draftsw
|| file
) {
285 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
286 vec
.msgs
[vec
.size
- 2] = "-file";
288 /* and add -show for backward compatibility */
289 app_msgarg(&vec
, "-show");
290 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
291 /* If "mhshow", add "-file" if showing file or draft. */
292 if (draftsw
|| file
) {
293 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
294 vec
.msgs
[vec
.size
- 2] = "-file";
296 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
297 /* If "mhl", then run it internally */
298 argsplit_insert(&vec
, "mhl", &program
);
299 app_msgarg(&vec
, NULL
);
300 mhl (vec
.size
, vec
.msgs
);
304 argsplit_insert(&vec
, proc
, &program
);
305 app_msgarg(&vec
, NULL
);
306 execvp (program
, vec
.msgs
);
307 adios (proc
, "unable to exec");
308 return 0; /* dead code to satisfy the compiler */
313 * Check if a message or file contains any non-text parts
316 is_nontext (char *msgnam
)
320 char buf
[BUFSIZ
], name
[NAMESZ
];
322 m_getfld_state_t gstate
= 0;
324 if ((fp
= fopen (msgnam
, "r")) == NULL
)
328 int bufsz
= sizeof buf
;
329 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
333 * Check Content-Type field
335 if (!strcasecmp (name
, TYPE_FIELD
)) {
339 cp
= add (buf
, NULL
);
340 while (state
== FLDPLUS
) {
342 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
349 for (; isspace ((unsigned char) *bp
); bp
++)
354 for (bp
++, i
= 0;;) {
384 for (dp
= bp
; istoken (*dp
); dp
++)
391 if ((result
= (strcasecmp (bp
, "plain") != 0)))
394 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
397 if ((result
= !uprf (dp
, "charset")))
399 dp
+= sizeof("charset") - 1;
400 while (isspace ((unsigned char) *dp
))
404 while (isspace ((unsigned char) *dp
))
407 if ((bp
= strchr(++dp
, '"')))
410 for (bp
= dp
; *bp
; bp
++)
411 if (!istoken (*bp
)) {
417 /* Default character set */
420 /* Check the character set */
421 result
= !check_charset (dp
, strlen (dp
));
423 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
434 m_getfld_state_destroy (&gstate
);
441 * Check Content-Transfer-Encoding field
443 if (!strcasecmp (name
, ENCODING_FIELD
)) {
444 cp
= add (buf
, NULL
);
445 while (state
== FLDPLUS
) {
447 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
450 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
452 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
455 result
= (strcasecmp (bp
, "7bit")
456 && strcasecmp (bp
, "8bit")
457 && strcasecmp (bp
, "binary"));
462 m_getfld_state_destroy (&gstate
);
469 * Just skip the rest of this header
470 * field and go to next one.
472 while (state
== FLDPLUS
) {
474 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
479 * We've passed the message header,
480 * so message is just text.
484 m_getfld_state_destroy (&gstate
);