Augmented new_fs() with an additional argument for a default form.
[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 NWHATSW  6
27         { "nowhatnowproc", 0 },
28 #define VERSIONSW  7
29         { "version", 0 },
30 #define HELPSW  8
31         { "help", 0 },
32         { NULL, 0 }
33 };
34
35
36 int
37 main(int argc, char **argv)
38 {
39         int use = NOUSE, nedit = 0, nwhat = 0;
40         int in, out;
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;
46         char *nfs;
47
48
49 #ifdef LOCALE
50         setlocale(LC_ALL, "");
51 #endif
52         invo_name = mhbasename(argv[0]);
53
54         /* read user profile/context */
55         context_read();
56
57         arguments = getarguments(invo_name, argc, argv, 1);
58         argp = arguments;
59
60         while ((cp = *argp++)) {
61                 if (*cp == '-') {
62                         switch (smatch(++cp, switches)) {
63                         case AMBIGSW:
64                                 ambigsw(cp, switches);
65                                 done(1);
66                         case UNKWNSW:
67                                 adios(NULL, "-%s unknown", cp);
68
69                         case HELPSW:
70                                 snprintf(buf, sizeof(buf), "%s [+folder] [msg] [switches]", invo_name);
71                                 print_help(buf, switches, 1);
72                                 done(1);
73                         case VERSIONSW:
74                                 print_version(invo_name);
75                                 done(1);
76
77                         case EDITRSW:
78                                 if (!(ed = *argp++) || *ed == '-')
79                                         adios(NULL, "missing argument to %s", argp[-2]);
80                                 nedit = 0;
81                                 continue;
82                         case NEDITSW:
83                                 nedit++;
84                                 continue;
85
86                         case WHATSW:
87                                 if (!(whatnowproc = *argp++) || *whatnowproc == '-')
88                                         adios(NULL, "missing argument to %s", argp[-2]);
89                                 nwhat = 0;
90                                 continue;
91                         case NWHATSW:
92                                 nwhat++;
93                                 continue;
94
95                         case FORMSW:
96                                 if (!(form = *argp++) || *form == '-')
97                                         adios(NULL, "missing argument to %s", argp[-2]);
98                                 continue;
99
100                         case USESW:
101                                 use++;
102                                 continue;
103                         case NUSESW:
104                                 use = NOUSE;
105                                 continue;
106                         }
107                 }
108                 if (*cp == '+' || *cp == '@') {
109                         if (folder)
110                                 adios(NULL, "only one folder at a time!");
111                         else
112                                 folder = getcpy(expandfol(cp));
113                 } else {
114                         if (msg)
115                                 adios(NULL, "only one message at a time!");
116                         else
117                                 msg = cp;
118                 }
119         }
120
121         cwd = getcpy(pwd());
122
123         if (form && (folder || msg))
124                 adios(NULL, "can't mix forms and folders/msgs");
125
126         if (use && folder)
127                 adios(NULL, "can't mix -use and +folder");
128
129         if (use) {
130                 /* Don't copy; the draft shall get removed in the end. */
131                 strncpy(drft, m_draft(msg ? msg : seq_cur), sizeof(drft));
132
133         } else if (folder || msg) {
134                 /* Take a message as the "form" for the new message. */
135                 if (!msg)
136                         msg = seq_cur;
137                 if (!folder)
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 */
146                 if (mp->nummsg == 0)
147                         adios(NULL, "no messages in %s", folder);
148                 /* parse the message range/sequence/name and set SELECTED */
149                 if (!m_convert(mp, msg))
150                         done(1);
151                 seq_setprev(mp);  /* set the previous-sequence */
152                 if (mp->numsel > 1)
153                         adios(NULL, "only one message at a time!");
154                 if ((in = open(form = getcpy(m_name(mp->lowsel)),
155                                 O_RDONLY)) == NOTOK)
156                         adios(form, "unable to open message");
157
158                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
159                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
160                         adios(drft, "unable to create");
161                 }
162                 cpydata(in, out, form, drft);
163                 close(in);
164                 close(out);
165
166         } else {
167                 nfs = new_fs(form, NULL, components, NULL);
168                 strncpy(drft, m_draft(seq_beyond), sizeof(drft));
169                 if ((out = creat(drft, m_gmprot())) == NOTOK) {
170                         adios(drft, "unable to create");
171                 }
172                 if (write(out, nfs, strlen(nfs)) != strlen(nfs)) {
173                         adios(drft, "error writing");
174                 }
175                 close(out);
176         }
177
178         context_save();
179
180         if (nwhat) {
181                 done(0);
182         }
183         what_now(ed, nedit, use, drft, NULL, 0, NULLMP, NULL, cwd);
184         done(1);
185         return 1;
186 }