xref: /openbsd-src/sys/lib/libkern/getsn.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: getsn.c,v 1.4 2004/08/07 00:38:32 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Theo de Raadt
5  * All rights reserved.
6  *
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.
15  *
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.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/types.h>
30 #include <sys/systm.h>
31 #include <dev/cons.h>
32 
33 int
34 getsn(char *cp, int size)
35 {
36 	int len, c;
37 	char *lp;
38 
39 	lp = cp;
40 	len = 0;
41 	for (;;) {
42 		c = cngetc();
43 		switch (c) {
44 		case '\n':
45 		case '\r':
46 			printf("\n");
47 			*lp++ = '\0';
48 			return (len);
49 		case '\b':
50 		case '\177':
51 			if (len) {
52 				printf("\b \b");
53 				--lp;
54 				--len;
55 			}
56 			break;
57 		case 'u' & 037:
58 			while (len) {
59 				printf("\b \b");
60 				--lp;
61 				--len;
62 			}
63 			break;
64 		case '\t':
65 			c = ' ';
66 		default:
67 			if (len + 1 >= size || c < ' ') {
68 				printf("\007");
69 				break;
70 			}
71 			printf("%c", c);
72 			++len;
73 			*lp++ = c;
74 		}
75 	}
76 }
77