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