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;
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]);
94 if (!(form = *argp++) || *form == '-')
95 adios(NULL, "missing argument to %s", argp[-2]);
106 if (*cp == '+' || *cp == '@') {
108 adios(NULL, "only one folder at a time!");
110 folder = getcpy(expandfol(cp));
113 adios(NULL, "only one message at a time!");
121 if (form && (folder || msg))
122 adios(NULL, "can't mix forms and folders/msgs");
125 adios(NULL, "can't mix -use and +folder");
128 /* Don't copy; the draft shall get removed in the end. */
129 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
131 } else if (folder || msg) {
132 /* Take a message as the "form" for the new message. */
136 folder = getcurfol();
137 maildir = toabsdir(folder);
138 if (chdir(maildir) == NOTOK)
139 adios(maildir, "unable to change directory to");
140 /* read folder and create message structure */
141 if (!(mp = folder_read(folder)))
142 adios(NULL, "unable to read folder %s", folder);
143 /* check for empty folder */
145 adios(NULL, "no messages in %s", folder);
146 /* parse the message range/sequence/name and set SELECTED */
147 if (!m_convert(mp, msg))
149 seq_setprev(mp); /* set the previous-sequence */
151 adios(NULL, "only one message at a time!");
152 if ((in = open(form = getcpy(m_name(mp->lowsel)),
154 adios(form, "unable to open message");
156 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
157 if ((out = creat(drft, m_gmprot())) == NOTOK) {
158 adios(drft, "unable to create");
160 cpydata(in, out, form, drft);
165 in = open_form(&form, components);
167 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
168 if ((out = creat(drft, m_gmprot())) == NOTOK) {
169 adios(drft, "unable to create");
171 cpydata(in, out, form, drft);
181 what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, cwd);