Change link-time overriding of done() to run-time reassigning of equivalent
[mmh] / uip / mhstore.c
1
2 /*
3  * mhstore.c -- store the contents of MIME messages
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <fcntl.h>
14 #include <h/signals.h>
15 #include <h/md5.h>
16 #include <errno.h>
17 #include <signal.h>
18 #include <h/mts.h>
19 #include <h/tws.h>
20 #include <h/mime.h>
21 #include <h/mhparse.h>
22 #include <h/mhcachesbr.h>
23 #include <h/utils.h>
24
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
27 #endif
28
29 static struct swit switches[] = {
30 #define AUTOSW                  0
31     { "auto", 0 },
32 #define NAUTOSW                 1
33     { "noauto", 0 },
34 #define CHECKSW                 2
35     { "check", 0 },
36 #define NCHECKSW                3
37     { "nocheck", 0 },
38 #define VERBSW                  4
39     { "verbose", 0 },
40 #define NVERBSW                 5
41     { "noverbose", 0 },
42 #define FILESW                  6       /* interface from show */
43     { "file file", 0 },
44 #define PARTSW                  7
45     { "part number", 0 },
46 #define TYPESW                  8
47     { "type content", 0 },
48 #define RCACHESW                9
49     { "rcache policy", 0 },
50 #define WCACHESW               10
51     { "wcache policy", 0 },
52 #define VERSIONSW              11
53     { "version", 0 },
54 #define HELPSW                 12
55     { "help", 0 },
56
57 /*
58  * switches for debugging
59  */
60 #define DEBUGSW                13
61     { "debug", -5 },
62     { NULL, 0 }
63 };
64
65
66 /* mhparse.c */
67 extern int checksw;
68 extern char *tmp;       /* directory to place temp files */
69
70 /* mhcachesbr.c */
71 extern int rcachesw;
72 extern int wcachesw;
73 extern char *cache_public;
74 extern char *cache_private;
75
76 /* mhstoresbr.c */
77 extern int autosw;
78 extern char *cwd;       /* cache current working directory */
79
80 /* mhmisc.c */
81 extern int npart;
82 extern int ntype;
83 extern char *parts[NPARTS + 1];
84 extern char *types[NTYPES + 1];
85 extern int userrs;
86
87 int debugsw = 0;
88 int verbosw = 0;
89
90 /* The list of top-level contents to display */
91 CT *cts = NULL;
92
93 #define quitser pipeser
94
95 /* mhparse.c */
96 CT parse_mime (char *);
97
98 /* mhmisc.c */
99 int part_ok (CT, int);
100 int type_ok (CT, int);
101 void set_endian (void);
102 void flush_errors (void);
103
104 /* mhstoresbr.c */
105 void store_all_messages (CT *);
106
107 /* mhfree.c */
108 void free_content (CT);
109
110 /*
111  * static prototypes
112  */
113 static RETSIGTYPE pipeser (int);
114 static int freectp_done (int);
115
116
117 int
118 main (int argc, char **argv)
119 {
120     int msgnum, *icachesw;
121     char *cp, *file = NULL, *folder = NULL;
122     char *maildir, buf[100], **argp;
123     char **arguments;
124     struct msgs_array msgs = { 0, 0, NULL };
125     struct msgs *mp = NULL;
126     CT ct, *ctp;
127     FILE *fp;
128
129     done=freectp_done;
130
131 #ifdef LOCALE
132     setlocale(LC_ALL, "");
133 #endif
134     invo_name = r1bindex (argv[0], '/');
135
136     /* read user profile/context */
137     context_read();
138
139     arguments = getarguments (invo_name, argc, argv, 1);
140     argp = arguments;
141
142     /*
143      * Parse arguments
144      */
145     while ((cp = *argp++)) {
146         if (*cp == '-') {
147             switch (smatch (++cp, switches)) {
148             case AMBIGSW: 
149                 ambigsw (cp, switches);
150                 done (1);
151             case UNKWNSW: 
152                 adios (NULL, "-%s unknown", cp);
153
154             case HELPSW: 
155                 snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
156                         invo_name);
157                 print_help (buf, switches, 1);
158                 done (1);
159             case VERSIONSW:
160                 print_version(invo_name);
161                 done (1);
162
163             case AUTOSW:
164                 autosw++;
165                 continue;
166             case NAUTOSW:
167                 autosw = 0;
168                 continue;
169
170             case RCACHESW:
171                 icachesw = &rcachesw;
172                 goto do_cache;
173             case WCACHESW:
174                 icachesw = &wcachesw;
175 do_cache:
176                 if (!(cp = *argp++) || *cp == '-')
177                     adios (NULL, "missing argument to %s", argp[-2]);
178                 switch (*icachesw = smatch (cp, caches)) {
179                 case AMBIGSW:
180                     ambigsw (cp, caches);
181                     done (1);
182                 case UNKWNSW:
183                     adios (NULL, "%s unknown", cp);
184                 default:
185                     break;
186                 }
187                 continue;
188
189             case CHECKSW:
190                 checksw++;
191                 continue;
192             case NCHECKSW:
193                 checksw = 0;
194                 continue;
195
196             case PARTSW:
197                 if (!(cp = *argp++) || *cp == '-')
198                     adios (NULL, "missing argument to %s", argp[-2]);
199                 if (npart >= NPARTS)
200                     adios (NULL, "too many parts (starting with %s), %d max",
201                            cp, NPARTS);
202                 parts[npart++] = cp;
203                 continue;
204
205             case TYPESW:
206                 if (!(cp = *argp++) || *cp == '-')
207                     adios (NULL, "missing argument to %s", argp[-2]);
208                 if (ntype >= NTYPES)
209                     adios (NULL, "too many types (starting with %s), %d max",
210                            cp, NTYPES);
211                 types[ntype++] = cp;
212                 continue;
213
214             case FILESW:
215                 if (!(cp = *argp++) || (*cp == '-' && cp[1]))
216                     adios (NULL, "missing argument to %s", argp[-2]);
217                 file = *cp == '-' ? cp : path (cp, TFILE);
218                 continue;
219
220             case VERBSW: 
221                 verbosw = 1;
222                 continue;
223             case NVERBSW: 
224                 verbosw = 0;
225                 continue;
226             case DEBUGSW:
227                 debugsw = 1;
228                 continue;
229             }
230         }
231         if (*cp == '+' || *cp == '@') {
232             if (folder)
233                 adios (NULL, "only one folder at a time!");
234             else
235                 folder = pluspath (cp);
236         } else
237                 app_msgarg(&msgs, cp);
238     }
239
240     /* null terminate the list of acceptable parts/types */
241     parts[npart] = NULL;
242     types[ntype] = NULL;
243
244     set_endian ();
245
246     /*
247      * Check if we've specified an additional profile
248      */
249     if ((cp = getenv ("MHSTORE"))) {
250         if ((fp = fopen (cp, "r"))) {
251             readconfig ((struct node **) 0, fp, cp, 0);
252             fclose (fp);
253         } else {
254             admonish ("", "unable to read $MHSTORE profile (%s)", cp);
255         }
256     }
257
258     /*
259      * Read the standard profile setup
260      */
261     if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
262         readconfig ((struct node **) 0, fp, cp, 0);
263         fclose (fp);
264     }
265
266     /* Check for public cache location */
267     if ((cache_public = context_find (nmhcache)) && *cache_public != '/')
268         cache_public = NULL;
269
270     /* Check for private cache location */
271     if (!(cache_private = context_find (nmhprivcache)))
272         cache_private = ".cache";
273     cache_private = getcpy (m_maildir (cache_private));
274
275     /*
276      * Cache the current directory before we do any chdirs()'s.
277      */
278     cwd = getcpy (pwd());
279
280     /*
281      * Check for storage directory.  If specified,
282      * then store temporary files there.  Else we
283      * store them in standard nmh directory.
284      */
285     if ((cp = context_find (nmhstorage)) && *cp)
286         tmp = concat (cp, "/", invo_name, NULL);
287     else
288         tmp = add (m_maildir (invo_name), NULL);
289
290     if (!context_find ("path"))
291         free (path ("./", TFOLDER));
292
293     if (file && msgs.size)
294         adios (NULL, "cannot specify msg and file at same time!");
295
296     /*
297      * check if message is coming from file
298      */
299     if (file) {
300         if (!(cts = (CT *) calloc ((size_t) 2, sizeof(*cts))))
301             adios (NULL, "out of memory");
302         ctp = cts;
303
304         if ((ct = parse_mime (file)));
305             *ctp++ = ct;
306     } else {
307         /*
308          * message(s) are coming from a folder
309          */
310         if (!msgs.size)
311             app_msgarg(&msgs, "cur");
312         if (!folder)
313             folder = getfolder (1);
314         maildir = m_maildir (folder);
315
316         if (chdir (maildir) == NOTOK)
317             adios (maildir, "unable to change directory to");
318
319         /* read folder and create message structure */
320         if (!(mp = folder_read (folder)))
321             adios (NULL, "unable to read folder %s", folder);
322
323         /* check for empty folder */
324         if (mp->nummsg == 0)
325             adios (NULL, "no messages in %s", folder);
326
327         /* parse all the message ranges/sequences and set SELECTED */
328         for (msgnum = 0; msgnum < msgs.size; msgnum++)
329             if (!m_convert (mp, msgs.msgs[msgnum]))
330                 done (1);
331         seq_setprev (mp);       /* set the previous-sequence */
332
333         if (!(cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof(*cts))))
334             adios (NULL, "out of memory");
335         ctp = cts;
336
337         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
338             if (is_selected(mp, msgnum)) {
339                 char *msgnam;
340
341                 msgnam = m_name (msgnum);
342                 if ((ct = parse_mime (msgnam)))
343                     *ctp++ = ct;
344             }
345         }
346     }
347
348     if (!*cts)
349         done (1);
350
351     userrs = 1;
352     SIGNAL (SIGQUIT, quitser);
353     SIGNAL (SIGPIPE, pipeser);
354
355     /*
356      * Get the associated umask for the relevant contents.
357      */
358     for (ctp = cts; *ctp; ctp++) {
359         struct stat st;
360
361         ct = *ctp;
362         if (type_ok (ct, 1) && !ct->c_umask) {
363             if (stat (ct->c_file, &st) != NOTOK)
364                 ct->c_umask = ~(st.st_mode & 0777);
365             else
366                 ct->c_umask = ~m_gmprot();
367         }
368     }
369
370     /*
371      * Store the message content
372      */
373     store_all_messages (cts);
374
375     /* Now free all the structures for the content */
376     for (ctp = cts; *ctp; ctp++)
377         free_content (*ctp);
378
379     free ((char *) cts);
380     cts = NULL;
381
382     /* If reading from a folder, do some updating */
383     if (mp) {
384         context_replace (pfolder, folder);/* update current folder  */
385         seq_setcur (mp, mp->hghsel);      /* update current message */
386         seq_save (mp);                    /* synchronize sequences  */
387         context_save ();                  /* save the context file  */
388     }
389
390     return done (0);
391 }
392
393
394 static RETSIGTYPE
395 pipeser (int i)
396 {
397     if (i == SIGQUIT) {
398         unlink ("core");
399         fflush (stdout);
400         fprintf (stderr, "\n");
401         fflush (stderr);
402     }
403
404     done (1);
405     /* NOTREACHED */
406 }
407
408
409 static int
410 freectp_done (int status)
411 {
412     CT *ctp;
413
414     if ((ctp = cts))
415         for (; *ctp; ctp++)
416             free_content (*ctp);
417
418     exit (status);
419     return 1;  /* dead code to satisfy the compiler */
420 }