1 /*	$OpenBSD: ww.h,v 1.9 2003/06/03 02:56:23 millert Exp $	*/
2 /*	$NetBSD: ww.h,v 1.9 1996/10/12 23:46:23 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1983, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Wang at The University of California, Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)ww.h	8.1 (Berkeley) 6/6/93
36  */
37 
38 #ifdef OLD_TTY
39 #include <sgtty.h>
40 #else
41 #include <termios.h>
42 #endif
43 #include <setjmp.h>
44 #include <sys/types.h>
45 
46 #define NWW	30		/* maximum number of windows */
47 
48 /* Macros to clear/set/test flags. */
49 #define	SET(t, f)	(t) |= (f)
50 #define	CLR(t, f)	(t) &= ~(f)
51 #define	ISSET(t, f)	((t) & (f))
52 
53 	/* a rectangle */
54 struct ww_dim {
55 	int nr;			/* number of rows */
56 	int nc;			/* number of columns */
57 	int t, b;		/* top, bottom */
58 	int l, r;		/* left, right */
59 };
60 
61 	/* a coordinate */
62 struct ww_pos {
63 	int r;			/* row */
64 	int c;			/* column */
65 };
66 
67 	/* the window structure */
68 struct ww {
69 	int ww_flags;
70 
71 		/* general flags and states */
72 	int ww_state;		/* state of window */
73 #define WWS_INITIAL	0	/* just opened */
74 #define WWS_HASPROC	1	/* has process on pty */
75 #define WWS_DEAD	3	/* child died */
76 #define	ww_oflags	ww_flags
77 #define WWO_REVERSE	0x0001	/* make it all reverse video */
78 #define WWO_GLASS	0x0002	/* make it all glass */
79 #define WWO_FRAME	0x0004	/* this is a frame window */
80 #define	WWO_ALLFLAGS	0x0007
81 
82 		/* information for overlap */
83 	struct ww *ww_forw;	/* doubly linked list, for overlapping info */
84 	struct ww *ww_back;
85 	unsigned char ww_index;	/* the window index, for wwindex[] */
86 #define WWX_NOBODY	NWW
87 	int ww_order;		/* the overlapping order */
88 
89 		/* sizes and positions */
90 	struct ww_dim ww_w;	/* window size and pos */
91 	struct ww_dim ww_b;	/* buffer size and pos */
92 	struct ww_dim ww_i;	/* the part inside the screen */
93 	struct ww_pos ww_cur;	/* the cursor position, relative to ww_w */
94 
95 		/* arrays */
96 	char **ww_win;		/* the window */
97 	union ww_char **ww_buf;	/* the buffer */
98 	char **ww_fmap;		/* map for frame and box windows */
99 	short *ww_nvis;		/* how many ww_buf chars are visible per row */
100 
101 		/* information for wwwrite() and company */
102 	int ww_wstate;		/* state for outputting characters */
103 	char ww_modes;		/* current display modes */
104 #define	ww_wflags	ww_flags
105 #define	WWW_INSERT	0x0008	/* insert mode */
106 #define	WWW_MAPNL	0x0010	/* map \n to \r\n */
107 #define	WWW_NOUPDATE	0x0020	/* don't do updates in wwwrite() */
108 #define	WWW_UNCTRL	0x0040	/* expand control characters */
109 #define	WWW_NOINTR	0x0080	/* wwwrite() not interruptable */
110 #define	WWW_HASCURSOR	0x0100	/* has fake cursor */
111 
112 		/* things for the window process and io */
113 	int ww_type;
114 #define	WWT_PTY		0	/* pty */
115 #define	WWT_SOCKET	1	/* socket pair */
116 #define	WWT_INTERNAL	2
117 #define	ww_pflags	ww_flags
118 #define	WWP_STOPPED	0x0200	/* output stopped */
119 	int ww_pty;		/* file descriptor of pty or socket pair */
120 	int ww_socket;		/* other end of socket pair */
121 	pid_t ww_pid;		/* pid of process, if WWS_HASPROC true */
122 	char ww_ttyname[11];	/* "/dev/ttyp?" */
123 	char *ww_ob;		/* output buffer */
124 	char *ww_obe;		/* end of ww_ob */
125 	char *ww_obp;		/* current read position in ww_ob */
126 	char *ww_obq;		/* current write position in ww_ob */
127 
128 		/* things for the user, they really don't belong here */
129 	int ww_id;		/* the user window id */
130 #define	ww_uflags	ww_flags
131 #define	WWU_CENTER	0x0400	/* center the label */
132 #define	WWU_HASFRAME	0x0800	/* frame it */
133 #define	WWU_KEEPOPEN	0x1000	/* keep it open after the process dies */
134 #define	WWU_ALLFLAGS	0x1c00
135 	char *ww_label;		/* the user supplied label */
136 	struct ww_dim ww_alt;	/* alternate position and size */
137 };
138 
139 	/* state of a tty */
140 struct ww_tty {
141 #ifdef OLD_TTY
142 	struct sgttyb ww_sgttyb;
143 	struct tchars ww_tchars;
144 	struct ltchars ww_ltchars;
145 	int ww_lmode;
146 	int ww_ldisc;
147 #else
148 	struct termios ww_termios;
149 #endif
150 };
151 
152 union ww_char {
153 	short c_w;		/* as a word */
154 	struct {
155 #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
156 		char C_c;	/* the character part */
157 		char C_m;	/* the mode part */
158 #endif
159 #if BYTE_ORDER == BIG_ENDIAN
160 		char C_m;	/* the mode part */
161 		char C_c;	/* the character part */
162 #endif
163 	} c_un;
164 };
165 #define c_c c_un.C_c
166 #define c_m c_un.C_m
167 
168 	/* for display update */
169 struct ww_update {
170 	int best_gain;
171 	int best_col;
172 	int gain;
173 };
174 
175 	/* parts of ww_char */
176 #define WWC_CMASK	0x00ff
177 #define WWC_MMASK	0xff00
178 #define WWC_MSHIFT	8
179 
180 	/* c_m bits */
181 #define WWM_REV		0x01	/* reverse video */
182 #define WWM_BLK		0x02	/* blinking */
183 #define WWM_UL		0x04	/* underlined */
184 #define WWM_GRP		0x08	/* graphics */
185 #define WWM_DIM		0x10	/* half intensity */
186 #define WWM_USR		0x20	/* user specified mode */
187 #define WWM_GLS		0x40	/* window only, glass, i.e., transparent */
188 
189 	/* flags for ww_fmap */
190 #define WWF_U		0x01
191 #define WWF_R		0x02
192 #define WWF_D		0x04
193 #define WWF_L		0x08
194 #define WWF_MASK	(WWF_U|WWF_R|WWF_D|WWF_L)
195 #define WWF_LABEL	0x40
196 #define WWF_TOP		0x80
197 
198 	/* error codes */
199 #define WWE_NOERR	0
200 #define WWE_SYS		1		/* system error */
201 #define WWE_NOMEM	2		/* out of memory */
202 #define WWE_TOOMANY	3		/* too many windows */
203 #define WWE_NOPTY	4		/* no more ptys */
204 #define WWE_SIZE	5		/* bad window size */
205 #define WWE_BADTERM	6		/* bad terminal type */
206 #define WWE_CANTDO	7		/* dumb terminal */
207 
208 	/* wwtouched[] bits, there used to be more than one */
209 #define WWU_TOUCHED	0x01		/* touched */
210 
211 	/* the window structures */
212 struct ww wwhead;
213 struct ww *wwindex[NWW + 1];		/* last location is for wwnobody */
214 struct ww wwnobody;
215 
216 	/* tty things */
217 struct ww_tty wwoldtty;		/* the old (saved) terminal settings */
218 struct ww_tty wwnewtty;		/* the new (current) terminal settings */
219 struct ww_tty wwwintty;		/* the terminal settings for windows */
220 char *wwterm;			/* the terminal name */
221 char wwtermcap[1024];		/* place for the termcap */
222 
223 	/* generally useful variables */
224 int wwnrow, wwncol;		/* the screen size */
225 char wwavailmodes;		/* actually supported modes */
226 char wwcursormodes;		/* the modes for the fake cursor */
227 char wwwrap;			/* terminal has auto wrap around */
228 int wwdtablesize;		/* result of getdtablesize() call */
229 unsigned char **wwsmap;		/* the screen map */
230 union ww_char **wwos;		/* the old (current) screen */
231 union ww_char **wwns;		/* the new (desired) screen */
232 union ww_char **wwcs;		/* the checkpointed screen */
233 char *wwtouched;		/* wwns changed flags */
234 struct ww_update *wwupd;	/* for display update */
235 int wwospeed;			/* output baud rate, copied from wwoldtty */
236 int wwbaud;			/* wwospeed converted into actual number */
237 int wwcursorrow, wwcursorcol;	/* where we want the cursor to be */
238 int wwerrno;			/* error number */
239 
240 	/* statistics */
241 int wwnflush, wwnwr, wwnwre, wwnwrz, wwnwrc;
242 int wwnwwr, wwnwwra, wwnwwrc;
243 int wwntokdef, wwntokuse, wwntokbad, wwntoksave, wwntokc;
244 int wwnupdate, wwnupdline, wwnupdmiss;
245 int wwnupdscan, wwnupdclreol, wwnupdclreos, wwnupdclreosmiss, wwnupdclreosline;
246 int wwnread, wwnreade, wwnreadz;
247 int wwnreadc, wwnreadack, wwnreadnack, wwnreadstat, wwnreadec;
248 int wwnwread, wwnwreade, wwnwreadz, wwnwreadd, wwnwreadc, wwnwreadp;
249 int wwnselect, wwnselecte, wwnselectz;
250 
251 	/* quicky macros */
252 #define wwsetcursor(r,c) (wwcursorrow = (r), wwcursorcol = (c))
253 #define wwcurtowin(w)	wwsetcursor((w)->ww_cur.r, (w)->ww_cur.c)
254 #define wwunbox(w)	wwunframe(w)
255 #define wwclreol(w,r,c)	wwclreol1((w), (r), (c), 0)
256 #define wwredrawwin(w)	wwredrawwin1((w), (w)->ww_i.t, (w)->ww_i.b, 0)
257 #define wwupdate()	wwupdate1(0, wwnrow);
258 
259 	/* things for handling input */
260 void wwrint();		/* interrupt handler */
261 struct ww *wwcurwin;	/* window to copy input into */
262 char *wwib;		/* input (keyboard) buffer */
263 char *wwibe;		/* wwib + sizeof buffer */
264 char *wwibp;		/* current read position in buffer */
265 char *wwibq;		/* current write position in buffer */
266 #define wwmaskc(c)	((c) & 0x7f)
267 #define wwgetc()	(wwibp < wwibq ? wwmaskc(*wwibp++) : -1)
268 #define wwpeekc()	(wwibp < wwibq ? wwmaskc(*wwibp) : -1)
269 #define wwungetc(c)	(wwibp > wwib ? *--wwibp = (c) : -1)
270 
271 	/* things for short circuiting wwiomux() */
272 char wwintr;		/* interrupting */
273 char wwsetjmp;		/* want a longjmp() from wwrint() and wwchild() */
274 jmp_buf wwjmpbuf;	/* jmpbuf for above */
275 #define wwinterrupt()	wwintr
276 #define wwsetintr()	do { wwintr = 1; if (wwsetjmp) longjmp(wwjmpbuf, 1); } \
277 			while (0)
278 #define wwclrintr()	(wwintr = 0)
279 
280 	/* checkpointing */
281 int wwdocheckpoint;
282 
283 	/* the window virtual terminal */
284 #define WWT_TERM	"window-v2"
285 #define WWT_TERMCAP	"window-v2|window program version 2:\
286 	:am:bs:da:db:ms:pt:cr=^M:nl=^J:bl=^G:ta=^I:\
287 	:cm=\\EY%+ %+ :le=^H:nd=\\EC:up=\\EA:do=\\EB:ho=\\EH:\
288 	:cd=\\EJ:ce=\\EK:cl=\\EE:me=\\Er^?:"
289 #define WWT_REV		"se=\\ErA:so=\\EsA:mr=\\EsA:"
290 #define WWT_BLK		"BE=\\ErB:BS=\\EsB:mb=\\EsB:"
291 #define WWT_UL		"ue=\\ErD:us=\\EsD:"
292 #define WWT_GRP		"ae=\\ErH:as=\\EsH:"
293 #define WWT_DIM		"HE=\\ErP:HS=\\EsP:mh=\\EsP:"
294 #define WWT_USR		"XE=\\Er`:XS=\\Es`:"
295 #define WWT_ALDL	"al=\\EL:dl=\\EM:"
296 #define WWT_IMEI	"im=\\E@:ei=\\EO:ic=:mi:" /* XXX, ic for emacs bug */
297 #define WWT_IC		"ic=\\EP:"
298 #define WWT_DC		"dc=\\EN:"
299 char wwwintermcap[1024];	/* terminal-specific but window-independent
300 				   part of the window termcap */
301 #ifdef TERMINFO
302 	/* where to put the temporary terminfo directory */
303 char wwterminfopath[1024];
304 #endif
305 
306 	/* our functions */
307 struct ww *wwopen();
308 void wwchild();
309 void wwalarm();
310 void wwquit();
311 char **wwalloc();
312 char *wwerror();
313 
314 #undef MIN
315 #undef MAX
316 #define MIN(x, y)	((x) > (y) ? (y) : (x))
317 #define MAX(x, y)	((x) > (y) ? (x) : (y))
318