]> diplodocus.org Git - nmh/blob - uip/mhlist.c
picksbr.c: Specify parameters of nexus's n_action function pointer.
[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 folder = pluspath (cp);
231 } else
232 app_msgarg(&msgs, cp);
233 }
234
235 /* null terminate the list of acceptable parts/types */
236 parts[npart] = NULL;
237 types[ntype] = NULL;
238
239 /* Check for public cache location */
240 if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
241 cache_public = NULL;
242
243 /* Check for private cache location */
244 if (!(cache_private = context_find (nmhprivcache)))
245 cache_private = ".cache";
246 cache_private = getcpy (m_maildir (cache_private));
247
248 if (!context_find ("path"))
249 free (path ("./", TFOLDER));
250
251 if (file && msgs.size)
252 adios (NULL, "cannot specify msg and file at same time!");
253
254 /*
255 * check if message is coming from file
256 */
257 if (file) {
258 cts = mh_xcalloc(2, sizeof *cts);
259 ctp = cts;
260
261 if ((ct = parse_mime (file)))
262 *ctp++ = ct;
263 } else {
264 /*
265 * message(s) are coming from a folder
266 */
267 if (!msgs.size)
268 app_msgarg(&msgs, "cur");
269 if (!folder)
270 folder = getfolder (1);
271 maildir = m_maildir (folder);
272
273 if (chdir (maildir) == NOTOK)
274 adios (maildir, "unable to change directory to");
275
276 /* read folder and create message structure */
277 if (!(mp = folder_read (folder, 0)))
278 adios (NULL, "unable to read folder %s", folder);
279
280 /* check for empty folder */
281 if (mp->nummsg == 0)
282 adios (NULL, "no messages in %s", folder);
283
284 /* parse all the message ranges/sequences and set SELECTED */
285 for (msgnum = 0; msgnum < msgs.size; msgnum++)
286 if (!m_convert (mp, msgs.msgs[msgnum]))
287 done (1);
288 seq_setprev (mp); /* set the previous-sequence */
289
290 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
291 ctp = cts;
292
293 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
294 if (is_selected(mp, msgnum)) {
295 char *msgnam;
296
297 msgnam = m_name (msgnum);
298 if ((ct = parse_mime (msgnam)))
299 *ctp++ = ct;
300 }
301 }
302 }
303
304 if (!*cts)
305 done (1);
306
307 userrs = 1;
308 SIGNAL (SIGQUIT, quitser);
309 SIGNAL (SIGPIPE, pipeser);
310
311 /*
312 * Get the associated umask for the relevant contents.
313 */
314 for (ctp = cts; *ctp; ctp++) {
315 struct stat st;
316
317 ct = *ctp;
318 if (type_ok (ct, 1) && !ct->c_umask) {
319 if (stat (ct->c_file, &st) != NOTOK)
320 ct->c_umask = ~(st.st_mode & 0777);
321 else
322 ct->c_umask = ~m_gmprot();
323 }
324 }
325
326 /*
327 * List the message content
328 */
329 list_all_messages (cts, headsw, sizesw, verbosw, debugsw, dispo);
330
331 /* Now free all the structures for the content */
332 for (ctp = cts; *ctp; ctp++)
333 free_content (*ctp);
334
335 free(cts);
336 cts = NULL;
337
338 /* If reading from a folder, do some updating */
339 if (mp) {
340 context_replace (pfolder, folder);/* update current folder */
341 if (chgflag)
342 seq_setcur (mp, mp->hghsel); /* update current message */
343 seq_save (mp); /* synchronize sequences */
344 context_save (); /* save the context file */
345 }
346
347 done (0);
348 return 1;
349 }
350
351
352 static void
353 pipeser (int i)
354 {
355 if (i == SIGQUIT) {
356 fflush (stdout);
357 fprintf (stderr, "\n");
358 fflush (stderr);
359 }
360
361 done (1);
362 /* NOTREACHED */
363 }