]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/wwinit.c
This commit was generated by cvs2svn to compensate for changes in r69482,
[FreeBSD/FreeBSD.git] / usr.bin / window / wwinit.c
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
37 #ifndef lint
38 static char sccsid[] = "@(#)wwinit.c    8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "ww.h"
43 #include "tt.h"
44 #include <signal.h>
45 #include <fcntl.h>
46 #include <termcap.h>
47 #include "char.h"
48
49 wwinit()
50 {
51         register i, j;
52         char *kp;
53         int s;
54
55         wwdtablesize = getdtablesize();
56         wwhead.ww_forw = &wwhead;
57         wwhead.ww_back = &wwhead;
58
59         s = sigblock(sigmask(SIGIO) | sigmask(SIGCHLD) | sigmask(SIGALRM) |
60                 sigmask(SIGHUP) | sigmask(SIGTERM));
61         if (signal(SIGIO, wwrint) == BADSIG ||
62             signal(SIGCHLD, wwchild) == BADSIG ||
63             signal(SIGHUP, wwquit) == BADSIG ||
64             signal(SIGTERM, wwquit) == BADSIG ||
65             signal(SIGPIPE, SIG_IGN) == BADSIG) {
66                 wwerrno = WWE_SYS;
67                 return -1;
68         }
69
70         if (wwgettty(0, &wwoldtty) < 0)
71                 return -1;
72         wwwintty = wwoldtty;
73 #ifdef OLD_TTY
74         wwwintty.ww_sgttyb.sg_flags &= ~XTABS;
75         wwnewtty.ww_sgttyb = wwoldtty.ww_sgttyb;
76         wwnewtty.ww_sgttyb.sg_erase = -1;
77         wwnewtty.ww_sgttyb.sg_kill = -1;
78         wwnewtty.ww_sgttyb.sg_flags |= CBREAK;
79         wwnewtty.ww_sgttyb.sg_flags &= ~(ECHO|CRMOD);
80         wwnewtty.ww_tchars.t_intrc = -1;
81         wwnewtty.ww_tchars.t_quitc = -1;
82         wwnewtty.ww_tchars.t_startc = -1;
83         wwnewtty.ww_tchars.t_stopc = -1;
84         wwnewtty.ww_tchars.t_eofc = -1;
85         wwnewtty.ww_tchars.t_brkc = -1;
86         wwnewtty.ww_ltchars.t_suspc = -1;
87         wwnewtty.ww_ltchars.t_dsuspc = -1;
88         wwnewtty.ww_ltchars.t_rprntc = -1;
89         wwnewtty.ww_ltchars.t_flushc = -1;
90         wwnewtty.ww_ltchars.t_werasc = -1;
91         wwnewtty.ww_ltchars.t_lnextc = -1;
92         wwnewtty.ww_lmode = wwoldtty.ww_lmode | LLITOUT;
93         wwnewtty.ww_ldisc = wwoldtty.ww_ldisc;
94 #else
95 #ifndef OXTABS
96 #define OXTABS XTABS
97 #endif
98 #ifndef _POSIX_VDISABLE
99 #define _POSIX_VDISABLE -1
100 #endif
101         wwwintty.ww_termios.c_oflag &= ~OXTABS;
102         wwnewtty.ww_termios = wwoldtty.ww_termios;
103         wwnewtty.ww_termios.c_iflag &=
104                 ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXOFF | IMAXBEL);
105         wwnewtty.ww_termios.c_oflag = 0;
106         wwnewtty.ww_termios.c_cflag &= ~(CSIZE | PARENB);
107         wwnewtty.ww_termios.c_cflag |= CS8;
108         wwnewtty.ww_termios.c_lflag = 0;
109         for (i = 0; i < NCCS; i++)
110                 wwnewtty.ww_termios.c_cc[i] = _POSIX_VDISABLE;
111         wwnewtty.ww_termios.c_cc[VMIN] = 0;
112         wwnewtty.ww_termios.c_cc[VTIME] = 0;
113 #endif
114         wwnewtty.ww_fflags = wwoldtty.ww_fflags | FASYNC;
115         if (wwsettty(0, &wwnewtty) < 0)
116                 goto bad;
117
118         if ((wwterm = getenv("TERM")) == 0) {
119                 wwerrno = WWE_BADTERM;
120                 goto bad;
121         }
122         if (tgetent(wwtermcap, wwterm) != 1) {
123                 wwerrno = WWE_BADTERM;
124                 goto bad;
125         }
126 #ifdef OLD_TTY
127         ospeed = wwoldtty.ww_sgttyb.sg_ospeed;
128         switch (ospeed) {
129         default:
130         case B0:
131                 goto bad;
132         case B50:
133                 wwbaud = 50;
134                 break;
135         case B75:
136                 wwbaud = 75;
137                 break;
138         case B110:
139                 wwbaud = 110;
140                 break;
141         case B134:
142                 wwbaud = 134;
143                 break;
144         case B150:
145                 wwbaud = 150;
146                 break;
147         case B200:
148                 wwbaud = 200;
149                 break;
150         case B300:
151                 wwbaud = 300;
152                 break;
153         case B600:
154                 wwbaud = 600;
155                 break;
156         case B1200:
157                 wwbaud = 1200;
158                 break;
159         case B1800:
160                 wwbaud = 1800;
161                 break;
162         case B2400:
163                 wwbaud = 2400;
164                 break;
165         case B4800:
166                 wwbaud = 4800;
167                 break;
168         case B9600:
169                 wwbaud = 9600;
170                 break;
171 #ifdef B19200
172         case B19200:
173 #else
174         case EXTA:
175 #endif
176                 wwbaud = 19200;
177                 break;
178 #ifdef B38400
179         case B38400:
180 #else
181         case EXTB:
182 #endif
183                 wwbaud = 38400;
184                 break;
185 #ifdef B57600
186         case B57600:
187                 wwbaud = 57600;
188                 break;
189 #endif
190 #ifdef B115200
191         case B115200:
192                 wwbaud = 115200;
193                 break;
194 #endif
195         }
196 #else
197         if ((wwbaud = cfgetospeed(&wwoldtty.ww_termios)) == B0)
198                 goto bad;
199 #endif
200         wwospeed = ospeed;
201
202         if (xxinit() < 0)
203                 goto bad;
204         wwnrow = tt.tt_nrow;
205         wwncol = tt.tt_ncol;
206         wwavailmodes = tt.tt_availmodes;
207         wwwrap = tt.tt_wrap;
208
209         if (wwavailmodes & WWM_REV)
210                 wwcursormodes = WWM_REV | wwavailmodes & WWM_BLK;
211         else if (wwavailmodes & WWM_UL)
212                 wwcursormodes = WWM_UL;
213
214         if ((wwib = malloc((unsigned) 512)) == 0)
215                 goto bad;
216         wwibe = wwib + 512;
217         wwibq = wwibp = wwib;
218
219         if ((wwsmap = wwalloc(0, 0, wwnrow, wwncol, sizeof (char))) == 0)
220                 goto bad;
221         for (i = 0; i < wwnrow; i++)
222                 for (j = 0; j < wwncol; j++)
223                         wwsmap[i][j] = WWX_NOBODY;
224
225         wwos = (union ww_char **)
226                 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
227         if (wwos == 0)
228                 goto bad;
229         /* wwos is cleared in wwstart1() */
230         wwns = (union ww_char **)
231                 wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
232         if (wwns == 0)
233                 goto bad;
234         for (i = 0; i < wwnrow; i++)
235                 for (j = 0; j < wwncol; j++)
236                         wwns[i][j].c_w = ' ';
237         if (tt.tt_checkpoint) {
238                 /* wwcs is also cleared in wwstart1() */
239                 wwcs = (union ww_char **)
240                         wwalloc(0, 0, wwnrow, wwncol, sizeof (union ww_char));
241                 if (wwcs == 0)
242                         goto bad;
243         }
244
245         wwtouched = malloc((unsigned) wwnrow);
246         if (wwtouched == 0) {
247                 wwerrno = WWE_NOMEM;
248                 goto bad;
249         }
250         for (i = 0; i < wwnrow; i++)
251                 wwtouched[i] = 0;
252
253         wwupd = (struct ww_update *) malloc((unsigned) wwnrow * sizeof *wwupd);
254         if (wwupd == 0) {
255                 wwerrno = WWE_NOMEM;
256                 goto bad;
257         }
258
259         wwindex[WWX_NOBODY] = &wwnobody;
260         wwnobody.ww_order = NWW;
261
262         kp = wwwintermcap;
263         if (wwavailmodes & WWM_REV)
264                 wwaddcap1(WWT_REV, &kp);
265         if (wwavailmodes & WWM_BLK)
266                 wwaddcap1(WWT_BLK, &kp);
267         if (wwavailmodes & WWM_UL)
268                 wwaddcap1(WWT_UL, &kp);
269         if (wwavailmodes & WWM_GRP)
270                 wwaddcap1(WWT_GRP, &kp);
271         if (wwavailmodes & WWM_DIM)
272                 wwaddcap1(WWT_DIM, &kp);
273         if (wwavailmodes & WWM_USR)
274                 wwaddcap1(WWT_USR, &kp);
275         if (tt.tt_insline && tt.tt_delline || tt.tt_setscroll)
276                 wwaddcap1(WWT_ALDL, &kp);
277         if (tt.tt_inschar)
278                 wwaddcap1(WWT_IMEI, &kp);
279         if (tt.tt_insspace)
280                 wwaddcap1(WWT_IC, &kp);
281         if (tt.tt_delchar)
282                 wwaddcap1(WWT_DC, &kp);
283         wwaddcap("kb", &kp);
284         wwaddcap2("ku", &kp);
285         wwaddcap2("kd", &kp);
286         wwaddcap2("kl", &kp);
287         wwaddcap2("kr", &kp);
288         wwaddcap("kh", &kp);
289         if ((j = tgetnum("kn")) >= 0) {
290                 char cap[32];
291
292                 (void) sprintf(kp, "kn#%d:", j);
293                 for (; *kp; kp++)
294                         ;
295                 for (i = 1; i <= j; i++) {
296                         (void) sprintf(cap, "k%d", i);
297                         wwaddcap(cap, &kp);
298                         cap[0] = 'l';
299                         wwaddcap(cap, &kp);
300                 }
301         }
302         /*
303          * It's ok to do this here even if setenv() is destructive
304          * since tt_init() has already made its own copy of it and
305          * wwterm now points to the copy.
306          */
307         (void) setenv("TERM", WWT_TERM, 1);
308 #ifdef TERMINFO
309         if (wwterminfoinit() < 0)
310                 goto bad;
311 #endif
312
313         if (tt.tt_checkpoint)
314                 if (signal(SIGALRM, wwalarm) == BADSIG) {
315                         wwerrno = WWE_SYS;
316                         goto bad;
317                 }
318         fcntl(0, F_SETOWN, getpid());
319         /* catch typeahead before ASYNC was set */
320         (void) kill(getpid(), SIGIO);
321         wwstart1();
322         (void) sigsetmask(s);
323         return 0;
324 bad:
325         /*
326          * Don't bother to free storage.  We're supposed
327          * to exit when wwinit fails anyway.
328          */
329         (void) signal(SIGIO, SIG_DFL);
330         (void) wwsettty(0, &wwoldtty);
331         (void) sigsetmask(s);
332         return -1;
333 }
334
335 wwaddcap(cap, kp)
336         register char *cap;
337         register char **kp;
338 {
339         char tbuf[512];
340         char *tp = tbuf;
341         register char *str, *p;
342
343         if ((str = tgetstr(cap, &tp)) != 0) {
344                 while (*(*kp)++ = *cap++)
345                         ;
346                 (*kp)[-1] = '=';
347                 while (*str) {
348                         for (p = unctrl(*str++); *(*kp)++ = *p++;)
349                                 ;
350                         (*kp)--;
351                 }
352                 *(*kp)++ = ':';
353                 **kp = 0;
354         }
355 }
356
357 wwaddcap1(cap, kp)
358         register char *cap;
359         register char **kp;
360 {
361         while (*(*kp)++ = *cap++)
362                 ;
363         (*kp)--;
364 }
365
366 wwaddcap2(cap, kp)
367         register char *cap;
368         register char **kp;
369 {
370         char tbuf[512];
371         char *tp = tbuf;
372         register char *str, *p;
373
374         if ((str = tgetstr(cap, &tp)) != 0) {
375                 /* we don't support vt100's application key mode, remap */
376                 if (str[0] == ctrl('[') && str[1] == 'O')
377                         str[1] = '[';
378                 while (*(*kp)++ = *cap++)
379                         ;
380                 (*kp)[-1] = '=';
381                 while (*str) {
382                         for (p = unctrl(*str++); *(*kp)++ = *p++;)
383                                 ;
384                         (*kp)--;
385                 }
386                 *(*kp)++ = ':';
387                 **kp = 0;
388         }
389 }
390
391 wwstart()
392 {
393         register i;
394
395         (void) wwsettty(0, &wwnewtty);
396         signal(SIGIO, wwrint);
397         for (i = 0; i < wwnrow; i++)
398                 wwtouched[i] = WWU_TOUCHED;
399         wwstart1();
400 }
401
402 wwstart1()
403 {
404         register i, j;
405
406         for (i = 0; i < wwnrow; i++)
407                 for (j = 0; j < wwncol; j++) {
408                         wwos[i][j].c_w = ' ';
409                         if (tt.tt_checkpoint)
410                                 wwcs[i][j].c_w = ' ';
411                 }
412         xxstart();
413         if (tt.tt_checkpoint)
414                 wwdocheckpoint = 1;
415 }
416
417 /*
418  * Reset data structures and terminal from an unknown state.
419  * Restoring wwos has been taken care of elsewhere.
420  */
421 wwreset()
422 {
423         register i;
424
425         xxreset();
426         for (i = 0; i < wwnrow; i++)
427                 wwtouched[i] = WWU_TOUCHED;
428 }