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