]> diplodocus.org Git - nmh/blob - uip/prompter.c
Replaced hard-coded value with MAX_DELIMITER_SIZE in m_getfld.c.
[nmh] / uip / prompter.c
1
2 /*
3 * prompter.c -- simple prompting editor front-end
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <errno.h>
14 #include <signal.h>
15 #include <setjmp.h>
16
17 #include <termios.h>
18
19 #define QUOTE '\\'
20
21 #ifndef CKILL
22 # define CKILL '@'
23 #endif
24
25 #ifndef CERASE
26 # define CERASE '#'
27 #endif
28
29 static struct swit switches[] = {
30 #define ERASESW 0
31 { "erase chr", 0 },
32 #define KILLSW 1
33 { "kill chr", 0 },
34 #define PREPSW 2
35 { "prepend", 0 },
36 #define NPREPSW 3
37 { "noprepend", 0 },
38 #define RAPDSW 4
39 { "rapid", 0 },
40 #define NRAPDSW 5
41 { "norapid", 0 },
42 #define BODYSW 6
43 { "body", -4 },
44 #define NBODYSW 7
45 { "nobody", -6 },
46 #define DOTSW 8
47 { "doteof", 0 },
48 #define NDOTSW 9
49 { "nodoteof", 0 },
50 #define VERSIONSW 10
51 { "version", 0 },
52 #define HELPSW 11
53 { "help", 0 },
54 { NULL, 0 }
55 };
56
57
58 static struct termios tio;
59 #define ERASE tio.c_cc[VERASE]
60 #define KILL tio.c_cc[VKILL]
61 #define INTR tio.c_cc[VINTR]
62
63 static int wtuser = 0;
64 static int sigint = 0;
65 static jmp_buf sigenv;
66
67 /*
68 * prototypes
69 */
70 int getln (char *, int);
71 static int chrcnv (char *);
72 static void chrdsp (char *, char);
73 static void intrser (int);
74
75
76 int
77 main (int argc, char **argv)
78 {
79 int body = 1, prepend = 1, rapid = 0;
80 int doteof = 0, fdi, fdo, i, state;
81 char *cp, *drft = NULL, *erasep = NULL;
82 char *killp = NULL, name[NAMESZ], field[BUFSIZ];
83 char buffer[BUFSIZ], tmpfil[BUFSIZ];
84 char **arguments, **argp;
85 FILE *in, *out;
86 char *tfile = NULL;
87 m_getfld_state_t gstate;
88
89 #ifdef LOCALE
90 setlocale(LC_ALL, "");
91 #endif
92 invo_name = r1bindex (argv[0], '/');
93
94 /* read user profile/context */
95 context_read();
96
97 arguments = getarguments (invo_name, argc, argv, 1);
98 argp = arguments;
99
100 while ((cp = *argp++))
101 if (*cp == '-') {
102 switch (smatch (++cp, switches)) {
103 case AMBIGSW:
104 ambigsw (cp, switches);
105 done (1);
106 case UNKWNSW:
107 adios (NULL, "-%s unknown", cp);
108
109 case HELPSW:
110 snprintf (buffer, sizeof(buffer), "%s [switches] file",
111 invo_name);
112 print_help (buffer, switches, 1);
113 done (0);
114 case VERSIONSW:
115 print_version(invo_name);
116 done (0);
117
118 case ERASESW:
119 if (!(erasep = *argp++) || *erasep == '-')
120 adios (NULL, "missing argument to %s", argp[-2]);
121 continue;
122 case KILLSW:
123 if (!(killp = *argp++) || *killp == '-')
124 adios (NULL, "missing argument to %s", argp[-2]);
125 continue;
126
127 case PREPSW:
128 prepend++;
129 continue;
130 case NPREPSW:
131 prepend = 0;
132 continue;
133
134 case RAPDSW:
135 rapid++;
136 continue;
137 case NRAPDSW:
138 rapid = 0;
139 continue;
140
141 case BODYSW:
142 body++;
143 continue;
144 case NBODYSW:
145 body = 0;
146 continue;
147
148 case DOTSW:
149 doteof++;
150 continue;
151 case NDOTSW:
152 doteof = 0;
153 continue;
154 }
155 } else {
156 if (!drft)
157 drft = cp;
158 }
159
160 if (!drft)
161 adios (NULL, "usage: %s [switches] file", invo_name);
162 if ((in = fopen (drft, "r")) == NULL)
163 adios (drft, "unable to open");
164
165 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
166 if (tfile == NULL) adios("prompter", "unable to create temporary file");
167 chmod (tmpfil, 0600);
168 strncpy (tmpfil, tfile, sizeof(tmpfil));
169
170 /*
171 * Are we changing the kill or erase character?
172 */
173 if (killp || erasep) {
174 cc_t save_erase, save_kill;
175
176 /* get the current terminal attributes */
177 tcgetattr(0, &tio);
178
179 /* save original kill, erase character for later */
180 save_kill = KILL;
181 save_erase = ERASE;
182
183 /* set new kill, erase character in terminal structure */
184 KILL = killp ? chrcnv (killp) : save_kill;
185 ERASE = erasep ? chrcnv (erasep) : save_erase;
186
187 /* set the new terminal attributes */
188 tcsetattr(0, TCSADRAIN, &tio);
189
190 /* print out new kill erase characters */
191 chrdsp ("erase", ERASE);
192 chrdsp (", kill", KILL);
193 chrdsp (", intr", INTR);
194 putchar ('\n');
195 fflush (stdout);
196
197 /*
198 * We set the kill and erase character back to original
199 * setup in terminal structure so we can easily
200 * restore it upon exit.
201 */
202 KILL = save_kill;
203 ERASE = save_erase;
204 }
205
206 sigint = 0;
207 SIGNAL2 (SIGINT, intrser);
208
209 /*
210 * Loop through the lines of the draft skeleton.
211 */
212 m_getfld_state_init (&gstate);
213 for (;;) {
214 int fieldsz = sizeof field;
215 switch (state = m_getfld (gstate, name, field, &fieldsz, in)) {
216 case FLD:
217 case FLDPLUS:
218 /*
219 * Check if the value of field contains anything
220 * other than space or tab.
221 */
222 for (cp = field; *cp; cp++)
223 if (*cp != ' ' && *cp != '\t')
224 break;
225
226 /* If so, just add header line to draft */
227 if (*cp++ != '\n' || *cp != 0) {
228 printf ("%s:%s", name, field);
229 fprintf (out, "%s:%s", name, field);
230 while (state == FLDPLUS) {
231 fieldsz = sizeof field;
232 state = m_getfld (gstate, name, field, &fieldsz, in);
233 printf ("%s", field);
234 fprintf (out, "%s", field);
235 }
236 } else {
237 /* Else, get value of header field */
238 printf ("%s: ", name);
239 fflush (stdout);
240 i = getln (field, sizeof(field));
241 if (i == -1) {
242 abort:
243 if (killp || erasep) {
244 tcsetattr(0, TCSADRAIN, &tio);
245 }
246 unlink (tmpfil);
247 done (1);
248 }
249 if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
250 fprintf (out, "%s:", name);
251 do {
252 if (field[0] != ' ' && field[0] != '\t')
253 putc (' ', out);
254 fprintf (out, "%s", field);
255 } while (i == 1
256 && (i = getln (field, sizeof(field))) >= 0);
257 if (i == -1)
258 goto abort;
259 }
260 }
261
262 continue;
263
264 case BODY:
265 case FILEEOF:
266 if (!body)
267 break;
268 fprintf (out, "--------\n");
269 if (field[0] == 0 || !prepend)
270 printf ("--------\n");
271 if (field[0]) {
272 if (prepend && body) {
273 printf ("\n--------Enter initial text\n\n");
274 fflush (stdout);
275 for (;;) {
276 getln (buffer, sizeof(buffer));
277 if (doteof && buffer[0] == '.' && buffer[1] == '\n')
278 break;
279 if (buffer[0] == 0)
280 break;
281 fprintf (out, "%s", buffer);
282 }
283 }
284
285 do {
286 fprintf (out, "%s", field);
287 if (!rapid && !sigint)
288 printf ("%s", field);
289 } while (state == BODY &&
290 (fieldsz = sizeof field,
291 state = m_getfld (gstate, name, field, &fieldsz, in)));
292 if (prepend || !body)
293 break;
294 else
295 printf ("\n--------Enter additional text\n\n");
296 }
297
298 fflush (stdout);
299 for (;;) {
300 getln (field, sizeof(field));
301 if (doteof && field[0] == '.' && field[1] == '\n')
302 break;
303 if (field[0] == 0)
304 break;
305 fprintf (out, "%s", field);
306 }
307 break;
308
309 default:
310 adios (NULL, "skeleton is poorly formatted");
311 }
312 break;
313 }
314 m_getfld_state_destroy (&gstate);
315
316 if (body)
317 printf ("--------\n");
318
319 fflush (stdout);
320 fclose (in);
321 fclose (out);
322 SIGNAL (SIGINT, SIG_IGN);
323
324 if (killp || erasep) {
325 tcsetattr(0, TCSADRAIN, &tio);
326 }
327
328 if ((fdi = open (tmpfil, O_RDONLY)) == NOTOK)
329 adios (tmpfil, "unable to re-open");
330 if ((fdo = creat (drft, m_gmprot ())) == NOTOK)
331 adios (drft, "unable to write");
332 cpydata (fdi, fdo, tmpfil, drft);
333 close (fdi);
334 close (fdo);
335 unlink (tmpfil);
336
337 context_save (); /* save the context file */
338 done (0);
339 return 1;
340 }
341
342
343 int
344 getln (char *buffer, int n)
345 {
346 int c;
347 char *cp;
348
349 cp = buffer;
350 *cp = 0;
351
352 switch (setjmp (sigenv)) {
353 case OK:
354 wtuser = 1;
355 break;
356
357 case DONE:
358 wtuser = 0;
359 return 0;
360
361 default:
362 wtuser = 0;
363 return NOTOK;
364 }
365
366 for (;;) {
367 switch (c = getchar ()) {
368 case EOF:
369 clearerr (stdin);
370 longjmp (sigenv, DONE);
371
372 case '\n':
373 if (cp[-1] == QUOTE) {
374 cp[-1] = c;
375 wtuser = 0;
376 return 1;
377 }
378 *cp++ = c;
379 *cp = 0;
380 wtuser = 0;
381 return 0;
382
383 default:
384 if (cp < buffer + n)
385 *cp++ = c;
386 *cp = 0;
387 }
388 }
389 }
390
391
392 static void
393 intrser (int i)
394 {
395 NMH_UNUSED (i);
396
397 if (wtuser)
398 longjmp (sigenv, NOTOK);
399 sigint++;
400 }
401
402
403 static int
404 chrcnv (char *cp)
405 {
406 return (*cp != QUOTE ? *cp : m_atoi (++cp));
407 }
408
409
410 static void
411 chrdsp (char *s, char c)
412 {
413 printf ("%s ", s);
414 if (c < ' ' || c == 0177)
415 printf ("^%c", c ^ 0100);
416 else
417 printf ("%c", c);
418 }