BSD 4_3_Reno release
[unix-history] / usr / src / usr.bin / ex / ex_vget.c
index 8d0a4a5..2f8ecc3 100644 (file)
@@ -1,5 +1,13 @@
-/* Copyright (c) 1980 Regents of the University of California */
-static char *sccsid = "@(#)ex_vget.c   4.2 %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)ex_vget.c   6.13 (Berkeley) 9/11/89";
+#endif not lint
+
 #include "ex.h"
 #include "ex_tty.h"
 #include "ex_vis.h"
 #include "ex.h"
 #include "ex_tty.h"
 #include "ex_vis.h"
@@ -15,11 +23,11 @@ static char *sccsid = "@(#)ex_vget.c        4.2 %G%";
  * Return the key.
  */
 ungetkey(c)
  * Return the key.
  */
 ungetkey(c)
-       char c;
+       int c;          /* mjm: char --> int */
 {
 
 {
 
-       if (Peekkey != ATTN)
-               Peekkey = c;
+       if (Peek_key != ATTN)
+               Peek_key = c;
 }
 
 /*
 }
 
 /*
@@ -27,7 +35,7 @@ ungetkey(c)
  */
 getkey()
 {
  */
 getkey()
 {
-       register char c;
+       register int c;         /* mjm: char --> int */
 
        do {
                c = getbr();
 
        do {
                c = getbr();
@@ -43,11 +51,13 @@ getkey()
 peekbr()
 {
 
 peekbr()
 {
 
-       Peekkey = getbr();
-       return (Peekkey == 0);
+       Peek_key = getbr();
+       return (Peek_key == 0);
 }
 
 short  precbksl;
 }
 
 short  precbksl;
+jmp_buf        readbuf;
+int    doingread = 0;
 
 /*
  * Get a keystroke, including a ^@.
 
 /*
  * Get a keystroke, including a ^@.
@@ -71,9 +81,9 @@ getbr()
        extern short slevel, ttyindes;
 
 getATTN:
        extern short slevel, ttyindes;
 
 getATTN:
-       if (Peekkey) {
-               c = Peekkey;
-               Peekkey = 0;
+       if (Peek_key) {
+               c = Peek_key;
+               Peek_key = 0;
                return (c);
        }
 #ifdef BEEHIVE
                return (c);
        }
 #ifdef BEEHIVE
@@ -101,7 +111,16 @@ getATTN:
        }
        flusho();
 again:
        }
        flusho();
 again:
-       if (read(slevel == 0 ? 0 : ttyindes, &ch, 1) != 1) {
+       if (setjmp(readbuf))
+               goto getATTN;
+       doingread = 1;
+#ifndef        vms
+       c = read(slevel == 0 ? 0 : ttyindes, &ch, 1);
+#else
+       c = vms_read(slevel == 0 ? 0 : ttyindes, &ch, 1);
+#endif
+       doingread = 0;
+       if (c != 1) {
                if (errno == EINTR)
                        goto getATTN;
                error("Input read error");
                if (errno == EINTR)
                        goto getATTN;
                error("Input read error");
@@ -118,7 +137,7 @@ again:
                        Peek2key = 0;
                        break;
                case 'q':       /* f2 -> ^C */
                        Peek2key = 0;
                        break;
                case 'q':       /* f2 -> ^C */
-                       c = CTRL(c);
+                       c = CTRL('c');
                        Peek2key = 0;
                        break;
                case 'p':       /* f1 -> esc */
                        Peek2key = 0;
                        break;
                case 'p':       /* f1 -> esc */
@@ -156,14 +175,14 @@ again:
                        }
                        if (precbksl == 2) {
                                if (!d) {
                        }
                        if (precbksl == 2) {
                                if (!d) {
-                                       Peekkey = c;
+                                       Peek_key = c;
                                        precbksl = 0;
                                        c = '\\';
                                }
                        } else if (d)
                                c = d;
                        else {
                                        precbksl = 0;
                                        c = '\\';
                                }
                        } else if (d)
                                c = d;
                        else {
-                               Peekkey = c;
+                               Peek_key = c;
                                precbksl = 0;
                                c = '\\';
                        }
                                precbksl = 0;
                                c = '\\';
                        }
@@ -197,8 +216,8 @@ getesc()
        c = getkey();
        switch (c) {
 
        c = getkey();
        switch (c) {
 
-       case CTRL(v):
-       case CTRL(q):
+       case CTRL('v'):
+       case CTRL('q'):
                c = getkey();
                return (c);
 
                c = getkey();
                return (c);
 
@@ -219,8 +238,8 @@ getesc()
 peekkey()
 {
 
 peekkey()
 {
 
-       Peekkey = getkey();
-       return (Peekkey);
+       Peek_key = getkey();
+       return (Peek_key);
 }
 
 /*
 }
 
 /*
@@ -241,7 +260,7 @@ readecho(c)
                vclrech(0);
        splitw++;
        vgoto(WECHO, 0);
                vclrech(0);
        splitw++;
        vgoto(WECHO, 0);
-       putchar(c);
+       ex_putchar(c);
        vclreol();
        vgoto(WECHO, 1);
        cursor = linebuf; linebuf[0] = 0; genbuf[0] = c;
        vclreol();
        vgoto(WECHO, 1);
        cursor = linebuf; linebuf[0] = 0; genbuf[0] = c;
@@ -251,23 +270,23 @@ readecho(c)
                vglobp = INS;
        }
        OP = Pline; Pline = normline;
                vglobp = INS;
        }
        OP = Pline; Pline = normline;
-       ignore(vgetline(0, genbuf + 1, &waste));
+       ignore(vgetline(0, genbuf + 1, &waste, c));
        if (Outchar == termchar)
        if (Outchar == termchar)
-               putchar('\n');
+               ex_putchar('\n');
        vscrap();
        Pline = OP;
        vscrap();
        Pline = OP;
-       if (Peekkey != ATTN && Peekkey != QUIT && Peekkey != CTRL(h)) {
+       if (Peek_key != ATTN && Peek_key != QUIT && Peek_key != CTRL('h')) {
                cursor = sc;
                vclreol();
                return (0);
        }
 blewit:
                cursor = sc;
                vclreol();
                return (0);
        }
 blewit:
-       OPeek = Peekkey==CTRL(h) ? 0 : Peekkey; Peekkey = 0;
+       OPeek = Peek_key==CTRL('h') ? 0 : Peek_key; Peek_key = 0;
        splitw = 0;
        vclean();
        vshow(dot, NOLINE);
        vnline(sc);
        splitw = 0;
        vclean();
        vshow(dot, NOLINE);
        vnline(sc);
-       Peekkey = OPeek;
+       Peek_key = OPeek;
        return (1);
 }
 
        return (1);
 }
 
@@ -307,13 +326,13 @@ addtext(cp)
 setDEL()
 {
 
 setDEL()
 {
 
-       setBUF(DEL);
+       ex_setBUF(DEL);
 }
 
 /*
  * Put text from cursor upto wcursor in BUF.
  */
 }
 
 /*
  * Put text from cursor upto wcursor in BUF.
  */
-setBUF(BUF)
+ex_setBUF(BUF)
        register char *BUF;
 {
        register int c;
        register char *BUF;
 {
        register int c;
@@ -356,14 +375,14 @@ noteit(must)
        if (WBOT == WECHO)
                vmoveitup(1, 1);
        vigoto(WECHO, 0);
        if (WBOT == WECHO)
                vmoveitup(1, 1);
        vigoto(WECHO, 0);
-       printf("%d %sline", notecnt, notesgn);
+       ex_printf("%d %sline", notecnt, notesgn);
        if (notecnt > 1)
        if (notecnt > 1)
-               putchar('s');
+               ex_putchar('s');
        if (*notenam) {
        if (*notenam) {
-               printf(" %s", notenam);
+               ex_printf(" %s", notenam);
                if (*(strend(notenam) - 1) != 'e')
                if (*(strend(notenam) - 1) != 'e')
-                       putchar('e');
-               putchar('d');
+                       ex_putchar('e');
+               ex_putchar('d');
        }
        vclreol();
        notecnt = 0;
        }
        vclreol();
        notecnt = 0;
@@ -386,7 +405,7 @@ beep()
        if (VB)
                vputp(VB, 0);
        else
        if (VB)
                vputp(VB, 0);
        else
-               vputc(CTRL(g));
+               vputc(CTRL('g'));
 }
 
 /*
 }
 
 /*
@@ -455,7 +474,7 @@ map(c,maps)
                                        if ((c=='#' ? peekkey() : fastpeekkey()) == 0) {
 #ifdef MDEBUG
                                                if (trace)
                                        if ((c=='#' ? peekkey() : fastpeekkey()) == 0) {
 #ifdef MDEBUG
                                                if (trace)
-                                                       fprintf(trace,"fpk=0: return '%c'",c);
+                                                       fprintf(trace,"fpk=0: will return '%c'",c);
 #endif
                                                /*
                                                 * Nothing waiting.  Push back
 #endif
                                                /*
                                                 * Nothing waiting.  Push back
@@ -467,7 +486,15 @@ map(c,maps)
                                                 * to undo part of an insertion
                                                 * so if in input mode don't.
                                                 */
                                                 * to undo part of an insertion
                                                 * so if in input mode don't.
                                                 */
+#ifdef MDEBUG
+                                               if (trace)
+                                                       fprintf(trace, "Call macpush, b %d %d %d\n", b[0], b[1], b[2]);
+#endif
                                                macpush(&b[1],maps == arrows);
                                                macpush(&b[1],maps == arrows);
+#ifdef MDEBUG
+                                               if (trace)
+                                                       fprintf(trace, "return %d\n", c);       
+#endif
                                                return(c);
                                        }
                                        *q = getkey();
                                                return(c);
                                        }
                                        *q = getkey();
@@ -511,11 +538,7 @@ int canundo;
 
        if (st==0 || *st==0)
                return;
 
        if (st==0 || *st==0)
                return;
-#ifdef notdef
-       if (!value(UNDOMACRO))
-               canundo = 0;
-#endif
-#ifdef TRACE
+#ifdef MDEBUG
        if (trace)
                fprintf(trace, "macpush(%s), canundo=%d\n",st,canundo);
 #endif
        if (trace)
                fprintf(trace, "macpush(%s), canundo=%d\n",st,canundo);
 #endif
@@ -565,6 +588,7 @@ vudump(s)
 char *s;
 {
        register line *p;
 char *s;
 {
        register line *p;
+       char savelb[1024];
 
        if (!trace) return;
 
 
        if (!trace) return;
 
@@ -572,10 +596,16 @@ char *s;
                s, undkind, vundkind, lineno(unddel), lineno(undap1), lineno(undap2));
        fprintf(trace, "  undadot=%d, dot=%d, dol=%d, unddol=%d, truedol=%d\n",
                lineno(undadot), lineno(dot), lineno(dol), lineno(unddol), lineno(truedol));
                s, undkind, vundkind, lineno(unddel), lineno(undap1), lineno(undap2));
        fprintf(trace, "  undadot=%d, dot=%d, dol=%d, unddol=%d, truedol=%d\n",
                lineno(undadot), lineno(dot), lineno(dol), lineno(unddol), lineno(truedol));
-       fprintf(trace, "  [");
-       for (p=zero+1; p<=truedol; p++)
+       fprintf(trace, "  [\n");
+       CP(savelb, linebuf);
+       fprintf(trace, "linebuf = '%s'\n", linebuf);
+       for (p=zero+1; p<=truedol; p++) {
                fprintf(trace, "%o ", *p);
                fprintf(trace, "%o ", *p);
+               getline(*p);
+               fprintf(trace, "'%s'\n", linebuf);
+       }
        fprintf(trace, "]\n");
        fprintf(trace, "]\n");
+       CP(linebuf, savelb);
 }
 #endif
 
 }
 #endif
 
@@ -609,6 +639,7 @@ vgetcnt()
 fastpeekkey()
 {
        int trapalarm();
 fastpeekkey()
 {
        int trapalarm();
+       void (*Oint)();
        register int c;
 
        /*
        register int c;
 
        /*
@@ -622,32 +653,45 @@ fastpeekkey()
         * as separate.  notimeout is provided for people who dislike such
         * nondeterminism.
         */
         * as separate.  notimeout is provided for people who dislike such
         * nondeterminism.
         */
-       if (value(TIMEOUT) && inopen >= 0) {
-               signal(SIGALRM, trapalarm);
-               alarm(1);
-       }
-       CATCH
-               c = peekkey();
 #ifdef MDEBUG
        if (trace)
 #ifdef MDEBUG
        if (trace)
-               fprintf(trace,"[OK]",c);
+               fprintf(trace,"\nfastpeekkey: ");
+#endif
+       Oint = signal(SIGINT, trapalarm);
+       CATCH
+               if (value(TIMEOUT) && inopen >= 0) {
+                       signal(SIGALRM, trapalarm);
+#ifdef MDEBUG
+                       alarm(10);
+                       if (trace)
+                               fprintf(trace, "set alarm ");
+#else
+                       alarm(1);
 #endif
 #endif
+               }
+               c = peekkey();
                alarm(0);
                alarm(0);
+#ifdef MDEBUG
+               if (trace)
+                       fprintf(trace,"[OK]");
+#endif
        ONERR
                c = 0;
 #ifdef MDEBUG
        ONERR
                c = 0;
 #ifdef MDEBUG
-       if (trace)
-               fprintf(trace,"[TOUT]",c);
+               if (trace)
+                       fprintf(trace,"[TIMEOUT]");
 #endif
        ENDCATCH
 #ifdef MDEBUG
        if (trace)
                fprintf(trace,"[fpk:%o]",c);
 #endif
 #endif
        ENDCATCH
 #ifdef MDEBUG
        if (trace)
                fprintf(trace,"[fpk:%o]",c);
 #endif
+       signal(SIGINT,Oint);
        return(c);
 }
 
 trapalarm() {
        alarm(0);
        return(c);
 }
 
 trapalarm() {
        alarm(0);
-       longjmp(vreslab,1);
+       if (vcatch)
+               longjmp(vreslab,1);
 }
 }