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