head	1.8;
access;
symbols
	perseant-exfatfs-base-20250801:1.8
	perseant-exfatfs-base-20240630:1.8
	perseant-exfatfs:1.8.0.36
	perseant-exfatfs-base:1.8
	cjep_sun2x:1.8.0.34
	cjep_sun2x-base:1.8
	cjep_staticlib_x-base1:1.8
	cjep_staticlib_x:1.8.0.32
	cjep_staticlib_x-base:1.8
	phil-wifi-20200421:1.8
	phil-wifi-20200411:1.8
	phil-wifi-20200406:1.8
	pgoyette-compat-merge-20190127:1.8
	pgoyette-compat-20190127:1.8
	pgoyette-compat-20190118:1.8
	pgoyette-compat-1226:1.8
	pgoyette-compat-1126:1.8
	pgoyette-compat-1020:1.8
	pgoyette-compat-0930:1.8
	pgoyette-compat-0906:1.8
	pgoyette-compat-0728:1.8
	pgoyette-compat-0625:1.8
	pgoyette-compat-0521:1.8
	pgoyette-compat-0502:1.8
	pgoyette-compat-0422:1.8
	pgoyette-compat-0415:1.8
	pgoyette-compat-0407:1.8
	pgoyette-compat-0330:1.8
	pgoyette-compat-0322:1.8
	pgoyette-compat-0315:1.8
	pgoyette-compat:1.8.0.30
	pgoyette-compat-base:1.8
	perseant-stdc-iso10646:1.8.0.28
	perseant-stdc-iso10646-base:1.8
	prg-localcount2-base3:1.8
	prg-localcount2-base2:1.8
	prg-localcount2-base1:1.8
	prg-localcount2:1.8.0.26
	prg-localcount2-base:1.8
	pgoyette-localcount-20170426:1.8
	bouyer-socketcan-base1:1.8
	pgoyette-localcount-20170320:1.8
	bouyer-socketcan:1.8.0.24
	bouyer-socketcan-base:1.8
	pgoyette-localcount-20170107:1.8
	pgoyette-localcount-20161104:1.8
	localcount-20160914:1.8
	pgoyette-localcount-20160806:1.8
	pgoyette-localcount-20160726:1.8
	pgoyette-localcount:1.8.0.22
	pgoyette-localcount-base:1.8
	netbsd-5-2-3-RELEASE:1.8
	netbsd-5-1-5-RELEASE:1.8
	yamt-pagecache-base9:1.8
	yamt-pagecache-tag8:1.8
	tls-earlyentropy:1.8.0.18
	tls-earlyentropy-base:1.8
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.8
	riastradh-drm2-base3:1.8
	netbsd-5-2-2-RELEASE:1.8
	netbsd-5-1-4-RELEASE:1.8
	netbsd-5-2-1-RELEASE:1.8
	netbsd-5-1-3-RELEASE:1.8
	agc-symver:1.8.0.20
	agc-symver-base:1.8
	tls-maxphys-base:1.8
	yamt-pagecache-base8:1.8
	netbsd-5-2:1.8.0.16
	yamt-pagecache-base7:1.8
	netbsd-5-2-RELEASE:1.8
	netbsd-5-2-RC1:1.8
	yamt-pagecache-base6:1.8
	yamt-pagecache-base5:1.8
	yamt-pagecache-base4:1.8
	netbsd-5-1-2-RELEASE:1.8
	netbsd-5-1-1-RELEASE:1.8
	yamt-pagecache-base3:1.8
	yamt-pagecache-base2:1.8
	yamt-pagecache:1.8.0.14
	yamt-pagecache-base:1.8
	bouyer-quota2-nbase:1.8
	bouyer-quota2:1.8.0.12
	bouyer-quota2-base:1.8
	matt-nb5-pq3:1.8.0.10
	matt-nb5-pq3-base:1.8
	netbsd-5-1:1.8.0.8
	netbsd-5-1-RELEASE:1.8
	netbsd-5-1-RC4:1.8
	netbsd-5-1-RC3:1.8
	netbsd-5-1-RC2:1.8
	netbsd-5-1-RC1:1.8
	netbsd-5-0-2-RELEASE:1.8
	netbsd-5-0-1-RELEASE:1.8
	jym-xensuspend-nbase:1.8
	netbsd-5-0:1.8.0.6
	netbsd-5-0-RELEASE:1.8
	netbsd-5-0-RC4:1.8
	netbsd-5-0-RC3:1.8
	netbsd-5-0-RC2:1.8
	jym-xensuspend:1.8.0.4
	jym-xensuspend-base:1.8
	netbsd-5-0-RC1:1.8
	netbsd-5:1.8.0.2
	netbsd-5-base:1.8
	matt-mips64-base2:1.7
	matt-mips64:1.7.0.26
	mjf-devfs2:1.7.0.24
	mjf-devfs2-base:1.7
	netbsd-4-0-1-RELEASE:1.7
	wrstuden-revivesa-base-3:1.7
	wrstuden-revivesa-base-2:1.7
	wrstuden-fixsa-newbase:1.7
	wrstuden-revivesa-base-1:1.7
	yamt-pf42-base4:1.7
	yamt-pf42-base3:1.7
	hpcarm-cleanup-nbase:1.7
	yamt-pf42-baseX:1.7
	yamt-pf42-base2:1.7
	wrstuden-revivesa:1.7.0.22
	wrstuden-revivesa-base:1.7
	yamt-pf42:1.7.0.20
	yamt-pf42-base:1.7
	keiichi-mipv6-nbase:1.7
	keiichi-mipv6:1.7.0.18
	keiichi-mipv6-base:1.7
	matt-armv6-nbase:1.7
	matt-armv6-prevmlocking:1.7
	wrstuden-fixsa-base-1:1.7
	netbsd-4-0:1.7.0.16
	netbsd-4-0-RELEASE:1.7
	cube-autoconf:1.7.0.14
	cube-autoconf-base:1.7
	netbsd-4-0-RC5:1.7
	netbsd-4-0-RC4:1.7
	netbsd-4-0-RC3:1.7
	netbsd-4-0-RC2:1.7
	netbsd-4-0-RC1:1.7
	matt-armv6:1.7.0.12
	matt-armv6-base:1.7
	matt-mips64-base:1.7
	hpcarm-cleanup:1.7.0.10
	hpcarm-cleanup-base:1.7
	netbsd-3-1-1-RELEASE:1.6
	netbsd-3-0-3-RELEASE:1.6
	wrstuden-fixsa:1.7.0.8
	wrstuden-fixsa-base:1.7
	abandoned-netbsd-4-base:1.7
	abandoned-netbsd-4:1.7.0.4
	netbsd-3-1:1.6.0.14
	netbsd-3-1-RELEASE:1.6
	netbsd-3-0-2-RELEASE:1.6
	netbsd-3-1-RC4:1.6
	netbsd-3-1-RC3:1.6
	netbsd-3-1-RC2:1.6
	netbsd-3-1-RC1:1.6
	netbsd-4:1.7.0.6
	netbsd-4-base:1.7
	chap-midi-nbase:1.7
	netbsd-3-0-1-RELEASE:1.6
	chap-midi:1.7.0.2
	chap-midi-base:1.7
	netbsd-3-0:1.6.0.12
	netbsd-3-0-RELEASE:1.6
	netbsd-3-0-RC6:1.6
	netbsd-3-0-RC5:1.6
	netbsd-3-0-RC4:1.6
	netbsd-3-0-RC3:1.6
	netbsd-3-0-RC2:1.6
	netbsd-3-0-RC1:1.6
	netbsd-2-0-3-RELEASE:1.6
	netbsd-2-1:1.6.0.10
	netbsd-2-1-RELEASE:1.6
	netbsd-2-1-RC6:1.6
	netbsd-2-1-RC5:1.6
	netbsd-2-1-RC4:1.6
	netbsd-2-1-RC3:1.6
	netbsd-2-1-RC2:1.6
	netbsd-2-1-RC1:1.6
	netbsd-2-0-2-RELEASE:1.6
	netbsd-3:1.6.0.8
	netbsd-3-base:1.6
	netbsd-2-0-1-RELEASE:1.6
	netbsd-2:1.6.0.6
	netbsd-2-base:1.6
	netbsd-2-0-RELEASE:1.6
	netbsd-2-0-RC5:1.6
	netbsd-2-0-RC4:1.6
	netbsd-2-0-RC3:1.6
	netbsd-2-0-RC2:1.6
	netbsd-2-0-RC1:1.6
	netbsd-2-0:1.6.0.4
	netbsd-2-0-base:1.6
	netbsd-1-6-PATCH002-RELEASE:1.6
	netbsd-1-6-PATCH002:1.6
	netbsd-1-6-PATCH002-RC4:1.6
	netbsd-1-6-PATCH002-RC3:1.6
	netbsd-1-6-PATCH002-RC2:1.6
	netbsd-1-6-PATCH002-RC1:1.6
	netbsd-1-6-PATCH001:1.6
	netbsd-1-6-PATCH001-RELEASE:1.6
	netbsd-1-6-PATCH001-RC3:1.6
	netbsd-1-6-PATCH001-RC2:1.6
	netbsd-1-6-PATCH001-RC1:1.6
	fvdl_fs64_base:1.6
	netbsd-1-6-RELEASE:1.6
	netbsd-1-6-RC3:1.6
	netbsd-1-6-RC2:1.6
	netbsd-1-6-RC1:1.6
	netbsd-1-6:1.6.0.2
	netbsd-1-6-base:1.6
	netbsd-1-5-PATCH003:1.2
	netbsd-1-5-PATCH002:1.2
	netbsd-1-5-PATCH001:1.2
	nvi_1_79:1.1.1.4
	netbsd-1-5-RELEASE:1.2
	netbsd-1-5-BETA2:1.2
	netbsd-1-5-BETA:1.2
	netbsd-1-4-PATCH003:1.2
	netbsd-1-5-ALPHA2:1.2
	netbsd-1-5:1.2.0.10
	netbsd-1-5-base:1.2
	minoura-xpg4dl-base:1.2
	minoura-xpg4dl:1.2.0.8
	netbsd-1-4-PATCH002:1.2
	wrstuden-devbsize-19991221:1.2
	wrstuden-devbsize:1.2.0.6
	wrstuden-devbsize-base:1.2
	comdex-fall-1999:1.2.0.4
	comdex-fall-1999-base:1.2
	netbsd-1-4-PATCH001:1.2
	netbsd-1-4-RELEASE:1.2
	netbsd-1-4:1.2.0.2
	netbsd-1-4-base:1.2
	netbsd-1-3-PATCH003:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE2:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE1:1.1.1.3
	netbsd-1-3-PATCH003-CANDIDATE0:1.1.1.3
	netbsd-1-3-PATCH002:1.1.1.3
	netbsd-1-3-PATCH001:1.1.1.3
	netbsd-1-3-RELEASE:1.1.1.3
	netbsd-1-3-BETA:1.1.1.3
	netbsd-1-3:1.1.1.3.0.2
	netbsd-1-3-base:1.1.1.3
	netbsd-1-2-PATCH001:1.1.1.3
	netbsd-1-2-RELEASE:1.1.1.3
	netbsd-1-2-BETA:1.1.1.3
	netbsd-1-2:1.1.1.3.0.4
	netbsd-1-2-base:1.1.1.3
	nvi_1_66:1.1.1.3
	BOSTIC:1.1.1
	netbsd-1-1-PATCH001:1.1.1.2
	netbsd-1-1-RELEASE:1.1.1.2
	netbsd-1-1:1.1.1.2.0.4
	netbsd-1-1-base:1.1.1.2
	netbsd-1-0-PATCH06:1.1.1.2
	netbsd-1-0-PATCH05:1.1.1.2
	netbsd-1-0-PATCH04:1.1.1.2
	netbsd-1-0-PATCH03:1.1.1.2
	netbsd-1-0-PATCH02:1.1.1.2
	netbsd-1-0-PATCH1:1.1.1.2
	netbsd-1-0-PATCH0:1.1.1.2
	netbsd-1-0-RELEASE:1.1.1.2
	netbsd-1-0:1.1.1.2.0.2
	nvi-1-34b:1.1.1.2
	nvi-1-33b:1.1.1.1
	bostic-nvi:1.1.1;
locks; strict;
comment	@ * @;


1.8
date	2008.10.29.16.49.34;	author christos;	state dead;
branches;
next	1.7;

1.7
date	2005.06.02.04.25.16;	author lukem;	state Exp;
branches;
next	1.6;

1.6
date	2002.04.09.01.47.31;	author thorpej;	state Exp;
branches;
next	1.5;

1.5
date	2001.10.20.10.04.49;	author aymeric;	state Exp;
branches;
next	1.4;

1.4
date	2001.09.09.11.01.09;	author aymeric;	state Exp;
branches;
next	1.3;

1.3
date	2001.03.31.11.37.45;	author aymeric;	state Exp;
branches;
next	1.2;

1.2
date	98.01.09.08.06.32;	author perry;	state Exp;
branches;
next	1.1;

1.1
date	94.08.17.16.19.01;	author cgd;	state Exp;
branches
	1.1.1.1;
next	;

1.1.1.1
date	94.08.17.16.19.02;	author cgd;	state Exp;
branches;
next	1.1.1.2;

1.1.1.2
date	94.08.17.19.24.33;	author cgd;	state Exp;
branches
	1.1.1.2.2.1;
next	1.1.1.3;

1.1.1.3
date	96.05.20.02.03.32;	author mrg;	state Exp;
branches;
next	1.1.1.4;

1.1.1.4
date	2001.03.31.11.30.01;	author aymeric;	state Exp;
branches;
next	;

1.1.1.2.2.1
date	94.08.17.19.24.33;	author cgd;	state dead;
branches;
next	1.1.1.2.2.2;

1.1.1.2.2.2
date	94.08.17.19.24.34;	author cgd;	state Exp;
branches;
next	;


desc
@@


1.8
log
@bye old vi!
@
text
@/*	$NetBSD: cut.c,v 1.7 2005/06/02 04:25:16 lukem Exp $	*/

/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
 *
 * See the LICENSE file for redistribution information.
 */

#include "config.h"

#include <sys/cdefs.h>
#ifndef lint
#if 0
static const char sccsid[] = "@@(#)cut.c	10.10 (Berkeley) 9/15/96";
#else
__RCSID("$NetBSD: cut.c,v 1.7 2005/06/02 04:25:16 lukem Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>

#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common.h"

static void	cb_rotate __P((SCR *));

/*
 * cut --
 *	Put a range of lines/columns into a TEXT buffer.
 *
 * There are two buffer areas, both found in the global structure.  The first
 * is the linked list of all the buffers the user has named, the second is the
 * unnamed buffer storage.  There is a pointer, too, which is the current
 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
 * depending on into what buffer the last text was cut.  Logically, in both
 * delete and yank operations, if the user names a buffer, the text is cut
 * into it.  If it's a delete of information on more than a single line, the
 * contents of the numbered buffers are rotated up one, the contents of the
 * buffer named '9' are discarded, and the text is cut into the buffer named
 * '1'.  The text is always cut into the unnamed buffer.
 *
 * In all cases, upper-case buffer names are the same as lower-case names,
 * with the exception that they cause the buffer to be appended to instead
 * of replaced.  Note, however, that if text is appended to a buffer, the
 * default buffer only contains the appended text, not the entire contents
 * of the buffer.
 *
 * !!!
 * The contents of the default buffer would disappear after most operations
 * in historic vi.  It's unclear that this is useful, so we don't bother.
 *
 * When users explicitly cut text into the numeric buffers, historic vi became
 * genuinely strange.  I've never been able to figure out what was supposed to
 * happen.  It behaved differently if you deleted text than if you yanked text,
 * and, in the latter case, the text was appended to the buffer instead of
 * replacing the contents.  Hopefully it's not worth getting right, and here
 * we just treat the numeric buffers like any other named buffer.
 *
 * PUBLIC: int cut __P((SCR *, CHAR_T *, MARK *, MARK *, int));
 */
int
cut(sp, namep, fm, tm, flags)
	SCR *sp;
	CHAR_T *namep;
	MARK *fm, *tm;
	int flags;
{
	CB *cbp;
	CHAR_T name;
	recno_t lno;
	int append, copy_one, copy_def;

	/*
	 * If the user specified a buffer, put it there.  (This may require
	 * a copy into the numeric buffers.  We do the copy so that we don't
	 * have to reference count and so we don't have to deal with things
	 * like appends to buffers that are used multiple times.)
	 *
	 * Otherwise, if it's supposed to be put in a numeric buffer (usually
	 * a delete) put it there.  The rules for putting things in numeric
	 * buffers were historically a little strange.  There were three cases.
	 *
	 *	1: Some motions are always line mode motions, which means
	 *	   that the cut always goes into the numeric buffers.
	 *	2: Some motions aren't line mode motions, e.g. d10w, but
	 *	   can cross line boundaries.  For these commands, if the
	 *	   cut crosses a line boundary, it goes into the numeric
	 *	   buffers.  This includes most of the commands.
	 *	3: Some motions aren't line mode motions, e.g. d`<char>,
	 *	   but always go into the numeric buffers, regardless.  This
	 *	   was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
	 *
	 * Otherwise, put it in the unnamed buffer.
	 */
	name = 0;	/* XXXGCC -Wuninitialized */
	append = copy_one = copy_def = 0;
	if (namep != NULL) {
		name = *namep;
		if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) &&
		    (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno)) {
			copy_one = 1;
			cb_rotate(sp);
		}
		if ((append = isupper(name)) == 1) {
			if (!copy_one)
				copy_def = 1;
			name = tolower(name);
		}
namecb:		CBNAME(sp, cbp, name);
	} else if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) &&
	    (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno)) {
		name = '1';
		cb_rotate(sp);
		goto namecb;
	} else
		cbp = &sp->gp->dcb_store;

copyloop:
	/*
	 * If this is a new buffer, create it and add it into the list.
	 * Otherwise, if it's not an append, free its current contents.
	 */
	if (cbp == NULL) {
		CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
		cbp->name = name;
		CIRCLEQ_INIT(&cbp->textq);
		LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
	} else if (!append) {
		text_lfree(&cbp->textq);
		cbp->len = 0;
		cbp->flags = 0;
	}


	/* In line mode, it's pretty easy, just cut the lines. */
	if (LF_ISSET(CUT_LINEMODE)) {
		cbp->flags |= CB_LMODE;
		for (lno = fm->lno; lno <= tm->lno; ++lno)
			if (cut_line(sp, lno, 0, CUT_LINE_TO_EOL, cbp))
				goto cut_line_err;
	} else {
		/*
		 * Get the first line.  A length of CUT_LINE_TO_EOL causes
		 * cut_line() to cut from the MARK to the end of the line.
		 */
		if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
		    CUT_LINE_TO_EOL : (tm->cno - fm->cno) + 1, cbp))
			goto cut_line_err;

		/* Get the intermediate lines. */
		for (lno = fm->lno; ++lno < tm->lno;)
			if (cut_line(sp, lno, 0, CUT_LINE_TO_EOL, cbp))
				goto cut_line_err;

		/* Get the last line. */
		if (tm->lno != fm->lno &&
		    cut_line(sp, lno, 0, tm->cno + 1, cbp))
			goto cut_line_err;
	}

	append = 0;		/* Only append to the named buffer. */
	sp->gp->dcbp = cbp;	/* Repoint the default buffer on each pass. */

	if (copy_one) {		/* Copy into numeric buffer 1. */
		name = '1';
		CBNAME(sp, cbp, name);
		copy_one = 0;
		goto copyloop;
	}
	if (copy_def) {		/* Copy into the default buffer. */
		cbp = &sp->gp->dcb_store;
		copy_def = 0;
		goto copyloop;
	}
	return (0);

cut_line_err:	
	text_lfree(&cbp->textq);
	cbp->len = 0;
	cbp->flags = 0;
	return (1);
}

/*
 * cb_rotate --
 *	Rotate the numbered buffers up one.
 */
static void
cb_rotate(sp)
	SCR *sp;
{
	CB *cbp, *del_cbp;

	del_cbp = NULL;
	for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
		switch(cbp->name) {
		case '1':
			cbp->name = '2';
			break;
		case '2':
			cbp->name = '3';
			break;
		case '3':
			cbp->name = '4';
			break;
		case '4':
			cbp->name = '5';
			break;
		case '5':
			cbp->name = '6';
			break;
		case '6':
			cbp->name = '7';
			break;
		case '7':
			cbp->name = '8';
			break;
		case '8':
			cbp->name = '9';
			break;
		case '9':
			del_cbp = cbp;
			break;
		}
	if (del_cbp != NULL) {
		LIST_REMOVE(del_cbp, q);
		text_lfree(&del_cbp->textq);
		free(del_cbp);
	}
}

/*
 * cut_line --
 *	Cut a portion of a single line.
 *
 * PUBLIC: int cut_line __P((SCR *, recno_t, size_t, size_t, CB *));
 */
int
cut_line(sp, lno, fcno, clen, cbp)
	SCR *sp;
	recno_t lno;
	size_t fcno, clen;
	CB *cbp;
{
	TEXT *tp;
	size_t len;
	char *p;

	/* Get the line. */
	if (db_get(sp, lno, DBG_FATAL, &p, &len))
		return (1);

	/* Create a TEXT structure that can hold the entire line. */
	if ((tp = text_init(sp, NULL, 0, len)) == NULL)
		return (1);

	/*
	 * If the line isn't empty and it's not the entire line,
	 * copy the portion we want, and reset the TEXT length.
	 */
	if (len != 0) {
		if (clen == CUT_LINE_TO_EOL)
			clen = len - fcno;
		memcpy(tp->lb, p + fcno, clen);
		tp->len = clen;
	}

	/* Append to the end of the cut buffer. */
	CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
	cbp->len += tp->len;

	return (0);
}

/*
 * cut_close --
 *	Discard all cut buffers.
 *
 * PUBLIC: void cut_close __P((GS *));
 */
void
cut_close(gp)
	GS *gp;
{
	CB *cbp;

	/* Free cut buffer list. */
	while ((cbp = gp->cutq.lh_first) != NULL) {
		if (cbp->textq.cqh_first != (void *)&cbp->textq)
			text_lfree(&cbp->textq);
		LIST_REMOVE(cbp, q);
		free(cbp);
	}

	/* Free default cut storage. */
	cbp = &gp->dcb_store;
	if (cbp->textq.cqh_first != (void *)&cbp->textq)
		text_lfree(&cbp->textq);
}

/*
 * text_init --
 *	Allocate a new TEXT structure.
 *
 * PUBLIC: TEXT *text_init __P((SCR *, const char *, size_t, size_t));
 */
TEXT *
text_init(sp, p, len, total_len)
	SCR *sp;
	const char *p;
	size_t len, total_len;
{
	TEXT *tp;

	CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
	if (tp == NULL)
		return (NULL);
	/* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
	if ((tp->lb_len = total_len) != 0) {
		MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
		if (tp->lb == NULL) {
			free(tp);
			return (NULL);
		}
		if (p != NULL && len != 0)
			memcpy(tp->lb, p, len);
	}
	tp->len = len;
	return (tp);
}

/*
 * text_lfree --
 *	Free a chain of text structures.
 *
 * PUBLIC: void text_lfree __P((TEXTH *));
 */
void
text_lfree(headp)
	TEXTH *headp;
{
	TEXT *tp;

	while ((tp = headp->cqh_first) != (void *)headp) {
		CIRCLEQ_REMOVE(headp, tp, q);
		text_free(tp);
	}
}

/*
 * text_free --
 *	Free a text structure.
 *
 * PUBLIC: void text_free __P((TEXT *));
 */
void
text_free(tp)
	TEXT *tp;
{
	if (tp->lb != NULL)
		free(tp->lb);
	free(tp);
}
@


1.7
log
@appease gcc -Wuninitialized.
Each is marked with XXXGCC, because in some cases it looks like gcc
isn't correctly detecting that
	for ( ; xxx ; yyy)
		foo = 0;
always sets foo ...
@
text
@d1 1
a1 1
/*	$NetBSD: cut.c,v 1.6 2002/04/09 01:47:31 thorpej Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD: cut.c,v 1.6 2002/04/09 01:47:31 thorpej Exp $");
@


1.6
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: cut.c,v 1.5 2001/10/20 10:04:49 aymeric Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
d107 1
@


1.5
log
@Fix a cut_line() caller not using the right value for (former) ENTIRE_LINE,
by defining the (newer) CUT_LINE_TO_EOL define in common/cut.h and using it
where due.

Bug reported on current-users by Masanori Kanaoka <kanaoka@@ann.hi-ho.ne.jp>
diagnosed by Bang Jun-Young <bjy@@mogua.org>,
quick-fixed by Robert Elz <kre@@munnari.OZ.AU>.
@
text
@d1 1
a1 1
/*	$NetBSD: cut.c,v 1.4 2001/09/09 11:01:09 aymeric Exp $	*/
d14 1
d16 1
d18 3
@


1.4
log
@Define ENTIRE_LINE to be -1 instead of 0 since we may want to copy 0 characters.
(and use ENTIRE_LINE instead of 0 where appropriate)

This fixes a bug in the dw command with for example:

<cursor>
a b c

~
~
if you hit dw there, only the empty line would be killed but both the empty
line and the subsequent one would be pasted when asked for with P for example.
@
text
@d1 1
a1 1
/*	$NetBSD: cut.c,v 1.3 2001/03/31 11:37:45 aymeric Exp $	*/
a140 1
#define	ENTIRE_LINE	-1
d145 1
a145 1
			if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
d149 2
a150 2
		 * Get the first line.  A length of ENTIRE_LINE causes cut_line
		 * to cut from the MARK to the end of the line.
d153 1
a153 1
		    ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
d158 1
a158 1
			if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
d268 1
a268 1
		if (clen == ENTIRE_LINE)
@


1.3
log
@merge changes after import of nvi 1.79
@
text
@d1 1
a1 1
/*	$NetBSD: cut.c,v 1.2 1998/01/09 08:06:32 perry Exp $	*/
d141 1
a141 1
#define	ENTIRE_LINE	0
d146 1
a146 1
			if (cut_line(sp, lno, 0, 0, cbp))
d150 1
a150 1
		 * Get the first line.  A length of 0 causes cut_line
d269 1
a269 1
		if (clen == 0)
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d15 1
a15 1
static const char sccsid[] = "@@(#)cut.c	10.9 (Berkeley) 3/30/96";
d271 1
a271 1
		memmove(tp->lb, p + fcno, clen);
d333 1
a333 1
			memmove(tp->lb, p, len);
@


1.1
log
@Initial revision
@
text
@d1 2
d6 2
d9 1
a9 27
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
d12 2
d15 1
a15 1
static const char sccsid[] = "@@(#)cut.c	8.33 (Berkeley) 8/17/94";
a19 1
#include <sys/time.h>
a25 1
#include <signal.h>
a28 1
#include <termios.h>
d30 1
a30 3
#include "compat.h"
#include <db.h>
#include <regex.h>
d32 1
a32 3
#include "vi.h"

static int	cb_rotate __P((SCR *));
d65 2
d69 1
a69 1
cut(sp, ep, namep, fm, tm, flags)
a70 1
	EXF *ep;
d72 1
a73 1
	MARK *fm, *tm;
d105 1
a105 1
		if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) && 
d146 1
a146 1
			if (cut_line(sp, ep, lno, 0, 0, cbp))
d153 1
a153 1
		if (cut_line(sp, ep, fm->lno, fm->cno, fm->lno != tm->lno ?
d159 1
a159 1
			if (cut_line(sp, ep, lno, 0, ENTIRE_LINE, cbp))
d164 2
a165 6
		    cut_line(sp, ep, lno, 0, tm->cno + 1, cbp)) {
cut_line_err:		text_lfree(&cbp->textq);
			cbp->len = 0;
			cbp->flags = 0;
			return (1);
		}
d183 6
d195 1
a195 1
static int
d235 1
a235 1
		FREE(del_cbp, sizeof(CB));
a236 1
	return (0);
d242 2
d246 1
a246 1
cut_line(sp, ep, lno, fcno, clen, cbp)
a247 1
	EXF *ep;
d257 1
a257 2
	if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
		GETLINE_ERR(sp, lno);
a258 1
	}
d283 26
d311 2
d325 1
a325 1
	/* ANSI C doesn't define a call to malloc(2) for 0 bytes. */
d342 2
d360 2
d368 2
a369 4
		FREE(tp->lb, tp->lb_len);
	if (tp->wd != NULL)
		FREE(tp->wd, tp->wd_len);
	FREE(tp, sizeof(TEXT));
@


1.1.1.1
log
@new public version of nvi
@
text
@@


1.1.1.2
log
@new public version of nvi
@
text
@d35 1
a35 1
static char sccsid[] = "@@(#)cut.c	8.34 (Berkeley) 8/17/94";
@


1.1.1.2.2.1
log
@file cut.c was added on branch netbsd-1-0 on 1994-08-17 19:24:34 +0000
@
text
@d1 366
@


1.1.1.2.2.2
log
@new public version of nvi
@
text
@a0 366
/*-
 * Copyright (c) 1992, 1993, 1994
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef lint
static char sccsid[] = "@@(#)cut.c	8.34 (Berkeley) 8/17/94";
#endif /* not lint */

#include <sys/types.h>
#include <sys/queue.h>
#include <sys/time.h>

#include <bitstring.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>

#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"

static int	cb_rotate __P((SCR *));

/*
 * cut --
 *	Put a range of lines/columns into a TEXT buffer.
 *
 * There are two buffer areas, both found in the global structure.  The first
 * is the linked list of all the buffers the user has named, the second is the
 * unnamed buffer storage.  There is a pointer, too, which is the current
 * default buffer, i.e. it may point to the unnamed buffer or a named buffer
 * depending on into what buffer the last text was cut.  Logically, in both
 * delete and yank operations, if the user names a buffer, the text is cut
 * into it.  If it's a delete of information on more than a single line, the
 * contents of the numbered buffers are rotated up one, the contents of the
 * buffer named '9' are discarded, and the text is cut into the buffer named
 * '1'.  The text is always cut into the unnamed buffer.
 *
 * In all cases, upper-case buffer names are the same as lower-case names,
 * with the exception that they cause the buffer to be appended to instead
 * of replaced.  Note, however, that if text is appended to a buffer, the
 * default buffer only contains the appended text, not the entire contents
 * of the buffer.
 *
 * !!!
 * The contents of the default buffer would disappear after most operations
 * in historic vi.  It's unclear that this is useful, so we don't bother.
 *
 * When users explicitly cut text into the numeric buffers, historic vi became
 * genuinely strange.  I've never been able to figure out what was supposed to
 * happen.  It behaved differently if you deleted text than if you yanked text,
 * and, in the latter case, the text was appended to the buffer instead of
 * replacing the contents.  Hopefully it's not worth getting right, and here
 * we just treat the numeric buffers like any other named buffer.
 */
int
cut(sp, ep, namep, fm, tm, flags)
	SCR *sp;
	EXF *ep;
	CHAR_T *namep;
	int flags;
	MARK *fm, *tm;
{
	CB *cbp;
	CHAR_T name;
	recno_t lno;
	int append, copy_one, copy_def;

	/*
	 * If the user specified a buffer, put it there.  (This may require
	 * a copy into the numeric buffers.  We do the copy so that we don't
	 * have to reference count and so we don't have to deal with things
	 * like appends to buffers that are used multiple times.)
	 *
	 * Otherwise, if it's supposed to be put in a numeric buffer (usually
	 * a delete) put it there.  The rules for putting things in numeric
	 * buffers were historically a little strange.  There were three cases.
	 *
	 *	1: Some motions are always line mode motions, which means
	 *	   that the cut always goes into the numeric buffers.
	 *	2: Some motions aren't line mode motions, e.g. d10w, but
	 *	   can cross line boundaries.  For these commands, if the
	 *	   cut crosses a line boundary, it goes into the numeric
	 *	   buffers.  This includes most of the commands.
	 *	3: Some motions aren't line mode motions, e.g. d`<char>,
	 *	   but always go into the numeric buffers, regardless.  This
	 *	   was the commands: % ` / ? ( ) N n { } -- and nvi adds ^A.
	 *
	 * Otherwise, put it in the unnamed buffer.
	 */
	append = copy_one = copy_def = 0;
	if (namep != NULL) {
		name = *namep;
		if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) && 
		    (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno)) {
			copy_one = 1;
			cb_rotate(sp);
		}
		if ((append = isupper(name)) == 1) {
			if (!copy_one)
				copy_def = 1;
			name = tolower(name);
		}
namecb:		CBNAME(sp, cbp, name);
	} else if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) &&
	    (LF_ISSET(CUT_LINEMODE) || fm->lno != tm->lno)) {
		name = '1';
		cb_rotate(sp);
		goto namecb;
	} else
		cbp = &sp->gp->dcb_store;

copyloop:
	/*
	 * If this is a new buffer, create it and add it into the list.
	 * Otherwise, if it's not an append, free its current contents.
	 */
	if (cbp == NULL) {
		CALLOC_RET(sp, cbp, CB *, 1, sizeof(CB));
		cbp->name = name;
		CIRCLEQ_INIT(&cbp->textq);
		LIST_INSERT_HEAD(&sp->gp->cutq, cbp, q);
	} else if (!append) {
		text_lfree(&cbp->textq);
		cbp->len = 0;
		cbp->flags = 0;
	}


#define	ENTIRE_LINE	0
	/* In line mode, it's pretty easy, just cut the lines. */
	if (LF_ISSET(CUT_LINEMODE)) {
		cbp->flags |= CB_LMODE;
		for (lno = fm->lno; lno <= tm->lno; ++lno)
			if (cut_line(sp, ep, lno, 0, 0, cbp))
				goto cut_line_err;
	} else {
		/*
		 * Get the first line.  A length of 0 causes cut_line
		 * to cut from the MARK to the end of the line.
		 */
		if (cut_line(sp, ep, fm->lno, fm->cno, fm->lno != tm->lno ?
		    ENTIRE_LINE : (tm->cno - fm->cno) + 1, cbp))
			goto cut_line_err;

		/* Get the intermediate lines. */
		for (lno = fm->lno; ++lno < tm->lno;)
			if (cut_line(sp, ep, lno, 0, ENTIRE_LINE, cbp))
				goto cut_line_err;

		/* Get the last line. */
		if (tm->lno != fm->lno &&
		    cut_line(sp, ep, lno, 0, tm->cno + 1, cbp)) {
cut_line_err:		text_lfree(&cbp->textq);
			cbp->len = 0;
			cbp->flags = 0;
			return (1);
		}
	}

	append = 0;		/* Only append to the named buffer. */
	sp->gp->dcbp = cbp;	/* Repoint the default buffer on each pass. */

	if (copy_one) {		/* Copy into numeric buffer 1. */
		name = '1';
		CBNAME(sp, cbp, name);
		copy_one = 0;
		goto copyloop;
	}
	if (copy_def) {		/* Copy into the default buffer. */
		cbp = &sp->gp->dcb_store;
		copy_def = 0;
		goto copyloop;
	}
	return (0);
}

/*
 * cb_rotate --
 *	Rotate the numbered buffers up one.
 */
static int
cb_rotate(sp)
	SCR *sp;
{
	CB *cbp, *del_cbp;

	del_cbp = NULL;
	for (cbp = sp->gp->cutq.lh_first; cbp != NULL; cbp = cbp->q.le_next)
		switch(cbp->name) {
		case '1':
			cbp->name = '2';
			break;
		case '2':
			cbp->name = '3';
			break;
		case '3':
			cbp->name = '4';
			break;
		case '4':
			cbp->name = '5';
			break;
		case '5':
			cbp->name = '6';
			break;
		case '6':
			cbp->name = '7';
			break;
		case '7':
			cbp->name = '8';
			break;
		case '8':
			cbp->name = '9';
			break;
		case '9':
			del_cbp = cbp;
			break;
		}
	if (del_cbp != NULL) {
		LIST_REMOVE(del_cbp, q);
		text_lfree(&del_cbp->textq);
		FREE(del_cbp, sizeof(CB));
	}
	return (0);
}

/*
 * cut_line --
 *	Cut a portion of a single line.
 */
int
cut_line(sp, ep, lno, fcno, clen, cbp)
	SCR *sp;
	EXF *ep;
	recno_t lno;
	size_t fcno, clen;
	CB *cbp;
{
	TEXT *tp;
	size_t len;
	char *p;

	/* Get the line. */
	if ((p = file_gline(sp, ep, lno, &len)) == NULL) {
		GETLINE_ERR(sp, lno);
		return (1);
	}

	/* Create a TEXT structure that can hold the entire line. */
	if ((tp = text_init(sp, NULL, 0, len)) == NULL)
		return (1);

	/*
	 * If the line isn't empty and it's not the entire line,
	 * copy the portion we want, and reset the TEXT length.
	 */
	if (len != 0) {
		if (clen == 0)
			clen = len - fcno;
		memmove(tp->lb, p + fcno, clen);
		tp->len = clen;
	}

	/* Append to the end of the cut buffer. */
	CIRCLEQ_INSERT_TAIL(&cbp->textq, tp, q);
	cbp->len += tp->len;

	return (0);
}

/*
 * text_init --
 *	Allocate a new TEXT structure.
 */
TEXT *
text_init(sp, p, len, total_len)
	SCR *sp;
	const char *p;
	size_t len, total_len;
{
	TEXT *tp;

	CALLOC(sp, tp, TEXT *, 1, sizeof(TEXT));
	if (tp == NULL)
		return (NULL);
	/* ANSI C doesn't define a call to malloc(2) for 0 bytes. */
	if ((tp->lb_len = total_len) != 0) {
		MALLOC(sp, tp->lb, CHAR_T *, tp->lb_len);
		if (tp->lb == NULL) {
			free(tp);
			return (NULL);
		}
		if (p != NULL && len != 0)
			memmove(tp->lb, p, len);
	}
	tp->len = len;
	return (tp);
}

/*
 * text_lfree --
 *	Free a chain of text structures.
 */
void
text_lfree(headp)
	TEXTH *headp;
{
	TEXT *tp;

	while ((tp = headp->cqh_first) != (void *)headp) {
		CIRCLEQ_REMOVE(headp, tp, q);
		text_free(tp);
	}
}

/*
 * text_free --
 *	Free a text structure.
 */
void
text_free(tp)
	TEXT *tp;
{
	if (tp->lb != NULL)
		FREE(tp->lb, tp->lb_len);
	if (tp->wd != NULL)
		FREE(tp->wd, tp->wd_len);
	FREE(tp, sizeof(TEXT));
}
@


1.1.1.3
log
@import of nvi 1.66
@
text
@a3 2
 * Copyright (c) 1992, 1993, 1994, 1995, 1996
 *	Keith Bostic.  All rights reserved.
d5 27
a31 1
 * See the LICENSE file for redistribution information.
a33 2
#include "config.h"

d35 1
a35 1
static const char sccsid[] = "@@(#)cut.c	10.9 (Berkeley) 3/30/96";
d40 1
d47 1
d51 5
d57 1
a57 1
#include "common.h"
d59 1
a59 1
static void	cb_rotate __P((SCR *));
a91 2
 *
 * PUBLIC: int cut __P((SCR *, CHAR_T *, MARK *, MARK *, int));
d94 1
a94 1
cut(sp, namep, fm, tm, flags)
d96 1
d98 1
a99 1
	int flags;
d131 1
a131 1
		if (LF_ISSET(CUT_NUMREQ) || LF_ISSET(CUT_NUMOPT) &&
d172 1
a172 1
			if (cut_line(sp, lno, 0, 0, cbp))
d179 1
a179 1
		if (cut_line(sp, fm->lno, fm->cno, fm->lno != tm->lno ?
d185 1
a185 1
			if (cut_line(sp, lno, 0, ENTIRE_LINE, cbp))
d190 6
a195 2
		    cut_line(sp, lno, 0, tm->cno + 1, cbp))
			goto cut_line_err;
a212 6

cut_line_err:	
	text_lfree(&cbp->textq);
	cbp->len = 0;
	cbp->flags = 0;
	return (1);
d219 1
a219 1
static void
d259 1
a259 1
		free(del_cbp);
d261 1
a266 2
 *
 * PUBLIC: int cut_line __P((SCR *, recno_t, size_t, size_t, CB *));
d269 1
a269 1
cut_line(sp, lno, fcno, clen, cbp)
d271 1
d281 2
a282 1
	if (db_get(sp, lno, DBG_FATAL, &p, &len))
d284 1
a308 26
 * cut_close --
 *	Discard all cut buffers.
 *
 * PUBLIC: void cut_close __P((GS *));
 */
void
cut_close(gp)
	GS *gp;
{
	CB *cbp;

	/* Free cut buffer list. */
	while ((cbp = gp->cutq.lh_first) != NULL) {
		if (cbp->textq.cqh_first != (void *)&cbp->textq)
			text_lfree(&cbp->textq);
		LIST_REMOVE(cbp, q);
		free(cbp);
	}

	/* Free default cut storage. */
	cbp = &gp->dcb_store;
	if (cbp->textq.cqh_first != (void *)&cbp->textq)
		text_lfree(&cbp->textq);
}

/*
a310 2
 *
 * PUBLIC: TEXT *text_init __P((SCR *, const char *, size_t, size_t));
d323 1
a323 1
	/* ANSI C doesn't define a call to malloc(3) for 0 bytes. */
a339 2
 *
 * PUBLIC: void text_lfree __P((TEXTH *));
a355 2
 *
 * PUBLIC: void text_free __P((TEXT *));
d362 4
a365 2
		free(tp->lb);
	free(tp);
@


1.1.1.4
log
@import of nvi 1.79
@
text
@d13 1
a13 1
static const char sccsid[] = "@@(#)cut.c	10.10 (Berkeley) 9/15/96";
d269 1
a269 1
		memcpy(tp->lb, p + fcno, clen);
d331 1
a331 1
			memcpy(tp->lb, p, len);
@

