3 * prompter.c -- simple prompting editor front-end
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
39 static struct swit switches[] = {
69 static struct termios tio;
70 # define ERASE tio.c_cc[VERASE]
71 # define KILL tio.c_cc[VKILL]
72 # define INTR tio.c_cc[VINTR]
75 static struct termio tio;
76 # define ERASE tio.c_cc[VERASE]
77 # define KILL tio.c_cc[VKILL]
78 # define INTR tio.c_cc[VINTR]
80 static struct sgttyb tio;
81 static struct tchars tc;
82 # define ERASE tio.sg_erase
83 # define KILL tio.sg_kill
84 # define INTR tc.t_intrc
88 static int wtuser = 0;
89 static int sigint = 0;
90 static jmp_buf sigenv;
95 int getln (char *, int);
96 static int chrcnv (char *);
97 static void chrdsp (char *, char);
98 static RETSIGTYPE intrser (int);
102 main (int argc, char **argv)
104 int body = 1, prepend = 1, rapid = 0;
105 int doteof = 0, fdi, fdo, i, state;
106 char *cp, *drft = NULL, *erasep = NULL;
107 char *killp = NULL, name[NAMESZ], field[BUFSIZ];
108 char buffer[BUFSIZ], tmpfil[BUFSIZ];
109 char **arguments, **argp;
113 setlocale(LC_ALL, "");
115 invo_name = r1bindex (argv[0], '/');
117 /* read user profile/context */
120 arguments = getarguments (invo_name, argc, argv, 1);
123 while ((cp = *argp++))
125 switch (smatch (++cp, switches)) {
127 ambigsw (cp, switches);
130 adios (NULL, "-%s unknown", cp);
133 snprintf (buffer, sizeof(buffer), "%s [switches] file",
135 print_help (buffer, switches, 1);
138 print_version(invo_name);
142 if (!(erasep = *argp++) || *erasep == '-')
143 adios (NULL, "missing argument to %s", argp[-2]);
146 if (!(killp = *argp++) || *killp == '-')
147 adios (NULL, "missing argument to %s", argp[-2]);
184 adios (NULL, "usage: %s [switches] file", invo_name);
185 if ((in = fopen (drft, "r")) == NULL)
186 adios (drft, "unable to open");
188 strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
189 if ((out = fopen (tmpfil, "w")) == NULL)
190 adios (tmpfil, "unable to create");
191 chmod (tmpfil, 0600);
194 * Are we changing the kill or erase character?
196 if (killp || erasep) {
197 #ifdef HAVE_TERMIOS_H
198 cc_t save_erase, save_kill;
200 int save_erase, save_kill;
203 /* get the current terminal attributes */
204 #ifdef HAVE_TERMIOS_H
207 # ifdef HAVE_TERMIO_H
208 ioctl(0, TCGETA, &tio);
210 ioctl (0, TIOCGETP, (char *) &tio);
211 ioctl (0, TIOCGETC, (char *) &tc);
215 /* save original kill, erase character for later */
219 /* set new kill, erase character in terminal structure */
220 KILL = killp ? chrcnv (killp) : save_kill;
221 ERASE = erasep ? chrcnv (erasep) : save_erase;
223 /* set the new terminal attributes */
224 #ifdef HAVE_TERMIOS_H
225 tcsetattr(0, TCSADRAIN, &tio);
227 # ifdef HAVE_TERMIO_H
228 ioctl(0, TCSETAW, &tio);
230 ioctl (0, TIOCSETN, (char *) &tio);
234 /* print out new kill erase characters */
235 chrdsp ("erase", ERASE);
236 chrdsp (", kill", KILL);
237 chrdsp (", intr", INTR);
242 * We set the kill and erase character back to original
243 * setup in terminal structure so we can easily
244 * restore it upon exit.
251 SIGNAL2 (SIGINT, intrser);
254 * Loop through the lines of the draft skeleton.
256 for (state = FLD;;) {
257 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
262 * Check if the value of field contains anything
263 * other than space or tab.
265 for (cp = field; *cp; cp++)
266 if (*cp != ' ' && *cp != '\t')
269 /* If so, just add header line to draft */
270 if (*cp++ != '\n' || *cp != 0) {
271 printf ("%s:%s", name, field);
272 fprintf (out, "%s:%s", name, field);
273 while (state == FLDPLUS) {
275 m_getfld (state, name, field, sizeof(field), in);
276 printf ("%s", field);
277 fprintf (out, "%s", field);
280 /* Else, get value of header field */
281 printf ("%s: ", name);
283 i = getln (field, sizeof(field));
286 if (killp || erasep) {
287 #ifdef HAVE_TERMIOS_H
288 tcsetattr(0, TCSADRAIN, &tio);
291 ioctl (0, TCSETA, &tio);
293 ioctl (0, TIOCSETN, (char *) &tio);
300 if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
301 fprintf (out, "%s:", name);
303 if (field[0] != ' ' && field[0] != '\t')
305 fprintf (out, "%s", field);
307 && (i = getln (field, sizeof(field))) >= 0);
313 if (state == FLDEOF) { /* moby hack */
314 fprintf (out, "--------\n");
315 printf ("--------\n");
327 fprintf (out, "--------\n");
328 if (field[0] == 0 || !prepend)
329 printf ("--------\n");
331 if (prepend && body) {
332 printf ("\n--------Enter initial text\n\n");
335 getln (buffer, sizeof(buffer));
336 if (doteof && buffer[0] == '.' && buffer[1] == '\n')
340 fprintf (out, "%s", buffer);
345 fprintf (out, "%s", field);
346 if (!rapid && !sigint)
347 printf ("%s", field);
348 } while (state == BODY &&
349 (state = m_getfld (state, name, field, sizeof(field), in)));
350 if (prepend || !body)
353 printf ("\n--------Enter additional text\n\n");
358 getln (field, sizeof(field));
359 if (doteof && field[0] == '.' && field[1] == '\n')
363 fprintf (out, "%s", field);
368 adios (NULL, "skeleton is poorly formatted");
374 printf ("--------\n");
379 SIGNAL (SIGINT, SIG_IGN);
381 if (killp || erasep) {
382 #ifdef HAVE_TERMIOS_H
383 tcsetattr(0, TCSADRAIN, &tio);
385 # ifdef HAVE_TERMIO_H
386 ioctl (0, TCSETAW, &tio);
388 ioctl (0, TIOCSETN, (char *) &tio);
393 if ((fdi = open (tmpfil, O_RDONLY)) == NOTOK)
394 adios (tmpfil, "unable to re-open");
395 if ((fdo = creat (drft, m_gmprot ())) == NOTOK)
396 adios (drft, "unable to write");
397 cpydata (fdi, fdo, tmpfil, drft);
402 context_save (); /* save the context file */
408 getln (char *buffer, int n)
416 switch (setjmp (sigenv)) {
431 switch (c = getchar ()) {
434 longjmp (sigenv, DONE);
437 if (cp[-1] == QUOTE) {
459 #ifndef RELIABLE_SIGNALS
460 SIGNAL (SIGINT, intrser);
464 longjmp (sigenv, NOTOK);
472 return (*cp != QUOTE ? *cp : m_atoi (++cp));
477 chrdsp (char *s, char c)
480 if (c < ' ' || c == 0177)
481 printf ("^%c", c ^ 0100);