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