]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/nvi/vi/v_ex.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / nvi / vi / v_ex.c
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: v_ex.c,v 10.61 2011/12/22 18:41:53 zy 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 <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "../common/common.h"
28 #include "vi.h"
29
30 static int v_ecl __P((SCR *));
31 static int v_ecl_init __P((SCR *));
32 static int v_ecl_log __P((SCR *, TEXT *));
33 static int v_ex_done __P((SCR *, VICMD *));
34 static int v_exec_ex __P((SCR *, VICMD *, EXCMD *));
35
36 /*
37  * v_again -- &
38  *      Repeat the previous substitution.
39  *
40  * PUBLIC: int v_again __P((SCR *, VICMD *));
41  */
42 int
43 v_again(SCR *sp, VICMD *vp)
44 {
45         EXCMD cmd;
46
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));
50 }
51
52 /*
53  * v_exmode -- Q
54  *      Switch the editor into EX mode.
55  *
56  * PUBLIC: int v_exmode __P((SCR *, VICMD *));
57  */
58 int
59 v_exmode(SCR *sp, VICMD *vp)
60 {
61         GS *gp;
62
63         gp = sp->gp;
64
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);
70         }
71         (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
72
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);
77
78         /* Switch to ex mode. */
79         F_CLR(sp, SC_VI | SC_SCR_VI);
80         F_SET(sp, SC_EX);
81
82         /* Move out of the vi screen. */
83         (void)ex_puts(sp, "\n");
84
85         return (0);
86 }
87
88 /*
89  * v_join -- [count]J
90  *      Join lines together.
91  *
92  * PUBLIC: int v_join __P((SCR *, VICMD *));
93  */
94 int
95 v_join(SCR *sp, VICMD *vp)
96 {
97         EXCMD cmd;
98         int lno;
99
100         /*
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.
108          */
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);
112
113         ex_cinit(sp, &cmd, C_JOIN, 2, vp->m_start.lno, lno, 0);
114         return (v_exec_ex(sp, vp, &cmd));
115 }
116
117 /*
118  * v_shiftl -- [count]<motion
119  *      Shift lines left.
120  *
121  * PUBLIC: int v_shiftl __P((SCR *, VICMD *));
122  */
123 int
124 v_shiftl(SCR *sp, VICMD *vp)
125 {
126         EXCMD cmd;
127
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));
131 }
132
133 /*
134  * v_shiftr -- [count]>motion
135  *      Shift lines right.
136  *
137  * PUBLIC: int v_shiftr __P((SCR *, VICMD *));
138  */
139 int
140 v_shiftr(SCR *sp, VICMD *vp)
141 {
142         EXCMD cmd;
143
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));
147 }
148
149 /*
150  * v_suspend -- ^Z
151  *      Suspend vi.
152  *
153  * PUBLIC: int v_suspend __P((SCR *, VICMD *));
154  */
155 int
156 v_suspend(SCR *sp, VICMD *vp)
157 {
158         EXCMD cmd;
159
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));
163 }
164
165 /*
166  * v_switch -- ^^
167  *      Switch to the previous file.
168  *
169  * PUBLIC: int v_switch __P((SCR *, VICMD *));
170  */
171 int
172 v_switch(SCR *sp, VICMD *vp)
173 {
174         EXCMD cmd;
175         char *name;
176         CHAR_T *wp;
177         size_t wlen;
178
179         /*
180          * Try the alternate file name, then the previous file
181          * name.  Use the real name, not the user's current name.
182          */
183         if ((name = sp->alt_name) == NULL) {
184                 msgq(sp, M_ERR, "180|No previous file to edit");
185                 return (1);
186         }
187
188         /* If autowrite is set, write out the file. */
189         if (file_m1(sp, 0, FS_ALL))
190                 return (1);
191
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));
196 }
197
198 /*
199  * v_tagpush -- ^[
200  *      Do a tag search on the cursor keyword.
201  *
202  * PUBLIC: int v_tagpush __P((SCR *, VICMD *));
203  */
204 int
205 v_tagpush(SCR *sp, VICMD *vp)
206 {
207         EXCMD cmd;
208
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));
212 }
213
214 /*
215  * v_tagpop -- ^T
216  *      Pop the tags stack.
217  *
218  * PUBLIC: int v_tagpop __P((SCR *, VICMD *));
219  */
220 int
221 v_tagpop(SCR *sp, VICMD *vp)
222 {
223         EXCMD cmd;
224
225         ex_cinit(sp, &cmd, C_TAGPOP, 0, OOBLNO, 0, 0);
226         return (v_exec_ex(sp, vp, &cmd));
227 }
228
229 /*
230  * v_filter -- [count]!motion command(s)
231  *      Run range through shell commands, replacing text.
232  *
233  * PUBLIC: int v_filter __P((SCR *, VICMD *));
234  */
235 int
236 v_filter(SCR *sp, VICMD *vp)
237 {
238         EXCMD cmd;
239         TEXT *tp;
240
241         /*
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.
247          *
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.
255          *
256          * !!!
257          * Historical vi ran the last bang command if N or n was used as the
258          * search motion.
259          */
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 */
265
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));
271         }
272
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);
277
278         /*
279          * Check to see if the user changed their mind.
280          *
281          * !!!
282          * Entering <escape> on an empty line was historically an error,
283          * this implementation doesn't bother.
284          */
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);
290         }
291
292         /* Home the cursor. */
293         vs_home(sp);
294
295         ex_cinit(sp, &cmd, C_BANG, 2, vp->m_start.lno, vp->m_stop.lno, 0);
296         EXP(sp)->argsoff = 0;                   /* XXX */
297
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));
303 }
304
305 /*
306  * v_exec_ex --
307  *      Execute an ex command.
308  */
309 static int
310 v_exec_ex(SCR *sp, VICMD *vp, EXCMD *exp)
311 {
312         int rval;
313
314         rval = exp->cmd->fn(sp, exp);
315         return (v_ex_done(sp, vp) || rval);
316 }
317
318 /*
319  * v_ex -- :
320  *      Execute a colon command line.
321  *
322  * PUBLIC: int v_ex __P((SCR *, VICMD *));
323  */
324 int
325 v_ex(SCR *sp, VICMD *vp)
326 {
327         GS *gp;
328         TEXT *tp;
329         int do_cedit, do_resolution, ifcontinue;
330
331         gp = sp->gp;
332
333         /*
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.
341          */
342         for (do_cedit = do_resolution = 0;;) {
343                 /*
344                  * !!!
345                  * There may already be an ex command waiting to run.  If
346                  * so, we continue with it.
347                  */
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);
354
355                         /*
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.
359                          */
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;
365                         }
366
367                         /* If the user didn't enter anything, return. */
368                         if (tp->term == TERM_BS)
369                                 break;
370
371                         /* If the user changed their mind, return. */
372                         if (tp->term != TERM_OK)
373                                 break;
374
375                         /* Log the command. */
376                         if (O_STR(sp, O_CEDIT) != NULL && v_ecl_log(sp, tp))
377                                 return (1);
378
379                         /* Push a command on the command stack. */
380                         if (ex_run_str(sp, NULL, tp->lb, tp->len, 0, 1))
381                                 return (1);
382                 }
383
384                 /* Home the cursor. */
385                 vs_home(sp);
386
387                 /*
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.
400                  */
401                 if (F_ISSET(sp, SC_SCR_EXWROTE))
402                         (void)putchar('\n');
403
404                 /* Call the ex parser. */
405                 (void)ex_cmd(sp);
406
407                 /* Flush ex messages. */
408                 (void)ex_fflush(sp);
409
410                 /* Resolve any messages. */
411                 if (vs_ex_resolve(sp, &ifcontinue))
412                         return (1);
413
414                 /*
415                  * Continue or return.  If continuing, make sure that we
416                  * eventually do resolution.
417                  */
418                 if (!ifcontinue)
419                         break;
420                 do_resolution = 1;
421
422                 /* If we're continuing, it's a new command. */
423                 ++sp->ccnt;
424         }
425
426         /*
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.
430          */
431         if (do_resolution) {
432                 F_SET(sp, SC_EX_WAIT_NO);
433                 if (vs_ex_resolve(sp, &ifcontinue))
434                         return (1);
435         }
436
437         /* Cleanup from the ex command. */
438         if (v_ex_done(sp, vp))
439                 return (1);
440
441         /* The user may want to edit their colon command history. */
442         if (do_cedit)
443                 return (v_ecl(sp));
444
445         return (0);
446 }
447
448 /*
449  * v_ex_done --
450  *      Cleanup from an ex command.
451  */
452 static int
453 v_ex_done(SCR *sp, VICMD *vp)
454 {
455         size_t len;
456
457         /*
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.
464          *
465          * Don't trust ANYTHING.
466          *
467          * XXX
468          * Ex will soon have to start handling the column correctly; see
469          * the POSIX 1003.2 standard.
470          */
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;
476
477         vp->m_final.lno = sp->lno;
478         vp->m_final.cno = sp->cno;
479
480         /*
481          * Don't re-adjust the cursor after executing an ex command,
482          * and ex movements are permanent.
483          */
484         F_CLR(vp, VM_RCM_MASK);
485         F_SET(vp, VM_RCM_SET);
486
487         return (0);
488 }
489
490 /*
491  * v_ecl --
492  *      Start an edit window on the colon command-line commands.
493  */
494 static int
495 v_ecl(SCR *sp)
496 {
497         GS *gp;
498         SCR *new;
499
500         /* Initialize the screen, if necessary. */
501         gp = sp->gp;
502         if (gp->ccl_sp == NULL && v_ecl_init(sp))
503                 return (1);
504
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);
511         }
512
513         /* Attach to the screen. */
514         new->ep = gp->ccl_sp->ep;
515         ++new->ep->refcnt;
516
517         new->frp = gp->ccl_sp->frp;
518         new->frp->flags = sp->frp->flags;
519
520         /* Move the cursor to the end. */
521         (void)db_last(new, &new->lno);
522         if (new->lno == 0)
523                 new->lno = 1;
524
525         /* Remember the originating window. */
526         sp->ccl_parent = sp;
527
528         /* It's a special window. */
529         F_SET(new, SC_COMEDIT);
530
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
535
536         /* Set up the switch. */
537         sp->nextdisp = new;
538         F_SET(sp, SC_SSWITCH);
539         return (0);
540 }
541
542 /*
543  * v_ecl_exec --
544  *      Execute a command from a colon command-line window.
545  *
546  * PUBLIC: int v_ecl_exec __P((SCR *));
547  */
548 int
549 v_ecl_exec(SCR *sp)
550 {
551         size_t len;
552         CHAR_T *p;
553
554         if (db_get(sp, sp->lno, 0, &p, &len) && sp->lno == 1) {
555                 v_emsg(sp, NULL, VIM_EMPTY);
556                 return (1);
557         }
558         if (len == 0) {
559                 msgq(sp, M_BERR, "307|No ex command to execute");
560                 return (1);
561         }
562         
563         /* Push the command on the command stack. */
564         if (ex_run_str(sp, NULL, p, len, 0, 0))
565                 return (1);
566
567         /* Set up the switch. */
568         sp->nextdisp = sp->ccl_parent;
569         F_SET(sp, SC_EXIT);
570         return (0);
571 }
572
573 /*
574  * v_ecl_log --
575  *      Log a command into the colon command-line log file.
576  */
577 static int
578 v_ecl_log(SCR *sp, TEXT *tp)
579 {
580         recno_t lno;
581         int rval;
582         CHAR_T *p;
583         size_t len;
584         SCR *ccl_sp;
585
586         /* Initialize the screen, if necessary. */
587         if (sp->gp->ccl_sp == NULL && v_ecl_init(sp))
588                 return (1);
589
590         ccl_sp = sp->gp->ccl_sp;
591
592         /*
593          * Don't log colon command window commands into the colon command
594          * window...
595          */
596         if (sp->ep == ccl_sp->ep)
597                 return (0);
598
599         if (db_last(ccl_sp, &lno)) {
600                 return (1);
601         }
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);
613         }
614
615         return (rval);
616 }
617
618 /*
619  * v_ecl_init --
620  *      Initialize the colon command-line log file.
621  */
622 static int
623 v_ecl_init(SCR *sp)
624 {
625         FREF *frp;
626         GS *gp;
627
628         gp = sp->gp;
629
630         /* Get a temporary file. */
631         if ((frp = file_add(sp, NULL)) == NULL)
632                 return (1);
633
634         /*
635          * XXX
636          * Create a screen -- the file initialization code wants one.
637          */
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);
644         }
645
646         /* The underlying file isn't recoverable. */
647         F_CLR(gp->ccl_sp->ep, F_RCV_ON);
648
649         return (0);
650 }