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