]> diplodocus.org Git - nmh/blob - uip/comp.c
lock_file.c: close(2) file descriptor on failure, avoiding leak.
[nmh] / uip / comp.c
1 /* comp.c -- compose a message
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 <h/utils.h>
10 #include <h/fmt_scan.h>
11 #include "sbr/m_maildir.h"
12 #include <fcntl.h>
13
14 #define COMP_SWITCHES \
15 X("draftfolder +folder", 0, DFOLDSW) \
16 X("draftmessage msg", 0, DMSGSW) \
17 X("nodraftfolder", 0, NDFLDSW) \
18 X("editor editor", 0, EDITRSW) \
19 X("noedit", 0, NEDITSW) \
20 X("file file", 0, FILESW) \
21 X("form formfile", 0, FORMSW) \
22 X("use", 0, USESW) \
23 X("nouse", 0, NUSESW) \
24 X("whatnowproc program", 0, WHATSW) \
25 X("nowhatnowproc", 0, NWHATSW) \
26 X("build", 5, BILDSW) \
27 X("version", 0, VERSIONSW) \
28 X("help", 0, HELPSW) \
29 X("to address", 0, TOSW) \
30 X("cc address", 0, CCSW) \
31 X("from address", 0, FROMSW) \
32 X("fcc mailbox", 0, FCCSW) \
33 X("width columns", 0, WIDTHSW) \
34 X("subject text", 0, SUBJECTSW) \
35
36 #define X(sw, minchars, id) id,
37 DEFINE_SWITCH_ENUM(COMP);
38 #undef X
39
40 #define X(sw, minchars, id) { sw, minchars, id },
41 DEFINE_SWITCH_ARRAY(COMP, switches);
42 #undef X
43
44 #define DISPO_SWITCHES \
45 X("quit", 0, NOSW) \
46 X("replace", 0, YESW) \
47 X("use", 0, USELSW) \
48 X("list", 0, LISTDSW) \
49 X("refile +folder", 0, REFILSW) \
50 X("new", 0, NEWSW) \
51
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(DISPO);
54 #undef X
55
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(DISPO, aqrunl);
58 #undef X
59
60 static struct swit aqrul[] = {
61 { "quit", 0, NOSW },
62 { "replace", 0, YESW },
63 { "use", 0, USELSW },
64 { "list", 0, LISTDSW },
65 { "refile", 0, REFILSW },
66 { NULL, 0, 0 }
67 };
68
69 int
70 main (int argc, char **argv)
71 {
72 int use = NOUSE, nedit = 0, nwhat = 0, build = 0;
73 int i, in = NOTOK, isdf = 0, out, dat[5], format_len = 0;
74 int outputlinelen = OUTPUTLINELEN;
75 char *cp, *cwd, *maildir, *dfolder = NULL;
76 char *ed = NULL, *file = NULL, *form = NULL;
77 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
78 char *to = NULL, *from = NULL, *cc = NULL, *fcc = NULL, *dp;
79 char *subject = NULL;
80 char drft[BUFSIZ], **argp, **arguments;
81 struct msgs *mp = NULL;
82 struct format *fmt;
83 struct stat st;
84
85 if (nmh_init(argv[0], 1)) { return 1; }
86
87 arguments = getarguments (invo_name, argc, argv, 1);
88 argp = arguments;
89
90 while ((cp = *argp++)) {
91 if (*cp == '-') {
92 switch (smatch (++cp, switches)) {
93 case AMBIGSW:
94 ambigsw (cp, switches);
95 done (1);
96 case UNKWNSW:
97 adios (NULL, "-%s unknown", cp);
98
99 case HELPSW:
100 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
101 invo_name);
102 print_help (buf, switches, 1);
103 done (0);
104 case VERSIONSW:
105 print_version(invo_name);
106 done (0);
107
108 case EDITRSW:
109 if (!(ed = *argp++) || *ed == '-')
110 adios (NULL, "missing argument to %s", argp[-2]);
111 nedit = 0;
112 continue;
113 case NEDITSW:
114 nedit++;
115 continue;
116
117 case WHATSW:
118 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
119 adios (NULL, "missing argument to %s", argp[-2]);
120 nwhat = 0;
121 continue;
122
123 case BILDSW:
124 build++;
125 /* FALLTHRU */
126 case NWHATSW:
127 nwhat++;
128 continue;
129
130 case FORMSW:
131 if (!(form = *argp++) || *form == '-')
132 adios (NULL, "missing argument to %s", argp[-2]);
133 continue;
134
135 case USESW:
136 use++;
137 continue;
138 case NUSESW:
139 use = NOUSE;
140 continue;
141
142 case FILESW: /* compatibility */
143 if (file)
144 adios (NULL, "only one file at a time!");
145 if (!(file = *argp++) || *file == '-')
146 adios (NULL, "missing argument to %s", argp[-2]);
147 isdf = NOTOK;
148 continue;
149
150 case DFOLDSW:
151 if (dfolder)
152 adios (NULL, "only one draft folder at a time!");
153 if (!(cp = *argp++) || *cp == '-')
154 adios (NULL, "missing argument to %s", argp[-2]);
155 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
156 *cp != '@' ? TFOLDER : TSUBCWF);
157 continue;
158 case DMSGSW:
159 if (file)
160 adios (NULL, "only one draft message at a time!");
161 if (!(file = *argp++) || *file == '-')
162 adios (NULL, "missing argument to %s", argp[-2]);
163 continue;
164 case NDFLDSW:
165 dfolder = NULL;
166 isdf = NOTOK;
167 continue;
168
169 case TOSW:
170 if (!(cp = *argp++) || *cp == '-')
171 adios (NULL, "missing argument to %s", argp[-2]);
172 to = addlist(to, cp);
173 continue;
174
175 case CCSW:
176 if (!(cp = *argp++) || *cp == '-')
177 adios (NULL, "missing argument to %s", argp[-2]);
178 cc = addlist(cc, cp);
179 continue;
180
181 case FROMSW:
182 if (!(cp = *argp++) || *cp == '-')
183 adios (NULL, "missing argument to %s", argp[-2]);
184 from = addlist(from, cp);
185 continue;
186
187 case FCCSW:
188 if (!(cp = *argp++) || *cp == '-')
189 adios (NULL, "missing argument to %s", argp[-2]);
190 dp = NULL;
191 if (*cp == '@')
192 cp = dp = path(cp + 1, TSUBCWF);
193 fcc = addlist(fcc, cp);
194 free(dp);
195 continue;
196
197 case WIDTHSW:
198 if (!(cp = *argp++) || *cp == '-')
199 adios (NULL, "missing argument to %s", argp[-2]);
200 if ((outputlinelen = atoi(cp)) < 10)
201 adios (NULL, "impossible width %d", outputlinelen);
202 continue;
203
204 case SUBJECTSW:
205 if (!(cp = *argp++) || *cp == '-')
206 adios (NULL, "missing argument to %s", argp[-2]);
207 subject = cp;
208 continue;
209 }
210 }
211 if (*cp == '+' || *cp == '@') {
212 if (folder)
213 adios (NULL, "only one folder at a time!");
214 folder = pluspath (cp);
215 } else {
216 if (msg)
217 adios (NULL, "only one message at a time!");
218 msg = cp;
219 }
220 }
221
222 cwd = mh_xstrdup(pwd ());
223
224 if (!context_find ("path"))
225 free (path ("./", TFOLDER));
226
227 /*
228 * Check if we are using a draft folder
229 * and have specified a message in it.
230 */
231 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
232 file = msg;
233 msg = NULL;
234 }
235 if (form && (folder || msg))
236 adios (NULL, "can't mix forms and folders/msgs");
237
238 cp = NULL;
239
240 if (folder || msg) {
241 /*
242 * Use a message as the "form" for the new message.
243 */
244 if (!msg)
245 msg = "cur";
246 if (!folder)
247 folder = getfolder (1);
248 maildir = m_maildir (folder);
249
250 if (chdir (maildir) == NOTOK)
251 adios (maildir, "unable to change directory to");
252
253 /* read folder and create message structure */
254 if (!(mp = folder_read (folder, 1)))
255 adios (NULL, "unable to read folder %s", folder);
256
257 /* check for empty folder */
258 if (mp->nummsg == 0)
259 adios (NULL, "no messages in %s", folder);
260
261 /* parse the message range/sequence/name and set SELECTED */
262 if (!m_convert (mp, msg))
263 done (1);
264 seq_setprev (mp); /* set the previous-sequence */
265 seq_save (mp);
266
267 if (mp->numsel > 1)
268 adios (NULL, "only one message at a time!");
269
270 if ((in = open (form = mh_xstrdup(m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
271 adios (form, "unable to open message");
272 } else {
273 struct comp *cptr;
274
275 if (! form)
276 form = components;
277
278 cp = new_fs(form, NULL, NULL);
279 format_len = strlen(cp);
280 fmt_compile(cp, &fmt, 1);
281
282 /*
283 * Set up any components that were fed to us on the command line
284 */
285
286 if (from) {
287 cptr = fmt_findcomp("from");
288 if (cptr)
289 cptr->c_text = from;
290 }
291 if (to) {
292 cptr = fmt_findcomp("to");
293 if (cptr)
294 cptr->c_text = to;
295 }
296 if (cc) {
297 cptr = fmt_findcomp("cc");
298 if (cptr)
299 cptr->c_text = cc;
300 }
301 if (fcc) {
302 cptr = fmt_findcomp("fcc");
303 if (cptr)
304 cptr->c_text = fcc;
305 }
306 if (subject) {
307 cptr = fmt_findcomp("subject");
308 if (cptr)
309 cptr->c_text = subject;
310 }
311 }
312
313 try_it_again:
314 strncpy (drft, build ? m_maildir ("draft")
315 : m_draft (dfolder, file, use, &isdf), sizeof(drft));
316
317 /*
318 * Check if we have an existing draft
319 */
320 if (!build && (out = open (drft, O_RDONLY)) != NOTOK) {
321 i = fdcompare (in, out);
322 close (out);
323
324 /*
325 * If we have given -use flag, or if the
326 * draft is just the same as the components
327 * file, then no need to ask any questions.
328 */
329 if (use || i)
330 goto edit_it;
331
332 if (stat (drft, &st) == NOTOK)
333 adios (drft, "unable to stat");
334 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
335 for (i = LISTDSW; i != YESW;) {
336 if (!(argp = read_switch_multiword ("\nDisposition? ",
337 isdf ? aqrunl : aqrul)))
338 done (1);
339 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
340 case NOSW:
341 done (0);
342 case NEWSW:
343 file = NULL;
344 use = NOUSE;
345 goto try_it_again;
346 case YESW:
347 break;
348 case USELSW:
349 use++;
350 goto edit_it;
351 case LISTDSW:
352 showfile (++argp, drft);
353 break;
354 case REFILSW:
355 if (refile (++argp, drft) == 0)
356 i = YESW;
357 break;
358 default:
359 inform("say what?");
360 break;
361 }
362 }
363 } else {
364 if (use)
365 adios (drft, "unable to open");
366 }
367
368 if ((out = creat (drft, m_gmprot ())) == NOTOK)
369 adios (drft, "unable to create");
370 if (cp) {
371 charstring_t scanl;
372
373 i = format_len + 1024;
374 scanl = charstring_create (i + 2);
375 dat[0] = 0;
376 dat[1] = 0;
377 dat[2] = 0;
378 dat[3] = outputlinelen;
379 dat[4] = 0;
380 fmt_scan(fmt, scanl, i, dat, NULL);
381 if (write(out, charstring_buffer (scanl),
382 charstring_bytes (scanl)) < 0) {
383 advise (drft, "write");
384 }
385 charstring_free(scanl);
386 } else {
387 cpydata (in, out, form, drft);
388 close (in);
389 }
390 close (out);
391
392 edit_it:
393 context_save (); /* save the context file */
394
395 if (nwhat)
396 done (0);
397 what_now (ed, nedit, use, drft, NULL, 0, NULL, NULL, 0, cwd, 0);
398 done (1);
399 return 1;
400 }