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