1994-08-16 23:36:45 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1996-11-01 06:45:43 +00:00
|
|
|
* Copyright (c) 1992, 1993, 1994, 1995, 1996
|
|
|
|
* Keith Bostic. All rights reserved.
|
1994-08-16 23:36:45 +00:00
|
|
|
*
|
1996-11-01 06:45:43 +00:00
|
|
|
* See the LICENSE file for redistribution information.
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/queue.h>
|
2013-08-11 09:44:58 +00:00
|
|
|
#include <sys/time.h>
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
#include <bitstring.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
#include "common.h"
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* seq_set --
|
|
|
|
* Internal version to enter a sequence.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int seq_set(SCR *, CHAR_T *,
|
|
|
|
* PUBLIC: size_t, CHAR_T *, size_t, CHAR_T *, size_t, seq_t, int);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
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)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
|
|
|
CHAR_T *p;
|
1996-11-01 06:45:43 +00:00
|
|
|
SEQ *lastqp, *qp;
|
1994-08-16 23:36:45 +00:00
|
|
|
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.
|
|
|
|
*/
|
1996-11-01 06:45:43 +00:00
|
|
|
if ((qp =
|
|
|
|
seq_find(sp, &lastqp, NULL, input, ilen, stype, NULL)) != NULL) {
|
|
|
|
if (LF_ISSET(SEQ_NOOVERWRITE))
|
|
|
|
return (0);
|
1994-08-16 23:36:45 +00:00
|
|
|
if (output == NULL || olen == 0) {
|
|
|
|
p = NULL;
|
|
|
|
olen = 0;
|
2013-08-11 09:44:58 +00:00
|
|
|
} else if ((p = v_wstrdup(sp, output, olen)) == NULL) {
|
1994-08-16 23:36:45 +00:00
|
|
|
sv_errno = errno;
|
|
|
|
goto mem1;
|
|
|
|
}
|
2020-09-09 07:57:59 +00:00
|
|
|
free(qp->output);
|
1994-08-16 23:36:45 +00:00
|
|
|
qp->olen = olen;
|
|
|
|
qp->output = p;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate and initialize SEQ structure. */
|
2020-09-09 07:57:59 +00:00
|
|
|
CALLOC(sp, qp, 1, sizeof(SEQ));
|
1994-08-16 23:36:45 +00:00
|
|
|
if (qp == NULL) {
|
|
|
|
sv_errno = errno;
|
|
|
|
goto mem1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Name. */
|
|
|
|
if (name == NULL || nlen == 0)
|
|
|
|
qp->name = NULL;
|
2013-08-11 09:44:58 +00:00
|
|
|
else if ((qp->name = v_wstrdup(sp, name, nlen)) == NULL) {
|
1994-08-16 23:36:45 +00:00
|
|
|
sv_errno = errno;
|
|
|
|
goto mem2;
|
|
|
|
}
|
|
|
|
qp->nlen = nlen;
|
|
|
|
|
|
|
|
/* Input. */
|
2013-08-11 09:44:58 +00:00
|
|
|
if ((qp->input = v_wstrdup(sp, input, ilen)) == NULL) {
|
1994-08-16 23:36:45 +00:00
|
|
|
sv_errno = errno;
|
|
|
|
goto mem3;
|
|
|
|
}
|
|
|
|
qp->ilen = ilen;
|
|
|
|
|
|
|
|
/* Output. */
|
|
|
|
if (output == NULL) {
|
|
|
|
qp->output = NULL;
|
|
|
|
olen = 0;
|
2013-08-11 09:44:58 +00:00
|
|
|
} else if ((qp->output = v_wstrdup(sp, output, olen)) == NULL) {
|
1994-08-16 23:36:45 +00:00
|
|
|
sv_errno = errno;
|
|
|
|
free(qp->input);
|
2020-09-09 07:57:59 +00:00
|
|
|
mem3: free(qp->name);
|
1996-11-01 06:45:43 +00:00
|
|
|
mem2: free(qp);
|
1994-08-16 23:36:45 +00:00
|
|
|
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) {
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_INSERT_HEAD(sp->gp->seqq, qp, q);
|
1994-08-16 23:36:45 +00:00
|
|
|
} else {
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_INSERT_AFTER(lastqp, qp, q);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the fast lookup bit. */
|
2013-08-11 09:44:58 +00:00
|
|
|
if ((qp->input[0] & ~MAX_BIT_SEQ) == 0)
|
1996-11-01 06:45:43 +00:00
|
|
|
bit_set(sp->gp->seqb, qp->input[0]);
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* seq_delete --
|
|
|
|
* Delete a sequence.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int seq_delete(SCR *, CHAR_T *, size_t, seq_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
seq_delete(SCR *sp, CHAR_T *input, size_t ilen, seq_t stype)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
2013-08-11 09:44:58 +00:00
|
|
|
SEQ *qp, *pre_qp = NULL;
|
|
|
|
int diff;
|
1994-08-16 23:36:45 +00:00
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_FOREACH(qp, sp->gp->seqq, q) {
|
|
|
|
if (qp->stype == stype && qp->ilen == ilen) {
|
|
|
|
diff = MEMCMP(qp->input, input, ilen);
|
|
|
|
if (!diff) {
|
|
|
|
if (F_ISSET(qp, SEQ_FUNCMAP))
|
|
|
|
break;
|
|
|
|
if (qp == SLIST_FIRST(sp->gp->seqq))
|
|
|
|
SLIST_REMOVE_HEAD(sp->gp->seqq, q);
|
|
|
|
else
|
|
|
|
SLIST_REMOVE_AFTER(pre_qp, q);
|
|
|
|
return (seq_free(qp));
|
|
|
|
}
|
|
|
|
if (diff > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pre_qp = qp;
|
|
|
|
}
|
|
|
|
return (1);
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-08-11 09:44:58 +00:00
|
|
|
* seq_free --
|
|
|
|
* Free a map entry.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int seq_free(SEQ *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-08-11 09:44:58 +00:00
|
|
|
seq_free(SEQ *qp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
2020-09-09 07:57:59 +00:00
|
|
|
free(qp->name);
|
|
|
|
free(qp->input);
|
|
|
|
free(qp->output);
|
1996-11-01 06:45:43 +00:00
|
|
|
free(qp);
|
1994-08-16 23:36:45 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* seq_find --
|
|
|
|
* Search the sequence list for a match to a buffer, if ispartial
|
|
|
|
* isn't NULL, partial matches count.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
|
|
|
* PUBLIC: SEQ *seq_find
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: (SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
SEQ *
|
2020-09-09 07:57:59 +00:00
|
|
|
seq_find(SCR *sp, SEQ **lastqp, EVENT *e_input, CHAR_T *c_input, size_t ilen,
|
|
|
|
seq_t stype, int *ispartialp)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
2013-08-11 09:44:58 +00:00
|
|
|
SEQ *lqp = NULL, *qp;
|
1994-08-16 23:36:45 +00:00
|
|
|
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;
|
2013-08-11 09:44:58 +00:00
|
|
|
for (qp = SLIST_FIRST(sp->gp->seqq); qp != NULL;
|
|
|
|
lqp = qp, qp = SLIST_NEXT(qp, q)) {
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2013-08-11 09:44:58 +00:00
|
|
|
diff = MEMCMP(qp->input, c_input, MIN(qp->ilen, ilen));
|
1996-11-01 06:45:43 +00:00
|
|
|
} 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));
|
|
|
|
}
|
1994-08-16 23:36:45 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
1996-11-01 06:45:43 +00:00
|
|
|
/*
|
|
|
|
* seq_close --
|
|
|
|
* Discard all sequences.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: void seq_close(GS *);
|
1996-11-01 06:45:43 +00:00
|
|
|
*/
|
|
|
|
void
|
2013-08-11 09:44:58 +00:00
|
|
|
seq_close(GS *gp)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
SEQ *qp;
|
|
|
|
|
2013-08-11 09:44:58 +00:00
|
|
|
while ((qp = SLIST_FIRST(gp->seqq)) != NULL) {
|
|
|
|
SLIST_REMOVE_HEAD(gp->seqq, q);
|
|
|
|
(void)seq_free(qp);
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-08-16 23:36:45 +00:00
|
|
|
/*
|
|
|
|
* seq_dump --
|
|
|
|
* Display the sequence entries of a specified type.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int seq_dump(SCR *, seq_t, int);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
seq_dump(SCR *sp, seq_t stype, int isname)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
1996-11-01 06:45:43 +00:00
|
|
|
CHAR_T *p;
|
|
|
|
GS *gp;
|
1994-08-16 23:36:45 +00:00
|
|
|
SEQ *qp;
|
|
|
|
int cnt, len, olen;
|
|
|
|
|
|
|
|
cnt = 0;
|
1996-11-01 06:45:43 +00:00
|
|
|
gp = sp->gp;
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_FOREACH(qp, sp->gp->seqq, q) {
|
1994-08-16 23:36:45 +00:00
|
|
|
if (stype != qp->stype || F_ISSET(qp, SEQ_FUNCMAP))
|
|
|
|
continue;
|
|
|
|
++cnt;
|
|
|
|
for (p = qp->input,
|
|
|
|
olen = qp->ilen, len = 0; olen > 0; --olen, ++p)
|
1996-11-01 06:45:43 +00:00
|
|
|
len += ex_puts(sp, KEY_NAME(sp, *p));
|
1994-08-16 23:36:45 +00:00
|
|
|
for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
|
1996-11-01 06:45:43 +00:00
|
|
|
len -= ex_puts(sp, " ");
|
1994-08-16 23:36:45 +00:00
|
|
|
|
|
|
|
if (qp->output != NULL)
|
|
|
|
for (p = qp->output,
|
|
|
|
olen = qp->olen, len = 0; olen > 0; --olen, ++p)
|
1996-11-01 06:45:43 +00:00
|
|
|
len += ex_puts(sp, KEY_NAME(sp, *p));
|
1994-08-16 23:36:45 +00:00
|
|
|
else
|
|
|
|
len = 0;
|
|
|
|
|
|
|
|
if (isname && qp->name != NULL) {
|
|
|
|
for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
|
1996-11-01 06:45:43 +00:00
|
|
|
len -= ex_puts(sp, " ");
|
1994-08-16 23:36:45 +00:00
|
|
|
for (p = qp->name,
|
|
|
|
olen = qp->nlen; olen > 0; --olen, ++p)
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)ex_puts(sp, KEY_NAME(sp, *p));
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
(void)ex_puts(sp, "\n");
|
1994-08-16 23:36:45 +00:00
|
|
|
}
|
|
|
|
return (cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* seq_save --
|
|
|
|
* Save the sequence entries to a file.
|
1996-11-01 06:45:43 +00:00
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int seq_save(SCR *, FILE *, char *, seq_t);
|
1994-08-16 23:36:45 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
seq_save(SCR *sp, FILE *fp, char *prefix, seq_t stype)
|
1994-08-16 23:36:45 +00:00
|
|
|
{
|
1996-11-01 06:45:43 +00:00
|
|
|
CHAR_T *p;
|
1994-08-16 23:36:45 +00:00
|
|
|
SEQ *qp;
|
|
|
|
size_t olen;
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
/* Write a sequence command for all keys the user defined. */
|
2013-08-11 09:44:58 +00:00
|
|
|
SLIST_FOREACH(qp, sp->gp->seqq, q) {
|
1996-11-01 06:45:43 +00:00
|
|
|
if (stype != qp->stype || !F_ISSET(qp, SEQ_USERDEF))
|
1994-08-16 23:36:45 +00:00
|
|
|
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 == '|' ||
|
2013-08-11 09:44:58 +00:00
|
|
|
cmdskip(ch) || KEY_VAL(sp, ch) == K_NL)
|
1994-08-16 23:36:45 +00:00
|
|
|
(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);
|
|
|
|
}
|
1996-11-01 06:45:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* e_memcmp --
|
|
|
|
* Compare a string of EVENT's to a string of CHAR_T's.
|
|
|
|
*
|
2015-04-10 13:26:05 +00:00
|
|
|
* PUBLIC: int e_memcmp(CHAR_T *, EVENT *, size_t);
|
1996-11-01 06:45:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2020-09-09 07:57:59 +00:00
|
|
|
e_memcmp(CHAR_T *p1, EVENT *ep, size_t n)
|
1996-11-01 06:45:43 +00:00
|
|
|
{
|
|
|
|
if (n != 0) {
|
2015-04-10 13:26:05 +00:00
|
|
|
do {
|
|
|
|
if (*p1++ != ep->e_c)
|
|
|
|
return (*--p1 - ep->e_c);
|
1996-11-01 06:45:43 +00:00
|
|
|
++ep;
|
2015-04-10 13:26:05 +00:00
|
|
|
} while (--n != 0);
|
|
|
|
}
|
|
|
|
return (0);
|
1996-11-01 06:45:43 +00:00
|
|
|
}
|