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