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