xref: /netbsd-src/usr.bin/systat/keyboard.c (revision ef363e2dc91f016d50a3086bcc434f79f54034b0)
1 /*	$NetBSD: keyboard.c,v 1.26 2015/08/23 18:33:15 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1992, 1993
5  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)keyboard.c	8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: keyboard.c,v 1.26 2015/08/23 18:33:15 mrg Exp $");
38 #endif /* not lint */
39 
40 #include <sys/types.h>
41 
42 #include <ctype.h>
43 #include <signal.h>
44 #include <termios.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <signal.h>
48 
49 #include "systat.h"
50 #include "extern.h"
51 
52 extern sig_atomic_t needsredraw;
53 
54 void
keyboard(void)55 keyboard(void)
56 {
57 	int ch, rch, col;
58 	char *line;
59 	int i, linesz;
60 	static char help[] = "help";
61 	static char quit[] = "quit";
62 
63 	ch = 0;	/* XXX gcc */
64 	linesz = COLS - 2;		/* XXX does not get updated on SIGWINCH */
65 	if ((line = malloc(linesz)) == NULL) {
66 		error("malloc failed");
67 		die(0);
68 	}
69 
70 	for (;;) {
71 		col = 0;
72 		move(CMDLINE, 0);
73 
74 		while (col == 0 || (ch != '\r' && ch != '\n')) {
75 			refresh();
76 			for (;;) {
77 				ch = getch();
78 				if (!needsredraw)
79 					break;
80 				redraw();
81 			}
82 			if (ch == ERR) {
83 				display(SIGALRM);
84 				continue;
85 			}
86 			if (ch == KEY_RESIZE) {
87 				redraw();
88 				continue;
89 			}
90 			ch &= 0177;
91 			rch = ch;
92 			if (col == 0) {
93 				switch(ch) {
94 				    case '\n':
95 				    case '\r':
96 				    case ' ':
97 					clearerror();
98 					display(0);
99 					break;
100 				    case CTRL('l'):
101 					clearerror();
102 					wrefresh(curscr);
103 					break;
104 				    case CTRL('g'):
105 					status();
106 					break;
107 				    case '?':
108 				    case 'H':
109 				    case 'h':
110 					command(help);
111 					move(CMDLINE, 0);
112 					break;
113 				    case 'Q':
114 				    case 'q':
115 					command(quit);
116 					break;
117 				    case ':':
118 					move(CMDLINE, 0);
119 					clrtoeol();
120 					addch(':');
121 					col++;
122 					break;
123 				}
124 				continue;
125 			}
126 			if (ch == '\b' || ch == '\177' || ch == erasechar()) {
127 				if (col > 0)
128 					col--;
129 				goto doerase;
130 			}
131 			if (ch == CTRL('w') && col > 0) {
132 				while (--col >= 0 &&
133 				    isspace((unsigned char)line[col]))
134 					continue;
135 				col++;
136 				while (--col >= 0 &&
137 				    !isspace((unsigned char)line[col]))
138 					if (col == 0)
139 						break;
140 				col++;
141 				goto doerase;
142 			}
143 			if (ch == killchar() && col > 0) {
144 				col = 1;
145 		doerase:
146 				move(CMDLINE, col);
147 				clrtoeol();
148 				continue;
149 			}
150 			if (isprint(rch) || rch == ' ') {
151 				if (col < linesz) {
152 					line[col] = rch;
153 					mvaddch(CMDLINE, col, rch);
154 					col++;
155 				}
156 			}
157 		}
158 		line[col] = '\0';
159 		/* pass commands as lowercase */
160 		for (i = 1; i < col ; i++)
161 			line[i] = tolower((unsigned char)line[i]);
162 		command(line + 1);
163 	}
164 	/* NOTREACHED */
165 }
166