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