Home | History | Annotate | Line # | Download | only in common
      1  1.4  christos /*	$NetBSD: seq.c,v 1.4 2014/01/26 21:43:45 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.4  christos #include <sys/cdefs.h>
     14  1.4  christos #if 0
     15  1.1  christos #ifndef lint
     16  1.1  christos static const char sccsid[] = "Id: seq.c,v 10.15 2001/06/25 15:19:12 skimo Exp  (Berkeley) Date: 2001/06/25 15:19:12 ";
     17  1.1  christos #endif /* not lint */
     18  1.4  christos #else
     19  1.4  christos __RCSID("$NetBSD: seq.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
     20  1.4  christos #endif
     21  1.1  christos 
     22  1.1  christos #include <sys/types.h>
     23  1.1  christos #include <sys/queue.h>
     24  1.1  christos 
     25  1.1  christos #include <bitstring.h>
     26  1.1  christos #include <ctype.h>
     27  1.1  christos #include <errno.h>
     28  1.1  christos #include <limits.h>
     29  1.1  christos #include <stdio.h>
     30  1.1  christos #include <stdlib.h>
     31  1.1  christos #include <string.h>
     32  1.1  christos 
     33  1.1  christos #include "common.h"
     34  1.1  christos 
     35  1.1  christos /*
     36  1.1  christos  * seq_set --
     37  1.1  christos  *	Internal version to enter a sequence.
     38  1.1  christos  *
     39  1.1  christos  * PUBLIC: int seq_set __P((SCR *, CHAR_T *,
     40  1.1  christos  * PUBLIC:    size_t, CHAR_T *, size_t, CHAR_T *, size_t, seq_t, int));
     41  1.1  christos  */
     42  1.1  christos int
     43  1.1  christos seq_set(SCR *sp, CHAR_T *name, size_t nlen, CHAR_T *input, size_t ilen, CHAR_T *output, size_t olen, seq_t stype, int flags)
     44  1.1  christos {
     45  1.1  christos 	CHAR_T *p;
     46  1.1  christos 	SEQ *lastqp, *qp;
     47  1.1  christos 	int sv_errno;
     48  1.1  christos 
     49  1.1  christos 	/*
     50  1.1  christos 	 * An input string must always be present.  The output string
     51  1.1  christos 	 * can be NULL, when set internally, that's how we throw away
     52  1.1  christos 	 * input.
     53  1.1  christos 	 *
     54  1.1  christos 	 * Just replace the output field if the string already set.
     55  1.1  christos 	 */
     56  1.1  christos 	if ((qp =
     57  1.1  christos 	    seq_find(sp, &lastqp, NULL, input, ilen, stype, NULL)) != NULL) {
     58  1.1  christos 		if (LF_ISSET(SEQ_NOOVERWRITE))
     59  1.1  christos 			return (0);
     60  1.1  christos 		if (output == NULL || olen == 0) {
     61  1.1  christos 			p = NULL;
     62  1.1  christos 			olen = 0;
     63  1.1  christos 		} else if ((p = v_wstrdup(sp, output, olen)) == NULL) {
     64  1.1  christos 			sv_errno = errno;
     65  1.1  christos 			goto mem1;
     66  1.1  christos 		}
     67  1.1  christos 		if (qp->output != NULL)
     68  1.1  christos 			free(qp->output);
     69  1.1  christos 		qp->olen = olen;
     70  1.1  christos 		qp->output = p;
     71  1.1  christos 		return (0);
     72  1.1  christos 	}
     73  1.1  christos 
     74  1.1  christos 	/* Allocate and initialize SEQ structure. */
     75  1.1  christos 	CALLOC(sp, qp, SEQ *, 1, sizeof(SEQ));
     76  1.1  christos 	if (qp == NULL) {
     77  1.1  christos 		sv_errno = errno;
     78  1.1  christos 		goto mem1;
     79  1.1  christos 	}
     80  1.1  christos 
     81  1.1  christos 	/* Name. */
     82  1.1  christos 	if (name == NULL || nlen == 0)
     83  1.1  christos 		qp->name = NULL;
     84  1.1  christos 	else if ((qp->name = v_wstrdup(sp, name, nlen)) == NULL) {
     85  1.1  christos 		sv_errno = errno;
     86  1.1  christos 		goto mem2;
     87  1.1  christos 	}
     88  1.1  christos 	qp->nlen = nlen;
     89  1.1  christos 
     90  1.1  christos 	/* Input. */
     91  1.1  christos 	if ((qp->input = v_wstrdup(sp, input, ilen)) == NULL) {
     92  1.1  christos 		sv_errno = errno;
     93  1.1  christos 		goto mem3;
     94  1.1  christos 	}
     95  1.1  christos 	qp->ilen = ilen;
     96  1.1  christos 
     97  1.1  christos 	/* Output. */
     98  1.1  christos 	if (output == NULL) {
     99  1.1  christos 		qp->output = NULL;
    100  1.1  christos 		olen = 0;
    101  1.1  christos 	} else if ((qp->output = v_wstrdup(sp, output, olen)) == NULL) {
    102  1.1  christos 		sv_errno = errno;
    103  1.1  christos 		free(qp->input);
    104  1.1  christos mem3:		if (qp->name != NULL)
    105  1.1  christos 			free(qp->name);
    106  1.1  christos mem2:		free(qp);
    107  1.1  christos mem1:		errno = sv_errno;
    108  1.1  christos 		msgq(sp, M_SYSERR, NULL);
    109  1.1  christos 		return (1);
    110  1.1  christos 	}
    111  1.1  christos 	qp->olen = olen;
    112  1.1  christos 
    113  1.1  christos 	/* Type, flags. */
    114  1.1  christos 	qp->stype = stype;
    115  1.1  christos 	qp->flags = flags;
    116  1.1  christos 
    117  1.1  christos 	/* Link into the chain. */
    118  1.1  christos 	if (lastqp == NULL) {
    119  1.1  christos 		LIST_INSERT_HEAD(&sp->gp->seqq, qp, q);
    120  1.1  christos 	} else {
    121  1.1  christos 		LIST_INSERT_AFTER(lastqp, qp, q);
    122  1.1  christos 	}
    123  1.1  christos 
    124  1.1  christos 	/* Set the fast lookup bit. */
    125  1.2  christos 	if ((qp->input[0] & ~MAX_BIT_SEQ) == 0)
    126  1.1  christos 		bit_set(sp->gp->seqb, qp->input[0]);
    127  1.1  christos 
    128  1.1  christos 	return (0);
    129  1.1  christos }
    130  1.1  christos 
    131  1.1  christos /*
    132  1.1  christos  * seq_delete --
    133  1.1  christos  *	Delete a sequence.
    134  1.1  christos  *
    135  1.1  christos  * PUBLIC: int seq_delete __P((SCR *, CHAR_T *, size_t, seq_t));
    136  1.1  christos  */
    137  1.1  christos int
    138  1.1  christos seq_delete(SCR *sp, CHAR_T *input, size_t ilen, seq_t stype)
    139  1.1  christos {
    140  1.1  christos 	SEQ *qp;
    141  1.1  christos 
    142  1.1  christos 	if ((qp = seq_find(sp, NULL, NULL, input, ilen, stype, NULL)) == NULL)
    143  1.1  christos 		return (1);
    144  1.1  christos 	return (seq_mdel(qp));
    145  1.1  christos }
    146  1.1  christos 
    147  1.1  christos /*
    148  1.1  christos  * seq_mdel --
    149  1.1  christos  *	Delete a map entry, without lookup.
    150  1.1  christos  *
    151  1.1  christos  * PUBLIC: int seq_mdel __P((SEQ *));
    152  1.1  christos  */
    153  1.1  christos int
    154  1.1  christos seq_mdel(SEQ *qp)
    155  1.1  christos {
    156  1.1  christos 	LIST_REMOVE(qp, q);
    157  1.1  christos 	if (qp->name != NULL)
    158  1.1  christos 		free(qp->name);
    159  1.1  christos 	free(qp->input);
    160  1.1  christos 	if (qp->output != NULL)
    161  1.1  christos 		free(qp->output);
    162  1.1  christos 	free(qp);
    163  1.1  christos 	return (0);
    164  1.1  christos }
    165  1.1  christos 
    166  1.1  christos /*
    167  1.1  christos  * seq_find --
    168  1.1  christos  *	Search the sequence list for a match to a buffer, if ispartial
    169  1.1  christos  *	isn't NULL, partial matches count.
    170  1.1  christos  *
    171  1.1  christos  * PUBLIC: SEQ *seq_find
    172  1.1  christos  * PUBLIC:    __P((SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *));
    173  1.1  christos  */
    174  1.1  christos SEQ *
    175  1.1  christos seq_find(SCR *sp, SEQ **lastqp, EVENT *e_input, CHAR_T *c_input, size_t ilen, seq_t stype, int *ispartialp)
    176  1.1  christos {
    177  1.1  christos 	SEQ *lqp, *qp;
    178  1.1  christos 	int diff;
    179  1.1  christos 
    180  1.1  christos 	/*
    181  1.1  christos 	 * Ispartialp is a location where we return if there was a
    182  1.1  christos 	 * partial match, i.e. if the string were extended it might
    183  1.1  christos 	 * match something.
    184  1.1  christos 	 *
    185  1.1  christos 	 * XXX
    186  1.1  christos 	 * Overload the meaning of ispartialp; only the terminal key
    187  1.1  christos 	 * search doesn't want the search limited to complete matches,
    188  1.1  christos 	 * i.e. ilen may be longer than the match.
    189  1.1  christos 	 */
    190  1.1  christos 	if (ispartialp != NULL)
    191  1.1  christos 		*ispartialp = 0;
    192  1.3  christos 
    193  1.3  christos 	for (lqp = NULL, qp = LIST_FIRST(&sp->gp->seqq);
    194  1.3  christos 	    qp != NULL;
    195  1.3  christos 	    lqp = qp, qp = LIST_NEXT(qp, q)) {
    196  1.1  christos 		/*
    197  1.1  christos 		 * Fast checks on the first character and type, and then
    198  1.1  christos 		 * a real comparison.
    199  1.1  christos 		 */
    200  1.1  christos 		if (e_input == NULL) {
    201  1.1  christos 			if (qp->input[0] > c_input[0])
    202  1.1  christos 				break;
    203  1.1  christos 			if (qp->input[0] < c_input[0] ||
    204  1.1  christos 			    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
    205  1.1  christos 				continue;
    206  1.1  christos 			diff = MEMCMP(qp->input, c_input, MIN(qp->ilen, ilen));
    207  1.1  christos 		} else {
    208  1.1  christos 			if (qp->input[0] > e_input->e_c)
    209  1.1  christos 				break;
    210  1.1  christos 			if (qp->input[0] < e_input->e_c ||
    211  1.1  christos 			    qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
    212  1.1  christos 				continue;
    213  1.1  christos 			diff =
    214  1.1  christos 			    e_memcmp(qp->input, e_input, MIN(qp->ilen, ilen));
    215  1.1  christos 		}
    216  1.1  christos 		if (diff > 0)
    217  1.1  christos 			break;
    218  1.1  christos 		if (diff < 0)
    219  1.1  christos 			continue;
    220  1.1  christos 		/*
    221  1.1  christos 		 * If the entry is the same length as the string, return a
    222  1.1  christos 		 * match.  If the entry is shorter than the string, return a
    223  1.1  christos 		 * match if called from the terminal key routine.  Otherwise,
    224  1.1  christos 		 * keep searching for a complete match.
    225  1.1  christos 		 */
    226  1.1  christos 		if (qp->ilen <= ilen) {
    227  1.1  christos 			if (qp->ilen == ilen || ispartialp != NULL) {
    228  1.1  christos 				if (lastqp != NULL)
    229  1.1  christos 					*lastqp = lqp;
    230  1.1  christos 				return (qp);
    231  1.1  christos 			}
    232  1.1  christos 			continue;
    233  1.1  christos 		}
    234  1.1  christos 		/*
    235  1.1  christos 		 * If the entry longer than the string, return partial match
    236  1.1  christos 		 * if called from the terminal key routine.  Otherwise, no
    237  1.1  christos 		 * match.
    238  1.1  christos 		 */
    239  1.1  christos 		if (ispartialp != NULL)
    240  1.1  christos 			*ispartialp = 1;
    241  1.1  christos 		break;
    242  1.1  christos 	}
    243  1.1  christos 	if (lastqp != NULL)
    244  1.1  christos 		*lastqp = lqp;
    245  1.1  christos 	return (NULL);
    246  1.1  christos }
    247  1.1  christos 
    248  1.1  christos /*
    249  1.1  christos  * seq_close --
    250  1.1  christos  *	Discard all sequences.
    251  1.1  christos  *
    252  1.1  christos  * PUBLIC: void seq_close __P((GS *));
    253  1.1  christos  */
    254  1.1  christos void
    255  1.1  christos seq_close(GS *gp)
    256  1.1  christos {
    257  1.1  christos 	SEQ *qp;
    258  1.1  christos 
    259  1.3  christos 	while ((qp = LIST_FIRST(&gp->seqq)) != NULL) {
    260  1.1  christos 		if (qp->name != NULL)
    261  1.1  christos 			free(qp->name);
    262  1.1  christos 		if (qp->input != NULL)
    263  1.1  christos 			free(qp->input);
    264  1.1  christos 		if (qp->output != NULL)
    265  1.1  christos 			free(qp->output);
    266  1.1  christos 		LIST_REMOVE(qp, q);
    267  1.1  christos 		free(qp);
    268  1.1  christos 	}
    269  1.1  christos }
    270  1.1  christos 
    271  1.1  christos /*
    272  1.1  christos  * seq_dump --
    273  1.1  christos  *	Display the sequence entries of a specified type.
    274  1.1  christos  *
    275  1.1  christos  * PUBLIC: int seq_dump __P((SCR *, seq_t, int));
    276  1.1  christos  */
    277  1.1  christos int
    278  1.1  christos seq_dump(SCR *sp, seq_t stype, int isname)
    279  1.1  christos {
    280  1.1  christos 	CHAR_T *p;
    281  1.1  christos 	GS *gp;
    282  1.1  christos 	SEQ *qp;
    283  1.1  christos 	int cnt, len, olen;
    284  1.1  christos 
    285  1.1  christos 	cnt = 0;
    286  1.1  christos 	gp = sp->gp;
    287  1.3  christos 	LIST_FOREACH(qp, &gp->seqq, q) {
    288  1.1  christos 		if (stype != qp->stype || F_ISSET(qp, SEQ_FUNCMAP))
    289  1.1  christos 			continue;
    290  1.1  christos 		++cnt;
    291  1.1  christos 		for (p = qp->input,
    292  1.1  christos 		    olen = qp->ilen, len = 0; olen > 0; --olen, ++p)
    293  1.2  christos 			len += ex_puts(sp, (char *)KEY_NAME(sp, *p));
    294  1.1  christos 		for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
    295  1.1  christos 			len -= ex_puts(sp, " ");
    296  1.1  christos 
    297  1.1  christos 		if (qp->output != NULL)
    298  1.1  christos 			for (p = qp->output,
    299  1.1  christos 			    olen = qp->olen, len = 0; olen > 0; --olen, ++p)
    300  1.2  christos 				len += ex_puts(sp, (char *)KEY_NAME(sp, *p));
    301  1.1  christos 		else
    302  1.1  christos 			len = 0;
    303  1.1  christos 
    304  1.1  christos 		if (isname && qp->name != NULL) {
    305  1.1  christos 			for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
    306  1.1  christos 				len -= ex_puts(sp, " ");
    307  1.1  christos 			for (p = qp->name,
    308  1.1  christos 			    olen = qp->nlen; olen > 0; --olen, ++p)
    309  1.2  christos 				(void)ex_puts(sp, (char *)KEY_NAME(sp, *p));
    310  1.1  christos 		}
    311  1.1  christos 		(void)ex_puts(sp, "\n");
    312  1.1  christos 	}
    313  1.1  christos 	return (cnt);
    314  1.1  christos }
    315  1.1  christos 
    316  1.1  christos /*
    317  1.1  christos  * seq_save --
    318  1.1  christos  *	Save the sequence entries to a file.
    319  1.1  christos  *
    320  1.2  christos  * PUBLIC: int seq_save __P((SCR *, FILE *, const char *, seq_t));
    321  1.1  christos  */
    322  1.1  christos int
    323  1.2  christos seq_save(SCR *sp, FILE *fp, const char *prefix, seq_t stype)
    324  1.1  christos {
    325  1.1  christos 	CHAR_T *p;
    326  1.1  christos 	SEQ *qp;
    327  1.1  christos 	size_t olen;
    328  1.2  christos 	ARG_CHAR_T ch;
    329  1.1  christos 
    330  1.1  christos 	/* Write a sequence command for all keys the user defined. */
    331  1.3  christos 	LIST_FOREACH(qp, &sp->gp->seqq, q) {
    332  1.1  christos 		if (stype != qp->stype || !F_ISSET(qp, SEQ_USERDEF))
    333  1.1  christos 			continue;
    334  1.1  christos 		if (prefix)
    335  1.1  christos 			(void)fprintf(fp, "%s", prefix);
    336  1.1  christos 		for (p = qp->input, olen = qp->ilen; olen > 0; --olen) {
    337  1.2  christos 			ch = (UCHAR_T)*p++;
    338  1.1  christos 			if (ch == CH_LITERAL || ch == '|' ||
    339  1.2  christos 			    ISBLANK(ch) || KEY_VAL(sp, ch) == K_NL)
    340  1.1  christos 				(void)putc(CH_LITERAL, fp);
    341  1.2  christos 			(void)fprintf(fp, WC, ch);
    342  1.1  christos 		}
    343  1.1  christos 		(void)putc(' ', fp);
    344  1.1  christos 		if (qp->output != NULL)
    345  1.1  christos 			for (p = qp->output,
    346  1.1  christos 			    olen = qp->olen; olen > 0; --olen) {
    347  1.2  christos 				ch = (UCHAR_T)*p++;
    348  1.1  christos 				if (ch == CH_LITERAL || ch == '|' ||
    349  1.1  christos 				    KEY_VAL(sp, ch) == K_NL)
    350  1.1  christos 					(void)putc(CH_LITERAL, fp);
    351  1.2  christos 				(void)fprintf(fp, WC, ch);
    352  1.1  christos 			}
    353  1.1  christos 		(void)putc('\n', fp);
    354  1.1  christos 	}
    355  1.1  christos 	return (0);
    356  1.1  christos }
    357  1.1  christos 
    358  1.1  christos /*
    359  1.1  christos  * e_memcmp --
    360  1.1  christos  *	Compare a string of EVENT's to a string of CHAR_T's.
    361  1.1  christos  *
    362  1.1  christos  * PUBLIC: int e_memcmp __P((CHAR_T *, EVENT *, size_t));
    363  1.1  christos  */
    364  1.1  christos int
    365  1.1  christos e_memcmp(CHAR_T *p1, EVENT *ep, size_t n)
    366  1.1  christos {
    367  1.1  christos 	if (n != 0) {
    368  1.1  christos                 do {
    369  1.1  christos                         if (*p1++ != ep->e_c)
    370  1.1  christos                                 return (*--p1 - ep->e_c);
    371  1.1  christos 			++ep;
    372  1.1  christos                 } while (--n != 0);
    373  1.1  christos         }
    374  1.1  christos         return (0);
    375  1.1  christos }
    376