xref: /openbsd-src/sys/lib/libkern/getsn.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: getsn.c,v 1.2 1996/08/23 19:22:43 niklas 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Theo de Raadt.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <dev/cons.h>
37 
38 int
39 getsn(cp, size)
40 	char *cp;
41 	int size;
42 {
43 	register char *lp;
44 	register int len;
45 	register int c;
46 
47 	lp = cp;
48 	len = 0;
49 	for (;;) {
50 		c = cngetc();
51 		switch (c) {
52 		case '\n':
53 		case '\r':
54 			printf("\n");
55 			*lp++ = '\0';
56 			return (len);
57 		case '\b':
58 		case '\177':
59 			if (len) {
60 				printf("\b \b");
61 				--lp;
62 				--len;
63 			}
64 			break;
65 		case 'u' & 037:
66 			while (len) {
67 				printf("\b \b");
68 				--lp;
69 				--len;
70 			}
71 			break;
72 		case '\t':
73 			c = ' ';
74 		default:
75 			if (len + 1 >= size || c < ' ') {
76 				printf("\007");
77 				break;
78 			}
79 			printf("%c", c);
80 			++len;
81 			*lp++ = c;
82 		}
83 	}
84 }
85