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