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