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;
44 setlocale(LC_ALL, "");
45 invo_name = mhbasename(argv[0]);
47 /* read user profile/context */
50 arguments = getarguments(invo_name, argc, argv, 1);
53 while ((cp = *argp++)) {
55 switch (smatch(++cp, switches)) {
57 ambigsw(cp, switches);
58 /* sysexits.h EX_USAGE */
61 adios(NULL, "-%s unknown", cp);
64 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
65 print_help(buf, switches, 1);
68 print_version(invo_name);
72 if (!(ed = *argp++) || *ed == '-')
73 adios(NULL, "missing argument to %s", argp[-2]);
77 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
78 adios(NULL, "missing argument to %s", argp[-2]);
82 if (!(form = *argp++) || *form == '-')
83 adios(NULL, "missing argument to %s", argp[-2]);
94 if (*cp == '+' || *cp == '@') {
96 adios(NULL, "only one folder at a time!");
98 folder = getcpy(expandfol(cp));
101 adios(NULL, "only one message at a time!");
109 if (form && (folder || msg))
110 adios(NULL, "can't mix forms and folders/msgs");
113 adios(NULL, "can't mix -use and +folder");
116 /* Don't copy; the draft shall get removed in the end. */
117 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
119 } else if (folder || msg) {
120 /* Take a message as the "form" for the new message. */
124 folder = getcurfol();
125 maildir = toabsdir(folder);
126 if (chdir(maildir) == NOTOK)
127 adios(maildir, "unable to change directory to");
128 /* read folder and create message structure */
129 if (!(mp = folder_read(folder)))
130 adios(NULL, "unable to read folder %s", folder);
131 /* check for empty folder */
133 adios(NULL, "no messages in %s", folder);
134 /* parse the message range/sequence/name and set SELECTED */
135 if (!m_convert(mp, msg))
136 /* sysexits.h EX_USAGE */
138 seq_setprev(mp); /* set the previous-sequence */
140 adios(NULL, "only one message at a time!");
141 if ((in = open(form = getcpy(m_name(mp->lowsel)),
143 adios(form, "unable to open message");
145 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
146 if ((out = creat(drft, m_gmprot())) == NOTOK) {
147 adios(drft, "unable to create");
149 cpydata(in, out, form, drft);
154 fmtstr = new_fs(form, components);
155 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
156 if ((out = creat(drft, m_gmprot())) == NOTOK) {
157 adios(drft, "unable to create");
159 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
160 adios(drft, "error writing");
166 what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);
167 /* sysexits.h EX_SOFTWARE */