tools/llvm: Do not build with symbols
[minix3.git] / external / bsd / nvi / dist / ex / ex_bang.c
blobbc814cd2cf362530de5357fef47734c348de6739
1 /* $NetBSD: ex_bang.c,v 1.2 2013/11/22 15:52:05 christos Exp $ */
2 /*-
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
9 */
11 #include "config.h"
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/time.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "../vi/vi.h"
33 * ex_bang -- :[line [,line]] ! command
35 * Pass the rest of the line after the ! character to the program named by
36 * the O_SHELL option.
38 * Historical vi did NOT do shell expansion on the arguments before passing
39 * them, only file name expansion. This means that the O_SHELL program got
40 * "$t" as an argument if that is what the user entered. Also, there's a
41 * special expansion done for the bang command. Any exclamation points in
42 * the user's argument are replaced by the last, expanded ! command.
44 * There's some fairly amazing slop in this routine to make the different
45 * ways of getting here display the right things. It took a long time to
46 * get it right (wrong?), so be careful.
48 * PUBLIC: int ex_bang __P((SCR *, EXCMD *));
50 int
51 ex_bang(SCR *sp, EXCMD *cmdp)
53 enum filtertype ftype;
54 ARGS *ap;
55 EX_PRIVATE *exp;
56 MARK rm;
57 db_recno_t lno;
58 const char *msg;
59 const char *np;
60 size_t nlen;
62 ap = cmdp->argv[0];
63 if (ap->len == 0) {
64 ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
65 return (1);
68 /* Set the "last bang command" remembered value. */
69 exp = EXP(sp);
70 if (exp->lastbcomm != NULL)
71 free(exp->lastbcomm);
72 if ((exp->lastbcomm = v_wstrdup(sp, ap->bp, ap->len)) == NULL) {
73 msgq(sp, M_SYSERR, NULL);
74 return (1);
78 * If the command was modified by the expansion, it was historically
79 * redisplayed.
81 if (F_ISSET(cmdp, E_MODIFY) && !F_ISSET(sp, SC_EX_SILENT)) {
83 * Display the command if modified. Historic ex/vi displayed
84 * the command if it was modified due to file name and/or bang
85 * expansion. If piping lines in vi, it would be immediately
86 * overwritten by any error or line change reporting.
88 if (F_ISSET(sp, SC_VI))
89 vs_update(sp, "!", ap->bp);
90 else {
91 INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
92 (void)ex_printf(sp, "!%s\n", np);
93 (void)ex_fflush(sp);
98 * If no addresses were specified, run the command. If there's an
99 * underlying file, it's been modified and autowrite is set, write
100 * the file back. If the file has been modified, autowrite is not
101 * set and the warn option is set, tell the user about the file.
103 if (cmdp->addrcnt == 0) {
104 msg = NULL;
105 if (sp->ep != NULL && F_ISSET(sp->ep, F_MODIFIED)) {
106 if (O_ISSET(sp, O_AUTOWRITE)) {
107 if (file_aw(sp, FS_ALL))
108 return (0);
109 } else if (O_ISSET(sp, O_WARN) &&
110 !F_ISSET(sp, SC_EX_SILENT))
111 msg = msg_cat(sp,
112 "303|File modified since last write.",
113 NULL);
116 /* If we're still in a vi screen, move out explicitly. */
117 INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
118 (void)ex_exec_proc(sp,
119 cmdp, np, msg, !F_ISSET(sp, SC_EX | SC_SCR_EXWROTE));
123 * If addresses were specified, pipe lines from the file through the
124 * command.
126 * Historically, vi lines were replaced by both the stdout and stderr
127 * lines of the command, but ex lines by only the stdout lines. This
128 * makes no sense to me, so nvi makes it consistent for both, and
129 * matches vi's historic behavior.
131 else {
132 NEEDFILE(sp, cmdp);
134 /* Autoprint is set historically, even if the command fails. */
135 F_SET(cmdp, E_AUTOPRINT);
138 * !!!
139 * Historical vi permitted "!!" in an empty file. When this
140 * happens, we arrive here with two addresses of 1,1 and a
141 * bad attitude. The simple solution is to turn it into a
142 * FILTER_READ operation, with the exception that stdin isn't
143 * opened for the utility, and the cursor position isn't the
144 * same. The only historic glitch (I think) is that we don't
145 * put an empty line into the default cut buffer, as historic
146 * vi did. Imagine, if you can, my disappointment.
148 ftype = FILTER_BANG;
149 if (cmdp->addr1.lno == 1 && cmdp->addr2.lno == 1) {
150 if (db_last(sp, &lno))
151 return (1);
152 if (lno == 0) {
153 cmdp->addr1.lno = cmdp->addr2.lno = 0;
154 ftype = FILTER_RBANG;
157 (void)ex_filter(sp, cmdp,
158 &cmdp->addr1, &cmdp->addr2, &rm, ap->bp, ftype);
161 * If in vi mode, move to the first nonblank.
163 * !!!
164 * Historic vi wasn't consistent in this area -- if you used
165 * a forward motion it moved to the first nonblank, but if you
166 * did a backward motion it didn't. And, if you followed a
167 * backward motion with a forward motion, it wouldn't move to
168 * the nonblank for either. Going to the nonblank generally
169 * seems more useful and consistent, so we do it.
171 sp->lno = rm.lno;
172 if (F_ISSET(sp, SC_VI)) {
173 sp->cno = 0;
174 (void)nonblank(sp, sp->lno, &sp->cno);
175 } else
176 sp->cno = rm.cno;
179 /* Ex terminates with a bang, even if the command fails. */
180 if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
181 (void)ex_puts(sp, "!\n");
184 * XXX
185 * The ! commands never return an error, so that autoprint always
186 * happens in the ex parser.
188 return (0);