xref: /openbsd-src/sys/lib/libkern/getsn.c (revision 85f30cb46d0aa3a003f1c7fd776a16c8d1b72c4b)
1*85f30cb4Sdlg /*	$OpenBSD: getsn.c,v 1.7 2018/04/25 11:15:58 dlg Exp $	*/
271a6dad2Sderaadt 
371a6dad2Sderaadt /*
471a6dad2Sderaadt  * Copyright (c) 1996 Theo de Raadt
571a6dad2Sderaadt  * All rights reserved.
671a6dad2Sderaadt  *
771a6dad2Sderaadt  * Redistribution and use in source and binary forms, with or without
871a6dad2Sderaadt  * modification, are permitted provided that the following conditions
971a6dad2Sderaadt  * are met:
1071a6dad2Sderaadt  * 1. Redistributions of source code must retain the above copyright
1171a6dad2Sderaadt  *    notice, this list of conditions and the following disclaimer.
1271a6dad2Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
1371a6dad2Sderaadt  *    notice, this list of conditions and the following disclaimer in the
1471a6dad2Sderaadt  *    documentation and/or other materials provided with the distribution.
1571a6dad2Sderaadt  *
1671a6dad2Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1771a6dad2Sderaadt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1871a6dad2Sderaadt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1971a6dad2Sderaadt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2071a6dad2Sderaadt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2171a6dad2Sderaadt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2271a6dad2Sderaadt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2371a6dad2Sderaadt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2471a6dad2Sderaadt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2571a6dad2Sderaadt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2671a6dad2Sderaadt  */
2771a6dad2Sderaadt 
2805b5403fSniklas #include <sys/types.h>
2905b5403fSniklas #include <sys/systm.h>
3005b5403fSniklas #include <dev/cons.h>
3105b5403fSniklas 
32*85f30cb4Sdlg size_t
getsn(char * cp,size_t size)33*85f30cb4Sdlg getsn(char *cp, size_t size)
3471a6dad2Sderaadt {
35*85f30cb4Sdlg 	size_t len = 0;
36*85f30cb4Sdlg 	int  c;
370f2c9e3fSderaadt 	char *lp = cp;
3871a6dad2Sderaadt 
390f2c9e3fSderaadt 	while (1) {
4071a6dad2Sderaadt 		c = cngetc();
4171a6dad2Sderaadt 		switch (c) {
4271a6dad2Sderaadt 		case '\n':
4371a6dad2Sderaadt 		case '\r':
4471a6dad2Sderaadt 			printf("\n");
4571a6dad2Sderaadt 			*lp++ = '\0';
4671a6dad2Sderaadt 			return (len);
4771a6dad2Sderaadt 		case '\b':
4871a6dad2Sderaadt 		case '\177':
4971a6dad2Sderaadt 			if (len) {
5071a6dad2Sderaadt 				printf("\b \b");
5171a6dad2Sderaadt 				--lp;
5271a6dad2Sderaadt 				--len;
5371a6dad2Sderaadt 			}
5471a6dad2Sderaadt 			break;
5571a6dad2Sderaadt 		case 'u' & 037:
5671a6dad2Sderaadt 			while (len) {
5771a6dad2Sderaadt 				printf("\b \b");
5871a6dad2Sderaadt 				--lp;
5971a6dad2Sderaadt 				--len;
6071a6dad2Sderaadt 			}
6171a6dad2Sderaadt 			break;
6271a6dad2Sderaadt 		case '\t':
6371a6dad2Sderaadt 			c = ' ';
6471a6dad2Sderaadt 		default:
6571a6dad2Sderaadt 			if (len + 1 >= size || c < ' ') {
6671a6dad2Sderaadt 				printf("\007");
6771a6dad2Sderaadt 				break;
6871a6dad2Sderaadt 			}
6971a6dad2Sderaadt 			printf("%c", c);
7071a6dad2Sderaadt 			++len;
7171a6dad2Sderaadt 			*lp++ = c;
7271a6dad2Sderaadt 		}
7371a6dad2Sderaadt 	}
7471a6dad2Sderaadt }
75