xref: /freebsd/usr.bin/systat/keyboard.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 
34 __FBSDID("$FreeBSD$");
35 
36 #ifdef lint
37 static const char sccsid[] = "@(#)keyboard.c	8.1 (Berkeley) 6/6/93";
38 #endif
39 
40 #include <sys/select.h>
41 #include <sys/time.h>
42 
43 #include <errno.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 #include "systat.h"
50 #include "extern.h"
51 
52 static char line[80];
53 static int keyboard_dispatch(int ch);
54 
55 int
56 keyboard(void)
57 {
58 	int ch, n;
59 	struct timeval last, intvl, now, tm;
60 	fd_set rfds;
61 
62 	/* Set initial timings */
63 	gettimeofday(&last, NULL);
64 	intvl.tv_sec = delay / 1000000;
65 	intvl.tv_usec = delay % 1000000;
66 	for (;;) {
67 		col = 0;
68 		move(CMDLINE, 0);
69 		for (;;) {
70 			/* Determine interval to sleep */
71 			(void)gettimeofday(&now, NULL);
72 			tm.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
73 			tm.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
74 			while (tm.tv_usec < 0) {
75 				tm.tv_usec += 1000000;
76 				tm.tv_sec--;
77 			}
78 			while (tm.tv_usec >= 1000000) {
79 				tm.tv_usec -= 1000000;
80 				tm.tv_sec++;
81 			}
82 			if (tm.tv_sec < 0) {
83 				/* We have to update screen immediately */
84 				display();
85 				gettimeofday(&last, NULL);
86 				continue;
87 			}
88 
89 			/* Prepare select  */
90 			FD_ZERO(&rfds);
91 			FD_SET(STDIN_FILENO, &rfds);
92 			n = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tm);
93 
94 			if (n > 0) {
95 				/* Read event on stdin */
96 				ch = getch();
97 
98 				if (keyboard_dispatch(ch) == 0) {
99 					refresh();
100 					continue;
101 				}
102 
103 				line[col] = '\0';
104 				command(line + 1);
105 				/* Refresh delay */
106 				intvl.tv_sec = delay / 1000000;
107 				intvl.tv_usec = delay % 1000000;
108 				refresh();
109 				break;
110 			}
111 
112 			if (n < 0 && errno != EINTR)
113 				exit(1);
114 
115 			/* Timeout or signal. Call display another time */
116 			display();
117 			gettimeofday(&last, NULL);
118 		}
119 	}
120 }
121 
122 static int
123 keyboard_dispatch(int ch)
124 {
125 
126 	if (ch == ERR) {
127 		if (errno == EINTR)
128 			return 0;
129 		exit(1);
130 	}
131 	if (ch >= 'A' && ch <= 'Z')
132 		ch += 'a' - 'A';
133 	if (col == 0) {
134 		if (ch == CTRL('l')) {
135 			wrefresh(curscr);
136 			return 0;
137 		}
138 		if (ch == CTRL('g')) {
139 			status();
140 			return 0;
141 		}
142 		if (ch != ':')
143 			return 0;
144 		move(CMDLINE, 0);
145 		clrtoeol();
146 	}
147 	if (ch == erasechar() && col > 0) {
148 		if (col == 1 && line[0] == ':')
149 			return 0;
150 		col--;
151 		goto doerase;
152 	}
153 	if (ch == CTRL('w') && col > 0) {
154 		while (--col >= 0 && isspace(line[col]))
155 			;
156 		col++;
157 		while (--col >= 0 && !isspace(line[col]))
158 			if (col == 0 && line[0] == ':')
159 				return 1;
160 		col++;
161 		goto doerase;
162 	}
163 	if (ch == killchar() && col > 0) {
164 		col = 0;
165 		if (line[0] == ':')
166 			col++;
167 doerase:
168 		move(CMDLINE, col);
169 		clrtoeol();
170 		return 0;
171 	}
172 	if (isprint(ch) || ch == ' ') {
173 		line[col] = ch;
174 		mvaddch(CMDLINE, col, ch);
175 		col++;
176 	}
177 
178 	if (col == 0 || (ch != '\r' && ch != '\n'))
179 		return 0;
180 
181 	return 1;
182 }
183