tty_subr.c revision 1.20.2.3 1 /* $NetBSD: tty_subr.c,v 1.20.2.3 2003/01/15 18:58:31 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Theo de Raadt
5 * All rights reserved.
6 *
7 * Per Lindqvist <pgd (at) compuram.bbt.se> supplied an almost fully working
8 * set of true clist functions that this is very loosely based on.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Theo de Raadt.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: tty_subr.c,v 1.20.2.3 2003/01/15 18:58:31 thorpej Exp $");
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/buf.h>
42 #include <sys/ioctl.h>
43 #include <sys/tty.h>
44 #include <sys/malloc.h>
45
46 /*
47 * At compile time, choose:
48 * There are two ways the TTY_QUOTE bit can be stored. If QBITS is
49 * defined we allocate an array of bits -- 1/8th as much memory but
50 * setbit(), clrbit(), and isset() take more cpu. If QBITS is
51 * undefined, we just use an array of bytes.
52 *
53 * If TTY_QUOTE functionality isn't required by a line discipline,
54 * it can free c_cq and set it to NULL. This speeds things up,
55 * and also does not use any extra memory. This is useful for (say)
56 * a SLIP line discipline that wants a 32K ring buffer for data
57 * but doesn't need quoting.
58 */
59 #define QBITS
60
61 #ifdef QBITS
62 #define QMEM(n) ((((n)-1)/NBBY)+1)
63 #else
64 #define QMEM(n) (n)
65 #endif
66
67 #ifdef QBITS
68 void clrbits __P((u_char *, int, int));
69 #endif
70
71 /*
72 * Initialize a particular clist. Ok, they are really ring buffers,
73 * of the specified length, with/without quoting support.
74 */
75 int
76 clalloc(clp, size, quot)
77 struct clist *clp;
78 int size;
79 int quot;
80 {
81
82 clp->c_cs = malloc(size, M_TTYS, M_WAITOK);
83 if (!clp->c_cs)
84 return (-1);
85 memset(clp->c_cs, 0, size);
86
87 if(quot) {
88 clp->c_cq = malloc(QMEM(size), M_TTYS, M_WAITOK);
89 if (!clp->c_cq) {
90 free(clp->c_cs, M_TTYS);
91 return (-1);
92 }
93 memset(clp->c_cq, 0, QMEM(size));
94 } else
95 clp->c_cq = (u_char *)0;
96
97 clp->c_cf = clp->c_cl = (u_char *)0;
98 clp->c_ce = clp->c_cs + size;
99 clp->c_cn = size;
100 clp->c_cc = 0;
101 return (0);
102 }
103
104 void
105 clfree(clp)
106 struct clist *clp;
107 {
108 if(clp->c_cs)
109 free(clp->c_cs, M_TTYS);
110 if(clp->c_cq)
111 free(clp->c_cq, M_TTYS);
112 clp->c_cs = clp->c_cq = (u_char *)0;
113 }
114
115
116 /*
117 * Get a character from a clist.
118 */
119 int
120 getc(clp)
121 struct clist *clp;
122 {
123 int c = -1;
124 int s;
125
126 s = spltty();
127 if (clp->c_cc == 0)
128 goto out;
129
130 c = *clp->c_cf & 0xff;
131 if (clp->c_cq) {
132 #ifdef QBITS
133 if (isset(clp->c_cq, clp->c_cf - clp->c_cs) )
134 c |= TTY_QUOTE;
135 #else
136 if (*(clp->c_cf - clp->c_cs + clp->c_cq))
137 c |= TTY_QUOTE;
138 #endif
139 }
140 if (++clp->c_cf == clp->c_ce)
141 clp->c_cf = clp->c_cs;
142 if (--clp->c_cc == 0)
143 clp->c_cf = clp->c_cl = (u_char *)0;
144 out:
145 splx(s);
146 return c;
147 }
148
149 /*
150 * Copy clist to buffer.
151 * Return number of bytes moved.
152 */
153 int
154 q_to_b(clp, cp, count)
155 struct clist *clp;
156 u_char *cp;
157 int count;
158 {
159 int cc;
160 u_char *p = cp;
161 int s;
162
163 s = spltty();
164 /* optimize this while loop */
165 while (count > 0 && clp->c_cc > 0) {
166 cc = clp->c_cl - clp->c_cf;
167 if (clp->c_cf >= clp->c_cl)
168 cc = clp->c_ce - clp->c_cf;
169 if (cc > count)
170 cc = count;
171 memcpy(p, clp->c_cf, cc);
172 count -= cc;
173 p += cc;
174 clp->c_cc -= cc;
175 clp->c_cf += cc;
176 if (clp->c_cf == clp->c_ce)
177 clp->c_cf = clp->c_cs;
178 }
179 if (clp->c_cc == 0)
180 clp->c_cf = clp->c_cl = (u_char *)0;
181 splx(s);
182 return p - cp;
183 }
184
185 /*
186 * Return count of contiguous characters in clist.
187 * Stop counting if flag&character is non-null.
188 */
189 int
190 ndqb(clp, flag)
191 struct clist *clp;
192 int flag;
193 {
194 int count = 0;
195 int i;
196 int cc;
197 int s;
198
199 s = spltty();
200 if ((cc = clp->c_cc) == 0)
201 goto out;
202
203 if (flag == 0) {
204 count = clp->c_cl - clp->c_cf;
205 if (count <= 0)
206 count = clp->c_ce - clp->c_cf;
207 goto out;
208 }
209
210 i = clp->c_cf - clp->c_cs;
211 if (flag & TTY_QUOTE) {
212 while (cc-- > 0 && !(clp->c_cs[i++] & (flag & ~TTY_QUOTE) ||
213 isset(clp->c_cq, i))) {
214 count++;
215 if (i == clp->c_cn)
216 break;
217 }
218 } else {
219 while (cc-- > 0 && !(clp->c_cs[i++] & flag)) {
220 count++;
221 if (i == clp->c_cn)
222 break;
223 }
224 }
225 out:
226 splx(s);
227 return count;
228 }
229
230 /*
231 * Flush count bytes from clist.
232 */
233 void
234 ndflush(clp, count)
235 struct clist *clp;
236 int count;
237 {
238 int cc;
239 int s;
240
241 s = spltty();
242 if (count == clp->c_cc) {
243 clp->c_cc = 0;
244 clp->c_cf = clp->c_cl = (u_char *)0;
245 goto out;
246 }
247 /* optimize this while loop */
248 while (count > 0 && clp->c_cc > 0) {
249 cc = clp->c_cl - clp->c_cf;
250 if (clp->c_cf >= clp->c_cl)
251 cc = clp->c_ce - clp->c_cf;
252 if (cc > count)
253 cc = count;
254 count -= cc;
255 clp->c_cc -= cc;
256 clp->c_cf += cc;
257 if (clp->c_cf == clp->c_ce)
258 clp->c_cf = clp->c_cs;
259 }
260 if (clp->c_cc == 0)
261 clp->c_cf = clp->c_cl = (u_char *)0;
262 out:
263 splx(s);
264 }
265
266 /*
267 * Put a character into the output queue.
268 */
269 int
270 putc(c, clp)
271 int c;
272 struct clist *clp;
273 {
274 int i;
275 int s;
276
277 s = spltty();
278 if (clp->c_cc == clp->c_cn)
279 goto out;
280
281 if (clp->c_cc == 0) {
282 if (!clp->c_cs) {
283 #if defined(DIAGNOSTIC) || 1
284 printf("putc: required clalloc\n");
285 #endif
286 if(clalloc(clp, 1024, 1)) {
287 out:
288 splx(s);
289 return -1;
290 }
291 }
292 clp->c_cf = clp->c_cl = clp->c_cs;
293 }
294
295 *clp->c_cl = c & 0xff;
296 i = clp->c_cl - clp->c_cs;
297 if (clp->c_cq) {
298 #ifdef QBITS
299 if (c & TTY_QUOTE)
300 setbit(clp->c_cq, i);
301 else
302 clrbit(clp->c_cq, i);
303 #else
304 q = clp->c_cq + i;
305 *q = (c & TTY_QUOTE) ? 1 : 0;
306 #endif
307 }
308 clp->c_cc++;
309 clp->c_cl++;
310 if (clp->c_cl == clp->c_ce)
311 clp->c_cl = clp->c_cs;
312 splx(s);
313 return 0;
314 }
315
316 #ifdef QBITS
317 /*
318 * optimized version of
319 *
320 * for (i = 0; i < len; i++)
321 * clrbit(cp, off + len);
322 */
323 void
324 clrbits(cp, off, len)
325 u_char *cp;
326 int off;
327 int len;
328 {
329 int sby, sbi, eby, ebi;
330 int i;
331 u_char mask;
332
333 if(len==1) {
334 clrbit(cp, off);
335 return;
336 }
337
338 sby = off / NBBY;
339 sbi = off % NBBY;
340 eby = (off+len) / NBBY;
341 ebi = (off+len) % NBBY;
342 if (sby == eby) {
343 mask = ((1 << (ebi - sbi)) - 1) << sbi;
344 cp[sby] &= ~mask;
345 } else {
346 mask = (1<<sbi) - 1;
347 cp[sby++] &= mask;
348
349 mask = (1<<ebi) - 1;
350 cp[eby] &= ~mask;
351
352 for (i = sby; i < eby; i++)
353 cp[i] = 0x00;
354 }
355 }
356 #endif
357
358 /*
359 * Copy buffer to clist.
360 * Return number of bytes not transfered.
361 */
362 int
363 b_to_q(cp, count, clp)
364 const u_char *cp;
365 int count;
366 struct clist *clp;
367 {
368 int cc;
369 const u_char *p = cp;
370 int s;
371
372 if (count <= 0)
373 return 0;
374
375 s = spltty();
376 if (clp->c_cc == clp->c_cn)
377 goto out;
378
379 if (clp->c_cc == 0) {
380 if (!clp->c_cs) {
381 #if defined(DIAGNOSTIC) || 1
382 printf("b_to_q: required clalloc\n");
383 #endif
384 if(clalloc(clp, 1024, 1))
385 goto out;
386 }
387 clp->c_cf = clp->c_cl = clp->c_cs;
388 }
389
390 /* optimize this while loop */
391 while (count > 0 && clp->c_cc < clp->c_cn) {
392 cc = clp->c_ce - clp->c_cl;
393 if (clp->c_cf > clp->c_cl)
394 cc = clp->c_cf - clp->c_cl;
395 if (cc > count)
396 cc = count;
397 memcpy(clp->c_cl, p, cc);
398 if (clp->c_cq) {
399 #ifdef QBITS
400 clrbits(clp->c_cq, clp->c_cl - clp->c_cs, cc);
401 #else
402 memset(clp->c_cl - clp->c_cs + clp->c_cq, 0, cc);
403 #endif
404 }
405 p += cc;
406 count -= cc;
407 clp->c_cc += cc;
408 clp->c_cl += cc;
409 if (clp->c_cl == clp->c_ce)
410 clp->c_cl = clp->c_cs;
411 }
412 out:
413 splx(s);
414 return count;
415 }
416
417 static int cc;
418
419 /*
420 * Given a non-NULL pointer into the clist return the pointer
421 * to the next character in the list or return NULL if no more chars.
422 *
423 * Callers must not allow getc's to happen between firstc's and getc's
424 * so that the pointer becomes invalid. Note that interrupts are NOT
425 * masked.
426 */
427 u_char *
428 nextc(clp, cp, c)
429 struct clist *clp;
430 u_char *cp;
431 int *c;
432 {
433
434 if (clp->c_cf == cp) {
435 /*
436 * First time initialization.
437 */
438 cc = clp->c_cc;
439 }
440 if (cc == 0 || cp == NULL)
441 return NULL;
442 if (--cc == 0)
443 return NULL;
444 if (++cp == clp->c_ce)
445 cp = clp->c_cs;
446 *c = *cp & 0xff;
447 if (clp->c_cq) {
448 #ifdef QBITS
449 if (isset(clp->c_cq, cp - clp->c_cs))
450 *c |= TTY_QUOTE;
451 #else
452 if (*(clp->c_cf - clp->c_cs + clp->c_cq))
453 *c |= TTY_QUOTE;
454 #endif
455 }
456 return cp;
457 }
458
459 /*
460 * Given a non-NULL pointer into the clist return the pointer
461 * to the first character in the list or return NULL if no more chars.
462 *
463 * Callers must not allow getc's to happen between firstc's and getc's
464 * so that the pointer becomes invalid. Note that interrupts are NOT
465 * masked.
466 *
467 * *c is set to the NEXT character
468 */
469 u_char *
470 firstc(clp, c)
471 struct clist *clp;
472 int *c;
473 {
474 u_char *cp;
475
476 cc = clp->c_cc;
477 if (cc == 0)
478 return NULL;
479 cp = clp->c_cf;
480 *c = *cp & 0xff;
481 if(clp->c_cq) {
482 #ifdef QBITS
483 if (isset(clp->c_cq, cp - clp->c_cs))
484 *c |= TTY_QUOTE;
485 #else
486 if (*(cp - clp->c_cs + clp->c_cq))
487 *c |= TTY_QUOTE;
488 #endif
489 }
490 return clp->c_cf;
491 }
492
493 /*
494 * Remove the last character in the clist and return it.
495 */
496 int
497 unputc(clp)
498 struct clist *clp;
499 {
500 unsigned int c = -1;
501 int s;
502
503 s = spltty();
504 if (clp->c_cc == 0)
505 goto out;
506
507 if (clp->c_cl == clp->c_cs)
508 clp->c_cl = clp->c_ce - 1;
509 else
510 --clp->c_cl;
511 clp->c_cc--;
512
513 c = *clp->c_cl & 0xff;
514 if (clp->c_cq) {
515 #ifdef QBITS
516 if (isset(clp->c_cq, clp->c_cl - clp->c_cs))
517 c |= TTY_QUOTE;
518 #else
519 if (*(clp->c_cf - clp->c_cs + clp->c_cq))
520 c |= TTY_QUOTE;
521 #endif
522 }
523 if (clp->c_cc == 0)
524 clp->c_cf = clp->c_cl = (u_char *)0;
525 out:
526 splx(s);
527 return c;
528 }
529
530 /*
531 * Put the chars in the from queue on the end of the to queue.
532 */
533 void
534 catq(from, to)
535 struct clist *from, *to;
536 {
537 int c;
538
539 while ((c = getc(from)) != -1)
540 putc(c, to);
541 }
542