Renamed -version switch to -Version to remove the conflict with -verbose.
[mmh] / uip / mhlist.c
1 /*
2 ** mhlist.c -- list the contents of MIME messages
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <h/tws.h>
15 #include <h/mime.h>
16 #include <h/mhparse.h>
17 #include <h/utils.h>
18
19 static struct swit switches[] = {
20 #define VERBSW  0
21         { "verbose", 0 },
22 #define NVERBSW  1
23         { "noverbose", 0 },
24 #define FILESW  2  /* interface from show */
25         { "file file", 0 },
26 #define PARTSW  3
27         { "part number", 0 },
28 #define TYPESW  4
29         { "type content", 0 },
30 #define VERSIONSW  5
31         { "Version", 0 },
32 #define HELPSW  6
33         { "help", 0 },
34 #define DEBUGSW  7
35         { "debug", -5 },
36         { NULL, 0 }
37 };
38
39
40 /* mhparse.c */
41 extern char *tmp;  /* directory to place temp files */
42
43 /* mhmisc.c */
44 extern int npart;
45 extern int ntype;
46 extern char *parts[NPARTS + 1];
47 extern char *types[NTYPES + 1];
48 extern int userrs;
49
50 /*
51 ** This is currently needed to keep mhparse happy.
52 ** This needs to be changed.
53 */
54 pid_t xpid  = 0;
55
56 int debugsw = 0;
57 int verbosw = 0;
58
59 #define quitser pipeser
60
61 /* mhparse.c */
62 CT parse_mime(char *);
63
64 /* mhmisc.c */
65 int part_ok(CT, int);
66 int type_ok(CT, int);
67 void set_endian(void);
68 void flush_errors(void);
69
70 /* mhlistsbr.c */
71 void list_all_messages(CT *, int, int);
72
73 /* mhfree.c */
74 void free_content(CT);
75 extern CT *cts;
76 void freects_done(int) NORETURN;
77
78 /*
79 ** static prototypes
80 */
81 static void pipeser(int);
82
83
84 int
85 main(int argc, char **argv)
86 {
87         int msgnum;
88         char *cp, *file = NULL, *folder = NULL;
89         char *maildir, buf[100], **argp;
90         char **arguments;
91         struct msgs_array msgs = { 0, 0, NULL };
92         struct msgs *mp = NULL;
93         CT ct, *ctp;
94
95         done=freects_done;
96
97 #ifdef LOCALE
98         setlocale(LC_ALL, "");
99 #endif
100         invo_name = mhbasename(argv[0]);
101
102         /* read user profile/context */
103         context_read();
104
105         arguments = getarguments(invo_name, argc, argv, 1);
106         argp = arguments;
107
108         /*
109         ** Parse arguments
110         */
111         while ((cp = *argp++)) {
112                 if (*cp == '-') {
113                         switch (smatch(++cp, switches)) {
114                         case AMBIGSW:
115                                 ambigsw(cp, switches);
116                                 done(1);
117                         case UNKWNSW:
118                                 adios(NULL, "-%s unknown", cp);
119
120                         case HELPSW:
121                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
122                                 print_help(buf, switches, 1);
123                                 done(1);
124                         case VERSIONSW:
125                                 print_version(invo_name);
126                                 done(1);
127
128                         case PARTSW:
129                                 if (!(cp = *argp++) || *cp == '-')
130                                         adios(NULL, "missing argument to %s",
131                                                         argp[-2]);
132                                 if (npart >= NPARTS)
133                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
134                                 parts[npart++] = cp;
135                                 continue;
136
137                         case TYPESW:
138                                 if (!(cp = *argp++) || *cp == '-')
139                                         adios(NULL, "missing argument to %s",
140                                                         argp[-2]);
141                                 if (ntype >= NTYPES)
142                                         adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
143                                 types[ntype++] = cp;
144                                 continue;
145
146                         case FILESW:
147                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
148                                         adios(NULL, "missing argument to %s",
149                                                         argp[-2]);
150                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
151                                 continue;
152
153                         case VERBSW:
154                                 verbosw = 1;
155                                 continue;
156                         case NVERBSW:
157                                 verbosw = 0;
158                                 continue;
159                         case DEBUGSW:
160                                 debugsw = 1;
161                                 continue;
162                         }
163                 }
164                 if (*cp == '+' || *cp == '@') {
165                         if (folder)
166                                 adios(NULL, "only one folder at a time!");
167                         else
168                                 folder = getcpy(expandfol(cp));
169                 } else
170                         app_msgarg(&msgs, cp);
171         }
172
173         /* null terminate the list of acceptable parts/types */
174         parts[npart] = NULL;
175         types[ntype] = NULL;
176
177         set_endian();
178
179         /*
180         ** Check for storage directory.  If specified,
181         ** then store temporary files there.  Else we
182         ** store them in standard nmh directory.
183         */
184         if ((cp = context_find(nmhstorage)) && *cp)
185                 tmp = concat(cp, "/", invo_name, NULL);
186         else
187                 tmp = getcpy(toabsdir(invo_name));
188
189         if (file && msgs.size)
190                 adios(NULL, "cannot specify msg and file at same time!");
191
192         /*
193         ** check if message is coming from file
194         */
195         if (file) {
196                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
197                         adios(NULL, "out of memory");
198                 ctp = cts;
199
200                 if ((ct = parse_mime(file)))
201                         *ctp++ = ct;
202         } else {
203                 /*
204                 ** message(s) are coming from a folder
205                 */
206                 if (!msgs.size)
207                         app_msgarg(&msgs, seq_cur);
208                 if (!folder)
209                         folder = getcurfol();
210                 maildir = toabsdir(folder);
211
212                 if (chdir(maildir) == NOTOK)
213                         adios(maildir, "unable to change directory to");
214
215                 /* read folder and create message structure */
216                 if (!(mp = folder_read(folder)))
217                         adios(NULL, "unable to read folder %s", folder);
218
219                 /* check for empty folder */
220                 if (mp->nummsg == 0)
221                         adios(NULL, "no messages in %s", folder);
222
223                 /* parse all the message ranges/sequences and set SELECTED */
224                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
225                         if (!m_convert(mp, msgs.msgs[msgnum]))
226                                 done (1);
227                 seq_setprev(mp);  /* set the previous-sequence */
228
229                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
230                                 sizeof(*cts))))
231                         adios(NULL, "out of memory");
232                 ctp = cts;
233
234                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
235                         if (is_selected(mp, msgnum)) {
236                                 char *msgnam;
237
238                                 msgnam = m_name(msgnum);
239                                 if ((ct = parse_mime(msgnam)))
240                                         *ctp++ = ct;
241                         }
242                 }
243         }
244
245         if (!*cts)
246                 done(1);
247
248         userrs = 1;
249         SIGNAL(SIGQUIT, quitser);
250         SIGNAL(SIGPIPE, pipeser);
251
252         /*
253         ** Get the associated umask for the relevant contents.
254         */
255         for (ctp = cts; *ctp; ctp++) {
256                 struct stat st;
257
258                 ct = *ctp;
259                 if (type_ok(ct, 1) && !ct->c_umask) {
260                         if (stat(ct->c_file, &st) != NOTOK)
261                                 ct->c_umask = ~(st.st_mode & 0777);
262                         else
263                                 ct->c_umask = ~m_gmprot();
264                 }
265         }
266
267         /*
268         ** List the message content
269         */
270         list_all_messages(cts, verbosw, debugsw);
271
272         /* Now free all the structures for the content */
273         for (ctp = cts; *ctp; ctp++)
274                 free_content(*ctp);
275
276         free((char *) cts);
277         cts = NULL;
278
279         /* If reading from a folder, do some updating */
280         if (mp) {
281                 context_replace(curfolder, folder); /* update current folder */
282                 seq_setcur(mp, mp->hghsel);  /* update current message */
283                 seq_save(mp);  /* synchronize sequences  */
284                 context_save();  /* save the context file  */
285         }
286
287         done(0);
288         return 1;
289 }
290
291
292 static void
293 pipeser(int i)
294 {
295         if (i == SIGQUIT) {
296                 unlink("core");
297                 fflush(stdout);
298                 fprintf(stderr, "\n");
299                 fflush(stderr);
300         }
301
302         done(1);
303         /* NOTREACHED */
304 }