19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 13*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 149b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 159b50d902SRodney W. Grimes * without specific prior written permission. 169b50d902SRodney W. Grimes * 179b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 189b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 199b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 209b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 219b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 229b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 239b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 249b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 259b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 269b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 279b50d902SRodney W. Grimes * SUCH DAMAGE. 289b50d902SRodney W. Grimes */ 299b50d902SRodney W. Grimes 309ff712b0SMark Murray #include <sys/cdefs.h> 319ff712b0SMark Murray 329ff712b0SMark Murray __FBSDID("$FreeBSD$"); 339ff712b0SMark Murray 349ff712b0SMark Murray #ifdef lint 359ff712b0SMark Murray static const char sccsid[] = "@(#)keyboard.c 8.1 (Berkeley) 6/6/93"; 369ff712b0SMark Murray #endif 379b50d902SRodney W. Grimes 388b3daf89SAlexander V. Chernikov #include <sys/select.h> 398b3daf89SAlexander V. Chernikov #include <sys/time.h> 408b3daf89SAlexander V. Chernikov 41c2dbb0deSJaakko Heinonen #include <errno.h> 429b50d902SRodney W. Grimes #include <ctype.h> 43c2dbb0deSJaakko Heinonen #include <stdlib.h> 449b50d902SRodney W. Grimes #include <termios.h> 458b3daf89SAlexander V. Chernikov #include <unistd.h> 469b50d902SRodney W. Grimes 479b50d902SRodney W. Grimes #include "systat.h" 489b50d902SRodney W. Grimes #include "extern.h" 499b50d902SRodney W. Grimes 508b3daf89SAlexander V. Chernikov static char line[80]; 518b3daf89SAlexander V. Chernikov static int keyboard_dispatch(int ch); 528b3daf89SAlexander V. Chernikov 539b50d902SRodney W. Grimes int 5493b9f504SXin LI keyboard(void) 559b50d902SRodney W. Grimes { 568b3daf89SAlexander V. Chernikov int ch, n; 578b3daf89SAlexander V. Chernikov struct timeval last, intvl, now, tm; 588b3daf89SAlexander V. Chernikov fd_set rfds; 599b50d902SRodney W. Grimes 608b3daf89SAlexander V. Chernikov /* Set initial timings */ 618b3daf89SAlexander V. Chernikov gettimeofday(&last, NULL); 628b3daf89SAlexander V. Chernikov intvl.tv_sec = delay / 1000000; 638b3daf89SAlexander V. Chernikov intvl.tv_usec = delay % 1000000; 649b50d902SRodney W. Grimes for (;;) { 659b50d902SRodney W. Grimes col = 0; 669b50d902SRodney W. Grimes move(CMDLINE, 0); 678b3daf89SAlexander V. Chernikov for (;;) { 688b3daf89SAlexander V. Chernikov /* Determine interval to sleep */ 698b3daf89SAlexander V. Chernikov (void)gettimeofday(&now, NULL); 708b3daf89SAlexander V. Chernikov tm.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec; 718b3daf89SAlexander V. Chernikov tm.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec; 728b3daf89SAlexander V. Chernikov while (tm.tv_usec < 0) { 738b3daf89SAlexander V. Chernikov tm.tv_usec += 1000000; 748b3daf89SAlexander V. Chernikov tm.tv_sec--; 758b3daf89SAlexander V. Chernikov } 768b3daf89SAlexander V. Chernikov while (tm.tv_usec >= 1000000) { 778b3daf89SAlexander V. Chernikov tm.tv_usec -= 1000000; 788b3daf89SAlexander V. Chernikov tm.tv_sec++; 798b3daf89SAlexander V. Chernikov } 808b3daf89SAlexander V. Chernikov if (tm.tv_sec < 0) { 818b3daf89SAlexander V. Chernikov /* We have to update screen immediately */ 828b3daf89SAlexander V. Chernikov display(); 838b3daf89SAlexander V. Chernikov gettimeofday(&last, NULL); 848b3daf89SAlexander V. Chernikov continue; 858b3daf89SAlexander V. Chernikov } 868b3daf89SAlexander V. Chernikov 878b3daf89SAlexander V. Chernikov /* Prepare select */ 888b3daf89SAlexander V. Chernikov FD_ZERO(&rfds); 898b3daf89SAlexander V. Chernikov FD_SET(STDIN_FILENO, &rfds); 908b3daf89SAlexander V. Chernikov n = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tm); 918b3daf89SAlexander V. Chernikov 928b3daf89SAlexander V. Chernikov if (n > 0) { 938b3daf89SAlexander V. Chernikov /* Read event on stdin */ 94c2dbb0deSJaakko Heinonen ch = getch(); 958b3daf89SAlexander V. Chernikov 968b3daf89SAlexander V. Chernikov if (keyboard_dispatch(ch) == 0) { 978b3daf89SAlexander V. Chernikov refresh(); 988b3daf89SAlexander V. Chernikov continue; 998b3daf89SAlexander V. Chernikov } 1008b3daf89SAlexander V. Chernikov 1018b3daf89SAlexander V. Chernikov line[col] = '\0'; 1028b3daf89SAlexander V. Chernikov command(line + 1); 1038b3daf89SAlexander V. Chernikov /* Refresh delay */ 1048b3daf89SAlexander V. Chernikov intvl.tv_sec = delay / 1000000; 1058b3daf89SAlexander V. Chernikov intvl.tv_usec = delay % 1000000; 1068b3daf89SAlexander V. Chernikov refresh(); 1078b3daf89SAlexander V. Chernikov break; 1088b3daf89SAlexander V. Chernikov } 1098b3daf89SAlexander V. Chernikov 1108b3daf89SAlexander V. Chernikov if (n < 0 && errno != EINTR) 1118b3daf89SAlexander V. Chernikov exit(1); 1128b3daf89SAlexander V. Chernikov 1138b3daf89SAlexander V. Chernikov /* Timeout or signal. Call display another time */ 1148b3daf89SAlexander V. Chernikov display(); 1158b3daf89SAlexander V. Chernikov gettimeofday(&last, NULL); 1168b3daf89SAlexander V. Chernikov } 1178b3daf89SAlexander V. Chernikov } 1188b3daf89SAlexander V. Chernikov } 1198b3daf89SAlexander V. Chernikov 1208b3daf89SAlexander V. Chernikov static int 1218b3daf89SAlexander V. Chernikov keyboard_dispatch(int ch) 1228b3daf89SAlexander V. Chernikov { 1238b3daf89SAlexander V. Chernikov 124c2dbb0deSJaakko Heinonen if (ch == ERR) { 125c2dbb0deSJaakko Heinonen if (errno == EINTR) 1268b3daf89SAlexander V. Chernikov return 0; 127c2dbb0deSJaakko Heinonen exit(1); 1289b50d902SRodney W. Grimes } 1299b50d902SRodney W. Grimes if (ch >= 'A' && ch <= 'Z') 1309b50d902SRodney W. Grimes ch += 'a' - 'A'; 1319b50d902SRodney W. Grimes if (col == 0) { 1329b50d902SRodney W. Grimes if (ch == CTRL('l')) { 1339b50d902SRodney W. Grimes wrefresh(curscr); 1348b3daf89SAlexander V. Chernikov return 0; 1359b50d902SRodney W. Grimes } 1369b50d902SRodney W. Grimes if (ch == CTRL('g')) { 1379b50d902SRodney W. Grimes status(); 1388b3daf89SAlexander V. Chernikov return 0; 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes if (ch != ':') 1418b3daf89SAlexander V. Chernikov return 0; 1429b50d902SRodney W. Grimes move(CMDLINE, 0); 1439b50d902SRodney W. Grimes clrtoeol(); 1449b50d902SRodney W. Grimes } 1459b50d902SRodney W. Grimes if (ch == erasechar() && col > 0) { 1469b50d902SRodney W. Grimes if (col == 1 && line[0] == ':') 1478b3daf89SAlexander V. Chernikov return 0; 1489b50d902SRodney W. Grimes col--; 1499b50d902SRodney W. Grimes goto doerase; 1509b50d902SRodney W. Grimes } 1519b50d902SRodney W. Grimes if (ch == CTRL('w') && col > 0) { 1529b50d902SRodney W. Grimes while (--col >= 0 && isspace(line[col])) 1539b50d902SRodney W. Grimes ; 1549b50d902SRodney W. Grimes col++; 1559b50d902SRodney W. Grimes while (--col >= 0 && !isspace(line[col])) 1569b50d902SRodney W. Grimes if (col == 0 && line[0] == ':') 1578b3daf89SAlexander V. Chernikov return 1; 1589b50d902SRodney W. Grimes col++; 1599b50d902SRodney W. Grimes goto doerase; 1609b50d902SRodney W. Grimes } 1619b50d902SRodney W. Grimes if (ch == killchar() && col > 0) { 1629b50d902SRodney W. Grimes col = 0; 1639b50d902SRodney W. Grimes if (line[0] == ':') 1649b50d902SRodney W. Grimes col++; 1659b50d902SRodney W. Grimes doerase: 1669b50d902SRodney W. Grimes move(CMDLINE, col); 1679b50d902SRodney W. Grimes clrtoeol(); 1688b3daf89SAlexander V. Chernikov return 0; 1699b50d902SRodney W. Grimes } 1709b50d902SRodney W. Grimes if (isprint(ch) || ch == ' ') { 1719b50d902SRodney W. Grimes line[col] = ch; 1729b50d902SRodney W. Grimes mvaddch(CMDLINE, col, ch); 1739b50d902SRodney W. Grimes col++; 1749b50d902SRodney W. Grimes } 1758b3daf89SAlexander V. Chernikov 1768b3daf89SAlexander V. Chernikov if (col == 0 || (ch != '\r' && ch != '\n')) 1778b3daf89SAlexander V. Chernikov return 0; 1788b3daf89SAlexander V. Chernikov 1798b3daf89SAlexander V. Chernikov return 1; 1809b50d902SRodney W. Grimes } 181