No empty .Rs/.Re
[netbsd-mini2440.git] / sys / kern / cnmagic.c
blobd0361bb8e1d3c27359aacc587b2597ec35a9aeae
1 /* $NetBSD: cnmagic.c,v 1.9 2006/11/01 10:17:58 yamt Exp $ */
3 /*
4 * Copyright (c) 2000 Eduardo Horvath
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: cnmagic.c,v 1.9 2006/11/01 10:17:58 yamt Exp $");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #define ENCODE_STATE(c, n) (short)(((c)&0x1ff)|(((n)&0x7f)<<9))
37 static unsigned short cn_magic[CNS_LEN];
40 * Initialize a cnm_state_t.
42 void
43 cn_init_magic(cnm_state_t *cnm)
45 cnm->cnm_state = 0;
46 cnm->cnm_magic = cn_magic;
50 * Destroy a cnm_state_t.
52 void
53 cn_destroy_magic(cnm_state_t *cnm)
55 cnm->cnm_state = 0;
56 cnm->cnm_magic = NULL;
60 * Translate a magic string to a state
61 * machine table.
63 int
64 cn_set_magic(const char *magic)
66 unsigned int i, c, n;
67 unsigned short m[CNS_LEN];
69 for (i = 0; i < CNS_LEN; i++) {
70 c = (*magic++) & 0xff;
71 n = *magic ? i+1 : CNS_TERM;
72 switch (c) {
73 case 0:
74 /* End of string */
75 if (i == 0) {
76 /* empty string? */
77 cn_magic[0] = 0;
78 #ifdef DEBUG
79 printf("cn_set_magic(): empty!\n");
80 #endif
81 return (0);
83 do {
84 cn_magic[i] = m[i];
85 } while (i--);
86 return(0);
87 case 0x27:
88 /* Escape sequence */
89 c = (*magic++) & 0xff;
90 n = *magic ? i+1 : CNS_TERM;
91 switch (c) {
92 case 0x27:
93 break;
94 case 0x01:
95 /* BREAK */
96 c = CNC_BREAK;
97 break;
98 case 0x02:
99 /* NUL */
100 c = 0;
101 break;
103 /* FALLTHROUGH */
104 default:
105 /* Transition to the next state. */
106 #ifdef DEBUG
107 if (!cold)
108 printf("mag %d %x:%x\n", i, c, n);
109 #endif
110 m[i] = ENCODE_STATE(c, n);
111 break;
114 return (EINVAL);
118 * Translatea state machine table back to
119 * a magic string.
122 cn_get_magic(char *magic, size_t maglen)
124 size_t i, c;
126 for (i = 0; i < CNS_LEN;) {
127 c = cn_magic[i];
128 /* Translate a character */
129 switch (CNS_MAGIC_VAL(c)) {
130 case CNC_BREAK:
131 *magic++ = 0x27;
132 *magic++ = 0x01;
133 break;
134 case 0:
135 *magic++ = 0x27;
136 *magic++ = 0x02;
137 break;
138 case 0x27:
139 *magic++ = 0x27;
140 *magic++ = 0x27;
141 break;
142 default:
143 *magic++ = (c & 0x0ff);
144 break;
146 /* Now go to the next state */
147 i = CNS_MAGIC_NEXT(c);
148 if (i == CNS_TERM || i == 0) {
149 /* Either termination state or empty machine */
150 *magic++ = 0;
151 return (0);
154 return (EINVAL);