1*e0b8e63eSJohn Marino /*-
2*e0b8e63eSJohn Marino * Copyright (c) 1993, 1994
3*e0b8e63eSJohn Marino * The Regents of the University of California. All rights reserved.
4*e0b8e63eSJohn Marino * Copyright (c) 1993, 1994, 1995, 1996
5*e0b8e63eSJohn Marino * Keith Bostic. All rights reserved.
6*e0b8e63eSJohn Marino *
7*e0b8e63eSJohn Marino * See the LICENSE file for redistribution information.
8*e0b8e63eSJohn Marino */
9*e0b8e63eSJohn Marino
10*e0b8e63eSJohn Marino #include "config.h"
11*e0b8e63eSJohn Marino
12*e0b8e63eSJohn Marino #include <sys/types.h>
13*e0b8e63eSJohn Marino #include <sys/queue.h>
14*e0b8e63eSJohn Marino #include <sys/time.h>
15*e0b8e63eSJohn Marino
16*e0b8e63eSJohn Marino #include <bitstring.h>
17*e0b8e63eSJohn Marino #include <ctype.h>
18*e0b8e63eSJohn Marino #include <signal.h>
19*e0b8e63eSJohn Marino #include <stdio.h>
20*e0b8e63eSJohn Marino #include <stdlib.h>
21*e0b8e63eSJohn Marino #include <string.h>
22*e0b8e63eSJohn Marino #ifdef HAVE_TERM_H
23*e0b8e63eSJohn Marino #include <term.h>
24*e0b8e63eSJohn Marino #endif
25*e0b8e63eSJohn Marino #include <termios.h>
26*e0b8e63eSJohn Marino #include <unistd.h>
27*e0b8e63eSJohn Marino
28*e0b8e63eSJohn Marino #include "../common/common.h"
29*e0b8e63eSJohn Marino #include "../vi/vi.h"
30*e0b8e63eSJohn Marino #include "cl.h"
31*e0b8e63eSJohn Marino
32*e0b8e63eSJohn Marino static void cl_rdiv(SCR *);
33*e0b8e63eSJohn Marino
34*e0b8e63eSJohn Marino static int
addstr4(SCR * sp,void * str,size_t len,int wide)35*e0b8e63eSJohn Marino addstr4(SCR *sp, void *str, size_t len, int wide)
36*e0b8e63eSJohn Marino {
37*e0b8e63eSJohn Marino CL_PRIVATE *clp;
38*e0b8e63eSJohn Marino WINDOW *win;
39*e0b8e63eSJohn Marino size_t y, x;
40*e0b8e63eSJohn Marino int iv;
41*e0b8e63eSJohn Marino
42*e0b8e63eSJohn Marino clp = CLP(sp);
43*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
44*e0b8e63eSJohn Marino
45*e0b8e63eSJohn Marino /*
46*e0b8e63eSJohn Marino * If ex isn't in control, it's the last line of the screen and
47*e0b8e63eSJohn Marino * it's a split screen, use inverse video.
48*e0b8e63eSJohn Marino */
49*e0b8e63eSJohn Marino iv = 0;
50*e0b8e63eSJohn Marino getyx(win, y, x);
51*e0b8e63eSJohn Marino if (!F_ISSET(sp, SC_SCR_EXWROTE) &&
52*e0b8e63eSJohn Marino y == RLNO(sp, LASTLINE(sp)) && IS_SPLIT(sp)) {
53*e0b8e63eSJohn Marino iv = 1;
54*e0b8e63eSJohn Marino (void)wstandout(win);
55*e0b8e63eSJohn Marino }
56*e0b8e63eSJohn Marino
57*e0b8e63eSJohn Marino #ifdef USE_WIDECHAR
58*e0b8e63eSJohn Marino if (wide) {
59*e0b8e63eSJohn Marino if (waddnwstr(win, str, len) == ERR)
60*e0b8e63eSJohn Marino return (1);
61*e0b8e63eSJohn Marino } else
62*e0b8e63eSJohn Marino #endif
63*e0b8e63eSJohn Marino if (waddnstr(win, str, len) == ERR)
64*e0b8e63eSJohn Marino return (1);
65*e0b8e63eSJohn Marino
66*e0b8e63eSJohn Marino if (iv)
67*e0b8e63eSJohn Marino (void)wstandend(win);
68*e0b8e63eSJohn Marino return (0);
69*e0b8e63eSJohn Marino }
70*e0b8e63eSJohn Marino
71*e0b8e63eSJohn Marino /*
72*e0b8e63eSJohn Marino * cl_waddstr --
73*e0b8e63eSJohn Marino * Add len bytes from the string at the cursor, advancing the cursor.
74*e0b8e63eSJohn Marino *
75*e0b8e63eSJohn Marino * PUBLIC: int cl_waddstr(SCR *, const CHAR_T *, size_t);
76*e0b8e63eSJohn Marino */
77*e0b8e63eSJohn Marino int
cl_waddstr(SCR * sp,const CHAR_T * str,size_t len)78*e0b8e63eSJohn Marino cl_waddstr(SCR *sp, const CHAR_T *str, size_t len)
79*e0b8e63eSJohn Marino {
80*e0b8e63eSJohn Marino return addstr4(sp, (void *)str, len, 1);
81*e0b8e63eSJohn Marino }
82*e0b8e63eSJohn Marino
83*e0b8e63eSJohn Marino /*
84*e0b8e63eSJohn Marino * cl_addstr --
85*e0b8e63eSJohn Marino * Add len bytes from the string at the cursor, advancing the cursor.
86*e0b8e63eSJohn Marino *
87*e0b8e63eSJohn Marino * PUBLIC: int cl_addstr(SCR *, const char *, size_t);
88*e0b8e63eSJohn Marino */
89*e0b8e63eSJohn Marino int
cl_addstr(SCR * sp,const char * str,size_t len)90*e0b8e63eSJohn Marino cl_addstr(SCR *sp, const char *str, size_t len)
91*e0b8e63eSJohn Marino {
92*e0b8e63eSJohn Marino return addstr4(sp, (void *)str, len, 0);
93*e0b8e63eSJohn Marino }
94*e0b8e63eSJohn Marino
95*e0b8e63eSJohn Marino /*
96*e0b8e63eSJohn Marino * cl_attr --
97*e0b8e63eSJohn Marino * Toggle a screen attribute on/off.
98*e0b8e63eSJohn Marino *
99*e0b8e63eSJohn Marino * PUBLIC: int cl_attr(SCR *, scr_attr_t, int);
100*e0b8e63eSJohn Marino */
101*e0b8e63eSJohn Marino int
cl_attr(SCR * sp,scr_attr_t attribute,int on)102*e0b8e63eSJohn Marino cl_attr(SCR *sp, scr_attr_t attribute, int on)
103*e0b8e63eSJohn Marino {
104*e0b8e63eSJohn Marino CL_PRIVATE *clp;
105*e0b8e63eSJohn Marino WINDOW *win;
106*e0b8e63eSJohn Marino
107*e0b8e63eSJohn Marino clp = CLP(sp);
108*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
109*e0b8e63eSJohn Marino
110*e0b8e63eSJohn Marino switch (attribute) {
111*e0b8e63eSJohn Marino case SA_ALTERNATE:
112*e0b8e63eSJohn Marino /*
113*e0b8e63eSJohn Marino * !!!
114*e0b8e63eSJohn Marino * There's a major layering violation here. The problem is that the
115*e0b8e63eSJohn Marino * X11 xterm screen has what's known as an "alternate" screen. Some
116*e0b8e63eSJohn Marino * xterm termcap/terminfo entries include sequences to switch to/from
117*e0b8e63eSJohn Marino * that alternate screen as part of the ti/te (smcup/rmcup) strings.
118*e0b8e63eSJohn Marino * Vi runs in the alternate screen, so that you are returned to the
119*e0b8e63eSJohn Marino * same screen contents on exit from vi that you had when you entered
120*e0b8e63eSJohn Marino * vi. Further, when you run :shell, or :!date or similar ex commands,
121*e0b8e63eSJohn Marino * you also see the original screen contents. This wasn't deliberate
122*e0b8e63eSJohn Marino * on vi's part, it's just that it historically sent terminal init/end
123*e0b8e63eSJohn Marino * sequences at those times, and the addition of the alternate screen
124*e0b8e63eSJohn Marino * sequences to the strings changed the behavior of vi. The problem
125*e0b8e63eSJohn Marino * caused by this is that we don't want to switch back to the alternate
126*e0b8e63eSJohn Marino * screen while getting a new command from the user, when the user is
127*e0b8e63eSJohn Marino * continuing to enter ex commands, e.g.:
128*e0b8e63eSJohn Marino *
129*e0b8e63eSJohn Marino * :!date <<< switch to original screen
130*e0b8e63eSJohn Marino * [Hit return to continue] <<< prompt user to continue
131*e0b8e63eSJohn Marino * :command <<< get command from user
132*e0b8e63eSJohn Marino *
133*e0b8e63eSJohn Marino * Note that the :command input is a true vi input mode, e.g., input
134*e0b8e63eSJohn Marino * maps and abbreviations are being done. So, we need to be able to
135*e0b8e63eSJohn Marino * switch back into the vi screen mode, without flashing the screen.
136*e0b8e63eSJohn Marino *
137*e0b8e63eSJohn Marino * To make matters worse, the curses initscr() and endwin() calls will
138*e0b8e63eSJohn Marino * do this automatically -- so, this attribute isn't as controlled by
139*e0b8e63eSJohn Marino * the higher level screen as closely as one might like.
140*e0b8e63eSJohn Marino */
141*e0b8e63eSJohn Marino if (on) {
142*e0b8e63eSJohn Marino if (clp->ti_te != TI_SENT) {
143*e0b8e63eSJohn Marino clp->ti_te = TI_SENT;
144*e0b8e63eSJohn Marino if (clp->smcup == NULL)
145*e0b8e63eSJohn Marino (void)cl_getcap(sp, "smcup", &clp->smcup);
146*e0b8e63eSJohn Marino if (clp->smcup != NULL)
147*e0b8e63eSJohn Marino (void)tputs(clp->smcup, 1, cl_putchar);
148*e0b8e63eSJohn Marino }
149*e0b8e63eSJohn Marino } else
150*e0b8e63eSJohn Marino if (clp->ti_te != TE_SENT) {
151*e0b8e63eSJohn Marino clp->ti_te = TE_SENT;
152*e0b8e63eSJohn Marino if (clp->rmcup == NULL)
153*e0b8e63eSJohn Marino (void)cl_getcap(sp, "rmcup", &clp->rmcup);
154*e0b8e63eSJohn Marino if (clp->rmcup != NULL)
155*e0b8e63eSJohn Marino (void)tputs(clp->rmcup, 1, cl_putchar);
156*e0b8e63eSJohn Marino (void)fflush(stdout);
157*e0b8e63eSJohn Marino }
158*e0b8e63eSJohn Marino (void)fflush(stdout);
159*e0b8e63eSJohn Marino break;
160*e0b8e63eSJohn Marino case SA_INVERSE:
161*e0b8e63eSJohn Marino if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE)) {
162*e0b8e63eSJohn Marino if (clp->smso == NULL)
163*e0b8e63eSJohn Marino return (1);
164*e0b8e63eSJohn Marino if (on)
165*e0b8e63eSJohn Marino (void)tputs(clp->smso, 1, cl_putchar);
166*e0b8e63eSJohn Marino else
167*e0b8e63eSJohn Marino (void)tputs(clp->rmso, 1, cl_putchar);
168*e0b8e63eSJohn Marino (void)fflush(stdout);
169*e0b8e63eSJohn Marino } else {
170*e0b8e63eSJohn Marino if (on)
171*e0b8e63eSJohn Marino (void)wstandout(win);
172*e0b8e63eSJohn Marino else
173*e0b8e63eSJohn Marino (void)wstandend(win);
174*e0b8e63eSJohn Marino }
175*e0b8e63eSJohn Marino break;
176*e0b8e63eSJohn Marino default:
177*e0b8e63eSJohn Marino abort();
178*e0b8e63eSJohn Marino }
179*e0b8e63eSJohn Marino return (0);
180*e0b8e63eSJohn Marino }
181*e0b8e63eSJohn Marino
182*e0b8e63eSJohn Marino /*
183*e0b8e63eSJohn Marino * cl_baud --
184*e0b8e63eSJohn Marino * Return the baud rate.
185*e0b8e63eSJohn Marino *
186*e0b8e63eSJohn Marino * PUBLIC: int cl_baud(SCR *, u_long *);
187*e0b8e63eSJohn Marino */
188*e0b8e63eSJohn Marino int
cl_baud(SCR * sp,u_long * ratep)189*e0b8e63eSJohn Marino cl_baud(SCR *sp, u_long *ratep)
190*e0b8e63eSJohn Marino {
191*e0b8e63eSJohn Marino CL_PRIVATE *clp;
192*e0b8e63eSJohn Marino
193*e0b8e63eSJohn Marino /*
194*e0b8e63eSJohn Marino * XXX
195*e0b8e63eSJohn Marino * There's no portable way to get a "baud rate" -- cfgetospeed(3)
196*e0b8e63eSJohn Marino * returns the value associated with some #define, which we may
197*e0b8e63eSJohn Marino * never have heard of, or which may be a purely local speed. Vi
198*e0b8e63eSJohn Marino * only cares if it's SLOW (w300), slow (w1200) or fast (w9600).
199*e0b8e63eSJohn Marino * Try and detect the slow ones, and default to fast.
200*e0b8e63eSJohn Marino */
201*e0b8e63eSJohn Marino clp = CLP(sp);
202*e0b8e63eSJohn Marino switch (cfgetospeed(&clp->orig)) {
203*e0b8e63eSJohn Marino case B50:
204*e0b8e63eSJohn Marino case B75:
205*e0b8e63eSJohn Marino case B110:
206*e0b8e63eSJohn Marino case B134:
207*e0b8e63eSJohn Marino case B150:
208*e0b8e63eSJohn Marino case B200:
209*e0b8e63eSJohn Marino case B300:
210*e0b8e63eSJohn Marino case B600:
211*e0b8e63eSJohn Marino *ratep = 600;
212*e0b8e63eSJohn Marino break;
213*e0b8e63eSJohn Marino case B1200:
214*e0b8e63eSJohn Marino *ratep = 1200;
215*e0b8e63eSJohn Marino break;
216*e0b8e63eSJohn Marino default:
217*e0b8e63eSJohn Marino *ratep = 9600;
218*e0b8e63eSJohn Marino break;
219*e0b8e63eSJohn Marino }
220*e0b8e63eSJohn Marino return (0);
221*e0b8e63eSJohn Marino }
222*e0b8e63eSJohn Marino
223*e0b8e63eSJohn Marino /*
224*e0b8e63eSJohn Marino * cl_bell --
225*e0b8e63eSJohn Marino * Ring the bell/flash the screen.
226*e0b8e63eSJohn Marino *
227*e0b8e63eSJohn Marino * PUBLIC: int cl_bell(SCR *);
228*e0b8e63eSJohn Marino */
229*e0b8e63eSJohn Marino int
cl_bell(SCR * sp)230*e0b8e63eSJohn Marino cl_bell(SCR *sp)
231*e0b8e63eSJohn Marino {
232*e0b8e63eSJohn Marino if (F_ISSET(sp, SC_EX | SC_SCR_EXWROTE | SC_SCR_EX))
233*e0b8e63eSJohn Marino (void)write(STDOUT_FILENO, "\07", 1); /* \a */
234*e0b8e63eSJohn Marino else {
235*e0b8e63eSJohn Marino /*
236*e0b8e63eSJohn Marino * Vi has an edit option which determines if the terminal
237*e0b8e63eSJohn Marino * should be beeped or the screen flashed.
238*e0b8e63eSJohn Marino */
239*e0b8e63eSJohn Marino if (O_ISSET(sp, O_FLASH))
240*e0b8e63eSJohn Marino (void)flash();
241*e0b8e63eSJohn Marino else
242*e0b8e63eSJohn Marino (void)beep();
243*e0b8e63eSJohn Marino }
244*e0b8e63eSJohn Marino return (0);
245*e0b8e63eSJohn Marino }
246*e0b8e63eSJohn Marino
247*e0b8e63eSJohn Marino /*
248*e0b8e63eSJohn Marino * cl_clrtoeol --
249*e0b8e63eSJohn Marino * Clear from the current cursor to the end of the line.
250*e0b8e63eSJohn Marino *
251*e0b8e63eSJohn Marino * PUBLIC: int cl_clrtoeol(SCR *);
252*e0b8e63eSJohn Marino */
253*e0b8e63eSJohn Marino int
cl_clrtoeol(SCR * sp)254*e0b8e63eSJohn Marino cl_clrtoeol(SCR *sp)
255*e0b8e63eSJohn Marino {
256*e0b8e63eSJohn Marino WINDOW *win;
257*e0b8e63eSJohn Marino #if 0
258*e0b8e63eSJohn Marino size_t spcnt, y, x;
259*e0b8e63eSJohn Marino #endif
260*e0b8e63eSJohn Marino
261*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
262*e0b8e63eSJohn Marino
263*e0b8e63eSJohn Marino #if 0
264*e0b8e63eSJohn Marino if (IS_VSPLIT(sp)) {
265*e0b8e63eSJohn Marino /* The cursor must be returned to its original position. */
266*e0b8e63eSJohn Marino getyx(win, y, x);
267*e0b8e63eSJohn Marino for (spcnt = (sp->coff + sp->cols) - x; spcnt > 0; --spcnt)
268*e0b8e63eSJohn Marino (void)waddch(win, ' ');
269*e0b8e63eSJohn Marino (void)wmove(win, y, x);
270*e0b8e63eSJohn Marino return (0);
271*e0b8e63eSJohn Marino } else
272*e0b8e63eSJohn Marino #endif
273*e0b8e63eSJohn Marino return (wclrtoeol(win) == ERR);
274*e0b8e63eSJohn Marino }
275*e0b8e63eSJohn Marino
276*e0b8e63eSJohn Marino /*
277*e0b8e63eSJohn Marino * cl_cursor --
278*e0b8e63eSJohn Marino * Return the current cursor position.
279*e0b8e63eSJohn Marino *
280*e0b8e63eSJohn Marino * PUBLIC: int cl_cursor(SCR *, size_t *, size_t *);
281*e0b8e63eSJohn Marino */
282*e0b8e63eSJohn Marino int
cl_cursor(SCR * sp,size_t * yp,size_t * xp)283*e0b8e63eSJohn Marino cl_cursor(SCR *sp, size_t *yp, size_t *xp)
284*e0b8e63eSJohn Marino {
285*e0b8e63eSJohn Marino WINDOW *win;
286*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
287*e0b8e63eSJohn Marino /*
288*e0b8e63eSJohn Marino * The curses screen support splits a single underlying curses screen
289*e0b8e63eSJohn Marino * into multiple screens to support split screen semantics. For this
290*e0b8e63eSJohn Marino * reason the returned value must be adjusted to be relative to the
291*e0b8e63eSJohn Marino * current screen, and not absolute. Screens that implement the split
292*e0b8e63eSJohn Marino * using physically distinct screens won't need this hack.
293*e0b8e63eSJohn Marino */
294*e0b8e63eSJohn Marino getyx(win, *yp, *xp);
295*e0b8e63eSJohn Marino /*
296*e0b8e63eSJohn Marino *yp -= sp->roff;
297*e0b8e63eSJohn Marino *xp -= sp->coff;
298*e0b8e63eSJohn Marino */
299*e0b8e63eSJohn Marino return (0);
300*e0b8e63eSJohn Marino }
301*e0b8e63eSJohn Marino
302*e0b8e63eSJohn Marino /*
303*e0b8e63eSJohn Marino * cl_deleteln --
304*e0b8e63eSJohn Marino * Delete the current line, scrolling all lines below it.
305*e0b8e63eSJohn Marino *
306*e0b8e63eSJohn Marino * PUBLIC: int cl_deleteln(SCR *);
307*e0b8e63eSJohn Marino */
308*e0b8e63eSJohn Marino int
cl_deleteln(SCR * sp)309*e0b8e63eSJohn Marino cl_deleteln(SCR *sp)
310*e0b8e63eSJohn Marino {
311*e0b8e63eSJohn Marino CL_PRIVATE *clp;
312*e0b8e63eSJohn Marino WINDOW *win;
313*e0b8e63eSJohn Marino size_t y, x;
314*e0b8e63eSJohn Marino
315*e0b8e63eSJohn Marino clp = CLP(sp);
316*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
317*e0b8e63eSJohn Marino
318*e0b8e63eSJohn Marino /*
319*e0b8e63eSJohn Marino * This clause is required because the curses screen uses reverse
320*e0b8e63eSJohn Marino * video to delimit split screens. If the screen does not do this,
321*e0b8e63eSJohn Marino * this code won't be necessary.
322*e0b8e63eSJohn Marino *
323*e0b8e63eSJohn Marino * If the bottom line was in reverse video, rewrite it in normal
324*e0b8e63eSJohn Marino * video before it's scrolled.
325*e0b8e63eSJohn Marino */
326*e0b8e63eSJohn Marino if (!F_ISSET(sp, SC_SCR_EXWROTE) && IS_SPLIT(sp)) {
327*e0b8e63eSJohn Marino getyx(win, y, x);
328*e0b8e63eSJohn Marino mvwchgat(win, RLNO(sp, LASTLINE(sp)), 0, -1, A_NORMAL, 0, NULL);
329*e0b8e63eSJohn Marino (void)wmove(win, y, x);
330*e0b8e63eSJohn Marino }
331*e0b8e63eSJohn Marino
332*e0b8e63eSJohn Marino /*
333*e0b8e63eSJohn Marino * The bottom line is expected to be blank after this operation,
334*e0b8e63eSJohn Marino * and other screens must support that semantic.
335*e0b8e63eSJohn Marino */
336*e0b8e63eSJohn Marino return (wdeleteln(win) == ERR);
337*e0b8e63eSJohn Marino }
338*e0b8e63eSJohn Marino
339*e0b8e63eSJohn Marino /*
340*e0b8e63eSJohn Marino * cl_discard --
341*e0b8e63eSJohn Marino * Discard a screen.
342*e0b8e63eSJohn Marino *
343*e0b8e63eSJohn Marino * PUBLIC: int cl_discard(SCR *, SCR **);
344*e0b8e63eSJohn Marino */
345*e0b8e63eSJohn Marino int
cl_discard(SCR * discardp,SCR ** acquirep)346*e0b8e63eSJohn Marino cl_discard(SCR *discardp, SCR **acquirep)
347*e0b8e63eSJohn Marino {
348*e0b8e63eSJohn Marino CL_PRIVATE *clp;
349*e0b8e63eSJohn Marino SCR* tsp;
350*e0b8e63eSJohn Marino
351*e0b8e63eSJohn Marino if (discardp) {
352*e0b8e63eSJohn Marino clp = CLP(discardp);
353*e0b8e63eSJohn Marino F_SET(clp, CL_LAYOUT);
354*e0b8e63eSJohn Marino
355*e0b8e63eSJohn Marino if (CLSP(discardp)) {
356*e0b8e63eSJohn Marino delwin(CLSP(discardp));
357*e0b8e63eSJohn Marino discardp->cl_private = NULL;
358*e0b8e63eSJohn Marino }
359*e0b8e63eSJohn Marino }
360*e0b8e63eSJohn Marino
361*e0b8e63eSJohn Marino /* no screens got a piece; we're done */
362*e0b8e63eSJohn Marino if (!acquirep)
363*e0b8e63eSJohn Marino return 0;
364*e0b8e63eSJohn Marino
365*e0b8e63eSJohn Marino for (; (tsp = *acquirep) != NULL; ++acquirep) {
366*e0b8e63eSJohn Marino clp = CLP(tsp);
367*e0b8e63eSJohn Marino F_SET(clp, CL_LAYOUT);
368*e0b8e63eSJohn Marino
369*e0b8e63eSJohn Marino if (CLSP(tsp))
370*e0b8e63eSJohn Marino delwin(CLSP(tsp));
371*e0b8e63eSJohn Marino tsp->cl_private = subwin(stdscr, tsp->rows, tsp->cols,
372*e0b8e63eSJohn Marino tsp->roff, tsp->coff);
373*e0b8e63eSJohn Marino }
374*e0b8e63eSJohn Marino
375*e0b8e63eSJohn Marino /* discardp is going away, acquirep is taking up its space. */
376*e0b8e63eSJohn Marino return (0);
377*e0b8e63eSJohn Marino }
378*e0b8e63eSJohn Marino
379*e0b8e63eSJohn Marino /*
380*e0b8e63eSJohn Marino * cl_ex_adjust --
381*e0b8e63eSJohn Marino * Adjust the screen for ex. This routine is purely for standalone
382*e0b8e63eSJohn Marino * ex programs. All special purpose, all special case.
383*e0b8e63eSJohn Marino *
384*e0b8e63eSJohn Marino * PUBLIC: int cl_ex_adjust(SCR *, exadj_t);
385*e0b8e63eSJohn Marino */
386*e0b8e63eSJohn Marino int
cl_ex_adjust(SCR * sp,exadj_t action)387*e0b8e63eSJohn Marino cl_ex_adjust(SCR *sp, exadj_t action)
388*e0b8e63eSJohn Marino {
389*e0b8e63eSJohn Marino CL_PRIVATE *clp;
390*e0b8e63eSJohn Marino int cnt;
391*e0b8e63eSJohn Marino
392*e0b8e63eSJohn Marino clp = CLP(sp);
393*e0b8e63eSJohn Marino switch (action) {
394*e0b8e63eSJohn Marino case EX_TERM_SCROLL:
395*e0b8e63eSJohn Marino /* Move the cursor up one line if that's possible. */
396*e0b8e63eSJohn Marino if (clp->cuu1 != NULL)
397*e0b8e63eSJohn Marino (void)tputs(clp->cuu1, 1, cl_putchar);
398*e0b8e63eSJohn Marino else if (clp->cup != NULL)
399*e0b8e63eSJohn Marino (void)tputs(tgoto(clp->cup,
400*e0b8e63eSJohn Marino 0, LINES - 2), 1, cl_putchar);
401*e0b8e63eSJohn Marino else
402*e0b8e63eSJohn Marino return (0);
403*e0b8e63eSJohn Marino /* FALLTHROUGH */
404*e0b8e63eSJohn Marino case EX_TERM_CE:
405*e0b8e63eSJohn Marino /* Clear the line. */
406*e0b8e63eSJohn Marino if (clp->el != NULL) {
407*e0b8e63eSJohn Marino (void)putchar('\r');
408*e0b8e63eSJohn Marino (void)tputs(clp->el, 1, cl_putchar);
409*e0b8e63eSJohn Marino } else {
410*e0b8e63eSJohn Marino /*
411*e0b8e63eSJohn Marino * Historically, ex didn't erase the line, so, if the
412*e0b8e63eSJohn Marino * displayed line was only a single glyph, and <eof>
413*e0b8e63eSJohn Marino * was more than one glyph, the output would not fully
414*e0b8e63eSJohn Marino * overwrite the user's input. To fix this, output
415*e0b8e63eSJohn Marino * the maxiumum character number of spaces. Note,
416*e0b8e63eSJohn Marino * this won't help if the user entered extra prompt
417*e0b8e63eSJohn Marino * or <blank> characters before the command character.
418*e0b8e63eSJohn Marino * We'd have to do a lot of work to make that work, and
419*e0b8e63eSJohn Marino * it's almost certainly not worth the effort.
420*e0b8e63eSJohn Marino */
421*e0b8e63eSJohn Marino for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
422*e0b8e63eSJohn Marino (void)putchar('\b');
423*e0b8e63eSJohn Marino for (cnt = 0; cnt < MAX_CHARACTER_COLUMNS; ++cnt)
424*e0b8e63eSJohn Marino (void)putchar(' ');
425*e0b8e63eSJohn Marino (void)putchar('\r');
426*e0b8e63eSJohn Marino (void)fflush(stdout);
427*e0b8e63eSJohn Marino }
428*e0b8e63eSJohn Marino break;
429*e0b8e63eSJohn Marino default:
430*e0b8e63eSJohn Marino abort();
431*e0b8e63eSJohn Marino }
432*e0b8e63eSJohn Marino return (0);
433*e0b8e63eSJohn Marino }
434*e0b8e63eSJohn Marino
435*e0b8e63eSJohn Marino /*
436*e0b8e63eSJohn Marino * cl_insertln --
437*e0b8e63eSJohn Marino * Push down the current line, discarding the bottom line.
438*e0b8e63eSJohn Marino *
439*e0b8e63eSJohn Marino * PUBLIC: int cl_insertln(SCR *);
440*e0b8e63eSJohn Marino */
441*e0b8e63eSJohn Marino int
cl_insertln(SCR * sp)442*e0b8e63eSJohn Marino cl_insertln(SCR *sp)
443*e0b8e63eSJohn Marino {
444*e0b8e63eSJohn Marino WINDOW *win;
445*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
446*e0b8e63eSJohn Marino /*
447*e0b8e63eSJohn Marino * The current line is expected to be blank after this operation,
448*e0b8e63eSJohn Marino * and the screen must support that semantic.
449*e0b8e63eSJohn Marino */
450*e0b8e63eSJohn Marino return (winsertln(win) == ERR);
451*e0b8e63eSJohn Marino }
452*e0b8e63eSJohn Marino
453*e0b8e63eSJohn Marino /*
454*e0b8e63eSJohn Marino * cl_keyval --
455*e0b8e63eSJohn Marino * Return the value for a special key.
456*e0b8e63eSJohn Marino *
457*e0b8e63eSJohn Marino * PUBLIC: int cl_keyval(SCR *, scr_keyval_t, CHAR_T *, int *);
458*e0b8e63eSJohn Marino */
459*e0b8e63eSJohn Marino int
cl_keyval(SCR * sp,scr_keyval_t val,CHAR_T * chp,int * dnep)460*e0b8e63eSJohn Marino cl_keyval(SCR *sp, scr_keyval_t val, CHAR_T *chp, int *dnep)
461*e0b8e63eSJohn Marino {
462*e0b8e63eSJohn Marino CL_PRIVATE *clp;
463*e0b8e63eSJohn Marino
464*e0b8e63eSJohn Marino /*
465*e0b8e63eSJohn Marino * VEOF, VERASE and VKILL are required by POSIX 1003.1-1990,
466*e0b8e63eSJohn Marino * VWERASE is a 4BSD extension.
467*e0b8e63eSJohn Marino */
468*e0b8e63eSJohn Marino clp = CLP(sp);
469*e0b8e63eSJohn Marino switch (val) {
470*e0b8e63eSJohn Marino case KEY_VEOF:
471*e0b8e63eSJohn Marino *dnep = (*chp = clp->orig.c_cc[VEOF]) == _POSIX_VDISABLE;
472*e0b8e63eSJohn Marino break;
473*e0b8e63eSJohn Marino case KEY_VERASE:
474*e0b8e63eSJohn Marino *dnep = (*chp = clp->orig.c_cc[VERASE]) == _POSIX_VDISABLE;
475*e0b8e63eSJohn Marino break;
476*e0b8e63eSJohn Marino case KEY_VKILL:
477*e0b8e63eSJohn Marino *dnep = (*chp = clp->orig.c_cc[VKILL]) == _POSIX_VDISABLE;
478*e0b8e63eSJohn Marino break;
479*e0b8e63eSJohn Marino #ifdef VWERASE
480*e0b8e63eSJohn Marino case KEY_VWERASE:
481*e0b8e63eSJohn Marino *dnep = (*chp = clp->orig.c_cc[VWERASE]) == _POSIX_VDISABLE;
482*e0b8e63eSJohn Marino break;
483*e0b8e63eSJohn Marino #endif
484*e0b8e63eSJohn Marino default:
485*e0b8e63eSJohn Marino *dnep = 1;
486*e0b8e63eSJohn Marino break;
487*e0b8e63eSJohn Marino }
488*e0b8e63eSJohn Marino return (0);
489*e0b8e63eSJohn Marino }
490*e0b8e63eSJohn Marino
491*e0b8e63eSJohn Marino /*
492*e0b8e63eSJohn Marino * cl_move --
493*e0b8e63eSJohn Marino * Move the cursor.
494*e0b8e63eSJohn Marino *
495*e0b8e63eSJohn Marino * PUBLIC: int cl_move(SCR *, size_t, size_t);
496*e0b8e63eSJohn Marino */
497*e0b8e63eSJohn Marino int
cl_move(SCR * sp,size_t lno,size_t cno)498*e0b8e63eSJohn Marino cl_move(SCR *sp, size_t lno, size_t cno)
499*e0b8e63eSJohn Marino {
500*e0b8e63eSJohn Marino WINDOW *win;
501*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
502*e0b8e63eSJohn Marino /* See the comment in cl_cursor. */
503*e0b8e63eSJohn Marino if (wmove(win, RLNO(sp, lno), RCNO(sp, cno)) == ERR) {
504*e0b8e63eSJohn Marino msgq(sp, M_ERR, "Error: move: l(%zu + %zu) c(%zu + %zu)",
505*e0b8e63eSJohn Marino lno, sp->roff, cno, sp->coff);
506*e0b8e63eSJohn Marino return (1);
507*e0b8e63eSJohn Marino }
508*e0b8e63eSJohn Marino return (0);
509*e0b8e63eSJohn Marino }
510*e0b8e63eSJohn Marino
511*e0b8e63eSJohn Marino /*
512*e0b8e63eSJohn Marino * cl_refresh --
513*e0b8e63eSJohn Marino * Refresh the screen.
514*e0b8e63eSJohn Marino *
515*e0b8e63eSJohn Marino * PUBLIC: int cl_refresh(SCR *, int);
516*e0b8e63eSJohn Marino */
517*e0b8e63eSJohn Marino int
cl_refresh(SCR * sp,int repaint)518*e0b8e63eSJohn Marino cl_refresh(SCR *sp, int repaint)
519*e0b8e63eSJohn Marino {
520*e0b8e63eSJohn Marino GS *gp;
521*e0b8e63eSJohn Marino CL_PRIVATE *clp;
522*e0b8e63eSJohn Marino WINDOW *win;
523*e0b8e63eSJohn Marino SCR *psp, *tsp;
524*e0b8e63eSJohn Marino size_t y, x;
525*e0b8e63eSJohn Marino
526*e0b8e63eSJohn Marino gp = sp->gp;
527*e0b8e63eSJohn Marino clp = CLP(sp);
528*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
529*e0b8e63eSJohn Marino
530*e0b8e63eSJohn Marino /*
531*e0b8e63eSJohn Marino * If we received a killer signal, we're done, there's no point
532*e0b8e63eSJohn Marino * in refreshing the screen.
533*e0b8e63eSJohn Marino */
534*e0b8e63eSJohn Marino if (clp->killersig)
535*e0b8e63eSJohn Marino return (0);
536*e0b8e63eSJohn Marino
537*e0b8e63eSJohn Marino /*
538*e0b8e63eSJohn Marino * If repaint is set, the editor is telling us that we don't know
539*e0b8e63eSJohn Marino * what's on the screen, so we have to repaint from scratch.
540*e0b8e63eSJohn Marino *
541*e0b8e63eSJohn Marino * If repaint set or the screen layout changed, we need to redraw
542*e0b8e63eSJohn Marino * any lines separating vertically split screens. If the horizontal
543*e0b8e63eSJohn Marino * offsets are the same, then the split was vertical, and need to
544*e0b8e63eSJohn Marino * draw a dividing line.
545*e0b8e63eSJohn Marino */
546*e0b8e63eSJohn Marino if (repaint || F_ISSET(clp, CL_LAYOUT)) {
547*e0b8e63eSJohn Marino getyx(stdscr, y, x);
548*e0b8e63eSJohn Marino for (psp = sp; psp != NULL; psp = TAILQ_NEXT(psp, q))
549*e0b8e63eSJohn Marino for (tsp = TAILQ_NEXT(psp, q); tsp != NULL;
550*e0b8e63eSJohn Marino tsp = TAILQ_NEXT(tsp, q))
551*e0b8e63eSJohn Marino if (psp->roff == tsp->roff) {
552*e0b8e63eSJohn Marino if (psp->coff + psp->cols + 1 == tsp->coff)
553*e0b8e63eSJohn Marino cl_rdiv(psp);
554*e0b8e63eSJohn Marino else
555*e0b8e63eSJohn Marino if (tsp->coff + tsp->cols + 1 == psp->coff)
556*e0b8e63eSJohn Marino cl_rdiv(tsp);
557*e0b8e63eSJohn Marino }
558*e0b8e63eSJohn Marino (void)wmove(stdscr, y, x);
559*e0b8e63eSJohn Marino F_CLR(clp, CL_LAYOUT);
560*e0b8e63eSJohn Marino }
561*e0b8e63eSJohn Marino
562*e0b8e63eSJohn Marino /*
563*e0b8e63eSJohn Marino * In the curses library, doing wrefresh(curscr) is okay, but the
564*e0b8e63eSJohn Marino * screen flashes when we then apply the refresh() to bring it up
565*e0b8e63eSJohn Marino * to date. So, use clearok().
566*e0b8e63eSJohn Marino */
567*e0b8e63eSJohn Marino if (repaint)
568*e0b8e63eSJohn Marino clearok(curscr, 1);
569*e0b8e63eSJohn Marino /*
570*e0b8e63eSJohn Marino * Only do an actual refresh, when this is the focus window,
571*e0b8e63eSJohn Marino * i.e. the one holding the cursor. This assumes that refresh
572*e0b8e63eSJohn Marino * is called for that window after refreshing the others.
573*e0b8e63eSJohn Marino * This prevents the cursor being drawn in the other windows.
574*e0b8e63eSJohn Marino */
575*e0b8e63eSJohn Marino return (wnoutrefresh(stdscr) == ERR ||
576*e0b8e63eSJohn Marino wnoutrefresh(win) == ERR ||
577*e0b8e63eSJohn Marino (sp == clp->focus && doupdate() == ERR));
578*e0b8e63eSJohn Marino }
579*e0b8e63eSJohn Marino
580*e0b8e63eSJohn Marino /*
581*e0b8e63eSJohn Marino * cl_rdiv --
582*e0b8e63eSJohn Marino * Draw a dividing line between two vertically split screens.
583*e0b8e63eSJohn Marino */
584*e0b8e63eSJohn Marino static void
cl_rdiv(SCR * sp)585*e0b8e63eSJohn Marino cl_rdiv(SCR *sp)
586*e0b8e63eSJohn Marino {
587*e0b8e63eSJohn Marino #ifdef __NetBSD__
588*e0b8e63eSJohn Marino mvvline(sp->roff, sp->cols + sp->coff, '|', sp->rows);
589*e0b8e63eSJohn Marino #else
590*e0b8e63eSJohn Marino mvvline(sp->roff, sp->cols + sp->coff, ACS_VLINE, sp->rows);
591*e0b8e63eSJohn Marino #endif
592*e0b8e63eSJohn Marino }
593*e0b8e63eSJohn Marino
594*e0b8e63eSJohn Marino /*
595*e0b8e63eSJohn Marino * cl_rename --
596*e0b8e63eSJohn Marino * Rename the file.
597*e0b8e63eSJohn Marino *
598*e0b8e63eSJohn Marino * PUBLIC: int cl_rename(SCR *, char *, int);
599*e0b8e63eSJohn Marino */
600*e0b8e63eSJohn Marino int
cl_rename(SCR * sp,char * name,int on)601*e0b8e63eSJohn Marino cl_rename(SCR *sp, char *name, int on)
602*e0b8e63eSJohn Marino {
603*e0b8e63eSJohn Marino GS *gp;
604*e0b8e63eSJohn Marino CL_PRIVATE *clp;
605*e0b8e63eSJohn Marino FILE *pfp;
606*e0b8e63eSJohn Marino char buf[256], *s, *e;
607*e0b8e63eSJohn Marino char * wid;
608*e0b8e63eSJohn Marino char cmd[64];
609*e0b8e63eSJohn Marino
610*e0b8e63eSJohn Marino gp = sp->gp;
611*e0b8e63eSJohn Marino clp = CLP(sp);
612*e0b8e63eSJohn Marino
613*e0b8e63eSJohn Marino /*
614*e0b8e63eSJohn Marino * XXX
615*e0b8e63eSJohn Marino * We can only rename windows for xterm.
616*e0b8e63eSJohn Marino */
617*e0b8e63eSJohn Marino if (on) {
618*e0b8e63eSJohn Marino clp->focus = sp;
619*e0b8e63eSJohn Marino if (!F_ISSET(clp, CL_RENAME_OK) ||
620*e0b8e63eSJohn Marino strncmp(OG_STR(gp, GO_TERM), "xterm", 5))
621*e0b8e63eSJohn Marino return (0);
622*e0b8e63eSJohn Marino
623*e0b8e63eSJohn Marino if (clp->oname == NULL && (wid = getenv("WINDOWID"))) {
624*e0b8e63eSJohn Marino snprintf(cmd, sizeof(cmd), "xprop -id %s WM_NAME", wid);
625*e0b8e63eSJohn Marino if ((pfp = popen(cmd, "r")) == NULL)
626*e0b8e63eSJohn Marino goto rename;
627*e0b8e63eSJohn Marino if (fgets(buf, sizeof(buf), pfp) == NULL) {
628*e0b8e63eSJohn Marino pclose(pfp);
629*e0b8e63eSJohn Marino goto rename;
630*e0b8e63eSJohn Marino }
631*e0b8e63eSJohn Marino pclose(pfp);
632*e0b8e63eSJohn Marino if ((s = strchr(buf, '"')) != NULL &&
633*e0b8e63eSJohn Marino (e = strrchr(buf, '"')) != NULL)
634*e0b8e63eSJohn Marino clp->oname = strndup(s + 1, e - s - 1);
635*e0b8e63eSJohn Marino }
636*e0b8e63eSJohn Marino
637*e0b8e63eSJohn Marino rename: cl_setname(gp, name);
638*e0b8e63eSJohn Marino
639*e0b8e63eSJohn Marino F_SET(clp, CL_RENAME);
640*e0b8e63eSJohn Marino } else
641*e0b8e63eSJohn Marino if (F_ISSET(clp, CL_RENAME)) {
642*e0b8e63eSJohn Marino cl_setname(gp, clp->oname);
643*e0b8e63eSJohn Marino
644*e0b8e63eSJohn Marino F_CLR(clp, CL_RENAME);
645*e0b8e63eSJohn Marino }
646*e0b8e63eSJohn Marino return (0);
647*e0b8e63eSJohn Marino }
648*e0b8e63eSJohn Marino
649*e0b8e63eSJohn Marino /*
650*e0b8e63eSJohn Marino * cl_setname --
651*e0b8e63eSJohn Marino * Set a X11 icon/window name.
652*e0b8e63eSJohn Marino *
653*e0b8e63eSJohn Marino * PUBLIC: void cl_setname(GS *, char *);
654*e0b8e63eSJohn Marino */
655*e0b8e63eSJohn Marino void
cl_setname(GS * gp,char * name)656*e0b8e63eSJohn Marino cl_setname(GS *gp, char *name)
657*e0b8e63eSJohn Marino {
658*e0b8e63eSJohn Marino /* X11 xterm escape sequence to rename the icon/window. */
659*e0b8e63eSJohn Marino #define XTERM_RENAME "\033]0;%s\007"
660*e0b8e63eSJohn Marino
661*e0b8e63eSJohn Marino (void)printf(XTERM_RENAME, name == NULL ? OG_STR(gp, GO_TERM) : name);
662*e0b8e63eSJohn Marino (void)fflush(stdout);
663*e0b8e63eSJohn Marino #undef XTERM_RENAME
664*e0b8e63eSJohn Marino }
665*e0b8e63eSJohn Marino
666*e0b8e63eSJohn Marino /*
667*e0b8e63eSJohn Marino * cl_split --
668*e0b8e63eSJohn Marino * Split a screen.
669*e0b8e63eSJohn Marino *
670*e0b8e63eSJohn Marino * PUBLIC: int cl_split(SCR *, SCR *);
671*e0b8e63eSJohn Marino */
672*e0b8e63eSJohn Marino int
cl_split(SCR * origp,SCR * newp)673*e0b8e63eSJohn Marino cl_split(SCR *origp, SCR *newp)
674*e0b8e63eSJohn Marino {
675*e0b8e63eSJohn Marino CL_PRIVATE *clp;
676*e0b8e63eSJohn Marino
677*e0b8e63eSJohn Marino clp = CLP(origp);
678*e0b8e63eSJohn Marino F_SET(clp, CL_LAYOUT);
679*e0b8e63eSJohn Marino
680*e0b8e63eSJohn Marino if (CLSP(origp))
681*e0b8e63eSJohn Marino delwin(CLSP(origp));
682*e0b8e63eSJohn Marino
683*e0b8e63eSJohn Marino origp->cl_private = subwin(stdscr, origp->rows, origp->cols,
684*e0b8e63eSJohn Marino origp->roff, origp->coff);
685*e0b8e63eSJohn Marino newp->cl_private = subwin(stdscr, newp->rows, newp->cols,
686*e0b8e63eSJohn Marino newp->roff, newp->coff);
687*e0b8e63eSJohn Marino
688*e0b8e63eSJohn Marino /* origp is the original screen, giving up space to newp. */
689*e0b8e63eSJohn Marino return (0);
690*e0b8e63eSJohn Marino }
691*e0b8e63eSJohn Marino
692*e0b8e63eSJohn Marino /*
693*e0b8e63eSJohn Marino * cl_suspend --
694*e0b8e63eSJohn Marino * Suspend a screen.
695*e0b8e63eSJohn Marino *
696*e0b8e63eSJohn Marino * PUBLIC: int cl_suspend(SCR *, int *);
697*e0b8e63eSJohn Marino */
698*e0b8e63eSJohn Marino int
cl_suspend(SCR * sp,int * allowedp)699*e0b8e63eSJohn Marino cl_suspend(SCR *sp, int *allowedp)
700*e0b8e63eSJohn Marino {
701*e0b8e63eSJohn Marino struct termios t;
702*e0b8e63eSJohn Marino CL_PRIVATE *clp;
703*e0b8e63eSJohn Marino WINDOW *win;
704*e0b8e63eSJohn Marino GS *gp;
705*e0b8e63eSJohn Marino size_t y, x;
706*e0b8e63eSJohn Marino int changed;
707*e0b8e63eSJohn Marino
708*e0b8e63eSJohn Marino gp = sp->gp;
709*e0b8e63eSJohn Marino clp = CLP(sp);
710*e0b8e63eSJohn Marino win = CLSP(sp) ? CLSP(sp) : stdscr;
711*e0b8e63eSJohn Marino *allowedp = 1;
712*e0b8e63eSJohn Marino
713*e0b8e63eSJohn Marino /*
714*e0b8e63eSJohn Marino * The ex implementation of this function isn't needed by screens not
715*e0b8e63eSJohn Marino * supporting ex commands that require full terminal canonical mode
716*e0b8e63eSJohn Marino * (e.g. :suspend).
717*e0b8e63eSJohn Marino *
718*e0b8e63eSJohn Marino * The vi implementation of this function isn't needed by screens not
719*e0b8e63eSJohn Marino * supporting vi process suspension, i.e. any screen that isn't backed
720*e0b8e63eSJohn Marino * by a UNIX shell.
721*e0b8e63eSJohn Marino *
722*e0b8e63eSJohn Marino * Setting allowedp to 0 will cause the editor to reject the command.
723*e0b8e63eSJohn Marino */
724*e0b8e63eSJohn Marino if (F_ISSET(sp, SC_EX)) {
725*e0b8e63eSJohn Marino /* Save the terminal settings, and restore the original ones. */
726*e0b8e63eSJohn Marino if (F_ISSET(clp, CL_STDIN_TTY)) {
727*e0b8e63eSJohn Marino (void)tcgetattr(STDIN_FILENO, &t);
728*e0b8e63eSJohn Marino (void)tcsetattr(STDIN_FILENO,
729*e0b8e63eSJohn Marino TCSASOFT | TCSADRAIN, &clp->orig);
730*e0b8e63eSJohn Marino }
731*e0b8e63eSJohn Marino
732*e0b8e63eSJohn Marino /* Stop the process group. */
733*e0b8e63eSJohn Marino (void)kill(0, SIGTSTP);
734*e0b8e63eSJohn Marino
735*e0b8e63eSJohn Marino /* Time passes ... */
736*e0b8e63eSJohn Marino
737*e0b8e63eSJohn Marino /* Restore terminal settings. */
738*e0b8e63eSJohn Marino if (F_ISSET(clp, CL_STDIN_TTY))
739*e0b8e63eSJohn Marino (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t);
740*e0b8e63eSJohn Marino return (0);
741*e0b8e63eSJohn Marino }
742*e0b8e63eSJohn Marino
743*e0b8e63eSJohn Marino /*
744*e0b8e63eSJohn Marino * Move to the lower left-hand corner of the screen.
745*e0b8e63eSJohn Marino *
746*e0b8e63eSJohn Marino * XXX
747*e0b8e63eSJohn Marino * Not sure this is necessary in System V implementations, but it
748*e0b8e63eSJohn Marino * shouldn't hurt.
749*e0b8e63eSJohn Marino */
750*e0b8e63eSJohn Marino getyx(win, y, x);
751*e0b8e63eSJohn Marino (void)wmove(win, LINES - 1, 0);
752*e0b8e63eSJohn Marino (void)wrefresh(win);
753*e0b8e63eSJohn Marino
754*e0b8e63eSJohn Marino /*
755*e0b8e63eSJohn Marino * Temporarily end the screen. System V introduced a semantic where
756*e0b8e63eSJohn Marino * endwin() could be restarted. We use it because restarting curses
757*e0b8e63eSJohn Marino * from scratch often fails in System V. 4BSD curses didn't support
758*e0b8e63eSJohn Marino * restarting after endwin(), so we have to do what clean up we can
759*e0b8e63eSJohn Marino * without calling it.
760*e0b8e63eSJohn Marino */
761*e0b8e63eSJohn Marino /* Save the terminal settings. */
762*e0b8e63eSJohn Marino (void)tcgetattr(STDIN_FILENO, &t);
763*e0b8e63eSJohn Marino
764*e0b8e63eSJohn Marino /* Restore the cursor keys to normal mode. */
765*e0b8e63eSJohn Marino (void)keypad(stdscr, FALSE);
766*e0b8e63eSJohn Marino
767*e0b8e63eSJohn Marino /* Restore the window name. */
768*e0b8e63eSJohn Marino (void)cl_rename(sp, NULL, 0);
769*e0b8e63eSJohn Marino
770*e0b8e63eSJohn Marino (void)endwin();
771*e0b8e63eSJohn Marino
772*e0b8e63eSJohn Marino /*
773*e0b8e63eSJohn Marino * XXX
774*e0b8e63eSJohn Marino * Restore the original terminal settings. This is bad -- the
775*e0b8e63eSJohn Marino * reset can cause character loss from the tty queue. However,
776*e0b8e63eSJohn Marino * we can't call endwin() in BSD curses implementations, and too
777*e0b8e63eSJohn Marino * many System V curses implementations don't get it right.
778*e0b8e63eSJohn Marino */
779*e0b8e63eSJohn Marino (void)tcsetattr(STDIN_FILENO, TCSADRAIN | TCSASOFT, &clp->orig);
780*e0b8e63eSJohn Marino
781*e0b8e63eSJohn Marino /* Stop the process group. */
782*e0b8e63eSJohn Marino (void)kill(0, SIGTSTP);
783*e0b8e63eSJohn Marino
784*e0b8e63eSJohn Marino /* Time passes ... */
785*e0b8e63eSJohn Marino
786*e0b8e63eSJohn Marino /*
787*e0b8e63eSJohn Marino * If we received a killer signal, we're done. Leave everything
788*e0b8e63eSJohn Marino * unchanged. In addition, the terminal has already been reset
789*e0b8e63eSJohn Marino * correctly, so leave it alone.
790*e0b8e63eSJohn Marino */
791*e0b8e63eSJohn Marino if (clp->killersig) {
792*e0b8e63eSJohn Marino F_CLR(clp, CL_SCR_EX_INIT | CL_SCR_VI_INIT);
793*e0b8e63eSJohn Marino return (0);
794*e0b8e63eSJohn Marino }
795*e0b8e63eSJohn Marino
796*e0b8e63eSJohn Marino /* Restore terminal settings. */
797*e0b8e63eSJohn Marino wrefresh(win); /* Needed on SunOs/Solaris ? */
798*e0b8e63eSJohn Marino if (F_ISSET(clp, CL_STDIN_TTY))
799*e0b8e63eSJohn Marino (void)tcsetattr(STDIN_FILENO, TCSASOFT | TCSADRAIN, &t);
800*e0b8e63eSJohn Marino
801*e0b8e63eSJohn Marino /* Set the window name. */
802*e0b8e63eSJohn Marino (void)cl_rename(sp, sp->frp->name, 1);
803*e0b8e63eSJohn Marino
804*e0b8e63eSJohn Marino /* Put the cursor keys into application mode. */
805*e0b8e63eSJohn Marino (void)keypad(stdscr, TRUE);
806*e0b8e63eSJohn Marino
807*e0b8e63eSJohn Marino /* Refresh and repaint the screen. */
808*e0b8e63eSJohn Marino (void)wmove(win, y, x);
809*e0b8e63eSJohn Marino (void)cl_refresh(sp, 1);
810*e0b8e63eSJohn Marino
811*e0b8e63eSJohn Marino /* If the screen changed size, set the SIGWINCH bit. */
812*e0b8e63eSJohn Marino if (cl_ssize(sp, 1, NULL, NULL, &changed))
813*e0b8e63eSJohn Marino return (1);
814*e0b8e63eSJohn Marino if (changed)
815*e0b8e63eSJohn Marino F_SET(CLP(sp), CL_SIGWINCH);
816*e0b8e63eSJohn Marino
817*e0b8e63eSJohn Marino return (0);
818*e0b8e63eSJohn Marino }
819*e0b8e63eSJohn Marino
820*e0b8e63eSJohn Marino /*
821*e0b8e63eSJohn Marino * cl_usage --
822*e0b8e63eSJohn Marino * Print out the curses usage messages.
823*e0b8e63eSJohn Marino *
824*e0b8e63eSJohn Marino * PUBLIC: void cl_usage(void);
825*e0b8e63eSJohn Marino */
826*e0b8e63eSJohn Marino void
cl_usage(void)827*e0b8e63eSJohn Marino cl_usage(void)
828*e0b8e63eSJohn Marino {
829*e0b8e63eSJohn Marino #define USAGE "\
830*e0b8e63eSJohn Marino usage: ex [-eFRrSsv] [-c command] [-t tag] [-w size] [file ...]\n\
831*e0b8e63eSJohn Marino usage: vi [-eFlRrSv] [-c command] [-t tag] [-w size] [file ...]\n"
832*e0b8e63eSJohn Marino (void)fprintf(stderr, "%s", USAGE);
833*e0b8e63eSJohn Marino #undef USAGE
834*e0b8e63eSJohn Marino }
835*e0b8e63eSJohn Marino
836*e0b8e63eSJohn Marino #ifdef DEBUG
837*e0b8e63eSJohn Marino /*
838*e0b8e63eSJohn Marino * gdbrefresh --
839*e0b8e63eSJohn Marino * Stub routine so can flush out curses screen changes using gdb.
840*e0b8e63eSJohn Marino */
841*e0b8e63eSJohn Marino static int
842*e0b8e63eSJohn Marino __attribute__((unused))
gdbrefresh(void)843*e0b8e63eSJohn Marino gdbrefresh(void)
844*e0b8e63eSJohn Marino {
845*e0b8e63eSJohn Marino refresh();
846*e0b8e63eSJohn Marino return (0); /* XXX Convince gdb to run it. */
847*e0b8e63eSJohn Marino }
848*e0b8e63eSJohn Marino #endif
849