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