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