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