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