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