2 ** comp.c -- compose a message
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
13 static struct swit switches[] = {
15 { "editor editor", 0 },
17 { "form formfile", 0 },
23 { "whatnowproc program", 0 },
33 main(int argc, char **argv)
37 char *cp, *cwd, *maildir;
38 char *ed = NULL, *form = NULL;
39 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
40 char drft[BUFSIZ], **argp, **arguments;
41 struct msgs *mp = NULL;
46 setlocale(LC_ALL, "");
48 invo_name = mhbasename(argv[0]);
50 /* read user profile/context */
53 arguments = getarguments(invo_name, argc, argv, 1);
56 while ((cp = *argp++)) {
58 switch (smatch(++cp, switches)) {
60 ambigsw(cp, switches);
63 adios(NULL, "-%s unknown", cp);
66 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
67 print_help(buf, switches, 1);
70 print_version(invo_name);
74 if (!(ed = *argp++) || *ed == '-')
75 adios(NULL, "missing argument to %s", argp[-2]);
79 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
80 adios(NULL, "missing argument to %s", argp[-2]);
84 if (!(form = *argp++) || *form == '-')
85 adios(NULL, "missing argument to %s", argp[-2]);
96 if (*cp == '+' || *cp == '@') {
98 adios(NULL, "only one folder at a time!");
100 folder = getcpy(expandfol(cp));
103 adios(NULL, "only one message at a time!");
111 if (form && (folder || msg))
112 adios(NULL, "can't mix forms and folders/msgs");
115 adios(NULL, "can't mix -use and +folder");
118 /* Don't copy; the draft shall get removed in the end. */
119 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
121 } else if (folder || msg) {
122 /* Take a message as the "form" for the new message. */
126 folder = getcurfol();
127 maildir = toabsdir(folder);
128 if (chdir(maildir) == NOTOK)
129 adios(maildir, "unable to change directory to");
130 /* read folder and create message structure */
131 if (!(mp = folder_read(folder)))
132 adios(NULL, "unable to read folder %s", folder);
133 /* check for empty folder */
135 adios(NULL, "no messages in %s", folder);
136 /* parse the message range/sequence/name and set SELECTED */
137 if (!m_convert(mp, msg))
139 seq_setprev(mp); /* set the previous-sequence */
141 adios(NULL, "only one message at a time!");
142 if ((in = open(form = getcpy(m_name(mp->lowsel)),
144 adios(form, "unable to open message");
146 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
147 if ((out = creat(drft, m_gmprot())) == NOTOK) {
148 adios(drft, "unable to create");
150 cpydata(in, out, form, drft);
155 fmtstr = new_fs(form, components);
156 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
157 if ((out = creat(drft, m_gmprot())) == NOTOK) {
158 adios(drft, "unable to create");
160 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
161 adios(drft, "error writing");
167 what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);