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