1/* $NetBSD: keyboard.c,v 1.23 2006/05/10 21:53:48 mrg 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. 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#include <sys/cdefs.h> 33#ifndef lint 34#if 0 35static char sccsid[] = "@(#)keyboard.c 8.1 (Berkeley) 6/6/93"; 36#endif 37__RCSID("$NetBSD: keyboard.c,v 1.23 2006/05/10 21:53:48 mrg Exp $"); 38#endif /* not lint */ 39 40#include <sys/types.h> 41 42#include <ctype.h> 43#include <signal.h> 44#include <termios.h> 45#include <stdlib.h> 46#include <string.h> 47#include <signal.h> 48 49#include "systat.h" 50#include "extern.h" 51 52extern sig_atomic_t needsredraw; 53 54void 55keyboard(void) 56{ 57 int ch, rch, col; 58 char *line; 59 int i, linesz; 60 static char help[] = "help"; 61 static char quit[] = "quit"; 62 63 ch = 0; /* XXX gcc */ 64 linesz = COLS - 2; /* XXX does not get updated on SIGWINCH */ 65 if ((line = malloc(linesz)) == NULL) { 66 error("malloc failed"); 67 die(0); 68 } 69 70 for (;;) { 71 col = 0; 72 move(CMDLINE, 0); 73 74 while (col == 0 || (ch != '\r' && ch != '\n')) { 75 refresh(); 76 for (;;) { 77 ch = getch(); 78 if (!needsredraw) 79 break; 80 redraw(); 81 } 82 if (ch == ERR) { 83 display(SIGALRM); 84 continue; 85 } 86 if (ch == KEY_RESIZE) { 87 redraw(); 88 continue; 89 } 90 ch &= 0177; 91 rch = ch; 92 if (col == 0) { 93 switch(ch) { 94 case '\n': 95 case '\r': 96 case ' ': 97 display(0); 98 break; 99 case CTRL('l'): 100 wrefresh(curscr); 101 break; 102 case CTRL('g'): 103 status(); 104 break; 105 case '?': 106 case 'H': 107 case 'h': 108 command(help); 109 move(CMDLINE, 0); 110 break; 111 case 'Q': 112 case 'q': 113 command(quit); 114 break; 115 case ':': 116 move(CMDLINE, 0); 117 clrtoeol(); 118 addch(':'); 119 col++; 120 break; 121 } 122 continue; 123 } 124 if (ch == '\b' || ch == '\?' || ch == erasechar()) { 125 if (col > 0) 126 col--; 127 goto doerase; 128 } 129 if (ch == CTRL('w') && col > 0) { 130 while (--col >= 0 && 131 isspace((unsigned char)line[col])) 132 continue; 133 col++; 134 while (--col >= 0 && 135 !isspace((unsigned char)line[col])) 136 if (col == 0) 137 break; 138 col++; 139 goto doerase; 140 } 141 if (ch == killchar() && col > 0) { 142 col = 1; 143 doerase: 144 move(CMDLINE, col); 145 clrtoeol(); 146 continue; 147 } 148 if (isprint(rch) || rch == ' ') { 149 if (col < linesz) { 150 line[col] = rch; 151 mvaddch(CMDLINE, col, rch); 152 col++; 153 } 154 } 155 } 156 line[col] = '\0'; 157 /* pass commands as lowercase */ 158 for (i = 1; i < col ; i++) 159 line[i] = tolower((unsigned char)line[i]); 160 command(line + 1); 161 } 162 /* NOTREACHED */ 163} 164