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 },
27 { "nowhatnowproc", 0 },
37 main(int argc, char **argv)
39 int use = NOUSE, nedit = 0, nwhat = 0;
41 char *cp, *cwd, *maildir;
42 char *ed = NULL, *form = NULL;
43 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
44 char drft[BUFSIZ], **argp, **arguments;
45 struct msgs *mp = NULL;
50 setlocale(LC_ALL, "");
52 invo_name = mhbasename(argv[0]);
54 /* read user profile/context */
57 arguments = getarguments(invo_name, argc, argv, 1);
60 while ((cp = *argp++)) {
62 switch (smatch(++cp, switches)) {
64 ambigsw(cp, switches);
67 adios(NULL, "-%s unknown", cp);
70 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
71 print_help(buf, switches, 1);
74 print_version(invo_name);
78 if (!(ed = *argp++) || *ed == '-')
79 adios(NULL, "missing argument to %s", argp[-2]);
87 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
88 adios(NULL, "missing argument to %s", argp[-2]);
96 if (!(form = *argp++) || *form == '-')
97 adios(NULL, "missing argument to %s", argp[-2]);
108 if (*cp == '+' || *cp == '@') {
110 adios(NULL, "only one folder at a time!");
112 folder = getcpy(expandfol(cp));
115 adios(NULL, "only one message at a time!");
123 if (form && (folder || msg))
124 adios(NULL, "can't mix forms and folders/msgs");
127 adios(NULL, "can't mix -use and +folder");
130 /* Don't copy; the draft shall get removed in the end. */
131 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
133 } else if (folder || msg) {
134 /* Take a message as the "form" for the new message. */
138 folder = getcurfol();
139 maildir = toabsdir(folder);
140 if (chdir(maildir) == NOTOK)
141 adios(maildir, "unable to change directory to");
142 /* read folder and create message structure */
143 if (!(mp = folder_read(folder)))
144 adios(NULL, "unable to read folder %s", folder);
145 /* check for empty folder */
147 adios(NULL, "no messages in %s", folder);
148 /* parse the message range/sequence/name and set SELECTED */
149 if (!m_convert(mp, msg))
151 seq_setprev(mp); /* set the previous-sequence */
153 adios(NULL, "only one message at a time!");
154 if ((in = open(form = getcpy(m_name(mp->lowsel)),
156 adios(form, "unable to open message");
158 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
159 if ((out = creat(drft, m_gmprot())) == NOTOK) {
160 adios(drft, "unable to create");
162 cpydata(in, out, form, drft);
167 fmtstr = new_fs(form, components);
168 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
169 if ((out = creat(drft, m_gmprot())) == NOTOK) {
170 adios(drft, "unable to create");
172 if (write(out, fmtstr, strlen(fmtstr)) != strlen(fmtstr)) {
173 adios(drft, "error writing");
183 what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, cwd);