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