]> diplodocus.org Git - nmh/blob - uip/mhlist.c
Convert the MIME content cache switches over to the smatch() New World Order.
[nmh] / uip / mhlist.c
1
2 /*
3 * mhlist.c -- list 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 MHLIST_SWITCHES \
24 X("check", 0, CHECKSW) \
25 X("nocheck", 0, NCHECKSW) \
26 X("headers", 0, HEADSW) \
27 X("noheaders", 0, NHEADSW) \
28 X("realsize", 0, SIZESW) \
29 X("norealsize", 0, NSIZESW) \
30 X("verbose", 0, VERBSW) \
31 X("noverbose", 0, NVERBSW) \
32 X("file file", 0, FILESW) \
33 X("part number", 0, PARTSW) \
34 X("type content", 0, TYPESW) \
35 X("rcache policy", 0, RCACHESW) \
36 X("wcache policy", 0, WCACHESW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
39 X("debug", -5, DEBUGSW) \
40
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(MHLIST);
43 #undef X
44
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(MHLIST, switches);
47 #undef X
48
49
50 /* mhparse.c */
51 extern char *tmp; /* directory to place temp files */
52
53 /* mhcachesbr.c */
54 extern int rcachesw;
55 extern int wcachesw;
56 extern char *cache_public;
57 extern char *cache_private;
58
59 /* mhmisc.c */
60 extern int npart;
61 extern int ntype;
62 extern char *parts[NPARTS + 1];
63 extern char *types[NTYPES + 1];
64 extern int userrs;
65
66 /*
67 * This is currently needed to keep mhparse happy.
68 * This needs to be changed.
69 */
70 pid_t xpid = 0;
71
72 int debugsw = 0;
73 int verbosw = 0;
74
75 #define quitser pipeser
76
77 /* mhparse.c */
78 CT parse_mime (char *);
79
80 /* mhmisc.c */
81 int part_ok (CT, int);
82 int type_ok (CT, int);
83 void flush_errors (void);
84
85 /* mhlistsbr.c */
86 void list_all_messages (CT *, int, int, int, int);
87
88 /* mhfree.c */
89 void free_content (CT);
90 extern CT *cts;
91 void freects_done (int) NORETURN;
92
93 /*
94 * static prototypes
95 */
96 static void pipeser (int);
97
98
99 int
100 main (int argc, char **argv)
101 {
102 int sizesw = 1, headsw = 1;
103 int msgnum, *icachesw;
104 char *cp, *file = NULL, *folder = NULL;
105 char *maildir, buf[100], **argp;
106 char **arguments;
107 struct msgs_array msgs = { 0, 0, NULL };
108 struct msgs *mp = NULL;
109 CT ct, *ctp;
110
111 done=freects_done;
112
113 #ifdef LOCALE
114 setlocale(LC_ALL, "");
115 #endif
116 invo_name = r1bindex (argv[0], '/');
117
118 /* read user profile/context */
119 context_read();
120
121 arguments = getarguments (invo_name, argc, argv, 1);
122 argp = arguments;
123
124 /*
125 * Parse arguments
126 */
127 while ((cp = *argp++)) {
128 if (*cp == '-') {
129 switch (smatch (++cp, switches)) {
130 case AMBIGSW:
131 ambigsw (cp, switches);
132 done (1);
133 case UNKWNSW:
134 adios (NULL, "-%s unknown", cp);
135
136 case HELPSW:
137 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
138 invo_name);
139 print_help (buf, switches, 1);
140 done (0);
141 case VERSIONSW:
142 print_version(invo_name);
143 done (0);
144
145 case RCACHESW:
146 icachesw = &rcachesw;
147 goto do_cache;
148 case WCACHESW:
149 icachesw = &wcachesw;
150 do_cache:
151 if (!(cp = *argp++) || *cp == '-')
152 adios (NULL, "missing argument to %s", argp[-2]);
153 switch (*icachesw = smatch (cp, caches)) {
154 case AMBIGSW:
155 ambigsw (cp, caches);
156 done (1);
157 case UNKWNSW:
158 adios (NULL, "%s unknown", cp);
159 default:
160 break;
161 }
162 continue;
163
164 case CHECKSW:
165 checksw++;
166 continue;
167 case NCHECKSW:
168 checksw = 0;
169 continue;
170
171 case HEADSW:
172 headsw = 1;
173 continue;
174 case NHEADSW:
175 headsw = 0;
176 continue;
177
178 case SIZESW:
179 sizesw = 1;
180 continue;
181 case NSIZESW:
182 sizesw = 0;
183 continue;
184
185 case PARTSW:
186 if (!(cp = *argp++) || *cp == '-')
187 adios (NULL, "missing argument to %s", argp[-2]);
188 if (npart >= NPARTS)
189 adios (NULL, "too many parts (starting with %s), %d max",
190 cp, NPARTS);
191 parts[npart++] = cp;
192 continue;
193
194 case TYPESW:
195 if (!(cp = *argp++) || *cp == '-')
196 adios (NULL, "missing argument to %s", argp[-2]);
197 if (ntype >= NTYPES)
198 adios (NULL, "too many types (starting with %s), %d max",
199 cp, NTYPES);
200 types[ntype++] = cp;
201 continue;
202
203 case FILESW:
204 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
205 adios (NULL, "missing argument to %s", argp[-2]);
206 file = *cp == '-' ? cp : path (cp, TFILE);
207 continue;
208
209 case VERBSW:
210 verbosw = 1;
211 continue;
212 case NVERBSW:
213 verbosw = 0;
214 continue;
215 case DEBUGSW:
216 debugsw = 1;
217 continue;
218 }
219 }
220 if (*cp == '+' || *cp == '@') {
221 if (folder)
222 adios (NULL, "only one folder at a time!");
223 else
224 folder = pluspath (cp);
225 } else
226 app_msgarg(&msgs, cp);
227 }
228
229 /* null terminate the list of acceptable parts/types */
230 parts[npart] = NULL;
231 types[ntype] = NULL;
232
233 /* Check for public cache location */
234 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
235 cache_public = NULL;
236
237 /* Check for private cache location */
238 if (!(cache_private = context_find (nmhprivcache)))
239 cache_private = ".cache";
240 cache_private = getcpy (m_maildir (cache_private));
241
242 /*
243 * Check for storage directory. If specified,
244 * then store temporary files there. Else we
245 * store them in standard nmh directory.
246 */
247 if ((cp = context_find (nmhstorage)) && *cp)
248 tmp = concat (cp, "/", invo_name, NULL);
249 else
250 tmp = add (m_maildir (invo_name), NULL);
251
252 if (!context_find ("path"))
253 free (path ("./", TFOLDER));
254
255 if (file && msgs.size)
256 adios (NULL, "cannot specify msg and file at same time!");
257
258 /*
259 * check if message is coming from file
260 */
261 if (file) {
262 if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
263 adios (NULL, "out of memory");
264 ctp = cts;
265
266 if ((ct = parse_mime (file)))
267 *ctp++ = ct;
268 } else {
269 /*
270 * message(s) are coming from a folder
271 */
272 if (!msgs.size)
273 app_msgarg(&msgs, "cur");
274 if (!folder)
275 folder = getfolder (1);
276 maildir = m_maildir (folder);
277
278 if (chdir (maildir) == NOTOK)
279 adios (maildir, "unable to change directory to");
280
281 /* read folder and create message structure */
282 if (!(mp = folder_read (folder)))
283 adios (NULL, "unable to read folder %s", folder);
284
285 /* check for empty folder */
286 if (mp->nummsg == 0)
287 adios (NULL, "no messages in %s", folder);
288
289 /* parse all the message ranges/sequences and set SELECTED */
290 for (msgnum = 0; msgnum < msgs.size; msgnum++)
291 if (!m_convert (mp, msgs.msgs[msgnum]))
292 done (1);
293 seq_setprev (mp); /* set the previous-sequence */
294
295 if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
296 adios (NULL, "out of memory");
297 ctp = cts;
298
299 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
300 if (is_selected(mp, msgnum)) {
301 char *msgnam;
302
303 msgnam = m_name (msgnum);
304 if ((ct = parse_mime (msgnam)))
305 *ctp++ = ct;
306 }
307 }
308 }
309
310 if (!*cts)
311 done (1);
312
313 userrs = 1;
314 SIGNAL (SIGQUIT, quitser);
315 SIGNAL (SIGPIPE, pipeser);
316
317 /*
318 * Get the associated umask for the relevant contents.
319 */
320 for (ctp = cts; *ctp; ctp++) {
321 struct stat st;
322
323 ct = *ctp;
324 if (type_ok (ct, 1) && !ct->c_umask) {
325 if (stat (ct->c_file, &st) != NOTOK)
326 ct->c_umask = ~(st.st_mode & 0777);
327 else
328 ct->c_umask = ~m_gmprot();
329 }
330 }
331
332 /*
333 * List the message content
334 */
335 list_all_messages (cts, headsw, sizesw, verbosw, debugsw);
336
337 /* Now free all the structures for the content */
338 for (ctp = cts; *ctp; ctp++)
339 free_content (*ctp);
340
341 free ((char *) cts);
342 cts = NULL;
343
344 /* If reading from a folder, do some updating */
345 if (mp) {
346 context_replace (pfolder, folder);/* update current folder */
347 seq_setcur (mp, mp->hghsel); /* update current message */
348 seq_save (mp); /* synchronize sequences */
349 context_save (); /* save the context file */
350 }
351
352 done (0);
353 return 1;
354 }
355
356
357 static void
358 pipeser (int i)
359 {
360 if (i == SIGQUIT) {
361 unlink ("core");
362 fflush (stdout);
363 fprintf (stderr, "\n");
364 fflush (stderr);
365 }
366
367 done (1);
368 /* NOTREACHED */
369 }