home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume25 / tcsh-6.01 / part08 / ed.init.c next >
C/C++ Source or Header  |  1991-12-19  |  19KB  |  689 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/ed.init.c,v 3.23 1991/12/19 22:34:14 christos Exp $ */
  2. /*
  3.  * ed.init.c: Editor initializations
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTS_ION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: ed.init.c,v 3.23 1991/12/19 22:34:14 christos Exp $")
  40.  
  41. #include "ed.h"
  42. #include "ed.term.h"
  43. #include "tc.h"
  44. #include "ed.defns.h"
  45.  
  46. /* ed.init.c -- init routines for the line editor */
  47. /* #define DEBUG_TTY */
  48.  
  49. int     Tty_raw_mode = 0;    /* Last tty change was to raw mode */
  50. int     MacroLvl = -1;        /* pointer to current macro nesting level; */
  51.                 /* (-1 == none) */
  52. static int Tty_quote_mode = 0;    /* Last tty change was to quote mode */
  53. static unsigned char vdisable;    /* The value of _POSIX_VDISABLE from 
  54.                  * pathconf(2) */
  55.  
  56. int     Tty_eight_bit = -1;    /* does the tty handle eight bits */
  57.  
  58. extern bool GotTermCaps;
  59.  
  60. static ttydata_t extty, edtty, tstty;
  61. #define qutty tstty
  62.  
  63. extern int insource;
  64. #define SHTTY (insource ? OLDSTD : SHIN)
  65.  
  66. static unsigned char ttychars[NN_IO][C_NCC] = {
  67.     {
  68.     CINTR,         CQUIT,      CERASE,        CKILL,    
  69.     CEOF,          CEOL,          CEOL2,        CSWTCH, 
  70.     CDSWTCH,     CERASE2,     CSTART,        CSTOP,
  71.     CWERASE,      CSUSP,      CDSUSP,        CREPRINT,
  72.     CDISCARD,      CLNEXT,     CSTATUS,       CPAGE,
  73.     CPGOFF,         CKILL2,      CBRK,            CMIN,
  74.     CTIME
  75.     },
  76.     {
  77.     CINTR,          CQUIT,       CERASE,        CKILL, 
  78.     _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 
  79.     _POSIX_VDISABLE, CERASE2,      CSTART,        CSTOP,        
  80.     _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 
  81.     CDISCARD,      _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 
  82.     _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 1,
  83.     0
  84.     },
  85.     {    
  86.     0,         0,          0,           0,
  87.     0,         0,          0,           0,
  88.     0,         0,          0,           0,
  89.     0,         0,          0,           0,
  90.     0,         0,          0,           0,
  91.     0,         0,          0,           0,
  92.     0
  93.     }
  94. };
  95.  
  96. #ifdef SIG_WINDOW
  97. void
  98. check_window_size(force)
  99.     int     force;
  100. {
  101. #ifdef BSDSIGS
  102.     sigmask_t omask;
  103. #endif /* BSDSIGS */
  104.     int     lins, cols;
  105.  
  106.     /* don't want to confuse things here */
  107. #ifdef BSDSIGS
  108.     omask = sigblock(sigmask(SIG_WINDOW)) & ~sigmask(SIG_WINDOW);
  109. #else /* BSDSIGS */
  110.     (void) sighold(SIG_WINDOW);
  111. #endif /* BSDSIGS */
  112.     /*
  113.      * From: bret@shark.agps.lanl.gov (Bret Thaeler) Avoid sunview bug, where a
  114.      * partially hidden window gets a SIG_WINDOW every time the text is
  115.      * scrolled
  116.      */
  117.     if (GetSize(&lins, &cols) || force) {
  118.     if (GettingInput) {
  119.         ClearLines();
  120.         ClearDisp();
  121.         MoveToLine(0);
  122.         MoveToChar(0);
  123.         ChangeSize(lins, cols);
  124.         Refresh();
  125.     }
  126.     else
  127.         ChangeSize(lins, cols);
  128.     }
  129. #ifdef BSDSIGS
  130.     (void) sigsetmask(omask);    /* can change it again */
  131. #else                /* BSDSIGS */
  132.     (void) sigrelse(SIG_WINDOW);
  133. #endif /* BSDSIGS */
  134. }
  135.  
  136. sigret_t
  137. /*ARGSUSED*/
  138. window_change(snum)
  139. int snum;
  140. {
  141. #ifdef UNRELSIGS 
  142.     /* If we were called as a signal handler, restore it. */
  143.     if (snum > 0)
  144.       sigset(snum, window_change);
  145. #endif /* UNRELSIGS */
  146.     check_window_size(0);
  147. #ifndef SIGVOID
  148.     return (snum);
  149. #endif 
  150. }
  151.  
  152. #endif /* SIG_WINDOW */
  153.  
  154. void
  155. ed_set_tty_eight_bit()
  156. {
  157.     if (tty_getty(SHTTY, &extty) == -1) {
  158. #ifdef DEBUG_TTY
  159.     xprintf("ed_set_tty_eight_bit: tty_getty: %s\n", strerror(errno));
  160. #endif /* DEBUG_TTY */
  161.     return;
  162.     }
  163.     Tty_eight_bit = tty_geteightbit(&extty);
  164. }
  165.  
  166.             
  167. int
  168. ed_Setup(rst)
  169.     int rst;
  170. {
  171.     static int havesetup = 0;
  172.  
  173.     if (havesetup)     /* if we have never been called */
  174.     return(0);
  175.  
  176. #if defined(POSIX) && defined(_PC_VDISABLE) && !defined(BSD4_4)
  177.     { 
  178.     long pcret;
  179.  
  180.     if ((pcret = fpathconf(SHTTY, _PC_VDISABLE)) == -1L)
  181.         vdisable = _POSIX_VDISABLE;
  182.     else 
  183.         vdisable = pcret;
  184.     if (vdisable != _POSIX_VDISABLE && rst != 0)
  185.         for (rst = 0; rst < C_NCC - 2; rst++) {
  186.         if (ttychars[ED_IO][rst] == _POSIX_VDISABLE)
  187.             ttychars[ED_IO][rst] = vdisable;
  188.         if (ttychars[EX_IO][rst] == _POSIX_VDISABLE)
  189.             ttychars[EX_IO][rst] = vdisable;
  190.         }
  191.     }
  192. #else /* ! POSIX || !_PC_VDISABLE && !defined(BSD4_4) */
  193.     vdisable = _POSIX_VDISABLE;
  194. #endif /* POSIX && _PC_VDISABLE */
  195.     
  196.     inputmode = MODE_INSERT;    /* start out in insert mode */
  197.     ed_InitMaps();
  198.     Hist_num = 0;
  199.     Expand = 0;
  200.  
  201.     if (tty_getty(SHTTY, &extty) == -1) {
  202. #ifdef DEBUG_TTY
  203.     xprintf("ed_Setup: tty_getty: %s\n", strerror(errno));
  204. #endif /* DEBUG_TTY */
  205.     return(-1);
  206.     }
  207.  
  208.     tstty = edtty = extty;
  209.  
  210.     T_Speed = tty_getspeed(&extty);
  211.     T_Tabs = tty_gettabs(&extty);
  212.     Tty_eight_bit = tty_geteightbit(&extty);
  213.  
  214. #if defined(POSIX) || defined(TERMIO)
  215.     extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
  216.     extty.d_t.c_iflag |=  ttylist[EX_IO][M_INPUT].t_setmask;
  217.  
  218.     extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
  219.     extty.d_t.c_oflag |=  ttylist[EX_IO][M_OUTPUT].t_setmask;
  220.  
  221.     extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  222.     extty.d_t.c_cflag |=  ttylist[EX_IO][M_CONTROL].t_setmask;
  223.  
  224.     extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
  225.     extty.d_t.c_lflag |=  ttylist[EX_IO][M_LINED].t_setmask;
  226.  
  227. # ifdef IRIX3_3
  228.     extty.d_t.c_line = NTTYDISC;
  229. # endif /* IRIX3_3 */
  230.  
  231. #else    /* GSTTY */        /* V7, Berkeley style tty */
  232.  
  233.     if (T_Tabs) {    /* order of &= and |= is important to XTABS */
  234.     extty.d_t.sg_flags &= ~(ttylist[EX_IO][M_CONTROL].t_clrmask|XTABS);
  235.     extty.d_t.sg_flags |=   ttylist[EX_IO][M_CONTROL].t_setmask;
  236.     }
  237.     else {
  238.     extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  239.     extty.d_t.sg_flags |= (ttylist[EX_IO][M_CONTROL].t_setmask|XTABS);
  240.     }
  241.  
  242.     extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
  243.     extty.d_lb |=  ttylist[EX_IO][M_LOCAL].t_setmask;
  244.  
  245. #endif /* GSTTY */
  246.     /*
  247.      * Reset the tty chars to reasonable defaults
  248.      * If they are disabled, then enable them.
  249.      */
  250.     if (rst) {
  251.     if (tty_cooked_mode(&tstty)) {
  252.         tty_getchar(&tstty, ttychars[TS_IO]);
  253.         /*
  254.          * Don't affect CMIN and CTIME
  255.          */
  256.         for (rst = 0; rst < C_NCC - 2; rst++) {
  257.         if (ttychars[TS_IO][rst] != vdisable &&
  258.             ttychars[EX_IO][rst] != vdisable)
  259.             ttychars[EX_IO][rst] = ttychars[TS_IO][rst];
  260.         if (ttychars[TS_IO][rst] != vdisable &&
  261.             ttychars[ED_IO][rst] != vdisable)
  262.             ttychars[ED_IO][rst] = ttychars[TS_IO][rst];
  263.         }
  264.     }
  265.     tty_setchar(&extty, ttychars[EX_IO]);
  266.     if (tty_setty(SHTTY, &extty) == -1) {
  267. #ifdef DEBUG_TTY
  268.         xprintf("ed_Setup: tty_setty: %s\n", strerror(errno));
  269. #endif /* DEBUG_TTY */
  270.         return(-1);
  271.     }
  272.     }
  273.     else
  274.     tty_setchar(&extty, ttychars[EX_IO]);
  275.  
  276. # ifdef SIG_WINDOW
  277.     (void) sigset(SIG_WINDOW, window_change);    /* for window systems */
  278. # endif 
  279.     havesetup = 1;
  280.     return(0);
  281. }
  282.  
  283. void
  284. ed_Init()
  285. {
  286.     ResetInLine();        /* reset the input pointers */
  287.     GettingInput = 0;        /* just in case */
  288.     LastKill = KillBuf;        /* no kill buffer */
  289.  
  290. #ifdef DEBUG_EDIT
  291.     CheckMaps();        /* do a little error checking on key maps */
  292. #endif 
  293.  
  294.     if (ed_Setup(0) == -1)
  295.     return;
  296.  
  297.     /*
  298.      * if we have been called before but GotTermCaps isn't set, our TERM has
  299.      * changed, so get new termcaps and try again
  300.      */
  301.  
  302.     if (!GotTermCaps)
  303.     GetTermCaps();        /* does the obvious, but gets term type each
  304.                  * time */
  305.  
  306. #if defined(TERMIO) || defined(POSIX)
  307.     edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
  308.     edtty.d_t.c_iflag |=  ttylist[ED_IO][M_INPUT].t_setmask;
  309.  
  310.     edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
  311.     edtty.d_t.c_oflag |=  ttylist[ED_IO][M_OUTPUT].t_setmask;
  312.  
  313.     edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  314.     edtty.d_t.c_cflag |=  ttylist[ED_IO][M_CONTROL].t_setmask;
  315.  
  316.     edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
  317.     edtty.d_t.c_lflag |=  ttylist[ED_IO][M_LINED].t_setmask;
  318.  
  319.  
  320. # ifdef IRIX3_3
  321.     edtty.d_t.c_line = NTTYDISC;
  322. # endif /* IRIX3_3 */
  323.  
  324. #else /* GSTTY */
  325.  
  326.     if (T_Tabs) {    /* order of &= and |= is important to XTABS */
  327.     edtty.d_t.sg_flags &= ~(ttylist[ED_IO][M_CONTROL].t_clrmask | XTABS);
  328.     edtty.d_t.sg_flags |=   ttylist[ED_IO][M_CONTROL].t_setmask;
  329.     }
  330.     else {
  331.     edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  332.     edtty.d_t.sg_flags |= (ttylist[ED_IO][M_CONTROL].t_setmask | XTABS);
  333.     }
  334.  
  335.     edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
  336.     edtty.d_lb |=  ttylist[ED_IO][M_LOCAL].t_setmask;
  337. #endif /* POSIX || TERMIO */
  338.  
  339.     tty_setchar(&edtty, ttychars[ED_IO]);
  340. }
  341.  
  342. /* 
  343.  * Check and re-init the line. set the terminal into 1 char at a time mode.
  344.  */
  345. int
  346. Rawmode()
  347. {
  348.     if (Tty_raw_mode)
  349.     return (0);
  350.  
  351. #ifdef _IBMR2
  352.     tty_setdisc(SHTTY, ED_IO);
  353. #endif /* _IBMR2 */
  354.  
  355.     if (tty_getty(SHTTY, &tstty) == -1) {
  356. #ifdef DEBUG_TTY
  357.     xprintf("Rawmode: tty_getty: %s\n", strerror(errno));
  358. #endif /* DEBUG_TTY */
  359.     return(-1);
  360.     }
  361.  
  362.     /*
  363.      * We always keep up with the eight bit setting and the speed of the
  364.      * tty. But only we only believe changes that are made to cooked mode!
  365.      */
  366. #if defined(POSIX) || defined(TERMIO)
  367.     Tty_eight_bit = tty_geteightbit(&tstty);
  368.     T_Speed = tty_getspeed(&tstty);
  369.  
  370. # ifdef POSIX
  371.     /*
  372.      * Fix from: Steven (Steve) B. Green <xrsbg@charney.gsfc.nasa.gov>
  373.      * Speed was not being set up correctly under POSIX.
  374.      */
  375.     if (tty_getspeed(&extty) != T_Speed || tty_getspeed(&edtty) != T_Speed) {
  376.     (void) cfsetispeed(&extty.d_t, T_Speed);
  377.     (void) cfsetospeed(&extty.d_t, T_Speed);
  378.     (void) cfsetispeed(&edtty.d_t, T_Speed);
  379.     (void) cfsetospeed(&edtty.d_t, T_Speed);
  380.     }
  381. # endif /* POSIX */
  382. #else /* GSTTY */
  383.  
  384.     T_Speed = tty_getspeed(&tstty);
  385.     Tty_eight_bit = tty_geteightbit(&tstty);
  386.  
  387.     if (extty.d_t.sg_ispeed != tstty.d_t.sg_ispeed) {
  388.     extty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
  389.     edtty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
  390.     }
  391.  
  392.     if (extty.d_t.sg_ospeed != tstty.d_t.sg_ospeed) {
  393.     extty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
  394.     edtty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
  395.     }
  396. #endif /* POSIX || TERMIO */
  397.  
  398.     if (tty_cooked_mode(&tstty)) {
  399. #if defined(POSIX) || defined(TERMIO)
  400.     if (tstty.d_t.c_cflag != extty.d_t.c_cflag) { 
  401.         extty.d_t.c_cflag  = tstty.d_t.c_cflag;
  402.         extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  403.         extty.d_t.c_cflag |=  ttylist[EX_IO][M_CONTROL].t_setmask;
  404.  
  405.         edtty.d_t.c_cflag  = tstty.d_t.c_cflag;
  406.         edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  407.         edtty.d_t.c_cflag |=  ttylist[ED_IO][M_CONTROL].t_setmask;
  408.     }
  409.  
  410.     if ((tstty.d_t.c_lflag != extty.d_t.c_lflag) &&
  411.         (tstty.d_t.c_lflag != edtty.d_t.c_lflag)) {
  412.         extty.d_t.c_lflag = tstty.d_t.c_lflag;
  413.         extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
  414.         extty.d_t.c_lflag |=  ttylist[EX_IO][M_LINED].t_setmask;
  415.  
  416.         edtty.d_t.c_lflag = tstty.d_t.c_lflag;
  417.         edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
  418.         edtty.d_t.c_lflag |=  ttylist[ED_IO][M_LINED].t_setmask;
  419.     }
  420.  
  421.     if ((tstty.d_t.c_iflag != extty.d_t.c_iflag) &&
  422.         (tstty.d_t.c_iflag != edtty.d_t.c_iflag)) {
  423.         extty.d_t.c_iflag = tstty.d_t.c_iflag;
  424.         extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
  425.         extty.d_t.c_iflag |=  ttylist[EX_IO][M_INPUT].t_setmask;
  426.  
  427.         edtty.d_t.c_iflag = tstty.d_t.c_iflag;
  428.         edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
  429.         edtty.d_t.c_iflag |=  ttylist[ED_IO][M_INPUT].t_setmask;
  430.     }
  431.  
  432.     if ((tstty.d_t.c_oflag != extty.d_t.c_oflag) &&
  433.         (tstty.d_t.c_oflag != edtty.d_t.c_oflag)) {
  434.         extty.d_t.c_oflag = tstty.d_t.c_oflag;
  435.         extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
  436.         extty.d_t.c_oflag |=  ttylist[EX_IO][M_OUTPUT].t_setmask;
  437.  
  438.         edtty.d_t.c_oflag = tstty.d_t.c_oflag;
  439.         edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
  440.         edtty.d_t.c_oflag |=  ttylist[ED_IO][M_OUTPUT].t_setmask;
  441.     }
  442.  
  443.     if (tty_gettabs(&extty) == 0) 
  444.         T_Tabs = 0;
  445.     else 
  446.         T_Tabs = CanWeTab();
  447.  
  448. #else /* GSTTY */
  449.  
  450.     if (((tstty.d_t.sg_flags != extty.d_t.sg_flags) || 
  451.          (tstty.d_lb != extty.d_lb)) &&
  452.         ((tstty.d_t.sg_flags != edtty.d_t.sg_flags) || 
  453.          (tstty.d_lb != edtty.d_lb))) {
  454.  
  455.         extty.d_t.sg_flags = tstty.d_t.sg_flags;
  456.  
  457.         /*
  458.          * re-test for some things here (like maybe the user typed 
  459.          * "stty -tabs"
  460.          */
  461.         if (tty_gettabs(&extty) == 0)
  462.         T_Tabs = 0;
  463.         else 
  464.         T_Tabs = CanWeTab();
  465.  
  466.         extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  467.         extty.d_t.sg_flags |=  ttylist[EX_IO][M_CONTROL].t_setmask;
  468.  
  469.         if (T_Tabs)        /* order of &= and |= is important to XTABS */
  470.         extty.d_t.sg_flags &= ~XTABS;
  471.         else 
  472.         extty.d_t.sg_flags |= XTABS;
  473.  
  474.         extty.d_lb = tstty.d_lb;
  475.         extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
  476.         extty.d_lb |= ttylist[EX_IO][M_LOCAL].t_setmask;
  477.  
  478.         edtty.d_t.sg_flags = extty.d_t.sg_flags;
  479.         if (T_Tabs) {    /* order of &= and |= is important to XTABS */
  480.         edtty.d_t.sg_flags &= 
  481.             ~(ttylist[ED_IO][M_CONTROL].t_clrmask|XTABS);
  482.         edtty.d_t.sg_flags |=   ttylist[ED_IO][M_CONTROL].t_setmask;
  483.         }
  484.         else {
  485.         edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  486.         edtty.d_t.sg_flags |= 
  487.             (ttylist[ED_IO][M_CONTROL].t_setmask|XTABS);
  488.         }
  489.  
  490.         edtty.d_lb = tstty.d_lb;
  491.         edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
  492.         edtty.d_lb |= ttylist[ED_IO][M_LOCAL].t_setmask;
  493.     }
  494. # endif /* TERMIO || POSIX */
  495.     {
  496.         extern int didsetty;
  497.         int i;
  498.  
  499.         tty_getchar(&tstty, ttychars[TS_IO]);
  500.         /*
  501.          * Check if the user made any changes.
  502.          * If he did, then propagate the changes to the
  503.          * edit and execute data structures.
  504.          */
  505.         for (i = 0; i < C_NCC; i++)
  506.         if (ttychars[TS_IO][i] != ttychars[EX_IO][i])
  507.             break;
  508.         
  509.         if (i != C_NCC || didsetty) {
  510.         didsetty = 0;
  511.         /*
  512.          * Propagate changes only to the unprotected chars
  513.          * that have been modified just now.
  514.          */
  515.         for (i = 0; i < C_NCC; i++) {
  516.             if (!((ttylist[ED_IO][M_CHAR].t_setmask & C_SH(i))) &&
  517.             (ttychars[TS_IO][i] != ttychars[EX_IO][i]))
  518.             ttychars[ED_IO][i] = ttychars[TS_IO][i];
  519.             if (ttylist[ED_IO][M_CHAR].t_clrmask & C_SH(i))
  520.             ttychars[ED_IO][i] = vdisable;
  521.         }
  522.         tty_setchar(&edtty, ttychars[ED_IO]);
  523.  
  524.         for (i = 0; i < C_NCC; i++) {
  525.             if (!((ttylist[EX_IO][M_CHAR].t_setmask & C_SH(i))) &&
  526.             (ttychars[TS_IO][i] != ttychars[EX_IO][i]))
  527.             ttychars[EX_IO][i] = ttychars[TS_IO][i];
  528.             if (ttylist[EX_IO][M_CHAR].t_clrmask & C_SH(i))
  529.             ttychars[EX_IO][i] = vdisable;
  530.         }
  531.         tty_setchar(&extty, ttychars[EX_IO]);
  532.         }
  533.  
  534.     }
  535.     }
  536.     if (tty_setty(SHTTY, &edtty) == -1) {
  537. #ifdef DEBUG_TTY
  538.     xprintf("Rawmode: tty_setty: %s\n", strerror(errno));
  539. #endif /* DEBUG_TTY */
  540.     return -1;
  541.     }
  542.     Tty_raw_mode = 1;
  543.     flush();            /* flush any buffered output */
  544.     return (0);
  545. }
  546.  
  547. int
  548. Cookedmode()
  549. {                /* set tty in normal setup */
  550.     sigret_t(*orig_intr) ();
  551.  
  552. #ifdef _IBMR2
  553.     tty_setdisc(SHTTY, EX_IO);
  554. #endif /* _IBMR2 */
  555.  
  556.     if (!Tty_raw_mode)
  557.     return (0);
  558.  
  559.     /* hold this for reseting tty */
  560. #ifdef BSDSIGS
  561.     orig_intr = (sigret_t (*)()) signal(SIGINT, SIG_IGN);
  562. #else
  563.     orig_intr = (sigret_t (*)()) sigset(SIGINT, SIG_IGN);
  564. #endif /* BSDSIGS */
  565.     if (tty_setty(SHTTY, &extty) == -1) {
  566. #ifdef DEBUG_TTY
  567.     xprintf("Cookedmode: tty_setty: %s\n", strerror(errno));
  568. #endif /* DEBUG_TTY */
  569.     return -1;
  570.     }
  571.     Tty_raw_mode = 0;
  572. #ifdef BSDSIGS
  573.     (void) signal(SIGINT, orig_intr);    /* take these again */
  574. #else
  575.     (void) sigset(SIGINT, orig_intr);    /* take these again */
  576. #endif /* BSDSIGS */
  577.     return (0);
  578. }
  579.  
  580. void
  581. ResetInLine()
  582. {
  583.     Cursor = InputBuf;        /* reset cursor */
  584.     LastChar = InputBuf;
  585.     InputLim = &InputBuf[INBUFSIZE - 2];
  586.     Mark = InputBuf;
  587.     MetaNext = 0;
  588.     CurrentKeyMap = CcKeyMap;
  589.     AltKeyMap = 0;
  590.     Hist_num = 0;
  591.     DoingArg = 0;
  592.     Argument = 1;
  593. #ifdef notdef
  594.     LastKill = KillBuf;        /* no kill buffer */
  595. #endif 
  596.     LastCmd = F_UNASSIGNED;    /* previous command executed */
  597.     MacroLvl = -1;        /* no currently active macros */
  598. }
  599.  
  600. static Char *Input_Line = NULL;
  601. int
  602. Load_input_line()
  603. {
  604.     long    chrs = 0;
  605.  
  606.     if (Input_Line)
  607.     xfree((ptr_t) Input_Line);
  608.     Input_Line = NULL;
  609.  
  610.     if (Tty_raw_mode)
  611.     return 0;
  612.  
  613. #ifdef FIONREAD
  614.     (void) ioctl(SHIN, FIONREAD, &chrs);
  615.     if (chrs > 0) {
  616.     char    buf[BUFSIZE];
  617.  
  618.     chrs = read(SHIN, buf, (size_t) min(chrs, BUFSIZE - 1));
  619.     if (chrs > 0) {
  620.         buf[chrs] = '\0';
  621.         Input_Line = Strsave(str2short(buf));
  622.         PushMacro(Input_Line);
  623.     }
  624.     }
  625. #endif  /* FIONREAD */
  626.     return chrs > 0;
  627. }
  628.  
  629. /*
  630.  * Bugfix (in Swedish) by:
  631.  * Johan Widen
  632.  * SICS, PO Box 1263, S-163 13 SPANGA, SWEDEN
  633.  * {mcvax,munnari,cernvax,diku,inria,prlb2,penet,ukc,unido}!enea!sics.se!jw
  634.  * Internet: jw@sics.se
  635.  *
  636.  * (via Hans J Albertsson (thanks))
  637.  */
  638. void
  639. QuoteModeOn()
  640. {
  641.     if (MacroLvl >= 0)
  642.     return;
  643.  
  644.     qutty = edtty;
  645.  
  646. #if defined(TERMIO) || defined(POSIX)
  647.     qutty.d_t.c_iflag &= ~ttylist[QU_IO][M_INPUT].t_clrmask;
  648.     qutty.d_t.c_iflag |=  ttylist[QU_IO][M_INPUT].t_setmask;
  649.  
  650.     qutty.d_t.c_oflag &= ~ttylist[QU_IO][M_OUTPUT].t_clrmask;
  651.     qutty.d_t.c_oflag |=  ttylist[QU_IO][M_OUTPUT].t_setmask;
  652.  
  653.     qutty.d_t.c_cflag &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
  654.     qutty.d_t.c_cflag |=  ttylist[QU_IO][M_CONTROL].t_setmask;
  655.  
  656.     qutty.d_t.c_lflag &= ~ttylist[QU_IO][M_LINED].t_clrmask;
  657.     qutty.d_t.c_lflag |=  ttylist[QU_IO][M_LINED].t_setmask;
  658. #else /* GSTTY */
  659.     qutty.d_t.sg_flags &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
  660.     qutty.d_t.sg_flags |= ttylist[QU_IO][M_CONTROL].t_setmask;
  661.     qutty.d_lb &= ~ttylist[QU_IO][M_LOCAL].t_clrmask;
  662.     qutty.d_lb |= ttylist[QU_IO][M_LOCAL].t_setmask;
  663.  
  664. #endif /* TERMIO || POSIX */
  665.     if (tty_setty(SHTTY, &qutty) == -1) {
  666. #ifdef DEBUG_TTY
  667.     xprintf("QuoteModeOn: tty_setty: %s\n", strerror(errno));
  668. #endif /* DEBUG_TTY */
  669.     return;
  670.     }
  671.     Tty_quote_mode = 1;
  672.     return;
  673. }
  674.  
  675. void
  676. QuoteModeOff()
  677. {
  678.     if (!Tty_quote_mode)
  679.     return;
  680.     Tty_quote_mode = 0;
  681.     if (tty_setty(SHTTY, &edtty) == -1) {
  682. #ifdef DEBUG_TTY
  683.     xprintf("QuoteModeOff: tty_setty: %s\n", strerror(errno));
  684. #endif /* DEBUG_TTY */
  685.     return;
  686.     }
  687.     return;
  688. }
  689.