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