]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/systat/keyboard.c
Fix merge of r240605 (done in r243684).
[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         int ch, n;
61         struct timeval last, intvl, now, tm;
62         fd_set rfds;
63
64         /* Set initial timings */
65         gettimeofday(&last, NULL);
66         intvl.tv_sec = delay / 1000000;
67         intvl.tv_usec = delay % 1000000;
68         for (;;) {
69                 col = 0;
70                 move(CMDLINE, 0);
71                 for (;;) {
72                         /* Determine interval to sleep */
73                         (void)gettimeofday(&now, NULL);
74                         tm.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
75                         tm.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
76                         while (tm.tv_usec < 0) {
77                                 tm.tv_usec += 1000000;
78                                 tm.tv_sec--;
79                         }
80                         while (tm.tv_usec >= 1000000) {
81                                 tm.tv_usec -= 1000000;
82                                 tm.tv_sec++;
83                         }
84                         if (tm.tv_sec < 0) {
85                                 /* We have to update screen immediately */
86                                 display();
87                                 gettimeofday(&last, NULL);
88                                 continue;
89                         }
90
91                         /* Prepare select  */
92                         FD_ZERO(&rfds);
93                         FD_SET(STDIN_FILENO, &rfds);
94                         n = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tm);
95
96                         if (n > 0) {
97                                 /* Read event on stdin */
98                                 ch = getch();
99
100                                 if (keyboard_dispatch(ch) == 0) {
101                                         refresh();
102                                         continue;
103                                 }
104         
105                                 line[col] = '\0';
106                                 command(line + 1);
107                                 /* Refresh delay */
108                                 intvl.tv_sec = delay / 1000000;
109                                 intvl.tv_usec = delay % 1000000;
110                                 refresh();
111                                 break;
112                         }
113
114                         if (n < 0 && errno != EINTR)
115                                 exit(1);
116
117                         /* Timeout or signal. Call display another time */
118                         display();
119                         gettimeofday(&last, NULL);
120                 }
121         }
122 }
123
124 static int
125 keyboard_dispatch(int ch)
126 {
127
128         if (ch == ERR) {
129                 if (errno == EINTR)
130                         return 0;
131                 exit(1);
132         }
133         if (ch >= 'A' && ch <= 'Z')
134                 ch += 'a' - 'A';
135         if (col == 0) {
136                 if (ch == CTRL('l')) {
137                         wrefresh(curscr);
138                         return 0;
139                 }
140                 if (ch == CTRL('g')) {
141                         status();
142                         return 0;
143                 }
144                 if (ch != ':')
145                         return 0;
146                 move(CMDLINE, 0);
147                 clrtoeol();
148         }
149         if (ch == erasechar() && col > 0) {
150                 if (col == 1 && line[0] == ':')
151                         return 0;
152                 col--;
153                 goto doerase;
154         }
155         if (ch == CTRL('w') && col > 0) {
156                 while (--col >= 0 && isspace(line[col]))
157                         ;
158                 col++;
159                 while (--col >= 0 && !isspace(line[col]))
160                         if (col == 0 && line[0] == ':')
161                                 return 1;
162                 col++;
163                 goto doerase;
164         }
165         if (ch == killchar() && col > 0) {
166                 col = 0;
167                 if (line[0] == ':')
168                         col++;
169 doerase:
170                 move(CMDLINE, col);
171                 clrtoeol();
172                 return 0;
173         }
174         if (isprint(ch) || ch == ' ') {
175                 line[col] = ch;
176                 mvaddch(CMDLINE, col, ch);
177                 col++;
178         }
179
180         if (col == 0 || (ch != '\r' && ch != '\n'))
181                 return 0;
182
183         return 1;
184 }