When building on gcc, use noreturn attribute on adios and the various done
[mmh] / uip / packf.c
1
2 /*
3  * packf.c -- pack a nmh folder into a file
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/dropsbr.h>
15 #include <h/utils.h>
16 #include <errno.h>
17
18 static struct swit switches[] = {
19 #define FILESW         0
20     { "file name", 0 },
21 #define MBOXSW         1
22     { "mbox", 0 },
23 #define MMDFSW         2
24     { "mmdf", 0 },
25 #define VERSIONSW      3
26     { "version", 0 },
27 #define HELPSW         4
28     { "help", 0 },
29     { NULL, 0 }
30 };
31
32 static int md = NOTOK;
33 static int mbx_style = MBOX_FORMAT;
34 static int mapping = 0;
35
36 static void mbxclose_done(int) NORETURN;
37
38 char *file = NULL;
39
40
41 int
42 main (int argc, char **argv)
43 {
44     int fd, msgnum;
45     char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
46     char **argp, **arguments;
47     struct msgs_array msgs = { 0, 0, NULL };
48     struct msgs *mp;
49     struct stat st;
50
51     done=mbxclose_done;
52
53 #ifdef LOCALE
54     setlocale(LC_ALL, "");
55 #endif
56     invo_name = r1bindex (argv[0], '/');
57
58     /* read user profile/context */
59     context_read();
60
61     arguments = getarguments (invo_name, argc, argv, 1);
62     argp = arguments;
63
64     /*
65      * Parse arguments
66      */
67     while ((cp = *argp++)) {
68         if (*cp == '-') {
69             switch (smatch (++cp, switches)) {
70                 case AMBIGSW: 
71                     ambigsw (cp, switches);
72                     done (1);
73                 case UNKWNSW: 
74                     adios (NULL, "-%s unknown", cp);
75
76                 case HELPSW: 
77                     snprintf (buf, sizeof(buf), "%s [+folder] [msgs] [switches]",
78                         invo_name);
79                     print_help (buf, switches, 1);
80                     done (1);
81                 case VERSIONSW:
82                     print_version(invo_name);
83                     done (1);
84
85                 case FILESW: 
86                     if (file)
87                         adios (NULL, "only one file at a time!");
88                     if (!(file = *argp++) || *file == '-')
89                         adios (NULL, "missing argument to %s", argp[-2]);
90                     continue;
91
92                 case MBOXSW:
93                     mbx_style = MBOX_FORMAT;
94                     mapping = 0;
95                     continue;
96                 case MMDFSW:
97                     mbx_style = MMDF_FORMAT;
98                     mapping = 1;
99                     continue;
100             }
101         }
102         if (*cp == '+' || *cp == '@') {
103             if (folder)
104                 adios (NULL, "only one folder at a time!");
105             folder = pluspath (cp);
106         } else
107                 app_msgarg(&msgs, cp);
108     }
109
110     if (!file)
111         file = "./msgbox";
112     file = path (file, TFILE);
113
114     /*
115      * Check if file to be created (or appended to)
116      * exists.  If not, ask for confirmation.
117      */
118     if (stat (file, &st) == NOTOK) {
119         if (errno != ENOENT)
120             adios (file, "error on file");
121         cp = concat ("Create file \"", file, "\"? ", NULL);
122         if (!getanswer (cp))
123             done (1);
124         free (cp);
125     }
126
127     if (!context_find ("path"))
128         free (path ("./", TFOLDER));
129
130     /* default is to pack whole folder */
131     if (!msgs.size)
132         app_msgarg(&msgs, "all");
133
134     if (!folder)
135         folder = getfolder (1);
136     maildir = m_maildir (folder);
137
138     if (chdir (maildir) == NOTOK)
139         adios (maildir, "unable to change directory to ");
140
141     /* read folder and create message structure */
142     if (!(mp = folder_read (folder)))
143         adios (NULL, "unable to read folder %s", folder);
144
145     /* check for empty folder */
146     if (mp->nummsg == 0)
147         adios (NULL, "no messages in %s", folder);
148
149     /* parse all the message ranges/sequences and set SELECTED */
150     for (msgnum = 0; msgnum < msgs.size; msgnum++)
151         if (!m_convert (mp, msgs.msgs[msgnum]))
152             done (1);
153     seq_setprev (mp);   /* set the previous-sequence */
154
155     /* open and lock new maildrop file */
156     if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
157         adios (file, "unable to open");
158
159     /* copy all the SELECTED messages to the file */
160     for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
161         if (is_selected(mp, msgnum)) {
162             if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
163                 admonish (msgnam, "unable to read message");
164                 break;
165             }
166
167             if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
168                 adios (file, "error writing to file");
169
170             close (fd);
171         }
172
173     /* close and unlock maildrop file */
174     mbx_close (file, md);
175
176     context_replace (pfolder, folder);  /* update current folder         */
177     if (mp->hghsel != mp->curmsg)
178         seq_setcur (mp, mp->lowsel);
179     seq_save (mp);
180     context_save ();                    /* save the context file         */
181     folder_free (mp);                   /* free folder/message structure */
182     done (0);
183     return 1;
184 }
185
186 static void
187 mbxclose_done (int status)
188 {
189     mbx_close (file, md);
190     exit (status);
191 }