]> diplodocus.org Git - nmh/blob - uip/mhlist.c
Specify function parameters in prototypes, mainly void.
[nmh] / uip / mhlist.c
1 /* mhlist.c -- list the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <fcntl.h>
10 #include <h/signals.h>
11 #include <h/md5.h>
12 #include <h/mts.h>
13 #include <h/tws.h>
14 #include <h/mime.h>
15 #include <h/mhparse.h>
16 #include <h/mhcachesbr.h>
17 #include <h/utils.h>
18 #include "mhfree.h"
19
20 #define MHLIST_SWITCHES \
21 X("check", 0, CHECKSW) \
22 X("nocheck", 0, NCHECKSW) \
23 X("headers", 0, HEADSW) \
24 X("noheaders", 0, NHEADSW) \
25 X("realsize", 0, SIZESW) \
26 X("norealsize", 0, NSIZESW) \
27 X("verbose", 0, VERBSW) \
28 X("noverbose", 0, NVERBSW) \
29 X("disposition", 0, DISPOSW) \
30 X("nodisposition", 0, NDISPOSW) \
31 X("file file", 0, FILESW) \
32 X("part number", 0, PARTSW) \
33 X("type content", 0, TYPESW) \
34 X("prefer content", 0, PREFERSW) \
35 X("rcache policy", 0, RCACHESW) \
36 X("wcache policy", 0, WCACHESW) \
37 X("changecur", 0, CHGSW) \
38 X("nochangecur", 0, NCHGSW) \
39 X("version", 0, VERSIONSW) \
40 X("help", 0, HELPSW) \
41 X("debug", -5, DEBUGSW) \
42
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(MHLIST);
45 #undef X
46
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(MHLIST, switches);
49 #undef X
50
51
52 /* mhcachesbr.c */
53 extern int rcachesw;
54 extern int wcachesw;
55 extern char *cache_public;
56 extern char *cache_private;
57
58 /* mhmisc.c */
59 extern int npart;
60 extern int ntype;
61 extern char *parts[NPARTS + 1];
62 extern char *types[NTYPES + 1];
63 extern int userrs;
64
65 /* mhparse.c */
66 extern char *preferred_types[];
67 extern char *preferred_subtypes[];
68 extern int npreferred;
69
70 /*
71 * This is currently needed to keep mhparse happy.
72 * This needs to be changed.
73 */
74 int debugsw = 0;
75
76 #define quitser pipeser
77
78 /* mhparse.c */
79 CT parse_mime (char *);
80
81 /* mhmisc.c */
82 int part_ok (CT);
83 int type_ok (CT, int);
84 void flush_errors (void);
85
86 /*
87 * static prototypes
88 */
89 static void pipeser (int);
90
91
92 int
93 main (int argc, char **argv)
94 {
95 int sizesw = 1, headsw = 1, chgflag = 1, verbosw = 0, dispo = 0;
96 int msgnum, *icachesw;
97 char *cp, *file = NULL, *folder = NULL;
98 char *maildir, buf[100], **argp;
99 char **arguments;
100 struct msgs_array msgs = { 0, 0, NULL };
101 struct msgs *mp = NULL;
102 CT ct, *ctp;
103
104 if (nmh_init(argv[0], 1)) { return 1; }
105
106 done=freects_done;
107
108 arguments = getarguments (invo_name, argc, argv, 1);
109 argp = arguments;
110
111 /*
112 * Parse arguments
113 */
114 while ((cp = *argp++)) {
115 if (*cp == '-') {
116 switch (smatch (++cp, switches)) {
117 case AMBIGSW:
118 ambigsw (cp, switches);
119 done (1);
120 case UNKWNSW:
121 adios (NULL, "-%s unknown", cp);
122
123 case HELPSW:
124 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
125 invo_name);
126 print_help (buf, switches, 1);
127 done (0);
128 case VERSIONSW:
129 print_version(invo_name);
130 done (0);
131
132 case RCACHESW:
133 icachesw = &rcachesw;
134 goto do_cache;
135 case WCACHESW:
136 icachesw = &wcachesw;
137 do_cache:
138 if (!(cp = *argp++) || *cp == '-')
139 adios (NULL, "missing argument to %s", argp[-2]);
140 switch (*icachesw = smatch (cp, caches)) {
141 case AMBIGSW:
142 ambigsw (cp, caches);
143 done (1);
144 case UNKWNSW:
145 adios (NULL, "%s unknown", cp);
146 default:
147 break;
148 }
149 continue;
150
151 case CHECKSW:
152 checksw++;
153 continue;
154 case NCHECKSW:
155 checksw = 0;
156 continue;
157
158 case HEADSW:
159 headsw = 1;
160 continue;
161 case NHEADSW:
162 headsw = 0;
163 continue;
164
165 case SIZESW:
166 sizesw = 1;
167 continue;
168 case NSIZESW:
169 sizesw = 0;
170 continue;
171
172 case PARTSW:
173 if (!(cp = *argp++) || *cp == '-')
174 adios (NULL, "missing argument to %s", argp[-2]);
175 if (npart >= NPARTS)
176 adios (NULL, "too many parts (starting with %s), %d max",
177 cp, NPARTS);
178 parts[npart++] = cp;
179 continue;
180
181 case TYPESW:
182 if (!(cp = *argp++) || *cp == '-')
183 adios (NULL, "missing argument to %s", argp[-2]);
184 if (ntype >= NTYPES)
185 adios (NULL, "too many types (starting with %s), %d max",
186 cp, NTYPES);
187 types[ntype++] = cp;
188 continue;
189
190 case PREFERSW:
191 if (!(cp = *argp++) || *cp == '-')
192 adios (NULL, "missing argument to %s", argp[-2]);
193 if (npreferred >= NPREFS)
194 adios (NULL, "too many preferred types (starting with %s), %d max",
195 cp, NPREFS);
196 preferred_types[npreferred] = cp;
197 cp = strchr(cp, '/');
198 if (cp) *cp++ = '\0';
199 preferred_subtypes[npreferred++] = cp;
200 continue;
201
202 case FILESW:
203 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
204 adios (NULL, "missing argument to %s", argp[-2]);
205 file = *cp == '-' ? cp : path (cp, TFILE);
206 continue;
207
208 case CHGSW:
209 chgflag++;
210 continue;
211 case NCHGSW:
212 chgflag = 0;
213 continue;
214
215 case VERBSW:
216 verbosw = 1;
217 continue;
218 case NVERBSW:
219 verbosw = 0;
220 continue;
221 case DISPOSW:
222 dispo = 1;
223 continue;
224 case NDISPOSW:
225 dispo = 0;
226 continue;
227 case DEBUGSW:
228 debugsw = 1;
229 continue;
230 }
231 }
232 if (*cp == '+' || *cp == '@') {
233 if (folder)
234 adios (NULL, "only one folder at a time!");
235 else
236 folder = pluspath (cp);
237 } else
238 app_msgarg(&msgs, cp);
239 }
240
241 /* null terminate the list of acceptable parts/types */
242 parts[npart] = NULL;
243 types[ntype] = NULL;
244
245 /* Check for public cache location */
246 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
247 cache_public = NULL;
248
249 /* Check for private cache location */
250 if (!(cache_private = context_find (nmhprivcache)))
251 cache_private = ".cache";
252 cache_private = getcpy (m_maildir (cache_private));
253
254 if (!context_find ("path"))
255 free (path ("./", TFOLDER));
256
257 if (file && msgs.size)
258 adios (NULL, "cannot specify msg and file at same time!");
259
260 /*
261 * check if message is coming from file
262 */
263 if (file) {
264 cts = mh_xcalloc(2, sizeof *cts);
265 ctp = cts;
266
267 if ((ct = parse_mime (file)))
268 *ctp++ = ct;
269 } else {
270 /*
271 * message(s) are coming from a folder
272 */
273 if (!msgs.size)
274 app_msgarg(&msgs, "cur");
275 if (!folder)
276 folder = getfolder (1);
277 maildir = m_maildir (folder);
278
279 if (chdir (maildir) == NOTOK)
280 adios (maildir, "unable to change directory to");
281
282 /* read folder and create message structure */
283 if (!(mp = folder_read (folder, 0)))
284 adios (NULL, "unable to read folder %s", folder);
285
286 /* check for empty folder */
287 if (mp->nummsg == 0)
288 adios (NULL, "no messages in %s", folder);
289
290 /* parse all the message ranges/sequences and set SELECTED */
291 for (msgnum = 0; msgnum < msgs.size; msgnum++)
292 if (!m_convert (mp, msgs.msgs[msgnum]))
293 done (1);
294 seq_setprev (mp); /* set the previous-sequence */
295
296 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
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, dispo);
336
337 /* Now free all the structures for the content */
338 for (ctp = cts; *ctp; ctp++)
339 free_content (*ctp);
340
341 free(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 if (chgflag)
348 seq_setcur (mp, mp->hghsel); /* update current message */
349 seq_save (mp); /* synchronize sequences */
350 context_save (); /* save the context file */
351 }
352
353 done (0);
354 return 1;
355 }
356
357
358 static void
359 pipeser (int i)
360 {
361 if (i == SIGQUIT) {
362 fflush (stdout);
363 fprintf (stderr, "\n");
364 fflush (stderr);
365 }
366
367 done (1);
368 /* NOTREACHED */
369 }