home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / window / wwopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  5.1 KB  |  186 lines

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