1 /* $NetBSD: seq.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: seq.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
37 * Internal version to enter a sequence.
39 * PUBLIC: int seq_set __P((SCR *, CHAR_T *,
40 * PUBLIC: size_t, CHAR_T *, size_t, CHAR_T *, size_t, seq_t, int));
43 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
)
50 * An input string must always be present. The output string
51 * can be NULL, when set internally, that's how we throw away
54 * Just replace the output field if the string already set.
57 seq_find(sp
, &lastqp
, NULL
, input
, ilen
, stype
, NULL
)) != NULL
) {
58 if (LF_ISSET(SEQ_NOOVERWRITE
))
60 if (output
== NULL
|| olen
== 0) {
63 } else if ((p
= v_wstrdup(sp
, output
, olen
)) == NULL
) {
67 if (qp
->output
!= NULL
)
74 /* Allocate and initialize SEQ structure. */
75 CALLOC(sp
, qp
, SEQ
*, 1, sizeof(SEQ
));
82 if (name
== NULL
|| nlen
== 0)
84 else if ((qp
->name
= v_wstrdup(sp
, name
, nlen
)) == NULL
) {
91 if ((qp
->input
= v_wstrdup(sp
, input
, ilen
)) == NULL
) {
101 } else if ((qp
->output
= v_wstrdup(sp
, output
, olen
)) == NULL
) {
104 mem3
: if (qp
->name
!= NULL
)
107 mem1
: errno
= sv_errno
;
108 msgq(sp
, M_SYSERR
, NULL
);
117 /* Link into the chain. */
118 if (lastqp
== NULL
) {
119 LIST_INSERT_HEAD(&sp
->gp
->seqq
, qp
, q
);
121 LIST_INSERT_AFTER(lastqp
, qp
, q
);
124 /* Set the fast lookup bit. */
125 if ((qp
->input
[0] & ~MAX_BIT_SEQ
) == 0)
126 bit_set(sp
->gp
->seqb
, qp
->input
[0]);
135 * PUBLIC: int seq_delete __P((SCR *, CHAR_T *, size_t, seq_t));
138 seq_delete(SCR
*sp
, CHAR_T
*input
, size_t ilen
, seq_t stype
)
142 if ((qp
= seq_find(sp
, NULL
, NULL
, input
, ilen
, stype
, NULL
)) == NULL
)
144 return (seq_mdel(qp
));
149 * Delete a map entry, without lookup.
151 * PUBLIC: int seq_mdel __P((SEQ *));
157 if (qp
->name
!= NULL
)
160 if (qp
->output
!= NULL
)
168 * Search the sequence list for a match to a buffer, if ispartial
169 * isn't NULL, partial matches count.
171 * PUBLIC: SEQ *seq_find
172 * PUBLIC: __P((SCR *, SEQ **, EVENT *, CHAR_T *, size_t, seq_t, int *));
175 seq_find(SCR
*sp
, SEQ
**lastqp
, EVENT
*e_input
, CHAR_T
*c_input
, size_t ilen
, seq_t stype
, int *ispartialp
)
181 * Ispartialp is a location where we return if there was a
182 * partial match, i.e. if the string were extended it might
186 * Overload the meaning of ispartialp; only the terminal key
187 * search doesn't want the search limited to complete matches,
188 * i.e. ilen may be longer than the match.
190 if (ispartialp
!= NULL
)
193 for (lqp
= NULL
, qp
= LIST_FIRST(&sp
->gp
->seqq
);
195 lqp
= qp
, qp
= LIST_NEXT(qp
, q
)) {
197 * Fast checks on the first character and type, and then
200 if (e_input
== NULL
) {
201 if (qp
->input
[0] > c_input
[0])
203 if (qp
->input
[0] < c_input
[0] ||
204 qp
->stype
!= stype
|| F_ISSET(qp
, SEQ_FUNCMAP
))
206 diff
= MEMCMP(qp
->input
, c_input
, MIN(qp
->ilen
, ilen
));
208 if (qp
->input
[0] > e_input
->e_c
)
210 if (qp
->input
[0] < e_input
->e_c
||
211 qp
->stype
!= stype
|| F_ISSET(qp
, SEQ_FUNCMAP
))
214 e_memcmp(qp
->input
, e_input
, MIN(qp
->ilen
, ilen
));
221 * If the entry is the same length as the string, return a
222 * match. If the entry is shorter than the string, return a
223 * match if called from the terminal key routine. Otherwise,
224 * keep searching for a complete match.
226 if (qp
->ilen
<= ilen
) {
227 if (qp
->ilen
== ilen
|| ispartialp
!= NULL
) {
235 * If the entry longer than the string, return partial match
236 * if called from the terminal key routine. Otherwise, no
239 if (ispartialp
!= NULL
)
250 * Discard all sequences.
252 * PUBLIC: void seq_close __P((GS *));
259 while ((qp
= LIST_FIRST(&gp
->seqq
)) != NULL
) {
260 if (qp
->name
!= NULL
)
262 if (qp
->input
!= NULL
)
264 if (qp
->output
!= NULL
)
273 * Display the sequence entries of a specified type.
275 * PUBLIC: int seq_dump __P((SCR *, seq_t, int));
278 seq_dump(SCR
*sp
, seq_t stype
, int isname
)
287 LIST_FOREACH(qp
, &gp
->seqq
, q
) {
288 if (stype
!= qp
->stype
|| F_ISSET(qp
, SEQ_FUNCMAP
))
292 olen
= qp
->ilen
, len
= 0; olen
> 0; --olen
, ++p
)
293 len
+= ex_puts(sp
, (char *)KEY_NAME(sp
, *p
));
294 for (len
= STANDARD_TAB
- len
% STANDARD_TAB
; len
> 0;)
295 len
-= ex_puts(sp
, " ");
297 if (qp
->output
!= NULL
)
299 olen
= qp
->olen
, len
= 0; olen
> 0; --olen
, ++p
)
300 len
+= ex_puts(sp
, (char *)KEY_NAME(sp
, *p
));
304 if (isname
&& qp
->name
!= NULL
) {
305 for (len
= STANDARD_TAB
- len
% STANDARD_TAB
; len
> 0;)
306 len
-= ex_puts(sp
, " ");
308 olen
= qp
->nlen
; olen
> 0; --olen
, ++p
)
309 (void)ex_puts(sp
, (char *)KEY_NAME(sp
, *p
));
311 (void)ex_puts(sp
, "\n");
318 * Save the sequence entries to a file.
320 * PUBLIC: int seq_save __P((SCR *, FILE *, const char *, seq_t));
323 seq_save(SCR
*sp
, FILE *fp
, const char *prefix
, seq_t stype
)
330 /* Write a sequence command for all keys the user defined. */
331 LIST_FOREACH(qp
, &sp
->gp
->seqq
, q
) {
332 if (stype
!= qp
->stype
|| !F_ISSET(qp
, SEQ_USERDEF
))
335 (void)fprintf(fp
, "%s", prefix
);
336 for (p
= qp
->input
, olen
= qp
->ilen
; olen
> 0; --olen
) {
338 if (ch
== CH_LITERAL
|| ch
== '|' ||
339 ISBLANK(ch
) || KEY_VAL(sp
, ch
) == K_NL
)
340 (void)putc(CH_LITERAL
, fp
);
341 (void)fprintf(fp
, WC
, ch
);
344 if (qp
->output
!= NULL
)
346 olen
= qp
->olen
; olen
> 0; --olen
) {
348 if (ch
== CH_LITERAL
|| ch
== '|' ||
349 KEY_VAL(sp
, ch
) == K_NL
)
350 (void)putc(CH_LITERAL
, fp
);
351 (void)fprintf(fp
, WC
, ch
);
353 (void)putc('\n', fp
);
360 * Compare a string of EVENT's to a string of CHAR_T's.
362 * PUBLIC: int e_memcmp __P((CHAR_T *, EVENT *, size_t));
365 e_memcmp(CHAR_T
*p1
, EVENT
*ep
, size_t n
)
369 if (*p1
++ != ep
->e_c
)
370 return (*--p1
- ep
->e_c
);