e1f0bff8f37576a03e1fb6ef33cc1b2dc607efa2
[mmh] / uip / prompter.c
1 /*
2 ** prompter.c -- simple prompting editor front-end
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 <fcntl.h>
11 #include <h/signals.h>
12 #include <errno.h>
13 #include <signal.h>
14 #include <setjmp.h>
15
16 static struct swit switches[] = {
17 #define PREPSW  0
18         { "prepend", 0 },
19 #define NPREPSW  1
20         { "noprepend", 2 },
21 #define RAPDSW  2
22         { "rapid", 0 },
23 #define NRAPDSW  3
24         { "norapid", 2 },
25 #define BODYSW  4
26         { "body", 0 },
27 #define NBODYSW  5
28         { "nobody", 2 },
29 #define VERSIONSW 6
30         { "Version", 0 },
31 #define HELPSW  7
32         { "help", 0 },
33         { NULL, 0 }
34 };
35
36
37 static int wtuser = 0;
38 static int sigint = 0;
39 static jmp_buf sigenv;
40
41 /*
42 ** prototypes
43 */
44 int getln(char *, int);
45 static void intrser(int);
46
47
48 int
49 main(int argc, char **argv)
50 {
51         int qbody = 1, prepend = 1, rapid = 0;
52         int fdi, fdo, i, state;
53         char *cp, *drft = NULL;
54         char name[NAMESZ], field[BUFSIZ];
55         char buffer[BUFSIZ], tmpfil[BUFSIZ];
56         char **arguments, **argp;
57         FILE *in, *out;
58         char *tfile = NULL;
59
60 #ifdef LOCALE
61         setlocale(LC_ALL, "");
62 #endif
63         invo_name = mhbasename(argv[0]);
64
65         /* read user profile/context */
66         context_read();
67
68         arguments = getarguments(invo_name, argc, argv, 1);
69         argp = arguments;
70
71         while ((cp = *argp++)) {
72                 if (*cp == '-') {
73                         switch (smatch(++cp, switches)) {
74                         case AMBIGSW:
75                                 ambigsw(cp, switches);
76                                 done(1);
77                         case UNKWNSW:
78                                 adios(NULL, "-%s unknown", cp);
79
80                         case HELPSW:
81                                 snprintf(buffer, sizeof(buffer),
82                                                 "%s [switches] file",
83                                                 invo_name);
84                                 print_help(buffer, switches, 1);
85                                 done(1);
86                         case VERSIONSW:
87                                 print_version(invo_name);
88                                 done(1);
89
90                         case PREPSW:
91                                 prepend++;
92                                 continue;
93                         case NPREPSW:
94                                 prepend = 0;
95                                 continue;
96
97                         case RAPDSW:
98                                 rapid++;
99                                 continue;
100                         case NRAPDSW:
101                                 rapid = 0;
102                                 continue;
103
104                         case BODYSW:
105                                 qbody++;
106                                 continue;
107                         case NBODYSW:
108                                 qbody = 0;
109                                 continue;
110                         }
111                 } else if (!drft) {
112                         drft = cp;
113                 }
114         }
115
116         if (!drft)
117                 adios(NULL, "usage: %s [switches] file", invo_name);
118         if ((in = fopen(drft, "r")) == NULL)
119                 adios(drft, "unable to open");
120
121         tfile = m_mktemp2(NULL, invo_name, NULL, &out);
122         if (tfile == NULL)
123                 adios("prompter", "unable to create temporary file");
124         chmod(tmpfil, 0600);
125         strncpy(tmpfil, tfile, sizeof(tmpfil));
126
127         sigint = 0;
128         SIGNAL2(SIGINT, intrser);
129
130         /*
131         ** Loop through the lines of the draft skeleton.
132         */
133         for (state = FLD;;) {
134                 switch (state = m_getfld(state, name, field, sizeof(field),
135                                 in)) {
136                 case FLD:
137                 case FLDEOF:
138                 case FLDPLUS:
139                         /*
140                         ** Check if the value of field contains
141                         ** anything other than space or tab.
142                         */
143                         for (cp = field; *cp; cp++)
144                                 if (*cp != ' ' && *cp != '\t')
145                                         break;
146
147                         /* If so, just add header line to draft */
148                         if (*cp++ != '\n' || *cp) {
149                                 printf("%s:%s", name, field);
150                                 fprintf(out, "%s:%s", name, field);
151                                 while (state == FLDPLUS) {
152                                         state = m_getfld(state, name, field,
153                                                         sizeof(field), in);
154                                         printf("%s", field);
155                                         fprintf(out, "%s", field);
156                                 }
157                         } else {
158                                 /* Else, get value of header field */
159                                 printf("%s: ", name);
160                                 fflush(stdout);
161                                 i = getln(field, sizeof(field));
162                                 if (i == -1) {
163 abort:
164                                         unlink(tmpfil);
165                                         done(1);
166                                 }
167                                 if (i || (field[0]!='\n' && field[0]!='\0')) {
168                                         fprintf(out, "%s:", name);
169                                         do {
170                                                 if (field[0] != ' ' && field[0] != '\t')
171                                                         putc(' ', out);
172                                                 fprintf(out, "%s", field);
173                                         } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
174                                         if (i == -1)
175                                                 goto abort;
176                                 }
177                         }
178
179                         if (state == FLDEOF) {  /* moby hack */
180                                 /* draft has no body separator; only headers */
181                                 fprintf(out, "--------\n");
182                                 if (!qbody)
183                                         break;
184                                 printf("--------\n");
185                                 goto has_no_body;
186                         }
187                         continue;
188
189                 case BODY:
190                 case BODYEOF:
191                 case FILEEOF:
192                         fprintf(out, "--------\n");
193                         if (qbody) {
194                                 if (!*field) {
195                                         printf("--------\n");
196                                         goto has_no_body;
197                                 }
198
199                                 if (prepend) {
200                                         printf("--------Enter initial text\n");
201                                         fflush(stdout);
202                                         for (;;) {
203                                                 getln(buffer, sizeof(buffer));
204                                                 if (!*buffer)
205                                                         break;
206                                                 fprintf(out, "%s", buffer);
207                                         }
208                                 } else {
209                                         printf("--------\n");
210                                 }
211                         }
212
213                         do {
214                                 fprintf(out, "%s", field);
215                                 if (!rapid && !sigint)
216                                         printf("%s", field);
217                         } while (state == BODY &&
218                                         (state = m_getfld(state, name,
219                                         field, sizeof(field), in)));
220
221                         if (prepend || !qbody)
222                                 break;
223
224                         printf("--------Enter additional text\n");
225 has_no_body:
226                         fflush(stdout);
227                         for (;;) {
228                                 getln(field, sizeof(field));
229                                 if (!*field)
230                                         break;
231                                 fprintf(out, "%s", field);
232                         }
233                         break;
234
235                 default:
236                         adios(NULL, "skeleton is poorly formatted");
237                 }
238                 break;
239         }
240
241         if (qbody)
242                 printf("--------\n");
243
244         fflush(stdout);
245         fclose(in);
246         fclose(out);
247         SIGNAL(SIGINT, SIG_IGN);
248
249         if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
250                 adios(tmpfil, "unable to re-open");
251         if ((fdo = creat(drft, m_gmprot())) == NOTOK)
252                 adios(drft, "unable to write");
253         cpydata(fdi, fdo, tmpfil, drft);
254         close(fdi);
255         close(fdo);
256         unlink(tmpfil);
257
258         context_save();  /* save the context file */
259         done(0);
260         return 1;
261 }
262
263
264 int
265 getln(char *buffer, int n)
266 {
267         int c;
268         char *cp;
269
270         cp = buffer;
271         *cp = '\0';
272
273         switch (setjmp(sigenv)) {
274         case OK:
275                 wtuser = 1;
276                 break;
277
278         case DONE:
279                 wtuser = 0;
280                 return 0;
281
282         default:
283                 wtuser = 0;
284                 return NOTOK;
285         }
286
287         for (;;) {
288                 switch (c = getchar()) {
289                 case EOF:
290                         clearerr(stdin);
291                         longjmp(sigenv, DONE);
292
293                 case '\n':
294                         if (cp[-1] == '\\') {
295                                 cp[-1] = c;
296                                 wtuser = 0;
297                                 return 1;
298                         }
299                         *cp++ = c;
300                         *cp = '\0';
301                         wtuser = 0;
302                         return 0;
303
304                 default:
305                         if (cp < buffer + n)
306                                 *cp++ = c;
307                         *cp = '\0';
308                 }
309         }
310 }
311
312
313 static void
314 intrser(int i)
315 {
316         if (wtuser)
317                 longjmp(sigenv, NOTOK);
318         sigint++;
319 }