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