Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / RCS / prompter.c,v
1 head    1.9;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
6
7
8 1.9
9 date    92.12.15.00.20.22;      author jromine; state Exp;
10 branches;
11 next    1.8;
12
13 1.8
14 date    92.11.04.00.56.19;      author jromine; state Exp;
15 branches;
16 next    1.7;
17
18 1.7
19 date    92.01.31.22.24.08;      author jromine; state Exp;
20 branches;
21 next    1.6;
22
23 1.6
24 date    90.04.05.14.59.16;      author sources; state Exp;
25 branches;
26 next    1.5;
27
28 1.5
29 date    90.03.17.09.49.14;      author sources; state Exp;
30 branches;
31 next    1.4;
32
33 1.4
34 date    90.02.08.13.46.35;      author sources; state Exp;
35 branches;
36 next    1.3;
37
38 1.3
39 date    90.02.06.13.27.36;      author sources; state Exp;
40 branches;
41 next    1.2;
42
43 1.2
44 date    90.02.05.15.02.29;      author sources; state Exp;
45 branches;
46 next    1.1;
47
48 1.1
49 date    90.01.29.16.10.46;      author sources; state Exp;
50 branches;
51 next    ;
52
53
54 desc
55 @@
56
57
58 1.9
59 log
60 @endif sugar
61 @
62 text
63 @/* prompter.c - prompting editor front-end */
64 #ifndef lint
65 static char ident[] = "@@(#)$Id: prompter.c,v 1.8 1992/11/04 00:56:19 jromine Exp jromine $";
66 #endif  /* lint */
67
68 #include "../h/mh.h"
69 #include <stdio.h>
70 #include <errno.h>
71 #ifndef SYS5
72 #include <sgtty.h>
73 #else   /* SYS5 */
74 #include <sys/types.h>
75 #include <termio.h>
76 #ifndef NOIOCTLH
77 #include <sys/ioctl.h>
78 #endif  /* NOIOCTLH */
79 #endif  /* SYS5 */
80 #ifdef  BSD42
81 #include <setjmp.h>
82 #endif  /* BSD42 */
83 #include <signal.h>
84 #ifdef LOCALE
85 #include        <locale.h>
86 #endif
87
88
89 #define QUOTE   '\\'
90 #ifndef CKILL
91 #define CKILL   '@@'
92 #endif  /* not CKILL */
93 #ifndef CERASE
94 #define CERASE  '#'
95 #endif  /* not CERASE */
96
97 /* \f */
98
99 static struct swit switches[] = {
100 #define ERASESW 0
101     "erase chr", 0,
102 #define KILLSW  1
103     "kill chr", 0,
104
105 #define PREPSW  2
106     "prepend", 0,       
107 #define NPREPSW 3
108     "noprepend", 0,     
109
110 #define RAPDSW  4
111     "rapid", 0, 
112 #define NRAPDSW 5
113     "norapid", 0,       
114
115 #define BODYSW  6
116     "body", -4,
117 #define NBODYSW 7
118     "nobody", -6,
119
120 #define DOTSW   8
121     "doteof", 0,
122 #define NDOTSW  9
123     "nodoteof", 0,
124
125 #define HELPSW  10
126     "help", 4,          
127
128     NULL, 0
129 };
130
131 /* \f */
132
133 extern int  errno;
134
135
136 #ifndef SYS5
137 #define ERASE   sg.sg_erase
138 #define KILL    sg.sg_kill
139 static struct sgttyb    sg;
140
141 #define INTR    tc.t_intrc
142 static struct tchars    tc;
143 #else   /* SYS5 */
144 #define ERASE   sg.c_cc[VERASE]
145 #define KILL    sg.c_cc[VKILL]
146 #define INTR    sg.c_cc[VINTR]
147 static struct termio    sg;
148 #endif  /* SYS5 */
149
150
151 static TYPESIG  intrser ();
152
153 static int  wtuser = 0;
154 static int  sigint = 0;
155
156 #ifdef  BSD42
157 static jmp_buf sigenv;
158 #endif  /* BSD42 */
159
160 /* \f */
161
162 /* ARGSUSED */
163
164 main (argc, argv)
165 int     argc;
166 char   *argv[];
167 {
168     int     body = 1,
169             prepend = 1,
170             rapid = 0,
171             doteof = 0,
172             fdi,
173             fdo,
174             i,
175             state;
176     char   *cp,
177            *drft = NULL,
178            *erasep = NULL,
179            *killp = NULL,
180             name[NAMESZ],
181             field[BUFSIZ],
182             buffer[BUFSIZ],
183             tmpfil[BUFSIZ],
184           **ap,
185            *arguments[MAXARGS],
186           **argp;
187     FILE *in, *out;
188
189 #ifdef LOCALE
190         setlocale(LC_ALL, "");
191 #endif
192     invo_name = r1bindex (argv[0], '/');
193     if ((cp = m_find (invo_name)) != NULL) {
194         ap = brkstring (cp = getcpy (cp), " ", "\n");
195         ap = copyip (ap, arguments);
196     }
197     else
198         ap = arguments;
199     (void) copyip (argv + 1, ap);
200     argp = arguments;
201
202 /* \f */
203
204     while (cp = *argp++)
205         if (*cp == '-')
206             switch (smatch (++cp, switches)) {
207                 case AMBIGSW: 
208                     ambigsw (cp, switches);
209                     done (1);
210                 case UNKWNSW: 
211                     adios (NULLCP, "-%s unknown", cp);
212                 case HELPSW: 
213                     (void) sprintf (buffer, "%s [switches] file", invo_name);
214                     help (buffer, switches);
215                     done (1);
216
217                 case ERASESW: 
218                     if (!(erasep = *argp++) || *erasep == '-')
219                         adios (NULLCP, "missing argument to %s", argp[-2]);
220                     continue;
221                 case KILLSW: 
222                     if (!(killp = *argp++) || *killp == '-')
223                         adios (NULLCP, "missing argument to %s", argp[-2]);
224                     continue;
225
226                 case PREPSW: 
227                     prepend++;
228                     continue;
229                 case NPREPSW: 
230                     prepend = 0;
231                     continue;
232
233                 case RAPDSW: 
234                     rapid++;
235                     continue;
236                 case NRAPDSW: 
237                     rapid = 0;
238                     continue;
239
240                 case BODYSW: 
241                     body++;
242                     continue;
243                 case NBODYSW: 
244                     body = 0;
245                     continue;
246
247                 case DOTSW: 
248                     doteof++;
249                     continue;
250                 case NDOTSW: 
251                     doteof = 0;
252                     continue;
253             }
254         else
255             if (!drft)
256                 drft = cp;
257
258 /* \f */
259
260     if (!drft)
261         adios (NULLCP, "usage: %s [switches] file", invo_name);
262     if ((in = fopen (drft, "r")) == NULL)
263         adios (drft, "unable to open");
264
265     (void) strcpy (tmpfil, m_tmpfil (invo_name));
266     if ((out = fopen (tmpfil, "w")) == NULL)
267         adios (tmpfil, "unable to create");
268     (void) chmod (tmpfil, 0600);
269
270     if (killp || erasep) {
271 #ifndef SYS5
272         int    serase,
273                skill;
274 #else   /* SYS5 */
275         char   serase,
276                skill;
277 #endif  /* SYS5 */
278
279 #ifndef SYS5
280         (void) ioctl (0, TIOCGETP, (char *) &sg);
281         (void) ioctl (0, TIOCGETC, (char *) &tc);
282 #else   /* SYS5 */
283         (void) ioctl(0, TCGETA, &sg);
284 #endif  /* SYS5 */
285         skill = KILL;
286         serase = ERASE;
287         KILL = killp ? chrcnv (killp) : skill;
288         ERASE = erasep ? chrcnv (erasep) : serase;
289 #ifndef SYS5
290         (void) ioctl (0, TIOCSETN, (char *) &sg);
291 #else   /* SYS5 */
292         (void) ioctl(0, TCSETAW, &sg);
293 #endif  /* SYS5 */
294
295         chrdsp ("erase", ERASE);
296         chrdsp (", kill", KILL);
297         chrdsp (", intr", INTR);
298         (void) putchar ('\n');
299         (void) fflush (stdout);
300
301         KILL = skill;
302         ERASE = serase;
303     }
304
305 /* \f */
306
307     sigint = 0;
308     setsig (SIGINT, intrser);
309
310     for (state = FLD;;) {
311         switch (state = m_getfld (state, name, field, sizeof field, in)) {
312             case FLD: 
313             case FLDEOF: 
314             case FLDPLUS: 
315                 for (cp = field; *cp; cp++)
316                     if (*cp != ' ' && *cp != '\t')
317                         break;
318                 if (*cp++ != '\n' || *cp != 0) {
319                     printf ("%s:%s", name, field);
320                     fprintf (out, "%s:%s", name, field);
321                     while (state == FLDPLUS) {
322                         state =
323                             m_getfld (state, name, field, sizeof field, in);
324                         printf ("%s", field);
325                         fprintf (out, "%s", field);
326                     }
327                 }
328                 else {
329                     printf ("%s: ", name);
330                     (void) fflush (stdout);
331                     i = getln (field, sizeof field);
332                     if (i == -1) {
333 abort: ;
334                         if (killp || erasep)
335 #ifndef SYS5
336                             (void) ioctl (0, TIOCSETN, (char *) &sg);
337 #else   /* SYS5 */
338                             (void) ioctl (0, TCSETA, &sg);
339 #endif  /* SYS5 */
340                         (void) unlink (tmpfil);
341                         done (1);
342                     }
343                     if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
344                         fprintf (out, "%s:", name);
345                         do {
346                             if (field[0] != ' ' && field[0] != '\t')
347                                 (void) putc (' ', out);
348                             fprintf (out, "%s", field);
349                         } while (i == 1
350                                     && (i = getln (field, sizeof field)) >= 0);
351                         if (i == -1)
352                             goto abort;
353                     }
354                 }
355                 if (state == FLDEOF) {/* moby hack */
356                     fprintf (out, "--------\n");
357                     printf ("--------\n");
358                     if (!body)
359                         break;
360                     goto no_body;
361                 }
362                 continue;
363
364             case BODY: 
365             case BODYEOF:
366             case FILEEOF: 
367                 if (!body)
368                     break;
369                 fprintf (out, "--------\n");
370                 if (field[0] == 0 || !prepend)
371                     printf ("--------\n");
372                 if (field[0]) {
373                     if (prepend && body) {
374                         printf ("\n--------Enter initial text\n\n");
375                         (void) fflush (stdout);
376                         for (;;) {
377                             (void) getln (buffer, sizeof buffer);
378                             if (doteof && buffer[0] == '.' && buffer[1] == '\n')
379                                 break;
380                             if (buffer[0] == 0)
381                                 break;
382                             fprintf (out, "%s", buffer);
383                         }
384                     }
385
386                     do {
387                         fprintf (out, "%s", field);
388                         if (!rapid && !sigint)
389                             printf ("%s", field);
390                     } while (state == BODY &&
391                             (state = m_getfld (state, name, field, sizeof field, in)));
392                     if (prepend || !body)
393                         break;
394                     else
395                         printf ("\n--------Enter additional text\n\n");
396                 }
397 no_body: ;
398                 (void) fflush (stdout);
399                 for (;;) {
400                     (void) getln (field, sizeof field);
401                     if (doteof && field[0] == '.' && field[1] == '\n')
402                         break;
403                     if (field[0] == 0)
404                         break;
405                     fprintf (out, "%s", field);
406                 }
407                 break;
408
409             default: 
410                 adios (NULLCP, "skeleton is poorly formatted");
411         }
412         break;
413     }
414
415     if (body)
416         printf ("--------\n");
417     (void) fflush (stdout);
418
419     (void) fclose (in);
420     (void) fclose (out);
421
422     (void) signal (SIGINT, SIG_IGN);
423
424 /* \f */
425
426     if (killp || erasep)
427 #ifndef SYS5
428         (void) ioctl (0, TIOCSETN, (char *) &sg);
429 #else   /* SYS5 */
430         (void) ioctl (0, TCSETAW, &sg);
431 #endif  /* SYS5 */
432
433     if ((fdi = open (tmpfil, 0)) == NOTOK)
434         adios (tmpfil, "unable to re-open");
435     if ((fdo = creat (drft, m_gmprot ())) == NOTOK)
436         adios (drft, "unable to write");
437     cpydata (fdi, fdo, tmpfil, drft);
438     (void) close (fdi);
439     (void) close (fdo);
440     (void) unlink (tmpfil);
441
442     m_update ();
443
444     done (0);
445 }
446
447 /* \f */
448
449 getln (buffer, n)
450 char   *buffer;
451 int     n;
452 {
453     int     c;
454     char   *cp;
455
456     cp = buffer;
457     *cp = 0;
458
459 #ifndef BSD42
460     wtuser = 1;
461 #else   /* BSD42 */
462     switch (setjmp (sigenv)) {
463         case OK: 
464             wtuser = 1;
465             break;
466
467         case DONE: 
468             wtuser = 0;
469             return 0;
470
471         default: 
472             wtuser = 0;
473             return NOTOK;
474     }
475 #endif  /* BSD42 */
476
477     for (;;)
478         switch (c = getchar ()) {
479             case EOF: 
480 #ifndef BSD42
481                 wtuser = 0;
482                 return (errno != EINTR ? 0 : NOTOK);
483 #else   /* BSD42 */
484                 clearerr (stdin);
485                 longjmp (sigenv, DONE);
486 #endif  /* BSD42 */
487
488             case '\n': 
489                 if (cp[-1] == QUOTE) {
490                     cp[-1] = c;
491                     wtuser = 0;
492                     return 1;
493                 }
494                 *cp++ = c;
495                 *cp = 0;
496                 wtuser = 0;
497                 return 0;
498
499             default: 
500                 if (cp < buffer + n)
501                     *cp++ = c;
502                 *cp = 0;
503         }
504 }
505
506 /* \f */
507
508 /* ARGSUSED */
509
510 static  TYPESIG intrser (i)
511 int    i;
512 {
513 #ifndef BSD42
514     (void) signal (SIGINT, intrser);
515     if (!wtuser)
516         sigint++;
517 #else   /* BSD42 */
518     if (wtuser)
519         longjmp (sigenv, NOTOK);
520     sigint++;
521 #endif  /* BSD42 */
522 }
523
524
525 chrcnv (cp)
526 register char   *cp;
527 {
528     return (*cp != QUOTE ? *cp : m_atoi (++cp));
529 }
530
531
532 chrdsp (s, c)
533 char   *s,
534         c;
535 {
536     printf ("%s ", s);
537     if (c < ' ' || c == 0177)
538         printf ("^%c", c ^ 0100);
539     else
540         printf ("%c", c);
541 }
542 @
543
544
545 1.8
546 log
547 @LOCALE
548 TYPESIG
549 @
550 text
551 @d3 2
552 a4 2
553 static char ident[] = "@@(#)$Id: prompter.c,v 1.7 1992/01/31 22:24:08 jromine Exp jromine $";
554 #endif  lint
555 d11 1
556 a11 1
557 #else   SYS5
558 d16 2
559 a17 2
560 #endif  NOIOCTLH
561 #endif  SYS5
562 d20 1
563 a20 1
564 #endif  BSD42
565 d30 1
566 a30 1
567 #endif  not CKILL
568 d33 1
569 a33 1
570 #endif  not CERASE
571 d81 1
572 a81 1
573 #else   SYS5
574 d86 1
575 a86 1
576 #endif  SYS5
577 d96 1
578 a96 1
579 #endif  BSD42
580 d212 1
581 a212 1
582 #else   SYS5
583 d215 1
584 a215 1
585 #endif  SYS5
586 d220 1
587 a220 1
588 #else   SYS5
589 d222 1
590 a222 1
591 #endif  SYS5
592 d229 1
593 a229 1
594 #else   SYS5
595 d231 1
596 a231 1
597 #endif  SYS5
598 d275 1
599 a275 1
600 #else   SYS5
601 d277 1
602 a277 1
603 #endif  SYS5
604 d367 1
605 a367 1
606 #else   SYS5
607 d369 1
608 a369 1
609 #endif  SYS5
610 d399 1
611 a399 1
612 #else   BSD42
613 d413 1
614 a413 1
615 #endif  BSD42
616 d421 1
617 a421 1
618 #else   BSD42
619 d424 1
620 a424 1
621 #endif  BSD42
622 d455 1
623 a455 1
624 #else   BSD42
625 d459 1
626 a459 1
627 #endif  BSD42
628 @
629
630
631 1.7
632 log
633 @kerberos
634 @
635 text
636 @d3 1
637 a3 1
638 static char ident[] = "@@(#)$Id: prompter.c,v 1.6 1990/04/05 14:59:16 sources Exp jromine $";
639 d22 3
640 d89 1
641 a89 1
642 static int      intrser ();
643 d127 3
644 d448 1
645 a448 1
646 static  int intrser (i)
647 @
648
649
650 1.6
651 log
652 @add ID
653 @
654 text
655 @d3 1
656 a3 1
657 static char ident[] = "@@(#)$Id:$";
658 d63 1
659 a63 1
660     NULL, NULL
661 d250 1
662 a250 1
663                 if (*cp++ != '\n' || *cp != NULL) {
664 d275 1
665 a275 1
666                     if (i != 0 || (field[0] != '\n' && field[0] != NULL)) {
667 d302 1
668 a302 1
669                 if (field[0] == NULL || !prepend)
670 d312 1
671 a312 1
672                             if (buffer[0] == NULL)
673 d335 1
674 a335 1
675                     if (field[0] == NULL)
676 d389 1
677 a389 1
678     *cp = NULL;
679 d427 1
680 a427 1
681                 *cp = NULL;
682 d434 1
683 a434 1
684                 *cp = NULL;
685 @
686
687
688 1.5
689 log
690 @fix for -nobody from jeff honig
691 @
692 text
693 @d2 3
694 @
695
696
697 1.4
698 log
699 @let "." terminate prompter.
700 @
701 text
702 @d296 2
703 @
704
705
706 1.3
707 log
708 @ANSI Compilance
709 @
710 text
711 @d52 6
712 a57 1
713 #define HELPSW  8
714 d103 1
715 d175 7
716 d305 2
717 d328 2
718 @
719
720
721 1.2
722 log
723 @*** empty log message ***
724 @
725 text
726 @d78 1
727 a78 1
728 int     intrser ();
729 @
730
731
732 1.1
733 log
734 @Initial revision
735 @
736 text
737 @d11 1
738 d13 1
739 @