1 /* $NetBSD: ex_bang.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
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.
9 * See the LICENSE file for redistribution information.
15 static const char sccsid
[] = "Id: ex_bang.c,v 10.36 2001/06/25 15:19:14 skimo Exp (Berkeley) Date: 2001/06/25 15:19:14";
18 #include <sys/types.h>
19 #include <sys/queue.h>
22 #include <bitstring.h>
30 #include "../common/common.h"
34 * ex_bang -- :[line [,line]] ! command
36 * Pass the rest of the line after the ! character to the program named by
39 * Historical vi did NOT do shell expansion on the arguments before passing
40 * them, only file name expansion. This means that the O_SHELL program got
41 * "$t" as an argument if that is what the user entered. Also, there's a
42 * special expansion done for the bang command. Any exclamation points in
43 * the user's argument are replaced by the last, expanded ! command.
45 * There's some fairly amazing slop in this routine to make the different
46 * ways of getting here display the right things. It took a long time to
47 * get it right (wrong?), so be careful.
49 * PUBLIC: int ex_bang __P((SCR *, EXCMD *));
52 ex_bang(SCR
*sp
, EXCMD
*cmdp
)
54 enum filtertype ftype
;
66 ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
70 /* Set the "last bang command" remembered value. */
72 if (exp
->lastbcomm
!= NULL
)
74 if ((exp
->lastbcomm
= v_wstrdup(sp
, ap
->bp
, ap
->len
)) == NULL
) {
75 msgq(sp
, M_SYSERR
, NULL
);
80 * If the command was modified by the expansion, it was historically
83 if (F_ISSET(cmdp
, E_MODIFY
) && !F_ISSET(sp
, SC_EX_SILENT
)) {
85 * Display the command if modified. Historic ex/vi displayed
86 * the command if it was modified due to file name and/or bang
87 * expansion. If piping lines in vi, it would be immediately
88 * overwritten by any error or line change reporting.
90 if (F_ISSET(sp
, SC_VI
))
91 vs_update(sp
, "!", ap
->bp
);
93 INT2CHAR(sp
, ap
->bp
, ap
->len
+1, np
, nlen
);
94 (void)ex_printf(sp
, "!%s\n", np
);
100 * If no addresses were specified, run the command. If there's an
101 * underlying file, it's been modified and autowrite is set, write
102 * the file back. If the file has been modified, autowrite is not
103 * set and the warn option is set, tell the user about the file.
105 if (cmdp
->addrcnt
== 0) {
107 if (sp
->ep
!= NULL
&& F_ISSET(sp
->ep
, F_MODIFIED
)) {
108 if (O_ISSET(sp
, O_AUTOWRITE
)) {
109 if (file_aw(sp
, FS_ALL
))
111 } else if (O_ISSET(sp
, O_WARN
) &&
112 !F_ISSET(sp
, SC_EX_SILENT
))
114 "303|File modified since last write.",
118 /* If we're still in a vi screen, move out explicitly. */
119 INT2CHAR(sp
, ap
->bp
, ap
->len
+1, np
, nlen
);
120 (void)ex_exec_proc(sp
,
121 cmdp
, np
, msg
, !F_ISSET(sp
, SC_EX
| SC_SCR_EXWROTE
));
125 * If addresses were specified, pipe lines from the file through the
128 * Historically, vi lines were replaced by both the stdout and stderr
129 * lines of the command, but ex lines by only the stdout lines. This
130 * makes no sense to me, so nvi makes it consistent for both, and
131 * matches vi's historic behavior.
136 /* Autoprint is set historically, even if the command fails. */
137 F_SET(cmdp
, E_AUTOPRINT
);
141 * Historical vi permitted "!!" in an empty file. When this
142 * happens, we arrive here with two addresses of 1,1 and a
143 * bad attitude. The simple solution is to turn it into a
144 * FILTER_READ operation, with the exception that stdin isn't
145 * opened for the utility, and the cursor position isn't the
146 * same. The only historic glitch (I think) is that we don't
147 * put an empty line into the default cut buffer, as historic
148 * vi did. Imagine, if you can, my disappointment.
151 if (cmdp
->addr1
.lno
== 1 && cmdp
->addr2
.lno
== 1) {
152 if (db_last(sp
, &lno
))
155 cmdp
->addr1
.lno
= cmdp
->addr2
.lno
= 0;
156 ftype
= FILTER_RBANG
;
159 rval
= ex_filter(sp
, cmdp
,
160 &cmdp
->addr1
, &cmdp
->addr2
, &rm
, ap
->bp
, ftype
);
163 * If in vi mode, move to the first nonblank.
166 * Historic vi wasn't consistent in this area -- if you used
167 * a forward motion it moved to the first nonblank, but if you
168 * did a backward motion it didn't. And, if you followed a
169 * backward motion with a forward motion, it wouldn't move to
170 * the nonblank for either. Going to the nonblank generally
171 * seems more useful and consistent, so we do it.
174 if (F_ISSET(sp
, SC_VI
)) {
176 (void)nonblank(sp
, sp
->lno
, &sp
->cno
);
181 /* Ex terminates with a bang, even if the command fails. */
182 if (!F_ISSET(sp
, SC_VI
) && !F_ISSET(sp
, SC_EX_SILENT
))
183 (void)ex_puts(sp
, "!\n");
187 * The ! commands never return an error, so that autoprint always
188 * happens in the ex parser.