Merge from vendor branch OPENSSL:
[dragonfly.git] / usr.bin / window / wwopen.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  * @(#)wwopen.c 8.1 (Berkeley) 6/6/93
37  * $FreeBSD: src/usr.bin/window/wwopen.c,v 1.2.12.1 2001/05/17 09:45:01 obrien Exp $
38  * $DragonFly: src/usr.bin/window/wwopen.c,v 1.2 2003/06/17 04:29:34 dillon Exp $
39  */
40
41 #include "ww.h"
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <fcntl.h>
45 #include <stdlib.h>
46
47 struct ww *
48 wwopen(flags, nrow, ncol, row, col, nline)
49 {
50         register struct ww *w;
51         register i, j;
52         char m;
53         short nvis;
54
55         w = (struct ww *)calloc(sizeof (struct ww), 1);
56         if (w == 0) {
57                 wwerrno = WWE_NOMEM;
58                 goto bad;
59         }
60         w->ww_pty = -1;
61         w->ww_socket = -1;
62
63         for (i = 0; i < NWW && wwindex[i] != 0; i++)
64                 ;
65         if (i >= NWW) {
66                 wwerrno = WWE_TOOMANY;
67                 goto bad;
68         }
69         w->ww_index = i;
70
71         if (nline < nrow)
72                 nline = nrow;
73
74         w->ww_w.t = row;
75         w->ww_w.b = row + nrow;
76         w->ww_w.l = col;
77         w->ww_w.r = col + ncol;
78         w->ww_w.nr = nrow;
79         w->ww_w.nc = ncol;
80
81         w->ww_b.t = row;
82         w->ww_b.b = row + nline;
83         w->ww_b.l = col;
84         w->ww_b.r = col + ncol;
85         w->ww_b.nr = nline;
86         w->ww_b.nc = ncol;
87
88         w->ww_i.t = MAX(w->ww_w.t, 0);
89         w->ww_i.b = MIN(w->ww_w.b, wwnrow);
90         w->ww_i.l = MAX(w->ww_w.l, 0);
91         w->ww_i.r = MIN(w->ww_w.r, wwncol);
92         w->ww_i.nr = w->ww_i.b - w->ww_i.t;
93         w->ww_i.nc = w->ww_i.r - w->ww_i.l;
94
95         w->ww_cur.r = w->ww_w.t;
96         w->ww_cur.c = w->ww_w.l;
97
98         if (flags & WWO_PTY) {
99                 if (wwgetpty(w) < 0)
100                         goto bad;
101                 w->ww_ispty = 1;
102         } else if (flags & WWO_SOCKET) {
103                 int d[2];
104                 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
105                         wwerrno = WWE_SYS;
106                         goto bad;
107                 }
108                 (void) fcntl(d[0], F_SETFD, 1);
109                 (void) fcntl(d[1], F_SETFD, 1);
110                 w->ww_pty = d[0];
111                 w->ww_socket = d[1];
112         }
113         if (flags & (WWO_PTY|WWO_SOCKET)) {
114                 if ((w->ww_ob = malloc(512)) == 0) {
115                         wwerrno = WWE_NOMEM;
116                         goto bad;
117                 }
118                 w->ww_obe = w->ww_ob + 512;
119                 w->ww_obp = w->ww_obq = w->ww_ob;
120         }
121
122         w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
123                 w->ww_w.nr, w->ww_w.nc, sizeof (char));
124         if (w->ww_win == 0)
125                 goto bad;
126         m = 0;
127         if (flags & WWO_GLASS)
128                 m |= WWM_GLS;
129         if (flags & WWO_REVERSE)
130                 if (wwavailmodes & WWM_REV)
131                         m |= WWM_REV;
132                 else
133                         flags &= ~WWO_REVERSE;
134         for (i = w->ww_w.t; i < w->ww_w.b; i++)
135                 for (j = w->ww_w.l; j < w->ww_w.r; j++)
136                         w->ww_win[i][j] = m;
137
138         if (flags & WWO_FRAME) {
139                 w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
140                         w->ww_w.nr, w->ww_w.nc, sizeof (char));
141                 if (w->ww_fmap == 0)
142                         goto bad;
143                 for (i = w->ww_w.t; i < w->ww_w.b; i++)
144                         for (j = w->ww_w.l; j < w->ww_w.r; j++)
145                                 w->ww_fmap[i][j] = 0;
146         }
147
148         w->ww_buf = (union ww_char **)
149                 wwalloc(w->ww_b.t, w->ww_b.l,
150                         w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
151         if (w->ww_buf == 0)
152                 goto bad;
153         for (i = w->ww_b.t; i < w->ww_b.b; i++)
154                 for (j = w->ww_b.l; j < w->ww_b.r; j++)
155                         w->ww_buf[i][j].c_w = ' ';
156
157         w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short));
158         if (w->ww_nvis == 0) {
159                 wwerrno = WWE_NOMEM;
160                 goto bad;
161         }
162         w->ww_nvis -= w->ww_w.t;
163         nvis = m ? 0 : w->ww_w.nc;
164         for (i = w->ww_w.t; i < w->ww_w.b; i++)
165                 w->ww_nvis[i] = nvis;
166
167         w->ww_state = WWS_INITIAL;
168         w->ww_oflags = flags;
169         return wwindex[w->ww_index] = w;
170 bad:
171         if (w != 0) {
172                 if (w->ww_win != 0)
173                         wwfree(w->ww_win, w->ww_w.t);
174                 if (w->ww_fmap != 0)
175                         wwfree(w->ww_fmap, w->ww_w.t);
176                 if (w->ww_buf != 0)
177                         wwfree((char **)w->ww_buf, w->ww_b.t);
178                 if (w->ww_nvis != 0)
179                         free((char *)(w->ww_nvis + w->ww_w.t));
180                 if (w->ww_ob != 0)
181                         free(w->ww_ob);
182                 if (w->ww_pty >= 0)
183                         (void) close(w->ww_pty);
184                 if (w->ww_socket >= 0)
185                         (void) close(w->ww_socket);
186                 free((char *)w);
187         }
188         return 0;
189 }