xref: /freebsd/contrib/nvi/ex/ex_txt.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_txt.c,v 10.23 2001/06/25 15:19:21 skimo Exp $";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
19 
20 #include <bitstring.h>
21 #include <ctype.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "../common/common.h"
28 #include "../vi/vi.h"
29 
30 /*
31  * !!!
32  * The backslash characters was special when it preceded a newline as part of
33  * a substitution replacement pattern.  For example, the input ":a\<cr>" would
34  * failed immediately with an error, as the <cr> wasn't part of a substitution
35  * replacement pattern.  This implies a frightening integration of the editor
36  * and the parser and/or the RE engine.  There's no way I'm going to reproduce
37  * those semantics.
38  *
39  * So, if backslashes are special, this code inserts the backslash and the next
40  * character into the string, without regard for the character or the command
41  * being entered.  Since "\<cr>" was illegal historically (except for the one
42  * special case), and the command will fail eventually, no historical scripts
43  * should break (presuming they didn't depend on the failure mode itself or the
44  * characters remaining when failure occurred.
45  */
46 
47 static int	txt_dent(SCR *, TEXT *);
48 static void	txt_prompt(SCR *, TEXT *, ARG_CHAR_T, u_int32_t);
49 
50 /*
51  * ex_txt --
52  *	Get lines from the terminal for ex.
53  *
54  * PUBLIC: int ex_txt(SCR *, TEXTH *, ARG_CHAR_T, u_int32_t);
55  */
56 int
57 ex_txt(SCR *sp, TEXTH *tiqh, ARG_CHAR_T prompt, u_int32_t flags)
58 {
59 	EVENT ev;
60 	GS *gp;
61 	TEXT ait, *ntp, *tp;
62 	carat_t carat_st;
63 	size_t cnt;
64 	int rval;
65 	int nochange;
66 
67 	rval = 0;
68 
69 	/*
70 	 * Get a TEXT structure with some initial buffer space, reusing the
71 	 * last one if it's big enough.  (All TEXT bookkeeping fields default
72 	 * to 0 -- text_init() handles this.)
73 	 */
74 	if (!TAILQ_EMPTY(tiqh)) {
75 		tp = TAILQ_FIRST(tiqh);
76 		if (TAILQ_NEXT(tp, q) != NULL || tp->lb_len < 32) {
77 			text_lfree(tiqh);
78 			goto newtp;
79 		}
80 		tp->len = 0;
81 	} else {
82 newtp:		if ((tp = text_init(sp, NULL, 0, 32)) == NULL)
83 			goto err;
84 		TAILQ_INSERT_HEAD(tiqh, tp, q);
85 	}
86 
87 	/* Set the starting line number. */
88 	tp->lno = sp->lno + 1;
89 
90 	/*
91 	 * If it's a terminal, set up autoindent, put out the prompt, and
92 	 * set it up so we know we were suspended.  Otherwise, turn off
93 	 * the autoindent flag, as that requires less special casing below.
94 	 *
95 	 * XXX
96 	 * Historic practice is that ^Z suspended command mode (but, because
97 	 * it ran in cooked mode, it was unaffected by the autowrite option.)
98 	 * On restart, any "current" input was discarded, whether in insert
99 	 * mode or not, and ex was in command mode.  This code matches historic
100 	 * practice, but not 'cause it's easier.
101 	 */
102 	gp = sp->gp;
103 	if (F_ISSET(gp, G_SCRIPTED))
104 		LF_CLR(TXT_AUTOINDENT);
105 	else {
106 		if (LF_ISSET(TXT_AUTOINDENT)) {
107 			LF_SET(TXT_EOFCHAR);
108 			if (v_txt_auto(sp, sp->lno, NULL, 0, tp))
109 				goto err;
110 		}
111 		txt_prompt(sp, tp, prompt, flags);
112 	}
113 
114 	for (carat_st = C_NOTSET, nochange = 0;;) {
115 		if (v_event_get(sp, &ev, 0, 0))
116 			goto err;
117 
118 		/* Deal with all non-character events. */
119 		switch (ev.e_event) {
120 		case E_CHARACTER:
121 			break;
122 		case E_ERR:
123 			goto err;
124 		case E_REPAINT:
125 		case E_WRESIZE:
126 			continue;
127 		case E_EOF:
128 			rval = 1;
129 			/* FALLTHROUGH */
130 		case E_INTERRUPT:
131 			/*
132 			 * Handle EOF/SIGINT events by discarding partially
133 			 * entered text and returning.  EOF returns failure,
134 			 * E_INTERRUPT returns success.
135 			 */
136 			goto notlast;
137 		default:
138 			v_event_err(sp, &ev);
139 			goto notlast;
140 		}
141 
142 		/*
143 		 * Deal with character events.
144 		 *
145 		 * Check to see if the character fits into the input buffer.
146 		 * (Use tp->len, ignore overwrite and non-printable chars.)
147 		 */
148 		BINC_GOTOW(sp, tp->lb, tp->lb_len, tp->len + 1);
149 
150 		switch (ev.e_value) {
151 		case K_CR:
152 			/*
153 			 * !!!
154 			 * Historically, <carriage-return>'s in the command
155 			 * weren't special, so the ex parser would return an
156 			 * unknown command error message.  However, if they
157 			 * terminated the command if they were in a map.  I'm
158 			 * pretty sure this still isn't right, but it handles
159 			 * what I've seen so far.
160 			 */
161 			if (!F_ISSET(&ev.e_ch, CH_MAPPED))
162 				goto ins_ch;
163 			/* FALLTHROUGH */
164 		case K_NL:
165 			/*
166 			 * '\' can escape <carriage-return>/<newline>.  We
167 			 * don't discard the backslash because we need it
168 			 * to get the <newline> through the ex parser.
169 			 */
170 			if (LF_ISSET(TXT_BACKSLASH) &&
171 			    tp->len != 0 && tp->lb[tp->len - 1] == '\\')
172 				goto ins_ch;
173 
174 			/*
175 			 * CR returns from the ex command line.
176 			 *
177 			 * XXX
178 			 * Terminate with a nul, needed by filter.
179 			 */
180 			if (LF_ISSET(TXT_CR)) {
181 				tp->lb[tp->len] = '\0';
182 				goto done;
183 			}
184 
185 			/*
186 			 * '.' may terminate text input mode; free the current
187 			 * TEXT.
188 			 */
189 			if (LF_ISSET(TXT_DOTTERM) && tp->len == tp->ai + 1 &&
190 			    tp->lb[tp->len - 1] == '.') {
191 notlast:			TAILQ_REMOVE(tiqh, tp, q);
192 				text_free(tp);
193 				goto done;
194 			}
195 
196 			/* Set up bookkeeping for the new line. */
197 			if ((ntp = text_init(sp, NULL, 0, 32)) == NULL)
198 				goto err;
199 			ntp->lno = tp->lno + 1;
200 
201 			/*
202 			 * Reset the autoindent line value.  0^D keeps the ai
203 			 * line from changing, ^D changes the level, even if
204 			 * there were no characters in the old line.  Note, if
205 			 * using the current tp structure, use the cursor as
206 			 * the length, the autoindent characters may have been
207 			 * erased.
208 			 */
209 			if (LF_ISSET(TXT_AUTOINDENT)) {
210 				if (nochange) {
211 					nochange = 0;
212 					if (v_txt_auto(sp,
213 					    OOBLNO, &ait, ait.ai, ntp))
214 						goto err;
215 					free(ait.lb);
216 				} else
217 					if (v_txt_auto(sp,
218 					    OOBLNO, tp, tp->len, ntp))
219 						goto err;
220 				carat_st = C_NOTSET;
221 			}
222 			txt_prompt(sp, ntp, prompt, flags);
223 
224 			/*
225 			 * Swap old and new TEXT's, and insert the new TEXT
226 			 * into the queue.
227 			 */
228 			tp = ntp;
229 			TAILQ_INSERT_TAIL(tiqh, tp, q);
230 			break;
231 		case K_CARAT:			/* Delete autoindent chars. */
232 			if (tp->len <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
233 				carat_st = C_CARATSET;
234 			goto ins_ch;
235 		case K_ZERO:			/* Delete autoindent chars. */
236 			if (tp->len <= tp->ai && LF_ISSET(TXT_AUTOINDENT))
237 				carat_st = C_ZEROSET;
238 			goto ins_ch;
239 		case K_CNTRLD:			/* Delete autoindent char. */
240 			/*
241 			 * !!!
242 			 * Historically, the ^D command took (but then ignored)
243 			 * a count.  For simplicity, we don't return it unless
244 			 * it's the first character entered.  The check for len
245 			 * equal to 0 is okay, TXT_AUTOINDENT won't be set.
246 			 */
247 			if (LF_ISSET(TXT_CNTRLD)) {
248 				for (cnt = 0; cnt < tp->len; ++cnt)
249 					if (!isblank(tp->lb[cnt]))
250 						break;
251 				if (cnt == tp->len) {
252 					tp->len = 1;
253 					tp->lb[0] = ev.e_c;
254 					tp->lb[1] = '\0';
255 
256 					/*
257 					 * Put out a line separator, in case
258 					 * the command fails.
259 					 */
260 					(void)putchar('\n');
261 					goto done;
262 				}
263 			}
264 
265 			/*
266 			 * POSIX 1003.1b-1993, paragraph 7.1.1.9, states that
267 			 * the EOF characters are discarded if there are other
268 			 * characters to process in the line, i.e. if the EOF
269 			 * is not the first character in the line.  For this
270 			 * reason, historic ex discarded the EOF characters,
271 			 * even if occurring in the middle of the input line.
272 			 * We match that historic practice.
273 			 *
274 			 * !!!
275 			 * The test for discarding in the middle of the line is
276 			 * done in the switch, because the CARAT forms are N+1,
277 			 * not N.
278 			 *
279 			 * !!!
280 			 * There's considerable magic to make the terminal code
281 			 * return the EOF character at all.  See that code for
282 			 * details.
283 			 */
284 			if (!LF_ISSET(TXT_AUTOINDENT) || tp->len == 0)
285 				continue;
286 			switch (carat_st) {
287 			case C_CARATSET:		/* ^^D */
288 				if (tp->len > tp->ai + 1)
289 					continue;
290 
291 				/* Save the ai string for later. */
292 				ait.lb = NULL;
293 				ait.lb_len = 0;
294 				BINC_GOTOW(sp, ait.lb, ait.lb_len, tp->ai);
295 				MEMCPY(ait.lb, tp->lb, tp->ai);
296 				ait.ai = ait.len = tp->ai;
297 
298 				carat_st = C_NOTSET;
299 				nochange = 1;
300 				goto leftmargin;
301 			case C_ZEROSET:			/* 0^D */
302 				if (tp->len > tp->ai + 1)
303 					continue;
304 
305 				carat_st = C_NOTSET;
306 leftmargin:			(void)gp->scr_ex_adjust(sp, EX_TERM_CE);
307 				tp->ai = tp->len = 0;
308 				break;
309 			case C_NOTSET:			/* ^D */
310 				if (tp->len > tp->ai)
311 					continue;
312 
313 				if (txt_dent(sp, tp))
314 					goto err;
315 				break;
316 			default:
317 				abort();
318 			}
319 
320 			/* Clear and redisplay the line. */
321 			(void)gp->scr_ex_adjust(sp, EX_TERM_CE);
322 			txt_prompt(sp, tp, prompt, flags);
323 			break;
324 		default:
325 			/*
326 			 * See the TXT_BEAUTIFY comment in vi/v_txt_ev.c.
327 			 *
328 			 * Silently eliminate any iscntrl() character that was
329 			 * not already handled specially, except for <tab> and
330 			 * <ff>.
331 			 */
332 ins_ch:			if (LF_ISSET(TXT_BEAUTIFY) && ISCNTRL(ev.e_c) &&
333 			    ev.e_value != K_FORMFEED && ev.e_value != K_TAB)
334 				break;
335 
336 			tp->lb[tp->len++] = ev.e_c;
337 			break;
338 		}
339 	}
340 	/* NOTREACHED */
341 
342 done:	return (rval);
343 
344 err:
345 alloc_err:
346 	return (1);
347 }
348 
349 /*
350  * txt_prompt --
351  *	Display the ex prompt, line number, ai characters.  Characters had
352  *	better be printable by the terminal driver, but that's its problem,
353  *	not ours.
354  */
355 static void
356 txt_prompt(SCR *sp, TEXT *tp, ARG_CHAR_T prompt, u_int32_t flags)
357 {
358 	/* Display the prompt. */
359 	if (LF_ISSET(TXT_PROMPT))
360 		(void)ex_printf(sp, "%c", prompt);
361 
362 	/* Display the line number. */
363 	if (LF_ISSET(TXT_NUMBER) && O_ISSET(sp, O_NUMBER))
364 		(void)ex_printf(sp, "%6lu  ", (u_long)tp->lno);
365 
366 	/* Print out autoindent string. */
367 	if (LF_ISSET(TXT_AUTOINDENT))
368 		(void)ex_printf(sp, WVS, (int)tp->ai, tp->lb);
369 	(void)ex_fflush(sp);
370 }
371 
372 /*
373  * txt_dent --
374  *	Handle ^D outdents.
375  *
376  * Ex version of vi/v_ntext.c:txt_dent().  See that code for the (usual)
377  * ranting and raving.  This is a fair bit simpler as ^T isn't special.
378  */
379 static int
380 txt_dent(SCR *sp, TEXT *tp)
381 {
382 	u_long sw, ts;
383 	size_t cno, off, scno, spaces, tabs;
384 
385 	ts = O_VAL(sp, O_TABSTOP);
386 	sw = O_VAL(sp, O_SHIFTWIDTH);
387 
388 	/* Get the current screen column. */
389 	for (off = scno = 0; off < tp->len; ++off)
390 		if (tp->lb[off] == '\t')
391 			scno += COL_OFF(scno, ts);
392 		else
393 			++scno;
394 
395 	/* Get the previous shiftwidth column. */
396 	cno = scno--;
397 	scno -= scno % sw;
398 
399 	/*
400 	 * Since we don't know what comes before the character(s) being
401 	 * deleted, we have to resolve the autoindent characters .  The
402 	 * example is a <tab>, which doesn't take up a full shiftwidth
403 	 * number of columns because it's preceded by <space>s.  This is
404 	 * easy to get if the user sets shiftwidth to a value less than
405 	 * tabstop, and then uses ^T to indent, and ^D to outdent.
406 	 *
407 	 * Count up spaces/tabs needed to get to the target.
408 	 */
409 	for (cno = 0, tabs = 0; cno + COL_OFF(cno, ts) <= scno; ++tabs)
410 		cno += COL_OFF(cno, ts);
411 	spaces = scno - cno;
412 
413 	/* Make sure there's enough room. */
414 	BINC_RETW(sp, tp->lb, tp->lb_len, tabs + spaces + 1);
415 
416 	/* Adjust the final ai character count. */
417 	tp->ai = tabs + spaces;
418 
419 	/* Enter the replacement characters. */
420 	for (tp->len = 0; tabs > 0; --tabs)
421 		tp->lb[tp->len++] = '\t';
422 	for (; spaces > 0; --spaces)
423 		tp->lb[tp->len++] = ' ';
424 	return (0);
425 }
426