]> diplodocus.org Git - nmh/blob - uip/packf.c
test/common.sh.in: Clarify it's the first failure by a *named* test.
[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/utils.h>
12 #include "../sbr/m_maildir.h"
13
14 #define PACKF_SWITCHES \
15 X("file name", 0, FILESW) \
16 X("mbox", 0, MBOXSW) \
17 X("mmdf", 0, MMDFSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(PACKF);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(PACKF, switches);
27 #undef X
28
29 static int md = NOTOK;
30 static int mbx_style = MBOX_FORMAT;
31 static int mapping = 0;
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], 1)) { return 1; }
49
50 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 adios (NULL, "-%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 adios (NULL, "only one file at a time!");
79 if (!(file = *argp++) || *file == '-')
80 adios (NULL, "missing argument to %s", argp[-2]);
81 continue;
82
83 case MBOXSW:
84 mbx_style = MBOX_FORMAT;
85 mapping = 0;
86 continue;
87 case MMDFSW:
88 mbx_style = MMDF_FORMAT;
89 mapping = 1;
90 continue;
91 }
92 }
93 if (*cp == '+' || *cp == '@') {
94 if (folder)
95 adios (NULL, "only one folder at a time!");
96 folder = pluspath (cp);
97 } else
98 app_msgarg(&msgs, cp);
99 }
100
101 if (!file)
102 file = "./msgbox";
103 file = path (file, TFILE);
104
105 /*
106 * Check if file to be created (or appended to)
107 * exists. If not, ask for confirmation.
108 */
109 if (stat (file, &st) == NOTOK) {
110 if (errno != ENOENT)
111 adios (file, "error on file");
112 cp = concat ("Create file \"", file, "\"? ", NULL);
113 if (!read_yes_or_no_if_tty (cp))
114 done (1);
115 free (cp);
116 }
117
118 if (!context_find ("path"))
119 free (path ("./", TFOLDER));
120
121 /* default is to pack whole folder */
122 if (!msgs.size)
123 app_msgarg(&msgs, "all");
124
125 if (!folder)
126 folder = getfolder (1);
127 maildir = m_maildir (folder);
128
129 if (chdir (maildir) == NOTOK)
130 adios (maildir, "unable to change directory to ");
131
132 /* read folder and create message structure */
133 if (!(mp = folder_read (folder, 1)))
134 adios (NULL, "unable to read folder %s", folder);
135
136 /* check for empty folder */
137 if (mp->nummsg == 0)
138 adios (NULL, "no messages in %s", folder);
139
140 /* parse all the message ranges/sequences and set SELECTED */
141 for (msgnum = 0; msgnum < msgs.size; msgnum++)
142 if (!m_convert (mp, msgs.msgs[msgnum]))
143 done (1);
144 seq_setprev (mp); /* set the previous-sequence */
145
146 /* open and lock new maildrop file */
147 if ((md = mbx_open(file, mbx_style, getuid(), getgid(), m_gmprot())) == NOTOK)
148 adios (file, "unable to open");
149
150 /* copy all the SELECTED messages to the file */
151 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++)
152 if (is_selected(mp, msgnum)) {
153 if ((fd = open (msgnam = m_name (msgnum), O_RDONLY)) == NOTOK) {
154 admonish (msgnam, "unable to read message");
155 break;
156 }
157
158 if (mbx_copy (file, mbx_style, md, fd, mapping, NULL, 1) == NOTOK)
159 adios (file, "error writing to file");
160
161 close (fd);
162 }
163
164 /* close and unlock maildrop file */
165 mbx_close (file, md);
166
167 context_replace (pfolder, folder); /* update current folder */
168 if (mp->hghsel != mp->curmsg)
169 seq_setcur (mp, mp->lowsel);
170 seq_save (mp);
171 context_save (); /* save the context file */
172 folder_free (mp); /* free folder/message structure */
173 done (0);
174 return 1;
175 }
176
177 static void NORETURN
178 mbxclose_done (int status)
179 {
180 mbx_close (file, md);
181 exit (status);
182 }