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