1 /* prompter.c - prompting editor front-end */
3 static char ident[] = "@(#)$Id: prompter.c,v 1.9 1992/12/15 00:20:22 jromine Exp $";
12 #include <sys/types.h>
15 #include <sys/ioctl.h>
30 #endif /* not CKILL */
33 #endif /* not CERASE */
37 static struct swit switches[] = {
75 #define ERASE sg.sg_erase
76 #define KILL sg.sg_kill
77 static struct sgttyb sg;
79 #define INTR tc.t_intrc
80 static struct tchars tc;
82 #define ERASE sg.c_cc[VERASE]
83 #define KILL sg.c_cc[VKILL]
84 #define INTR sg.c_cc[VINTR]
85 static struct termio sg;
89 static TYPESIG intrser ();
91 static int wtuser = 0;
92 static int sigint = 0;
95 static jmp_buf sigenv;
128 setlocale(LC_ALL, "");
130 invo_name = r1bindex (argv[0], '/');
131 if ((cp = m_find (invo_name)) != NULL) {
132 ap = brkstring (cp = getcpy (cp), " ", "\n");
133 ap = copyip (ap, arguments);
137 (void) copyip (argv + 1, ap);
144 switch (smatch (++cp, switches)) {
146 ambigsw (cp, switches);
149 adios (NULLCP, "-%s unknown", cp);
151 (void) sprintf (buffer, "%s [switches] file", invo_name);
152 help (buffer, switches);
156 if (!(erasep = *argp++) || *erasep == '-')
157 adios (NULLCP, "missing argument to %s", argp[-2]);
160 if (!(killp = *argp++) || *killp == '-')
161 adios (NULLCP, "missing argument to %s", argp[-2]);
199 adios (NULLCP, "usage: %s [switches] file", invo_name);
200 if ((in = fopen (drft, "r")) == NULL)
201 adios (drft, "unable to open");
203 (void) strcpy (tmpfil, m_tmpfil (invo_name));
204 if ((out = fopen (tmpfil, "w")) == NULL)
205 adios (tmpfil, "unable to create");
206 (void) chmod (tmpfil, 0600);
208 if (killp || erasep) {
218 (void) ioctl (0, TIOCGETP, (char *) &sg);
219 (void) ioctl (0, TIOCGETC, (char *) &tc);
221 (void) ioctl(0, TCGETA, &sg);
225 KILL = killp ? chrcnv (killp) : skill;
226 ERASE = erasep ? chrcnv (erasep) : serase;
228 (void) ioctl (0, TIOCSETN, (char *) &sg);
230 (void) ioctl(0, TCSETAW, &sg);
233 chrdsp ("erase", ERASE);
234 chrdsp (", kill", KILL);
235 chrdsp (", intr", INTR);
236 (void) putchar ('\n');
237 (void) fflush (stdout);
246 setsig (SIGINT, intrser);
248 for (state = FLD;;) {
249 switch (state = m_getfld (state, name, field, sizeof field, in)) {
253 for (cp = field; *cp; cp++)
254 if (*cp != ' ' && *cp != '\t')
256 if (*cp++ != '\n' || *cp != 0) {
257 printf ("%s:%s", name, field);
258 fprintf (out, "%s:%s", name, field);
259 while (state == FLDPLUS) {
261 m_getfld (state, name, field, sizeof field, in);
262 printf ("%s", field);
263 fprintf (out, "%s", field);
267 printf ("%s: ", name);
268 (void) fflush (stdout);
269 i = getln (field, sizeof field);
274 (void) ioctl (0, TIOCSETN, (char *) &sg);
276 (void) ioctl (0, TCSETA, &sg);
278 (void) unlink (tmpfil);
281 if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
282 fprintf (out, "%s:", name);
284 if (field[0] != ' ' && field[0] != '\t')
285 (void) putc (' ', out);
286 fprintf (out, "%s", field);
288 && (i = getln (field, sizeof field)) >= 0);
293 if (state == FLDEOF) {/* moby hack */
294 fprintf (out, "--------\n");
295 printf ("--------\n");
307 fprintf (out, "--------\n");
308 if (field[0] == 0 || !prepend)
309 printf ("--------\n");
311 if (prepend && body) {
312 printf ("\n--------Enter initial text\n\n");
313 (void) fflush (stdout);
315 (void) getln (buffer, sizeof buffer);
316 if (doteof && buffer[0] == '.' && buffer[1] == '\n')
320 fprintf (out, "%s", buffer);
325 fprintf (out, "%s", field);
326 if (!rapid && !sigint)
327 printf ("%s", field);
328 } while (state == BODY &&
329 (state = m_getfld (state, name, field, sizeof field, in)));
330 if (prepend || !body)
333 printf ("\n--------Enter additional text\n\n");
336 (void) fflush (stdout);
338 (void) getln (field, sizeof field);
339 if (doteof && field[0] == '.' && field[1] == '\n')
343 fprintf (out, "%s", field);
348 adios (NULLCP, "skeleton is poorly formatted");
354 printf ("--------\n");
355 (void) fflush (stdout);
360 (void) signal (SIGINT, SIG_IGN);
366 (void) ioctl (0, TIOCSETN, (char *) &sg);
368 (void) ioctl (0, TCSETAW, &sg);
371 if ((fdi = open (tmpfil, 0)) == NOTOK)
372 adios (tmpfil, "unable to re-open");
373 if ((fdo = creat (drft, m_gmprot ())) == NOTOK)
374 adios (drft, "unable to write");
375 cpydata (fdi, fdo, tmpfil, drft);
378 (void) unlink (tmpfil);
400 switch (setjmp (sigenv)) {
416 switch (c = getchar ()) {
420 return (errno != EINTR ? 0 : NOTOK);
423 longjmp (sigenv, DONE);
427 if (cp[-1] == QUOTE) {
448 static TYPESIG intrser (i)
452 (void) signal (SIGINT, intrser);
457 longjmp (sigenv, NOTOK);
466 return (*cp != QUOTE ? *cp : m_atoi (++cp));
475 if (c < ' ' || c == 0177)
476 printf ("^%c", c ^ 0100);