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