xref: /freebsd/usr.bin/systat/keyboard.c (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1 /*-
2  * Copyright (c) 1980, 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD$");
33 
34 #ifdef lint
35 static const char sccsid[] = "@(#)keyboard.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 
38 #include <errno.h>
39 #include <ctype.h>
40 #include <signal.h>
41 #include <stdlib.h>
42 #include <termios.h>
43 
44 #include "systat.h"
45 #include "extern.h"
46 
47 int
48 keyboard(void)
49 {
50 	char line[80];
51 	int ch, oldmask;
52 
53 	for (;;) {
54 		col = 0;
55 		move(CMDLINE, 0);
56 		do {
57 			refresh();
58 			ch = getch();
59 			if (ch == ERR) {
60 				if (errno == EINTR)
61 					continue;
62 				exit(1);
63 			}
64 			if (ch >= 'A' && ch <= 'Z')
65 				ch += 'a' - 'A';
66 			if (col == 0) {
67 #define	mask(s)	(1 << ((s) - 1))
68 				if (ch == CTRL('l')) {
69 					oldmask = sigblock(mask(SIGALRM));
70 					wrefresh(curscr);
71 					sigsetmask(oldmask);
72 					continue;
73 				}
74 				if (ch == CTRL('g')) {
75 					oldmask = sigblock(mask(SIGALRM));
76 					status();
77 					sigsetmask(oldmask);
78 					continue;
79 				}
80 				if (ch != ':')
81 					continue;
82 				move(CMDLINE, 0);
83 				clrtoeol();
84 			}
85 			if (ch == erasechar() && col > 0) {
86 				if (col == 1 && line[0] == ':')
87 					continue;
88 				col--;
89 				goto doerase;
90 			}
91 			if (ch == CTRL('w') && col > 0) {
92 				while (--col >= 0 && isspace(line[col]))
93 					;
94 				col++;
95 				while (--col >= 0 && !isspace(line[col]))
96 					if (col == 0 && line[0] == ':')
97 						break;
98 				col++;
99 				goto doerase;
100 			}
101 			if (ch == killchar() && col > 0) {
102 				col = 0;
103 				if (line[0] == ':')
104 					col++;
105 		doerase:
106 				move(CMDLINE, col);
107 				clrtoeol();
108 				continue;
109 			}
110 			if (isprint(ch) || ch == ' ') {
111 				line[col] = ch;
112 				mvaddch(CMDLINE, col, ch);
113 				col++;
114 			}
115 		} while (col == 0 || (ch != '\r' && ch != '\n'));
116 		line[col] = '\0';
117 		oldmask = sigblock(mask(SIGALRM));
118 		command(line + 1);
119 		sigsetmask(oldmask);
120 	}
121 	/*NOTREACHED*/
122 }
123