1: #ifndef lint
2: static char sccsid[] = "@(#)wwopen.c 3.21 4/24/85";
3: #endif
4:
5: /*
6: * Copyright (c) 1983 Regents of the University of California,
7: * All rights reserved. Redistribution permitted subject to
8: * the terms of the Berkeley Software License Agreement.
9: */
10:
11: #include "ww.h"
12: #include <sys/types.h>
13: #include <sys/socket.h>
14:
15: struct ww *
16: wwopen(flags, nrow, ncol, row, col, nline)
17: {
18: register struct ww *w;
19: register i, j;
20: char m;
21: short nvis;
22:
23: w = (struct ww *)calloc(sizeof (struct ww), 1);
24: if (w == 0) {
25: wwerrno = WWE_NOMEM;
26: goto bad;
27: }
28: w->ww_pty = -1;
29: w->ww_socket = -1;
30:
31: for (i = 0; i < NWW && wwindex[i] != 0; i++)
32: ;
33: if (i >= NWW) {
34: wwerrno = WWE_TOOMANY;
35: goto bad;
36: }
37: w->ww_index = i;
38:
39: if (nline < nrow)
40: nline = nrow;
41:
42: w->ww_w.t = row;
43: w->ww_w.b = row + nrow;
44: w->ww_w.l = col;
45: w->ww_w.r = col + ncol;
46: w->ww_w.nr = nrow;
47: w->ww_w.nc = ncol;
48:
49: w->ww_b.t = row;
50: w->ww_b.b = row + nline;
51: w->ww_b.l = col;
52: w->ww_b.r = col + ncol;
53: w->ww_b.nr = nline;
54: w->ww_b.nc = ncol;
55:
56: w->ww_i.t = MAX(w->ww_w.t, 0);
57: w->ww_i.b = MIN(w->ww_w.b, wwnrow);
58: w->ww_i.l = MAX(w->ww_w.l, 0);
59: w->ww_i.r = MIN(w->ww_w.r, wwncol);
60: w->ww_i.nr = w->ww_i.b - w->ww_i.t;
61: w->ww_i.nc = w->ww_i.r - w->ww_i.l;
62:
63: w->ww_cur.r = w->ww_w.t;
64: w->ww_cur.c = w->ww_w.l;
65:
66: if (flags & WWO_PTY) {
67: struct winsize winsize;
68:
69: if (wwgetpty(w) < 0)
70: goto bad;
71: if (wwsettty(w->ww_pty, &wwwintty, (struct ww_tty *)0) < 0)
72: goto bad;
73: winsize.ws_row = nrow;
74: winsize.ws_col = ncol;
75: winsize.ws_xpixel = winsize.ws_ypixel = 0;
76: if (ioctl(w->ww_pty, (int)TIOCSWINSZ, (char *)&winsize) < 0) {
77: wwerrno = WWE_SYS;
78: goto bad;
79: }
80: w->ww_ispty = 1;
81: } else if (flags & WWO_SOCKET) {
82: int d[2];
83: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
84: wwerrno = WWE_SYS;
85: goto bad;
86: }
87: w->ww_pty = d[0];
88: w->ww_socket = d[1];
89: }
90: if (flags & (WWO_PTY|WWO_SOCKET)) {
91: if ((w->ww_ob = malloc(512)) == 0) {
92: wwerrno = WWE_NOMEM;
93: goto bad;
94: }
95: w->ww_obe = w->ww_ob + 512;
96: w->ww_obp = w->ww_obq = w->ww_ob;
97: }
98:
99: w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
100: w->ww_w.nr, w->ww_w.nc, sizeof (char));
101: if (w->ww_win == 0)
102: goto bad;
103: m = 0;
104: if (flags & WWO_GLASS)
105: m |= WWM_GLS;
106: if (flags & WWO_REVERSE)
107: m |= WWM_REV;
108: for (i = w->ww_w.t; i < w->ww_w.b; i++)
109: for (j = w->ww_w.l; j < w->ww_w.r; j++)
110: w->ww_win[i][j] = m;
111:
112: if (flags & WWO_FRAME) {
113: w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
114: w->ww_w.nr, w->ww_w.nc, sizeof (char));
115: if (w->ww_fmap == 0)
116: goto bad;
117: for (i = w->ww_w.t; i < w->ww_w.b; i++)
118: for (j = w->ww_w.l; j < w->ww_w.r; j++)
119: w->ww_fmap[i][j] = 0;
120: }
121:
122: w->ww_buf = (union ww_char **)
123: wwalloc(w->ww_b.t, w->ww_b.l,
124: w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
125: if (w->ww_buf == 0)
126: goto bad;
127: for (i = w->ww_b.t; i < w->ww_b.b; i++)
128: for (j = w->ww_b.l; j < w->ww_b.r; j++)
129: w->ww_buf[i][j].c_w = ' ';
130:
131: w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short));
132: if (w->ww_nvis == 0) {
133: wwerrno = WWE_NOMEM;
134: goto bad;
135: }
136: w->ww_nvis -= w->ww_w.t;
137: nvis = m ? 0 : w->ww_w.nc;
138: for (i = w->ww_w.t; i < w->ww_w.b; i++)
139: w->ww_nvis[i] = nvis;
140:
141: w->ww_state = WWS_INITIAL;
142: w->ww_oflags = flags;
143: return wwindex[w->ww_index] = w;
144: bad:
145: if (w != 0) {
146: if (w->ww_win != 0)
147: wwfree(w->ww_win, w->ww_w.t);
148: if (w->ww_fmap != 0)
149: wwfree(w->ww_fmap, w->ww_w.t);
150: if (w->ww_buf != 0)
151: wwfree((char **)w->ww_buf, w->ww_b.t);
152: if (w->ww_nvis != 0)
153: free((char *)(w->ww_nvis + w->ww_w.t));
154: if (w->ww_ob != 0)
155: free(w->ww_ob);
156: if (w->ww_pty >= 0)
157: (void) close(w->ww_pty);
158: if (w->ww_socket >= 0)
159: (void) close(w->ww_socket);
160: free((char *)w);
161: }
162: return 0;
163: }
Defined functions
Defined variables
sccsid
defined in line
2;
never used