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 },
19 { "form formfile", 0 },
25 { "whatnowproc program", 0 },
35 main(int argc, char **argv)
37 int use = NOUSE, nedit = 0;
39 char *cp, *cwd, *maildir;
40 char *ed = NULL, *form = NULL;
41 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
42 char drft[BUFSIZ], **argp, **arguments;
43 struct msgs *mp = NULL;
48 setlocale(LC_ALL, "");
50 invo_name = mhbasename(argv[0]);
52 /* read user profile/context */
55 arguments = getarguments(invo_name, argc, argv, 1);
58 while ((cp = *argp++)) {
60 switch (smatch(++cp, switches)) {
62 ambigsw(cp, switches);
65 adios(NULL, "-%s unknown", cp);
68 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
69 print_help(buf, switches, 1);
72 print_version(invo_name);
76 if (!(ed = *argp++) || *ed == '-')
77 adios(NULL, "missing argument to %s", argp[-2]);
85 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
86 adios(NULL, "missing argument to %s", argp[-2]);
90 if (!(form = *argp++) || *form == '-')
91 adios(NULL, "missing argument to %s", argp[-2]);
102 if (*cp == '+' || *cp == '@') {
104 adios(NULL, "only one folder at a time!");
106 folder = getcpy(expandfol(cp));
109 adios(NULL, "only one message at a time!");
117 if (form && (folder || msg))
118 adios(NULL, "can't mix forms and folders/msgs");
121 adios(NULL, "can't mix -use and +folder");
124 /* Don't copy; the draft shall get removed in the end. */
125 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
127 } else if (folder || msg) {
128 /* Take a message as the "form" for the new message. */
132 folder = getcurfol();
133 maildir = toabsdir(folder);
134 if (chdir(maildir) == NOTOK)
135 adios(maildir, "unable to change directory to");
136 /* read folder and create message structure */
137 if (!(mp = folder_read(folder)))
138 adios(NULL, "unable to read folder %s", folder);
139 /* check for empty folder */
141 adios(NULL, "no messages in %s", folder);
142 /* parse the message range/sequence/name and set SELECTED */
143 if (!m_convert(mp, msg))
145 seq_setprev(mp); /* set the previous-sequence */
147 adios(NULL, "only one message at a time!");
148 if ((in = open(form = getcpy(m_name(mp->lowsel)),
150 adios(form, "unable to open message");
152 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
153 if ((out = creat(drft, m_gmprot())) == NOTOK) {
154 adios(drft, "unable to create");
156 cpydata(in, out, form, drft);
161 fmtstr = new_fs(form, components);
162 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
163 if ((out = creat(drft, m_gmprot())) == NOTOK) {
164 adios(drft, "unable to create");
166 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
167 adios(drft, "error writing");
173 what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, cwd);