]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/systat/keyboard.c
Merge r226396, r240605
[FreeBSD/stable/8.git] / usr.bin / systat / keyboard.c
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35
36 __FBSDID("$FreeBSD$");
37
38 #ifdef lint
39 static const char sccsid[] = "@(#)keyboard.c    8.1 (Berkeley) 6/6/93";
40 #endif
41
42 #include <sys/select.h>
43 #include <sys/time.h>
44
45 #include <errno.h>
46 #include <ctype.h>
47 #include <stdlib.h>
48 #include <termios.h>
49 #include <unistd.h>
50
51 #include "systat.h"
52 #include "extern.h"
53
54 static char line[80];
55 static int keyboard_dispatch(int ch);
56
57 int
58 keyboard(void)
59 {
60         char line[80];
61         int ch, n;
62         struct timeval last, intvl, now, tm;
63         fd_set rfds;
64
65         /* Set initial timings */
66         gettimeofday(&last, NULL);
67         intvl.tv_sec = delay / 1000000;
68         intvl.tv_usec = delay % 1000000;
69         for (;;) {
70                 col = 0;
71                 move(CMDLINE, 0);
72                 for (;;) {
73                         /* Determine interval to sleep */
74                         (void)gettimeofday(&now, NULL);
75                         tm.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
76                         tm.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
77                         while (tm.tv_usec < 0) {
78                                 tm.tv_usec += 1000000;
79                                 tm.tv_sec--;
80                         }
81                         while (tm.tv_usec >= 1000000) {
82                                 tm.tv_usec -= 1000000;
83                                 tm.tv_sec++;
84                         }
85                         if (tm.tv_sec < 0) {
86                                 /* We have to update screen immediately */
87                                 display();
88                                 gettimeofday(&last, NULL);
89                                 continue;
90                         }
91
92                         /* Prepare select  */
93                         FD_ZERO(&rfds);
94                         FD_SET(STDIN_FILENO, &rfds);
95                         n = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tm);
96
97                         if (n > 0) {
98                                 /* Read event on stdin */
99                                 ch = getch();
100
101                                 if (keyboard_dispatch(ch) == 0) {
102                                         refresh();
103                                         continue;
104                                 }
105         
106                                 line[col] = '\0';
107                                 command(line + 1);
108                                 /* Refresh delay */
109                                 intvl.tv_sec = delay / 1000000;
110                                 intvl.tv_usec = delay % 1000000;
111                                 refresh();
112                                 break;
113                         }
114
115                         if (n < 0 && errno != EINTR)
116                                 exit(1);
117
118                         /* Timeout or signal. Call display another time */
119                         display();
120                         gettimeofday(&last, NULL);
121                 }
122         }
123 }
124
125 static int
126 keyboard_dispatch(int ch)
127 {
128
129         if (ch == ERR) {
130                 if (errno == EINTR)
131                         return 0;
132                 exit(1);
133         }
134         if (ch >= 'A' && ch <= 'Z')
135                 ch += 'a' - 'A';
136         if (col == 0) {
137                 if (ch == CTRL('l')) {
138                         wrefresh(curscr);
139                         return 0;
140                 }
141                 if (ch == CTRL('g')) {
142                         status();
143                         return 0;
144                 }
145                 if (ch != ':')
146                         return 0;
147                 move(CMDLINE, 0);
148                 clrtoeol();
149         }
150         if (ch == erasechar() && col > 0) {
151                 if (col == 1 && line[0] == ':')
152                         return 0;
153                 col--;
154                 goto doerase;
155         }
156         if (ch == CTRL('w') && col > 0) {
157                 while (--col >= 0 && isspace(line[col]))
158                         ;
159                 col++;
160                 while (--col >= 0 && !isspace(line[col]))
161                         if (col == 0 && line[0] == ':')
162                                 return 1;
163                 col++;
164                 goto doerase;
165         }
166         if (ch == killchar() && col > 0) {
167                 col = 0;
168                 if (line[0] == ':')
169                         col++;
170 doerase:
171                 move(CMDLINE, col);
172                 clrtoeol();
173                 return 0;
174         }
175         if (isprint(ch) || ch == ' ') {
176                 line[col] = ch;
177                 mvaddch(CMDLINE, col, ch);
178                 col++;
179         }
180
181         if (col == 0 || (ch != '\r' && ch != '\n'))
182                 return 0;
183
184         return 1;
185 }