Annotation of src/usr.bin/window/wwopen.c, Revision 1.7
1.7 ! mpech 1: /* $OpenBSD: wwopen.c,v 1.6 1998/04/26 22:49:09 millert Exp $ */
1.3 niklas 2: /* $NetBSD: wwopen.c,v 1.6 1996/02/08 21:08:04 mycroft Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Edward Wang at The University of California, Berkeley.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the University of
22: * California, Berkeley and its contributors.
23: * 4. Neither the name of the University nor the names of its contributors
24: * may be used to endorse or promote products derived from this software
25: * without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37: * SUCH DAMAGE.
38: */
39:
40: #ifndef lint
41: #if 0
42: static char sccsid[] = "@(#)wwopen.c 8.2 (Berkeley) 4/28/95";
43: #else
1.7 ! mpech 44: static char rcsid[] = "$OpenBSD: wwopen.c,v 1.6 1998/04/26 22:49:09 millert Exp $";
1.1 deraadt 45: #endif
46: #endif /* not lint */
47:
1.5 downsj 48: #include <stdlib.h>
1.1 deraadt 49: #include "ww.h"
50: #include <sys/types.h>
51: #include <sys/socket.h>
52: #include <fcntl.h>
53:
54: struct ww *
1.3 niklas 55: wwopen(type, oflags, nrow, ncol, row, col, nline)
1.1 deraadt 56: {
1.7 ! mpech 57: struct ww *w;
! 58: int i, j;
1.1 deraadt 59: char m;
60: short nvis;
61:
62: w = (struct ww *)calloc(sizeof (struct ww), 1);
63: if (w == 0) {
64: wwerrno = WWE_NOMEM;
65: goto bad;
66: }
67: w->ww_pty = -1;
68: w->ww_socket = -1;
69:
70: for (i = 0; i < NWW && wwindex[i] != 0; i++)
71: ;
72: if (i >= NWW) {
73: wwerrno = WWE_TOOMANY;
74: goto bad;
75: }
76: w->ww_index = i;
77:
78: if (nline < nrow)
79: nline = nrow;
80:
81: w->ww_w.t = row;
82: w->ww_w.b = row + nrow;
83: w->ww_w.l = col;
84: w->ww_w.r = col + ncol;
85: w->ww_w.nr = nrow;
86: w->ww_w.nc = ncol;
87:
88: w->ww_b.t = row;
89: w->ww_b.b = row + nline;
90: w->ww_b.l = col;
91: w->ww_b.r = col + ncol;
92: w->ww_b.nr = nline;
93: w->ww_b.nc = ncol;
94:
95: w->ww_i.t = MAX(w->ww_w.t, 0);
96: w->ww_i.b = MIN(w->ww_w.b, wwnrow);
97: w->ww_i.l = MAX(w->ww_w.l, 0);
98: w->ww_i.r = MIN(w->ww_w.r, wwncol);
99: w->ww_i.nr = w->ww_i.b - w->ww_i.t;
100: w->ww_i.nc = w->ww_i.r - w->ww_i.l;
101:
102: w->ww_cur.r = w->ww_w.t;
103: w->ww_cur.c = w->ww_w.l;
104:
1.2 deraadt 105: w->ww_type = type;
106: switch (type) {
107: case WWT_PTY:
1.1 deraadt 108: if (wwgetpty(w) < 0)
109: goto bad;
1.2 deraadt 110: break;
111: case WWT_SOCKET:
112: {
1.1 deraadt 113: int d[2];
114: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
115: wwerrno = WWE_SYS;
116: goto bad;
117: }
118: (void) fcntl(d[0], F_SETFD, 1);
119: (void) fcntl(d[1], F_SETFD, 1);
120: w->ww_pty = d[0];
121: w->ww_socket = d[1];
1.2 deraadt 122: break;
123: }
1.1 deraadt 124: }
1.2 deraadt 125: if (type != WWT_INTERNAL) {
1.1 deraadt 126: if ((w->ww_ob = malloc(512)) == 0) {
127: wwerrno = WWE_NOMEM;
128: goto bad;
129: }
130: w->ww_obe = w->ww_ob + 512;
131: w->ww_obp = w->ww_obq = w->ww_ob;
132: if (w->ww_pty >= wwdtablesize)
133: wwdtablesize = w->ww_pty + 1;
134: }
135:
136: w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
137: w->ww_w.nr, w->ww_w.nc, sizeof (char));
138: if (w->ww_win == 0)
139: goto bad;
140: m = 0;
1.3 niklas 141: if (oflags & WWO_GLASS)
1.1 deraadt 142: m |= WWM_GLS;
1.3 niklas 143: if (oflags & WWO_REVERSE)
1.1 deraadt 144: if (wwavailmodes & WWM_REV)
145: m |= WWM_REV;
146: else
1.3 niklas 147: oflags &= ~WWO_REVERSE;
1.1 deraadt 148: for (i = w->ww_w.t; i < w->ww_w.b; i++)
149: for (j = w->ww_w.l; j < w->ww_w.r; j++)
150: w->ww_win[i][j] = m;
151:
1.3 niklas 152: if (oflags & WWO_FRAME) {
1.1 deraadt 153: w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
154: w->ww_w.nr, w->ww_w.nc, sizeof (char));
155: if (w->ww_fmap == 0)
156: goto bad;
157: for (i = w->ww_w.t; i < w->ww_w.b; i++)
158: for (j = w->ww_w.l; j < w->ww_w.r; j++)
159: w->ww_fmap[i][j] = 0;
160: }
161:
162: w->ww_buf = (union ww_char **)
163: wwalloc(w->ww_b.t, w->ww_b.l,
164: w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
165: if (w->ww_buf == 0)
166: goto bad;
167: for (i = w->ww_b.t; i < w->ww_b.b; i++)
168: for (j = w->ww_b.l; j < w->ww_b.r; j++)
169: w->ww_buf[i][j].c_w = ' ';
170:
1.6 millert 171: w->ww_nvis = (short *)malloc(w->ww_w.nr * sizeof (short));
1.1 deraadt 172: if (w->ww_nvis == 0) {
173: wwerrno = WWE_NOMEM;
174: goto bad;
175: }
176: w->ww_nvis -= w->ww_w.t;
177: nvis = m ? 0 : w->ww_w.nc;
178: for (i = w->ww_w.t; i < w->ww_w.b; i++)
179: w->ww_nvis[i] = nvis;
180:
181: w->ww_state = WWS_INITIAL;
1.3 niklas 182: CLR(w->ww_oflags, WWO_ALLFLAGS);
183: SET(w->ww_oflags, oflags);
1.1 deraadt 184: return wwindex[w->ww_index] = w;
185: bad:
186: if (w != 0) {
187: if (w->ww_win != 0)
188: wwfree(w->ww_win, w->ww_w.t);
189: if (w->ww_fmap != 0)
190: wwfree(w->ww_fmap, w->ww_w.t);
191: if (w->ww_buf != 0)
192: wwfree((char **)w->ww_buf, w->ww_b.t);
193: if (w->ww_nvis != 0)
194: free((char *)(w->ww_nvis + w->ww_w.t));
195: if (w->ww_ob != 0)
196: free(w->ww_ob);
197: if (w->ww_pty >= 0)
198: (void) close(w->ww_pty);
199: if (w->ww_socket >= 0)
200: (void) close(w->ww_socket);
201: free((char *)w);
202: }
203: return 0;
204: }