]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/wwopen.c
Include a missing header, and change a mktemp(3), mkdir(2) sequence to
[FreeBSD/FreeBSD.git] / usr.bin / window / wwopen.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  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    8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "ww.h"
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <fcntl.h>
46 #include <stdlib.h>
47
48 struct ww *
49 wwopen(flags, nrow, ncol, row, col, nline)
50 {
51         register struct ww *w;
52         register i, j;
53         char m;
54         short nvis;
55
56         w = (struct ww *)calloc(sizeof (struct ww), 1);
57         if (w == 0) {
58                 wwerrno = WWE_NOMEM;
59                 goto bad;
60         }
61         w->ww_pty = -1;
62         w->ww_socket = -1;
63
64         for (i = 0; i < NWW && wwindex[i] != 0; i++)
65                 ;
66         if (i >= NWW) {
67                 wwerrno = WWE_TOOMANY;
68                 goto bad;
69         }
70         w->ww_index = i;
71
72         if (nline < nrow)
73                 nline = nrow;
74
75         w->ww_w.t = row;
76         w->ww_w.b = row + nrow;
77         w->ww_w.l = col;
78         w->ww_w.r = col + ncol;
79         w->ww_w.nr = nrow;
80         w->ww_w.nc = ncol;
81
82         w->ww_b.t = row;
83         w->ww_b.b = row + nline;
84         w->ww_b.l = col;
85         w->ww_b.r = col + ncol;
86         w->ww_b.nr = nline;
87         w->ww_b.nc = ncol;
88
89         w->ww_i.t = MAX(w->ww_w.t, 0);
90         w->ww_i.b = MIN(w->ww_w.b, wwnrow);
91         w->ww_i.l = MAX(w->ww_w.l, 0);
92         w->ww_i.r = MIN(w->ww_w.r, wwncol);
93         w->ww_i.nr = w->ww_i.b - w->ww_i.t;
94         w->ww_i.nc = w->ww_i.r - w->ww_i.l;
95
96         w->ww_cur.r = w->ww_w.t;
97         w->ww_cur.c = w->ww_w.l;
98
99         if (flags & WWO_PTY) {
100                 if (wwgetpty(w) < 0)
101                         goto bad;
102                 w->ww_ispty = 1;
103         } else if (flags & WWO_SOCKET) {
104                 int d[2];
105                 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, d) < 0) {
106                         wwerrno = WWE_SYS;
107                         goto bad;
108                 }
109                 (void) fcntl(d[0], F_SETFD, 1);
110                 (void) fcntl(d[1], F_SETFD, 1);
111                 w->ww_pty = d[0];
112                 w->ww_socket = d[1];
113         }
114         if (flags & (WWO_PTY|WWO_SOCKET)) {
115                 if ((w->ww_ob = malloc(512)) == 0) {
116                         wwerrno = WWE_NOMEM;
117                         goto bad;
118                 }
119                 w->ww_obe = w->ww_ob + 512;
120                 w->ww_obp = w->ww_obq = w->ww_ob;
121         }
122
123         w->ww_win = wwalloc(w->ww_w.t, w->ww_w.l,
124                 w->ww_w.nr, w->ww_w.nc, sizeof (char));
125         if (w->ww_win == 0)
126                 goto bad;
127         m = 0;
128         if (flags & WWO_GLASS)
129                 m |= WWM_GLS;
130         if (flags & WWO_REVERSE)
131                 if (wwavailmodes & WWM_REV)
132                         m |= WWM_REV;
133                 else
134                         flags &= ~WWO_REVERSE;
135         for (i = w->ww_w.t; i < w->ww_w.b; i++)
136                 for (j = w->ww_w.l; j < w->ww_w.r; j++)
137                         w->ww_win[i][j] = m;
138
139         if (flags & WWO_FRAME) {
140                 w->ww_fmap = wwalloc(w->ww_w.t, w->ww_w.l,
141                         w->ww_w.nr, w->ww_w.nc, sizeof (char));
142                 if (w->ww_fmap == 0)
143                         goto bad;
144                 for (i = w->ww_w.t; i < w->ww_w.b; i++)
145                         for (j = w->ww_w.l; j < w->ww_w.r; j++)
146                                 w->ww_fmap[i][j] = 0;
147         }
148
149         w->ww_buf = (union ww_char **)
150                 wwalloc(w->ww_b.t, w->ww_b.l,
151                         w->ww_b.nr, w->ww_b.nc, sizeof (union ww_char));
152         if (w->ww_buf == 0)
153                 goto bad;
154         for (i = w->ww_b.t; i < w->ww_b.b; i++)
155                 for (j = w->ww_b.l; j < w->ww_b.r; j++)
156                         w->ww_buf[i][j].c_w = ' ';
157
158         w->ww_nvis = (short *)malloc((unsigned) w->ww_w.nr * sizeof (short));
159         if (w->ww_nvis == 0) {
160                 wwerrno = WWE_NOMEM;
161                 goto bad;
162         }
163         w->ww_nvis -= w->ww_w.t;
164         nvis = m ? 0 : w->ww_w.nc;
165         for (i = w->ww_w.t; i < w->ww_w.b; i++)
166                 w->ww_nvis[i] = nvis;
167
168         w->ww_state = WWS_INITIAL;
169         w->ww_oflags = flags;
170         return wwindex[w->ww_index] = w;
171 bad:
172         if (w != 0) {
173                 if (w->ww_win != 0)
174                         wwfree(w->ww_win, w->ww_w.t);
175                 if (w->ww_fmap != 0)
176                         wwfree(w->ww_fmap, w->ww_w.t);
177                 if (w->ww_buf != 0)
178                         wwfree((char **)w->ww_buf, w->ww_b.t);
179                 if (w->ww_nvis != 0)
180                         free((char *)(w->ww_nvis + w->ww_w.t));
181                 if (w->ww_ob != 0)
182                         free(w->ww_ob);
183                 if (w->ww_pty >= 0)
184                         (void) close(w->ww_pty);
185                 if (w->ww_socket >= 0)
186                         (void) close(w->ww_socket);
187                 free((char *)w);
188         }
189         return 0;
190 }