Commit | Line | Data |
---|---|---|
60de5df9 | 1 | /* |
46e9ea25 KB |
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 | |
5e8b0e60 KB |
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. | |
60de5df9 EW |
16 | */ |
17 | ||
46e9ea25 | 18 | #ifndef lint |
77f9a432 | 19 | static char sccsid[] = "@(#)wwopen.c 3.26 (Berkeley) %G%"; |
46e9ea25 KB |
20 | #endif /* not lint */ |
21 | ||
138b737c | 22 | #include "ww.h" |
7ecf4dca EW |
23 | #include <sys/types.h> |
24 | #include <sys/socket.h> | |
138b737c | 25 | |
138b737c | 26 | struct ww * |
bb05dfb5 | 27 | wwopen(flags, nrow, ncol, row, col, nline) |
138b737c | 28 | { |
a8a39d2c | 29 | register struct ww *w; |
bb05dfb5 EW |
30 | register i, j; |
31 | char m; | |
32 | short nvis; | |
138b737c | 33 | |
03f0d462 | 34 | w = (struct ww *)calloc(sizeof (struct ww), 1); |
03e75950 EW |
35 | if (w == 0) { |
36 | wwerrno = WWE_NOMEM; | |
03f0d462 | 37 | goto bad; |
03e75950 | 38 | } |
eec72f58 | 39 | w->ww_pty = -1; |
7ecf4dca | 40 | w->ww_socket = -1; |
03f0d462 | 41 | |
bb05dfb5 EW |
42 | for (i = 0; i < NWW && wwindex[i] != 0; i++) |
43 | ; | |
03e75950 EW |
44 | if (i >= NWW) { |
45 | wwerrno = WWE_TOOMANY; | |
138b737c | 46 | goto bad; |
03e75950 | 47 | } |
bb05dfb5 | 48 | w->ww_index = i; |
bb05dfb5 | 49 | |
f2a77fe1 EW |
50 | if (nline < nrow) |
51 | nline = nrow; | |
52 | ||
19f9784c EW |
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; | |
f2a77fe1 EW |
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 | ||
19f9784c EW |
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 | ||
f2a77fe1 EW |
74 | w->ww_cur.r = w->ww_w.t; |
75 | w->ww_cur.c = w->ww_w.l; | |
bb05dfb5 | 76 | |
03f0d462 | 77 | if (flags & WWO_PTY) { |
b27a9cfb EW |
78 | struct winsize winsize; |
79 | ||
03f0d462 EW |
80 | if (wwgetpty(w) < 0) |
81 | goto bad; | |
b27a9cfb EW |
82 | winsize.ws_row = nrow; |
83 | winsize.ws_col = ncol; | |
84 | winsize.ws_xpixel = winsize.ws_ypixel = 0; | |
20029082 | 85 | if (ioctl(w->ww_pty, TIOCSWINSZ, (char *)&winsize) < 0) { |
b27a9cfb EW |
86 | wwerrno = WWE_SYS; |
87 | goto bad; | |
88 | } | |
7ecf4dca EW |
89 | w->ww_ispty = 1; |
90 | } else if (flags & WWO_SOCKET) { | |
91 | int d[2]; | |
92 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) { | |
93 | wwerrno = WWE_SYS; | |
8fa6d94c | 94 | goto bad; |
7ecf4dca EW |
95 | } |
96 | w->ww_pty = d[0]; | |
97 | w->ww_socket = d[1]; | |
98 | } | |
99 | if (flags & (WWO_PTY|WWO_SOCKET)) { | |
100 | if ((w->ww_ob = malloc(512)) == 0) { | |
101 | wwerrno = WWE_NOMEM; | |
102 | goto bad; | |
103 | } | |
8fa6d94c | 104 | w->ww_obe = w->ww_ob + 512; |
7ecf4dca | 105 | w->ww_obp = w->ww_obq = w->ww_ob; |
03f0d462 EW |
106 | } |
107 | ||
f2a77fe1 EW |
108 | w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l, |
109 | w->ww_w.nr, w->ww_w.nc, sizeof (char)); | |
bb05dfb5 EW |
110 | if (w->ww_win == 0) |
111 | goto bad; | |
112 | m = 0; | |
113 | if (flags & WWO_GLASS) | |
114 | m |= WWM_GLS; | |
115 | if (flags & WWO_REVERSE) | |
f4f5703a EW |
116 | if (wwavailmodes & WWM_REV) |
117 | m |= WWM_REV; | |
118 | else | |
119 | flags &= ~WWO_REVERSE; | |
f2a77fe1 EW |
120 | for (i = w->ww_w.t; i < w->ww_w.b; i++) |
121 | for (j = w->ww_w.l; j < w->ww_w.r; j++) | |
bb05dfb5 | 122 | w->ww_win[i][j] = m; |
2357b64e EW |
123 | |
124 | if (flags & WWO_FRAME) { | |
f2a77fe1 EW |
125 | w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l, |
126 | w->ww_w.nr, w->ww_w.nc, sizeof (char)); | |
2357b64e | 127 | if (w->ww_fmap == 0) |
b27a9cfb | 128 | goto bad; |
f2a77fe1 EW |
129 | for (i = w->ww_w.t; i < w->ww_w.b; i++) |
130 | for (j = w->ww_w.l; j < w->ww_w.r; j++) | |
2357b64e EW |
131 | w->ww_fmap[i][j] = 0; |
132 | } | |
b27a9cfb | 133 | |
bb05dfb5 | 134 | w->ww_buf = (union ww_char **) |
f2a77fe1 EW |
135 | wwalloc(w->ww_b.t, w->ww_b.l, |
136 | w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char)); | |
bb05dfb5 EW |
137 | if (w->ww_buf == 0) |
138 | goto bad; | |
f2a77fe1 EW |
139 | for (i = w->ww_b.t; i < w->ww_b.b; i++) |
140 | for (j = w->ww_b.l; j < w->ww_b.r; j++) | |
bb05dfb5 EW |
141 | w->ww_buf[i][j].c_w = ' '; |
142 | ||
143 | w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short)); | |
03e75950 EW |
144 | if (w->ww_nvis == 0) { |
145 | wwerrno = WWE_NOMEM; | |
138b737c | 146 | goto bad; |
03e75950 | 147 | } |
f2a77fe1 | 148 | w->ww_nvis -= w->ww_w.t; |
bb05dfb5 | 149 | nvis = m ? 0 : w->ww_w.nc; |
f2a77fe1 | 150 | for (i = w->ww_w.t; i < w->ww_w.b; i++) |
bb05dfb5 EW |
151 | w->ww_nvis[i] = nvis; |
152 | ||
153 | w->ww_state = WWS_INITIAL; | |
b27a9cfb | 154 | w->ww_oflags = flags; |
03f0d462 | 155 | return wwindex[w->ww_index] = w; |
138b737c | 156 | bad: |
4711df8b | 157 | if (w != 0) { |
bb05dfb5 | 158 | if (w->ww_win != 0) |
f2a77fe1 | 159 | wwfree(w->ww_win, w->ww_w.t); |
07f640ea | 160 | if (w->ww_fmap != 0) |
f2a77fe1 | 161 | wwfree(w->ww_fmap, w->ww_w.t); |
bb05dfb5 | 162 | if (w->ww_buf != 0) |
f2a77fe1 | 163 | wwfree((char **)w->ww_buf, w->ww_b.t); |
bb05dfb5 | 164 | if (w->ww_nvis != 0) |
f2a77fe1 | 165 | free((char *)(w->ww_nvis + w->ww_w.t)); |
8fa6d94c EW |
166 | if (w->ww_ob != 0) |
167 | free(w->ww_ob); | |
7ecf4dca EW |
168 | if (w->ww_pty >= 0) |
169 | (void) close(w->ww_pty); | |
170 | if (w->ww_socket >= 0) | |
171 | (void) close(w->ww_socket); | |
489d8a09 | 172 | free((char *)w); |
4711df8b | 173 | } |
138b737c EW |
174 | return 0; |
175 | } |