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