home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / screen3 / part07 / ansi.c
Encoding:
C/C++ Source or Header  |  1991-12-18  |  66.5 KB  |  3,497 lines

  1. /* Copyright (c) 1991
  2.  *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
  3.  *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
  4.  * Copyright (c) 1987 Oliver Laumann
  5.  * All rights reserved.  Not derived from licensed software.
  6.  *
  7.  * Permission is granted to freely use, copy, modify, and redistribute
  8.  * this software, provided that no attempt is made to gain profit from it,
  9.  * the authors are not construed to be liable for any results of using the
  10.  * software, alterations are clearly marked as such, and this notice is
  11.  * not modified.
  12.  *
  13.  * Noteworthy contributors to screen's design and implementation:
  14.  *    Wayne Davison (davison@borland.com)
  15.  *    Patrick Wolfe (pat@kai.com, kailand!pat)
  16.  *    Bart Schaefer (schaefer@cse.ogi.edu)
  17.  *    Nathan Glasser (nathan@brokaw.lcs.mit.edu)
  18.  *    Larry W. Virden (lwv27%cas.BITNET@CUNYVM.CUNY.Edu)
  19.  *    Howard Chu (hyc@hanauma.jpl.nasa.gov)
  20.  *    Tim MacKenzie (tym@dibbler.cs.monash.edu.au)
  21.  *    Markku Jarvinen (mta@{cc,cs,ee}.tut.fi)
  22.  *    Marc Boucher (marc@CAM.ORG)
  23.  *
  24.  ****************************************************************
  25.  */
  26.  
  27. #ifndef lint
  28.   static char rcs_id[] = "$Id: ansi.c,v 1.88 91/10/11 11:27:40 jnweiger Exp $ FAU";
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #if defined(BSD) || defined(sequent) || defined(pyr)
  33. # include <strings.h>
  34. #else
  35. # include <string.h>
  36. #endif
  37. #include <sys/types.h>
  38. #include "config.h"
  39. #include "screen.h"
  40. #include "ansi.h"
  41. #include "extern.h"
  42. #include <fcntl.h>
  43. #ifndef sun /* we want to know about TIOCGWINSZ. jw. */
  44. # include <sys/ioctl.h>
  45. #endif
  46.  
  47. extern char *getenv(), *tgetstr(), *tgoto();
  48. #ifndef __STDC__
  49. extern char *malloc();
  50. #endif
  51.  
  52. extern struct win *fore;
  53. extern int ForeNum;
  54. extern force_vt, assume_LP;
  55. extern int BellDisplayed;
  56. extern int MsgMinWait;
  57.  
  58. #ifdef TIOCGWINSZ
  59. extern struct winsize glwz;
  60. #endif
  61.  
  62. static int rows, cols;        /* window size of the curr window. */
  63. int TermcapROWS, TermcapCOLS;    /* defaults that we learned from termcap */
  64. int maxwidth, Z0width, Z1width;
  65. int default_width, default_height;    /* width/height a new window will get */
  66.  
  67. int screenwidth, screenheight;    /* width/height of the fore window */
  68. int screentop, screenbot;    /* scrollregion start/end */
  69. int screenx, screeny;        /* cursor position */
  70. char GlobalAttr, GlobalCharset;
  71. int insert;            /* insert mode */
  72. int keypad;
  73. int flow;
  74.  
  75. int status;
  76. static int status_lastx, status_lasty;
  77.  
  78. int flowctl, wrap = 1, default_monitor = 0; 
  79. int visual_bell = 0, termcapHS, use_hardstatus = 1;
  80. char *Termcap, *extra_incap, *extra_outcap;
  81. static int Termcaplen;
  82. char *blank, *null, *LastMsg;
  83. char Term[MAXSTR+5];    /* +5: "TERM=" */
  84. char screenterm[20] = "screen";
  85. char *Z0, *Z1;
  86. int ISO2022, HS;
  87. time_t TimeDisplayed, time();
  88.  
  89. /*
  90.  * the termcap routines need this to work
  91.  */
  92. short ospeed;
  93. char *BC;
  94. char *UP;
  95.  
  96. static void AddCap __P((char *));
  97. static void MakeString __P((char *, char *, int, char *));
  98. static int Special __P((int));
  99. static void DoESC __P((int, int ));
  100. static void DoCSI __P((int, int ));
  101. static void CPutStr __P((char *, int));
  102. static void SetChar __P(());
  103. static void StartString __P((enum string_t));
  104. static void AddChar __P((int ));
  105. static void PrintChar __P((int ));
  106. static void PrintFlush __P((void));
  107. static void KeypadMode __P((int));
  108. static void FlowMode __P((int));
  109. static void DesignateCharset __P((int, int ));
  110. static void MapCharset __P((int));
  111. static void SaveCursor __P((void));
  112. static void RestoreCursor __P((void));
  113. static void CountChars __P((int));
  114. static int CalcCost __P((char *));
  115. static int Rewrite __P((int, int, int, int));
  116. static void BackSpace __P((void));
  117. static void Return __P((void));
  118. static void LineFeed __P((int));
  119. static void ReverseLineFeed __P((void));
  120. static void InsertAChar __P((int));
  121. static void InsertChar __P((int));
  122. static void DeleteChar __P((int));
  123. static void DeleteLine __P((int));
  124. static void InsertLine __P((int));
  125. static void ScrollUpMap __P((int));
  126. static void ScrollDownMap __P((int));
  127. static void Scroll __P((char *, int, int, char *));
  128. static void ForwardTab __P((void));
  129. static void BackwardTab __P((void));
  130. static void ClearScreen __P((void));
  131. static void ClearFromBOS __P((void));
  132. static void ClearToEOS __P((void));
  133. static void ClearLine __P((void));
  134. static void ClearToEOL __P((void));
  135. static void ClearFromBOL __P((void));
  136. static void ClearInLine __P((int, int, int, int ));
  137. static void CursorRight __P(());
  138. static void CursorUp __P(());
  139. static void CursorDown __P(());
  140. static void CursorLeft __P(());
  141. static void ASetMode __P((int));
  142. static void SelectRendition __P((void));
  143. static void FillWithEs __P((void));
  144. static void RedisplayLine __P((char *, char *, char *, int, int, int ));
  145. static void FindAKA __P((void));
  146. static void SetCurr __P((struct win *));
  147. static void inpRedisplayLine __P((int, int, int, int));
  148. static void process_inp_input __P((char **, int *));
  149. static void AbortInp __P((void));
  150. static void AKAfin __P((char *, int));
  151. static void Colonfin __P((char *, int));
  152. static void RAW_PUTCHAR __P((int));
  153. static char *e_tgetstr __P((char *, char **));
  154. static int e_tgetflag __P((char *));
  155. static int e_tgetnum __P((char *));
  156.  
  157.  
  158. static char *tbuf, *tentry, *termname;
  159. static char *tp;
  160. static char *TI, *TE, *BL, *VB, *CR, *NL, *CL, *IS;
  161. char *WS;    /* used in ResizeScreen() */
  162. char *CE;    /* used in help.c */
  163. static char *CM, *US, *UE, *SO, *SE, *CD, *DO, *SR, *SF, *AL;
  164. static char *CS, *DL, *DC, *IC, *IM, *EI, *ND, *KS, *KE;
  165. static char *MB, *MD, *MH, *MR, *ME, *PO, *PF, *HO;
  166. static char *TS, *FS, *DS, *VI, *VE, *VS;
  167. static char *CDC, *CDL, *CAL, *CUP, *CDO, *CLE, *CRI, *CIC;
  168. static char *attrtab[NATTR];
  169. static AM, MS, COP;
  170. int LP;
  171. /*
  172.  * Do not confuse S0 (es-zero), E0 (e-zero) with SO (es-oh), PO (pe-oh),
  173.  * Z0 (z-zero), DO (de-oh)... :-)
  174.  */
  175. static char *C0, *S0, *E0;
  176. static char c0_tab[256];
  177. /*
  178.  */
  179. static screencap = 0;
  180. char *OldImage, *OldAttr, *OldFont;
  181. static struct win *curr;
  182. static display = 1;
  183. static StrCost;
  184. static UPcost, DOcost, LEcost, NDcost, CRcost, IMcost, EIcost, NLcost;
  185. static tcLineLen;
  186. static StatLen;
  187. static lp_missing = 0;
  188.  
  189. int in_ovl;
  190. int ovl_blockfore;
  191. void (*ovl_process)();
  192. void (*ovl_RedisplayLine)();
  193. int (*ovl_Rewrite)();
  194.  
  195. static char *KeyCaps[] =
  196. {
  197.   "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9",
  198.   "kb", "kd", "kh", "kl", "ko", "kr", "ku",
  199.   "K1", "K2", "K3", "K4", "K5",
  200.   "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", "l8", "l9"
  201. };
  202. #define NKEYCAPS (sizeof(KeyCaps)/sizeof(*KeyCaps))
  203. static char *KeyCapsArr[NKEYCAPS];
  204.  
  205. static char TermcapConst[] = "\\\n\
  206. \t:DO=\\E[%dB:LE=\\E[%dD:RI=\\E[%dC:UP=\\E[%dA:bs:bt=\\E[Z:\\\n\
  207. \t:cd=\\E[J:ce=\\E[K:cl=\\E[H\\E[J:cm=\\E[%i%d;%dH:ct=\\E[3g:\\\n\
  208. \t:do=^J:nd=\\E[C:pt:rc=\\E8:rs=\\Ec:sc=\\E7:st=\\EH:up=\\EM:\\\n\
  209. \t:le=^H:bl=^G:cr=^M:it#8:ho=\\E[H:nw=\\EE:ta=^I:is=\\E)0:xv:";
  210.  
  211. void
  212. InitTermcap()
  213. {
  214.   register char *s;
  215.   int i;
  216.  
  217.   screencap = 0;
  218.   if ((s = getenv("SCREENCAP")) != 0)
  219.     {
  220.       if ((Termcap = malloc((unsigned) strlen(s) + 10)) != 0)
  221.     {
  222.       sprintf(Termcap, "TERMCAP=%s", s);
  223.       screencap = 1;
  224.     }
  225.     }
  226.   else
  227.     Termcap = malloc((unsigned) 1024);
  228.   Termcaplen = 0;
  229.   tbuf = malloc((unsigned) 1024);
  230.   tentry = tp = malloc((unsigned) 1024);
  231.   if (!(Termcap && tbuf && tentry))
  232.     Msg_nomem;
  233.   bzero(tbuf, 1024);
  234.   if ((termname = getenv("TERM")) == 0)
  235.     Msg(0, "No TERM in environment.");
  236.   debug1("InitTermcap: looking for tgetent('%s');\n", termname);
  237.   if (tgetent(tbuf, termname) != 1)
  238.     Msg(0, "Cannot find termcap entry for %s.", termname);
  239.   debug1("got it:\n%s\n",tbuf);
  240. #ifdef DEBUG
  241.   if (extra_incap)
  242.     debug1("Extra incap: %s\n", extra_incap);
  243.   if (extra_outcap)
  244.     debug1("Extra outcap: %s\n", extra_outcap);
  245. #endif
  246.  
  247.   TermcapCOLS = TermcapROWS = 0;
  248.   if (s = getenv("COLUMNS"))
  249.     TermcapCOLS = atoi(s);
  250.   if (TermcapCOLS <= 0)
  251.     TermcapCOLS = e_tgetnum("co");
  252.   if (TermcapCOLS <= 0)
  253.     TermcapCOLS = 80;
  254.   if (s = getenv("LINES"))
  255.     TermcapROWS = atoi(s);
  256.   if (TermcapROWS <= 0)
  257.     TermcapROWS = e_tgetnum("li");
  258.   if (TermcapROWS <= 0)
  259.     TermcapROWS = 24;
  260.  
  261.   if (e_tgetflag("hc"))
  262.     Msg(0, "You can't run screen on a hardcopy terminal.");
  263.   if (e_tgetflag("os"))
  264.     Msg(0, "You can't run screen on a terminal that overstrikes.");
  265.   if (e_tgetflag("ns"))
  266.     Msg(0, "Terminal must support scrolling.");
  267.   if (!(CL = e_tgetstr("cl", &tp)))
  268.     Msg(0, "Clear screen capability required.");
  269.   if (!(CM = e_tgetstr("cm", &tp)))
  270.     Msg(0, "Addressable cursor capability required.");
  271.   switch (flowctl)
  272.     {
  273.     case 0:
  274.       flow = !e_tgetflag("NF");
  275.       flowctl = flow + 1;
  276.       break;
  277.     case 1:
  278.       flow = 0;
  279.       break;
  280.     case 2:
  281.     case 3:
  282.       flow = 1;
  283.       break;
  284.     }
  285.   AM = e_tgetflag("am");
  286.   LP = assume_LP || (!extra_incap && !strncmp(termname, "vt", 2))
  287.            || !AM || e_tgetflag("LP") || e_tgetflag("xv");
  288.   COP = e_tgetflag("OP");
  289.   HO = e_tgetstr("ho", &tp);
  290.   TI = e_tgetstr("ti", &tp);
  291.   TE = e_tgetstr("te", &tp);
  292.   if (!(BL = e_tgetstr("bl", &tp)))
  293.     BL = "\007";
  294.   VB = e_tgetstr("vb", &tp);
  295.   if (!(BC = e_tgetstr("bc", &tp)))
  296.     {
  297.       if (e_tgetflag("bs"))
  298.     BC = "\b";
  299.       else
  300.     BC = e_tgetstr("le", &tp);
  301.     }
  302.   if (!(CR = e_tgetstr("cr", &tp)))
  303.     CR = "\r";
  304.   if (!(NL = e_tgetstr("nl", &tp)))
  305.     NL = "\n";
  306.   IS = e_tgetstr("is", &tp);
  307.   MS = 1;
  308.   if (e_tgetnum("sg") <= 0 && e_tgetnum("ug") <= 0)
  309.     {
  310.       MS = e_tgetflag("ms");
  311.       attrtab[ATTR_DI] = MH = e_tgetstr("mh", &tp);    /* Dim */
  312.       attrtab[ATTR_US] = US = e_tgetstr("us", &tp);    /* Underline */
  313.       attrtab[ATTR_BD] = MD = e_tgetstr("md", &tp);    /* Bold */
  314.       attrtab[ATTR_RV] = MR = e_tgetstr("mr", &tp);    /* Reverse */
  315.       attrtab[ATTR_SO] = SO = e_tgetstr("so", &tp);    /* Standout */
  316.       attrtab[ATTR_BL] = MB = e_tgetstr("mb", &tp);    /* Blinking */
  317.       ME = e_tgetstr("me", &tp);
  318.       SE = e_tgetstr("se", &tp);
  319.       UE = e_tgetstr("ue", &tp);
  320.       /*
  321.        * Does ME also reverse the effect of SO and/or US?  This is not
  322.        * clearly specified by the termcap manual. Anyway, we should at
  323.        * least look whether ME and SE/UE are equal:
  324.        */
  325.       if (UE && ((SE && strcmp(SE, UE) == 0) || (ME && strcmp(ME, UE) == 0)))
  326.     UE = 0;
  327.       if (SE && (ME && strcmp(ME, SE) == 0))
  328.     SE = 0;
  329.  
  330.       /* Set up missing entries */
  331.       s = 0;
  332.       for (i = NATTR-1; i >= 0; i--)
  333.     if (attrtab[i])
  334.       s = attrtab[i];
  335.       for (i = 0; i < NATTR; i++)
  336.     {
  337.       if (attrtab[i] == 0)
  338.         attrtab[i] = s;
  339.       else
  340.         s = attrtab[i];
  341.     }
  342.     }
  343.   else
  344.     {
  345.       US = UE = SO = SE = MB = MD = MH = MR = ME = 0;
  346.       for (i = 0; i < NATTR; i++)
  347.     attrtab[i] = 0;
  348.     }
  349.   CE = e_tgetstr("ce", &tp);
  350.   CD = e_tgetstr("cd", &tp);
  351.   if (!(DO = e_tgetstr("do", &tp)))
  352.     DO = NL;
  353.   UP = e_tgetstr("up", &tp);
  354.   ND = e_tgetstr("nd", &tp);
  355.   SR = e_tgetstr("sr", &tp);
  356.   if (!(SF = e_tgetstr("sf", &tp)))
  357.     SF = NL;
  358.   AL = e_tgetstr("al", &tp);
  359.   DL = e_tgetstr("dl", &tp);
  360.   CS = e_tgetstr("cs", &tp);
  361.   DC = e_tgetstr("dc", &tp);
  362.   IC = e_tgetstr("ic", &tp);
  363.   CIC = e_tgetstr("IC", &tp);
  364.   CDC = e_tgetstr("DC", &tp);
  365.   CDL = e_tgetstr("DL", &tp);
  366.   CAL = e_tgetstr("AL", &tp);
  367.   CUP = e_tgetstr("UP", &tp);
  368.   CDO = e_tgetstr("DO", &tp);
  369.   CLE = e_tgetstr("LE", &tp);
  370.   CRI = e_tgetstr("RI", &tp);
  371.   IM = e_tgetstr("im", &tp);
  372.   EI = e_tgetstr("ei", &tp);
  373.   if (e_tgetflag("in"))
  374.     IC = IM = 0;
  375.   if (IC && IC[0] == '\0')
  376.     IC = 0;
  377.   if (CIC && CIC[0] == '\0')
  378.     CIC = 0;
  379.   if (IM && IM[0] == '\0')
  380.     IM = 0;
  381.   if (EI && EI[0] == '\0')
  382.     EI = 0;
  383.   if (EI == 0)
  384.     IM = 0;
  385.   if (IC && IM && strcmp(IC, IM) == 0)
  386.     IC = 0;
  387.   KS = e_tgetstr("ks", &tp);
  388.   KE = e_tgetstr("ke", &tp);
  389.   if (KE == 0)
  390.     KS = 0;
  391.   ISO2022 = e_tgetflag("G0");
  392.   if (ISO2022)
  393.     {
  394.       if ((S0 = e_tgetstr("S0", &tp)) == NULL)
  395. #ifdef TERMINFO
  396.     S0 = "\033(%p1%c";
  397. #else
  398.     S0 = "\033(%.";
  399. #endif
  400.       if ((E0 = e_tgetstr("E0", &tp)) == NULL)
  401.     E0 = "\033(B";
  402.       C0 = e_tgetstr("C0", &tp);
  403.     }
  404.   else if ((S0 = e_tgetstr("as", &tp)) != NULL
  405.         && (E0 = e_tgetstr("ae", &tp)) != NULL)
  406.     {
  407.       ISO2022 = 1;
  408.       C0 = e_tgetstr("ac", &tp);
  409.     }
  410.   else
  411.     {
  412.       S0 = E0 = "";
  413.       C0 = "g.h.i'j-k-l-m-n+o~p\"q-r-s_t+u+v+w+x|y<z>";
  414.     }
  415.   for (i = 0; i < 256; i++)
  416.     c0_tab[i] = i;
  417.   if (C0)
  418.     for (i = strlen(C0)&~1; i >= 0; i-=2)
  419.       c0_tab[C0[i]] = C0[i+1];
  420.   debug1("ISO2022 = %d\n", ISO2022);
  421.   /* WS changes the window size */
  422.   WS = e_tgetstr("WS", &tp);
  423.   VI = e_tgetstr("vi", &tp);
  424.   VE = e_tgetstr("ve", &tp);
  425.   VS = e_tgetstr("vs", &tp);
  426.   PO = e_tgetstr("po", &tp);
  427.   if (!(PF = e_tgetstr("pf", &tp)))
  428.     PO = 0;
  429.   debug2("terminal size is %d, %d (says TERMCAP)\n", TermcapCOLS, TermcapROWS);
  430.   /* Termcap fields Z0 & Z1 contain width-changing sequences. */
  431.   if ((Z0 = e_tgetstr("Z0", &tp)) != NULL
  432.       && (Z1 = e_tgetstr("Z1", &tp)) == NULL)
  433.     Z0 = NULL;
  434.  
  435.   Z0width = 132;
  436.   Z1width = 80;
  437.  
  438.   CheckScreenSize(0);
  439.   if ((HS = e_tgetflag("hs")) != 0)
  440.     {
  441.       debug("oy! we have a hardware status line, says termcap\n");
  442.       TS = e_tgetstr("ts", &tp);
  443.       FS = e_tgetstr("fs", &tp);
  444.       DS = e_tgetstr("ds", &tp);
  445.       if ((HS = e_tgetnum("ws")) <= 0)
  446.     HS = screenwidth;
  447.       if (!TS || !FS || !DS)
  448.     HS = 0;
  449.     }
  450.   termcapHS = HS;
  451.   if (!use_hardstatus)
  452.     HS = 0;
  453.  
  454.   UPcost = CalcCost(UP);
  455.   DOcost = CalcCost(DO);
  456.   NLcost = CalcCost(NL);
  457.   LEcost = CalcCost(BC);
  458.   NDcost = CalcCost(ND);
  459.   CRcost = CalcCost(CR);
  460.   IMcost = CalcCost(IM);
  461.   EIcost = CalcCost(EI);
  462.   for (i = 0; i < NKEYCAPS; i++)
  463.     KeyCapsArr[i] = e_tgetstr(KeyCaps[i], &tp);
  464.   MakeTermcap(0);
  465. }
  466.  
  467. /*
  468.  * if the adaptflag is on, we keep the size of this display, else
  469.  * we may try to restore our old window sizes.
  470.  */
  471. void
  472. InitTerm(adapt)
  473. int adapt;
  474. {
  475.   display = 1;
  476.   screentop = screenbot = -1;
  477.   PutStr(IS);
  478.   PutStr(TI);
  479.   if (IM && strcmp(IM, EI))
  480.     PutStr(EI);
  481.   insert = 0;
  482.   if (KS && strcmp(KS, KE))
  483.     PutStr(KE);
  484.   keypad = 0;
  485.   ResizeScreen((struct win *)0);
  486.   ChangeScrollRegion(0, screenheight-1);
  487.   PutStr(CL);
  488.   screenx = screeny = 0;
  489.   fflush(stdout);
  490.   debug1("we %swant to adapt all our windows to the display\n", 
  491.      (adapt) ? "" : "don't ");
  492.   /* In case the size was changed by a init sequence */
  493.   CheckScreenSize((adapt) ? 2 : 0);
  494. }
  495.  
  496. void
  497. FinitTerm()
  498. {
  499.   display = 1;
  500.   flow = 1;
  501.   InsertMode(0);
  502.   KeypadMode(0);
  503.   ResizeScreen((struct win *)0);
  504.   ChangeScrollRegion(0, screenheight - 1);
  505.   SaveSetAttr(0, ASCII);
  506.   screenx = screeny = -1;
  507.   GotoPos(0, screenheight - 1);
  508.   PutStr(TE);
  509.   fflush(stdout);
  510.   if (Termcap) 
  511.     {
  512.       Free(Termcap);
  513.       debug("FinitTerm: old termcap freed\n");
  514.     }
  515.   if (tbuf) 
  516.     {
  517.       Free(tbuf);
  518.       debug("FinitTerm: old tbuf freed\n");
  519.     }
  520.   if (tentry) 
  521.     {
  522.       Free(tentry);
  523.       debug("FinitTerm: old tentry freed\n");
  524.     }
  525. }
  526.  
  527. static void AddCap(s)
  528. char *s;
  529. {
  530.   register int n;
  531.  
  532.   if (tcLineLen + (n = strlen(s)) > 55 && Termcaplen < 1024-4)
  533.     {
  534.       strcpy(Termcap + Termcaplen, "\\\n\t:");
  535.       Termcaplen += 4;
  536.       tcLineLen = 0;
  537.     }
  538.   if (Termcaplen + n < 1024)
  539.     {
  540.       strcpy(Termcap + Termcaplen, s);
  541.       Termcaplen += n;
  542.       tcLineLen += n;
  543.     }
  544.   else
  545.     Msg(0, "TERMCAP overflow - sorry.");
  546. }
  547.  
  548. char *MakeTermcap(aflag)
  549. int aflag;
  550. {
  551.   char buf[1024];
  552.   register char *p, *cp, ch;
  553.   int i;
  554.  
  555.   if (screencap)
  556.     {
  557.       sprintf(Term, "TERM=screen");
  558.       return Termcap;
  559.     }
  560.   if (screenterm == 0 || *screenterm == '\0')
  561.     {
  562.       debug("MakeTermcap sets screenterm=screen\n");
  563.       strcpy(screenterm, "screen");
  564.     }
  565.   for (;;)
  566.     {
  567.       sprintf(Term, "TERM=");
  568.       p = Term + 5;
  569.       if (!aflag && (unsigned)strlen(screenterm)+strlen(termname) < MAXSTR-1)
  570.     {
  571.       sprintf(p, "%s.%s", screenterm, termname);
  572.       if (tgetent(buf, p) == 1)
  573.         break;
  574.     }
  575.       if (screenwidth >= 132)
  576.     {
  577.       sprintf(p, "%s-w", screenterm);
  578.           if (tgetent(buf, p) == 1)
  579.         break;
  580.     }
  581.       sprintf(p, "%s", screenterm);
  582.       if (tgetent(buf, p) == 1)
  583.     break;
  584.       sprintf(p, "vt100");
  585.       break;
  586.     }
  587.   tcLineLen = 100;    /* Force NL */
  588.   sprintf(Termcap,
  589.       "TERMCAP=SC|%s|VT 100/ANSI X3.64 virtual terminal|", p);
  590.   Termcaplen = strlen(Termcap);
  591.   if (extra_outcap && *extra_outcap)
  592.     {
  593.       for (cp = extra_outcap; p = index(cp, ':'); cp = p)
  594.     {
  595.       ch = *++p;
  596.       *p = '\0';
  597.       AddCap(cp);
  598.       *p = ch;
  599.     }
  600.       tcLineLen = 100;    /* Force NL */
  601.     }
  602.   if (Termcaplen + (unsigned)strlen(TermcapConst) < 1024)
  603.     {
  604.       strcpy(Termcap + Termcaplen, TermcapConst);
  605.       Termcaplen += strlen(TermcapConst);
  606.     }
  607.   sprintf(buf, "li#%d:co#%d:", screenheight, screenwidth);
  608.   AddCap(buf);
  609.   if ((force_vt && !COP) || LP || !AM)
  610.     AddCap("LP:");
  611.   else
  612.     AddCap("am:");
  613.   if (VB)
  614.     AddCap("vb=\\E[?5h\\E[?5l:");
  615.   if (US)
  616.     {
  617.       AddCap("us=\\E[4m:");
  618.       AddCap("ue=\\E[24m:");
  619.     }
  620.   if (SO)
  621.     {
  622.       AddCap("so=\\E[3m:");
  623.       AddCap("se=\\E[23m:");
  624.     }
  625.   if (MB)
  626.     AddCap("mb=\\E[5m:");
  627.   if (MD)
  628.     AddCap("md=\\E[1m:");
  629.   if (MH)
  630.     AddCap("mh=\\E[2m:");
  631.   if (MR)
  632.     AddCap("mr=\\E[7m:");
  633.   if (MB || MD || MH || MR)
  634.     AddCap("me=\\E[m:ms:");
  635.   if ((CS && SR) || AL || CAL || aflag)
  636.     {
  637.       AddCap("sr=\\EM:");
  638.       AddCap("al=\\E[L:");
  639.       AddCap("AL=\\E[%dL:");
  640.     }
  641.   else if (SR)
  642.     AddCap("sr=\\EM:");
  643.   if (CS || DL || CDL || aflag)
  644.     {
  645.       AddCap("dl=\\E[M:");
  646.       AddCap("DL=\\E[%dM:");
  647.     }
  648.   if (CS)
  649.     AddCap("cs=\\E[%i%d;%dr:");
  650.   if (DC || CDC || aflag)
  651.     {
  652.       AddCap("dc=\\E[P:");
  653.       AddCap("DC=\\E[%dP:");
  654.     }
  655.   if (CIC || IC || IM || aflag)
  656.     {
  657.       AddCap("im=\\E[4h:");
  658.       AddCap("ei=\\E[4l:");
  659.       AddCap("mi:");
  660.       AddCap("ic=\\E[@:");
  661.       AddCap("IC=\\E[%d@:");
  662.     }
  663.   if (KS)
  664.     AddCap("ks=\\E=:");
  665.   if (KE)
  666.     AddCap("ke=\\E>:");
  667.   if (ISO2022)
  668.     AddCap("G0:");
  669.   if (PO)
  670.     {
  671.       AddCap("po=\\E[5i:");
  672.       AddCap("pf=\\E[4i:");
  673.     }
  674.   if (Z0)
  675.     {
  676.       AddCap("Z0=\\E[?3h:");
  677.       AddCap("Z1=\\E[?3l:");
  678.     }
  679.   if (WS)
  680.     AddCap("WS=\\E[8;%d;%dt:");
  681.   for (i = 0; i < NKEYCAPS; i++)
  682.     {
  683.       if (KeyCapsArr[i] == 0)
  684.     continue;
  685.       MakeString(KeyCaps[i], buf, sizeof(buf), KeyCapsArr[i]);
  686.       AddCap(buf);
  687.     }
  688.   return Termcap;
  689. }
  690.  
  691. static void MakeString(cap, buf, buflen, s)
  692. char *cap, *buf;
  693. int buflen;
  694. char *s;
  695. {
  696.   register char *p, *pmax;
  697.   register unsigned int c;
  698.  
  699.   p = buf;
  700.   pmax = p + buflen - (3+4+2);
  701.   *p++ = *cap++;
  702.   *p++ = *cap;
  703.   *p++ = '=';
  704.   while ((c = *s++) && p < pmax)
  705.     {
  706.       switch (c)
  707.     {
  708.     case '\033':
  709.       *p++ = '\\';
  710.       *p++ = 'E';
  711.       break;
  712.     case ':':
  713.       sprintf(p, "\\072");
  714.       p += 4;
  715.       break;
  716.     case '^':
  717.     case '\\':
  718.       *p++ = '\\';
  719.       *p++ = c;
  720.       break;
  721.     default:
  722.       if (c >= 200)
  723.         {
  724.           sprintf(p, "\\%03o", c & 0377);
  725.           p += 4;
  726.         }
  727.       else if (c < ' ')
  728.         {
  729.           *p++ = '^';
  730.           *p++ = c + '@';
  731.         }
  732.       else
  733.         *p++ = c;
  734.     }
  735.     }
  736.   *p++ = ':';
  737.   *p = '\0';
  738. }
  739.  
  740. void
  741. Activate()
  742. {
  743.   debug("Activate()\n");
  744.   if (display)
  745.     RemoveStatus();
  746.   display = fore->active = 1;
  747.   ResizeScreen(fore);
  748.   SetCurr(fore);
  749.   debug3("Fore (%d) has size %dx%d", ForeNum, curr->width, curr->height);
  750.   debug1("(%d)\n", curr->histheight);
  751.   ChangeScrollRegion(curr->top, curr->bot);
  752.   KeypadMode(curr->keypad);
  753.   FlowMode(curr->flow);
  754.   if (curr->monitor != MON_OFF)
  755.     curr->monitor = MON_ON;
  756.   curr->bell = BELL_OFF;
  757.   Redisplay();
  758. }
  759.  
  760. void
  761. ResetScreen(p)
  762. register struct win *p;
  763. {
  764.   register int i;
  765.  
  766.   p->wrap = wrap;
  767.   p->origin = 0;
  768.   p->insert = 0;
  769.   p->vbwait = 0;
  770.   p->keypad = 0;
  771.   p->top = 0;
  772.   p->bot = p->height - 1;
  773.   p->saved = 0;
  774.   p->LocalAttr = 0;
  775.   p->x = p->y = 0;
  776.   p->state = LIT;
  777.   p->StringType = NONE;
  778.   p->ss = 0;
  779.   p->LocalCharset = G0;
  780.   bzero(p->tabs, p->width);
  781.   for (i = 8; i < p->width; i += 8)
  782.     p->tabs[i] = 1;
  783.   for (i = G0; i <= G3; i++)
  784.     p->charsets[i] = ASCII;
  785. }
  786.  
  787. void
  788. WriteString(wp, buf, len)
  789. struct win *wp;
  790. register char *buf;
  791. int len;
  792. {
  793.   register int c, intermediate = 0;
  794.  
  795.   if (!len)
  796.     return;
  797.   if (wp->logfp != NULL)
  798.     if (fwrite(buf, len, 1, wp->logfp) < 1)
  799.       {
  800.     extern int errno;
  801.  
  802.     Msg(errno, "Error writing logfile");
  803.     fclose(wp->logfp);
  804.     wp->logfp = NULL;
  805.       }
  806.   /*
  807.    * SetCurr() here may prevent output, as it may set display = 0
  808.    */
  809.   SetCurr(wp);
  810.   if (display)
  811.     {
  812.       if (!HS)
  813.     RemoveStatus();
  814.     }
  815.   else if (curr->monitor == MON_ON)
  816.     curr->monitor = MON_FOUND;
  817.  
  818.   do
  819.     {
  820.       c = *buf++;
  821.       if (c == '\0' || c == '\177')
  822.     continue;
  823.     NextChar:
  824.       switch (curr->state)
  825.     {
  826.     case PRIN:
  827.       switch (c)
  828.         {
  829.         case '\033':
  830.           curr->state = PRINESC;
  831.           break;
  832.         default:
  833.           PrintChar(c);
  834.         }
  835.       break;
  836.     case PRINESC:
  837.       switch (c)
  838.         {
  839.         case '[':
  840.           curr->state = PRINCSI;
  841.           break;
  842.         default:
  843.           PrintChar('\033');
  844.           PrintChar(c);
  845.           curr->state = PRIN;
  846.         }
  847.       break;
  848.     case PRINCSI:
  849.       switch (c)
  850.         {
  851.         case '4':
  852.           curr->state = PRIN4;
  853.           break;
  854.         default:
  855.           PrintChar('\033');
  856.           PrintChar('[');
  857.           PrintChar(c);
  858.           curr->state = PRIN;
  859.         }
  860.       break;
  861.     case PRIN4:
  862.       switch (c)
  863.         {
  864.         case 'i':
  865.           curr->state = LIT;
  866.           PrintFlush();
  867.           break;
  868.         default:
  869.           PrintChar('\033');
  870.           PrintChar('[');
  871.           PrintChar('4');
  872.           PrintChar(c);
  873.           curr->state = PRIN;
  874.         }
  875.       break;
  876.     case STRESC:
  877.       switch (c)
  878.         {
  879.         case '\\':
  880.           curr->state = LIT;
  881.           *(curr->stringp) = '\0';
  882.           switch (curr->StringType)
  883.         {
  884.         case PM:
  885.           if (!display)
  886.             break;
  887.           MakeStatus(curr->string);
  888.           if (!HS && status && len > 1)
  889.             {
  890.               curr->outlen = len - 1;
  891.               bcopy(buf, curr->outbuf, curr->outlen);
  892.               return;
  893.             }
  894.           break;
  895.         case DCS:
  896.           if (display)
  897.             printf("%s", curr->string);
  898.           break;
  899.         case AKA:
  900.           if (curr->akapos == 0 && !*curr->string)
  901.             break;
  902.           strncpy(curr->cmd + curr->akapos, curr->string, 20);
  903.           if (!*curr->string)
  904.             curr->autoaka = curr->y + 1;
  905.           break;
  906.         default:
  907.           break;
  908.         }
  909.           break;
  910.         default:
  911.           curr->state = ASTR;
  912.           AddChar('\033');
  913.           AddChar(c);
  914.         }
  915.       break;
  916.     case ASTR:
  917.       switch (c)
  918.         {
  919.         case '\0':
  920.           break;
  921.         case '\033':
  922.           curr->state = STRESC;
  923.           break;
  924.         default:
  925.           AddChar(c);
  926.         }
  927.       break;
  928.     case ESC:
  929.       switch (c)
  930.         {
  931.         case '[':
  932.           curr->NumArgs = 0;
  933.           intermediate = 0;
  934.           bzero((char *) curr->args, MAXARGS * sizeof(int));
  935.           curr->state = CSI;
  936.           break;
  937.         case ']':
  938.           StartString(OSC);
  939.           break;
  940.         case '_':
  941.           StartString(APC);
  942.           break;
  943.         case 'P':
  944.           StartString(DCS);
  945.           break;
  946.         case '^':
  947.           StartString(PM);
  948.           break;
  949.         case '"':
  950.         case 'k':
  951.           StartString(AKA);
  952.           break;
  953.         default:
  954.           if (Special(c))
  955.         break;
  956.           if (c >= ' ' && c <= '/')
  957.         intermediate = intermediate ? -1 : c;
  958.           else if (c >= '0' && c <= '~')
  959.         {
  960.           DoESC(c, intermediate);
  961.           curr->state = LIT;
  962.         }
  963.           else
  964.         {
  965.           curr->state = LIT;
  966.           goto NextChar;
  967.         }
  968.         }
  969.       break;
  970.     case CSI:
  971.       switch (c)
  972.         {
  973.         case '0':
  974.         case '1':
  975.         case '2':
  976.         case '3':
  977.         case '4':
  978.         case '5':
  979.         case '6':
  980.         case '7':
  981.         case '8':
  982.         case '9':
  983.           if (curr->NumArgs < MAXARGS)
  984.         {
  985.           curr->args[curr->NumArgs] =
  986.             10 * curr->args[curr->NumArgs] + c - '0';
  987.         }
  988.           break;
  989.         case ';':
  990.         case ':':
  991.           curr->NumArgs++;
  992.           break;
  993.         default:
  994.           if (Special(c))
  995.         break;
  996.           if (c >= '@' && c <= '~')
  997.         {
  998.           curr->NumArgs++;
  999.           DoCSI(c, intermediate);
  1000.           if (curr->state != PRIN)
  1001.             curr->state = LIT;
  1002.         }
  1003.           else if ((c >= ' ' && c <= '/') || (c >= '<' && c <= '?'))
  1004.         intermediate = intermediate ? -1 : c;
  1005.           else
  1006.         {
  1007.           curr->state = LIT;
  1008.           goto NextChar;
  1009.         }
  1010.         }
  1011.       break;
  1012.     case LIT:
  1013.     default:
  1014.       if (!Special(c))
  1015.         {
  1016.           if (c == '\033')
  1017.         {
  1018.           intermediate = 0;
  1019.           curr->state = ESC;
  1020.           if (display && lp_missing && (CIC || IC || IM))
  1021.             {
  1022.               RedisplayLine(blank, null, null, screenbot,
  1023.                     cols - 2, cols - 1);
  1024.               GotoPos(curr->x, curr->y);
  1025.             }
  1026.           if (curr->autoaka < 0)
  1027.             curr->autoaka = 0;
  1028.         }
  1029.           else if (c < ' ')
  1030.         break;
  1031.           else
  1032.         {
  1033.           NewRendition(curr->LocalAttr);
  1034.           NewCharset(curr->charsets[(curr->ss) ? curr->ss :
  1035.                          curr->LocalCharset]);
  1036.           if (curr->x < cols - 1)
  1037.             {
  1038.               if (curr->insert)
  1039.             InsertAChar(c);
  1040.               else
  1041.             {
  1042.               if (display)
  1043.                 PUTCHAR(c);
  1044.               SetChar(c);
  1045.             }
  1046.               curr->x++;
  1047.             }
  1048.           else if (curr->x == cols - 1)
  1049.             {
  1050.               if (curr->wrap && (LP || !force_vt || COP))
  1051.             {
  1052.               if (display)
  1053.                 RAW_PUTCHAR(c);
  1054.               SetChar(c);
  1055.               if (AM && !LP)
  1056.                 {
  1057.                   curr->x = 0; /* terminal auto-wrapped */
  1058.                   LineFeed(0);
  1059.                 }
  1060.               else
  1061.                 curr->x++;
  1062.             }
  1063.               else
  1064.             {
  1065.               if (display)
  1066.                 {
  1067.                   if (LP || curr->y != screenbot)
  1068.                 {
  1069.                   RAW_PUTCHAR(c);
  1070.                   GotoPos(curr->x, curr->y);
  1071.                 }
  1072.                   else
  1073.                 CheckLP(c);
  1074.                 }
  1075.               SetChar(c);
  1076.               if (curr->wrap)
  1077.                 curr->x++;
  1078.             }
  1079.             }
  1080.           else
  1081.             {
  1082.               LineFeed(2); /* cr+lf, handle LP */
  1083.               if (curr->insert)
  1084.             InsertAChar(c);
  1085.               else
  1086.             {
  1087.               if (display)
  1088.                 PUTCHAR(c);
  1089.               SetChar(c);
  1090.             }
  1091.               curr->x = 1;
  1092.             }
  1093.           if (curr->ss)
  1094.             {
  1095.               NewCharset(curr->charsets[curr->LocalCharset]);
  1096.               curr->ss = 0;
  1097.             }
  1098.         }
  1099.         }
  1100.     }
  1101.     } while (--len);
  1102.   curr->outlen = 0;
  1103.   if (curr->state == PRIN)
  1104.     PrintFlush();
  1105. }
  1106.  
  1107. static int Special(c)
  1108. register int c;
  1109. {
  1110.   switch (c)
  1111.     {
  1112.     case '\b':
  1113.       BackSpace();
  1114.       return 1;
  1115.     case '\r':
  1116.       Return();
  1117.       return 1;
  1118.     case '\n':
  1119.       if (curr->autoaka)
  1120.     FindAKA();
  1121.       LineFeed(1);
  1122.       return 1;
  1123.     case '\007':
  1124.       if (!visual_bell)
  1125.     PutStr(BL);
  1126.       else
  1127.     {
  1128.       if (!VB)
  1129.         curr->bell = BELL_VISUAL;
  1130.       else
  1131.         PutStr(VB);
  1132.     }
  1133.       if (!display)
  1134.     curr->bell = BELL_ON;
  1135.       return 1;
  1136.     case '\t':
  1137.       ForwardTab();
  1138.       return 1;
  1139.     case '\017':        /* SI */
  1140.       MapCharset(G0);
  1141.       return 1;
  1142.     case '\016':        /* SO */
  1143.       MapCharset(G1);
  1144.       return 1;
  1145.     }
  1146.   return 0;
  1147. }
  1148.  
  1149. static void DoESC(c, intermediate)
  1150. int c, intermediate;
  1151. {
  1152.   switch (intermediate)
  1153.     {
  1154.     case 0:
  1155.       switch (c)
  1156.     {
  1157.     case 'E':
  1158.       LineFeed(2);
  1159.       break;
  1160.     case 'D':
  1161.       LineFeed(1);
  1162.       break;
  1163.     case 'M':
  1164.       ReverseLineFeed();
  1165.       break;
  1166.     case 'H':
  1167.       curr->tabs[curr->x] = 1;
  1168.       break;
  1169.     case 'Z':        /* jph: Identify as VT100 */
  1170.       Report(curr, "\033[?%d;%dc", 1, 2);
  1171.       break;
  1172.     case '7':
  1173.       SaveCursor();
  1174.       break;
  1175.     case '8':
  1176.       RestoreCursor();
  1177.       break;
  1178.     case 'c':
  1179.       ClearScreen();
  1180.       ResetScreen(curr);
  1181.       NewRendition(0);
  1182.       NewCharset(ASCII);
  1183.       InsertMode(0);
  1184.       KeypadMode(0);
  1185.       ChangeScrollRegion(0, rows-1);
  1186.       break;
  1187.     case '=':
  1188.       KeypadMode(1);
  1189.       curr->keypad = 1;
  1190.       if (curr->autoflow)
  1191.         FlowMode(curr->flow = 0);
  1192.       break;
  1193.     case '>':
  1194.       KeypadMode(0);
  1195.       curr->keypad = 0;
  1196.       if (curr->autoflow)
  1197.         FlowMode(curr->flow = 1);
  1198.       break;
  1199.     case 'n':        /* LS2 */
  1200.       MapCharset(G2);
  1201.       break;
  1202.     case 'o':        /* LS3 */
  1203.       MapCharset(G3);
  1204.       break;
  1205.     case 'N':        /* SS2 */
  1206.       if (curr->charsets[curr->LocalCharset] != curr->charsets[G2])
  1207.         curr->ss = G2;
  1208.       else
  1209.         curr->ss = 0;
  1210.       break;
  1211.     case 'O':        /* SS3 */
  1212.       if (curr->charsets[curr->LocalCharset] != curr->charsets[G3])
  1213.         curr->ss = G3;
  1214.       else
  1215.         curr->ss = 0;
  1216.       break;
  1217.     }
  1218.       break;
  1219.     case '#':
  1220.       switch (c)
  1221.     {
  1222.     case '8':
  1223.       FillWithEs();
  1224.       break;
  1225.     }
  1226.       break;
  1227.     case '(':
  1228.       DesignateCharset(c, G0);
  1229.       break;
  1230.     case ')':
  1231.       DesignateCharset(c, G1);
  1232.       break;
  1233.     case '*':
  1234.       DesignateCharset(c, G2);
  1235.       break;
  1236.     case '+':
  1237.       DesignateCharset(c, G3);
  1238.       break;
  1239.     }
  1240. }
  1241.  
  1242. static void DoCSI(c, intermediate)
  1243. int c, intermediate;
  1244. {
  1245.   register int i, a1 = curr->args[0], a2 = curr->args[1];
  1246.  
  1247.   if (curr->NumArgs > MAXARGS)
  1248.     curr->NumArgs = MAXARGS;
  1249.   switch (intermediate)
  1250.     {
  1251.     case 0:
  1252.       switch (c)
  1253.     {
  1254.     case 'H':
  1255.     case 'f':
  1256.       if (a1 < 1)
  1257.         a1 = 1;
  1258.       if (curr->origin)
  1259.         a1 += curr->top;
  1260.       if (a1 > rows)
  1261.         a1 = rows;
  1262.       if (a2 < 1)
  1263.         a2 = 1;
  1264.       if (a2 > cols)
  1265.         a2 = cols;
  1266.       GotoPos(--a2, --a1);
  1267.       curr->x = a2;
  1268.       curr->y = a1;
  1269.       if (curr->autoaka)
  1270.         curr->autoaka = a1 + 1;
  1271.       break;
  1272.     case 'J':
  1273.       if (a1 < 0 || a1 > 2)
  1274.         a1 = 0;
  1275.       switch (a1)
  1276.         {
  1277.         case 0:
  1278.           ClearToEOS();
  1279.           break;
  1280.         case 1:
  1281.           ClearFromBOS();
  1282.           break;
  1283.         case 2:
  1284.           ClearScreen();
  1285.           GotoPos(curr->x, curr->y);
  1286.           break;
  1287.         }
  1288.       break;
  1289.     case 'K':
  1290.       if (a1 < 0 || a1 > 2)
  1291.         a1 %= 3;
  1292.       switch (a1)
  1293.         {
  1294.         case 0:
  1295.           ClearToEOL();
  1296.           break;
  1297.         case 1:
  1298.           ClearFromBOL();
  1299.           break;
  1300.         case 2:
  1301.           ClearLine();
  1302.           break;
  1303.         }
  1304.       break;
  1305.     case 'A':
  1306.       CursorUp(a1 ? a1 : 1);
  1307.       break;
  1308.     case 'B':
  1309.       CursorDown(a1 ? a1 : 1);
  1310.       break;
  1311.     case 'C':
  1312.       CursorRight(a1 ? a1 : 1);
  1313.       break;
  1314.     case 'D':
  1315.       CursorLeft(a1 ? a1 : 1);
  1316.       break;
  1317.     case 'm':
  1318.       SelectRendition();
  1319.       break;
  1320.     case 'g':
  1321.       if (a1 == 0)
  1322.         curr->tabs[curr->x] = 0;
  1323.       else if (a1 == 3)
  1324.         bzero(curr->tabs, cols);
  1325.       break;
  1326.     case 'r':
  1327.       if (!a1)
  1328.         a1 = 1;
  1329.       if (!a2)
  1330.         a2 = rows;
  1331.       if (a1 < 1 || a2 > rows || a1 >= a2)
  1332.         break;
  1333.       curr->top = a1 - 1;
  1334.       curr->bot = a2 - 1;
  1335.       ChangeScrollRegion(curr->top, curr->bot);
  1336.       if (curr->origin)
  1337.         {
  1338.           GotoPos(0, curr->top);
  1339.           curr->y = curr->top;
  1340.           curr->x = 0;
  1341.         }
  1342.       else
  1343.         {
  1344.           GotoPos(0, 0);
  1345.           curr->y = curr->x = 0;
  1346.         }
  1347.       break;
  1348.     case 's':
  1349.       SaveCursor();
  1350.       break;
  1351.     case 't':
  1352.       if (a1 != 8)
  1353.         break;
  1354.       a1 = curr->args[2];
  1355.       if (a1 < 1)
  1356.         a1 = curr->width;
  1357.       if (a2 < 1)
  1358.         a2 = curr->height;
  1359.       if (WS == NULL)
  1360.         {
  1361.           a2 = curr->height;
  1362.           if (Z0 == NULL || (a1 != Z0width && a1 != Z1width))
  1363.             a1 = curr->width;
  1364.          }
  1365.       if (a1 == curr->width && a2 == curr->height)
  1366.         break;
  1367.           ChangeWindowSize(curr, a1, a2);
  1368.       SetCurr(curr);
  1369.       if (display)
  1370.         Activate();
  1371.       break;
  1372.     case 'u':
  1373.       RestoreCursor();
  1374.       break;
  1375.     case 'I':
  1376.       if (!a1)
  1377.         a1 = 1;
  1378.       while (a1--)
  1379.         ForwardTab();
  1380.       break;
  1381.     case 'Z':
  1382.       if (!a1)
  1383.         a1 = 1;
  1384.       while (a1--)
  1385.         BackwardTab();
  1386.       break;
  1387.     case 'L':
  1388.       InsertLine(a1 ? a1 : 1);
  1389.       break;
  1390.     case 'M':
  1391.       DeleteLine(a1 ? a1 : 1);
  1392.       break;
  1393.     case 'P':
  1394.       DeleteChar(a1 ? a1 : 1);
  1395.       break;
  1396.     case '@':
  1397.       InsertChar(a1 ? a1 : 1);
  1398.       break;
  1399.     case 'h':
  1400.       ASetMode(1);
  1401.       break;
  1402.     case 'l':
  1403.       ASetMode(0);
  1404.       break;
  1405.     case 'i':
  1406.       if (PO && a1 == 5)
  1407.         {
  1408.           curr->stringp = curr->string;
  1409.           curr->state = PRIN;
  1410.         }
  1411.       break;
  1412.     case 'n':
  1413.       if (a1 == 6)        /* Report cursor position */
  1414.         Report(curr, "\033[%d;%dR", curr->y + 1, curr->x + 1);
  1415.       break;
  1416.     case 'c':        /* Identify as VT100 */
  1417.       Report(curr, "\033[?%d;%dc", 1, 2);
  1418.       break;
  1419.     }
  1420.       break;
  1421.     case '?':
  1422.       debug2("\\E[?%d%c\n",a1,c);
  1423.       if (c != 'h' && c != 'l')
  1424.     break;
  1425.       i = (c == 'h');
  1426.       switch (a1)
  1427.     {
  1428.     case 3:
  1429.       i = (i ? Z0width : Z1width);
  1430.       if ((Z0 || WS) && curr->width != i)
  1431.         {
  1432.               ChangeWindowSize(curr, i, curr->height);
  1433.           SetCurr(curr);
  1434.           if (display)
  1435.         Activate();
  1436.         }
  1437.       break;
  1438.     case 5:
  1439.       if (i)
  1440.         curr->vbwait = 1;
  1441.       else
  1442.         {
  1443.           if (curr->vbwait)
  1444.         PutStr(VB);
  1445.           curr->vbwait = 0;
  1446.         }
  1447.       break;
  1448.     case 6:
  1449.       if ((curr->origin = i) != 0)
  1450.         {
  1451.           GotoPos(0, curr->top);
  1452.           curr->y = curr->top;
  1453.           curr->x = 0;
  1454.         }
  1455.       else
  1456.         {
  1457.           GotoPos(0, 0);
  1458.           curr->y = curr->x = 0;
  1459.         }
  1460.       break;
  1461.     case 7:
  1462.       curr->wrap = i;
  1463.       break;
  1464.     case 35:
  1465.       debug1("Cursor %svisible\n", i?"in":"");
  1466.       curr->cursor_invisible = i;
  1467.       break;
  1468.     }
  1469.       break;
  1470.     }
  1471. }
  1472.  
  1473. void
  1474. INSERTCHAR(c)
  1475. int c;
  1476. {
  1477.   if (!insert && (IC || CIC))
  1478.     {
  1479.       if (IC)
  1480.         PutStr(IC);
  1481.       else
  1482.         CPutStr(CIC, 1);
  1483.       RAW_PUTCHAR(c);
  1484.       return;
  1485.     }
  1486.   InsertMode(1);
  1487.   if (insert)
  1488.     RAW_PUTCHAR(c);
  1489.   else
  1490.     RefreshLine(screeny, screenx, screenwidth-1);
  1491. }
  1492.  
  1493. void
  1494. PUTCHAR(c)
  1495. int c;
  1496. {
  1497.   if (insert)
  1498.     InsertMode(0);
  1499.   RAW_PUTCHAR(c);
  1500. }
  1501.  
  1502. /*
  1503.  * RAW_PUTCHAR() is for all text that will be displayed.
  1504.  * NOTE, that charset Nr. 0 has a conversion table, but c1, c2, ... don't.
  1505.  */
  1506.  
  1507. static void
  1508. RAW_PUTCHAR(c)
  1509. int c;
  1510. {
  1511.   if (GlobalCharset == '0')
  1512.     putchar(c0_tab[c]);
  1513.   else
  1514.     putchar(c);
  1515.   if (screenx < screenwidth - 1)
  1516.     screenx++;
  1517.   else
  1518.     {
  1519.       screenx++;
  1520.       if ((AM && !LP) || screenx > screenwidth)
  1521.     {
  1522.       screenx -= screenwidth;
  1523.       if (screeny < screenheight-1 && screeny != screenbot)
  1524.         screeny++;
  1525.     }
  1526.     }
  1527. }
  1528.  
  1529. void
  1530. PutChar(c)
  1531. int c;
  1532. {
  1533.   /* this PutChar for ESC-sequences only */
  1534.   putchar(c);
  1535. }
  1536.  
  1537. void
  1538. PutStr(s)
  1539. char *s;
  1540. {
  1541.   if (display && s)
  1542.     tputs(s, 1, PutChar);
  1543. }
  1544.  
  1545. static void CPutStr(s, c)
  1546. char *s;
  1547. int c;
  1548. {
  1549.   if (display && s)
  1550.     tputs(tgoto(s, 0, c), 1, PutChar);
  1551. }
  1552.  
  1553. static void SetChar(c)
  1554. register int c;
  1555. {
  1556.   register struct win *p = curr;
  1557.  
  1558.   p->image[p->y][p->x] = c;
  1559.   p->attr[p->y][p->x] = p->LocalAttr;
  1560.   p->font[p->y][p->x] = p->charsets[p->ss ? p->ss : p->LocalCharset];
  1561. }
  1562.  
  1563. static void StartString(type)
  1564. enum string_t type;
  1565. {
  1566.   curr->StringType = type;
  1567.   curr->stringp = curr->string;
  1568.   curr->state = ASTR;
  1569. }
  1570.  
  1571. static void AddChar(c)
  1572. int c;
  1573. {
  1574.   if (curr->stringp >= curr->string + MAXSTR - 1)
  1575.     curr->state = LIT;
  1576.   else
  1577.     *(curr->stringp)++ = c;
  1578. }
  1579.  
  1580. static void PrintChar(c)
  1581. int c;
  1582. {
  1583.   if (curr->stringp >= curr->string + MAXSTR - 1)
  1584.     PrintFlush();
  1585.   *(curr->stringp)++ = c;
  1586. }
  1587.  
  1588. static void PrintFlush()
  1589. {
  1590.   if (curr->stringp > curr->string)
  1591.     {
  1592.       tputs(PO, 1, PutChar);
  1593.       (void) fflush(stdout);
  1594.       (void) write(1, curr->string, curr->stringp - curr->string);
  1595.       tputs(PF, 1, PutChar);
  1596.       (void) fflush(stdout);
  1597.       curr->stringp = curr->string;
  1598.     }
  1599. }
  1600.  
  1601. /* Insert mode is a toggle on some terminals, so we need this hack:
  1602.  */
  1603. void
  1604. InsertMode(on)
  1605. int on;
  1606. {
  1607.   if (display && on != insert && IM)
  1608.     {
  1609.       insert = on;
  1610.       if (insert)
  1611.     PutStr(IM);
  1612.       else
  1613.     PutStr(EI);
  1614.     }
  1615. }
  1616.  
  1617. /* ...and maybe keypad application mode is a toggle, too:
  1618.  */
  1619. static void KeypadMode(on)
  1620. int on;
  1621. {
  1622.   if (display && keypad != on && KS)
  1623.     {
  1624.       keypad = on;
  1625.       if (keypad)
  1626.     PutStr(KS);
  1627.       else
  1628.     PutStr(KE);
  1629.     }
  1630. }
  1631.  
  1632. static void FlowMode(on)
  1633. int on;
  1634. {
  1635.   if (display && flow != on)
  1636.     {
  1637.       flow = on;
  1638.       SetFlow(on);
  1639.     }
  1640. }
  1641.  
  1642. void
  1643. ToggleFlow()
  1644. {
  1645.   flow = fore->flow = !fore->flow;
  1646.   SetFlow(flow);
  1647. }
  1648.  
  1649. static void DesignateCharset(c, n)
  1650. int c, n;
  1651. {
  1652.   curr->ss = 0;
  1653.   if (c == 'B')
  1654.     c = ASCII;
  1655.   if (curr->charsets[n] != c)
  1656.     {
  1657.       curr->charsets[n] = c;
  1658.       if (curr->LocalCharset == n)
  1659.     NewCharset(c);
  1660.     }
  1661. }
  1662.  
  1663. static void MapCharset(n)
  1664. int n;
  1665. {
  1666.   curr->ss = 0;
  1667.   if (curr->LocalCharset != n)
  1668.     {
  1669.       curr->LocalCharset = n;
  1670.       NewCharset(curr->charsets[n]);
  1671.     }
  1672. }
  1673.  
  1674. void
  1675. NewCharset(new)
  1676. int new;
  1677. {
  1678.   if (!display || GlobalCharset == new)
  1679.     return;
  1680.   GlobalCharset = new;
  1681.   if (new == ASCII)
  1682.     PutStr(E0);
  1683.   else
  1684.     CPutStr(S0, new);
  1685. }
  1686.  
  1687. static void SaveCursor()
  1688. {
  1689.   curr->saved = 1;
  1690.   curr->Saved_x = curr->x;
  1691.   curr->Saved_y = curr->y;
  1692.   curr->SavedLocalAttr = curr->LocalAttr;
  1693.   curr->SavedLocalCharset = curr->LocalCharset;
  1694.   bcopy((char *) curr->charsets, (char *) curr->SavedCharsets,
  1695.     4 * sizeof(int));
  1696. }
  1697.  
  1698. static void RestoreCursor()
  1699. {
  1700.   if (curr->saved)
  1701.     {
  1702.       GotoPos(curr->Saved_x, curr->Saved_y);
  1703.       curr->x = curr->Saved_x;
  1704.       curr->y = curr->Saved_y;
  1705.       curr->LocalAttr = curr->SavedLocalAttr;
  1706.       NewRendition(curr->LocalAttr);
  1707.       bcopy((char *) curr->SavedCharsets, (char *) curr->charsets,
  1708.         4 * sizeof(int));
  1709.       curr->LocalCharset = curr->SavedLocalCharset;
  1710.       NewCharset(curr->charsets[curr->LocalCharset]);
  1711.     }
  1712. }
  1713.  
  1714. /*ARGSUSED*/
  1715. static void CountChars(c)
  1716. int c;
  1717. {
  1718.   StrCost++;
  1719. }
  1720.  
  1721. static int CalcCost(s)
  1722. register char *s;
  1723. {
  1724.   if (s)
  1725.     {
  1726.       StrCost = 0;
  1727.       tputs(s, 1, CountChars);
  1728.       return StrCost;
  1729.     }
  1730.   else
  1731.     return EXPENSIVE;
  1732. }
  1733.  
  1734. void
  1735. GotoPos(x2, y2)
  1736. int x2, y2;
  1737. {
  1738.   register int dy, dx, x1, y1;
  1739.   register int costx, costy;
  1740.   register int m;
  1741.   register char *s;
  1742.   int CMcost;
  1743.   enum move_t xm = M_NONE, ym = M_NONE;
  1744.  
  1745.   if (!display)
  1746.     return;
  1747.  
  1748.   x1 = screenx;
  1749.   y1 = screeny;
  1750.  
  1751.   if (x1 == screenwidth)
  1752.     if (LP && AM)
  1753.       x1 = -1;        /* don't know how the terminal treats this */
  1754.     else
  1755.       x1--;
  1756.   if (x2 == screenwidth)
  1757.     x2--;
  1758.   dx = x2 - x1;
  1759.   dy = y2 - y1;
  1760.   if (dy == 0 && dx == 0)
  1761.     {
  1762.       return;
  1763.     }
  1764.   if (!MS && GlobalAttr)    /* Save to move in SO mode ? */
  1765.     NewRendition(0);
  1766.   if (y1 < 0            /* don't know the y position */
  1767.       || (y2 > screenbot && y1 <= screenbot)    /* have to cross border */
  1768.       || (y2 < screentop && y1 >= screentop))    /* of scrollregion ?    */
  1769.     {
  1770.     DoCM:
  1771.       if (HO && !x2 && !y2)
  1772.         PutStr(HO);
  1773.       else
  1774.         PutStr(tgoto(CM, x2, y2));
  1775.       screenx = x2;
  1776.       screeny = y2;
  1777.       return;
  1778.     }
  1779.   /* Calculate CMcost */
  1780.   if (HO && !x2 && !y2)
  1781.     s = HO;
  1782.   else
  1783.     s = tgoto(CM, x2, y2);
  1784.   CMcost = CalcCost(s);
  1785.  
  1786.   /* Calculate the cost to move the cursor to the right x position */
  1787.   costx = EXPENSIVE;
  1788.   if (x1 >= 0)    /* relativ x positioning only if we know where we are */
  1789.     {
  1790.       if (dx > 0)
  1791.     {
  1792.       if (CRI && (dx > 1 || !ND))
  1793.         {
  1794.           costx = CalcCost(tgoto(CRI, 0, dx));
  1795.           xm = M_CRI;
  1796.         }
  1797.       if ((m = NDcost * dx) < costx)
  1798.         {
  1799.           costx = m;
  1800.           xm = M_RI;
  1801.         }
  1802.       /* Speedup: dx <= Rewrite() */
  1803.       if (dx < costx && (m = Rewrite(y1, x1, x2, 0)) < costx)
  1804.         {
  1805.           costx = m;
  1806.           xm = M_RW;
  1807.         }
  1808.     }
  1809.       else if (dx < 0)
  1810.     {
  1811.       if (CLE && (dx < -1 || !BC))
  1812.         {
  1813.           costx = CalcCost(tgoto(CLE, 0, -dx));
  1814.           xm = M_CLE;
  1815.         }
  1816.       if ((m = -dx * LEcost) < costx)
  1817.         {
  1818.           costx = m;
  1819.           xm = M_LE;
  1820.         }
  1821.     }
  1822.       else
  1823.     costx = 0;
  1824.     }
  1825.   /* Speedup: Rewrite() >= x2 */
  1826.   if (x2 + CRcost < costx && (m = Rewrite(y1, 0, x2, 0) + CRcost) < costx)
  1827.     {
  1828.       costx = m;
  1829.       xm = M_CR;
  1830.     }
  1831.  
  1832.   /* Check if it is already cheaper to do CM */
  1833.   if (costx >= CMcost)
  1834.     goto DoCM;
  1835.  
  1836.   /* Calculate the cost to move the cursor to the right y position */
  1837.   costy = EXPENSIVE;
  1838.   if (dy > 0)
  1839.     {
  1840.       if (CDO && dy > 1)    /* DO & NL are always != 0 */
  1841.     {
  1842.       costy = CalcCost(tgoto(CDO, 0, dy));
  1843.       ym = M_CDO;
  1844.     }
  1845.       if ((m = dy * ((x2 == 0) ? NLcost : DOcost)) < costy)
  1846.     {
  1847.       costy = m;
  1848.       ym = M_DO;
  1849.     }
  1850.     }
  1851.   else if (dy < 0)
  1852.     {
  1853.       if (CUP && (dy < -1 || !UP))
  1854.     {
  1855.       costy = CalcCost(tgoto(CUP, 0, -dy));
  1856.       ym = M_CUP;
  1857.     }
  1858.       if ((m = -dy * UPcost) < costy)
  1859.     {
  1860.       costy = m;
  1861.       ym = M_UP;
  1862.     }
  1863.     }
  1864.   else
  1865.     costy = 0;
  1866.  
  1867.   /* Finally check if it is cheaper to do CM */
  1868.   if (costx + costy >= CMcost)
  1869.     goto DoCM;
  1870.  
  1871.   switch (xm)
  1872.     {
  1873.     case M_LE:
  1874.       while (dx++ < 0)
  1875.     PutStr(BC);
  1876.       break;
  1877.     case M_CLE:
  1878.       CPutStr(CLE, -dx);
  1879.       break;
  1880.     case M_RI:
  1881.       while (dx-- > 0)
  1882.     PutStr(ND);
  1883.       break;
  1884.     case M_CRI:
  1885.       CPutStr(CRI, dx);
  1886.       break;
  1887.     case M_CR:
  1888.       PutStr(CR);
  1889.       screenx = 0;
  1890.       x1 = 0;
  1891.       /* FALLTHROUGH */
  1892.     case M_RW:
  1893.       if (x1 < x2)
  1894.     Rewrite(y1, x1, x2, 1);
  1895.       break;
  1896.     default:
  1897.       break;
  1898.     }
  1899.   switch (ym)
  1900.     {
  1901.     case M_UP:
  1902.       while (dy++ < 0)
  1903.     PutStr(UP);
  1904.       break;
  1905.     case M_CUP:
  1906.       CPutStr(CUP, -dy);
  1907.       break;
  1908.     case M_DO:
  1909.       s =  (x2 == 0) ? NL : DO;
  1910.       while (dy-- > 0)
  1911.     PutStr(s);
  1912.       break;
  1913.     case M_CDO:
  1914.       CPutStr(CDO, dy);
  1915.       break;
  1916.     default:
  1917.       break;
  1918.     }
  1919.   screenx = x2;
  1920.   screeny = y2;
  1921. }
  1922.  
  1923. static int
  1924. Rewrite(y, x1, x2, doit)
  1925. int y, x1, x2, doit;
  1926. {
  1927.   register int cost, dx;
  1928.   register char *p, *f, *i;
  1929.  
  1930.   if (x1 == x2)
  1931.     return(0);
  1932.   if (in_ovl)
  1933.     {
  1934.       if (ovl_Rewrite == 0)
  1935.         return EXPENSIVE;
  1936.       else
  1937.         return ((*ovl_Rewrite)(y, x1, x2, doit));
  1938.     }
  1939.   dx = x2 - x1;
  1940.   if (doit)
  1941.     {
  1942.       i = curr->image[y] + x1;
  1943.       while (dx-- > 0)
  1944.     PUTCHAR(*i++);
  1945.       return(0);
  1946.     }
  1947.   p = curr->attr[y] + x1;
  1948.   f = curr->font[y] + x1;
  1949.  
  1950.   cost = dx = x2 - x1;
  1951.   if (insert)
  1952.     cost += EIcost + IMcost;
  1953.   while(dx-- > 0)
  1954.     {
  1955.       if (*p++ != GlobalAttr || *f++ != GlobalCharset)
  1956.     return EXPENSIVE;
  1957.     }
  1958.   return cost;
  1959. }
  1960.  
  1961. static void BackSpace()
  1962. {
  1963.   if (curr->x > 0)
  1964.     {
  1965.       curr->x--;
  1966.     }
  1967.   else if (curr->wrap && curr->y > 0)
  1968.     {
  1969.       curr->x = cols - 1;
  1970.       curr->y--;
  1971.     }
  1972.   if (display)
  1973.     GotoPos(curr->x, curr->y);
  1974. }
  1975.  
  1976. static void Return()
  1977. {
  1978.   if (curr->x > 0)
  1979.     {
  1980.       curr->x = 0;
  1981.       if (display)
  1982.         GotoPos(curr->x, curr->y);
  1983.     }
  1984. }
  1985.  
  1986. static void LineFeed(out_mode)
  1987. int out_mode;
  1988. {
  1989.   /* out_mode: 0=no-output lf, 1=lf, 2=cr+lf */
  1990.   if (out_mode == 2)
  1991.     curr->x = 0;
  1992.   if (curr->y != curr->bot)        /* Don't scroll */
  1993.     {
  1994.       if (curr->y < rows-1)
  1995.     curr->y++;
  1996.       if (out_mode && display)
  1997.     GotoPos(curr->x, curr->y);
  1998.       return;
  1999.     }
  2000.   ScrollUpMap(1);
  2001.   if (curr->autoaka > 1)
  2002.     curr->autoaka--;
  2003.   if (out_mode && display)
  2004.     {
  2005.       ScrollRegion(curr->top, curr->bot, 1);
  2006.       GotoPos(curr->x, curr->y);
  2007.     }
  2008. }
  2009.  
  2010. static void ReverseLineFeed()
  2011. {
  2012.   if (curr->y == curr->top)
  2013.     {
  2014.       ScrollDownMap(1);
  2015.       if (!display)
  2016.     return;
  2017.       ScrollRegion(curr->top, curr->bot, -1);
  2018.       GotoPos(curr->x, curr->y);
  2019.     }
  2020.   else if (curr->y > 0)
  2021.     CursorUp(1);
  2022. }
  2023.  
  2024. static void InsertAChar(c)
  2025. int c;
  2026. {
  2027.   register int y = curr->y, x = curr->x;
  2028.  
  2029.   if (x == cols)
  2030.     x--;
  2031.   bcopy(curr->image[y], OldImage, cols);
  2032.   bcopy(curr->attr[y], OldAttr, cols);
  2033.   bcopy(curr->font[y], OldFont, cols);
  2034.   bcopy(curr->image[y] + x, curr->image[y] + x + 1, cols - x - 1);
  2035.   bcopy(curr->attr[y] + x, curr->attr[y] + x + 1, cols - x - 1);
  2036.   bcopy(curr->font[y] + x, curr->font[y] + x + 1, cols - x - 1);
  2037.   SetChar(c);
  2038.   if (!display)
  2039.     return;
  2040.   if (CIC || IC || IM)
  2041.     {
  2042.       InsertMode(curr->insert);
  2043.       INSERTCHAR(c);
  2044.       if (y == screenbot)
  2045.     lp_missing = 0;
  2046.     }
  2047.   else
  2048.     {
  2049.       RedisplayLine(OldImage, OldAttr, OldFont, y, x, cols - 1);
  2050.       GotoPos(++x, y);
  2051.     }
  2052. }
  2053.  
  2054. static void InsertChar(n)
  2055. int n;
  2056. {
  2057.   register int i, y = curr->y, x = curr->x;
  2058.  
  2059.   if (n <= 0)
  2060.     return;
  2061.   /*
  2062.    * The termcap manual states that only one of IM and IC is
  2063.    * to be defined unless the terminal needs both sequences.
  2064.    * We don't like this because we think that there may be cases
  2065.    * where it is preferable to send IC instead of IM/EI.
  2066.    * The hack is to ignore the IC sequence if we are already
  2067.    * in insert mode, so that programs which follow the termcap
  2068.    * guidelines still work. (I don't believe that there are
  2069.    * terminals which need IC in the insert mode. Why switch to
  2070.    * insert mode if you must send IC before every character ???)
  2071.    */
  2072.   if (curr->insert)
  2073.     return;
  2074.   if (x == cols)
  2075.     --x;
  2076.   bcopy(curr->image[y], OldImage, cols);
  2077.   bcopy(curr->attr[y], OldAttr, cols);
  2078.   bcopy(curr->font[y], OldFont, cols);
  2079.   if (n > cols - x)
  2080.     n = cols - x;
  2081.   bcopy(curr->image[y] + x, curr->image[y] + x + n, cols - x - n);
  2082.   bcopy(curr->attr[y] + x, curr->attr[y] + x + n, cols - x - n);
  2083.   bcopy(curr->font[y] + x, curr->font[y] + x + n, cols - x - n);
  2084.   ClearInLine(0, y, x, x + n - 1);
  2085.   if (!display)
  2086.     return;
  2087.   if (IC || CIC || IM)
  2088.     {
  2089.       if (y == screenbot)
  2090.     lp_missing = 0;
  2091.       if (!insert)
  2092.     {
  2093.       if (n == 1 && IC)
  2094.         {
  2095.           PutStr(IC);
  2096.           return;
  2097.             }
  2098.       if (CIC)
  2099.         {
  2100.           CPutStr(CIC, n);
  2101.           return;
  2102.             }
  2103.     }
  2104.       InsertMode(1);
  2105.       for (i = n; i--; )
  2106.     INSERTCHAR(' ');
  2107.       GotoPos(x, y);
  2108.     }
  2109.   else
  2110.     {
  2111.       RedisplayLine(OldImage, OldAttr, OldFont, y, x, cols - 1);
  2112.       GotoPos(x, y);
  2113.     }
  2114. }
  2115.  
  2116. static void DeleteChar(n)
  2117. int n;
  2118. {
  2119.   register int i, y = curr->y, x = curr->x;
  2120.  
  2121.   if (x == cols)
  2122.     --x;
  2123.   bcopy(curr->image[y], OldImage, cols);
  2124.   bcopy(curr->attr[y], OldAttr, cols);
  2125.   bcopy(curr->font[y], OldFont, cols);
  2126.   if (n > cols - x)
  2127.     n = cols - x;
  2128.   bcopy(curr->image[y] + x + n, curr->image[y] + x, cols - x - n);
  2129.   bcopy(curr->attr[y] + x + n, curr->attr[y] + x, cols - x - n);
  2130.   bcopy(curr->font[y] + x + n, curr->font[y] + x, cols - x - n);
  2131.   ClearInLine(0, y, cols - n, cols - 1);
  2132.   if (!display)
  2133.     return;
  2134.   if (CDC && !(n == 1 && DC))
  2135.     {
  2136.       CPutStr(CDC, n);
  2137.       if (lp_missing && y == screenbot)
  2138.     {
  2139.       FixLP(cols - 1 - n, y);
  2140.           GotoPos(x, y);
  2141.     }
  2142.     }
  2143.   else if (DC)
  2144.     {
  2145.       for (i = n; i; i--)
  2146.     PutStr(DC);
  2147.       if (lp_missing && y == screenbot)
  2148.     {
  2149.       FixLP(cols - 1 - n, y);
  2150.           GotoPos(x, y);
  2151.     }
  2152.     }
  2153.   else
  2154.     {
  2155.       RedisplayLine(OldImage, OldAttr, OldFont, y, x, cols - 1);
  2156.       GotoPos(x, y);
  2157.     }
  2158. }
  2159.  
  2160. static void DeleteLine(n)
  2161. int n;
  2162. {
  2163.   register int old = curr->top;
  2164.   
  2165.   if (curr->y < curr->top || curr->y > curr->bot)
  2166.     return;
  2167.   if (n > curr->bot - curr->y + 1)
  2168.     n = curr->bot - curr->y + 1;
  2169.   curr->top = curr->y;
  2170.   ScrollUpMap(n);
  2171.   curr->top = old;
  2172.   if (!display)
  2173.     return;
  2174.   ScrollRegion(curr->y, curr->bot, n);
  2175.   GotoPos(curr->x, curr->y);
  2176. }
  2177.  
  2178. static void InsertLine(n)
  2179. int n;
  2180. {
  2181.   register int old = curr->top;
  2182.  
  2183.   if (curr->y < curr->top || curr->y > curr->bot)
  2184.     return;
  2185.   if (n > curr->bot - curr->y + 1)
  2186.     n = curr->bot - curr->y + 1;
  2187.   curr->top = curr->y;
  2188.   ScrollDownMap(n);
  2189.   curr->top = old;
  2190.   if (!display)
  2191.     return;
  2192.   ScrollRegion(curr->y, curr->bot, -n);
  2193.   GotoPos(curr->x, curr->y);
  2194. }
  2195.  
  2196. void
  2197. ScrollRegion(ys, ye, n)
  2198. int ys, ye, n;
  2199. {
  2200.   int i;
  2201.   int up;
  2202.   int oldtop, oldbot;
  2203.   int alok, dlok, aldlfaster;
  2204.   int missy = 0;
  2205.  
  2206.   if (n == 0)
  2207.     return;
  2208.   if (ys == 0 && ye == screenheight-1 && 
  2209.       (n >= screenheight || -n >= screenheight))
  2210.     {
  2211.       PutStr(CL);
  2212.       screeny = screenx = 0;
  2213.       lp_missing = 0;
  2214.       return;
  2215.     }
  2216.  
  2217.   if (lp_missing)
  2218.     {
  2219.       if (screenbot>ye || screenbot<ys)
  2220.     missy = screenbot;
  2221.       else
  2222.     {
  2223.       missy = screenbot - n;
  2224.           if (missy>ye || missy<ys)
  2225.         lp_missing = 0;
  2226.     }
  2227.     }
  2228.  
  2229.   up = 1;
  2230.   if (n < 0)
  2231.     {
  2232.       up = 0;
  2233.       n = -n;
  2234.     }
  2235.   if (n >= ye-ys+1)
  2236.     n = ye-ys+1;
  2237.  
  2238.   oldtop = screentop;
  2239.   oldbot = screenbot;
  2240.   if (screenbot != ye)
  2241.     ChangeScrollRegion(ys, ye);
  2242.   alok = (AL || CAL || (ye == screenbot &&  up));
  2243.   dlok = (DL || CDL || (ye == screenbot && !up));
  2244.   if (screentop != ys && !(alok && dlok))
  2245.     ChangeScrollRegion(ys, ye);
  2246.  
  2247.   if (lp_missing && 
  2248.       (oldbot != screenbot ||
  2249.        oldbot == screenbot && up && screentop == ys && screenbot == ye))
  2250.     {
  2251.       /* Can't use FixLP */
  2252.       GotoPos(screenwidth-1, oldbot);
  2253.       SaveSetAttr(curr->attr[missy][screenwidth-1], curr->font[missy][screenwidth-1]);
  2254.       PUTCHAR(curr->image[missy][screenwidth-1]);
  2255.       RestoreAttr();
  2256.       lp_missing = 0;
  2257.       if (oldbot == screenbot)        /* have scrolled */
  2258.     {
  2259.       if (--n == 0)
  2260.         {
  2261.           ChangeScrollRegion(oldtop, oldbot);
  2262.           return;
  2263.         }
  2264.     }
  2265.     }
  2266.  
  2267.   aldlfaster = (n > 1 && ye == screenbot && (up && CDL || !up && CAL));
  2268.  
  2269.   if ((up || SR) && screentop == ys && screenbot == ye && !aldlfaster)
  2270.     {
  2271.       if (up)
  2272.     {
  2273.       GotoPos(0, ye);
  2274.       while (n-- > 0)
  2275.         PutStr(NL); /* was SF, I think NL is faster */
  2276.     }
  2277.       else
  2278.     {
  2279.       GotoPos(0, ys);
  2280.       while (n-- > 0)
  2281.         PutStr(SR);
  2282.     }
  2283.     }
  2284.   else if (alok && dlok)
  2285.     {
  2286.       if (up || ye != screenbot)
  2287.     {
  2288.           GotoPos(0, up ? ys : ye+1-n);
  2289.           if (CDL && !(n == 1 && DL))
  2290.         CPutStr(CDL, n);
  2291.       else
  2292.         for(i=n; i--; )
  2293.           PutStr(DL);
  2294.     }
  2295.       if (!up || ye != screenbot)
  2296.     {
  2297.           GotoPos(0, up ? ye+1-n : ys);
  2298.           if (CAL && !(n == 1 && AL))
  2299.         CPutStr(CAL, n);
  2300.       else
  2301.         for(i=n; i--; )
  2302.           PutStr(AL);
  2303.     }
  2304.     }
  2305.   else
  2306.     {
  2307.       Redisplay();
  2308.       return;
  2309.     }
  2310.   if (lp_missing && missy != screenbot)
  2311.     FixLP(screenwidth-1, missy);
  2312.   ChangeScrollRegion(oldtop, oldbot);
  2313.   if (lp_missing && missy != screenbot)
  2314.     FixLP(screenwidth-1, missy);
  2315. }
  2316.  
  2317. static void ScrollUpMap(n)
  2318. int n;
  2319. {
  2320.   char tmp[256 * sizeof(char *)];
  2321.   register int ii, i, cnt1, cnt2;
  2322.   register char **ppi, **ppa, **ppf;
  2323.  
  2324.   i = curr->top + n;
  2325.   cnt1 = n * sizeof(char *);
  2326.   cnt2 = (curr->bot - i + 1) * sizeof(char *);
  2327.   ppi = curr->image + i;
  2328.   ppa = curr->attr + i;
  2329.   ppf = curr->font + i;
  2330.   for(ii = curr->top; ii < i; ii++)
  2331.      AddLineToHist(curr, &curr->image[ii], &curr->attr[ii], &curr->font[ii]);
  2332.   for (i = n; i; --i)
  2333.     {
  2334.       bclear(*--ppi, cols);
  2335.       bzero(*--ppa, cols);
  2336.       bzero(*--ppf, cols);
  2337.     }
  2338.   Scroll((char *) ppi, cnt1, cnt2, tmp);
  2339.   Scroll((char *) ppa, cnt1, cnt2, tmp);
  2340.   Scroll((char *) ppf, cnt1, cnt2, tmp);
  2341. }
  2342.  
  2343. static void ScrollDownMap(n)
  2344. int n;
  2345. {
  2346.   char tmp[256 * sizeof(char *)];
  2347.   register int i, cnt1, cnt2;
  2348.   register char **ppi, **ppa, **ppf;
  2349.  
  2350.   i = curr->top;
  2351.   cnt1 = (curr->bot - i - n + 1) * sizeof(char *);
  2352.   cnt2 = n * sizeof(char *);
  2353.   Scroll((char *) (ppi = curr->image + i), cnt1, cnt2, tmp);
  2354.   Scroll((char *) (ppa = curr->attr + i), cnt1, cnt2, tmp);
  2355.   Scroll((char *) (ppf = curr->font + i), cnt1, cnt2, tmp);
  2356.   for (i = n; i; --i)
  2357.     {
  2358.       bclear(*ppi++, cols);
  2359.       bzero(*ppa++, cols);
  2360.       bzero(*ppf++, cols);
  2361.     }
  2362. }
  2363.  
  2364. static void Scroll(cp, cnt1, cnt2, tmp)
  2365. char *cp, *tmp;
  2366. int cnt1, cnt2;
  2367. {
  2368.   if (!cnt1 || !cnt2)
  2369.     return;
  2370.   if (cnt1 <= cnt2)
  2371.     {
  2372.       bcopy(cp, tmp, cnt1);
  2373.       bcopy(cp + cnt1, cp, cnt2);
  2374.       bcopy(tmp, cp + cnt2, cnt1);
  2375.     }
  2376.   else
  2377.     {
  2378.       bcopy(cp + cnt1, tmp, cnt2);
  2379.       bcopy(cp, cp + cnt2, cnt1);
  2380.       bcopy(tmp, cp, cnt2);
  2381.     }
  2382. }
  2383.  
  2384. static void ForwardTab()
  2385. {
  2386.   register int x = curr->x;
  2387.  
  2388.   if (x == cols)
  2389.     {
  2390.       LineFeed(2);
  2391.       x = 0;
  2392.     }
  2393.   if (curr->tabs[x] && x < cols - 1)
  2394.     x++;
  2395.   while (x < cols - 1 && !curr->tabs[x])
  2396.     x++;
  2397.   GotoPos(x, curr->y);
  2398.   curr->x = x;
  2399. }
  2400.  
  2401. static void BackwardTab()
  2402. {
  2403.   register int x = curr->x;
  2404.  
  2405.   if (curr->tabs[x] && x > 0)
  2406.     x--;
  2407.   while (x > 0 && !curr->tabs[x])
  2408.     x--;
  2409.   GotoPos(x, curr->y);
  2410.   curr->x = x;
  2411. }
  2412.  
  2413. static void ClearScreen()
  2414. {
  2415.   register int i;
  2416.   register char **ppi = curr->image, **ppa = curr->attr, **ppf = curr->font;
  2417.  
  2418.   for (i = 0; i < rows; ++i)
  2419.     {
  2420.       AddLineToHist(curr, ppi, ppa, ppf);
  2421.       bclear(*ppi++, cols);
  2422.       bzero(*ppa++, cols);
  2423.       bzero(*ppf++, cols);
  2424.     }
  2425.   if (display)
  2426.     {
  2427.       PutStr(CL);
  2428.       screenx = screeny = 0;
  2429.       lp_missing = 0;
  2430.     }
  2431. }
  2432.  
  2433. static void ClearFromBOS()
  2434. {
  2435.   register int n, y = curr->y, x = curr->x;
  2436.  
  2437.   for (n = 0; n < y; ++n)
  2438.     ClearInLine(1, n, 0, cols - 1);
  2439.   ClearInLine(1, y, 0, x);
  2440.   GotoPos(x, y);
  2441.   RestoreAttr();
  2442. }
  2443.  
  2444. static void ClearToEOS()
  2445. {
  2446.   register int n, y = curr->y, x = curr->x;
  2447.  
  2448.   if (!y && !x)
  2449.     {
  2450.       ClearScreen();
  2451.       return;
  2452.     }
  2453.   if (display && CD)
  2454.     {
  2455.       PutStr(CD);
  2456.       lp_missing = 0;
  2457.     }
  2458.   ClearInLine(!CD, y, x, cols - 1);
  2459.   for (n = y + 1; n < rows; n++)
  2460.     ClearInLine(!CD, n, 0, cols - 1);
  2461.   GotoPos(x, y);
  2462.   RestoreAttr();
  2463. }
  2464.  
  2465. static void ClearLine()
  2466. {
  2467.   register int y = curr->y, x = curr->x;
  2468.  
  2469.   ClearInLine(1, y, 0, cols - 1);
  2470.   GotoPos(x, y);
  2471.   RestoreAttr();
  2472. }
  2473.  
  2474. static void ClearToEOL()
  2475. {
  2476.   register int y = curr->y, x = curr->x;
  2477.  
  2478.   ClearInLine(1, y, x, cols - 1);
  2479.   GotoPos(x, y);
  2480.   RestoreAttr();
  2481. }
  2482.  
  2483. static void ClearFromBOL()
  2484. {
  2485.   register int y = curr->y, x = curr->x;
  2486.  
  2487.   ClearInLine(1, y, 0, x);
  2488.   GotoPos(x, y);
  2489.   RestoreAttr();
  2490. }
  2491.  
  2492. static void ClearInLine(displ, y, x1, x2)
  2493. int displ, y, x1, x2;
  2494. {
  2495.   register int n;
  2496.  
  2497.   if (x1 == cols)
  2498.     x1--;
  2499.   if (x2 == cols)
  2500.     x2--;
  2501.   if ((n = x2 - x1 + 1) != 0)
  2502.     {
  2503.       if (displ && display)
  2504.     {
  2505.       if (x2 == cols - 1 && CE)
  2506.         {
  2507.           GotoPos(x1, y);
  2508.           PutStr(CE);
  2509.           if (y == screenbot)
  2510.         lp_missing = 0;
  2511.         }
  2512.       else
  2513.         DisplayLine(curr->image[y], curr->attr[y], curr->font[y],
  2514.             blank, null, null, y, x1, x2);
  2515.     }
  2516.       if (curr)
  2517.     {
  2518.           bclear(curr->image[y] + x1, n);
  2519.           bzero(curr->attr[y] + x1, n);
  2520.           bzero(curr->font[y] + x1, n);
  2521.     }
  2522.     }
  2523. }
  2524.  
  2525. static void CursorRight(n)
  2526. register int n;
  2527. {
  2528.   register int x = curr->x;
  2529.  
  2530.   if (x == cols)
  2531.     {
  2532.       LineFeed(2);
  2533.       x = 0;
  2534.     }
  2535.   if ((curr->x += n) >= cols)
  2536.     curr->x = cols - 1;
  2537.   GotoPos(curr->x, curr->y);
  2538. }
  2539.  
  2540. static void CursorUp(n)
  2541. register int n;
  2542. {
  2543.   if (curr->y < curr->top)        /* if above scrolling rgn, */
  2544.     {
  2545.       if ((curr->y -= n) < 0)        /* ignore it's limits      */
  2546.          curr->y = 0;
  2547.     }
  2548.   else
  2549.     if ((curr->y -= n) < curr->top)
  2550.       curr->y = curr->top;
  2551.   GotoPos(curr->x, curr->y);
  2552. }
  2553.  
  2554. static void CursorDown(n)
  2555. register int n;
  2556. {
  2557.   if (curr->y > curr->bot)        /* if below scrolling rgn, */
  2558.     {
  2559.       if ((curr->y += n) > rows - 1)    /* ignore it's limits      */
  2560.         curr->y = rows - 1;
  2561.     }
  2562.   else
  2563.     if ((curr->y += n) > curr->bot)
  2564.       curr->y = curr->bot;
  2565.   GotoPos(curr->x, curr->y);
  2566. }
  2567.  
  2568. static void CursorLeft(n)
  2569. register int n;
  2570. {
  2571.   if ((curr->x -= n) < 0)
  2572.     curr->x = 0;
  2573.   GotoPos(curr->x, curr->y);
  2574. }
  2575.  
  2576. static void ASetMode(on)
  2577. int on;
  2578. {
  2579.   register int i;
  2580.  
  2581.   for (i = 0; i < curr->NumArgs; ++i)
  2582.     {
  2583.       switch (curr->args[i])
  2584.     {
  2585.     case 4:
  2586.       curr->insert = on;
  2587.       InsertMode(on);
  2588.       break;
  2589.     }
  2590.     }
  2591. }
  2592.  
  2593. static void SelectRendition()
  2594. {
  2595.   register int i = 0, a = curr->LocalAttr;
  2596.  
  2597.   do
  2598.     {
  2599.       switch (curr->args[i])
  2600.     {
  2601.     case 0:
  2602.       a = 0;
  2603.       break;
  2604.     case 1:
  2605.       a |= A_BD;
  2606.       break;
  2607.     case 2:
  2608.       a |= A_DI;
  2609.       break;
  2610.     case 3:
  2611.       a |= A_SO;
  2612.       break;
  2613.     case 4:
  2614.       a |= A_US;
  2615.       break;
  2616.     case 5:
  2617.       a |= A_BL;
  2618.       break;
  2619.     case 7:
  2620.       a |= A_RV;
  2621.       break;
  2622.     case 22:
  2623.       a &= ~(A_BD | A_SO | A_DI);
  2624.       break;
  2625.     case 23:
  2626.       a &= ~A_SO;
  2627.       break;
  2628.     case 24:
  2629.       a &= ~A_US;
  2630.       break;
  2631.     case 25:
  2632.       a &= ~A_BL;
  2633.       break;
  2634.     case 27:
  2635.       a &= ~A_RV;
  2636.       break;
  2637.     }
  2638.     } while (++i < curr->NumArgs);
  2639.   NewRendition(curr->LocalAttr = a);
  2640. }
  2641.  
  2642. void
  2643. NewRendition(new)
  2644. register int new;
  2645. {
  2646.   register int i, old = GlobalAttr;
  2647.  
  2648.   if (!display || old == new)
  2649.     return;
  2650.   GlobalAttr = new;
  2651.   for (i = 1; i <= A_MAX; i <<= 1)
  2652.     {
  2653.       if ((old & i) && !(new & i))
  2654.     {
  2655.       PutStr(UE);
  2656.       PutStr(SE);
  2657.       PutStr(ME);
  2658.       if (new & A_DI)
  2659.         PutStr(attrtab[ATTR_DI]);
  2660.       if (new & A_US)
  2661.         PutStr(attrtab[ATTR_US]);
  2662.       if (new & A_BD)
  2663.         PutStr(attrtab[ATTR_BD]);
  2664.       if (new & A_RV)
  2665.         PutStr(attrtab[ATTR_RV]);
  2666.       if (new & A_SO)
  2667.         PutStr(attrtab[ATTR_SO]);
  2668.       if (new & A_BL)
  2669.         PutStr(attrtab[ATTR_BL]);
  2670.       return;
  2671.     }
  2672.     }
  2673.   if ((new & A_DI) && !(old & A_DI))
  2674.     PutStr(attrtab[ATTR_DI]);
  2675.   if ((new & A_US) && !(old & A_US))
  2676.     PutStr(attrtab[ATTR_US]);
  2677.   if ((new & A_BD) && !(old & A_BD))
  2678.     PutStr(attrtab[ATTR_BD]);
  2679.   if ((new & A_RV) && !(old & A_RV))
  2680.     PutStr(attrtab[ATTR_RV]);
  2681.   if ((new & A_SO) && !(old & A_SO))
  2682.     PutStr(attrtab[ATTR_SO]);
  2683.   if ((new & A_BL) && !(old & A_BL))
  2684.     PutStr(attrtab[ATTR_BL]);
  2685. }
  2686.  
  2687. void
  2688. SaveSetAttr(newattr, newcharset)
  2689. int newattr, newcharset;
  2690. {
  2691.   NewRendition(newattr);
  2692.   NewCharset(newcharset);
  2693. }
  2694.  
  2695. void
  2696. RestoreAttr()
  2697. {
  2698.   NewRendition(curr->LocalAttr);
  2699.   NewCharset(curr->charsets[curr->LocalCharset]);
  2700. }
  2701.  
  2702. static void FillWithEs()
  2703. {
  2704.   register int i;
  2705.   register char *p, *ep;
  2706.  
  2707.   curr->y = curr->x = 0;
  2708.   for (i = 0; i < rows; ++i)
  2709.     {
  2710.       bzero(curr->attr[i], cols);
  2711.       bzero(curr->font[i], cols);
  2712.       p = curr->image[i];
  2713.       ep = p + cols;
  2714.       while (p < ep)
  2715.     *p++ = 'E';
  2716.     }
  2717.   if (display)
  2718.     Redisplay();
  2719. }
  2720.  
  2721. void Redisplay()
  2722. {
  2723.   register int i;
  2724.  
  2725.   PutStr(CL);
  2726.   screenx = screeny = 0;
  2727.   lp_missing = 0;
  2728.   for (i = 0; i < rows; ++i)
  2729.     {
  2730.       if (in_ovl)
  2731.     (*ovl_RedisplayLine)(i, 0, cols - 1, 1);
  2732.       else
  2733.         DisplayLine(blank, null, null, curr->image[i], curr->attr[i],
  2734.             curr->font[i], i, 0, cols - 1);
  2735.     }
  2736.   if (!in_ovl)
  2737.     {
  2738.       GotoPos(curr->x, curr->y);
  2739.       NewRendition(curr->LocalAttr);
  2740.       NewCharset(curr->charsets[curr->LocalCharset]);
  2741.     }
  2742. }
  2743.  
  2744. void
  2745. DisplayLine(os, oa, of, s, as, fs, y, from, to)
  2746. int from, to, y;
  2747. register char *os, *oa, *of, *s, *as, *fs;
  2748. {
  2749.   register int x;
  2750.   int last2flag = 0, delete_lp = 0;
  2751.  
  2752.   if (!LP && y == screenbot && to == cols - 1)
  2753.     if (lp_missing
  2754.     || s[to] != os[to] || as[to] != oa[to] || of[to] != fs[to])
  2755.       {
  2756.     if ((IC || IM) && (from < to || !in_ovl))
  2757.       {
  2758.         if ((to -= 2) < from - 1)
  2759.           from--;
  2760.         last2flag = 1;
  2761.         lp_missing = 0;
  2762.       }
  2763.     else
  2764.       {
  2765.         to--;
  2766.         delete_lp = (CE || DC || CDC);
  2767.         lp_missing = (s[to] != ' ' || as[to] || fs[to]);
  2768.       }
  2769.       }
  2770.     else
  2771.       to--;
  2772.   for (x = from; x <= to; ++x)
  2773.     {
  2774.       if (s[x] == os[x] && as[x] == oa[x] && of[x] == fs[x])
  2775.     continue;
  2776.       GotoPos(x, y);
  2777.       NewRendition(as[x]);
  2778.       NewCharset(fs[x]);
  2779.       PUTCHAR(s[x]);
  2780.     }
  2781.   if (last2flag)
  2782.     {
  2783.       GotoPos(x, y);
  2784.       NewRendition(as[x + 1]);
  2785.       NewCharset(fs[x + 1]);
  2786.       PUTCHAR(s[x + 1]);
  2787.       GotoPos(x, y);
  2788.       NewRendition(as[x]);
  2789.       NewCharset(fs[x]);
  2790.       INSERTCHAR(s[x]);
  2791.     }
  2792.   else if (delete_lp)
  2793.     {
  2794.       if (DC)
  2795.     PutStr(DC);
  2796.       else if (CDC)
  2797.     CPutStr(CDC, 1);
  2798.       else if (CE)
  2799.     PutStr(CE);
  2800.     }
  2801. }
  2802.  
  2803. void
  2804. RefreshLine(y, from, to)
  2805. int y, from, to;
  2806. {
  2807.   char *oi = null;
  2808.  
  2809.   if (CE && to == screenwidth-1)
  2810.     {
  2811.       GotoPos(from, y);
  2812.       PutStr(CE);
  2813.       oi = blank;
  2814.     }
  2815.   if (in_ovl)
  2816.     (*ovl_RedisplayLine)(y, from, to, (oi == blank));
  2817.   else
  2818.     DisplayLine(oi, null, null, curr->image[y], curr->attr[y],
  2819.                 curr->font[y], y, from, to);
  2820. }
  2821.  
  2822. static void RedisplayLine(os, oa, of, y, from, to)
  2823. int from, to, y;
  2824. char *os, *oa, *of;
  2825. {
  2826.   DisplayLine(os, oa, of, curr->image[y], curr->attr[y],
  2827.           curr->font[y], y, from, to);
  2828.   NewRendition(curr->LocalAttr);
  2829.   NewCharset(curr->charsets[curr->LocalCharset]);
  2830. }
  2831.  
  2832. void
  2833. FixLP(x2, y2)
  2834. register int x2, y2;
  2835. {
  2836.   register struct win *p = curr;
  2837.  
  2838.   GotoPos(x2, y2);
  2839.   SaveSetAttr(p->attr[y2][x2], p->font[y2][x2]);
  2840.   PUTCHAR(p->image[y2][x2]);
  2841.   RestoreAttr();
  2842.   lp_missing = 0;
  2843. }
  2844.  
  2845. void
  2846. CheckLP(n_ch)
  2847. char n_ch;
  2848. {
  2849.   register int y = screenbot, x = cols - 1;
  2850.   register char n_at, n_fo, o_ch, o_at, o_fo;
  2851.  
  2852.   o_ch = curr->image[y][x];
  2853.   o_at = curr->attr[y][x];
  2854.   o_fo = curr->font[y][x];
  2855.  
  2856.   n_at = curr->LocalAttr;
  2857.   n_fo = curr->charsets[curr->LocalCharset];
  2858.  
  2859.   lp_missing = 0;
  2860.   if (n_ch == o_ch && n_at == o_at && n_fo == o_fo)
  2861.     {
  2862.       return;
  2863.     }
  2864.   if (n_ch != ' ' || n_at || n_fo)
  2865.     lp_missing = 1;
  2866.   if (o_ch != ' ' || o_at || o_fo)
  2867.     {
  2868.       if (DC)
  2869.     PutStr(DC);
  2870.       else if (CDC)
  2871.     CPutStr(CDC, 1);
  2872.       else if (CE)
  2873.     PutStr(CE);
  2874.       else
  2875.     lp_missing = 1;
  2876.     }
  2877. }
  2878.  
  2879. static void FindAKA()
  2880. {
  2881.   register char *cp, *line, ch;
  2882.   register struct win *wp = curr;
  2883.   register int len = strlen(wp->cmd);
  2884.   int y;
  2885.  
  2886.   y = (wp->autoaka > 0 && wp->autoaka <= wp->height) ? wp->autoaka - 1 : wp->y;
  2887.   cols = wp->width;
  2888.  try_line:
  2889.   cp = line = wp->image[y];
  2890.   if (wp->autoaka > 0 && (ch = *wp->cmd) != '\0')
  2891.     {
  2892.       for (;;)
  2893.     {
  2894.       if ((cp = index(cp, ch)) != NULL
  2895.           && !strncmp(cp, wp->cmd, len))
  2896.         break;
  2897.       if (!cp || ++cp - line >= cols - len)
  2898.         {
  2899.           if (++y == wp->autoaka && y < rows)
  2900.         goto try_line;
  2901.           return;
  2902.         }
  2903.     }
  2904.       cp += len;
  2905.     }
  2906.   for (len = cols - (cp - line); len && *cp == ' '; len--, cp++)
  2907.     ;
  2908.   if (len)
  2909.     {
  2910.       if (wp->autoaka > 0 && (*cp == '!' || *cp == '%' || *cp == '^'))
  2911.     wp->autoaka = -1;
  2912.       else
  2913.     wp->autoaka = 0;
  2914.       line = wp->cmd + wp->akapos;
  2915.       while (len && *cp != ' ')
  2916.     {
  2917.       if ((*line++ = *cp++) == '/')
  2918.         line = wp->cmd + wp->akapos;
  2919.       len--;
  2920.     }
  2921.       *line = '\0';
  2922.     }
  2923.   else
  2924.     wp->autoaka = 0;
  2925. }
  2926.  
  2927.  
  2928. /* We dont use HS status line with Input.
  2929.  * If we would use it, then we should check e_tgetflag("es") if
  2930.  * we are allowed to use esc sequences there.
  2931.  * For now, we hope that Goto(,,STATLINE,0) brings us in the bottom
  2932.  * line. jw.
  2933.  */
  2934.  
  2935. static char inpbuf[101];
  2936. static int inplen;
  2937. static int inpmaxlen;
  2938. static char *inpstring;
  2939. static int inpstringlen;
  2940. static void (*inpfinfunc)();
  2941.  
  2942. void
  2943. Input(istr, len, finfunc)
  2944. char *istr;
  2945. int len;
  2946. void (*finfunc)();
  2947. {
  2948.   int maxlen;
  2949.  
  2950.   inpstring = istr;
  2951.   inpstringlen = strlen(istr);
  2952.   if (len > 100)
  2953.     len = 100;
  2954.   maxlen = screenwidth - inpstringlen;
  2955.   if (!LP && STATLINE == screenbot)
  2956.     maxlen--;
  2957.   if (len > maxlen)
  2958.     len = maxlen;
  2959.   if (len < 2)
  2960.     {
  2961.       Msg(0, "Width too small");
  2962.       return;
  2963.     }
  2964.   inpmaxlen = len;
  2965.   inpfinfunc = finfunc;
  2966.   InitOverlayPage(process_inp_input, inpRedisplayLine, (int (*)())0, 1);
  2967.   inplen = 0;
  2968.   GotoPos(0, STATLINE);
  2969.   if (CE)
  2970.     PutStr(CE);
  2971.   else
  2972.     {
  2973.       DisplayLine(curr->image[screeny], curr->attr[screeny],
  2974.           curr->font[screeny],
  2975.           blank, null, null, screeny, 0, cols - 1);
  2976.     }
  2977.   inpRedisplayLine(STATLINE, 0, inpstringlen - 1, 0);
  2978.   GotoPos(inpstringlen, STATLINE);
  2979. }
  2980.  
  2981. static void
  2982. process_inp_input(ppbuf, plen)
  2983. char **ppbuf;
  2984. int *plen;
  2985. {
  2986.   int len, x;
  2987.   char *pbuf;
  2988.   char ch;
  2989.  
  2990.   if (ppbuf == 0)
  2991.     {
  2992.       AbortInp();
  2993.       return;
  2994.     }
  2995.   x = inpstringlen+inplen;
  2996.   len = *plen;
  2997.   pbuf = *ppbuf;
  2998.   while (len)
  2999.     {
  3000.       ch = *pbuf++;
  3001.       len--;
  3002.       if (ch >= ' ' && ch <= '~' && inplen < inpmaxlen)
  3003.     {
  3004.       inpbuf[inplen++] = ch;
  3005.         GotoPos(x, STATLINE);
  3006.       SaveSetAttr(A_SO, ASCII);
  3007.       PUTCHAR(ch);
  3008.       x++;
  3009.     }
  3010.       else if ((ch == '\b' || ch == 0177) && inplen > 0)
  3011.     {
  3012.       inplen--;
  3013.       x--;
  3014.         GotoPos(x, STATLINE);
  3015.       SaveSetAttr(0, ASCII);
  3016.       PUTCHAR(' ');
  3017.         GotoPos(x, STATLINE);
  3018.     }
  3019.       else if (ch == '\004' || ch == '\003' || ch == '\000' || ch == '\n' || ch == '\r')
  3020.     {
  3021.           if (ch != '\n' && ch != '\r')
  3022.         inplen = 0;
  3023.       inpbuf[inplen] = 0;
  3024.           AbortInp(); /* redisplays... */
  3025.           (*inpfinfunc)(inpbuf, inplen);
  3026.       break;
  3027.     }
  3028.     }
  3029.   *ppbuf = pbuf;
  3030.   *plen = len;
  3031. }
  3032.  
  3033. static void
  3034. AbortInp()
  3035. {
  3036.   in_ovl = 0;    /* So we can use RefreshLine() */
  3037.   RefreshLine(STATLINE, 0, screenwidth-1);
  3038.   ExitOverlayPage();
  3039. }
  3040.  
  3041. static void
  3042. inpRedisplayLine(y, xs, xe, isblank)
  3043. int y, xs, xe, isblank;
  3044. {
  3045.   int q, r, s, l, v;
  3046.  
  3047.   if (y != STATLINE)
  3048.     return;
  3049.   inpbuf[inplen] = 0;
  3050.   GotoPos(xs,y);
  3051.   q = xs;
  3052.   v = xe - xs + 1;
  3053.   s = 0;
  3054.   r = inpstringlen;
  3055.   if (v > 0 && q < r)
  3056.     {
  3057.       SaveSetAttr(A_SO, ASCII);
  3058.       l = v;
  3059.       if (l > r-q)
  3060.     l = r-q;
  3061.       printf("%-*.*s", l, l, inpstring + q - s);
  3062.       q += l;
  3063.       v -= l;
  3064.     }
  3065.   s = r;
  3066.   r += inplen;
  3067.   if (v > 0 && q < r)
  3068.     {
  3069.       SaveSetAttr(A_SO, ASCII);
  3070.       l = v;
  3071.       if (l > r-q)
  3072.     l = r-q;
  3073.       printf("%-*.*s", l, l, inpbuf + q - s);
  3074.       q += l;
  3075.       v -= l;
  3076.     }
  3077.   s = r;
  3078.   r = screenwidth;
  3079.   if (!isblank && v > 0 && q < r)
  3080.     {
  3081.       SaveSetAttr(0, ASCII);
  3082.       l = v;
  3083.       if (l > r-q)
  3084.     l = r-q;
  3085.       printf("%-*.*s", l, l, "");
  3086.       q += l;
  3087.     }
  3088.   SetLastPos(q, y);
  3089. }
  3090.  
  3091. static void
  3092. AKAfin(buf, len)
  3093. char *buf;
  3094. int len;
  3095. {
  3096.   if (len)
  3097.     {
  3098.       strcpy(curr->cmd + curr->akapos, buf);
  3099.     }
  3100. }
  3101.  
  3102. void
  3103. InputAKA()
  3104. {
  3105.   void Input(), AKAfin();
  3106.  
  3107.   Input("Set window's a.k.a. to: ", 20, AKAfin);
  3108. }
  3109.  
  3110. static void
  3111. Colonfin(buf, len)
  3112. char *buf;
  3113. int len;
  3114. {
  3115.   if (len)
  3116.     RcLine(buf);
  3117. }
  3118.  
  3119. void
  3120. InputColon()
  3121. {
  3122.   void Input(), Colonfin();
  3123.  
  3124.   Input(":", 100, Colonfin);
  3125. }
  3126.  
  3127. void
  3128. MakeBlankLine(p, n)
  3129. register char *p;
  3130. register int n;
  3131. {
  3132.   while (n--)
  3133.     *p++ = ' ';
  3134. }
  3135.  
  3136. void
  3137. MakeStatus(msg)
  3138. char *msg;
  3139. {
  3140.   register char *s, *t;
  3141.   register int max, ti;
  3142.  
  3143.   SetCurr(fore);
  3144.   display = 1;
  3145.   if (!(max = HS))
  3146.     {
  3147.       max = !LP ? cols - 1 : cols;
  3148.     }
  3149.   if (status)
  3150.     {
  3151.       if (!BellDisplayed)
  3152.     {
  3153.       ti = time((time_t *) 0) - TimeDisplayed;
  3154.       if (ti < MsgMinWait)
  3155.         sleep(MsgMinWait - ti);
  3156.     }
  3157.       RemoveStatus();
  3158.     }
  3159.   for (s = t = msg; *s && t - msg < max; ++s)
  3160.     if (*s == BELL)
  3161.       PutStr(BL);
  3162.     else if (*s >= ' ' && *s <= '~')
  3163.       *t++ = *s;
  3164.   *t = '\0';
  3165.   if (t > msg)
  3166.     {
  3167.       strncpy(LastMsg, msg, maxwidth);
  3168.       status = 1;
  3169.       status_lastx = screenx;
  3170.       status_lasty = screeny;
  3171.       StatLen = t - msg;
  3172.       if (!HS)
  3173.     {
  3174.       GotoPos(0, STATLINE);
  3175.           SaveSetAttr(A_SO, ASCII);
  3176.       InsertMode(0);
  3177.       printf("%s", msg);
  3178.           screenx = -1;
  3179.     }
  3180.       else
  3181.     {
  3182.       debug("HS:");
  3183.           SaveSetAttr(A_SO, ASCII);
  3184.       InsertMode(0);
  3185.       CPutStr(TS, 0);
  3186.       printf("%s", msg);
  3187.       PutStr(FS);
  3188.     }
  3189.       (void) fflush(stdout);
  3190.       (void) time(&TimeDisplayed);
  3191.     }
  3192. }
  3193.  
  3194. void
  3195. RemoveStatus()
  3196. {
  3197.   if (!status)
  3198.     return;
  3199.   status = 0;
  3200.   BellDisplayed = 0;
  3201.   SetCurr(fore);
  3202.   display = 1;
  3203.   if (!HS)
  3204.     {
  3205.       GotoPos(0, STATLINE);
  3206.       if (in_ovl)
  3207.     (*ovl_RedisplayLine)(STATLINE, 0, StatLen - 1, 0);
  3208.       else
  3209.     RedisplayLine(null, null, null, STATLINE, 0, StatLen - 1);
  3210.       GotoPos(status_lastx, status_lasty);
  3211.     }
  3212.   else
  3213.     {
  3214.       PutStr(DS);
  3215.     }
  3216. }
  3217.  
  3218. void
  3219. ClearDisplay()
  3220. {
  3221.   PutStr(CL);
  3222.   screeny = screenx = 0;
  3223.   fflush(stdout);
  3224. }
  3225.  
  3226. static void SetCurr(wp)
  3227. struct win *wp;
  3228. {
  3229.   curr = wp;
  3230.   cols = curr->width;
  3231.   rows = curr->height;
  3232.   display = curr->active;
  3233. }
  3234.  
  3235. void
  3236. InitOverlayPage(pro, red, rewrite, blockfore)
  3237. void (*pro)();
  3238. void (*red)();
  3239. int (*rewrite)();
  3240. int blockfore;
  3241. {
  3242.   RemoveStatus();
  3243.   SetOvlCurr();
  3244.   ChangeScrollRegion(0, screenheight - 1);
  3245.   ovl_process = pro;
  3246.   ovl_RedisplayLine = red;
  3247.   ovl_Rewrite = rewrite;
  3248.   ovl_blockfore = blockfore;
  3249.   curr->active = 0;
  3250.   in_ovl = 1;
  3251. }
  3252.  
  3253. void
  3254. ExitOverlayPage()
  3255. {
  3256.   GotoPos(curr->x, curr->y);
  3257.   RestoreAttr();
  3258.   ChangeScrollRegion(curr->top, curr->bot);
  3259.   curr->active = 1;
  3260.   in_ovl = 0;
  3261. }
  3262.  
  3263. void
  3264. SetOvlCurr()
  3265. {
  3266.   SetCurr(fore);
  3267.   SaveSetAttr(0, ASCII);
  3268.   InsertMode(0);
  3269.   display = 1;
  3270. }
  3271.  
  3272. void
  3273. SetLastPos(x,y)
  3274. int x,y;
  3275. {
  3276.   screenx = x;
  3277.   screeny = y;
  3278. }
  3279.  
  3280. void
  3281. WSresize(width, height)
  3282. int width, height;
  3283. {
  3284.   debug2("(display=%d:WSresize says:'%s'\n", display, tgoto(WS, width, height));
  3285.   PutStr(tgoto(WS, width, height));
  3286. }
  3287.  
  3288. void
  3289. ChangeScrollRegion(top, bot)
  3290. int top, bot;
  3291. {
  3292.   if (display == 0)
  3293.     return;
  3294.   if (CS == 0)
  3295.     {
  3296.       screentop = 0;
  3297.       screenbot = screenheight - 1;
  3298.       return;
  3299.     }
  3300.   if (top == screentop && bot == screenbot)
  3301.     return;
  3302.   debug2("ChangeScrollRegion: (%d - %d)\n", top, bot);
  3303.   PutStr(tgoto(CS, bot, top));
  3304.   screentop = top;
  3305.   screenbot = bot;
  3306.   screeny = screenx = -1;        /* Just in case... */
  3307. }
  3308.  
  3309.  
  3310. void AddLineToHist(wp, pi, pa, pf)
  3311. struct win *wp;
  3312. char **pi, **pa, **pf;
  3313. {
  3314.   register char *q;
  3315.  
  3316.   if (wp->histheight == 0)
  3317.     return;
  3318.   q = *pi; *pi = wp->ihist[wp->histidx]; wp->ihist[wp->histidx] = q;
  3319.   q = *pa; *pa = wp->ahist[wp->histidx]; wp->ahist[wp->histidx] = q;
  3320.   q = *pf; *pf = wp->fhist[wp->histidx]; wp->fhist[wp->histidx] = q;
  3321.   if (++wp->histidx >= wp->histheight)
  3322.     wp->histidx = 0;
  3323. }
  3324.  
  3325.  
  3326. /*
  3327.  *
  3328.  *  Termcap routines that use our extra_incap
  3329.  *
  3330.  */
  3331.  
  3332. /* findcap:
  3333.  *   cap = capability we are looking for
  3334.  *   tepp = pointer to bufferpointer
  3335.  *   n = size of buffer (0 = infinity)
  3336.  */
  3337.  
  3338. char *
  3339. findcap(cap, tepp, n)
  3340. char *cap;
  3341. char **tepp;
  3342. int n;
  3343. {
  3344.   char *tep;
  3345.   char c, *p, *cp;
  3346.   int mode;    /* mode: 0=LIT  1=^  2=\x  3,4,5=\nnn */
  3347.   int num = 0, capl;
  3348.  
  3349.   if (!extra_incap)
  3350.     return (0);
  3351.   tep = *tepp;
  3352.   capl = strlen(cap);
  3353.   cp = 0;
  3354.   mode = 0;
  3355.   for (p = extra_incap; *p; )
  3356.     {
  3357.       if (strncmp(p, cap, capl) == 0)
  3358.     {
  3359.       p+=capl;
  3360.       c = *p;
  3361.       if (c && c != ':' && c != '@')
  3362.         p++;
  3363.       if (c == 0 || c == '@' || c == '=' || c == ':' || c == '#')
  3364.         cp = tep;
  3365.     }
  3366.       while (c = *p)
  3367.     {
  3368.       p++;
  3369.       if (mode == 0)
  3370.         {
  3371.           if (c == ':')
  3372.             break;
  3373.           if (c == '^')
  3374.         mode = 1;
  3375.           if (c == '\\')
  3376.         mode = 2;
  3377.         }
  3378.       else if (mode == 1)
  3379.         {
  3380.           c = c & 0x1f;
  3381.           mode = 0;
  3382.         }
  3383.       else if (mode == 2)
  3384.         {
  3385.           switch(c)
  3386.         {
  3387.         case '0':
  3388.         case '1':
  3389.         case '2':
  3390.         case '3':
  3391.         case '4':
  3392.         case '5':
  3393.         case '6':
  3394.         case '7':
  3395.         case '8':
  3396.         case '9':
  3397.           mode = 3;
  3398.           num = 0;
  3399.           break;
  3400.         case 'E':
  3401.           c = 27;
  3402.           break;
  3403.         case 'n':
  3404.           c = '\n';
  3405.           break;
  3406.         case 'r':
  3407.           c = '\r';
  3408.           break;
  3409.         case 't':
  3410.           c = '\t';
  3411.           break;
  3412.         case 'b':
  3413.           c = '\b';
  3414.           break;
  3415.         case 'f':
  3416.           c = '\f';
  3417.           break;
  3418.         }
  3419.           if (mode == 2)
  3420.         mode = 0;
  3421.         }
  3422.       if (mode > 2)
  3423.         {
  3424.           num = num * 8 + (c - '0');
  3425.           if (mode++ == 5 || (*p < '0' || *p > '9'))
  3426.         {
  3427.           c = num;
  3428.           mode = 0;
  3429.         }
  3430.         }
  3431.       if (mode)
  3432.         continue;
  3433.  
  3434.       if (cp && n != 1)
  3435.         {
  3436.           *cp++ = c;
  3437.           n--;
  3438.         }
  3439.     }
  3440.       if (cp)
  3441.     {
  3442.       *cp++ = 0;
  3443.       *tepp = cp;
  3444.       debug2("'%s' found in extra_incap -> %s\n", cap, tep);
  3445.       return(tep);
  3446.     }
  3447.     }
  3448.   return(0);
  3449. }
  3450.  
  3451. static char *
  3452. e_tgetstr(cap, tepp)
  3453. char *cap;
  3454. char **tepp;
  3455. {
  3456.   char *tep;
  3457.   if (tep = findcap(cap, tepp, 0))
  3458.     return((*tep == '@') ? 0 : tep);
  3459.   return (tgetstr(cap, tepp));
  3460. }
  3461.  
  3462. static int
  3463. e_tgetflag(cap)
  3464. char *cap;
  3465. {
  3466.   char buf[2], *bufp;
  3467.   char *tep;
  3468.   bufp = buf;
  3469.   if (tep = findcap(cap, &bufp, 2))
  3470.     return((*tep == '@') ? 0 : 1);
  3471.   return (tgetflag(cap));
  3472. }
  3473.  
  3474. static int
  3475. e_tgetnum(cap)
  3476. char *cap;
  3477. {
  3478.   char buf[20], *bufp;
  3479.   char *tep, c;
  3480.   int res, base = 10;
  3481.  
  3482.   bufp = buf;
  3483.   if (tep = findcap(cap, &bufp, 20))
  3484.     {
  3485.       c = *tep;
  3486.       if (c == '@')
  3487.     return(-1);
  3488.       if (c == '0')
  3489.     base = 8;
  3490.       res = 0;
  3491.       while ((c = *tep++) >= '0' && c <= '9')
  3492.     res = res * base + (c - '0');
  3493.       return(res);
  3494.     }
  3495.   return (tgetnum(cap));
  3496. }
  3497.