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