BSD 4_4_Lite1 release
[unix-history] / usr / src / contrib / nvi.1.14 / sex / sex_screen.c
CommitLineData
ad787160 1/*-
ed554bc5 2 * Copyright (c) 1993, 1994
ad787160
C
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
ed554bc5 35static char sccsid[] = "@(#)sex_screen.c 8.40 (Berkeley) 4/14/94";
ad787160
C
36#endif /* not lint */
37
38#include <sys/types.h>
ed554bc5
C
39#include <sys/queue.h>
40#include <sys/time.h>
ad787160 41
ed554bc5
C
42#include <bitstring.h>
43#include <limits.h>
44#include <signal.h>
45#include <stdio.h>
ad787160 46#include <stdlib.h>
ed554bc5
C
47#include <string.h>
48#include <termios.h>
ad787160
C
49#include <unistd.h>
50
ed554bc5
C
51#include "compat.h"
52#include <db.h>
53#include <regex.h>
54
ad787160
C
55#include "vi.h"
56#include "excmd.h"
ad787160 57#include "sex_screen.h"
ed554bc5 58#include "../svi/svi_screen.h"
ad787160
C
59
60static void sex_abort __P((void));
ad787160 61static int sex_noop __P((void));
ed554bc5
C
62static int sex_nope __P((SCR *));
63
64/*
65 * sex_screen_init --
66 * Initialize the ex screen.
67 */
68int
69sex_screen_init(sp)
70 SCR *sp;
71{
72 /* Initialize support routines. */
73 sp->s_bell = sex_bell;
74 sp->s_bg = (int (*)())sex_nope;
75 sp->s_busy = (int (*)())sex_busy;
76 sp->s_change = (int (*)())sex_noop;
77 sp->s_clear = (int (*)())sex_noop;
78 sp->s_colpos = (size_t (*)())sex_abort;
79 sp->s_column = (int (*)())sex_abort;
80 sp->s_confirm = sex_confirm;
81 sp->s_crel = (int (*)())sex_nope;
82 sp->s_down = (int (*)())sex_abort;
83 sp->s_edit = sex_screen_edit;
84 sp->s_end = (int (*)())sex_noop;
85 sp->s_ex_cmd = (int (*)())sex_abort;
86 sp->s_ex_run = (int (*)())sex_abort;
87 sp->s_ex_write = (int (*)())sex_abort;
88 sp->s_fg = (int (*)())sex_nope;
89 sp->s_fill = (int (*)())sex_abort;
90 sp->s_get = sex_get;
91 sp->s_key_read = sex_key_read;
92 sp->s_optchange = (int (*)())sex_noop;
93 sp->s_position = (int (*)())sex_abort;
94 sp->s_rabs = (int (*)())sex_nope;
95 sp->s_rcm = (size_t (*)())sex_abort;
96 sp->s_refresh = sex_refresh;
97 sp->s_split = (int (*)())sex_nope;
98 sp->s_suspend = sex_suspend;
99 sp->s_up = (int (*)())sex_abort;
100
101 return (0);
102}
103
104/*
105 * sex_screen_copy --
106 * Copy to a new screen.
107 */
108int
109sex_screen_copy(orig, sp)
110 SCR *orig, *sp;
111{
112 SEX_PRIVATE *osex, *nsex;
113
114 /* Create the private screen structure. */
115 CALLOC_RET(orig, nsex, SEX_PRIVATE *, 1, sizeof(SEX_PRIVATE));
116 sp->sex_private = nsex;
117
118/* INITIALIZED AT SCREEN CREATE. */
119
120/* PARTIALLY OR COMPLETELY COPIED FROM PREVIOUS SCREEN. */
121 if (orig == NULL) {
122 } else {
123 osex = SXP(orig);
124 if (osex->SE != NULL && (nsex->SE = strdup(osex->SE)) == NULL) {
125 msgq(sp, M_SYSERR, NULL);
126 return (1);
127 }
128 if (osex->SO != NULL && (nsex->SO = strdup(osex->SO)) == NULL) {
129 msgq(sp, M_SYSERR, NULL);
130 free(osex->SE);
131 return (1);
132 }
133 F_SET(nsex, F_ISSET(osex, SEX_NO_INVERSE));
134 }
135
136 return (0);
137}
138
139/*
140 * sex_screen_end --
141 * End a screen.
142 */
143int
144sex_screen_end(sp)
145 SCR *sp;
146{
147 /* Free inverse video strings. */
148 if (SXP(sp)->SE != NULL)
149 free(SXP(sp)->SE);
150 if (SXP(sp)->SO != NULL)
151 free(SXP(sp)->SO);
152
153 /* Free private memory. */
154 FREE(SXP(sp), sizeof(SEX_PRIVATE));
155 sp->sex_private = NULL;
156
157 return (0);
158}
ad787160
C
159
160/*
ed554bc5 161 * sex_screen_edit --
ad787160
C
162 * Main ex screen loop. The ex screen is relatively uncomplicated.
163 * As long as it has a stdio FILE pointer for output, it's happy.
164 */
165int
ed554bc5
C
166sex_screen_edit(sp, ep)
167 SCR *sp;
ad787160
C
168 EXF *ep;
169{
ed554bc5
C
170 struct termios t;
171 GS *gp;
172 int force, rval;
ad787160
C
173
174 /* Initialize the terminal state. */
ed554bc5
C
175 gp = sp->gp;
176 if (F_ISSET(gp, G_STDIN_TTY))
177 SEX_RAW(t);
ad787160
C
178
179 /* Write to the terminal. */
180 sp->stdfp = stdout;
181
ed554bc5 182 for (;;) {
ad787160
C
183 sp->rows = O_VAL(sp, O_LINES);
184 sp->cols = O_VAL(sp, O_COLUMNS);
185
ed554bc5
C
186 /*
187 * Run ex. If ex fails, sex data structures
188 * may be corrupted, be careful what you do.
189 */
190 if (rval = ex(sp, sp->ep)) {
ad787160
C
191 if (F_ISSET(ep, F_RCV_ON)) {
192 F_SET(ep, F_RCV_NORM);
193 (void)rcv_sync(sp, sp->ep);
194 }
ed554bc5
C
195 (void)file_end(sp, sp->ep, 1);
196 (void)screen_end(sp); /* General SCR info. */
197 break;
ad787160
C
198 }
199
ed554bc5 200 force = 0;
ad787160 201 switch (F_ISSET(sp, S_MAJOR_CHANGE)) {
ad787160 202 case S_EXIT_FORCE:
ed554bc5
C
203 force = 1;
204 /* FALLTHROUGH */
205 case S_EXIT:
206 F_CLR(sp, S_EXIT_FORCE | S_EXIT);
207 if (file_end(sp, sp->ep, force))
ad787160 208 break;
ed554bc5
C
209 (void)screen_end(sp); /* General SCR info. */
210 goto ret;
211 case 0: /* Changing from ex mode. */
212 goto ret;
ad787160
C
213 case S_FSWITCH:
214 F_CLR(sp, S_FSWITCH);
ad787160 215 break;
ed554bc5 216 case S_SSWITCH:
ad787160
C
217 default:
218 abort();
219 }
ed554bc5 220 }
ad787160
C
221
222 /* Reset the terminal state. */
ed554bc5
C
223ret: if (F_ISSET(gp, G_STDIN_TTY) && SEX_NORAW(t))
224 rval = 1;
225 return (rval);
ad787160
C
226}
227
228/*
229 * sex_abort --
230 * Fake function. Die.
231 */
232static void
233sex_abort()
234{
235 abort();
236}
237
238/*
239 * sex_noop --
240 * Fake function. Do nothing.
241 */
242static int
243sex_noop()
244{
245 return (0);
246}
ed554bc5
C
247
248/*
249 * sex_nope --
250 * Fake function. Not in ex, you don't.
251 */
252static int
253sex_nope(sp)
254 SCR *sp;
255{
256 msgq(sp, M_ERR, "Command not applicable to ex mode.");
257 return (1);
258}