Annotation of src/usr.bin/systat/keyboard.c, Revision 1.18
1.18 ! deraadt 1: /* $OpenBSD: keyboard.c,v 1.17 2005/08/01 22:51:50 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: keyboard.c,v 1.2 1995/01/20 08:51:59 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1980, 1992, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.13 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: #if 0
35: static char sccsid[] = "@(#)keyboard.c 8.1 (Berkeley) 6/6/93";
36: #endif
1.18 ! deraadt 37: static char rcsid[] = "$OpenBSD: keyboard.c,v 1.17 2005/08/01 22:51:50 millert Exp $";
1.1 deraadt 38: #endif /* not lint */
39:
1.16 pvalchev 40: #include <sys/types.h>
1.1 deraadt 41: #include <ctype.h>
42: #include <signal.h>
1.14 david 43: #include <stdlib.h>
1.16 pvalchev 44: #include <unistd.h>
1.1 deraadt 45: #include <termios.h>
1.10 deraadt 46: #include <errno.h>
1.1 deraadt 47:
48: #include "systat.h"
49: #include "extern.h"
50:
1.4 kstailey 51: void
1.12 deraadt 52: keyboard(void)
1.1 deraadt 53: {
1.11 deraadt 54: char line[80];
1.7 millert 55: sigset_t mask, omask;
1.18 ! deraadt 56: int ch, col;
1.1 deraadt 57:
1.10 deraadt 58: for (;;) {
59: col = 0;
60: move(CMDLINE, 0);
61: do {
1.15 deraadt 62: if (gottstp) {
63: endwin();
1.17 millert 64: signal(SIGTSTP, SIG_DFL);
65: kill(getpid(), SIGTSTP);
66: signal(SIGTSTP, sigtstp);
67: siginterrupt(SIGTSTP, 1);
1.15 deraadt 68: gotwinch = 1;
69: gottstp = 0;
70: }
1.10 deraadt 71: if (gotdisplay) {
72: display();
73: gotdisplay = 0;
74: }
75: if (gotdie) {
76: die();
77: }
78: if (gotwinch) {
79: clearok(curscr, TRUE);
80: wrefresh(curscr);
81: gotwinch = 0;
82: }
83:
84: refresh();
85: if ((ch = getch()) == ERR) {
86: if (errno == EINTR)
87: continue;
1.6 millert 88: exit(1);
1.10 deraadt 89: }
90: ch &= 0177;
91: if (ch == 0177 && ferror(stdin)) {
92: clearerr(stdin);
93: continue;
94: }
95: if (ch >= 'A' && ch <= 'Z')
96: ch += 'a' - 'A';
97: if (col == 0) {
1.7 millert 98: switch (ch) {
99: case CTRL('l'):
100: case CTRL('g'):
101: sigemptyset(&mask);
102: sigaddset(&mask, SIGALRM);
103: sigprocmask(SIG_BLOCK, &mask, &omask);
104: if (ch == CTRL('l'))
105: wrefresh(curscr);
106: else
107: status();
108: sigprocmask(SIG_SETMASK, &omask, NULL);
1.10 deraadt 109: continue;
1.7 millert 110: case ':':
111: break;
112: default:
1.1 deraadt 113: continue;
114: }
1.10 deraadt 115: move(CMDLINE, 0);
116: clrtoeol();
117: }
118: if (ch == erasechar() && col > 0) {
119: if (col == 1 && line[0] == ':')
120: continue;
121: col--;
122: goto doerase;
123: }
124: if (ch == CTRL('w') && col > 0) {
125: while (--col >= 0 && isspace(line[col]))
126: ;
127: col++;
128: while (--col >= 0 && !isspace(line[col]))
129: if (col == 0 && line[0] == ':')
130: break;
131: col++;
132: goto doerase;
133: }
134: if (ch == killchar() && col > 0) {
135: col = 0;
136: if (line[0] == ':')
137: col++;
138: doerase:
139: move(CMDLINE, col);
140: clrtoeol();
141: continue;
142: }
1.5 aaron 143: if (col >= sizeof(line) - 1) {
1.3 deraadt 144: /* line too long */
145: beep();
146: continue;
147: }
1.10 deraadt 148: if (isprint(ch) || ch == ' ') {
149: line[col] = ch;
1.18 ! deraadt 150: mvaddch(CMDLINE, col, (chtype)ch);
1.10 deraadt 151: col++;
152: }
153: } while (col == 0 || (ch != '\r' && ch != '\n'));
154: line[col] = '\0';
1.7 millert 155: sigemptyset(&mask);
156: sigaddset(&mask, SIGALRM);
157: sigprocmask(SIG_BLOCK, &mask, &omask);
1.10 deraadt 158: command(line + 1);
1.7 millert 159: sigprocmask(SIG_SETMASK, &omask, NULL);
1.10 deraadt 160: }
1.1 deraadt 161: /*NOTREACHED*/
162: }