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