114407Sedward /* 2*62473Sbostic * Copyright (c) 1983, 1993 3*62473Sbostic * The Regents of the University of California. All rights reserved. 433514Sbostic * 542954Sbostic * This code is derived from software contributed to Berkeley by 642954Sbostic * Edward Wang at The University of California, Berkeley. 742954Sbostic * 842835Sbostic * %sccs.include.redist.c% 933514Sbostic * 10*62473Sbostic * @(#)ww.h 8.1 (Berkeley) 06/06/93 1114407Sedward */ 1213930Sedward 1345033Sedward #ifdef OLD_TTY 1413930Sedward #include <sgtty.h> 1542834Sedward #else 1642834Sedward #include <termios.h> 1742834Sedward #endif 1816112Sedward #include <setjmp.h> 1936257Sedward #include <machine/endian.h> 2013930Sedward 2115870Sedward #define NWW 30 /* maximum number of windows */ 2214407Sedward 2315726Sedward /* a rectangle */ 2414071Sedward struct ww_dim { 2514407Sedward int nr; /* number of rows */ 2614407Sedward int nc; /* number of columns */ 2714407Sedward int t, b; /* top, bottom */ 2814407Sedward int l, r; /* left, right */ 2914071Sedward }; 3014071Sedward 3115726Sedward /* a coordinate */ 3214407Sedward struct ww_pos { 3314407Sedward int r; /* row */ 3414407Sedward int c; /* column */ 3514407Sedward }; 3614407Sedward 3715726Sedward /* the window structure */ 3813930Sedward struct ww { 3918205Sedward /* general flags and states */ 4018205Sedward char ww_state; /* state of window */ 4118205Sedward char ww_oflags; /* wwopen flags */ 4218205Sedward 4316309Sedward /* information for overlap */ 4414407Sedward struct ww *ww_forw; /* doubly linked list, for overlapping info */ 4514407Sedward struct ww *ww_back; 4616309Sedward char ww_index; /* the window index, for wwindex[] */ 4714407Sedward char ww_order; /* the overlapping order */ 4814987Sedward 4914987Sedward /* sizes and positions */ 5014987Sedward struct ww_dim ww_w; /* window size and pos */ 5114987Sedward struct ww_dim ww_b; /* buffer size and pos */ 5214969Sedward struct ww_dim ww_i; /* the part inside the screen */ 5314407Sedward struct ww_pos ww_cur; /* the cursor position, relative to ww_w */ 5414987Sedward 5514987Sedward /* arrays */ 5614407Sedward char **ww_win; /* the window */ 5714407Sedward union ww_char **ww_buf; /* the buffer */ 5814772Sedward char **ww_fmap; /* map for frame and box windows */ 5914407Sedward short *ww_nvis; /* how many ww_buf chars are visible per row */ 6014987Sedward 6116309Sedward /* information for wwwrite() and company */ 6216309Sedward char ww_wstate; /* state for outputting characters */ 6316309Sedward char ww_modes; /* current display modes */ 6416309Sedward char ww_insert; /* insert mode */ 6516309Sedward char ww_mapnl; /* map \n to \r\n */ 6616309Sedward char ww_noupdate; /* don't do updates in wwwrite() */ 6716309Sedward char ww_unctrl; /* expand control characters */ 6816309Sedward char ww_nointr; /* wwwrite() not interruptable */ 6916309Sedward char ww_hascursor; /* has fake cursor */ 7016309Sedward 7116112Sedward /* things for the window process and io */ 7216313Sedward char ww_ispty; /* ww_pty is really a pty, not socket pair */ 7316313Sedward char ww_stopped; /* output stopped */ 7416313Sedward int ww_pty; /* file descriptor of pty or socket pair */ 7516313Sedward int ww_socket; /* other end of socket pair */ 7614407Sedward int ww_pid; /* pid of process, if WWS_HASPROC true */ 7714591Sedward char ww_ttyname[11]; /* "/dev/ttyp?" */ 7815870Sedward char *ww_ob; /* output buffer */ 7915870Sedward char *ww_obe; /* end of ww_ob */ 8016313Sedward char *ww_obp; /* current read position in ww_ob */ 8116313Sedward char *ww_obq; /* current write position in ww_ob */ 8214997Sedward 8314997Sedward /* things for the user, they really don't belong here */ 8418205Sedward char ww_id; /* the user window id */ 8516112Sedward char ww_center; /* center the label */ 8618205Sedward char ww_hasframe; /* frame it */ 8731139Sedward char ww_keepopen; /* keep it open after the process dies */ 8814180Sedward char *ww_label; /* the user supplied label */ 8918205Sedward struct ww_dim ww_alt; /* alternate position and size */ 9013962Sedward }; 9113962Sedward 9215726Sedward /* state of a tty */ 9313962Sedward struct ww_tty { 9445033Sedward #ifdef OLD_TTY 9513930Sedward struct sgttyb ww_sgttyb; 9613930Sedward struct tchars ww_tchars; 9713930Sedward struct ltchars ww_ltchars; 9813930Sedward int ww_lmode; 9913930Sedward int ww_ldisc; 10042834Sedward #else 10142834Sedward struct termios ww_termios; 10242834Sedward #endif 10316112Sedward int ww_fflags; 10413930Sedward }; 10513930Sedward 10614591Sedward union ww_char { 10714591Sedward short c_w; /* as a word */ 10814591Sedward struct { 10936257Sedward #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN 11014591Sedward char C_c; /* the character part */ 11114591Sedward char C_m; /* the mode part */ 11236257Sedward #endif 11336257Sedward #if BYTE_ORDER == BIG_ENDIAN 11415264Sedward char C_m; /* the mode part */ 11515264Sedward char C_c; /* the character part */ 11615264Sedward #endif 11714591Sedward } c_un; 11814591Sedward }; 11914591Sedward #define c_c c_un.C_c 12014591Sedward #define c_m c_un.C_m 12114591Sedward 12235330Sedward /* for display update */ 12335330Sedward struct ww_update { 12435330Sedward int best_gain; 12535330Sedward int best_col; 12635330Sedward int gain; 12735330Sedward }; 12835330Sedward 12914591Sedward /* parts of ww_char */ 13014591Sedward #define WWC_CMASK 0x00ff 13114591Sedward #define WWC_MMASK 0xff00 13214591Sedward #define WWC_MSHIFT 8 13314591Sedward 13414591Sedward /* c_m bits */ 13514591Sedward #define WWM_REV 0x01 /* reverse video */ 13614591Sedward #define WWM_BLK 0x02 /* blinking */ 13714591Sedward #define WWM_UL 0x04 /* underlined */ 13815733Sedward #define WWM_GRP 0x08 /* graphics */ 13929712Sedward #define WWM_DIM 0x10 /* half intensity */ 14029712Sedward #define WWM_USR 0x20 /* user specified mode */ 14129712Sedward #define WWM_GLS 0x40 /* window only, glass, i.e., transparent */ 14214591Sedward 14314591Sedward /* ww_state values */ 14414591Sedward #define WWS_INITIAL 0 /* just opened */ 14515726Sedward #define WWS_HASPROC 1 /* has process on pty */ 14614591Sedward #define WWS_DEAD 3 /* child died */ 14714591Sedward 14814772Sedward /* flags for ww_fmap */ 14914591Sedward #define WWF_U 0x01 15014591Sedward #define WWF_R 0x02 15114591Sedward #define WWF_D 0x04 15214591Sedward #define WWF_L 0x08 15314591Sedward #define WWF_MASK (WWF_U|WWF_R|WWF_D|WWF_L) 15414591Sedward #define WWF_LABEL 0x40 15514591Sedward #define WWF_TOP 0x80 15614591Sedward 15714772Sedward /* flags to wwopen() */ 15814772Sedward #define WWO_PTY 0x01 /* want pty */ 15916313Sedward #define WWO_SOCKET 0x02 /* want socket pair */ 16016313Sedward #define WWO_REVERSE 0x04 /* make it all reverse video */ 16116313Sedward #define WWO_GLASS 0x08 /* make it all glass */ 16216313Sedward #define WWO_FRAME 0x10 /* this is a frame window */ 16314772Sedward 16414591Sedward /* special ww_index value */ 16514591Sedward #define WWX_NOBODY NWW 16614591Sedward 16715652Sedward /* error codes */ 16814853Sedward #define WWE_NOERR 0 16914853Sedward #define WWE_SYS 1 /* system error */ 17014853Sedward #define WWE_NOMEM 2 /* out of memory */ 17114853Sedward #define WWE_TOOMANY 3 /* too many windows */ 17214853Sedward #define WWE_NOPTY 4 /* no more ptys */ 17314853Sedward #define WWE_SIZE 5 /* bad window size */ 17414853Sedward #define WWE_BADTERM 6 /* bad terminal type */ 17514853Sedward #define WWE_CANTDO 7 /* dumb terminal */ 17614853Sedward 17735330Sedward /* wwtouched[] bits, there used to be more than one */ 17815652Sedward #define WWU_TOUCHED 0x01 /* touched */ 17915652Sedward 18015870Sedward /* the window structures */ 18114407Sedward struct ww wwhead; 18214591Sedward struct ww *wwindex[NWW + 1]; /* last location is for wwnobody */ 18314407Sedward struct ww wwnobody; 18413975Sedward 18515870Sedward /* tty things */ 18614591Sedward struct ww_tty wwoldtty; /* the old (saved) terminal settings */ 18714591Sedward struct ww_tty wwnewtty; /* the new (current) terminal settings */ 18814591Sedward struct ww_tty wwwintty; /* the terminal settings for windows */ 18914591Sedward char *wwterm; /* the terminal name */ 19014591Sedward char wwtermcap[1024]; /* place for the termcap */ 19113930Sedward 19215870Sedward /* generally useful variables */ 19314407Sedward int wwnrow, wwncol; /* the screen size */ 19414685Sedward char wwavailmodes; /* actually supported modes */ 19514763Sedward char wwcursormodes; /* the modes for the fake cursor */ 19614696Sedward char wwwrap; /* terminal has auto wrap around */ 19714591Sedward int wwdtablesize; /* result of getdtablesize() call */ 19814407Sedward char **wwsmap; /* the screen map */ 19914591Sedward union ww_char **wwos; /* the old (current) screen */ 20014591Sedward union ww_char **wwns; /* the new (desired) screen */ 20156710Sedward union ww_char **wwcs; /* the checkpointed screen */ 20214662Sedward char *wwtouched; /* wwns changed flags */ 20335330Sedward struct ww_update *wwupd; /* for display update */ 20445033Sedward int wwospeed; /* output baud rate, copied from wwoldtty */ 20545033Sedward int wwbaud; /* wwospeed converted into actual number */ 20614591Sedward int wwcursorrow, wwcursorcol; /* where we want the cursor to be */ 20714853Sedward int wwerrno; /* error number */ 20813930Sedward 20914407Sedward /* statistics */ 21016112Sedward int wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc; 21116112Sedward int wwnwwr, wwnwwra, wwnwwrc; 21238750Sedward int wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc; 21335330Sedward int wwnupdate, wwnupdline, wwnupdmiss; 21435330Sedward int wwnupdscan, wwnupdclreol, wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline; 21556710Sedward int wwnread, wwnreade, wwnreadz; 21656710Sedward int wwnreadc, wwnreadack, wwnreadnack, wwnreadstat, wwnreadec; 21715870Sedward int wwnwread, wwnwreade, wwnwreadz, wwnwreadd, wwnwreadc, wwnwreadp; 21815870Sedward int wwnselect, wwnselecte, wwnselectz; 21913975Sedward 22014591Sedward /* quicky macros */ 22114407Sedward #define wwsetcursor(r,c) (wwcursorrow = (r), wwcursorcol = (c)) 22214987Sedward #define wwcurtowin(w) wwsetcursor((w)->ww_cur.r, (w)->ww_cur.c) 22314772Sedward #define wwunbox(w) wwunframe(w) 22414987Sedward #define wwclreol(w,r,c) wwclreol1((w), (r), (c), 0) 22514995Sedward #define wwredrawwin(w) wwredrawwin1((w), (w)->ww_i.t, (w)->ww_i.b, 0) 22616112Sedward #define wwupdate() wwupdate1(0, wwnrow); 22714407Sedward 22815870Sedward /* things for handling input */ 22946901Sbostic void wwrint(); /* interrupt handler */ 23016112Sedward struct ww *wwcurwin; /* window to copy input into */ 23115870Sedward char *wwib; /* input (keyboard) buffer */ 23215870Sedward char *wwibe; /* wwib + sizeof buffer */ 23316112Sedward char *wwibp; /* current read position in buffer */ 23416112Sedward char *wwibq; /* current write position in buffer */ 23555912Sedward #define wwmaskc(c) ((c) & 0x7f) 23655912Sedward #define wwgetc() (wwibp < wwibq ? wwmaskc(*wwibp++) : -1) 23755912Sedward #define wwpeekc() (wwibp < wwibq ? wwmaskc(*wwibp) : -1) 23816112Sedward #define wwungetc(c) (wwibp > wwib ? *--wwibp = (c) : -1) 23915870Sedward 24031443Sedward /* things for short circuiting wwiomux() */ 24131443Sedward char wwintr; /* interrupting */ 24231443Sedward char wwsetjmp; /* want a longjmp() from wwrint() and wwchild() */ 24331443Sedward jmp_buf wwjmpbuf; /* jmpbuf for above */ 24431443Sedward #define wwinterrupt() wwintr 24536801Sedward #define wwsetintr() do { wwintr = 1; if (wwsetjmp) longjmp(wwjmpbuf, 1); } \ 24636801Sedward while (0) 24731443Sedward #define wwclrintr() (wwintr = 0) 24831443Sedward 24956710Sedward /* checkpointing */ 25056710Sedward int wwdocheckpoint; 25156710Sedward 25214685Sedward /* the window virtual terminal */ 25333758Sedward #define WWT_TERM "window-v2" 25433758Sedward #define WWT_TERMCAP "WW|window-v2|window program version 2:\ 25534803Sedward :am:bs:da:db:ms:pt:cr=^M:nl=^J:bl=^G:ta=^I:\ 25634803Sedward :cm=\\EY%+ %+ :le=^H:nd=\\EC:up=\\EA:do=\\EB:ho=\\EH:\ 25734803Sedward :cd=\\EJ:ce=\\EK:cl=\\EE:me=\\Er^?:" 25829712Sedward #define WWT_REV "se=\\ErA:so=\\EsA:mr=\\EsA:" 25929712Sedward #define WWT_BLK "BE=\\ErB:BS=\\EsB:mb=\\EsB:" 26029712Sedward #define WWT_UL "ue=\\ErD:us=\\EsD:" 26129712Sedward #define WWT_GRP "ae=\\ErH:as=\\EsH:" 26229712Sedward #define WWT_DIM "HE=\\ErP:HS=\\EsP:mh=\\EsP:" 26329712Sedward #define WWT_USR "XE=\\Er`:XS=\\Es`:" 26434803Sedward #define WWT_ALDL "al=\\EL:dl=\\EM:" 26538751Sedward #define WWT_IMEI "im=\\E@:ei=\\EO:ic=:mi:" /* XXX, ic for emacs bug */ 26639338Sedward #define WWT_IC "ic=\\EP:" 26734803Sedward #define WWT_DC "dc=\\EN:" 26834803Sedward char wwwintermcap[1024]; /* terminal-specific but window-independent 26934803Sedward part of the window termcap */ 27055911Sedward #ifdef TERMINFO 27155911Sedward /* where to put the temporary terminfo directory */ 27255911Sedward char wwterminfopath[1024]; 27355911Sedward #endif 27414685Sedward 27514591Sedward /* our functions */ 27613975Sedward struct ww *wwopen(); 27745034Sedward void wwchild(); 27856710Sedward void wwalarm(); 27955911Sedward void wwquit(); 28014407Sedward char **wwalloc(); 28114853Sedward char *wwerror(); 28214407Sedward 28314591Sedward /* c library functions */ 28414407Sedward char *malloc(); 28514407Sedward char *calloc(); 28614407Sedward char *getenv(); 28714407Sedward char *tgetstr(); 28814407Sedward char *rindex(); 28914407Sedward char *strcpy(); 29014407Sedward char *strcat(); 29114407Sedward 29214407Sedward #undef MIN 29314407Sedward #undef MAX 29414407Sedward #define MIN(x, y) ((x) > (y) ? (y) : (x)) 29514407Sedward #define MAX(x, y) ((x) > (y) ? (x) : (y)) 296