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