]>
diplodocus.org Git - nmh/blob - uip/show.c
14d4191120d69e9ff8cbea48d6403bc6a5b6257b
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 setlocale(LC_ALL
, "");
65 invo_name
= r1bindex (argv
[0], '/');
67 /* read user profile/context */
70 if (!strcasecmp (invo_name
, "next")) {
72 } else if (!strcasecmp (invo_name
, "prev")) {
75 arguments
= getarguments (invo_name
, argc
, argv
, 1);
78 while ((cp
= *argp
++)) {
80 switch (smatch (++cp
, switches
)) {
82 ambigsw (cp
, switches
);
87 app_msgarg(&vec
, --cp
);
91 snprintf (buf
, sizeof(buf
),
92 "%s [+folder] %s[switches] [switches for showproc]",
93 invo_name
, mode
== SHOW
? "[msgs] ": "");
94 print_help (buf
, switches
, 1);
97 print_version(invo_name
);
102 adios (NULL
, "only one file at a time!");
108 "usage: %s [+folder] [switches] [switches for showproc]",
114 adios (NULL
, "only one file at a time!");
115 if (!(cp
= *argp
++) || *cp
== '-')
116 adios (NULL
, "missing argument to %s", argp
[-2]);
117 file
= path (cp
, TFILE
);
128 app_msgarg(&vec
, --cp
);
129 if (!(cp
= *argp
++) || *cp
== '-')
130 adios (NULL
, "missing argument to %s", argp
[-2]);
131 app_msgarg(&vec
, getcpy (etcpath(cp
)));
138 app_msgarg(&vec
, --cp
);
139 if (!(cp
= *argp
++) || *cp
== '-')
140 adios (NULL
, "missing argument to %s", argp
[-2]);
141 app_msgarg(&vec
, cp
);
145 if (!(showproc
= *argp
++) || *showproc
== '-')
146 adios (NULL
, "missing argument to %s", argp
[-2]);
154 if (!(showmimeproc
= *argp
++) || *showmimeproc
== '-')
155 adios (NULL
, "missing argument to %s", argp
[-2]);
166 if (*cp
== '+' || *cp
== '@') {
168 adios (NULL
, "only one folder at a time!");
170 folder
= pluspath (cp
);
175 app_msgarg(&msgs
, cp
);
179 if (!context_find ("path"))
180 free (path ("./", TFOLDER
));
182 if (draftsw
|| file
) {
184 adios (NULL
, "only one file at a time!");
186 app_msgarg(&vec
, getcpy (m_draft (folder
, NULL
, 1, &isdf
)));
188 app_msgarg(&vec
, file
);
195 app_msgarg(&msgs
, "next");
198 app_msgarg(&msgs
, "prev");
201 app_msgarg(&msgs
, "cur");
207 folder
= getfolder (1);
208 maildir
= m_maildir (folder
);
210 if (chdir (maildir
) == NOTOK
)
211 adios (maildir
, "unable to change directory to");
213 /* read folder and create message structure */
214 if (!(mp
= folder_read (folder
, 1)))
215 adios (NULL
, "unable to read folder %s", folder
);
217 /* check for empty folder */
219 adios (NULL
, "no messages in %s", folder
);
221 /* parse all the message ranges/sequences and set SELECTED */
222 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
223 if (!m_convert (mp
, msgs
.msgs
[msgnum
]))
227 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
228 * since we will use that as a tag to know which messages
229 * to remove from the "unseen" sequence.
231 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
232 if (is_selected(mp
, msgnum
))
233 set_unseen (mp
, msgnum
);
235 seq_setprev (mp
); /* set the Previous-Sequence */
236 seq_setunseen (mp
, 1); /* unset the Unseen-Sequence */
238 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
239 if (is_selected(mp
, msgnum
))
240 app_msgarg(&vec
, getcpy (m_name (msgnum
)));
242 seq_setcur (mp
, mp
->hghsel
); /* update current message */
243 seq_save (mp
); /* synchronize sequences */
244 context_replace (pfolder
, folder
); /* update current folder */
245 context_save (); /* save the context file */
247 if (headersw
&& vec
.size
== 1)
248 printf ("(Message %s:%s)\n", folder
, vec
.msgs
[0]);
254 * Decide which "proc" to use
260 /* check if any messages are non-text MIME messages */
262 if (!draftsw
&& !file
) {
263 /* loop through selected messages and check for MIME */
264 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++)
265 if (is_selected (mp
, msgnum
) && is_nontext (m_name (msgnum
))) {
270 /* check the file or draft for MIME */
271 if (is_nontext (vec
.msgs
[vec
.size
- 1]))
283 if (folder
&& !draftsw
&& !file
)
284 m_putenv ("mhfolder", folder
);
286 if (strcmp (r1bindex (proc
, '/'), "mhn") == 0) {
287 /* Add "-file" if showing file or draft, */
288 if (draftsw
|| file
) {
289 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
290 vec
.msgs
[vec
.size
- 2] = "-file";
292 /* and add -show for backward compatibility */
293 app_msgarg(&vec
, "-show");
294 } else if (strcmp (r1bindex (proc
, '/'), "mhshow") == 0) {
295 /* If "mhshow", add "-file" if showing file or draft. */
296 if (draftsw
|| file
) {
297 app_msgarg(&vec
, vec
.msgs
[vec
.size
- 1]);
298 vec
.msgs
[vec
.size
- 2] = "-file";
300 } else if (strcmp (r1bindex (proc
, '/'), "mhl") == 0) {
301 /* If "mhl", then run it internally */
302 argsplit_insert(&vec
, "mhl", &program
);
303 app_msgarg(&vec
, NULL
);
304 mhl (vec
.size
, vec
.msgs
);
308 argsplit_insert(&vec
, proc
, &program
);
309 app_msgarg(&vec
, NULL
);
310 execvp (program
, vec
.msgs
);
311 adios (proc
, "unable to exec");
312 return 0; /* dead code to satisfy the compiler */
317 * Check if a message or file contains any non-text parts
320 is_nontext (char *msgnam
)
324 char buf
[BUFSIZ
], name
[NAMESZ
];
326 m_getfld_state_t gstate
= 0;
328 if ((fp
= fopen (msgnam
, "r")) == NULL
)
332 int bufsz
= sizeof buf
;
333 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
337 * Check Content-Type field
339 if (!strcasecmp (name
, TYPE_FIELD
)) {
343 cp
= add (buf
, NULL
);
344 while (state
== FLDPLUS
) {
346 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
353 for (; isspace ((unsigned char) *bp
); bp
++)
358 for (bp
++, i
= 0;;) {
388 for (dp
= bp
; istoken (*dp
); dp
++)
395 if ((result
= (strcasecmp (bp
, "plain") != 0)))
398 for (dp
++; isspace ((unsigned char) *dp
); dp
++)
401 if ((result
= !uprf (dp
, "charset")))
403 dp
+= sizeof("charset") - 1;
404 while (isspace ((unsigned char) *dp
))
408 while (isspace ((unsigned char) *dp
))
411 if ((bp
= strchr(++dp
, '"')))
414 for (bp
= dp
; *bp
; bp
++)
415 if (!istoken (*bp
)) {
421 /* Default character set */
424 /* Check the character set */
425 result
= !check_charset (dp
, strlen (dp
));
427 if (!(result
= (strcasecmp (bp
, "text") != 0))) {
438 m_getfld_state_destroy (&gstate
);
445 * Check Content-Transfer-Encoding field
447 if (!strcasecmp (name
, ENCODING_FIELD
)) {
448 cp
= add (buf
, NULL
);
449 while (state
== FLDPLUS
) {
451 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
454 for (bp
= cp
; isspace ((unsigned char) *bp
); bp
++)
456 for (dp
= bp
; istoken ((unsigned char) *dp
); dp
++)
459 result
= (strcasecmp (bp
, "7bit")
460 && strcasecmp (bp
, "8bit")
461 && strcasecmp (bp
, "binary"));
466 m_getfld_state_destroy (&gstate
);
473 * Just skip the rest of this header
474 * field and go to next one.
476 while (state
== FLDPLUS
) {
478 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
483 * We've passed the message header,
484 * so message is just text.
488 m_getfld_state_destroy (&gstate
);