2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid[] = "@(#)seq.c 10.10 (Berkeley) 3/30/96";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
31 * Internal version to enter a sequence.
33 * PUBLIC: int seq_set __P((SCR *, CHAR_T *,
34 * PUBLIC: size_t, CHAR_T *, size_t, CHAR_T *, size_t, seq_t, int));
37 seq_set(sp, name, nlen, input, ilen, output, olen, stype, flags)
39 CHAR_T *name, *input, *output;
40 size_t nlen, ilen, olen;
49 * An input string must always be present. The output string
50 * can be NULL, when set internally, that's how we throw away
53 * Just replace the output field if the string already set.
56 seq_find(sp, &lastqp, NULL, input, ilen, stype, NULL)) != NULL) {
57 if (LF_ISSET(SEQ_NOOVERWRITE))
59 if (output == NULL || olen == 0) {
62 } else if ((p = v_strdup(sp, output, olen)) == NULL) {
66 if (qp->output != NULL)
73 /* Allocate and initialize SEQ structure. */
74 CALLOC(sp, qp, SEQ *, 1, sizeof(SEQ));
81 if (name == NULL || nlen == 0)
83 else if ((qp->name = v_strdup(sp, name, nlen)) == NULL) {
90 if ((qp->input = v_strdup(sp, input, ilen)) == NULL) {
100 } else if ((qp->output = v_strdup(sp, output, olen)) == NULL) {
103 mem3: if (qp->name != NULL)
106 mem1: errno = sv_errno;
107 msgq(sp, M_SYSERR, NULL);
116 /* Link into the chain. */
117 if (lastqp == NULL) {
118 LIST_INSERT_HEAD(&sp->gp->seqq, qp, q);
120 LIST_INSERT_AFTER(lastqp, qp, q);
123 /* Set the fast lookup bit. */
124 if (qp->input[0] < MAX_BIT_SEQ)
125 bit_set(sp->gp->seqb, qp->input[0]);
134 * PUBLIC: int seq_delete __P((SCR *, CHAR_T *, size_t, seq_t));
137 seq_delete(sp, input, ilen, stype)
145 if ((qp = seq_find(sp, NULL, NULL, input, ilen, stype, NULL)) == NULL)
147 return (seq_mdel(qp));
152 * Delete a map entry, without lookup.
154 * PUBLIC: int seq_mdel __P((SEQ *));
161 if (qp->name != NULL)
164 if (qp->output != NULL)
172 * Search the sequence list for a match to a buffer, if ispartial
173 * isn't NULL, partial matches count.
175 * PUBLIC: SEQ *seq_find
176 * PUBLIC: __P((SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *));
179 seq_find(sp, lastqp, e_input, c_input, ilen, stype, ispartialp)
192 * Ispartialp is a location where we return if there was a
193 * partial match, i.e. if the string were extended it might
197 * Overload the meaning of ispartialp; only the terminal key
198 * search doesn't want the search limited to complete matches,
199 * i.e. ilen may be longer than the match.
201 if (ispartialp != NULL)
203 for (lqp = NULL, qp = sp->gp->seqq.lh_first;
204 qp != NULL; lqp = qp, qp = qp->q.le_next) {
206 * Fast checks on the first character and type, and then
209 if (e_input == NULL) {
210 if (qp->input[0] > c_input[0])
212 if (qp->input[0] < c_input[0] ||
213 qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
215 diff = memcmp(qp->input, c_input, MIN(qp->ilen, ilen));
217 if (qp->input[0] > e_input->e_c)
219 if (qp->input[0] < e_input->e_c ||
220 qp->stype != stype || F_ISSET(qp, SEQ_FUNCMAP))
223 e_memcmp(qp->input, e_input, MIN(qp->ilen, ilen));
230 * If the entry is the same length as the string, return a
231 * match. If the entry is shorter than the string, return a
232 * match if called from the terminal key routine. Otherwise,
233 * keep searching for a complete match.
235 if (qp->ilen <= ilen) {
236 if (qp->ilen == ilen || ispartialp != NULL) {
244 * If the entry longer than the string, return partial match
245 * if called from the terminal key routine. Otherwise, no
248 if (ispartialp != NULL)
259 * Discard all sequences.
261 * PUBLIC: void seq_close __P((GS *));
269 while ((qp = gp->seqq.lh_first) != NULL) {
270 if (qp->name != NULL)
272 if (qp->input != NULL)
274 if (qp->output != NULL)
283 * Display the sequence entries of a specified type.
285 * PUBLIC: int seq_dump __P((SCR *, seq_t, int));
288 seq_dump(sp, stype, isname)
300 for (qp = gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
301 if (stype != qp->stype || F_ISSET(qp, SEQ_FUNCMAP))
305 olen = qp->ilen, len = 0; olen > 0; --olen, ++p)
306 len += ex_puts(sp, KEY_NAME(sp, *p));
307 for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
308 len -= ex_puts(sp, " ");
310 if (qp->output != NULL)
312 olen = qp->olen, len = 0; olen > 0; --olen, ++p)
313 len += ex_puts(sp, KEY_NAME(sp, *p));
317 if (isname && qp->name != NULL) {
318 for (len = STANDARD_TAB - len % STANDARD_TAB; len > 0;)
319 len -= ex_puts(sp, " ");
321 olen = qp->nlen; olen > 0; --olen, ++p)
322 (void)ex_puts(sp, KEY_NAME(sp, *p));
324 (void)ex_puts(sp, "\n");
331 * Save the sequence entries to a file.
333 * PUBLIC: int seq_save __P((SCR *, FILE *, char *, seq_t));
336 seq_save(sp, fp, prefix, stype)
347 /* Write a sequence command for all keys the user defined. */
348 for (qp = sp->gp->seqq.lh_first; qp != NULL; qp = qp->q.le_next) {
349 if (stype != qp->stype || !F_ISSET(qp, SEQ_USERDEF))
352 (void)fprintf(fp, "%s", prefix);
353 for (p = qp->input, olen = qp->ilen; olen > 0; --olen) {
355 if (ch == CH_LITERAL || ch == '|' ||
356 isblank(ch) || KEY_VAL(sp, ch) == K_NL)
357 (void)putc(CH_LITERAL, fp);
361 if (qp->output != NULL)
363 olen = qp->olen; olen > 0; --olen) {
365 if (ch == CH_LITERAL || ch == '|' ||
366 KEY_VAL(sp, ch) == K_NL)
367 (void)putc(CH_LITERAL, fp);
370 (void)putc('\n', fp);
377 * Compare a string of EVENT's to a string of CHAR_T's.
379 * PUBLIC: int e_memcmp __P((CHAR_T *, EVENT *, size_t));
389 if (*p1++ != ep->e_c)
390 return (*--p1 - ep->e_c);