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