Use sysexits.h for better exit-codes
[mmh] / uip / packf.c
1 /*
2 ** packf.c -- pack a nmh folder into a file
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/dropsbr.h>
12 #include <h/utils.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <locale.h>
16 #include <sysexits.h>
17
18 static struct swit switches[] = {
19 #define VERSIONSW  0
20         { "Version", 0 },
21 #define HELPSW  1
22         { "help", 0 },
23         { NULL, 0 }
24 };
25
26
27 int
28 main(int argc, char **argv)
29 {
30         int fd, msgnum;
31         char *cp, *maildir, *msgnam, *folder = NULL, buf[BUFSIZ];
32         char **argp, **arguments;
33         struct msgs_array msgs = { 0, 0, NULL };
34         struct msgs *mp;
35
36         setlocale(LC_ALL, "");
37         invo_name = mhbasename(argv[0]);
38
39         /* read user profile/context */
40         context_read();
41
42         arguments = getarguments(invo_name, argc, argv, 1);
43         argp = arguments;
44
45         /*
46         ** Parse arguments
47         */
48         while ((cp = *argp++)) {
49                 if (*cp == '-') {
50                         switch (smatch(++cp, switches)) {
51                         case AMBIGSW:
52                                 ambigsw(cp, switches);
53                                 exit(EX_USAGE);
54                         case UNKWNSW:
55                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
56
57                         case HELPSW:
58                                 snprintf(buf, sizeof(buf), "%s [+folder] [msgs] [switches]", invo_name);
59                                 print_help(buf, switches, 1);
60                                 exit(argc == 2 ? EX_OK : EX_USAGE);
61                         case VERSIONSW:
62                                 print_version(invo_name);
63                                 exit(argc == 2 ? EX_OK : EX_USAGE);
64                         }
65                 }
66                 if (*cp == '+' || *cp == '@') {
67                         if (folder)
68                                 adios(EX_USAGE, NULL, "only one folder at a time!");
69                         folder = getcpy(expandfol(cp));
70                 } else
71                         app_msgarg(&msgs, cp);
72         }
73
74         /* default is to pack whole folder */
75         if (!msgs.size)
76                 app_msgarg(&msgs, seq_all);
77
78         if (!folder)
79                 folder = getcurfol();
80         maildir = toabsdir(folder);
81
82         if (chdir(maildir) == NOTOK)
83                 adios(EX_OSERR, maildir, "unable to change directory to ");
84
85         /* read folder and create message structure */
86         if (!(mp = folder_read(folder)))
87                 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
88
89         /* check for empty folder */
90         if (mp->nummsg == 0)
91                 adios(EX_DATAERR, NULL, "no messages in %s", folder);
92
93         /* parse all the message ranges/sequences and set SELECTED */
94         for (msgnum = 0; msgnum < msgs.size; msgnum++)
95                 if (!m_convert(mp, msgs.msgs[msgnum]))
96                         exit(EX_USAGE);
97         seq_setprev(mp);  /* set the previous-sequence */
98
99         /* copy all the SELECTED messages to stdout */
100         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
101                 if (is_selected(mp, msgnum)) {
102                         if ((fd = open(msgnam = m_name(msgnum), O_RDONLY))
103                                         == NOTOK) {
104                                 admonish(msgnam, "unable to read message");
105                                 break;
106                         }
107                         if (mbox_copy(fileno(stdout), fd) == NOTOK) {
108                                 adios(EX_IOERR, NULL, "error writing to stdout");
109                         }
110                         close(fd);
111                 }
112         }
113         context_replace(curfolder, folder);
114         if (mp->hghsel != mp->curmsg)
115                 seq_setcur(mp, mp->lowsel);
116         seq_save(mp);
117         context_save();
118         folder_free(mp);
119         return 0;
120 }