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