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 },
34 char *version=VERSION;
37 main(int argc, char **argv)
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, "");
49 invo_name = mhbasename(argv[0]);
51 /* read user profile/context */
54 arguments = getarguments(invo_name, argc, argv, 1);
57 while ((cp = *argp++)) {
59 switch (smatch(++cp, switches)) {
61 ambigsw(cp, switches);
64 adios(EX_USAGE, NULL, "-%s unknown", cp);
67 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
68 print_help(buf, switches, 1);
69 exit(argc == 2 ? EX_OK : EX_USAGE);
71 print_version(invo_name);
72 exit(argc == 2 ? EX_OK : EX_USAGE);
75 if (!(ed = *argp++) || *ed == '-') {
76 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
81 if (!(whatnowproc = *argp++) || *whatnowproc == '-') {
82 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
87 if (!(form = *argp++) || *form == '-') {
88 adios(EX_USAGE, NULL, "missing argument to %s", argp[-2]);
100 if (*cp == '+' || *cp == '@') {
102 adios(EX_USAGE, NULL, "only one folder at a time!");
104 folder = mh_xstrdup(expandfol(cp));
108 adios(EX_USAGE, NULL, "only one message at a time!");
115 cwd = mh_xstrdup(pwd());
117 if (form && (folder || msg)) {
118 adios(EX_USAGE, NULL, "can't mix forms and folders/msgs");
122 adios(EX_USAGE, NULL, "can't mix -use and +folder");
126 /* Don't copy; the draft shall get removed in the end. */
127 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
129 } else if (folder || msg) {
130 /* Take a message as the "form" for the new message. */
134 folder = getcurfol();
135 maildir = toabsdir(folder);
136 if (chdir(maildir) == NOTOK) {
137 adios(EX_OSERR, maildir, "unable to change directory to");
139 /* read folder and create message structure */
140 if (!(mp = folder_read(folder))) {
141 adios(EX_IOERR, NULL, "unable to read folder %s", folder);
143 /* check for empty folder */
144 if (mp->nummsg == 0) {
145 adios(EX_DATAERR, NULL, "no messages in %s", folder);
147 /* parse the message range/sequence/name and set SELECTED */
148 if (!m_convert(mp, msg)) {
151 seq_setprev(mp); /* set the previous-sequence */
152 if (mp->numsel > 1) {
153 adios(EX_USAGE, NULL, "only one message at a time!");
155 if ((in = open(form = mh_xstrdup(m_name(mp->lowsel)),
156 O_RDONLY)) == NOTOK) {
157 adios(EX_IOERR, form, "unable to open message");
160 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
161 if ((out = creat(drft, m_gmprot())) == NOTOK) {
162 adios(EX_CANTCREAT, drft, "unable to create");
164 cpydata(in, out, form, drft);
169 fmtstr = new_fs(form, components);
170 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
171 if ((out = creat(drft, m_gmprot())) == NOTOK) {
172 adios(EX_CANTCREAT, drft, "unable to create");
174 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
175 adios(EX_IOERR, drft, "error writing");
181 what_now(ed, use, drft, NULL, 0, NULLMP, NULL, cwd);