Removed -nowhatnowproc switch as it does nothing useful.
[mmh] / uip / comp.c
1 /*
2 ** comp.c -- compose a message
3 **
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11 #include <fcntl.h>
12
13 static struct swit switches[] = {
14 #define EDITRSW  0
15         { "editor editor", 0 },
16 #define NEDITSW  1
17         { "noedit", 0 },
18 #define FORMSW  2
19         { "form formfile", 0 },
20 #define USESW  3
21         { "use", 0 },
22 #define NUSESW  4
23         { "nouse", 0 },
24 #define WHATSW  5
25         { "whatnowproc program", 0 },
26 #define VERSIONSW  6
27         { "version", 0 },
28 #define HELPSW  7
29         { "help", 0 },
30         { NULL, 0 }
31 };
32
33
34 int
35 main(int argc, char **argv)
36 {
37         int use = NOUSE, nedit = 0;
38         int in, out;
39         char *cp, *cwd, *maildir;
40         char *ed = NULL, *form = NULL;
41         char *folder = NULL, *msg = NULL, buf[BUFSIZ];
42         char drft[BUFSIZ], **argp, **arguments;
43         struct msgs *mp = NULL;
44         char *fmtstr;
45
46
47 #ifdef LOCALE
48         setlocale(LC_ALL, "");
49 #endif
50         invo_name = mhbasename(argv[0]);
51
52         /* read user profile/context */
53         context_read();
54
55         arguments = getarguments(invo_name, argc, argv, 1);
56         argp = arguments;
57
58         while ((cp = *argp++)) {
59                 if (*cp == '-') {
60                         switch (smatch(++cp, switches)) {
61                         case AMBIGSW:
62                                 ambigsw(cp, switches);
63                                 done(1);
64                         case UNKWNSW:
65                                 adios(NULL, "-%s unknown", cp);
66
67                         case HELPSW:
68                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
69                                 print_help(buf, switches, 1);
70                                 done(1);
71                         case VERSIONSW:
72                                 print_version(invo_name);
73                                 done(1);
74
75                         case EDITRSW:
76                                 if (!(ed = *argp++) || *ed == '-')
77                                         adios(NULL, "missing argument to %s", argp[-2]);
78                                 nedit = 0;
79                                 continue;
80                         case NEDITSW:
81                                 nedit++;
82                                 continue;
83
84                         case WHATSW:
85                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
86                                         adios(NULL, "missing argument to %s", argp[-2]);
87                                 continue;
88
89                         case FORMSW:
90                                 if (!(form = *argp++) || *form == '-')
91                                         adios(NULL, "missing argument to %s", argp[-2]);
92                                 continue;
93
94                         case USESW:
95                                 use++;
96                                 continue;
97                         case NUSESW:
98                                 use = NOUSE;
99                                 continue;
100                         }
101                 }
102                 if (*cp == '+' || *cp == '@') {
103                         if (folder)
104                                 adios(NULL, "only one folder at a time!");
105                         else
106                                 folder = getcpy(expandfol(cp));
107                 } else {
108                         if (msg)
109                                 adios(NULL, "only one message at a time!");
110                         else
111                                 msg = cp;
112                 }
113         }
114
115         cwd = getcpy(pwd());
116
117         if (form && (folder || msg))
118                 adios(NULL, "can't mix forms and folders/msgs");
119
120         if (use && folder)
121                 adios(NULL, "can't mix -use and +folder");
122
123         if (use) {
124                 /* Don't copy; the draft shall get removed in the end. */
125                 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
126
127         } else if (folder || msg) {
128                 /* Take a message as the "form" for the new message. */
129                 if (!msg)
130                         msg = seq_cur;
131                 if (!folder)
132                         folder = getcurfol();
133                 maildir = toabsdir(folder);
134                 if (chdir(maildir) == NOTOK)
135                         adios(maildir, "unable to change directory to");
136                 /* read folder and create message structure */
137                 if (!(mp = folder_read(folder)))
138                         adios(NULL, "unable to read folder %s", folder);
139                 /* check for empty folder */
140                 if (mp->nummsg == 0)
141                         adios(NULL, "no messages in %s", folder);
142                 /* parse the message range/sequence/name and set SELECTED */
143                 if (!m_convert(mp, msg))
144                         done(1);
145                 seq_setprev(mp);  /* set the previous-sequence */
146                 if (mp->numsel > 1)
147                         adios(NULL, "only one message at a time!");
148                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
149                                 O_RDONLY)) == NOTOK)
150                         adios(form, "unable to open message");
151
152                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
153                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
154                         adios(drft, "unable to create");
155                 }
156                 cpydata(in, out, form, drft);
157                 close(in);
158                 close(out);
159
160         } else {
161                 fmtstr = new_fs(form, components);
162                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
163                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
164                         adios(drft, "unable to create");
165                 }
166                 if (write(out, fmtstr, strlen(fmtstr)) != (int)strlen(fmtstr)) {
167                         adios(drft, "error writing");
168                 }
169                 close(out);
170         }
171
172         context_save();
173         what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, cwd);
174         done(1);
175         return 1;
176 }