1*0a6a1f1dSLionel Sambuc /* $NetBSD: ex_bang.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1992, 1993, 1994
484d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
584d9c625SLionel Sambuc * Copyright (c) 1992, 1993, 1994, 1995, 1996
684d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc */
1084d9c625SLionel Sambuc
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ex_bang.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/time.h>
2584d9c625SLionel Sambuc
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <errno.h>
2884d9c625SLionel Sambuc #include <limits.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc
3484d9c625SLionel Sambuc #include "../common/common.h"
3584d9c625SLionel Sambuc #include "../vi/vi.h"
3684d9c625SLionel Sambuc
3784d9c625SLionel Sambuc /*
3884d9c625SLionel Sambuc * ex_bang -- :[line [,line]] ! command
3984d9c625SLionel Sambuc *
4084d9c625SLionel Sambuc * Pass the rest of the line after the ! character to the program named by
4184d9c625SLionel Sambuc * the O_SHELL option.
4284d9c625SLionel Sambuc *
4384d9c625SLionel Sambuc * Historical vi did NOT do shell expansion on the arguments before passing
4484d9c625SLionel Sambuc * them, only file name expansion. This means that the O_SHELL program got
4584d9c625SLionel Sambuc * "$t" as an argument if that is what the user entered. Also, there's a
4684d9c625SLionel Sambuc * special expansion done for the bang command. Any exclamation points in
4784d9c625SLionel Sambuc * the user's argument are replaced by the last, expanded ! command.
4884d9c625SLionel Sambuc *
4984d9c625SLionel Sambuc * There's some fairly amazing slop in this routine to make the different
5084d9c625SLionel Sambuc * ways of getting here display the right things. It took a long time to
5184d9c625SLionel Sambuc * get it right (wrong?), so be careful.
5284d9c625SLionel Sambuc *
5384d9c625SLionel Sambuc * PUBLIC: int ex_bang __P((SCR *, EXCMD *));
5484d9c625SLionel Sambuc */
5584d9c625SLionel Sambuc int
ex_bang(SCR * sp,EXCMD * cmdp)5684d9c625SLionel Sambuc ex_bang(SCR *sp, EXCMD *cmdp)
5784d9c625SLionel Sambuc {
5884d9c625SLionel Sambuc enum filtertype ftype;
5984d9c625SLionel Sambuc ARGS *ap;
6084d9c625SLionel Sambuc EX_PRIVATE *exp;
6184d9c625SLionel Sambuc MARK rm;
6284d9c625SLionel Sambuc db_recno_t lno;
6384d9c625SLionel Sambuc const char *msg;
6484d9c625SLionel Sambuc const char *np;
6584d9c625SLionel Sambuc size_t nlen;
6684d9c625SLionel Sambuc
6784d9c625SLionel Sambuc ap = cmdp->argv[0];
6884d9c625SLionel Sambuc if (ap->len == 0) {
6984d9c625SLionel Sambuc ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
7084d9c625SLionel Sambuc return (1);
7184d9c625SLionel Sambuc }
7284d9c625SLionel Sambuc
7384d9c625SLionel Sambuc /* Set the "last bang command" remembered value. */
7484d9c625SLionel Sambuc exp = EXP(sp);
7584d9c625SLionel Sambuc if (exp->lastbcomm != NULL)
7684d9c625SLionel Sambuc free(exp->lastbcomm);
7784d9c625SLionel Sambuc if ((exp->lastbcomm = v_wstrdup(sp, ap->bp, ap->len)) == NULL) {
7884d9c625SLionel Sambuc msgq(sp, M_SYSERR, NULL);
7984d9c625SLionel Sambuc return (1);
8084d9c625SLionel Sambuc }
8184d9c625SLionel Sambuc
8284d9c625SLionel Sambuc /*
8384d9c625SLionel Sambuc * If the command was modified by the expansion, it was historically
8484d9c625SLionel Sambuc * redisplayed.
8584d9c625SLionel Sambuc */
8684d9c625SLionel Sambuc if (F_ISSET(cmdp, E_MODIFY) && !F_ISSET(sp, SC_EX_SILENT)) {
8784d9c625SLionel Sambuc /*
8884d9c625SLionel Sambuc * Display the command if modified. Historic ex/vi displayed
8984d9c625SLionel Sambuc * the command if it was modified due to file name and/or bang
9084d9c625SLionel Sambuc * expansion. If piping lines in vi, it would be immediately
9184d9c625SLionel Sambuc * overwritten by any error or line change reporting.
9284d9c625SLionel Sambuc */
9384d9c625SLionel Sambuc if (F_ISSET(sp, SC_VI))
9484d9c625SLionel Sambuc vs_update(sp, "!", ap->bp);
9584d9c625SLionel Sambuc else {
9684d9c625SLionel Sambuc INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
9784d9c625SLionel Sambuc (void)ex_printf(sp, "!%s\n", np);
9884d9c625SLionel Sambuc (void)ex_fflush(sp);
9984d9c625SLionel Sambuc }
10084d9c625SLionel Sambuc }
10184d9c625SLionel Sambuc
10284d9c625SLionel Sambuc /*
10384d9c625SLionel Sambuc * If no addresses were specified, run the command. If there's an
10484d9c625SLionel Sambuc * underlying file, it's been modified and autowrite is set, write
10584d9c625SLionel Sambuc * the file back. If the file has been modified, autowrite is not
10684d9c625SLionel Sambuc * set and the warn option is set, tell the user about the file.
10784d9c625SLionel Sambuc */
10884d9c625SLionel Sambuc if (cmdp->addrcnt == 0) {
10984d9c625SLionel Sambuc msg = NULL;
11084d9c625SLionel Sambuc if (sp->ep != NULL && F_ISSET(sp->ep, F_MODIFIED)) {
11184d9c625SLionel Sambuc if (O_ISSET(sp, O_AUTOWRITE)) {
11284d9c625SLionel Sambuc if (file_aw(sp, FS_ALL))
11384d9c625SLionel Sambuc return (0);
11484d9c625SLionel Sambuc } else if (O_ISSET(sp, O_WARN) &&
11584d9c625SLionel Sambuc !F_ISSET(sp, SC_EX_SILENT))
11684d9c625SLionel Sambuc msg = msg_cat(sp,
11784d9c625SLionel Sambuc "303|File modified since last write.",
11884d9c625SLionel Sambuc NULL);
11984d9c625SLionel Sambuc }
12084d9c625SLionel Sambuc
12184d9c625SLionel Sambuc /* If we're still in a vi screen, move out explicitly. */
12284d9c625SLionel Sambuc INT2CHAR(sp, ap->bp, ap->len+1, np, nlen);
12384d9c625SLionel Sambuc (void)ex_exec_proc(sp,
12484d9c625SLionel Sambuc cmdp, np, msg, !F_ISSET(sp, SC_EX | SC_SCR_EXWROTE));
12584d9c625SLionel Sambuc }
12684d9c625SLionel Sambuc
12784d9c625SLionel Sambuc /*
12884d9c625SLionel Sambuc * If addresses were specified, pipe lines from the file through the
12984d9c625SLionel Sambuc * command.
13084d9c625SLionel Sambuc *
13184d9c625SLionel Sambuc * Historically, vi lines were replaced by both the stdout and stderr
13284d9c625SLionel Sambuc * lines of the command, but ex lines by only the stdout lines. This
13384d9c625SLionel Sambuc * makes no sense to me, so nvi makes it consistent for both, and
13484d9c625SLionel Sambuc * matches vi's historic behavior.
13584d9c625SLionel Sambuc */
13684d9c625SLionel Sambuc else {
13784d9c625SLionel Sambuc NEEDFILE(sp, cmdp);
13884d9c625SLionel Sambuc
13984d9c625SLionel Sambuc /* Autoprint is set historically, even if the command fails. */
14084d9c625SLionel Sambuc F_SET(cmdp, E_AUTOPRINT);
14184d9c625SLionel Sambuc
14284d9c625SLionel Sambuc /*
14384d9c625SLionel Sambuc * !!!
14484d9c625SLionel Sambuc * Historical vi permitted "!!" in an empty file. When this
14584d9c625SLionel Sambuc * happens, we arrive here with two addresses of 1,1 and a
14684d9c625SLionel Sambuc * bad attitude. The simple solution is to turn it into a
14784d9c625SLionel Sambuc * FILTER_READ operation, with the exception that stdin isn't
14884d9c625SLionel Sambuc * opened for the utility, and the cursor position isn't the
14984d9c625SLionel Sambuc * same. The only historic glitch (I think) is that we don't
15084d9c625SLionel Sambuc * put an empty line into the default cut buffer, as historic
15184d9c625SLionel Sambuc * vi did. Imagine, if you can, my disappointment.
15284d9c625SLionel Sambuc */
15384d9c625SLionel Sambuc ftype = FILTER_BANG;
15484d9c625SLionel Sambuc if (cmdp->addr1.lno == 1 && cmdp->addr2.lno == 1) {
15584d9c625SLionel Sambuc if (db_last(sp, &lno))
15684d9c625SLionel Sambuc return (1);
15784d9c625SLionel Sambuc if (lno == 0) {
15884d9c625SLionel Sambuc cmdp->addr1.lno = cmdp->addr2.lno = 0;
15984d9c625SLionel Sambuc ftype = FILTER_RBANG;
16084d9c625SLionel Sambuc }
16184d9c625SLionel Sambuc }
16284d9c625SLionel Sambuc (void)ex_filter(sp, cmdp,
16384d9c625SLionel Sambuc &cmdp->addr1, &cmdp->addr2, &rm, ap->bp, ftype);
16484d9c625SLionel Sambuc
16584d9c625SLionel Sambuc /*
16684d9c625SLionel Sambuc * If in vi mode, move to the first nonblank.
16784d9c625SLionel Sambuc *
16884d9c625SLionel Sambuc * !!!
16984d9c625SLionel Sambuc * Historic vi wasn't consistent in this area -- if you used
17084d9c625SLionel Sambuc * a forward motion it moved to the first nonblank, but if you
17184d9c625SLionel Sambuc * did a backward motion it didn't. And, if you followed a
17284d9c625SLionel Sambuc * backward motion with a forward motion, it wouldn't move to
17384d9c625SLionel Sambuc * the nonblank for either. Going to the nonblank generally
17484d9c625SLionel Sambuc * seems more useful and consistent, so we do it.
17584d9c625SLionel Sambuc */
17684d9c625SLionel Sambuc sp->lno = rm.lno;
17784d9c625SLionel Sambuc if (F_ISSET(sp, SC_VI)) {
17884d9c625SLionel Sambuc sp->cno = 0;
17984d9c625SLionel Sambuc (void)nonblank(sp, sp->lno, &sp->cno);
18084d9c625SLionel Sambuc } else
18184d9c625SLionel Sambuc sp->cno = rm.cno;
18284d9c625SLionel Sambuc }
18384d9c625SLionel Sambuc
18484d9c625SLionel Sambuc /* Ex terminates with a bang, even if the command fails. */
18584d9c625SLionel Sambuc if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
18684d9c625SLionel Sambuc (void)ex_puts(sp, "!\n");
18784d9c625SLionel Sambuc
18884d9c625SLionel Sambuc /*
18984d9c625SLionel Sambuc * XXX
19084d9c625SLionel Sambuc * The ! commands never return an error, so that autoprint always
19184d9c625SLionel Sambuc * happens in the ex parser.
19284d9c625SLionel Sambuc */
19384d9c625SLionel Sambuc return (0);
19484d9c625SLionel Sambuc }
195