fix ctrl macro for ANSI C
[unix-history] / usr / src / usr.bin / window / ttgeneric.c
index 886d64b..10c4254 100644 (file)
@@ -1,71 +1,91 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)ttgeneric.c 3.12 83/08/23";
+static char sccsid[] = "@(#)ttgeneric.c        3.32 %G%";
 #endif
 
 #endif
 
+/*
+ * Copyright (c) 1983 Regents of the University of California,
+ * All rights reserved.  Redistribution permitted subject to
+ * the terms of the Berkeley Software License Agreement.
+ */
+
 #include "ww.h"
 #include "tt.h"
 
 #include "ww.h"
 #include "tt.h"
 
-char *tgoto();
+char PC, *BC, *UP;
+short ospeed;
 
 
-char gen_frame[16] = {
+       /* normal frame */
+short gen_frame[16] = {
        ' ', '|', '-', '+',
        '|', '|', '+', '+',
        '-', '+', '-', '+',
        '+', '+', '+', '+'
 };
 
        ' ', '|', '-', '+',
        '|', '|', '+', '+',
        '-', '+', '-', '+',
        '+', '+', '+', '+'
 };
 
-int gen_row, gen_col;
-char gen_modes;
-char gen_insert;
+       /* ANSI graphics frame */
+#define G (WWM_GRP << WWC_MSHIFT)
+short ansi_frame[16] = {
+       ' ',    'x'|G,  'Q'|G,  'm'|G,
+       'x'|G,  'x'|G,  'l'|G,  't'|G,
+       'q'|G,  'j'|G,  'q'|G,  'v'|G,
+       'k'|G,  'u'|G,  'w'|G,  'n'|G
+};
+struct tt_str ansi_AS = {
+       "\033(0", 3
+};
 
 
-char *gen_CM;
-char *gen_IM;
-char *gen_IC;
-char *gen_IP;
-char *gen_EI;
-char *gen_DC;
-char *gen_AL;
-char *gen_DL;
-char *gen_CE;
-char *gen_CD;
-char *gen_CL;
-char *gen_VS;
-char *gen_VE;
-char *gen_SO;
-char *gen_SE;
-char *gen_US;
-char *gen_UE;
-char *gen_UP;
-char *gen_PC;
-char *gen_BC;
-char *gen_ND;
-char *gen_HO;
-char *gen_NL;
+struct tt_str *gen_CM;
+struct tt_str *gen_IM;
+struct tt_str *gen_IC;
+struct tt_str *gen_IP;
+struct tt_str *gen_EI;
+struct tt_str *gen_DC;
+struct tt_str *gen_AL;
+struct tt_str *gen_DL;
+struct tt_str *gen_CE;
+struct tt_str *gen_CD;
+struct tt_str *gen_CL;
+struct tt_str *gen_VS;
+struct tt_str *gen_VE;
+struct tt_str *gen_TI;
+struct tt_str *gen_TE;
+struct tt_str *gen_SO;
+struct tt_str *gen_SE;
+struct tt_str *gen_US;
+struct tt_str *gen_UE;
+struct tt_str *gen_UP;
+struct tt_str *gen_PC;
+struct tt_str *gen_BC;
+struct tt_str *gen_ND;
+struct tt_str *gen_HO;
+struct tt_str *gen_NL;
+struct tt_str *gen_AS;
+struct tt_str *gen_AE;
+struct tt_str *gen_XS;
+struct tt_str *gen_XE;
 char gen_MI;
 char gen_MS;
 char gen_AM;
 char gen_OS;
 char gen_BS;
 char gen_MI;
 char gen_MS;
 char gen_AM;
 char gen_OS;
 char gen_BS;
+char gen_DB;
+char gen_NS;
+char gen_XN;
 int gen_CO;
 int gen_LI;
 int gen_UG;
 int gen_SG;
 
 int gen_CO;
 int gen_LI;
 int gen_UG;
 int gen_SG;
 
-#define pc(c) putchar('c')
-#define ps(s) fputs((s), stdout)
-
 gen_setinsert(new)
 char new;
 {
 gen_setinsert(new)
 char new;
 {
-       if (gen_insert == new)
-               return;
        if (new) {
                if (gen_IM)
        if (new) {
                if (gen_IM)
-                       ps(gen_IM);
+                       ttxputs(gen_IM);
        } else
                if (gen_EI)
        } else
                if (gen_EI)
-                       ps(gen_EI);
-       gen_insert = new;
+                       ttxputs(gen_EI);
+       tt.tt_insert = new;
 }
 
 gen_setmodes(new)
 }
 
 gen_setmodes(new)
@@ -73,261 +93,263 @@ register new;
 {
        register diff;
 
 {
        register diff;
 
-       new &= tt.tt_availmodes;
-       if ((diff = new ^ gen_modes) == 0)
-               return;
+       diff = new ^ tt.tt_modes;
        if (diff & WWM_REV) {
                if (new & WWM_REV) {
                        if (gen_SO)
        if (diff & WWM_REV) {
                if (new & WWM_REV) {
                        if (gen_SO)
-                               ps(gen_SO);
+                               ttxputs(gen_SO);
                } else
                        if (gen_SE)
                } else
                        if (gen_SE)
-                               ps(gen_SE);
+                               ttxputs(gen_SE);
        }
        if (diff & WWM_UL) {
                if (new & WWM_UL) {
                        if (gen_US)
        }
        if (diff & WWM_UL) {
                if (new & WWM_UL) {
                        if (gen_US)
-                               ps(gen_US);
+                               ttxputs(gen_US);
                } else
                        if (gen_UE)
                } else
                        if (gen_UE)
-                               ps(gen_UE);
+                               ttxputs(gen_UE);
+       }
+       if (diff & WWM_GRP) {
+               if (new & WWM_GRP) {
+                       if (gen_AS)
+                               ttxputs(gen_AS);
+               } else
+                       if (gen_AE)
+                               ttxputs(gen_AE);
+       }
+       if (diff & WWM_USR) {
+               if (new & WWM_USR) {
+                       if (gen_XS)
+                               ttxputs(gen_XS);
+               } else
+                       if (gen_XE)
+                               ttxputs(gen_XE);
        }
        }
-       gen_modes = new;
+       tt.tt_modes = new;
 }
 
 gen_insline()
 {
 }
 
 gen_insline()
 {
+       if (tt.tt_modes)                        /* for concept 100 */
+               gen_setmodes(0);
        if (gen_AL)
        if (gen_AL)
-               tt_tputs(gen_AL, gen_LI - gen_row);
+               tttputs(gen_AL, gen_LI - tt.tt_row);
 }
 
 gen_delline()
 {
 }
 
 gen_delline()
 {
+       if (tt.tt_modes)                        /* for concept 100 */
+               gen_setmodes(0);
        if (gen_DL)
        if (gen_DL)
-               tt_tputs(gen_DL, gen_LI - gen_row);
+               tttputs(gen_DL, gen_LI - tt.tt_row);
 }
 
 gen_putc(c)
 register char c;
 {
 }
 
 gen_putc(c)
 register char c;
 {
-       if (gen_insert) {
+       if (tt.tt_ninsert != tt.tt_insert)
+               gen_setinsert(tt.tt_ninsert);
+       if (tt.tt_nmodes != tt.tt_modes)
+               gen_setmodes(tt.tt_nmodes);
+       if (tt.tt_insert) {
                if (gen_IC)
                if (gen_IC)
-                       tt_tputs(gen_IC, gen_CO - gen_col);
-               putchar(c);
+                       tttputs(gen_IC, gen_CO - tt.tt_col);
+               ttputc(c);
                if (gen_IP)
                if (gen_IP)
-                       tt_tputs(gen_IP, gen_CO - gen_col);
+                       tttputs(gen_IP, gen_CO - tt.tt_col);
        } else
        } else
-               putchar(c);
-       if (++gen_col == gen_CO)
-               if (gen_AM)
-                       gen_col = 0, gen_row++;
-               else
-                       gen_col--;
-}
-
-gen_write(start, end)
-register char *start, *end;
-{
-       if (gen_insert) {
-               while (start <= end) {
-                       if (gen_IC)
-                               tt_tputs(gen_IC, gen_CO - gen_col);
-                       putchar(*start++);
-                       if (gen_IP)
-                               tt_tputs(gen_IP, gen_CO - gen_col);
-                       gen_col++;
-               }
-       } else {
-               gen_col += end - start + 1;
-               while (start <= end)
-                       putchar(*start++);
-       }
-       if (gen_col == gen_CO)
-               if (gen_AM)
-                       gen_col = 0, gen_row++;
+               ttputc(c);
+       if (++tt.tt_col == gen_CO)
+               if (gen_XN)
+                       tt.tt_col = tt.tt_row = -10;
+               else if (gen_AM)
+                       tt.tt_col = 0, tt.tt_row++;
                else
                else
-                       gen_col--;
+                       tt.tt_col--;
 }
 
 }
 
-gen_blank(n)
-register n;
+gen_write(p, n)
+       register char *p;
+       register n;
 {
 {
-       if (n <= 0)
-               return;
-       if (gen_insert) {
+       if (tt.tt_ninsert != tt.tt_insert)
+               gen_setinsert(tt.tt_ninsert);
+       if (tt.tt_nmodes != tt.tt_modes)
+               gen_setmodes(tt.tt_nmodes);
+       if (tt.tt_insert && (gen_IC || gen_IP)) {
                while (--n >= 0) {
                        if (gen_IC)
                while (--n >= 0) {
                        if (gen_IC)
-                               tt_tputs(gen_IC, gen_CO - gen_col);
-                       putchar(' ');
+                               tttputs(gen_IC, gen_CO - tt.tt_col);
+                       ttputc(*p++);
                        if (gen_IP)
                        if (gen_IP)
-                               tt_tputs(gen_IP, gen_CO - gen_col);
-                       gen_col++;
+                               tttputs(gen_IP, gen_CO - tt.tt_col);
+                       tt.tt_col++;
                }
        } else {
                }
        } else {
-               gen_col += n;
-               while (--n >= 0)
-                       putchar(' ');
+               tt.tt_col += n;
+               ttwrite(p, n);
        }
        }
-       if (gen_col == gen_CO)
-               if (gen_AM)
-                       gen_col = 0, gen_row++;
+       if (tt.tt_col == gen_CO)
+               if (gen_XN)
+                       tt.tt_col = tt.tt_row = -10;
+               else if (gen_AM)
+                       tt.tt_col = 0, tt.tt_row++;
                else
                else
-                       gen_col--;
+                       tt.tt_col--;
 }
 
 gen_move(row, col)
 }
 
 gen_move(row, col)
-register char row, col;
+register int row, col;
 {
 {
-       if (gen_row == row && gen_col == col)
+       if (tt.tt_row == row && tt.tt_col == col)
                return;
                return;
-       if (!gen_MI && gen_insert)
-               if (gen_EI)
-                       ps(gen_EI);
-       if (!gen_MS && gen_modes & WWM_REV)
-               if (gen_SE)
-                       ps(gen_SE);
-       if (gen_row == row) {
-               if (gen_col == col)
+       if (!gen_MI && tt.tt_insert)
+               gen_setinsert(0);
+       if (!gen_MS && tt.tt_modes)
+               gen_setmodes(0);
+       if (tt.tt_row == row) {
+               if (tt.tt_col == col)
                        return;
                        return;
-               if (gen_col == col - 1) {
+               if (tt.tt_col == col - 1) {
                        if (gen_ND) {
                        if (gen_ND) {
-                               ps(gen_ND);
+                               ttxputs(gen_ND);
                                goto out;
                        }
                                goto out;
                        }
-               } else if (gen_col == col + 1) {
+               } else if (tt.tt_col == col + 1) {
                        if (gen_BC) {
                        if (gen_BC) {
-                               ps(gen_BC);
+                               ttxputs(gen_BC);
                                goto out;
                        }
                }
        }
                                goto out;
                        }
                }
        }
-       if (gen_col == col) {
-               if (gen_row == row + 1) {
+       if (tt.tt_col == col) {
+               if (tt.tt_row == row + 1) {
                        if (gen_UP) {
                        if (gen_UP) {
-                               ps(gen_UP);
-                               goto out;
-                       }
-               } else if (gen_row == row + 1) {
-                       if (gen_NL) {
-                               ps(gen_NL);
+                               ttxputs(gen_UP);
                                goto out;
                        }
                                goto out;
                        }
+               } else if (tt.tt_row == row + 1) {
+                       ttxputs(gen_NL);
+                       goto out;
                }
        }
        if (gen_HO && col == 0 && row == 0) {
                }
        }
        if (gen_HO && col == 0 && row == 0) {
-               ps(gen_HO);
+               ttxputs(gen_HO);
                goto out;
        }
                goto out;
        }
-       ps(tgoto(gen_CM, col, row));
+       tttgoto(gen_CM, col, row);
 out:
 out:
-       gen_col = col;
-       gen_row = row;
-       if (!gen_MI && gen_insert)
-               if (gen_IM)
-                       ps(gen_IM);
-       if (!gen_MS && gen_modes & WWM_REV)
-               if (gen_SO)
-                       ps(gen_SO);
+       tt.tt_col = col;
+       tt.tt_row = row;
 }
 
 gen_init()
 {
        if (gen_VS)
 }
 
 gen_init()
 {
        if (gen_VS)
-               ps(gen_VS);
-       if (gen_CL)
-               ps(gen_CL);
-       gen_col = gen_row = 0;
-       gen_insert = 0;
-       gen_modes = 0;
+               ttxputs(gen_VS);
+       if (gen_TI)
+               ttxputs(gen_TI);
+       ttxputs(gen_CL);
+       tt.tt_col = tt.tt_row = 0;
+       tt.tt_ninsert = tt.tt_insert = 0;
+       tt.tt_nmodes = tt.tt_modes = 0;
 }
 
 gen_end()
 {
 }
 
 gen_end()
 {
-       gen_setmodes(0);
-       gen_setinsert(0);
+       if (gen_TE)
+               ttxputs(gen_TE);
        if (gen_VE)
        if (gen_VE)
-               ps(gen_VE);
+               ttxputs(gen_VE);
 }
 
 gen_clreol()
 {
 }
 
 gen_clreol()
 {
-       if (gen_CE)
-               tt_tputs(gen_CE, gen_CO - gen_col);
+       if (tt.tt_modes)                        /* for concept 100 */
+               gen_setmodes(0);
+       tttputs(gen_CE, gen_CO - tt.tt_col);
 }
 
 gen_clreos()
 {
 }
 
 gen_clreos()
 {
-       if (gen_CD)
-               tt_tputs(gen_CD, gen_LI - gen_row);
+       if (tt.tt_modes)                        /* for concept 100 */
+               gen_setmodes(0);
+       tttputs(gen_CD, gen_LI - tt.tt_row);
 }
 
 gen_clear()
 {
 }
 
 gen_clear()
 {
-       if (gen_CL)
-               ps(gen_CL);
+       if (tt.tt_modes)                        /* for concept 100 */
+               gen_setmodes(0);
+       ttxputs(gen_CL);
 }
 
 gen_delchar()
 {
 }
 
 gen_delchar()
 {
-       if (gen_DC)
-               tt_tputs(gen_DC, gen_CO - gen_col);
+       tttputs(gen_DC, gen_CO - tt.tt_col);
 }
 
 tt_generic()
 {
 }
 
 tt_generic()
 {
-       gen_CM = tt_xgetstr("cm");              /* may not work */
-       gen_IM = tt_xgetstr("im");
-       gen_IC = tt_tgetstr("ic");
-       gen_IP = tt_tgetstr("ip");
-       gen_EI = tt_xgetstr("ei");
-       gen_DC = tt_tgetstr("dc");
-       gen_AL = tt_tgetstr("al");
-       gen_DL = tt_tgetstr("dl");
-       gen_CE = tt_tgetstr("ce");
-       gen_CD = tt_tgetstr("cd");
-       gen_CL = tt_xgetstr("cl");
-       gen_VS = tt_xgetstr("vs");
-       gen_VE = tt_xgetstr("ve");
-       gen_SO = tt_xgetstr("so");
-       gen_SE = tt_xgetstr("se");
-       gen_US = tt_xgetstr("us");
-       gen_UE = tt_xgetstr("ue");
-       gen_UP = tt_xgetstr("up");
-       gen_PC = tt_tgetstr("pc");
-       gen_BC = tt_xgetstr("bc");
-       gen_ND = tt_xgetstr("nd");
-       gen_HO = tt_xgetstr("ho");
-       gen_NL = tt_xgetstr("nl");
+       gen_PC = tttgetstr("pc");
+       PC = gen_PC ? *gen_PC->ts_str : 0;
+       ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
+
+       gen_CM = ttxgetstr("cm");               /* may not work */
+       gen_IM = ttxgetstr("im");
+       gen_IC = tttgetstr("ic");
+       gen_IP = tttgetstr("ip");
+       gen_EI = ttxgetstr("ei");
+       gen_DC = tttgetstr("dc");
+       gen_AL = tttgetstr("al");
+       gen_DL = tttgetstr("dl");
+       gen_CE = tttgetstr("ce");
+       gen_CD = tttgetstr("cd");
+       gen_CL = ttxgetstr("cl");
+       gen_VS = ttxgetstr("vs");
+       gen_VE = ttxgetstr("ve");
+       gen_TI = ttxgetstr("ti");
+       gen_TE = ttxgetstr("te");
+       gen_SO = ttxgetstr("so");
+       gen_SE = ttxgetstr("se");
+       gen_US = ttxgetstr("us");
+       gen_UE = ttxgetstr("ue");
+       gen_UP = ttxgetstr("up");
+       gen_BC = ttxgetstr("bc");
+       gen_ND = ttxgetstr("nd");
+       gen_HO = ttxgetstr("ho");
+       gen_NL = ttxgetstr("nl");
+       gen_AS = ttxgetstr("as");
+       gen_AE = ttxgetstr("ae");
+       gen_XS = ttxgetstr("XS");
+       gen_XE = ttxgetstr("XE");
        gen_MI = tgetflag("mi");
        gen_MS = tgetflag("ms");
        gen_AM = tgetflag("am");
        gen_OS = tgetflag("os");
        gen_BS = tgetflag("bs");
        gen_MI = tgetflag("mi");
        gen_MS = tgetflag("ms");
        gen_AM = tgetflag("am");
        gen_OS = tgetflag("os");
        gen_BS = tgetflag("bs");
+       gen_DB = tgetflag("db");
+       gen_NS = tgetflag("ns");
+       gen_XN = tgetflag("xn");
        gen_CO = tgetnum("co");
        gen_LI = tgetnum("li");
        gen_UG = tgetnum("ug");
        gen_SG = tgetnum("sg");
 
        gen_CO = tgetnum("co");
        gen_LI = tgetnum("li");
        gen_UG = tgetnum("ug");
        gen_SG = tgetnum("sg");
 
-       if (gen_CL == 0 || gen_CM == 0 || gen_OS)
-               return -1;
-
-       if (gen_NL == 0)
-               gen_NL = "\n";
-       if (gen_BC == 0 && gen_BS)
-               gen_BC == "\b";
-
-       {
-               extern char PC, *BC, *UP;
-               extern short ospeed;
-
-               PC = gen_PC ? *gen_PC : 0;
-               BC = gen_BC;
-               UP = gen_UP;
-               ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
+       if (gen_NL == 0) {
+               static struct tt_str nl = { "\n", 1 };
+               gen_NL = &nl;
        }
        }
+       if (gen_BC == 0 && gen_BS) {
+               static struct tt_str bc = { "\b", 1 };
+               gen_BC = &bc;
+       }
+       BC = gen_BC ? gen_BC->ts_str : 0;
+       UP = gen_UP ? gen_UP->ts_str : 0;
+       if (gen_CL == 0 || gen_OS || gen_CM == 0)
+               return -1;
 
 
-       if (gen_IM)
-               tt.tt_setinsert = gen_setinsert;
        if (gen_DC)
                tt.tt_delchar = gen_delchar;
        if (gen_AL)
        if (gen_DC)
                tt.tt_delchar = gen_delchar;
        if (gen_AL)
@@ -338,26 +360,33 @@ tt_generic()
                tt.tt_clreol = gen_clreol;
        if (gen_CD)
                tt.tt_clreos = gen_clreos;
                tt.tt_clreol = gen_clreol;
        if (gen_CD)
                tt.tt_clreos = gen_clreos;
-       if (gen_CL)
-               tt.tt_clear = gen_clear;
        if (gen_SG > 0)
                gen_SO = 0;
        if (gen_SG > 0)
                gen_SO = 0;
-       if (gen_UG > 0)
+       if (gen_UG > 0 || gen_US && gen_SO && ttstrcmp(gen_US, gen_SO) == 0)
                gen_US = 0;
        if (gen_SO)
                tt.tt_availmodes |= WWM_REV;
        if (gen_US)
                tt.tt_availmodes |= WWM_UL;
                gen_US = 0;
        if (gen_SO)
                tt.tt_availmodes |= WWM_REV;
        if (gen_US)
                tt.tt_availmodes |= WWM_UL;
+       if (gen_AS)
+               tt.tt_availmodes |= WWM_GRP;
+       if (gen_XS)
+               tt.tt_availmodes |= WWM_USR;
+       tt.tt_hasinsert = gen_IM != 0;
        tt.tt_wrap = gen_AM;
        tt.tt_wrap = gen_AM;
+       tt.tt_retain = gen_DB;
+       tt.tt_noscroll = gen_NS;
        tt.tt_ncol = gen_CO;
        tt.tt_nrow = gen_LI;
        tt.tt_init = gen_init;
        tt.tt_end = gen_end;
        tt.tt_ncol = gen_CO;
        tt.tt_nrow = gen_LI;
        tt.tt_init = gen_init;
        tt.tt_end = gen_end;
-       tt.tt_setmodes = gen_setmodes;
-       tt.tt_blank = gen_blank;
        tt.tt_write = gen_write;
        tt.tt_putc = gen_putc;
        tt.tt_move = gen_move;
        tt.tt_write = gen_write;
        tt.tt_putc = gen_putc;
        tt.tt_move = gen_move;
-       tt.tt_frame = gen_frame;
+       tt.tt_clear = gen_clear;
+       tt.tt_setinsert = gen_setinsert;
+       tt.tt_setmodes = gen_setmodes;
+       tt.tt_frame = gen_AS && ttstrcmp(gen_AS, &ansi_AS) == 0 ?
+               ansi_frame : gen_frame;
        return 0;
 }
        return 0;
 }