]> diplodocus.org Git - nmh/blob - uip/mhshow.c
mh-sequence.man: document new '=+' and '=-' for selecting relative msgs
[nmh] / uip / mhshow.c
1
2 /*
3 * mhshow.c -- display the contents of MIME messages
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <h/mts.h>
15 #include <h/tws.h>
16 #include <h/mime.h>
17 #include <h/mhparse.h>
18 #include <h/mhcachesbr.h>
19 #include <h/utils.h>
20
21 #define MHSHOW_SWITCHES \
22 X("check", 0, CHECKSW) \
23 X("nocheck", 0, NCHECKSW) \
24 X("pause", 0, PAUSESW) \
25 X("nopause", 0, NPAUSESW) \
26 X("serialonly", 0, SERIALSW) \
27 X("noserialonly", 0, NSERIALSW) \
28 X("verbose", 0, VERBSW) \
29 X("noverbose", 0, NVERBSW) \
30 X("file file", 0, FILESW) \
31 X("form formfile", 0, FORMSW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("rcache policy", 0, RCACHESW) \
35 X("wcache policy", 0, WCACHESW) \
36 X("version", 0, VERSIONSW) \
37 X("help", 0, HELPSW) \
38 /* \
39 * switches for moreproc/mhlproc \
40 */ \
41 X("moreproc program", -4, PROGSW) \
42 X("nomoreproc", -3, NPROGSW) \
43 X("length lines", -4, LENSW) \
44 X("width columns", -4, WIDTHSW) \
45 /* \
46 * switches for debugging \
47 */ \
48 X("debug", -5, DEBUGSW) \
49
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(MHSHOW);
52 #undef X
53
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(MHSHOW, switches);
56 #undef X
57
58
59 /* mhparse.c */
60 extern char *tmp; /* directory to place temp files */
61
62 /* mhcachesbr.c */
63 extern int rcachesw;
64 extern int wcachesw;
65 extern char *cache_public;
66 extern char *cache_private;
67
68 /* mhshowsbr.c */
69 extern int pausesw;
70 extern int serialsw;
71 extern char *progsw;
72 extern int nolist;
73 extern int nomore; /* flags for moreproc/header display */
74 extern char *formsw;
75
76 /* mhmisc.c */
77 extern int npart;
78 extern int ntype;
79 extern char *parts[NPARTS + 1];
80 extern char *types[NTYPES + 1];
81 extern int userrs;
82
83 int debugsw = 0;
84 int verbosw = 0;
85
86 #define quitser pipeser
87
88 /* mhparse.c */
89 CT parse_mime (char *);
90
91 /* mhmisc.c */
92 int part_ok (CT, int);
93 int type_ok (CT, int);
94 void flush_errors (void);
95
96 /* mhshowsbr.c */
97 void show_all_messages (CT *);
98
99 /* mhfree.c */
100 extern CT *cts;
101 void freects_done (int) NORETURN;
102
103 /*
104 * static prototypes
105 */
106 static void pipeser (int);
107
108
109 int
110 main (int argc, char **argv)
111 {
112 int msgnum, *icachesw;
113 char *cp, *file = NULL, *folder = NULL;
114 char *maildir, buf[100], **argp;
115 char **arguments;
116 struct msgs_array msgs = { 0, 0, NULL };
117 struct msgs *mp = NULL;
118 CT ct, *ctp;
119 FILE *fp;
120
121 done=freects_done;
122
123 #ifdef LOCALE
124 setlocale(LC_ALL, "");
125 #endif
126 invo_name = r1bindex (argv[0], '/');
127
128 /* read user profile/context */
129 context_read();
130
131 arguments = getarguments (invo_name, argc, argv, 1);
132 argp = arguments;
133
134 /*
135 * Parse arguments
136 */
137 while ((cp = *argp++)) {
138 if (*cp == '-') {
139 switch (smatch (++cp, switches)) {
140 case AMBIGSW:
141 ambigsw (cp, switches);
142 done (1);
143 case UNKWNSW:
144 adios (NULL, "-%s unknown", cp);
145
146 case HELPSW:
147 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
148 invo_name);
149 print_help (buf, switches, 1);
150 done (0);
151 case VERSIONSW:
152 print_version(invo_name);
153 done (0);
154
155 case RCACHESW:
156 icachesw = &rcachesw;
157 goto do_cache;
158 case WCACHESW:
159 icachesw = &wcachesw;
160 do_cache:
161 if (!(cp = *argp++) || *cp == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 switch (*icachesw = smatch (cp, caches)) {
164 case AMBIGSW:
165 ambigsw (cp, caches);
166 done (1);
167 case UNKWNSW:
168 adios (NULL, "%s unknown", cp);
169 default:
170 break;
171 }
172 continue;
173
174 case CHECKSW:
175 checksw++;
176 continue;
177 case NCHECKSW:
178 checksw = 0;
179 continue;
180
181 case PAUSESW:
182 pausesw = 1;
183 continue;
184 case NPAUSESW:
185 pausesw = 0;
186 continue;
187
188 case SERIALSW:
189 serialsw = 1;
190 continue;
191 case NSERIALSW:
192 serialsw = 0;
193 continue;
194
195 case PARTSW:
196 if (!(cp = *argp++) || *cp == '-')
197 adios (NULL, "missing argument to %s", argp[-2]);
198 if (npart >= NPARTS)
199 adios (NULL, "too many parts (starting with %s), %d max",
200 cp, NPARTS);
201 parts[npart++] = cp;
202 continue;
203
204 case TYPESW:
205 if (!(cp = *argp++) || *cp == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
207 if (ntype >= NTYPES)
208 adios (NULL, "too many types (starting with %s), %d max",
209 cp, NTYPES);
210 types[ntype++] = cp;
211 continue;
212
213 case FILESW:
214 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
215 adios (NULL, "missing argument to %s", argp[-2]);
216 file = *cp == '-' ? cp : path (cp, TFILE);
217 continue;
218
219 case FORMSW:
220 if (!(cp = *argp++) || *cp == '-')
221 adios (NULL, "missing argument to %s", argp[-2]);
222 if (formsw)
223 free (formsw);
224 formsw = getcpy (etcpath (cp));
225 continue;
226
227 /*
228 * Switches for moreproc/mhlproc
229 */
230 case PROGSW:
231 if (!(progsw = *argp++) || *progsw == '-')
232 adios (NULL, "missing argument to %s", argp[-2]);
233 continue;
234 case NPROGSW:
235 nomore++;
236 continue;
237
238 case LENSW:
239 case WIDTHSW:
240 if (!(cp = *argp++) || *cp == '-')
241 adios (NULL, "missing argument to %s", argp[-2]);
242 continue;
243
244 case VERBSW:
245 verbosw = 1;
246 continue;
247 case NVERBSW:
248 verbosw = 0;
249 continue;
250 case DEBUGSW:
251 debugsw = 1;
252 continue;
253 }
254 }
255 if (*cp == '+' || *cp == '@') {
256 if (folder)
257 adios (NULL, "only one folder at a time!");
258 else
259 folder = pluspath (cp);
260 } else
261 app_msgarg(&msgs, cp);
262 }
263
264 /* null terminate the list of acceptable parts/types */
265 parts[npart] = NULL;
266 types[ntype] = NULL;
267
268 /*
269 * Check if we've specified an additional profile
270 */
271 if ((cp = getenv ("MHSHOW"))) {
272 if ((fp = fopen (cp, "r"))) {
273 readconfig ((struct node **) 0, fp, cp, 0);
274 fclose (fp);
275 } else {
276 admonish ("", "unable to read $MHSHOW profile (%s)", cp);
277 }
278 }
279
280 /*
281 * Read the standard profile setup
282 */
283 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
284 readconfig ((struct node **) 0, fp, cp, 0);
285 fclose (fp);
286 }
287
288 /* Check for public cache location */
289 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
290 cache_public = NULL;
291
292 /* Check for private cache location */
293 if (!(cache_private = context_find (nmhprivcache)))
294 cache_private = ".cache";
295 cache_private = getcpy (m_maildir (cache_private));
296
297 /*
298 * Check for storage directory. If specified,
299 * then store temporary files there. Else we
300 * store them in standard nmh directory.
301 */
302 if ((cp = context_find (nmhstorage)) && *cp)
303 tmp = concat (cp, "/", invo_name, NULL);
304 else
305 tmp = add (m_maildir (invo_name), NULL);
306
307 if (!context_find ("path"))
308 free (path ("./", TFOLDER));
309
310 if (file && msgs.size)
311 adios (NULL, "cannot specify msg and file at same time!");
312
313 /*
314 * check if message is coming from file
315 */
316 if (file) {
317 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
318 adios (NULL, "out of memory");
319 ctp = cts;
320
321 if ((ct = parse_mime (file)))
322 *ctp++ = ct;
323 } else {
324 /*
325 * message(s) are coming from a folder
326 */
327 if (!msgs.size)
328 app_msgarg(&msgs, "cur");
329 if (!folder)
330 folder = getfolder (1);
331 maildir = m_maildir (folder);
332
333 if (chdir (maildir) == NOTOK)
334 adios (maildir, "unable to change directory to");
335
336 /* read folder and create message structure */
337 if (!(mp = folder_read (folder, 1)))
338 adios (NULL, "unable to read folder %s", folder);
339
340 /* check for empty folder */
341 if (mp->nummsg == 0)
342 adios (NULL, "no messages in %s", folder);
343
344 /* parse all the message ranges/sequences and set SELECTED */
345 for (msgnum = 0; msgnum < msgs.size; msgnum++)
346 if (!m_convert (mp, msgs.msgs[msgnum]))
347 done (1);
348
349 /*
350 * Set the SELECT_UNSEEN bit for all the SELECTED messages,
351 * since we will use that as a tag to know which messages
352 * to remove from the "unseen" sequence.
353 */
354 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
355 if (is_selected(mp, msgnum))
356 set_unseen (mp, msgnum);
357
358 seq_setprev (mp); /* set the Previous-Sequence */
359 seq_setunseen (mp, 1); /* unset the Unseen-Sequence */
360
361 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
362 adios (NULL, "out of memory");
363 ctp = cts;
364
365 /*
366 * Parse all the SELECTED messages.
367 */
368 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
369 if (is_selected(mp, msgnum)) {
370 char *msgnam;
371
372 msgnam = m_name (msgnum);
373 if ((ct = parse_mime (msgnam)))
374 *ctp++ = ct;
375 }
376 }
377 }
378
379 if (!*cts)
380 done (1);
381
382 userrs = 1;
383 SIGNAL (SIGQUIT, quitser);
384 SIGNAL (SIGPIPE, pipeser);
385
386 /*
387 * Get the associated umask for the relevant contents.
388 */
389 for (ctp = cts; *ctp; ctp++) {
390 struct stat st;
391
392 ct = *ctp;
393 if (type_ok (ct, 1) && !ct->c_umask) {
394 if (stat (ct->c_file, &st) != NOTOK)
395 ct->c_umask = ~(st.st_mode & 0777);
396 else
397 ct->c_umask = ~m_gmprot();
398 }
399 }
400
401 /* If reading from a folder, do some updating */
402 if (mp) {
403 context_replace (pfolder, folder);/* update current folder */
404 seq_setcur (mp, mp->hghsel); /* update current message */
405 seq_save (mp); /* synchronize sequences */
406 context_save (); /* save the context file */
407 }
408
409 /*
410 * Show the message content
411 */
412 show_all_messages (cts);
413
414 /* Now free all the structures for the content */
415 for (ctp = cts; *ctp; ctp++)
416 free_content (*ctp);
417
418 free ((char *) cts);
419 cts = NULL;
420
421 done (0);
422 return 1;
423 }
424
425
426 static void
427 pipeser (int i)
428 {
429 if (i == SIGQUIT) {
430 fflush (stdout);
431 fprintf (stderr, "\n");
432 fflush (stderr);
433 }
434
435 done (1);
436 /* NOTREACHED */
437 }