Fix minor coding style issues (whitespace)
[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", 2 },
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         setlocale(LC_ALL, "");
98         invo_name = mhbasename(argv[0]);
99
100         /* read user profile/context */
101         context_read();
102
103         arguments = getarguments(invo_name, argc, argv, 1);
104         argp = arguments;
105
106         /*
107         ** Parse arguments
108         */
109         while ((cp = *argp++)) {
110                 if (*cp == '-') {
111                         switch (smatch(++cp, switches)) {
112                         case AMBIGSW:
113                                 ambigsw(cp, switches);
114                                 done(1);
115                         case UNKWNSW:
116                                 adios(NULL, "-%s unknown", cp);
117
118                         case HELPSW:
119                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
120                                 print_help(buf, switches, 1);
121                                 done(1);
122                         case VERSIONSW:
123                                 print_version(invo_name);
124                                 done(1);
125
126                         case PARTSW:
127                                 if (!(cp = *argp++) || *cp == '-')
128                                         adios(NULL, "missing argument to %s",
129                                                         argp[-2]);
130                                 if (npart >= NPARTS)
131                                         adios(NULL, "too many parts (starting with %s), %d max", cp, NPARTS);
132                                 parts[npart++] = cp;
133                                 continue;
134
135                         case TYPESW:
136                                 if (!(cp = *argp++) || *cp == '-')
137                                         adios(NULL, "missing argument to %s",
138                                                         argp[-2]);
139                                 if (ntype >= NTYPES)
140                                         adios(NULL, "too many types (starting with %s), %d max", cp, NTYPES);
141                                 types[ntype++] = cp;
142                                 continue;
143
144                         case FILESW:
145                                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
146                                         adios(NULL, "missing argument to %s",
147                                                         argp[-2]);
148                                 file = *cp == '-' ? cp : getcpy(expanddir(cp));
149                                 continue;
150
151                         case VERBSW:
152                                 verbosw = 1;
153                                 continue;
154                         case NVERBSW:
155                                 verbosw = 0;
156                                 continue;
157                         case DEBUGSW:
158                                 debugsw = 1;
159                                 continue;
160                         }
161                 }
162                 if (*cp == '+' || *cp == '@') {
163                         if (folder)
164                                 adios(NULL, "only one folder at a time!");
165                         else
166                                 folder = getcpy(expandfol(cp));
167                 } else
168                         app_msgarg(&msgs, cp);
169         }
170
171         /* null terminate the list of acceptable parts/types */
172         parts[npart] = NULL;
173         types[ntype] = NULL;
174
175         set_endian();
176
177         /*
178         ** Check for storage directory.  If specified,
179         ** then store temporary files there.  Else we
180         ** store them in standard nmh directory.
181         */
182         if ((cp = context_find(nmhstorage)) && *cp)
183                 tmp = concat(cp, "/", invo_name, NULL);
184         else
185                 tmp = getcpy(toabsdir(invo_name));
186
187         if (file && msgs.size)
188                 adios(NULL, "cannot specify msg and file at same time!");
189
190         /*
191         ** check if message is coming from file
192         */
193         if (file) {
194                 if (!(cts = (CT *) calloc((size_t) 2, sizeof(*cts))))
195                         adios(NULL, "out of memory");
196                 ctp = cts;
197
198                 if ((ct = parse_mime(file)))
199                         *ctp++ = ct;
200         } else {
201                 /*
202                 ** message(s) are coming from a folder
203                 */
204                 if (!msgs.size)
205                         app_msgarg(&msgs, seq_cur);
206                 if (!folder)
207                         folder = getcurfol();
208                 maildir = toabsdir(folder);
209
210                 if (chdir(maildir) == NOTOK)
211                         adios(maildir, "unable to change directory to");
212
213                 /* read folder and create message structure */
214                 if (!(mp = folder_read(folder)))
215                         adios(NULL, "unable to read folder %s", folder);
216
217                 /* check for empty folder */
218                 if (mp->nummsg == 0)
219                         adios(NULL, "no messages in %s", folder);
220
221                 /* parse all the message ranges/sequences and set SELECTED */
222                 for (msgnum = 0; msgnum < msgs.size; msgnum++)
223                         if (!m_convert(mp, msgs.msgs[msgnum]))
224                                 done(1);
225                 seq_setprev(mp);  /* set the previous-sequence */
226
227                 if (!(cts = (CT *) calloc((size_t) (mp->numsel + 1),
228                                 sizeof(*cts))))
229                         adios(NULL, "out of memory");
230                 ctp = cts;
231
232                 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
233                         if (is_selected(mp, msgnum)) {
234                                 char *msgnam;
235
236                                 msgnam = m_name(msgnum);
237                                 if ((ct = parse_mime(msgnam)))
238                                         *ctp++ = ct;
239                         }
240                 }
241         }
242
243         if (!*cts)
244                 done(1);
245
246         userrs = 1;
247         SIGNAL(SIGQUIT, quitser);
248         SIGNAL(SIGPIPE, pipeser);
249
250         /*
251         ** Get the associated umask for the relevant contents.
252         */
253         for (ctp = cts; *ctp; ctp++) {
254                 struct stat st;
255
256                 ct = *ctp;
257                 if (type_ok(ct, 1) && !ct->c_umask) {
258                         if (stat(ct->c_file, &st) != NOTOK)
259                                 ct->c_umask = ~(st.st_mode & 0777);
260                         else
261                                 ct->c_umask = ~m_gmprot();
262                 }
263         }
264
265         /*
266         ** List the message content
267         */
268         list_all_messages(cts, verbosw, debugsw);
269
270         /* Now free all the structures for the content */
271         for (ctp = cts; *ctp; ctp++)
272                 free_content(*ctp);
273
274         free((char *) cts);
275         cts = NULL;
276
277         /* If reading from a folder, do some updating */
278         if (mp) {
279                 context_replace(curfolder, folder); /* update current folder */
280                 seq_setcur(mp, mp->hghsel);  /* update current message */
281                 seq_save(mp);  /* synchronize sequences  */
282                 context_save();  /* save the context file  */
283         }
284
285         done(0);
286         return 1;
287 }
288
289
290 static void
291 pipeser(int i)
292 {
293         if (i == SIGQUIT) {
294                 unlink("core");
295                 fflush(stdout);
296                 fprintf(stderr, "\n");
297                 fflush(stderr);
298         }
299
300         done(1);
301         /* NOTREACHED */
302 }