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.
16 static struct swit switches[] = {
18 { "editor editor", 0 },
20 { "form formfile", 0 },
26 { "whatnowproc program", 0 },
36 main(int argc, char **argv)
40 char *cp, *cwd, *maildir;
41 char *ed = NULL, *form = NULL;
42 char *folder = NULL, *msg = NULL, buf[BUFSIZ];
43 char drft[BUFSIZ], **argp, **arguments;
44 struct msgs *mp = NULL;
47 setlocale(LC_ALL, "");
48 invo_name = mhbasename(argv[0]);
50 /* read user profile/context */
53 arguments = getarguments(invo_name, argc, argv, 1);
56 while ((cp = *argp++)) {
58 switch (smatch(++cp, switches)) {
60 ambigsw(cp, switches);
63 adios(EX_USAGE, NULL, "-%s unknown", cp);
66 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
67 print_help(buf, switches, 1);
68 exit(argc == 2 ? EX_OK : EX_USAGE);
70 print_version(invo_name);
71 exit(argc == 2 ? EX_OK : EX_USAGE);
74 if (!(ed = *argp++) || *ed == '-') {
75 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
80 if (!(whatnowproc = *argp++) || *whatnowproc == '-') {
81 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
86 if (!(form = *argp++) || *form == '-') {
87 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
99 if (*cp == '+' || *cp == '@') {
101 adios(EX_USAGE, NULL, "only one folder at a time!");
103 folder = getcpy(expandfol(cp));
107 adios(EX_USAGE, NULL, "only one message at a time!");
116 if (form && (folder || msg)) {
117 adios(EX_USAGE, NULL, "can't mix forms and folders/msgs");
121 adios(EX_USAGE, NULL, "can't mix -use and +folder");
125 /* Don't copy; the draft shall get removed in the end. */
126 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
128 } else if (folder || msg) {
129 /* Take a message as the "form" for the new message. */
133 folder = getcurfol();
134 maildir = toabsdir(folder);
135 if (chdir(maildir) == NOTOK) {
136 adios(EX_OSERR, maildir, "unable to change directory to");
138 /* read folder and create message structure */
139 if (!(mp = folder_read(folder))) {
140 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
142 /* check for empty folder */
143 if (mp->nummsg == 0) {
144 adios(EX_DATAERR, NULL, "no messages in %s", folder);
146 /* parse the message range/sequence/name and set SELECTED */
147 if (!m_convert(mp, msg)) {
150 seq_setprev(mp); /* set the previous-sequence */
151 if (mp->numsel > 1) {
152 adios(EX_USAGE, NULL, "only one message at a time!");
154 if ((in = open(form = getcpy(m_name(mp->lowsel)),
155 O_RDONLY)) == NOTOK) {
156 adios(EX_IOERR, form, "unable to open message");
159 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
160 if ((out = creat(drft, m_gmprot())) == NOTOK) {
161 adios(EX_CANTCREAT, drft, "unable to create");
163 cpydata(in, out, form, drft);
168 fmtstr = new_fs(form, components);
169 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
170 if ((out = creat(drft, m_gmprot())) == NOTOK) {
171 adios(EX_CANTCREAT, drft, "unable to create");
173 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
174 adios(EX_IOERR, drft, "error writing");
180 what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);