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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
24 #include "../common/common.h"
28 * ex_bang -- :[line [,line]] ! command
30 * Pass the rest of the line after the ! character to the program named by
33 * Historical vi did NOT do shell expansion on the arguments before passing
34 * them, only file name expansion. This means that the O_SHELL program got
35 * "$t" as an argument if that is what the user entered. Also, there's a
36 * special expansion done for the bang command. Any exclamation points in
37 * the user's argument are replaced by the last, expanded ! command.
39 * There's some fairly amazing slop in this routine to make the different
40 * ways of getting here display the right things. It took a long time to
41 * get it right (wrong?), so be careful.
43 * PUBLIC: int ex_bang(SCR *, EXCMD *);
46 ex_bang(SCR *sp, EXCMD *cmdp)
48 enum filtertype ftype;
60 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
64 /* Set the "last bang command" remembered value. */
67 if ((exp->lastbcomm = v_wstrdup(sp, ap->bp, ap->len)) == NULL) {
68 msgq(sp, M_SYSERR, NULL);
73 * If the command was modified by the expansion, it was historically
76 if (F_ISSET(cmdp, E_MODIFY) && !F_ISSET(sp, SC_EX_SILENT)) {
78 * Display the command if modified. Historic ex/vi displayed
79 * the command if it was modified due to file name and/or bang
80 * expansion. If piping lines in vi, it would be immediately
81 * overwritten by any error or line change reporting.
83 if (F_ISSET(sp, SC_VI))
84 vs_update(sp, "!", ap->bp);
86 (void)ex_printf(sp, "!"WS"\n", ap->bp);
92 * If no addresses were specified, run the command. If there's an
93 * underlying file, it's been modified and autowrite is set, write
94 * the file back. If the file has been modified, autowrite is not
95 * set and the warn option is set, tell the user about the file.
97 if (cmdp->addrcnt == 0) {
99 if (sp->ep != NULL && F_ISSET(sp->ep, F_MODIFIED))
100 if (O_ISSET(sp, O_AUTOWRITE)) {
101 if (file_aw(sp, FS_ALL))
103 } else if (O_ISSET(sp, O_WARN) &&
104 !F_ISSET(sp, SC_EX_SILENT))
106 "303|File modified since last write.",
109 /* If we're still in a vi screen, move out explicitly. */
110 INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
111 (void)ex_exec_proc(sp,
112 cmdp, np, msg, !F_ISSET(sp, SC_EX | SC_SCR_EXWROTE));
116 * If addresses were specified, pipe lines from the file through the
119 * Historically, vi lines were replaced by both the stdout and stderr
120 * lines of the command, but ex lines by only the stdout lines. This
121 * makes no sense to me, so nvi makes it consistent for both, and
122 * matches vi's historic behavior.
127 /* Autoprint is set historically, even if the command fails. */
128 F_SET(cmdp, E_AUTOPRINT);
132 * Historical vi permitted "!!" in an empty file. When this
133 * happens, we arrive here with two addresses of 1,1 and a
134 * bad attitude. The simple solution is to turn it into a
135 * FILTER_READ operation, with the exception that stdin isn't
136 * opened for the utility, and the cursor position isn't the
137 * same. The only historic glitch (I think) is that we don't
138 * put an empty line into the default cut buffer, as historic
139 * vi did. Imagine, if you can, my disappointment.
142 if (cmdp->addr1.lno == 1 && cmdp->addr2.lno == 1) {
143 if (db_last(sp, &lno))
146 cmdp->addr1.lno = cmdp->addr2.lno = 0;
147 ftype = FILTER_RBANG;
150 rval = ex_filter(sp, cmdp,
151 &cmdp->addr1, &cmdp->addr2, &rm, ap->bp, ftype);
154 * If in vi mode, move to the first nonblank.
157 * Historic vi wasn't consistent in this area -- if you used
158 * a forward motion it moved to the first nonblank, but if you
159 * did a backward motion it didn't. And, if you followed a
160 * backward motion with a forward motion, it wouldn't move to
161 * the nonblank for either. Going to the nonblank generally
162 * seems more useful and consistent, so we do it.
165 if (F_ISSET(sp, SC_VI)) {
167 (void)nonblank(sp, sp->lno, &sp->cno);
172 /* Ex terminates with a bang, even if the command fails. */
173 if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
174 (void)ex_puts(sp, "!\n");
176 /* Apply expandtab to the new text */
177 if (O_ISSET(sp, O_EXPANDTAB))
182 * The ! commands never return an error, so that autoprint always
183 * happens in the ex parser.