]> diplodocus.org Git - nmh/blob - uip/comp.c
Update manpages to use .TP for tagged paragraphs (part I).
[nmh] / uip / comp.c
1
2 /*
3 * comp.c -- compose a message
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 <h/utils.h>
12 #include <h/fmt_scan.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("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 colums", 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;
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 #ifdef LOCALE
86 setlocale(LC_ALL, "");
87 #endif
88 invo_name = r1bindex (argv[0], '/');
89
90 /* read user profile/context */
91 context_read();
92
93 arguments = getarguments (invo_name, argc, argv, 1);
94 argp = arguments;
95
96 while ((cp = *argp++)) {
97 if (*cp == '-') {
98 switch (smatch (++cp, switches)) {
99 case AMBIGSW:
100 ambigsw (cp, switches);
101 done (1);
102 case UNKWNSW:
103 adios (NULL, "-%s unknown", cp);
104
105 case HELPSW:
106 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
107 invo_name);
108 print_help (buf, switches, 1);
109 done (0);
110 case VERSIONSW:
111 print_version(invo_name);
112 done (0);
113
114 case EDITRSW:
115 if (!(ed = *argp++) || *ed == '-')
116 adios (NULL, "missing argument to %s", argp[-2]);
117 nedit = 0;
118 continue;
119 case NEDITSW:
120 nedit++;
121 continue;
122
123 case WHATSW:
124 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
125 adios (NULL, "missing argument to %s", argp[-2]);
126 nwhat = 0;
127 continue;
128 case NWHATSW:
129 nwhat++;
130 continue;
131
132 case FORMSW:
133 if (!(form = *argp++) || *form == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
135 continue;
136
137 case USESW:
138 use++;
139 continue;
140 case NUSESW:
141 use = NOUSE;
142 continue;
143
144 case FILESW: /* compatibility */
145 if (file)
146 adios (NULL, "only one file at a time!");
147 if (!(file = *argp++) || *file == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
149 isdf = NOTOK;
150 continue;
151
152 case DFOLDSW:
153 if (dfolder)
154 adios (NULL, "only one draft folder at a time!");
155 if (!(cp = *argp++) || *cp == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
157 dfolder = path (*cp == '+' || *cp == '@' ? cp + 1 : cp,
158 *cp != '@' ? TFOLDER : TSUBCWF);
159 continue;
160 case DMSGSW:
161 if (file)
162 adios (NULL, "only one draft message at a time!");
163 if (!(file = *argp++) || *file == '-')
164 adios (NULL, "missing argument to %s", argp[-2]);
165 continue;
166 case NDFLDSW:
167 dfolder = NULL;
168 isdf = NOTOK;
169 continue;
170
171 case TOSW:
172 if (!(cp = *argp++) || *cp == '-')
173 adios (NULL, "missing argument to %s", argp[-2]);
174 to = addlist(to, cp);
175 continue;
176
177 case CCSW:
178 if (!(cp = *argp++) || *cp == '-')
179 adios (NULL, "missing argument to %s", argp[-2]);
180 cc = addlist(cc, cp);
181 continue;
182
183 case FROMSW:
184 if (!(cp = *argp++) || *cp == '-')
185 adios (NULL, "missing argument to %s", argp[-2]);
186 from = addlist(from, cp);
187 continue;
188
189 case FCCSW:
190 if (!(cp = *argp++) || *cp == '-')
191 adios (NULL, "missing argument to %s", argp[-2]);
192 dp = NULL;
193 if (*cp == '@')
194 cp = dp = path(cp + 1, TSUBCWF);
195 fcc = addlist(fcc, cp);
196 if (dp)
197 free(dp);
198 continue;
199
200 case WIDTHSW:
201 if (!(cp = *argp++) || *cp == '-')
202 adios (NULL, "missing argument to %s", argp[-2]);
203 if ((outputlinelen = atoi(cp)) < 10)
204 adios (NULL, "impossible width %d", outputlinelen);
205 continue;
206
207 case SUBJECTSW:
208 if (!(cp = *argp++) || *cp == '-')
209 adios (NULL, "missing argument to %s", argp[-2]);
210 subject = cp;
211 continue;
212 }
213 }
214 if (*cp == '+' || *cp == '@') {
215 if (folder)
216 adios (NULL, "only one folder at a time!");
217 else
218 folder = pluspath (cp);
219 } else {
220 if (msg)
221 adios (NULL, "only one message at a time!");
222 else
223 msg = cp;
224 }
225 }
226
227 cwd = getcpy (pwd ());
228
229 if (!context_find ("path"))
230 free (path ("./", TFOLDER));
231
232 /*
233 * Check if we are using a draft folder
234 * and have specified a message in it.
235 */
236 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
237 file = msg;
238 msg = NULL;
239 }
240 if (form && (folder || msg))
241 adios (NULL, "can't mix forms and folders/msgs");
242
243 cp = NULL;
244
245 if (folder || msg) {
246 /*
247 * Use a message as the "form" for the new message.
248 */
249 if (!msg)
250 msg = "cur";
251 if (!folder)
252 folder = getfolder (1);
253 maildir = m_maildir (folder);
254
255 if (chdir (maildir) == NOTOK)
256 adios (maildir, "unable to change directory to");
257
258 /* read folder and create message structure */
259 if (!(mp = folder_read (folder)))
260 adios (NULL, "unable to read folder %s", folder);
261
262 /* check for empty folder */
263 if (mp->nummsg == 0)
264 adios (NULL, "no messages in %s", folder);
265
266 /* parse the message range/sequence/name and set SELECTED */
267 if (!m_convert (mp, msg))
268 done (1);
269 seq_setprev (mp); /* set the previous-sequence */
270
271 if (mp->numsel > 1)
272 adios (NULL, "only one message at a time!");
273
274 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
275 adios (form, "unable to open message");
276 } else {
277 struct comp *cptr;
278
279 if (! form)
280 form = components;
281
282 cp = new_fs(form, NULL, NULL);
283 format_len = strlen(cp);
284 fmt_compile(cp, &fmt, 1);
285
286 /*
287 * Set up any components that were fed to us on the command line
288 */
289
290 if (from) {
291 cptr = fmt_findcomp("from");
292 if (cptr)
293 cptr->c_text = from;
294 }
295 if (to) {
296 cptr = fmt_findcomp("to");
297 if (cptr)
298 cptr->c_text = to;
299 }
300 if (cc) {
301 cptr = fmt_findcomp("cc");
302 if (cptr)
303 cptr->c_text = cc;
304 }
305 if (fcc) {
306 cptr = fmt_findcomp("fcc");
307 if (cptr)
308 cptr->c_text = fcc;
309 }
310 if (subject) {
311 cptr = fmt_findcomp("subject");
312 if (cptr)
313 cptr->c_text = subject;
314 }
315 }
316
317 try_it_again:
318 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
319
320 /*
321 * Check if we have an existing draft
322 */
323 if ((out = open (drft, O_RDONLY)) != NOTOK) {
324 i = fdcompare (in, out);
325 close (out);
326
327 /*
328 * If we have given -use flag, or if the
329 * draft is just the same as the components
330 * file, then no need to ask any questions.
331 */
332 if (use || i)
333 goto edit_it;
334
335 if (stat (drft, &st) == NOTOK)
336 adios (drft, "unable to stat");
337 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
338 for (i = LISTDSW; i != YESW;) {
339 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
340 done (1);
341 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
342 case NOSW:
343 done (0);
344 case NEWSW:
345 file = NULL;
346 use = NOUSE;
347 goto try_it_again;
348 case YESW:
349 break;
350 case USELSW:
351 use++;
352 goto edit_it;
353 case LISTDSW:
354 showfile (++argp, drft);
355 break;
356 case REFILSW:
357 if (refile (++argp, drft) == 0)
358 i = YESW;
359 break;
360 default:
361 advise (NULL, "say what?");
362 break;
363 }
364 }
365 } else {
366 if (use)
367 adios (drft, "unable to open");
368 }
369
370 if ((out = creat (drft, m_gmprot ())) == NOTOK)
371 adios (drft, "unable to create");
372 if (cp) {
373 char *scanl;
374
375 i = format_len + 1024;
376 scanl = mh_xmalloc((size_t) i + 2);
377 dat[0] = 0;
378 dat[1] = 0;
379 dat[2] = 0;
380 dat[3] = outputlinelen;
381 dat[4] = 0;
382 fmt_scan(fmt, scanl, i + 1, i, dat);
383 write(out, scanl, strlen(scanl));
384 free(scanl);
385 } else {
386 cpydata (in, out, form, drft);
387 close (in);
388 }
389 close (out);
390
391 edit_it:
392 context_save (); /* save the context file */
393
394 if (nwhat)
395 done (0);
396 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd, 0);
397 done (1);
398 return 1;
399 }