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);
60 adios(NULL, "-%s unknown", cp);
63 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
64 print_help(buf, switches, 1);
67 print_version(invo_name);
71 if (!(ed = *argp++) || *ed == '-')
72 adios(NULL, "missing argument to %s", argp[-2]);
76 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
77 adios(NULL, "missing argument to %s", argp[-2]);
81 if (!(form = *argp++) || *form == '-')
82 adios(NULL, "missing argument to %s", argp[-2]);
93 if (*cp == '+' || *cp == '@') {
95 adios(NULL, "only one folder at a time!");
97 folder = getcpy(expandfol(cp));
100 adios(NULL, "only one message at a time!");
108 if (form && (folder || msg))
109 adios(NULL, "can't mix forms and folders/msgs");
112 adios(NULL, "can't mix -use and +folder");
115 /* Don't copy; the draft shall get removed in the end. */
116 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
118 } else if (folder || msg) {
119 /* Take a message as the "form" for the new message. */
123 folder = getcurfol();
124 maildir = toabsdir(folder);
125 if (chdir(maildir) == NOTOK)
126 adios(maildir, "unable to change directory to");
127 /* read folder and create message structure */
128 if (!(mp = folder_read(folder)))
129 adios(NULL, "unable to read folder %s", folder);
130 /* check for empty folder */
132 adios(NULL, "no messages in %s", folder);
133 /* parse the message range/sequence/name and set SELECTED */
134 if (!m_convert(mp, msg))
136 seq_setprev(mp); /* set the previous-sequence */
138 adios(NULL, "only one message at a time!");
139 if ((in = open(form = getcpy(m_name(mp->lowsel)),
141 adios(form, "unable to open message");
143 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
144 if ((out = creat(drft, m_gmprot())) == NOTOK) {
145 adios(drft, "unable to create");
147 cpydata(in, out, form, drft);
152 fmtstr = new_fs(form, components);
153 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
154 if ((out = creat(drft, m_gmprot())) == NOTOK) {
155 adios(drft, "unable to create");
157 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
158 adios(drft, "error writing");
164 what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);