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