3 * comp.c -- compose a message
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.
12 #include <h/fmt_scan.h>
15 static struct swit switches[] = {
17 { "draftfolder +folder", 0 },
19 { "draftmessage msg", 0 },
21 { "nodraftfolder", 0 },
23 { "editor editor", 0 },
29 { "form formfile", 0 },
35 { "whatnowproc program", 0 },
37 { "nowhatnowproc", 0 },
45 static struct swit aqrunl[] = {
55 { "refile +folder", 0 },
61 static struct swit aqrul[] = {
72 main (int argc, char **argv)
74 int use = NOUSE, nedit = 0, nwhat = 0;
75 int i, in = NOTOK, isdf = 0, out, dat[5], ncomps, format_len;
76 int outputlinelen = OUTPUTLINELEN;
77 char *cp, *cwd, *maildir, *dfolder = NULL;
78 char *ed = NULL, *file = NULL, *form = NULL;
79 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
80 char drft[BUFSIZ], **argp, **arguments;
81 struct msgs *mp = NULL;
86 setlocale(LC_ALL, "");
88 invo_name = r1bindex (argv[0], '/');
90 /* read user profile/context */
93 arguments = getarguments (invo_name, argc, argv, 1);
96 while ((cp = *argp++)) {
98 switch (smatch (++cp, switches)) {
100 ambigsw (cp, switches);
103 adios (NULL, "-%s unknown", cp);
106 snprintf (buf, sizeof(buf), "%s [+folder] [msg] [switches]",
108 print_help (buf, switches, 1);
111 print_version(invo_name);
115 if (!(ed = *argp++) || *ed == '-')
116 adios (NULL, "missing argument to %s", argp[-2]);
124 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
125 adios (NULL, "missing argument to %s", argp[-2]);
133 if (!(form = *argp++) || *form == '-')
134 adios (NULL, "missing argument to %s", argp[-2]);
144 case FILESW: /* compatibility */
146 adios (NULL, "only one file at a time!");
147 if (!(file = *argp++) || *file == '-')
148 adios (NULL, "missing argument to %s", argp[-2]);
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);
162 adios (NULL, "only one draft message at a time!");
163 if (!(file = *argp++) || *file == '-')
164 adios (NULL, "missing argument to %s", argp[-2]);
172 if (*cp == '+' || *cp == '@') {
174 adios (NULL, "only one folder at a time!");
176 folder = pluspath (cp);
179 adios (NULL, "only one message at a time!");
185 cwd = getcpy (pwd ());
187 if (!context_find ("path"))
188 free (path ("./", TFOLDER));
191 * Check if we are using a draft folder
192 * and have specified a message in it.
194 if ((dfolder || context_find ("Draft-Folder")) && !folder && msg && !file) {
198 if (form && (folder || msg))
199 adios (NULL, "can't mix forms and folders/msgs");
205 * Use a message as the "form" for the new message.
210 folder = getfolder (1);
211 maildir = m_maildir (folder);
213 if (chdir (maildir) == NOTOK)
214 adios (maildir, "unable to change directory to");
216 /* read folder and create message structure */
217 if (!(mp = folder_read (folder)))
218 adios (NULL, "unable to read folder %s", folder);
220 /* check for empty folder */
222 adios (NULL, "no messages in %s", folder);
224 /* parse the message range/sequence/name and set SELECTED */
225 if (!m_convert (mp, msg))
227 seq_setprev (mp); /* set the previous-sequence */
230 adios (NULL, "only one message at a time!");
232 if ((in = open (form = getcpy (m_name (mp->lowsel)), O_RDONLY)) == NOTOK)
233 adios (form, "unable to open message");
238 cp = new_fs(form, NULL, NULL);
239 format_len = strlen(cp);
240 ncomps = fmt_compile(cp, &fmt);
242 adios(NULL, "format components not supported when using comp");
247 strncpy (drft, m_draft (dfolder, file, use, &isdf), sizeof(drft));
250 * Check if we have an existing draft
252 if ((out = open (drft, O_RDONLY)) != NOTOK) {
253 i = fdcompare (in, out);
257 * If we have given -use flag, or if the
258 * draft is just the same as the components
259 * file, then no need to ask any questions.
264 if (stat (drft, &st) == NOTOK)
265 adios (drft, "unable to stat");
266 printf ("Draft \"%s\" exists (%ld bytes).", drft, (long) st.st_size);
267 for (i = LISTDSW; i != YESW;) {
268 if (!(argp = getans ("\nDisposition? ", isdf ? aqrunl : aqrul)))
270 switch (i = smatch (*argp, isdf ? aqrunl : aqrul)) {
283 showfile (++argp, drft);
286 if (refile (++argp, drft) == 0)
290 advise (NULL, "say what?");
296 adios (drft, "unable to open");
299 if ((out = creat (drft, m_gmprot ())) == NOTOK)
300 adios (drft, "unable to create");
304 i = format_len + 1024;
305 scanl = mh_xmalloc((size_t) i + 2);
309 dat[3] = outputlinelen;
311 fmt_scan(fmt, scanl, i, dat);
312 write(out, scanl, strlen(scanl));
315 cpydata (in, out, form, drft);
321 context_save (); /* save the context file */
325 what_now (ed, nedit, use, drft, NULL, 0, NULLMP, NULL, 0, cwd);