Bring in an errno.9 manual page (based on NetBSD's).
[dragonfly.git] / contrib / nvi2 / vi / v_ex.c
blobc6c1133c78d6ff3b1c7bfd2da08c49f229af67f2
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: v_ex.c,v 10.61 2011/12/22 18:41:53 zy Exp $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
18 #include <sys/time.h>
20 #include <bitstring.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "../common/common.h"
28 #include "vi.h"
30 static int v_ecl(SCR *);
31 static int v_ecl_init(SCR *);
32 static int v_ecl_log(SCR *, TEXT *);
33 static int v_ex_done(SCR *, VICMD *);
34 static int v_exec_ex(SCR *, VICMD *, EXCMD *);
37 * v_again -- &
38 * Repeat the previous substitution.
40 * PUBLIC: int v_again(SCR *, VICMD *);
42 int
43 v_again(SCR *sp, VICMD *vp)
45 EXCMD cmd;
47 ex_cinit(sp, &cmd, C_SUBAGAIN, 2, vp->m_start.lno, vp->m_start.lno, 1);
48 argv_exp0(sp, &cmd, L(""), 1);
49 return (v_exec_ex(sp, vp, &cmd));
53 * v_exmode -- Q
54 * Switch the editor into EX mode.
56 * PUBLIC: int v_exmode(SCR *, VICMD *);
58 int
59 v_exmode(SCR *sp, VICMD *vp)
61 GS *gp;
63 gp = sp->gp;
65 /* Try and switch screens -- the screen may not permit it. */
66 if (gp->scr_screen(sp, SC_EX)) {
67 msgq(sp, M_ERR,
68 "207|The Q command requires the ex terminal interface");
69 return (1);
71 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
73 /* Save the current cursor position. */
74 sp->frp->lno = sp->lno;
75 sp->frp->cno = sp->cno;
76 F_SET(sp->frp, FR_CURSORSET);
78 /* Switch to ex mode. */
79 F_CLR(sp, SC_VI | SC_SCR_VI);
80 F_SET(sp, SC_EX);
82 /* Move out of the vi screen. */
83 (void)ex_puts(sp, "\n");
85 return (0);
89 * v_join -- [count]J
90 * Join lines together.
92 * PUBLIC: int v_join(SCR *, VICMD *);
94 int
95 v_join(SCR *sp, VICMD *vp)
97 EXCMD cmd;
98 int lno;
101 * YASC.
102 * The general rule is that '#J' joins # lines, counting the current
103 * line. However, 'J' and '1J' are the same as '2J', i.e. join the
104 * current and next lines. This doesn't map well into the ex command
105 * (which takes two line numbers), so we handle it here. Note that
106 * we never test for EOF -- historically going past the end of file
107 * worked just fine.
109 lno = vp->m_start.lno + 1;
110 if (F_ISSET(vp, VC_C1SET) && vp->count > 2)
111 lno = vp->m_start.lno + (vp->count - 1);
113 ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
114 return (v_exec_ex(sp, vp, &cmd));
118 * v_shiftl -- [count]<motion
119 * Shift lines left.
121 * PUBLIC: int v_shiftl(SCR *, VICMD *);
124 v_shiftl(SCR *sp, VICMD *vp)
126 EXCMD cmd;
128 ex_cinit(sp, &cmd, C_SHIFTL, 2, vp->m_start.lno, vp->m_stop.lno, 0);
129 argv_exp0(sp, &cmd, L("<"), 2);
130 return (v_exec_ex(sp, vp, &cmd));
134 * v_shiftr -- [count]>motion
135 * Shift lines right.
137 * PUBLIC: int v_shiftr(SCR *, VICMD *);
140 v_shiftr(SCR *sp, VICMD *vp)
142 EXCMD cmd;
144 ex_cinit(sp, &cmd, C_SHIFTR, 2, vp->m_start.lno, vp->m_stop.lno, 0);
145 argv_exp0(sp, &cmd, L(">"), 2);
146 return (v_exec_ex(sp, vp, &cmd));
150 * v_suspend -- ^Z
151 * Suspend vi.
153 * PUBLIC: int v_suspend(SCR *, VICMD *);
156 v_suspend(SCR *sp, VICMD *vp)
158 EXCMD cmd;
160 ex_cinit(sp, &cmd, C_STOP, 0, OOBLNO, OOBLNO, 0);
161 argv_exp0(sp, &cmd, L("suspend"), SIZE(L("suspend")));
162 return (v_exec_ex(sp, vp, &cmd));
166 * v_switch -- ^^
167 * Switch to the previous file.
169 * PUBLIC: int v_switch(SCR *, VICMD *);
172 v_switch(SCR *sp, VICMD *vp)
174 EXCMD cmd;
175 char *name;
176 CHAR_T *wp;
177 size_t wlen;
180 * Try the alternate file name, then the previous file
181 * name. Use the real name, not the user's current name.
183 if ((name = sp->alt_name) == NULL) {
184 msgq(sp, M_ERR, "180|No previous file to edit");
185 return (1);
188 /* If autowrite is set, write out the file. */
189 if (file_m1(sp, 0, FS_ALL))
190 return (1);
192 ex_cinit(sp, &cmd, C_EDIT, 0, OOBLNO, OOBLNO, 0);
193 CHAR2INT(sp, name, strlen(name) + 1, wp, wlen);
194 argv_exp0(sp, &cmd, wp, wlen);
195 return (v_exec_ex(sp, vp, &cmd));
199 * v_tagpush -- ^[
200 * Do a tag search on the cursor keyword.
202 * PUBLIC: int v_tagpush(SCR *, VICMD *);
205 v_tagpush(SCR *sp, VICMD *vp)
207 EXCMD cmd;
209 ex_cinit(sp, &cmd, C_TAG, 0, OOBLNO, 0, 0);
210 argv_exp0(sp, &cmd, VIP(sp)->keyw, STRLEN(VIP(sp)->keyw) + 1);
211 return (v_exec_ex(sp, vp, &cmd));
215 * v_tagpop -- ^T
216 * Pop the tags stack.
218 * PUBLIC: int v_tagpop(SCR *, VICMD *);
221 v_tagpop(SCR *sp, VICMD *vp)
223 EXCMD cmd;
225 ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
226 return (v_exec_ex(sp, vp, &cmd));
230 * v_filter -- [count]!motion command(s)
231 * Run range through shell commands, replacing text.
233 * PUBLIC: int v_filter(SCR *, VICMD *);
236 v_filter(SCR *sp, VICMD *vp)
238 EXCMD cmd;
239 TEXT *tp;
242 * !!!
243 * Historical vi permitted "!!" in an empty file, and it's handled
244 * as a special case in the ex_bang routine. Don't modify this setup
245 * without understanding that one. In particular, note that we're
246 * manipulating the ex argument structures behind ex's back.
248 * !!!
249 * Historical vi did not permit the '!' command to be associated with
250 * a non-line oriented motion command, in general, although it did
251 * with search commands. So, !f; and !w would fail, but !/;<CR>
252 * would succeed, even if they all moved to the same location in the
253 * current line. I don't see any reason to disallow '!' using any of
254 * the possible motion commands.
256 * !!!
257 * Historical vi ran the last bang command if N or n was used as the
258 * search motion.
260 if (F_ISSET(vp, VC_ISDOT) ||
261 ISCMD(vp->rkp, 'N') || ISCMD(vp->rkp, 'n')) {
262 ex_cinit(sp,
263 &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
264 EXP(sp)->argsoff = 0; /* XXX */
266 if (argv_exp1(sp, &cmd, L("!"), 1, 1))
267 return (1);
268 cmd.argc = EXP(sp)->argsoff; /* XXX */
269 cmd.argv = EXP(sp)->args; /* XXX */
270 return (v_exec_ex(sp, vp, &cmd));
273 /* Get the command from the user. */
274 if (v_tcmd(sp, vp,
275 '!', TXT_BS | TXT_CR | TXT_ESCAPE | TXT_FILEC | TXT_PROMPT))
276 return (1);
279 * Check to see if the user changed their mind.
281 * !!!
282 * Entering <escape> on an empty line was historically an error,
283 * this implementation doesn't bother.
285 tp = TAILQ_FIRST(sp->tiq);
286 if (tp->term != TERM_OK) {
287 vp->m_final.lno = sp->lno;
288 vp->m_final.cno = sp->cno;
289 return (0);
292 /* Home the cursor. */
293 vs_home(sp);
295 ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
296 EXP(sp)->argsoff = 0; /* XXX */
298 if (argv_exp1(sp, &cmd, tp->lb + 1, tp->len - 1, 1))
299 return (1);
300 cmd.argc = EXP(sp)->argsoff; /* XXX */
301 cmd.argv = EXP(sp)->args; /* XXX */
302 return (v_exec_ex(sp, vp, &cmd));
306 * v_exec_ex --
307 * Execute an ex command.
309 static int
310 v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
312 int rval;
314 rval = exp->cmd->fn(sp, exp);
315 return (v_ex_done(sp, vp) || rval);
319 * v_ex -- :
320 * Execute a colon command line.
322 * PUBLIC: int v_ex(SCR *, VICMD *);
325 v_ex(SCR *sp, VICMD *vp)
327 GS *gp;
328 TEXT *tp;
329 int do_cedit, do_resolution, ifcontinue;
331 gp = sp->gp;
334 * !!!
335 * If we put out more than a single line of messages, or ex trashes
336 * the screen, the user may continue entering ex commands. We find
337 * this out when we do the screen/message resolution. We can't enter
338 * completely into ex mode however, because the user can elect to
339 * return into vi mode by entering any key, i.e. we have to be in raw
340 * mode.
342 for (do_cedit = do_resolution = 0;;) {
344 * !!!
345 * There may already be an ex command waiting to run. If
346 * so, we continue with it.
348 if (!EXCMD_RUNNING(gp)) {
349 /* Get a command. */
350 if (v_tcmd(sp, vp, ':',
351 TXT_BS | TXT_CEDIT | TXT_FILEC | TXT_PROMPT))
352 return (1);
353 tp = TAILQ_FIRST(sp->tiq);
356 * If the user entered a single <esc>, they want to
357 * edit their colon command history. If they already
358 * entered some text, move it into the edit history.
360 if (tp->term == TERM_CEDIT) {
361 if (tp->len > 1 && v_ecl_log(sp, tp))
362 return (1);
363 do_cedit = 1;
364 break;
367 /* If the user didn't enter anything, return. */
368 if (tp->term == TERM_BS)
369 break;
371 /* If the user changed their mind, return. */
372 if (tp->term != TERM_OK)
373 break;
375 /* Log the command. */
376 if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
377 return (1);
379 /* Push a command on the command stack. */
380 if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
381 return (1);
384 /* Home the cursor. */
385 vs_home(sp);
388 * !!!
389 * If the editor wrote the screen behind curses back, put out
390 * a <newline> so that we don't overwrite the user's command
391 * with its output or the next want-to-continue? message. This
392 * doesn't belong here, but I can't find another place to put
393 * it. See, we resolved the output from the last ex command,
394 * and the user entered another one. This is the only place
395 * where we have control before the ex command writes output.
396 * We could get control in vs_msg(), but we have no way to know
397 * if command didn't put out any output when we try and resolve
398 * this command. This fixes a bug where combinations of ex
399 * commands, e.g. ":set<CR>:!date<CR>:set" didn't look right.
401 if (F_ISSET(sp, SC_SCR_EXWROTE))
402 (void)putchar('\n');
404 /* Call the ex parser. */
405 (void)ex_cmd(sp);
407 /* Flush ex messages. */
408 (void)ex_fflush(sp);
410 /* Resolve any messages. */
411 if (vs_ex_resolve(sp, &ifcontinue))
412 return (1);
415 * Continue or return. If continuing, make sure that we
416 * eventually do resolution.
418 if (!ifcontinue)
419 break;
420 do_resolution = 1;
422 /* If we're continuing, it's a new command. */
423 ++sp->ccnt;
427 * If the user previously continued an ex command, we have to do
428 * resolution to clean up the screen. Don't wait, we already did
429 * that.
431 if (do_resolution) {
432 F_SET(sp, SC_EX_WAIT_NO);
433 if (vs_ex_resolve(sp, &ifcontinue))
434 return (1);
437 /* Cleanup from the ex command. */
438 if (v_ex_done(sp, vp))
439 return (1);
441 /* The user may want to edit their colon command history. */
442 if (do_cedit)
443 return (v_ecl(sp));
445 return (0);
449 * v_ex_done --
450 * Cleanup from an ex command.
452 static int
453 v_ex_done(SCR *sp, VICMD *vp)
455 size_t len;
458 * The only cursor modifications are real, however, the underlying
459 * line may have changed; don't trust anything. This code has been
460 * a remarkably fertile place for bugs. Do a reality check on a
461 * cursor value, and make sure it's okay. If necessary, change it.
462 * Ex keeps track of the line number, but it cares less about the
463 * column and it may have disappeared.
465 * Don't trust ANYTHING.
467 * XXX
468 * Ex will soon have to start handling the column correctly; see
469 * the POSIX 1003.2 standard.
471 if (db_eget(sp, sp->lno, NULL, &len, NULL)) {
472 sp->lno = 1;
473 sp->cno = 0;
474 } else if (sp->cno >= len)
475 sp->cno = len ? len - 1 : 0;
477 vp->m_final.lno = sp->lno;
478 vp->m_final.cno = sp->cno;
481 * Don't re-adjust the cursor after executing an ex command,
482 * and ex movements are permanent.
484 F_CLR(vp, VM_RCM_MASK);
485 F_SET(vp, VM_RCM_SET);
487 return (0);
491 * v_ecl --
492 * Start an edit window on the colon command-line commands.
494 static int
495 v_ecl(SCR *sp)
497 GS *gp;
498 SCR *new;
500 /* Initialize the screen, if necessary. */
501 gp = sp->gp;
502 if (gp->ccl_sp == NULL && v_ecl_init(sp))
503 return (1);
505 /* Get a new screen. */
506 if (screen_init(gp, sp, &new))
507 return (1);
508 if (vs_split(sp, new, 1)) {
509 (void)screen_end(new);
510 return (1);
513 /* Attach to the screen. */
514 new->ep = gp->ccl_sp->ep;
515 ++new->ep->refcnt;
517 new->frp = gp->ccl_sp->frp;
518 new->frp->flags = sp->frp->flags;
520 /* Move the cursor to the end. */
521 (void)db_last(new, &new->lno);
522 if (new->lno == 0)
523 new->lno = 1;
525 /* Remember the originating window. */
526 sp->ccl_parent = sp;
528 /* It's a special window. */
529 F_SET(new, SC_COMEDIT);
531 #if defined(USE_WIDECHAR) && defined(USE_ICONV)
532 /* Bypass iconv on writing to DB. */
533 o_set(new, O_FILEENCODING, OS_STRDUP, codeset(), 0);
534 #endif
536 /* Set up the switch. */
537 sp->nextdisp = new;
538 F_SET(sp, SC_SSWITCH);
539 return (0);
543 * v_ecl_exec --
544 * Execute a command from a colon command-line window.
546 * PUBLIC: int v_ecl_exec(SCR *);
549 v_ecl_exec(SCR *sp)
551 size_t len;
552 CHAR_T *p;
554 if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
555 v_emsg(sp, NULL, VIM_EMPTY);
556 return (1);
558 if (len == 0) {
559 msgq(sp, M_BERR, "307|No ex command to execute");
560 return (1);
563 /* Push the command on the command stack. */
564 if (ex_run_str(sp, NULL, p, len, 0, 0))
565 return (1);
567 /* Set up the switch. */
568 sp->nextdisp = sp->ccl_parent;
569 F_SET(sp, SC_EXIT);
570 return (0);
574 * v_ecl_log --
575 * Log a command into the colon command-line log file.
577 static int
578 v_ecl_log(SCR *sp, TEXT *tp)
580 recno_t lno;
581 int rval;
582 CHAR_T *p;
583 size_t len;
584 SCR *ccl_sp;
586 /* Initialize the screen, if necessary. */
587 if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
588 return (1);
590 ccl_sp = sp->gp->ccl_sp;
593 * Don't log colon command window commands into the colon command
594 * window...
596 if (sp->ep == ccl_sp->ep)
597 return (0);
599 if (db_last(ccl_sp, &lno)) {
600 return (1);
602 /* Don't log line that is identical to previous one */
603 if (lno > 0 &&
604 !db_get(ccl_sp, lno, 0, &p, &len) &&
605 len == tp->len &&
606 !MEMCMP(tp->lb, p, len))
607 rval = 0;
608 else {
609 rval = db_append(ccl_sp, 0, lno, tp->lb, tp->len);
610 /* XXXX end "transaction" on ccl */
611 /* Is this still necessary now that we no longer hijack sp ? */
612 log_cursor(ccl_sp);
615 return (rval);
619 * v_ecl_init --
620 * Initialize the colon command-line log file.
622 static int
623 v_ecl_init(SCR *sp)
625 FREF *frp;
626 GS *gp;
628 gp = sp->gp;
630 /* Get a temporary file. */
631 if ((frp = file_add(sp, NULL)) == NULL)
632 return (1);
635 * XXX
636 * Create a screen -- the file initialization code wants one.
638 if (screen_init(gp, sp, &gp->ccl_sp))
639 return (1);
640 if (file_init(gp->ccl_sp, frp, NULL, 0)) {
641 (void)screen_end(gp->ccl_sp);
642 gp->ccl_sp = NULL;
643 return (1);
646 /* The underlying file isn't recoverable. */
647 F_CLR(gp->ccl_sp->ep, F_RCV_ON);
649 return (0);