1994-01-24 08:52:58 +03:00
|
|
|
/*-
|
1994-03-28 08:28:20 +04:00
|
|
|
* Copyright (c) 1992, 1993, 1994
|
1994-01-24 08:52:58 +03:00
|
|
|
* 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.
|
1994-01-24 08:52:58 +03:00
|
|
|
*
|
1996-05-20 07:47:00 +04:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-01-24 08:52:58 +03:00
|
|
|
#ifndef lint
|
1996-05-20 07:47:00 +04:00
|
|
|
static const char sccsid[] = "@(#)ex_at.c 10.10 (Berkeley) 4/27/96";
|
1994-01-24 08:52:58 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <sys/queue.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <bitstring.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <ctype.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
1994-01-24 08:52:58 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1994-03-28 08:28:20 +04:00
|
|
|
|
1996-05-20 07:47:00 +04:00
|
|
|
#include "../common/common.h"
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 *));
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
|
|
|
int
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_at(sp, cmdp)
|
1994-01-24 08:52:58 +03:00
|
|
|
SCR *sp;
|
1996-05-20 07:47:00 +04:00
|
|
|
EXCMD *cmdp;
|
1994-01-24 08:52:58 +03:00
|
|
|
{
|
|
|
|
CB *cbp;
|
1996-05-20 07:47:00 +04:00
|
|
|
CHAR_T name;
|
|
|
|
EXCMD *ecp;
|
|
|
|
RANGE *rp;
|
1994-01-24 08:52:58 +03:00
|
|
|
TEXT *tp;
|
1996-05-20 07:47:00 +04:00
|
|
|
size_t len;
|
|
|
|
char *p;
|
1994-01-24 08:52:58 +03:00
|
|
|
|
1994-08-17 20:35:35 +04:00
|
|
|
/*
|
|
|
|
* !!!
|
|
|
|
* Historically, [@*]<carriage-return> and [@*][@*] executed the most
|
1996-05-20 07:47:00 +04:00
|
|
|
* recently executed buffer in ex mode.
|
1994-08-17 20:35:35 +04:00
|
|
|
*/
|
1996-05-20 07:47:00 +04:00
|
|
|
name = FL_ISSET(cmdp->iflags, E_C_BUFFER) ? cmdp->buffer : '@';
|
1994-08-17 20:35:35 +04:00
|
|
|
if (name == '@' || name == '*') {
|
1996-05-20 07:47:00 +04:00
|
|
|
if (!F_ISSET(sp, SC_AT_SET)) {
|
|
|
|
ex_emsg(sp, NULL, EXM_NOPREVBUF);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
name = sp->at_lbuf;
|
1994-01-24 08:52:58 +03:00
|
|
|
}
|
1996-05-20 07:47:00 +04:00
|
|
|
sp->at_lbuf = name;
|
|
|
|
F_SET(sp, SC_AT_SET);
|
1994-01-24 08:52:58 +03:00
|
|
|
|
|
|
|
CBNAME(sp, cbp, name);
|
|
|
|
if (cbp == NULL) {
|
1996-05-20 07:47:00 +04:00
|
|
|
ex_emsg(sp, KEY_NAME(sp, name), EXM_EMPTYBUF);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1994-08-17 20:35:35 +04:00
|
|
|
* !!!
|
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.
|
1994-01-24 08:52:58 +03:00
|
|
|
*/
|
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;
|
1994-08-17 20:35:35 +04:00
|
|
|
tp != (void *)&cbp->textq; tp = tp->q.cqe_prev)
|
1996-05-20 07:47:00 +04:00
|
|
|
len += tp->len + 1;
|
|
|
|
|
|
|
|
/* See ex.h for a discussion of SEARCH_TERMINATION. */
|
|
|
|
MALLOC_RET(sp, ecp->cp, char *, len * 2 + SEARCH_TERMINATION);
|
|
|
|
ecp->o_cp = ecp->cp;
|
|
|
|
ecp->o_clen = len;
|
|
|
|
ecp->cp[len] = '\0';
|
|
|
|
|
|
|
|
/* Copy the buffer into the command space. */
|
|
|
|
for (p = ecp->cp + len + SEARCH_TERMINATION, tp = cbp->textq.cqh_last;
|
|
|
|
tp != (void *)&cbp->textq; tp = tp->q.cqe_prev) {
|
|
|
|
memmove(p, tp->lb, tp->len);
|
|
|
|
p += tp->len;
|
|
|
|
*p++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&sp->gp->ecq, ecp, q);
|
1994-01-24 08:52:58 +03:00
|
|
|
return (0);
|
|
|
|
}
|