head	1.4;
access;
symbols
	perseant-exfatfs-base-20250801:1.4
	perseant-exfatfs-base-20240630:1.4
	perseant-exfatfs:1.4.0.36
	perseant-exfatfs-base:1.4
	cjep_sun2x:1.4.0.34
	cjep_sun2x-base:1.4
	cjep_staticlib_x-base1:1.4
	cjep_staticlib_x:1.4.0.32
	cjep_staticlib_x-base:1.4
	phil-wifi-20200421:1.4
	phil-wifi-20200411:1.4
	phil-wifi-20200406:1.4
	pgoyette-compat-merge-20190127:1.4
	pgoyette-compat-20190127:1.4
	pgoyette-compat-20190118:1.4
	pgoyette-compat-1226:1.4
	pgoyette-compat-1126:1.4
	pgoyette-compat-1020:1.4
	pgoyette-compat-0930:1.4
	pgoyette-compat-0906:1.4
	pgoyette-compat-0728:1.4
	pgoyette-compat-0625:1.4
	pgoyette-compat-0521:1.4
	pgoyette-compat-0502:1.4
	pgoyette-compat-0422:1.4
	pgoyette-compat-0415:1.4
	pgoyette-compat-0407:1.4
	pgoyette-compat-0330:1.4
	pgoyette-compat-0322:1.4
	pgoyette-compat-0315:1.4
	pgoyette-compat:1.4.0.30
	pgoyette-compat-base:1.4
	perseant-stdc-iso10646:1.4.0.28
	perseant-stdc-iso10646-base:1.4
	prg-localcount2-base3:1.4
	prg-localcount2-base2:1.4
	prg-localcount2-base1:1.4
	prg-localcount2:1.4.0.26
	prg-localcount2-base:1.4
	pgoyette-localcount-20170426:1.4
	bouyer-socketcan-base1:1.4
	pgoyette-localcount-20170320:1.4
	bouyer-socketcan:1.4.0.24
	bouyer-socketcan-base:1.4
	pgoyette-localcount-20170107:1.4
	pgoyette-localcount-20161104:1.4
	localcount-20160914:1.4
	pgoyette-localcount-20160806:1.4
	pgoyette-localcount-20160726:1.4
	pgoyette-localcount:1.4.0.22
	pgoyette-localcount-base:1.4
	netbsd-5-2-3-RELEASE:1.4
	netbsd-5-1-5-RELEASE:1.4
	yamt-pagecache-base9:1.4
	yamt-pagecache-tag8:1.4
	tls-earlyentropy:1.4.0.18
	tls-earlyentropy-base:1.4
	riastradh-xf86-video-intel-2-7-1-pre-2-21-15:1.4
	riastradh-drm2-base3:1.4
	netbsd-5-2-2-RELEASE:1.4
	netbsd-5-1-4-RELEASE:1.4
	netbsd-5-2-1-RELEASE:1.4
	netbsd-5-1-3-RELEASE:1.4
	agc-symver:1.4.0.20
	agc-symver-base:1.4
	tls-maxphys-base:1.4
	yamt-pagecache-base8:1.4
	netbsd-5-2:1.4.0.16
	yamt-pagecache-base7:1.4
	netbsd-5-2-RELEASE:1.4
	netbsd-5-2-RC1:1.4
	yamt-pagecache-base6:1.4
	yamt-pagecache-base5:1.4
	yamt-pagecache-base4:1.4
	netbsd-5-1-2-RELEASE:1.4
	netbsd-5-1-1-RELEASE:1.4
	yamt-pagecache-base3:1.4
	yamt-pagecache-base2:1.4
	yamt-pagecache:1.4.0.14
	yamt-pagecache-base:1.4
	bouyer-quota2-nbase:1.4
	bouyer-quota2:1.4.0.12
	bouyer-quota2-base:1.4
	matt-nb5-pq3:1.4.0.10
	matt-nb5-pq3-base:1.4
	netbsd-5-1:1.4.0.8
	netbsd-5-1-RELEASE:1.4
	netbsd-5-1-RC4:1.4
	netbsd-5-1-RC3:1.4
	netbsd-5-1-RC2:1.4
	netbsd-5-1-RC1:1.4
	netbsd-5-0-2-RELEASE:1.4
	netbsd-5-0-1-RELEASE:1.4
	jym-xensuspend-nbase:1.4
	netbsd-5-0:1.4.0.6
	netbsd-5-0-RELEASE:1.4
	netbsd-5-0-RC4:1.4
	netbsd-5-0-RC3:1.4
	netbsd-5-0-RC2:1.4
	jym-xensuspend:1.4.0.4
	jym-xensuspend-base:1.4
	netbsd-5-0-RC1:1.4
	netbsd-5:1.4.0.2
	netbsd-5-base:1.4
	matt-mips64-base2:1.3
	matt-mips64:1.3.0.40
	mjf-devfs2:1.3.0.38
	mjf-devfs2-base:1.3
	netbsd-4-0-1-RELEASE:1.3
	wrstuden-revivesa-base-3:1.3
	wrstuden-revivesa-base-2:1.3
	wrstuden-fixsa-newbase:1.3
	wrstuden-revivesa-base-1:1.3
	yamt-pf42-base4:1.3
	yamt-pf42-base3:1.3
	hpcarm-cleanup-nbase:1.3
	yamt-pf42-baseX:1.3
	yamt-pf42-base2:1.3
	wrstuden-revivesa:1.3.0.36
	wrstuden-revivesa-base:1.3
	yamt-pf42:1.3.0.34
	yamt-pf42-base:1.3
	keiichi-mipv6-nbase:1.3
	keiichi-mipv6:1.3.0.32
	keiichi-mipv6-base:1.3
	matt-armv6-nbase:1.3
	matt-armv6-prevmlocking:1.3
	wrstuden-fixsa-base-1:1.3
	netbsd-4-0:1.3.0.30
	netbsd-4-0-RELEASE:1.3
	cube-autoconf:1.3.0.28
	cube-autoconf-base:1.3
	netbsd-4-0-RC5:1.3
	netbsd-4-0-RC4:1.3
	netbsd-4-0-RC3:1.3
	netbsd-4-0-RC2:1.3
	netbsd-4-0-RC1:1.3
	matt-armv6:1.3.0.26
	matt-armv6-base:1.3
	matt-mips64-base:1.3
	hpcarm-cleanup:1.3.0.24
	hpcarm-cleanup-base:1.3
	netbsd-3-1-1-RELEASE:1.3
	netbsd-3-0-3-RELEASE:1.3
	wrstuden-fixsa:1.3.0.22
	wrstuden-fixsa-base:1.3
	abandoned-netbsd-4-base:1.3
	abandoned-netbsd-4:1.3.0.16
	netbsd-3-1:1.3.0.18
	netbsd-3-1-RELEASE:1.3
	netbsd-3-0-2-RELEASE:1.3
	netbsd-3-1-RC4:1.3
	netbsd-3-1-RC3:1.3
	netbsd-3-1-RC2:1.3
	netbsd-3-1-RC1:1.3
	netbsd-4:1.3.0.20
	netbsd-4-base:1.3
	chap-midi-nbase:1.3
	netbsd-3-0-1-RELEASE:1.3
	chap-midi:1.3.0.14
	chap-midi-base:1.3
	netbsd-3-0:1.3.0.12
	netbsd-3-0-RELEASE:1.3
	netbsd-3-0-RC6:1.3
	netbsd-3-0-RC5:1.3
	netbsd-3-0-RC4:1.3
	netbsd-3-0-RC3:1.3
	netbsd-3-0-RC2:1.3
	netbsd-3-0-RC1:1.3
	netbsd-2-0-3-RELEASE:1.3
	netbsd-2-1:1.3.0.10
	netbsd-2-1-RELEASE:1.3
	netbsd-2-1-RC6:1.3
	netbsd-2-1-RC5:1.3
	netbsd-2-1-RC4:1.3
	netbsd-2-1-RC3:1.3
	netbsd-2-1-RC2:1.3
	netbsd-2-1-RC1:1.3
	netbsd-2-0-2-RELEASE:1.3
	netbsd-3:1.3.0.8
	netbsd-3-base:1.3
	netbsd-2-0-1-RELEASE:1.3
	netbsd-2:1.3.0.6
	netbsd-2-base:1.3
	netbsd-2-0-RELEASE:1.3
	netbsd-2-0-RC5:1.3
	netbsd-2-0-RC4:1.3
	netbsd-2-0-RC3:1.3
	netbsd-2-0-RC2:1.3
	netbsd-2-0-RC1:1.3
	netbsd-2-0:1.3.0.4
	netbsd-2-0-base:1.3
	netbsd-1-6-PATCH002-RELEASE:1.3
	netbsd-1-6-PATCH002:1.3
	netbsd-1-6-PATCH002-RC4:1.3
	netbsd-1-6-PATCH002-RC3:1.3
	netbsd-1-6-PATCH002-RC2:1.3
	netbsd-1-6-PATCH002-RC1:1.3
	netbsd-1-6-PATCH001:1.3
	netbsd-1-6-PATCH001-RELEASE:1.3
	netbsd-1-6-PATCH001-RC3:1.3
	netbsd-1-6-PATCH001-RC2:1.3
	netbsd-1-6-PATCH001-RC1:1.3
	fvdl_fs64_base:1.3
	netbsd-1-6-RELEASE:1.3
	netbsd-1-6-RC3:1.3
	netbsd-1-6-RC2:1.3
	netbsd-1-6-RC1:1.3
	netbsd-1-6:1.3.0.2
	netbsd-1-6-base:1.3
	netbsd-1-5-PATCH003:1.2
	netbsd-1-5-PATCH002:1.2
	netbsd-1-5-PATCH001:1.2
	nvi_1_79:1.1.1.3
	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.4
date	2008.10.29.16.49.35;	author christos;	state dead;
branches;
next	1.3;

1.3
date	2002.04.09.01.47.32;	author thorpej;	state Exp;
branches;
next	1.2;

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

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

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

1.1.1.2
date	94.08.17.19.26.02;	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.05.54;	author mrg;	state Exp;
branches;
next	;

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

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


desc
@@


1.4
log
@bye old vi!
@
text
@/*	$NetBSD: seq.c,v 1.3 2002/04/09 01:47:32 thorpej 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[] = "@@(#)seq.c	10.10 (Berkeley) 3/30/96";
#else
__RCSID("$NetBSD: seq.c,v 1.3 2002/04/09 01:47:32 thorpej Exp $");
#endif
#endif /* not lint */

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

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

#include "common.h"

/*
 * seq_set --
 *	Internal version to enter a sequence.
 *
 * PUBLIC: int seq_set __P((SCR *, CHAR_T *,
 * PUBLIC:    size_t, CHAR_T *, size_t, CHAR_T *, size_t, seq_t, int));
 */
int
seq_set(sp, name, nlen, input, ilen, output, olen, stype, flags)
	SCR *sp;
	CHAR_T *name, *input, *output;
	size_t nlen, ilen, olen;
	seq_t stype;
	int flags;
{
	CHAR_T *p;
	SEQ *lastqp, *qp;
	int sv_errno;

	/*
	 * An input string must always be present.  The output string
	 * can be NULL, when set internally, that's how we throw away
	 * input.
	 *
	 * Just replace the output field if the string already set.
	 */
	if ((qp =
	    seq_find(sp, &lastqp, NULL, input, ilen, stype, NULL)) != NULL) {
		if (LF_ISSET(SEQ_NOOVERWRITE))
			return (0);
		if (output == NULL || olen == 0) {
			p = NULL;
			olen = 0;
		} else if ((p = v_strdup(sp, output, olen)) == NULL) {
			sv_errno = errno;
			goto mem1;
		}
		if (qp->output != NULL)
			free(qp->output);
		qp->olen = olen;
		qp->output = p;
		return (0);
	}

	/* Allocate and initialize SEQ structure. */
	CALLOC(sp, qp, SEQ *, 1, sizeof(SEQ));
	if (qp == NULL) {
		sv_errno = errno;
		goto mem1;
	}

	/* Name. */
	if (name == NULL || nlen == 0)
		qp->name = NULL;
	else if ((qp->name = v_strdup(sp, name, nlen)) == NULL) {
		sv_errno = errno;
		goto mem2;
	}
	qp->nlen = nlen;

	/* Input. */
	if ((qp->input = v_strdup(sp, input, ilen)) == NULL) {
		sv_errno = errno;
		goto mem3;
	}
	qp->ilen = ilen;

	/* Output. */
	if (output == NULL) {
		qp->output = NULL;
		olen = 0;
	} else if ((qp->output = v_strdup(sp, output, olen)) == NULL) {
		sv_errno = errno;
		free(qp->input);
mem3:		if (qp->name != NULL)
			free(qp->name);
mem2:		free(qp);
mem1:		errno = sv_errno;
		msgq(sp, M_SYSERR, NULL);
		return (1);
	}
	qp->olen = olen;

	/* Type, flags. */
	qp->stype = stype;
	qp->flags = flags;

	/* Link into the chain. */
	if (lastqp == NULL) {
		LIST_INSERT_HEAD(&sp->gp->seqq, qp, q);
	} else {
		LIST_INSERT_AFTER(lastqp, qp, q);
	}

	/* Set the fast lookup bit. */
	if (qp->input[0] < MAX_BIT_SEQ)
		bit_set(sp->gp->seqb, qp->input[0]);

	return (0);
}

/*
 * seq_delete --
 *	Delete a sequence.
 *
 * PUBLIC: int seq_delete __P((SCR *, CHAR_T *, size_t, seq_t));
 */
int
seq_delete(sp, input, ilen, stype)
	SCR *sp;
	CHAR_T *input;
	size_t ilen;
	seq_t stype;
{
	SEQ *qp;

	if ((qp = seq_find(sp, NULL, NULL, input, ilen, stype, NULL)) == NULL)
		return (1);
	return (seq_mdel(qp));
}

/*
 * seq_mdel --
 *	Delete a map entry, without lookup.
 *
 * PUBLIC: int seq_mdel __P((SEQ *));
 */
int
seq_mdel(qp)
	SEQ *qp;
{
	LIST_REMOVE(qp, q);
	if (qp->name != NULL)
		free(qp->name);
	free(qp->input);
	if (qp->output != NULL)
		free(qp->output);
	free(qp);
	return (0);
}

/*
 * seq_find --
 *	Search the sequence list for a match to a buffer, if ispartial
 *	isn't NULL, partial matches count.
 *
 * PUBLIC: SEQ *seq_find
 * PUBLIC:    __P((SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *));
 */
SEQ *
seq_find(sp, lastqp, e_input, c_input, ilen, stype, ispartialp)
	SCR *sp;
	SEQ **lastqp;
	EVENT *e_input;
	CHAR_T *c_input;
	size_t ilen;
	seq_t stype;
	int *ispartialp;
{
	SEQ *lqp, *qp;
	int diff;

	/*
	 * Ispartialp is a location where we return if there was a
	 * partial match, i.e. if the string were extended it might
	 * match something.
	 *
	 * XXX
	 * Overload the meaning of ispartialp; only the terminal key
	 * search doesn't want the search limited to complete matches,
	 * i.e. ilen may be longer than the match.
	 */
	if (ispartialp != NULL)
		*ispartialp = 0;
	for (lqp = NULL, qp = sp->gp->seqq.lh_first;
	    qp != NULL; lqp = qp, qp = qp->q.le_next) {
		/*
		 * Fast checks on the first character and type, and then
		 * a real comparison.
		 */
		if (e_input == NULL) {
			if (qp->input[0] > c_input[0])
				break;
			if (qp->input[0] < c_input[0] ||
			    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
				continue;
			diff = memcmp(qp->input, c_input, MIN(qp->ilen, ilen));
		} else {
			if (qp->input[0] > e_input->e_c)
				break;
			if (qp->input[0] < e_input->e_c ||
			    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
				continue;
			diff =
			    e_memcmp(qp->input, e_input, MIN(qp->ilen, ilen));
		}
		if (diff > 0)
			break;
		if (diff < 0)
			continue;
		/*
		 * If the entry is the same length as the string, return a
		 * match.  If the entry is shorter than the string, return a
		 * match if called from the terminal key routine.  Otherwise,
		 * keep searching for a complete match.
		 */
		if (qp->ilen <= ilen) {
			if (qp->ilen == ilen || ispartialp != NULL) {
				if (lastqp != NULL)
					*lastqp = lqp;
				return (qp);
			}
			continue;
		}
		/*
		 * If the entry longer than the string, return partial match
		 * if called from the terminal key routine.  Otherwise, no
		 * match.
		 */
		if (ispartialp != NULL)
			*ispartialp = 1;
		break;
	}
	if (lastqp != NULL)
		*lastqp = lqp;
	return (NULL);
}

/*
 * seq_close --
 *	Discard all sequences.
 *
 * PUBLIC: void seq_close __P((GS *));
 */
void
seq_close(gp)
	GS *gp;
{
	SEQ *qp;

	while ((qp = gp->seqq.lh_first) != NULL) {
		if (qp->name != NULL)
			free(qp->name);
		if (qp->input != NULL)
			free(qp->input);
		if (qp->output != NULL)
			free(qp->output);
		LIST_REMOVE(qp, q);
		free(qp);
	}
}

/*
 * seq_dump --
 *	Display the sequence entries of a specified type.
 *
 * PUBLIC: int seq_dump __P((SCR *, seq_t, int));
 */
int
seq_dump(sp, stype, isname)
	SCR *sp;
	seq_t stype;
	int isname;
{
	CHAR_T *p;
	GS *gp;
	SEQ *qp;
	int cnt, len, olen;

	cnt = 0;
	gp = sp->gp;
	for (qp = gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
		if (stype != qp->stype || F_ISSET(qp, SEQ_FUNCMAP))
			continue;
		++cnt;
		for (p = qp->input,
		    olen = qp->ilen, len = 0; olen > 0; --olen, ++p)
			len += ex_puts(sp, KEY_NAME(sp, *p));
		for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
			len -= ex_puts(sp, " ");

		if (qp->output != NULL)
			for (p = qp->output,
			    olen = qp->olen, len = 0; olen > 0; --olen, ++p)
				len += ex_puts(sp, KEY_NAME(sp, *p));
		else
			len = 0;

		if (isname && qp->name != NULL) {
			for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
				len -= ex_puts(sp, " ");
			for (p = qp->name,
			    olen = qp->nlen; olen > 0; --olen, ++p)
				(void)ex_puts(sp, KEY_NAME(sp, *p));
		}
		(void)ex_puts(sp, "\n");
	}
	return (cnt);
}

/*
 * seq_save --
 *	Save the sequence entries to a file.
 *
 * PUBLIC: int seq_save __P((SCR *, FILE *, char *, seq_t));
 */
int
seq_save(sp, fp, prefix, stype)
	SCR *sp;
	FILE *fp;
	char *prefix;
	seq_t stype;
{
	CHAR_T *p;
	SEQ *qp;
	size_t olen;
	int ch;

	/* Write a sequence command for all keys the user defined. */
	for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
		if (stype != qp->stype || !F_ISSET(qp, SEQ_USERDEF))
			continue;
		if (prefix)
			(void)fprintf(fp, "%s", prefix);
		for (p = qp->input, olen = qp->ilen; olen > 0; --olen) {
			ch = *p++;
			if (ch == CH_LITERAL || ch == '|' ||
			    isblank(ch) || KEY_VAL(sp, ch) == K_NL)
				(void)putc(CH_LITERAL, fp);
			(void)putc(ch, fp);
		}
		(void)putc(' ', fp);
		if (qp->output != NULL)
			for (p = qp->output,
			    olen = qp->olen; olen > 0; --olen) {
				ch = *p++;
				if (ch == CH_LITERAL || ch == '|' ||
				    KEY_VAL(sp, ch) == K_NL)
					(void)putc(CH_LITERAL, fp);
				(void)putc(ch, fp);
			}
		(void)putc('\n', fp);
	}
	return (0);
}

/*
 * e_memcmp --
 *	Compare a string of EVENT's to a string of CHAR_T's.
 *
 * PUBLIC: int e_memcmp __P((CHAR_T *, EVENT *, size_t));
 */
int
e_memcmp(p1, ep, n)
	CHAR_T *p1;
	EVENT *ep;
	size_t n;
{
	if (n != 0) {
                do {
                        if (*p1++ != ep->e_c)
                                return (*--p1 - ep->e_c);
			++ep;
                } while (--n != 0);
        }
        return (0);
}
@


1.3
log
@Use __RCSID() and __COPYRIGHT().
@
text
@d1 1
a1 1
/*	$NetBSD: seq.c,v 1.2 1998/01/09 08:07:09 perry Exp $	*/
d19 1
a19 1
__RCSID("$NetBSD$");
@


1.2
log
@RCS Id Police.
@
text
@d1 1
a1 1
/*	$NetBSD$	*/
d14 1
d16 1
d18 3
@


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[] = "@@(#)seq.c	8.32 (Berkeley) 8/17/94";
a19 1
#include <sys/time.h>
a24 1
#include <signal.h>
a27 1
#include <termios.h>
d29 1
a29 6
#include "compat.h"
#include <db.h>
#include <regex.h>

#include "vi.h"
#include "excmd.h"
d34 3
d43 1
a43 1
	enum seqtype stype;
d57 4
a60 1
	if ((qp = seq_find(sp, &lastqp, input, ilen, stype, NULL)) != NULL) {
d107 1
a107 1
mem2:		FREE(qp, sizeof(SEQ));
d135 2
d143 1
a143 1
	enum seqtype stype;
d147 1
a147 1
	if ((qp = seq_find(sp, NULL, input, ilen, stype, NULL)) == NULL)
d155 2
d168 1
a168 1
	FREE(qp, sizeof(SEQ));
d176 3
d181 1
a181 1
seq_find(sp, lastqp, input, ilen, stype, ispartialp)
d184 2
a185 1
	CHAR_T *input;
d187 1
a187 1
	enum seqtype stype;
d207 20
a226 9
		/* Fast checks on the first character and type. */
		if (qp->input[0] > input[0])
			break;
		if (qp->input[0] < input[0] ||
		    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
			continue;

		/* Check on the real comparison. */
		diff = memcmp(qp->input, input, MIN(qp->ilen, ilen));
d260 24
d286 2
d292 1
a292 1
	enum seqtype stype;
d296 1
d301 2
a302 1
	for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
d308 1
a308 1
			len += ex_printf(EXCOOKIE, "%s", KEY_NAME(sp, *p));
d310 1
a310 1
			len -= ex_printf(EXCOOKIE, " ");
d315 1
a315 2
				len +=
				    ex_printf(EXCOOKIE, "%s", KEY_NAME(sp, *p));
d321 1
a321 1
				len -= ex_printf(EXCOOKIE, " ");
d324 1
a324 2
				(void)ex_printf(EXCOOKIE,
				    "%s", KEY_NAME(sp, *p));
d326 1
a326 1
		(void)ex_printf(EXCOOKIE, "\n");
d334 2
d342 1
a342 1
	enum seqtype stype;
d351 1
a351 2
		if (stype != qp->stype ||
		    F_ISSET(qp, SEQ_FUNCMAP) || !F_ISSET(qp, SEQ_USERDEF))
d375 22
@


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[] = "@@(#)seq.c	8.33 (Berkeley) 8/17/94";
@


1.1.1.2.2.1
log
@file seq.c was added on branch netbsd-1-0 on 1994-08-17 19:26:03 +0000
@
text
@d1 351
@


1.1.1.2.2.2
log
@new public version of nvi
@
text
@a0 351
/*-
 * 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[] = "@@(#)seq.c	8.33 (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 <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"
#include "excmd.h"

/*
 * seq_set --
 *	Internal version to enter a sequence.
 */
int
seq_set(sp, name, nlen, input, ilen, output, olen, stype, flags)
	SCR *sp;
	CHAR_T *name, *input, *output;
	size_t nlen, ilen, olen;
	enum seqtype stype;
	int flags;
{
	CHAR_T *p;
	SEQ *lastqp, *qp;
	int sv_errno;

	/*
	 * An input string must always be present.  The output string
	 * can be NULL, when set internally, that's how we throw away
	 * input.
	 *
	 * Just replace the output field if the string already set.
	 */
	if ((qp = seq_find(sp, &lastqp, input, ilen, stype, NULL)) != NULL) {
		if (output == NULL || olen == 0) {
			p = NULL;
			olen = 0;
		} else if ((p = v_strdup(sp, output, olen)) == NULL) {
			sv_errno = errno;
			goto mem1;
		}
		if (qp->output != NULL)
			free(qp->output);
		qp->olen = olen;
		qp->output = p;
		return (0);
	}

	/* Allocate and initialize SEQ structure. */
	CALLOC(sp, qp, SEQ *, 1, sizeof(SEQ));
	if (qp == NULL) {
		sv_errno = errno;
		goto mem1;
	}

	/* Name. */
	if (name == NULL || nlen == 0)
		qp->name = NULL;
	else if ((qp->name = v_strdup(sp, name, nlen)) == NULL) {
		sv_errno = errno;
		goto mem2;
	}
	qp->nlen = nlen;

	/* Input. */
	if ((qp->input = v_strdup(sp, input, ilen)) == NULL) {
		sv_errno = errno;
		goto mem3;
	}
	qp->ilen = ilen;

	/* Output. */
	if (output == NULL) {
		qp->output = NULL;
		olen = 0;
	} else if ((qp->output = v_strdup(sp, output, olen)) == NULL) {
		sv_errno = errno;
		free(qp->input);
mem3:		if (qp->name != NULL)
			free(qp->name);
mem2:		FREE(qp, sizeof(SEQ));
mem1:		errno = sv_errno;
		msgq(sp, M_SYSERR, NULL);
		return (1);
	}
	qp->olen = olen;

	/* Type, flags. */
	qp->stype = stype;
	qp->flags = flags;

	/* Link into the chain. */
	if (lastqp == NULL) {
		LIST_INSERT_HEAD(&sp->gp->seqq, qp, q);
	} else {
		LIST_INSERT_AFTER(lastqp, qp, q);
	}

	/* Set the fast lookup bit. */
	if (qp->input[0] < MAX_BIT_SEQ)
		bit_set(sp->gp->seqb, qp->input[0]);

	return (0);
}

/*
 * seq_delete --
 *	Delete a sequence.
 */
int
seq_delete(sp, input, ilen, stype)
	SCR *sp;
	CHAR_T *input;
	size_t ilen;
	enum seqtype stype;
{
	SEQ *qp;

	if ((qp = seq_find(sp, NULL, input, ilen, stype, NULL)) == NULL)
		return (1);
	return (seq_mdel(qp));
}

/*
 * seq_mdel --
 *	Delete a map entry, without lookup.
 */
int
seq_mdel(qp)
	SEQ *qp;
{
	LIST_REMOVE(qp, q);
	if (qp->name != NULL)
		free(qp->name);
	free(qp->input);
	if (qp->output != NULL)
		free(qp->output);
	FREE(qp, sizeof(SEQ));
	return (0);
}

/*
 * seq_find --
 *	Search the sequence list for a match to a buffer, if ispartial
 *	isn't NULL, partial matches count.
 */
SEQ *
seq_find(sp, lastqp, input, ilen, stype, ispartialp)
	SCR *sp;
	SEQ **lastqp;
	CHAR_T *input;
	size_t ilen;
	enum seqtype stype;
	int *ispartialp;
{
	SEQ *lqp, *qp;
	int diff;

	/*
	 * Ispartialp is a location where we return if there was a
	 * partial match, i.e. if the string were extended it might
	 * match something.
	 *
	 * XXX
	 * Overload the meaning of ispartialp; only the terminal key
	 * search doesn't want the search limited to complete matches,
	 * i.e. ilen may be longer than the match.
	 */
	if (ispartialp != NULL)
		*ispartialp = 0;
	for (lqp = NULL, qp = sp->gp->seqq.lh_first;
	    qp != NULL; lqp = qp, qp = qp->q.le_next) {
		/* Fast checks on the first character and type. */
		if (qp->input[0] > input[0])
			break;
		if (qp->input[0] < input[0] ||
		    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
			continue;

		/* Check on the real comparison. */
		diff = memcmp(qp->input, input, MIN(qp->ilen, ilen));
		if (diff > 0)
			break;
		if (diff < 0)
			continue;
		/*
		 * If the entry is the same length as the string, return a
		 * match.  If the entry is shorter than the string, return a
		 * match if called from the terminal key routine.  Otherwise,
		 * keep searching for a complete match.
		 */
		if (qp->ilen <= ilen) {
			if (qp->ilen == ilen || ispartialp != NULL) {
				if (lastqp != NULL)
					*lastqp = lqp;
				return (qp);
			}
			continue;
		}
		/*
		 * If the entry longer than the string, return partial match
		 * if called from the terminal key routine.  Otherwise, no
		 * match.
		 */
		if (ispartialp != NULL)
			*ispartialp = 1;
		break;
	}
	if (lastqp != NULL)
		*lastqp = lqp;
	return (NULL);
}

/*
 * seq_dump --
 *	Display the sequence entries of a specified type.
 */
int
seq_dump(sp, stype, isname)
	SCR *sp;
	enum seqtype stype;
	int isname;
{
	CHAR_T *p;
	SEQ *qp;
	int cnt, len, olen;

	cnt = 0;
	for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
		if (stype != qp->stype || F_ISSET(qp, SEQ_FUNCMAP))
			continue;
		++cnt;
		for (p = qp->input,
		    olen = qp->ilen, len = 0; olen > 0; --olen, ++p)
			len += ex_printf(EXCOOKIE, "%s", KEY_NAME(sp, *p));
		for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
			len -= ex_printf(EXCOOKIE, " ");

		if (qp->output != NULL)
			for (p = qp->output,
			    olen = qp->olen, len = 0; olen > 0; --olen, ++p)
				len +=
				    ex_printf(EXCOOKIE, "%s", KEY_NAME(sp, *p));
		else
			len = 0;

		if (isname && qp->name != NULL) {
			for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
				len -= ex_printf(EXCOOKIE, " ");
			for (p = qp->name,
			    olen = qp->nlen; olen > 0; --olen, ++p)
				(void)ex_printf(EXCOOKIE,
				    "%s", KEY_NAME(sp, *p));
		}
		(void)ex_printf(EXCOOKIE, "\n");
	}
	return (cnt);
}

/*
 * seq_save --
 *	Save the sequence entries to a file.
 */
int
seq_save(sp, fp, prefix, stype)
	SCR *sp;
	FILE *fp;
	char *prefix;
	enum seqtype stype;
{
	CHAR_T *p;
	SEQ *qp;
	size_t olen;
	int ch;

	/* Write a sequence command for all keys the user defined. */
	for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
		if (stype != qp->stype ||
		    F_ISSET(qp, SEQ_FUNCMAP) || !F_ISSET(qp, SEQ_USERDEF))
			continue;
		if (prefix)
			(void)fprintf(fp, "%s", prefix);
		for (p = qp->input, olen = qp->ilen; olen > 0; --olen) {
			ch = *p++;
			if (ch == CH_LITERAL || ch == '|' ||
			    isblank(ch) || KEY_VAL(sp, ch) == K_NL)
				(void)putc(CH_LITERAL, fp);
			(void)putc(ch, fp);
		}
		(void)putc(' ', fp);
		if (qp->output != NULL)
			for (p = qp->output,
			    olen = qp->olen; olen > 0; --olen) {
				ch = *p++;
				if (ch == CH_LITERAL || ch == '|' ||
				    KEY_VAL(sp, ch) == K_NL)
					(void)putc(CH_LITERAL, fp);
				(void)putc(ch, fp);
			}
		(void)putc('\n', fp);
	}
	return (0);
}
@


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[] = "@@(#)seq.c	10.10 (Berkeley) 3/30/96";
d40 1
d46 1
d50 5
d56 2
a57 1
#include "common.h"
a61 3
 *
 * PUBLIC: int seq_set __P((SCR *, CHAR_T *,
 * PUBLIC:    size_t, CHAR_T *, size_t, CHAR_T *, size_t, seq_t, int));
d68 1
a68 1
	seq_t stype;
d82 1
a82 4
	if ((qp =
	    seq_find(sp, &lastqp, NULL, input, ilen, stype, NULL)) != NULL) {
		if (LF_ISSET(SEQ_NOOVERWRITE))
			return (0);
d129 1
a129 1
mem2:		free(qp);
a156 2
 *
 * PUBLIC: int seq_delete __P((SCR *, CHAR_T *, size_t, seq_t));
d163 1
a163 1
	seq_t stype;
d167 1
a167 1
	if ((qp = seq_find(sp, NULL, NULL, input, ilen, stype, NULL)) == NULL)
a174 2
 *
 * PUBLIC: int seq_mdel __P((SEQ *));
d186 1
a186 1
	free(qp);
a193 3
 *
 * PUBLIC: SEQ *seq_find
 * PUBLIC:    __P((SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *));
d196 1
a196 1
seq_find(sp, lastqp, e_input, c_input, ilen, stype, ispartialp)
d199 1
a199 2
	EVENT *e_input;
	CHAR_T *c_input;
d201 1
a201 1
	seq_t stype;
d221 9
a229 20
		/*
		 * Fast checks on the first character and type, and then
		 * a real comparison.
		 */
		if (e_input == NULL) {
			if (qp->input[0] > c_input[0])
				break;
			if (qp->input[0] < c_input[0] ||
			    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
				continue;
			diff = memcmp(qp->input, c_input, MIN(qp->ilen, ilen));
		} else {
			if (qp->input[0] > e_input->e_c)
				break;
			if (qp->input[0] < e_input->e_c ||
			    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
				continue;
			diff =
			    e_memcmp(qp->input, e_input, MIN(qp->ilen, ilen));
		}
a262 24
 * seq_close --
 *	Discard all sequences.
 *
 * PUBLIC: void seq_close __P((GS *));
 */
void
seq_close(gp)
	GS *gp;
{
	SEQ *qp;

	while ((qp = gp->seqq.lh_first) != NULL) {
		if (qp->name != NULL)
			free(qp->name);
		if (qp->input != NULL)
			free(qp->input);
		if (qp->output != NULL)
			free(qp->output);
		LIST_REMOVE(qp, q);
		free(qp);
	}
}

/*
a264 2
 *
 * PUBLIC: int seq_dump __P((SCR *, seq_t, int));
d269 1
a269 1
	seq_t stype;
a272 1
	GS *gp;
d277 1
a277 2
	gp = sp->gp;
	for (qp = gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
d283 1
a283 1
			len += ex_puts(sp, KEY_NAME(sp, *p));
d285 1
a285 1
			len -= ex_puts(sp, " ");
d290 2
a291 1
				len += ex_puts(sp, KEY_NAME(sp, *p));
d297 1
a297 1
				len -= ex_puts(sp, " ");
d300 2
a301 1
				(void)ex_puts(sp, KEY_NAME(sp, *p));
d303 1
a303 1
		(void)ex_puts(sp, "\n");
a310 2
 *
 * PUBLIC: int seq_save __P((SCR *, FILE *, char *, seq_t));
d317 1
a317 1
	seq_t stype;
d326 2
a327 1
		if (stype != qp->stype || !F_ISSET(qp, SEQ_USERDEF))
a350 22
}

/*
 * e_memcmp --
 *	Compare a string of EVENT's to a string of CHAR_T's.
 *
 * PUBLIC: int e_memcmp __P((CHAR_T *, EVENT *, size_t));
 */
int
e_memcmp(p1, ep, n)
	CHAR_T *p1;
	EVENT *ep;
	size_t n;
{
	if (n != 0) {
                do {
                        if (*p1++ != ep->e_c)
                                return (*--p1 - ep->e_c);
			++ep;
                } while (--n != 0);
        }
        return (0);
@
