[BACK]Return to wwopen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/wwopen.c, Revision 1.3

1.3     ! niklas      1: /*     $NetBSD: wwopen.c,v 1.6 1996/02/08 21:08:04 mycroft Exp $       */
1.1       deraadt     2:
                      3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Edward Wang at The University of California, Berkeley.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)wwopen.c   8.2 (Berkeley) 4/28/95";
                     42: #else
1.3     ! niklas     43: static char rcsid[] = "$NetBSD: wwopen.c,v 1.6 1996/02/08 21:08:04 mycroft Exp $";
1.1       deraadt    44: #endif
                     45: #endif /* not lint */
                     46:
                     47: #include "ww.h"
                     48: #include <sys/types.h>
                     49: #include <sys/socket.h>
                     50: #include <fcntl.h>
                     51:
                     52: struct ww *
1.3     ! niklas     53: wwopen(type, oflags, nrow, ncol, row, col, nline)
1.1       deraadt    54: {
                     55:        register struct ww *w;
                     56:        register i, j;
                     57:        char m;
                     58:        short nvis;
                     59:
                     60:        w = (struct ww *)calloc(sizeof (struct ww), 1);
                     61:        if (w == 0) {
                     62:                wwerrno = WWE_NOMEM;
                     63:                goto bad;
                     64:        }
                     65:        w->ww_pty = -1;
                     66:        w->ww_socket = -1;
                     67:
                     68:        for (i = 0; i < NWW && wwindex[i] != 0; i++)
                     69:                ;
                     70:        if (i >= NWW) {
                     71:                wwerrno = WWE_TOOMANY;
                     72:                goto bad;
                     73:        }
                     74:        w->ww_index = i;
                     75:
                     76:        if (nline < nrow)
                     77:                nline = nrow;
                     78:
                     79:        w->ww_w.t = row;
                     80:        w->ww_w.b = row + nrow;
                     81:        w->ww_w.l = col;
                     82:        w->ww_w.r = col + ncol;
                     83:        w->ww_w.nr = nrow;
                     84:        w->ww_w.nc = ncol;
                     85:
                     86:        w->ww_b.t = row;
                     87:        w->ww_b.b = row + nline;
                     88:        w->ww_b.l = col;
                     89:        w->ww_b.r = col + ncol;
                     90:        w->ww_b.nr = nline;
                     91:        w->ww_b.nc = ncol;
                     92:
                     93:        w->ww_i.t = MAX(w->ww_w.t, 0);
                     94:        w->ww_i.b = MIN(w->ww_w.b, wwnrow);
                     95:        w->ww_i.l = MAX(w->ww_w.l, 0);
                     96:        w->ww_i.r = MIN(w->ww_w.r, wwncol);
                     97:        w->ww_i.nr = w->ww_i.b - w->ww_i.t;
                     98:        w->ww_i.nc = w->ww_i.r - w->ww_i.l;
                     99:
                    100:        w->ww_cur.r = w->ww_w.t;
                    101:        w->ww_cur.c = w->ww_w.l;
                    102:
1.2       deraadt   103:        w->ww_type = type;
                    104:        switch (type) {
                    105:        case WWT_PTY:
1.1       deraadt   106:                if (wwgetpty(w) < 0)
                    107:                        goto bad;
1.2       deraadt   108:                break;
                    109:        case WWT_SOCKET:
                    110:            {
1.1       deraadt   111:                int d[2];
                    112:                if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
                    113:                        wwerrno = WWE_SYS;
                    114:                        goto bad;
                    115:                }
                    116:                (void) fcntl(d[0], F_SETFD, 1);
                    117:                (void) fcntl(d[1], F_SETFD, 1);
                    118:                w->ww_pty = d[0];
                    119:                w->ww_socket = d[1];
1.2       deraadt   120:                break;
                    121:            }
1.1       deraadt   122:        }
1.2       deraadt   123:        if (type != WWT_INTERNAL) {
1.1       deraadt   124:                if ((w->ww_ob = malloc(512)) == 0) {
                    125:                        wwerrno = WWE_NOMEM;
                    126:                        goto bad;
                    127:                }
                    128:                w->ww_obe = w->ww_ob + 512;
                    129:                w->ww_obp = w->ww_obq = w->ww_ob;
                    130:                if (w->ww_pty >= wwdtablesize)
                    131:                        wwdtablesize = w->ww_pty + 1;
                    132:        }
                    133:
                    134:        w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
                    135:                w->ww_w.nr, w->ww_w.nc, sizeof (char));
                    136:        if (w->ww_win == 0)
                    137:                goto bad;
                    138:        m = 0;
1.3     ! niklas    139:        if (oflags & WWO_GLASS)
1.1       deraadt   140:                m |= WWM_GLS;
1.3     ! niklas    141:        if (oflags & WWO_REVERSE)
1.1       deraadt   142:                if (wwavailmodes & WWM_REV)
                    143:                        m |= WWM_REV;
                    144:                else
1.3     ! niklas    145:                        oflags &= ~WWO_REVERSE;
1.1       deraadt   146:        for (i = w->ww_w.t; i < w->ww_w.b; i++)
                    147:                for (j = w->ww_w.l; j < w->ww_w.r; j++)
                    148:                        w->ww_win[i][j] = m;
                    149:
1.3     ! niklas    150:        if (oflags & WWO_FRAME) {
1.1       deraadt   151:                w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
                    152:                        w->ww_w.nr, w->ww_w.nc, sizeof (char));
                    153:                if (w->ww_fmap == 0)
                    154:                        goto bad;
                    155:                for (i = w->ww_w.t; i < w->ww_w.b; i++)
                    156:                        for (j = w->ww_w.l; j < w->ww_w.r; j++)
                    157:                                w->ww_fmap[i][j] = 0;
                    158:        }
                    159:
                    160:        w->ww_buf = (union ww_char **)
                    161:                wwalloc(w->ww_b.t, w->ww_b.l,
                    162:                        w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
                    163:        if (w->ww_buf == 0)
                    164:                goto bad;
                    165:        for (i = w->ww_b.t; i < w->ww_b.b; i++)
                    166:                for (j = w->ww_b.l; j < w->ww_b.r; j++)
                    167:                        w->ww_buf[i][j].c_w = ' ';
                    168:
                    169:        w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short));
                    170:        if (w->ww_nvis == 0) {
                    171:                wwerrno = WWE_NOMEM;
                    172:                goto bad;
                    173:        }
                    174:        w->ww_nvis -= w->ww_w.t;
                    175:        nvis = m ? 0 : w->ww_w.nc;
                    176:        for (i = w->ww_w.t; i < w->ww_w.b; i++)
                    177:                w->ww_nvis[i] = nvis;
                    178:
                    179:        w->ww_state = WWS_INITIAL;
1.3     ! niklas    180:        CLR(w->ww_oflags, WWO_ALLFLAGS);
        !           181:        SET(w->ww_oflags, oflags);
1.1       deraadt   182:        return wwindex[w->ww_index] = w;
                    183: bad:
                    184:        if (w != 0) {
                    185:                if (w->ww_win != 0)
                    186:                        wwfree(w->ww_win, w->ww_w.t);
                    187:                if (w->ww_fmap != 0)
                    188:                        wwfree(w->ww_fmap, w->ww_w.t);
                    189:                if (w->ww_buf != 0)
                    190:                        wwfree((char **)w->ww_buf, w->ww_b.t);
                    191:                if (w->ww_nvis != 0)
                    192:                        free((char *)(w->ww_nvis + w->ww_w.t));
                    193:                if (w->ww_ob != 0)
                    194:                        free(w->ww_ob);
                    195:                if (w->ww_pty >= 0)
                    196:                        (void) close(w->ww_pty);
                    197:                if (w->ww_socket >= 0)
                    198:                        (void) close(w->ww_socket);
                    199:                free((char *)w);
                    200:        }
                    201:        return 0;
                    202: }