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