ex_at.c revision 302408
1193323Sed/*- 2193323Sed * Copyright (c) 1992, 1993, 1994 3193323Sed * The Regents of the University of California. All rights reserved. 4193323Sed * Copyright (c) 1992, 1993, 1994, 1995, 1996 5193323Sed * Keith Bostic. All rights reserved. 6193323Sed * 7193323Sed * See the LICENSE file for redistribution information. 8193323Sed */ 9193323Sed 10193323Sed#include "config.h" 11193323Sed 12193323Sed#ifndef lint 13193323Sedstatic const char sccsid[] = "$Id: ex_at.c,v 10.16 2001/06/25 15:19:14 skimo Exp $"; 14193323Sed#endif /* not lint */ 15193323Sed 16193399Sed#include <sys/types.h> 17193323Sed#include <sys/queue.h> 18193323Sed#include <sys/time.h> 19193399Sed 20198090Srdivacky#include <bitstring.h> 21193323Sed#include <ctype.h> 22193323Sed#include <limits.h> 23193323Sed#include <stdio.h> 24193323Sed#include <stdlib.h> 25193323Sed#include <string.h> 26193323Sed 27193323Sed#include "../common/common.h" 28193323Sed 29193323Sed/* 30193323Sed * ex_at -- :@[@ | buffer] 31193323Sed * :*[* | buffer] 32193323Sed * 33193323Sed * Execute the contents of the buffer. 34193323Sed * 35193323Sed * PUBLIC: int ex_at(SCR *, EXCMD *); 36193323Sed */ 37193323Sedint 38193323Sedex_at(SCR *sp, EXCMD *cmdp) 39193323Sed{ 40193323Sed CB *cbp; 41193323Sed CHAR_T name; 42193323Sed EXCMD *ecp; 43193323Sed RANGE *rp; 44193323Sed TEXT *tp; 45193323Sed size_t len = 0; 46193323Sed CHAR_T *p; 47193323Sed 48193323Sed /* 49193323Sed * !!! 50193323Sed * Historically, [@*]<carriage-return> and [@*][@*] executed the most 51193323Sed * recently executed buffer in ex mode. 52193323Sed */ 53193323Sed name = FL_ISSET(cmdp->iflags, E_C_BUFFER) ? cmdp->buffer : '@'; 54193323Sed if (name == '@' || name == '*') { 55193323Sed if (!F_ISSET(sp, SC_AT_SET)) { 56193323Sed ex_emsg(sp, NULL, EXM_NOPREVBUF); 57193323Sed return (1); 58193323Sed } 59193323Sed name = sp->at_lbuf; 60193323Sed } 61193323Sed sp->at_lbuf = name; 62193323Sed F_SET(sp, SC_AT_SET); 63193323Sed 64193323Sed CBNAME(sp, cbp, name); 65193323Sed if (cbp == NULL) { 66193323Sed ex_emsg(sp, KEY_NAME(sp, name), EXM_EMPTYBUF); 67193323Sed return (1); 68193323Sed } 69193323Sed 70193323Sed /* 71193323Sed * !!! 72193323Sed * Historically the @ command took a range of lines, and the @ buffer 73193323Sed * was executed once per line. The historic vi could be trashed by 74193323Sed * this because it didn't notice if the underlying file changed, or, 75193323Sed * for that matter, if there were no more lines on which to operate. 76193323Sed * For example, take a 10 line file, load "%delete" into a buffer, 77193323Sed * and enter :8,10@<buffer>. 78193323Sed * 79193323Sed * The solution is a bit tricky. If the user specifies a range, take 80193323Sed * the same approach as for global commands, and discard the command 81193323Sed * if exit or switch to a new file/screen. If the user doesn't specify 82193323Sed * the range, continue to execute after a file/screen switch, which 83193323Sed * means @ buffers are still useful in a multi-screen environment. 84193323Sed */ 85193323Sed CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD)); 86193323Sed TAILQ_INIT(ecp->rq); 87193323Sed CALLOC_RET(sp, rp, RANGE *, 1, sizeof(RANGE)); 88193323Sed rp->start = cmdp->addr1.lno; 89193323Sed if (F_ISSET(cmdp, E_ADDR_DEF)) { 90193323Sed rp->stop = rp->start; 91193323Sed FL_SET(ecp->agv_flags, AGV_AT_NORANGE); 92193323Sed } else { 93193323Sed rp->stop = cmdp->addr2.lno; 94193323Sed FL_SET(ecp->agv_flags, AGV_AT); 95193323Sed } 96193323Sed TAILQ_INSERT_HEAD(ecp->rq, rp, q); 97193323Sed 98193323Sed /* 99193323Sed * Buffers executed in ex mode or from the colon command line in vi 100193323Sed * were ex commands. We can't push it on the terminal queue, since 101193323Sed * it has to be executed immediately, and we may be in the middle of 102193323Sed * an ex command already. Push the command on the ex command stack. 103193323Sed * Build two copies of the command. We need two copies because the 104193323Sed * ex parser may step on the command string when it's parsing it. 105193323Sed */ 106193323Sed TAILQ_FOREACH_REVERSE(tp, cbp->textq, _texth, q) 107193323Sed len += tp->len + 1; 108193323Sed 109193323Sed MALLOC_RET(sp, ecp->cp, CHAR_T *, len * 2 * sizeof(CHAR_T)); 110193323Sed ecp->o_cp = ecp->cp; 111193323Sed ecp->o_clen = len; 112193323Sed ecp->cp[len] = '\0'; 113193323Sed 114193323Sed /* Copy the buffer into the command space. */ 115193323Sed p = ecp->cp + len; 116193323Sed TAILQ_FOREACH_REVERSE(tp, cbp->textq, _texth, q) { 117193323Sed MEMCPY(p, tp->lb, tp->len); 118193323Sed p += tp->len; 119193323Sed *p++ = '\n'; 120193323Sed } 121193323Sed 122193323Sed SLIST_INSERT_HEAD(sp->gp->ecq, ecp, q); 123193323Sed return (0); 124193323Sed} 125193323Sed