|
|
1.1 root 1: /*
2: * Copyright (c) 1983 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef lint
19: static char sccsid[] = "@(#)wwopen.c 3.25 (Berkeley) 6/29/88";
20: #endif /* not lint */
21:
22: #include "ww.h"
23: #include <sys/types.h>
24: #include <sys/socket.h>
25:
26: struct ww *
27: wwopen(flags, nrow, ncol, row, col, nline)
28: {
29: register struct ww *w;
30: register i, j;
31: char m;
32: short nvis;
33:
34: w = (struct ww *)calloc(sizeof (struct ww), 1);
35: if (w == 0) {
36: wwerrno = WWE_NOMEM;
37: goto bad;
38: }
39: w->ww_pty = -1;
40: w->ww_socket = -1;
41:
42: for (i = 0; i < NWW && wwindex[i] != 0; i++)
43: ;
44: if (i >= NWW) {
45: wwerrno = WWE_TOOMANY;
46: goto bad;
47: }
48: w->ww_index = i;
49:
50: if (nline < nrow)
51: nline = nrow;
52:
53: w->ww_w.t = row;
54: w->ww_w.b = row + nrow;
55: w->ww_w.l = col;
56: w->ww_w.r = col + ncol;
57: w->ww_w.nr = nrow;
58: w->ww_w.nc = ncol;
59:
60: w->ww_b.t = row;
61: w->ww_b.b = row + nline;
62: w->ww_b.l = col;
63: w->ww_b.r = col + ncol;
64: w->ww_b.nr = nline;
65: w->ww_b.nc = ncol;
66:
67: w->ww_i.t = MAX(w->ww_w.t, 0);
68: w->ww_i.b = MIN(w->ww_w.b, wwnrow);
69: w->ww_i.l = MAX(w->ww_w.l, 0);
70: w->ww_i.r = MIN(w->ww_w.r, wwncol);
71: w->ww_i.nr = w->ww_i.b - w->ww_i.t;
72: w->ww_i.nc = w->ww_i.r - w->ww_i.l;
73:
74: w->ww_cur.r = w->ww_w.t;
75: w->ww_cur.c = w->ww_w.l;
76:
77: if (flags & WWO_PTY) {
78: struct winsize winsize;
79:
80: if (wwgetpty(w) < 0)
81: goto bad;
82: if (wwsettty(w->ww_pty, &wwwintty, (struct ww_tty *)0) < 0)
83: goto bad;
84: winsize.ws_row = nrow;
85: winsize.ws_col = ncol;
86: winsize.ws_xpixel = winsize.ws_ypixel = 0;
87: if (ioctl(w->ww_pty, TIOCSWINSZ, (char *)&winsize) < 0) {
88: wwerrno = WWE_SYS;
89: goto bad;
90: }
91: w->ww_ispty = 1;
92: } else if (flags & WWO_SOCKET) {
93: int d[2];
94: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
95: wwerrno = WWE_SYS;
96: goto bad;
97: }
98: w->ww_pty = d[0];
99: w->ww_socket = d[1];
100: }
101: if (flags & (WWO_PTY|WWO_SOCKET)) {
102: if ((w->ww_ob = malloc(512)) == 0) {
103: wwerrno = WWE_NOMEM;
104: goto bad;
105: }
106: w->ww_obe = w->ww_ob + 512;
107: w->ww_obp = w->ww_obq = w->ww_ob;
108: }
109:
110: w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
111: w->ww_w.nr, w->ww_w.nc, sizeof (char));
112: if (w->ww_win == 0)
113: goto bad;
114: m = 0;
115: if (flags & WWO_GLASS)
116: m |= WWM_GLS;
117: if (flags & WWO_REVERSE)
118: if (wwavailmodes & WWM_REV)
119: m |= WWM_REV;
120: else
121: flags &= ~WWO_REVERSE;
122: for (i = w->ww_w.t; i < w->ww_w.b; i++)
123: for (j = w->ww_w.l; j < w->ww_w.r; j++)
124: w->ww_win[i][j] = m;
125:
126: if (flags & WWO_FRAME) {
127: w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
128: w->ww_w.nr, w->ww_w.nc, sizeof (char));
129: if (w->ww_fmap == 0)
130: goto bad;
131: for (i = w->ww_w.t; i < w->ww_w.b; i++)
132: for (j = w->ww_w.l; j < w->ww_w.r; j++)
133: w->ww_fmap[i][j] = 0;
134: }
135:
136: w->ww_buf = (union ww_char **)
137: wwalloc(w->ww_b.t, w->ww_b.l,
138: w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
139: if (w->ww_buf == 0)
140: goto bad;
141: for (i = w->ww_b.t; i < w->ww_b.b; i++)
142: for (j = w->ww_b.l; j < w->ww_b.r; j++)
143: w->ww_buf[i][j].c_w = ' ';
144:
145: w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short));
146: if (w->ww_nvis == 0) {
147: wwerrno = WWE_NOMEM;
148: goto bad;
149: }
150: w->ww_nvis -= w->ww_w.t;
151: nvis = m ? 0 : w->ww_w.nc;
152: for (i = w->ww_w.t; i < w->ww_w.b; i++)
153: w->ww_nvis[i] = nvis;
154:
155: w->ww_state = WWS_INITIAL;
156: w->ww_oflags = flags;
157: return wwindex[w->ww_index] = w;
158: bad:
159: if (w != 0) {
160: if (w->ww_win != 0)
161: wwfree(w->ww_win, w->ww_w.t);
162: if (w->ww_fmap != 0)
163: wwfree(w->ww_fmap, w->ww_w.t);
164: if (w->ww_buf != 0)
165: wwfree((char **)w->ww_buf, w->ww_b.t);
166: if (w->ww_nvis != 0)
167: free((char *)(w->ww_nvis + w->ww_w.t));
168: if (w->ww_ob != 0)
169: free(w->ww_ob);
170: if (w->ww_pty >= 0)
171: (void) close(w->ww_pty);
172: if (w->ww_socket >= 0)
173: (void) close(w->ww_socket);
174: free((char *)w);
175: }
176: return 0;
177: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.