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