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
[] = "$Id: seq.c,v 10.15 2001/06/25 15:19:12 skimo Exp $ (Berkeley) $Date: 2001/06/25 15:19:12 $";
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(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 * An input string must always be present. The output string
45 * can be NULL, when set internally, that's how we throw away
48 * Just replace the output field if the string already set.
51 seq_find(sp
, &lastqp
, NULL
, input
, ilen
, stype
, NULL
)) != NULL
) {
52 if (LF_ISSET(SEQ_NOOVERWRITE
))
54 if (output
== NULL
|| olen
== 0) {
57 } else if ((p
= v_wstrdup(sp
, output
, olen
)) == NULL
) {
61 if (qp
->output
!= NULL
)
68 /* Allocate and initialize SEQ structure. */
69 CALLOC(sp
, qp
, SEQ
*, 1, sizeof(SEQ
));
76 if (name
== NULL
|| nlen
== 0)
78 else if ((qp
->name
= v_wstrdup(sp
, name
, nlen
)) == NULL
) {
85 if ((qp
->input
= v_wstrdup(sp
, input
, ilen
)) == NULL
) {
95 } else if ((qp
->output
= v_wstrdup(sp
, output
, olen
)) == NULL
) {
98 mem3
: if (qp
->name
!= NULL
)
101 mem1
: errno
= sv_errno
;
102 msgq(sp
, M_SYSERR
, NULL
);
111 /* Link into the chain. */
112 if (lastqp
== NULL
) {
113 LIST_INSERT_HEAD(&sp
->gp
->seqq
, qp
, q
);
115 LIST_INSERT_AFTER(lastqp
, qp
, q
);
118 /* Set the fast lookup bit. */
119 if ((UCHAR_T
)qp
->input
[0] < MAX_BIT_SEQ
)
120 bit_set(sp
->gp
->seqb
, qp
->input
[0]);
129 * PUBLIC: int seq_delete __P((SCR *, CHAR_T *, size_t, seq_t));
132 seq_delete(SCR
*sp
, CHAR_T
*input
, size_t ilen
, seq_t stype
)
136 if ((qp
= seq_find(sp
, NULL
, NULL
, input
, ilen
, stype
, NULL
)) == NULL
)
138 return (seq_mdel(qp
));
143 * Delete a map entry, without lookup.
145 * PUBLIC: int seq_mdel __P((SEQ *));
151 if (qp
->name
!= NULL
)
154 if (qp
->output
!= NULL
)
162 * Search the sequence list for a match to a buffer, if ispartial
163 * isn't NULL, partial matches count.
165 * PUBLIC: SEQ *seq_find
166 * PUBLIC: __P((SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *));
169 seq_find(SCR
*sp
, SEQ
**lastqp
, EVENT
*e_input
, CHAR_T
*c_input
, size_t ilen
, seq_t stype
, int *ispartialp
)
175 * Ispartialp is a location where we return if there was a
176 * partial match, i.e. if the string were extended it might
180 * Overload the meaning of ispartialp; only the terminal key
181 * search doesn't want the search limited to complete matches,
182 * i.e. ilen may be longer than the match.
184 if (ispartialp
!= NULL
)
186 for (lqp
= NULL
, qp
= sp
->gp
->seqq
.lh_first
;
187 qp
!= NULL
; lqp
= qp
, qp
= qp
->q
.le_next
) {
189 * Fast checks on the first character and type, and then
192 if (e_input
== NULL
) {
193 if (qp
->input
[0] > c_input
[0])
195 if (qp
->input
[0] < c_input
[0] ||
196 qp
->stype
!= stype
|| F_ISSET(qp
, SEQ_FUNCMAP
))
198 diff
= MEMCMP(qp
->input
, c_input
, MIN(qp
->ilen
, ilen
));
200 if (qp
->input
[0] > e_input
->e_c
)
202 if (qp
->input
[0] < e_input
->e_c
||
203 qp
->stype
!= stype
|| F_ISSET(qp
, SEQ_FUNCMAP
))
206 e_memcmp(qp
->input
, e_input
, MIN(qp
->ilen
, ilen
));
213 * If the entry is the same length as the string, return a
214 * match. If the entry is shorter than the string, return a
215 * match if called from the terminal key routine. Otherwise,
216 * keep searching for a complete match.
218 if (qp
->ilen
<= ilen
) {
219 if (qp
->ilen
== ilen
|| ispartialp
!= NULL
) {
227 * If the entry longer than the string, return partial match
228 * if called from the terminal key routine. Otherwise, no
231 if (ispartialp
!= NULL
)
242 * Discard all sequences.
244 * PUBLIC: void seq_close __P((GS *));
251 while ((qp
= gp
->seqq
.lh_first
) != NULL
) {
252 if (qp
->name
!= NULL
)
254 if (qp
->input
!= NULL
)
256 if (qp
->output
!= NULL
)
265 * Display the sequence entries of a specified type.
267 * PUBLIC: int seq_dump __P((SCR *, seq_t, int));
270 seq_dump(SCR
*sp
, seq_t stype
, int isname
)
279 for (qp
= gp
->seqq
.lh_first
; qp
!= NULL
; qp
= qp
->q
.le_next
) {
280 if (stype
!= qp
->stype
|| F_ISSET(qp
, SEQ_FUNCMAP
))
284 olen
= qp
->ilen
, len
= 0; olen
> 0; --olen
, ++p
)
285 len
+= ex_puts(sp
, KEY_NAME(sp
, *p
));
286 for (len
= STANDARD_TAB
- len
% STANDARD_TAB
; len
> 0;)
287 len
-= ex_puts(sp
, " ");
289 if (qp
->output
!= NULL
)
291 olen
= qp
->olen
, len
= 0; olen
> 0; --olen
, ++p
)
292 len
+= ex_puts(sp
, KEY_NAME(sp
, *p
));
296 if (isname
&& qp
->name
!= NULL
) {
297 for (len
= STANDARD_TAB
- len
% STANDARD_TAB
; len
> 0;)
298 len
-= ex_puts(sp
, " ");
300 olen
= qp
->nlen
; olen
> 0; --olen
, ++p
)
301 (void)ex_puts(sp
, KEY_NAME(sp
, *p
));
303 (void)ex_puts(sp
, "\n");
310 * Save the sequence entries to a file.
312 * PUBLIC: int seq_save __P((SCR *, FILE *, char *, seq_t));
315 seq_save(SCR
*sp
, FILE *fp
, char *prefix
, seq_t stype
)
322 /* Write a sequence command for all keys the user defined. */
323 for (qp
= sp
->gp
->seqq
.lh_first
; qp
!= NULL
; qp
= qp
->q
.le_next
) {
324 if (stype
!= qp
->stype
|| !F_ISSET(qp
, SEQ_USERDEF
))
327 (void)fprintf(fp
, "%s", prefix
);
328 for (p
= qp
->input
, olen
= qp
->ilen
; olen
> 0; --olen
) {
330 if (ch
== CH_LITERAL
|| ch
== '|' ||
331 isblank(ch
) || KEY_VAL(sp
, ch
) == K_NL
)
332 (void)putc(CH_LITERAL
, fp
);
336 if (qp
->output
!= NULL
)
338 olen
= qp
->olen
; olen
> 0; --olen
) {
340 if (ch
== CH_LITERAL
|| ch
== '|' ||
341 KEY_VAL(sp
, ch
) == K_NL
)
342 (void)putc(CH_LITERAL
, fp
);
345 (void)putc('\n', fp
);
352 * Compare a string of EVENT's to a string of CHAR_T's.
354 * PUBLIC: int e_memcmp __P((CHAR_T *, EVENT *, size_t));
357 e_memcmp(CHAR_T
*p1
, EVENT
*ep
, size_t n
)
361 if (*p1
++ != ep
->e_c
)
362 return (*--p1
- ep
->e_c
);