Home | History | Annotate | Line # | Download | only in vi
v_at.c revision 1.3
      1  1.3  christos /*	$NetBSD: v_at.c,v 1.3 2013/11/25 22:43:46 christos Exp $ */
      2  1.1  christos /*-
      3  1.1  christos  * Copyright (c) 1992, 1993, 1994
      4  1.1  christos  *	The Regents of the University of California.  All rights reserved.
      5  1.1  christos  * Copyright (c) 1992, 1993, 1994, 1995, 1996
      6  1.1  christos  *	Keith Bostic.  All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * See the LICENSE file for redistribution information.
      9  1.1  christos  */
     10  1.1  christos 
     11  1.1  christos #include "config.h"
     12  1.1  christos 
     13  1.1  christos #ifndef lint
     14  1.1  christos static const char sccsid[] = "Id: v_at.c,v 10.11 2001/06/25 15:19:30 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:30 ";
     15  1.1  christos #endif /* not lint */
     16  1.1  christos 
     17  1.1  christos #include <sys/types.h>
     18  1.1  christos #include <sys/queue.h>
     19  1.1  christos #include <sys/time.h>
     20  1.1  christos 
     21  1.1  christos #include <bitstring.h>
     22  1.1  christos #include <ctype.h>
     23  1.1  christos #include <limits.h>
     24  1.1  christos #include <stdio.h>
     25  1.1  christos #include <string.h>
     26  1.1  christos 
     27  1.1  christos #include "../common/common.h"
     28  1.1  christos #include "vi.h"
     29  1.1  christos 
     30  1.1  christos /*
     31  1.1  christos  * v_at -- @
     32  1.1  christos  *	Execute a buffer.
     33  1.1  christos  *
     34  1.1  christos  * PUBLIC: int v_at __P((SCR *, VICMD *));
     35  1.1  christos  */
     36  1.1  christos int
     37  1.1  christos v_at(SCR *sp, VICMD *vp)
     38  1.1  christos {
     39  1.1  christos 	CB *cbp;
     40  1.2  christos 	ARG_CHAR_T name;
     41  1.1  christos 	TEXT *tp;
     42  1.1  christos 	size_t len;
     43  1.1  christos 	char nbuf[20];
     44  1.1  christos 	CHAR_T wbuf[20];
     45  1.2  christos 	const CHAR_T *wp;
     46  1.1  christos 	size_t wlen;
     47  1.1  christos 
     48  1.1  christos 	/*
     49  1.1  christos 	 * !!!
     50  1.1  christos 	 * Historically, [@*]<carriage-return> and [@*][@*] executed the most
     51  1.1  christos 	 * recently executed buffer in ex mode.  In vi mode, only @@ repeated
     52  1.1  christos 	 * the last buffer.  We change historic practice and make @* work from
     53  1.1  christos 	 * vi mode as well, it's simpler and more consistent.
     54  1.1  christos 	 *
     55  1.1  christos 	 * My intent is that *[buffer] will, in the future, pass the buffer to
     56  1.1  christos 	 * whatever interpreter is loaded.
     57  1.1  christos 	 */
     58  1.1  christos 	name = F_ISSET(vp, VC_BUFFER) ? vp->buffer : '@';
     59  1.1  christos 	if (name == '@' || name == '*') {
     60  1.1  christos 		if (!F_ISSET(sp, SC_AT_SET)) {
     61  1.1  christos 			ex_emsg(sp, NULL, EXM_NOPREVBUF);
     62  1.1  christos 			return (1);
     63  1.1  christos 		}
     64  1.1  christos 		name = sp->at_lbuf;
     65  1.1  christos 	}
     66  1.1  christos 	F_SET(sp, SC_AT_SET);
     67  1.1  christos 
     68  1.1  christos 	CBNAME(sp, cbp, name);
     69  1.1  christos 	if (cbp == NULL) {
     70  1.2  christos 		ex_emsg(sp, (char *)KEY_NAME(sp, name), EXM_EMPTYBUF);
     71  1.1  christos 		return (1);
     72  1.1  christos 	}
     73  1.1  christos 
     74  1.1  christos 	/* Save for reuse. */
     75  1.1  christos 	sp->at_lbuf = name;
     76  1.1  christos 
     77  1.1  christos 	/*
     78  1.1  christos 	 * The buffer is executed in vi mode, while in vi mode, so simply
     79  1.1  christos 	 * push it onto the terminal queue and continue.
     80  1.1  christos 	 *
     81  1.1  christos 	 * !!!
     82  1.1  christos 	 * Historic practice is that if the buffer was cut in line mode,
     83  1.1  christos 	 * <newlines> were appended to each line as it was pushed onto
     84  1.1  christos 	 * the stack.  If the buffer was cut in character mode, <newlines>
     85  1.1  christos 	 * were appended to all lines but the last one.
     86  1.1  christos 	 *
     87  1.1  christos 	 * XXX
     88  1.1  christos 	 * Historic practice is that execution of an @ buffer could be
     89  1.1  christos 	 * undone by a single 'u' command, i.e. the changes were grouped
     90  1.1  christos 	 * together.  We don't get this right; I'm waiting for the new DB
     91  1.1  christos 	 * logging code to be available.
     92  1.1  christos 	 */
     93  1.3  christos 	TAILQ_FOREACH_REVERSE(tp, &cbp->textq, _texth, q) {
     94  1.1  christos 		static CHAR_T nl[] = { '\n', 0 };
     95  1.2  christos 		if (((F_ISSET(cbp, CB_LMODE) ||
     96  1.3  christos 		    TAILQ_NEXT(tp, q) != NULL) &&
     97  1.2  christos 		    v_event_push(sp, NULL, nl, 1, 0)) ||
     98  1.1  christos 		    v_event_push(sp, NULL, tp->lb, tp->len, 0))
     99  1.1  christos 			return (1);
    100  1.1  christos 	}
    101  1.1  christos 
    102  1.1  christos 	/*
    103  1.1  christos 	 * !!!
    104  1.1  christos 	 * If any count was supplied, it applies to the first command in the
    105  1.1  christos 	 * at buffer.
    106  1.1  christos 	 */
    107  1.1  christos 	if (F_ISSET(vp, VC_C1SET)) {
    108  1.1  christos 		len = snprintf(nbuf, sizeof(nbuf), "%lu", vp->count);
    109  1.1  christos 		CHAR2INT(sp, nbuf, len, wp, wlen);
    110  1.1  christos 		MEMCPYW(wbuf, wp, wlen);
    111  1.1  christos 		if (v_event_push(sp, NULL, wp, wlen, 0))
    112  1.1  christos 			return (1);
    113  1.1  christos 	}
    114  1.1  christos 	return (0);
    115  1.1  christos }
    116