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

134 lines
3.4 KiB
C
Raw Normal View History

2002-04-09 05:47:30 +04:00
/* $NetBSD: ex_at.c,v 1.9 2002/04/09 01:47:33 thorpej Exp $ */
1998-01-09 11:03:16 +03:00
/*-
* 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"
2002-04-09 05:47:30 +04:00
#include <sys/cdefs.h>
#ifndef lint
2002-04-09 05:47:30 +04:00
#if 0
2001-03-31 15:37:44 +04:00
static const char sccsid[] = "@(#)ex_at.c 10.12 (Berkeley) 9/15/96";
2002-04-09 05:47:30 +04:00
#else
__RCSID("$NetBSD");
#endif
#endif /* not lint */
#include <sys/types.h>
#include <sys/queue.h>
#include <bitstring.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
1996-05-20 07:47:00 +04:00
#include "../common/common.h"
/*
* ex_at -- :@[@ | buffer]
* :*[* | buffer]
*
* Execute the contents of the buffer.
1996-05-20 07:47:00 +04:00
*
* PUBLIC: int ex_at __P((SCR *, EXCMD *));
*/
int
1996-05-20 07:47:00 +04:00
ex_at(sp, cmdp)
SCR *sp;
1996-05-20 07:47:00 +04:00
EXCMD *cmdp;
{
CB *cbp;
1996-05-20 07:47:00 +04:00
CHAR_T name;
EXCMD *ecp;
RANGE *rp;
TEXT *tp;
1996-05-20 07:47:00 +04:00
size_t len;
char *p;
/*
* !!!
* Historically, [@*]<carriage-return> and [@*][@*] executed the most
1996-05-20 07:47:00 +04:00
* recently executed buffer in ex mode.
*/
1996-05-20 07:47:00 +04:00
name = FL_ISSET(cmdp->iflags, E_C_BUFFER) ? cmdp->buffer : '@';
if (name == '@' || name == '*') {
1996-05-20 07:47:00 +04:00
if (!F_ISSET(sp, SC_AT_SET)) {
ex_emsg(sp, NULL, EXM_NOPREVBUF);
return (1);
}
1996-05-20 07:47:00 +04:00
name = sp->at_lbuf;
}
1996-05-20 07:47:00 +04:00
sp->at_lbuf = name;
F_SET(sp, SC_AT_SET);
CBNAME(sp, cbp, name);
if (cbp == NULL) {
1996-05-20 07:47:00 +04:00
ex_emsg(sp, KEY_NAME(sp, name), EXM_EMPTYBUF);
return (1);
}
/*
* !!!
1996-05-20 07:47:00 +04:00
* Historically the @ command took a range of lines, and the @ buffer
* was executed once per line. The historic vi could be trashed by
* this because it didn't notice if the underlying file changed, or,
* for that matter, if there were no more lines on which to operate.
* For example, take a 10 line file, load "%delete" into a buffer,
* and enter :8,10@<buffer>.
*
* The solution is a bit tricky. If the user specifies a range, take
* the same approach as for global commands, and discard the command
* if exit or switch to a new file/screen. If the user doesn't specify
* the range, continue to execute after a file/screen switch, which
* means @ buffers are still useful in a multi-screen environment.
*/
1996-05-20 07:47:00 +04:00
CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
CIRCLEQ_INIT(&ecp->rq);
CALLOC_RET(sp, rp, RANGE *, 1, sizeof(RANGE));
rp->start = cmdp->addr1.lno;
if (F_ISSET(cmdp, E_ADDR_DEF)) {
rp->stop = rp->start;
FL_SET(ecp->agv_flags, AGV_AT_NORANGE);
} else {
rp->stop = cmdp->addr2.lno;
FL_SET(ecp->agv_flags, AGV_AT);
}
CIRCLEQ_INSERT_HEAD(&ecp->rq, rp, q);
/*
* Buffers executed in ex mode or from the colon command line in vi
* were ex commands. We can't push it on the terminal queue, since
* it has to be executed immediately, and we may be in the middle of
* an ex command already. Push the command on the ex command stack.
* Build two copies of the command. We need two copies because the
* ex parser may step on the command string when it's parsing it.
*/
for (len = 0, tp = cbp->textq.cqh_last;
tp != (void *)&cbp->textq; tp = tp->q.cqe_prev)
1996-05-20 07:47:00 +04:00
len += tp->len + 1;
2001-03-31 15:37:44 +04:00
MALLOC_RET(sp, ecp->cp, char *, len * 2);
1996-05-20 07:47:00 +04:00
ecp->o_cp = ecp->cp;
ecp->o_clen = len;
ecp->cp[len] = '\0';
/* Copy the buffer into the command space. */
2001-03-31 15:37:44 +04:00
for (p = ecp->cp + len, tp = cbp->textq.cqh_last;
1996-05-20 07:47:00 +04:00
tp != (void *)&cbp->textq; tp = tp->q.cqe_prev) {
2001-03-31 15:37:44 +04:00
memcpy(p, tp->lb, tp->len);
1996-05-20 07:47:00 +04:00
p += tp->len;
*p++ = '\n';
}
LIST_INSERT_HEAD(&sp->gp->ecq, ecp, q);
return (0);
}