NetBSD/usr.bin/vi/ex/ex_bang.c

189 lines
5.2 KiB
C
Raw Normal View History

1998-01-09 11:03:16 +03:00
/* $NetBSD: ex_bang.c,v 1.7 1998/01/09 08:07:42 perry Exp $ */
/*-
* Copyright (c) 1992, 1993, 1994
* The Regents of the University of California. All rights reserved.
1996-05-20 07:47:00 +04:00
* Copyright (c) 1992, 1993, 1994, 1995, 1996
* Keith Bostic. All rights reserved.
*
1996-05-20 07:47:00 +04:00
* See the LICENSE file for redistribution information.
*/
1996-05-20 07:47:00 +04:00
#include "config.h"
#ifndef lint
1996-05-20 07:47:00 +04:00
static const char sccsid[] = "@(#)ex_bang.c 10.32 (Berkeley) 5/18/96";
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>
#include <bitstring.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
1996-05-20 07:47:00 +04:00
#include "../common/common.h"
#include "../vi/vi.h"
/*
* ex_bang -- :[line [,line]] ! command
*
* Pass the rest of the line after the ! character to the program named by
* the O_SHELL option.
*
* Historical vi did NOT do shell expansion on the arguments before passing
* them, only file name expansion. This means that the O_SHELL program got
* "$t" as an argument if that is what the user entered. Also, there's a
* special expansion done for the bang command. Any exclamation points in
* the user's argument are replaced by the last, expanded ! command.
*
* There's some fairly amazing slop in this routine to make the different
* ways of getting here display the right things. It took a long time to
* get it right (wrong?), so be careful.
1996-05-20 07:47:00 +04:00
*
* PUBLIC: int ex_bang __P((SCR *, EXCMD *));
*/
int
1996-05-20 07:47:00 +04:00
ex_bang(sp, cmdp)
SCR *sp;
1996-05-20 07:47:00 +04:00
EXCMD *cmdp;
{
enum filtertype ftype;
ARGS *ap;
EX_PRIVATE *exp;
MARK rm;
recno_t lno;
int rval;
1996-05-20 07:47:00 +04:00
const char *msg;
ap = cmdp->argv[0];
if (ap->len == 0) {
1996-05-20 07:47:00 +04:00
ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
return (1);
}
1996-05-20 07:47:00 +04:00
/* Set the "last bang command" remembered value. */
exp = EXP(sp);
if (exp->lastbcomm != NULL)
free(exp->lastbcomm);
if ((exp->lastbcomm = strdup(ap->bp)) == NULL) {
msgq(sp, M_SYSERR, NULL);
return (1);
}
/*
1996-05-20 07:47:00 +04:00
* If the command was modified by the expansion, it was historically
* redisplayed.
*/
1996-05-20 07:47:00 +04:00
if (F_ISSET(cmdp, E_MODIFY) && !F_ISSET(sp, SC_EX_SILENT)) {
/*
1996-05-20 07:47:00 +04:00
* Display the command if modified. Historic ex/vi displayed
* the command if it was modified due to file name and/or bang
1996-05-20 07:47:00 +04:00
* expansion. If piping lines in vi, it would be immediately
* overwritten by any error or line change reporting.
*/
1996-05-20 07:47:00 +04:00
if (F_ISSET(sp, SC_VI))
vs_update(sp, "!", ap->bp);
else {
(void)ex_printf(sp, "!%s\n", ap->bp);
(void)ex_fflush(sp);
}
}
1996-05-20 07:47:00 +04:00
/*
* If no addresses were specified, run the command. If the file has
* been modified and autowrite is set, write the file back. If the
* file has been modified, autowrite is not set and the warn option is
* set, tell the user about the file.
*/
if (cmdp->addrcnt == 0) {
msg = NULL;
if (F_ISSET(sp->ep, F_MODIFIED))
if (O_ISSET(sp, O_AUTOWRITE)) {
if (file_aw(sp, FS_ALL))
return (0);
} else if (O_ISSET(sp, O_WARN) &&
!F_ISSET(sp, SC_EX_SILENT))
msg = msg_cat(sp,
"303|File modified since last write.",
NULL);
/* If we're still in a vi screen, move out explicitly. */
(void)ex_exec_proc(sp,
cmdp, ap->bp, msg, !F_ISSET(sp, SC_EX | SC_SCR_EXWROTE));
}
/*
* If addresses were specified, pipe lines from the file through the
* command.
*
* Historically, vi lines were replaced by both the stdout and stderr
1996-05-20 07:47:00 +04:00
* lines of the command, but ex lines by only the stdout lines. This
* makes no sense to me, so nvi makes it consistent for both, and
* matches vi's historic behavior.
*/
1996-05-20 07:47:00 +04:00
else {
NEEDFILE(sp, cmdp);
1996-05-20 07:47:00 +04:00
/* Autoprint is set historically, even if the command fails. */
F_SET(cmdp, E_AUTOPRINT);
/*
* !!!
1996-05-20 07:47:00 +04:00
* Historical vi permitted "!!" in an empty file. When this
* happens, we arrive here with two addresses of 1,1 and a
* bad attitude. The simple solution is to turn it into a
1996-05-20 07:47:00 +04:00
* FILTER_READ operation, with the exception that stdin isn't
* opened for the utility, and the cursor position isn't the
* same. The only historic glitch (I think) is that we don't
* put an empty line into the default cut buffer, as historic
* vi did. Imagine, if you can, my disappointment.
*/
1996-05-20 07:47:00 +04:00
ftype = FILTER_BANG;
if (cmdp->addr1.lno == 1 && cmdp->addr2.lno == 1) {
1996-05-20 07:47:00 +04:00
if (db_last(sp, &lno))
return (1);
if (lno == 0) {
cmdp->addr1.lno = cmdp->addr2.lno = 0;
1996-05-20 07:47:00 +04:00
ftype = FILTER_RBANG;
}
}
1996-05-20 07:47:00 +04:00
rval = ex_filter(sp, cmdp,
&cmdp->addr1, &cmdp->addr2, &rm, ap->bp, ftype);
/*
* If in vi mode, move to the first nonblank.
*
* !!!
* Historic vi wasn't consistent in this area -- if you used
* a forward motion it moved to the first nonblank, but if you
* did a backward motion it didn't. And, if you followed a
* backward motion with a forward motion, it wouldn't move to
* the nonblank for either. Going to the nonblank generally
1996-05-20 07:47:00 +04:00
* seems more useful and consistent, so we do it.
*/
1996-05-20 07:47:00 +04:00
sp->lno = rm.lno;
if (F_ISSET(sp, SC_VI)) {
sp->cno = 0;
(void)nonblank(sp, sp->lno, &sp->cno);
} else
sp->cno = rm.cno;
}
1996-05-20 07:47:00 +04:00
/* Ex terminates with a bang, even if the command fails. */
if (!F_ISSET(sp, SC_VI) && !F_ISSET(sp, SC_EX_SILENT))
(void)ex_puts(sp, "!\n");
/*
* XXX
* The ! commands never return an error, so that autoprint always
* happens in the ex parser.
*/
return (0);
}