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.26 (Berkeley) 02/16/89"; 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 winsize.ws_row = nrow; 83 winsize.ws_col = ncol; 84 winsize.ws_xpixel = winsize.ws_ypixel = 0; 85 if (ioctl(w->ww_pty, TIOCSWINSZ, (char *)&winsize) < 0) { 86 wwerrno = WWE_SYS; 87 goto bad; 88 } 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; 94 goto bad; 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 } 104 w->ww_obe = w->ww_ob + 512; 105 w->ww_obp = w->ww_obq = w->ww_ob; 106 } 107 108 w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l, 109 w->ww_w.nr, w->ww_w.nc, sizeof (char)); 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) 116 if (wwavailmodes & WWM_REV) 117 m |= WWM_REV; 118 else 119 flags &= ~WWO_REVERSE; 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++) 122 w->ww_win[i][j] = m; 123 124 if (flags & WWO_FRAME) { 125 w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l, 126 w->ww_w.nr, w->ww_w.nc, sizeof (char)); 127 if (w->ww_fmap == 0) 128 goto bad; 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++) 131 w->ww_fmap[i][j] = 0; 132 } 133 134 w->ww_buf = (union ww_char **) 135 wwalloc(w->ww_b.t, w->ww_b.l, 136 w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char)); 137 if (w->ww_buf == 0) 138 goto bad; 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++) 141 w->ww_buf[i][j].c_w = ' '; 142 143 w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short)); 144 if (w->ww_nvis == 0) { 145 wwerrno = WWE_NOMEM; 146 goto bad; 147 } 148 w->ww_nvis -= w->ww_w.t; 149 nvis = m ? 0 : w->ww_w.nc; 150 for (i = w->ww_w.t; i < w->ww_w.b; i++) 151 w->ww_nvis[i] = nvis; 152 153 w->ww_state = WWS_INITIAL; 154 w->ww_oflags = flags; 155 return wwindex[w->ww_index] = w; 156 bad: 157 if (w != 0) { 158 if (w->ww_win != 0) 159 wwfree(w->ww_win, w->ww_w.t); 160 if (w->ww_fmap != 0) 161 wwfree(w->ww_fmap, w->ww_w.t); 162 if (w->ww_buf != 0) 163 wwfree((char **)w->ww_buf, w->ww_b.t); 164 if (w->ww_nvis != 0) 165 free((char *)(w->ww_nvis + w->ww_w.t)); 166 if (w->ww_ob != 0) 167 free(w->ww_ob); 168 if (w->ww_pty >= 0) 169 (void) close(w->ww_pty); 170 if (w->ww_socket >= 0) 171 (void) close(w->ww_socket); 172 free((char *)w); 173 } 174 return 0; 175 } 176