2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
38 static char sccsid[] = "@(#)wwopen.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
45 #include <sys/socket.h>
50 wwopen(flags, nrow, ncol, row, col, nline)
52 register struct ww *w;
57 w = (struct ww *)calloc(sizeof (struct ww), 1);
65 for (i = 0; i < NWW && wwindex[i] != 0; i++)
68 wwerrno = WWE_TOOMANY;
77 w->ww_w.b = row + nrow;
79 w->ww_w.r = col + ncol;
84 w->ww_b.b = row + nline;
86 w->ww_b.r = col + ncol;
90 w->ww_i.t = MAX(w->ww_w.t, 0);
91 w->ww_i.b = MIN(w->ww_w.b, wwnrow);
92 w->ww_i.l = MAX(w->ww_w.l, 0);
93 w->ww_i.r = MIN(w->ww_w.r, wwncol);
94 w->ww_i.nr = w->ww_i.b - w->ww_i.t;
95 w->ww_i.nc = w->ww_i.r - w->ww_i.l;
97 w->ww_cur.r = w->ww_w.t;
98 w->ww_cur.c = w->ww_w.l;
100 if (flags & WWO_PTY) {
104 } else if (flags & WWO_SOCKET) {
106 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
110 (void) fcntl(d[0], F_SETFD, 1);
111 (void) fcntl(d[1], F_SETFD, 1);
115 if (flags & (WWO_PTY|WWO_SOCKET)) {
116 if ((w->ww_ob = malloc(512)) == 0) {
120 w->ww_obe = w->ww_ob + 512;
121 w->ww_obp = w->ww_obq = w->ww_ob;
124 w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
125 w->ww_w.nr, w->ww_w.nc, sizeof (char));
129 if (flags & WWO_GLASS)
131 if (flags & WWO_REVERSE)
132 if (wwavailmodes & WWM_REV)
135 flags &= ~WWO_REVERSE;
136 for (i = w->ww_w.t; i < w->ww_w.b; i++)
137 for (j = w->ww_w.l; j < w->ww_w.r; j++)
140 if (flags & WWO_FRAME) {
141 w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
142 w->ww_w.nr, w->ww_w.nc, sizeof (char));
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_fmap[i][j] = 0;
150 w->ww_buf = (union ww_char **)
151 wwalloc(w->ww_b.t, w->ww_b.l,
152 w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
155 for (i = w->ww_b.t; i < w->ww_b.b; i++)
156 for (j = w->ww_b.l; j < w->ww_b.r; j++)
157 w->ww_buf[i][j].c_w = ' ';
159 w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short));
160 if (w->ww_nvis == 0) {
164 w->ww_nvis -= w->ww_w.t;
165 nvis = m ? 0 : w->ww_w.nc;
166 for (i = w->ww_w.t; i < w->ww_w.b; i++)
167 w->ww_nvis[i] = nvis;
169 w->ww_state = WWS_INITIAL;
170 w->ww_oflags = flags;
171 return wwindex[w->ww_index] = w;
175 wwfree(w->ww_win, w->ww_w.t);
177 wwfree(w->ww_fmap, w->ww_w.t);
179 wwfree((char **)w->ww_buf, w->ww_b.t);
181 free((char *)(w->ww_nvis + w->ww_w.t));
185 (void) close(w->ww_pty);
186 if (w->ww_socket >= 0)
187 (void) close(w->ww_socket);