xref: /original-bsd/usr.bin/window/ww.h (revision 98298d89)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Wang at The University of California, Berkeley.
7  *
8  * %sccs.include.redist.c%
9  *
10  *	@(#)ww.h	8.1 (Berkeley) 06/06/93
11  */
12 
13 #ifdef OLD_TTY
14 #include <sgtty.h>
15 #else
16 #include <termios.h>
17 #endif
18 #include <setjmp.h>
19 #include <machine/endian.h>
20 
21 #define NWW	30		/* maximum number of windows */
22 
23 	/* a rectangle */
24 struct ww_dim {
25 	int nr;			/* number of rows */
26 	int nc;			/* number of columns */
27 	int t, b;		/* top, bottom */
28 	int l, r;		/* left, right */
29 };
30 
31 	/* a coordinate */
32 struct ww_pos {
33 	int r;			/* row */
34 	int c;			/* column */
35 };
36 
37 	/* the window structure */
38 struct ww {
39 		/* general flags and states */
40 	char ww_state;		/* state of window */
41 	char ww_oflags;		/* wwopen flags */
42 
43 		/* information for overlap */
44 	struct ww *ww_forw;	/* doubly linked list, for overlapping info */
45 	struct ww *ww_back;
46 	char ww_index;		/* the window index, for wwindex[] */
47 	char ww_order;		/* the overlapping order */
48 
49 		/* sizes and positions */
50 	struct ww_dim ww_w;	/* window size and pos */
51 	struct ww_dim ww_b;	/* buffer size and pos */
52 	struct ww_dim ww_i;	/* the part inside the screen */
53 	struct ww_pos ww_cur;	/* the cursor position, relative to ww_w */
54 
55 		/* arrays */
56 	char **ww_win;		/* the window */
57 	union ww_char **ww_buf;	/* the buffer */
58 	char **ww_fmap;		/* map for frame and box windows */
59 	short *ww_nvis;		/* how many ww_buf chars are visible per row */
60 
61 		/* information for wwwrite() and company */
62 	char ww_wstate;		/* state for outputting characters */
63 	char ww_modes;		/* current display modes */
64 	char ww_insert;		/* insert mode */
65 	char ww_mapnl;		/* map \n to \r\n */
66 	char ww_noupdate;	/* don't do updates in wwwrite() */
67 	char ww_unctrl;		/* expand control characters */
68 	char ww_nointr;		/* wwwrite() not interruptable */
69 	char ww_hascursor;	/* has fake cursor */
70 
71 		/* things for the window process and io */
72 	char ww_ispty;		/* ww_pty is really a pty, not socket pair */
73 	char ww_stopped;	/* output stopped */
74 	int ww_pty;		/* file descriptor of pty or socket pair */
75 	int ww_socket;		/* other end of socket pair */
76 	int ww_pid;		/* pid of process, if WWS_HASPROC true */
77 	char ww_ttyname[11];	/* "/dev/ttyp?" */
78 	char *ww_ob;		/* output buffer */
79 	char *ww_obe;		/* end of ww_ob */
80 	char *ww_obp;		/* current read position in ww_ob */
81 	char *ww_obq;		/* current write position in ww_ob */
82 
83 		/* things for the user, they really don't belong here */
84 	char ww_id;		/* the user window id */
85 	char ww_center;		/* center the label */
86 	char ww_hasframe;	/* frame it */
87 	char ww_keepopen;	/* keep it open after the process dies */
88 	char *ww_label;		/* the user supplied label */
89 	struct ww_dim ww_alt;	/* alternate position and size */
90 };
91 
92 	/* state of a tty */
93 struct ww_tty {
94 #ifdef OLD_TTY
95 	struct sgttyb ww_sgttyb;
96 	struct tchars ww_tchars;
97 	struct ltchars ww_ltchars;
98 	int ww_lmode;
99 	int ww_ldisc;
100 #else
101 	struct termios ww_termios;
102 #endif
103 	int ww_fflags;
104 };
105 
106 union ww_char {
107 	short c_w;		/* as a word */
108 	struct {
109 #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
110 		char C_c;	/* the character part */
111 		char C_m;	/* the mode part */
112 #endif
113 #if BYTE_ORDER == BIG_ENDIAN
114 		char C_m;	/* the mode part */
115 		char C_c;	/* the character part */
116 #endif
117 	} c_un;
118 };
119 #define c_c c_un.C_c
120 #define c_m c_un.C_m
121 
122 	/* for display update */
123 struct ww_update {
124 	int best_gain;
125 	int best_col;
126 	int gain;
127 };
128 
129 	/* parts of ww_char */
130 #define WWC_CMASK	0x00ff
131 #define WWC_MMASK	0xff00
132 #define WWC_MSHIFT	8
133 
134 	/* c_m bits */
135 #define WWM_REV		0x01	/* reverse video */
136 #define WWM_BLK		0x02	/* blinking */
137 #define WWM_UL		0x04	/* underlined */
138 #define WWM_GRP		0x08	/* graphics */
139 #define WWM_DIM		0x10	/* half intensity */
140 #define WWM_USR		0x20	/* user specified mode */
141 #define WWM_GLS		0x40	/* window only, glass, i.e., transparent */
142 
143 	/* ww_state values */
144 #define WWS_INITIAL	0	/* just opened */
145 #define WWS_HASPROC	1	/* has process on pty */
146 #define WWS_DEAD	3	/* child died */
147 
148 	/* flags for ww_fmap */
149 #define WWF_U		0x01
150 #define WWF_R		0x02
151 #define WWF_D		0x04
152 #define WWF_L		0x08
153 #define WWF_MASK	(WWF_U|WWF_R|WWF_D|WWF_L)
154 #define WWF_LABEL	0x40
155 #define WWF_TOP		0x80
156 
157 	/* flags to wwopen() */
158 #define WWO_PTY		0x01		/* want pty */
159 #define WWO_SOCKET	0x02		/* want socket pair */
160 #define WWO_REVERSE	0x04		/* make it all reverse video */
161 #define WWO_GLASS	0x08		/* make it all glass */
162 #define WWO_FRAME	0x10		/* this is a frame window */
163 
164 	/* special ww_index value */
165 #define WWX_NOBODY	NWW
166 
167 	/* error codes */
168 #define WWE_NOERR	0
169 #define WWE_SYS		1		/* system error */
170 #define WWE_NOMEM	2		/* out of memory */
171 #define WWE_TOOMANY	3		/* too many windows */
172 #define WWE_NOPTY	4		/* no more ptys */
173 #define WWE_SIZE	5		/* bad window size */
174 #define WWE_BADTERM	6		/* bad terminal type */
175 #define WWE_CANTDO	7		/* dumb terminal */
176 
177 	/* wwtouched[] bits, there used to be more than one */
178 #define WWU_TOUCHED	0x01		/* touched */
179 
180 	/* the window structures */
181 struct ww wwhead;
182 struct ww *wwindex[NWW + 1];		/* last location is for wwnobody */
183 struct ww wwnobody;
184 
185 	/* tty things */
186 struct ww_tty wwoldtty;		/* the old (saved) terminal settings */
187 struct ww_tty wwnewtty;		/* the new (current) terminal settings */
188 struct ww_tty wwwintty;		/* the terminal settings for windows */
189 char *wwterm;			/* the terminal name */
190 char wwtermcap[1024];		/* place for the termcap */
191 
192 	/* generally useful variables */
193 int wwnrow, wwncol;		/* the screen size */
194 char wwavailmodes;		/* actually supported modes */
195 char wwcursormodes;		/* the modes for the fake cursor */
196 char wwwrap;			/* terminal has auto wrap around */
197 int wwdtablesize;		/* result of getdtablesize() call */
198 char **wwsmap;			/* the screen map */
199 union ww_char **wwos;		/* the old (current) screen */
200 union ww_char **wwns;		/* the new (desired) screen */
201 union ww_char **wwcs;		/* the checkpointed screen */
202 char *wwtouched;		/* wwns changed flags */
203 struct ww_update *wwupd;	/* for display update */
204 int wwospeed;			/* output baud rate, copied from wwoldtty */
205 int wwbaud;			/* wwospeed converted into actual number */
206 int wwcursorrow, wwcursorcol;	/* where we want the cursor to be */
207 int wwerrno;			/* error number */
208 
209 	/* statistics */
210 int wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc;
211 int wwnwwr, wwnwwra, wwnwwrc;
212 int wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc;
213 int wwnupdate, wwnupdline, wwnupdmiss;
214 int wwnupdscan, wwnupdclreol, wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline;
215 int wwnread, wwnreade, wwnreadz;
216 int wwnreadc, wwnreadack, wwnreadnack, wwnreadstat, wwnreadec;
217 int wwnwread, wwnwreade, wwnwreadz, wwnwreadd, wwnwreadc, wwnwreadp;
218 int wwnselect, wwnselecte, wwnselectz;
219 
220 	/* quicky macros */
221 #define wwsetcursor(r,c) (wwcursorrow = (r), wwcursorcol = (c))
222 #define wwcurtowin(w)	wwsetcursor((w)->ww_cur.r, (w)->ww_cur.c)
223 #define wwunbox(w)	wwunframe(w)
224 #define wwclreol(w,r,c)	wwclreol1((w), (r), (c), 0)
225 #define wwredrawwin(w)	wwredrawwin1((w), (w)->ww_i.t, (w)->ww_i.b, 0)
226 #define wwupdate()	wwupdate1(0, wwnrow);
227 
228 	/* things for handling input */
229 void wwrint();		/* interrupt handler */
230 struct ww *wwcurwin;	/* window to copy input into */
231 char *wwib;		/* input (keyboard) buffer */
232 char *wwibe;		/* wwib + sizeof buffer */
233 char *wwibp;		/* current read position in buffer */
234 char *wwibq;		/* current write position in buffer */
235 #define wwmaskc(c)	((c) & 0x7f)
236 #define wwgetc()	(wwibp < wwibq ? wwmaskc(*wwibp++) : -1)
237 #define wwpeekc()	(wwibp < wwibq ? wwmaskc(*wwibp) : -1)
238 #define wwungetc(c)	(wwibp > wwib ? *--wwibp = (c) : -1)
239 
240 	/* things for short circuiting wwiomux() */
241 char wwintr;		/* interrupting */
242 char wwsetjmp;		/* want a longjmp() from wwrint() and wwchild() */
243 jmp_buf wwjmpbuf;	/* jmpbuf for above */
244 #define wwinterrupt()	wwintr
245 #define wwsetintr()	do { wwintr = 1; if (wwsetjmp) longjmp(wwjmpbuf, 1); } \
246 			while (0)
247 #define wwclrintr()	(wwintr = 0)
248 
249 	/* checkpointing */
250 int wwdocheckpoint;
251 
252 	/* the window virtual terminal */
253 #define WWT_TERM	"window-v2"
254 #define WWT_TERMCAP	"WW|window-v2|window program version 2:\
255 	:am:bs:da:db:ms:pt:cr=^M:nl=^J:bl=^G:ta=^I:\
256 	:cm=\\EY%+ %+ :le=^H:nd=\\EC:up=\\EA:do=\\EB:ho=\\EH:\
257 	:cd=\\EJ:ce=\\EK:cl=\\EE:me=\\Er^?:"
258 #define WWT_REV		"se=\\ErA:so=\\EsA:mr=\\EsA:"
259 #define WWT_BLK		"BE=\\ErB:BS=\\EsB:mb=\\EsB:"
260 #define WWT_UL		"ue=\\ErD:us=\\EsD:"
261 #define WWT_GRP		"ae=\\ErH:as=\\EsH:"
262 #define WWT_DIM		"HE=\\ErP:HS=\\EsP:mh=\\EsP:"
263 #define WWT_USR		"XE=\\Er`:XS=\\Es`:"
264 #define WWT_ALDL	"al=\\EL:dl=\\EM:"
265 #define WWT_IMEI	"im=\\E@:ei=\\EO:ic=:mi:" /* XXX, ic for emacs bug */
266 #define WWT_IC		"ic=\\EP:"
267 #define WWT_DC		"dc=\\EN:"
268 char wwwintermcap[1024];	/* terminal-specific but window-independent
269 				   part of the window termcap */
270 #ifdef TERMINFO
271 	/* where to put the temporary terminfo directory */
272 char wwterminfopath[1024];
273 #endif
274 
275 	/* our functions */
276 struct ww *wwopen();
277 void wwchild();
278 void wwalarm();
279 void wwquit();
280 char **wwalloc();
281 char *wwerror();
282 
283 	/* c library functions */
284 char *malloc();
285 char *calloc();
286 char *getenv();
287 char *tgetstr();
288 char *rindex();
289 char *strcpy();
290 char *strcat();
291 
292 #undef MIN
293 #undef MAX
294 #define MIN(x, y)	((x) > (y) ? (y) : (x))
295 #define MAX(x, y)	((x) > (y) ? (x) : (y))
296