Home | History | Annotate | Line # | Download | only in systat
keyboard.c revision 1.7
      1 /*	$NetBSD: keyboard.c,v 1.7 1999/11/15 06:16:56 simonb Exp $	*/
      2 
      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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)keyboard.c	8.1 (Berkeley) 6/6/93";
     40 #endif
     41 __RCSID("$NetBSD: keyboard.c,v 1.7 1999/11/15 06:16:56 simonb Exp $");
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 
     46 #include <ctype.h>
     47 #include <curses.h>
     48 #include <signal.h>
     49 #include <termios.h>
     50 #include <stdlib.h>
     51 
     52 #include "systat.h"
     53 #include "extern.h"
     54 
     55 int
     56 keyboard()
     57 {
     58 	char ch, rch, *line;
     59 	int i, linesz;
     60 	sigset_t set;
     61 
     62 	sigemptyset(&set);
     63 	sigaddset(&set, SIGALRM);
     64 
     65 	linesz = COLS - 2;		/* XXX does not get updated on SIGWINCH */
     66 	line = malloc(linesz);
     67 	if (line == NULL) {
     68 		error("malloc");
     69 		exit(1);
     70 	}
     71 
     72 	for (;;) {
     73 		col = 0;
     74 		move(CMDLINE, 0);
     75 		do {
     76 			refresh();
     77 			ch = getch() & 0177;
     78 			if (ch == 0177 && ferror(stdin)) {
     79 				clearerr(stdin);
     80 				continue;
     81 			}
     82 			rch = ch;
     83 			if (ch >= 'A' && ch <= 'Z')
     84 				ch += 'a' - 'A';
     85 			if (col == 0) {
     86 				if (ch == CTRL('l')) {
     87 					sigprocmask(SIG_BLOCK, &set, NULL);
     88 					wrefresh(curscr);
     89 					sigprocmask(SIG_UNBLOCK, &set, NULL);
     90 					continue;
     91 				}
     92 				if (ch == CTRL('g')) {
     93 					sigprocmask(SIG_BLOCK, &set, NULL);
     94 					status();
     95 					sigprocmask(SIG_UNBLOCK, &set, NULL);
     96 					continue;
     97 				}
     98 				if (ch == '?' || ch == 'h') {
     99 					command("help");
    100 					move(CMDLINE, 0);
    101 					continue;
    102 				}
    103 				if (ch != ':')
    104 					continue;
    105 				move(CMDLINE, 0);
    106 				clrtoeol();
    107 			}
    108 			if (ch == erasechar() && col > 0) {
    109 				if (col == 1 && line[0] == ':')
    110 					continue;
    111 				col--;
    112 				goto doerase;
    113 			}
    114 			if (ch == CTRL('w') && col > 0) {
    115 				while (--col >= 0 && isspace(line[col]));
    116 				col++;
    117 				while (--col >= 0 && !isspace(line[col]))
    118 					if (col == 0 && line[0] == ':')
    119 						break;
    120 				col++;
    121 				goto doerase;
    122 			}
    123 			if (ch == killchar() && col > 0) {
    124 				if (line[0] == ':')
    125 					col = 1;
    126 				else
    127 					col = 0;
    128 		doerase:
    129 				move(CMDLINE, col);
    130 				clrtoeol();
    131 				continue;
    132 			}
    133 			if (isprint(rch) || ch == ' ') {
    134 				if (col < linesz) {
    135 					line[col] = rch;
    136 					mvaddch(CMDLINE, col, rch);
    137 					col++;
    138 				}
    139 			}
    140 		} while (col == 0 || (ch != '\r' && ch != '\n'));
    141 		line[col] = '\0';
    142 		/* pass commands as lowercase */
    143 		for (i = 1; i < col && line[i] != ' '; i++)
    144 		    if (line[i] >= 'A' && line[i] <= 'Z')
    145 			line[i] += 'a' - 'A';
    146 		sigprocmask(SIG_BLOCK, &set, NULL);
    147 		command(line + 1);
    148 		sigprocmask(SIG_UNBLOCK, &set, NULL);
    149 	}
    150 	/* NOTREACHED */
    151 }
    152