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