19b50d902SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329ff712b0SMark Murray
339b50d902SRodney W. Grimes
348b3daf89SAlexander V. Chernikov #include <sys/select.h>
358b3daf89SAlexander V. Chernikov #include <sys/time.h>
368b3daf89SAlexander V. Chernikov
37c2dbb0deSJaakko Heinonen #include <errno.h>
389b50d902SRodney W. Grimes #include <ctype.h>
39c2dbb0deSJaakko Heinonen #include <stdlib.h>
409b50d902SRodney W. Grimes #include <termios.h>
418b3daf89SAlexander V. Chernikov #include <unistd.h>
429b50d902SRodney W. Grimes
439b50d902SRodney W. Grimes #include "systat.h"
449b50d902SRodney W. Grimes #include "extern.h"
459b50d902SRodney W. Grimes
468b3daf89SAlexander V. Chernikov static char line[80];
478b3daf89SAlexander V. Chernikov static int keyboard_dispatch(int ch);
488b3daf89SAlexander V. Chernikov
499b50d902SRodney W. Grimes int
keyboard(void)5093b9f504SXin LI keyboard(void)
519b50d902SRodney W. Grimes {
528b3daf89SAlexander V. Chernikov int ch, n;
538b3daf89SAlexander V. Chernikov struct timeval last, intvl, now, tm;
548b3daf89SAlexander V. Chernikov fd_set rfds;
559b50d902SRodney W. Grimes
568b3daf89SAlexander V. Chernikov /* Set initial timings */
578b3daf89SAlexander V. Chernikov gettimeofday(&last, NULL);
588b3daf89SAlexander V. Chernikov intvl.tv_sec = delay / 1000000;
598b3daf89SAlexander V. Chernikov intvl.tv_usec = delay % 1000000;
609b50d902SRodney W. Grimes for (;;) {
619b50d902SRodney W. Grimes col = 0;
629b50d902SRodney W. Grimes move(CMDLINE, 0);
638b3daf89SAlexander V. Chernikov for (;;) {
648b3daf89SAlexander V. Chernikov /* Determine interval to sleep */
658b3daf89SAlexander V. Chernikov (void)gettimeofday(&now, NULL);
668b3daf89SAlexander V. Chernikov tm.tv_sec = last.tv_sec + intvl.tv_sec - now.tv_sec;
678b3daf89SAlexander V. Chernikov tm.tv_usec = last.tv_usec + intvl.tv_usec - now.tv_usec;
688b3daf89SAlexander V. Chernikov while (tm.tv_usec < 0) {
698b3daf89SAlexander V. Chernikov tm.tv_usec += 1000000;
708b3daf89SAlexander V. Chernikov tm.tv_sec--;
718b3daf89SAlexander V. Chernikov }
728b3daf89SAlexander V. Chernikov while (tm.tv_usec >= 1000000) {
738b3daf89SAlexander V. Chernikov tm.tv_usec -= 1000000;
748b3daf89SAlexander V. Chernikov tm.tv_sec++;
758b3daf89SAlexander V. Chernikov }
768b3daf89SAlexander V. Chernikov if (tm.tv_sec < 0) {
778b3daf89SAlexander V. Chernikov /* We have to update screen immediately */
788b3daf89SAlexander V. Chernikov display();
798b3daf89SAlexander V. Chernikov gettimeofday(&last, NULL);
808b3daf89SAlexander V. Chernikov continue;
818b3daf89SAlexander V. Chernikov }
828b3daf89SAlexander V. Chernikov
838b3daf89SAlexander V. Chernikov /* Prepare select */
848b3daf89SAlexander V. Chernikov FD_ZERO(&rfds);
858b3daf89SAlexander V. Chernikov FD_SET(STDIN_FILENO, &rfds);
868b3daf89SAlexander V. Chernikov n = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tm);
878b3daf89SAlexander V. Chernikov
888b3daf89SAlexander V. Chernikov if (n > 0) {
898b3daf89SAlexander V. Chernikov /* Read event on stdin */
90c2dbb0deSJaakko Heinonen ch = getch();
918b3daf89SAlexander V. Chernikov
928b3daf89SAlexander V. Chernikov if (keyboard_dispatch(ch) == 0) {
938b3daf89SAlexander V. Chernikov refresh();
948b3daf89SAlexander V. Chernikov continue;
958b3daf89SAlexander V. Chernikov }
968b3daf89SAlexander V. Chernikov
978b3daf89SAlexander V. Chernikov line[col] = '\0';
988b3daf89SAlexander V. Chernikov command(line + 1);
998b3daf89SAlexander V. Chernikov /* Refresh delay */
1008b3daf89SAlexander V. Chernikov intvl.tv_sec = delay / 1000000;
1018b3daf89SAlexander V. Chernikov intvl.tv_usec = delay % 1000000;
1028b3daf89SAlexander V. Chernikov refresh();
1038b3daf89SAlexander V. Chernikov break;
1048b3daf89SAlexander V. Chernikov }
1058b3daf89SAlexander V. Chernikov
1068b3daf89SAlexander V. Chernikov if (n < 0 && errno != EINTR)
1078b3daf89SAlexander V. Chernikov exit(1);
1088b3daf89SAlexander V. Chernikov
1098b3daf89SAlexander V. Chernikov /* Timeout or signal. Call display another time */
1108b3daf89SAlexander V. Chernikov display();
1118b3daf89SAlexander V. Chernikov gettimeofday(&last, NULL);
1128b3daf89SAlexander V. Chernikov }
1138b3daf89SAlexander V. Chernikov }
1148b3daf89SAlexander V. Chernikov }
1158b3daf89SAlexander V. Chernikov
1168b3daf89SAlexander V. Chernikov static int
keyboard_dispatch(int ch)1178b3daf89SAlexander V. Chernikov keyboard_dispatch(int ch)
1188b3daf89SAlexander V. Chernikov {
1198b3daf89SAlexander V. Chernikov
120c2dbb0deSJaakko Heinonen if (ch == ERR) {
121c2dbb0deSJaakko Heinonen if (errno == EINTR)
1228b3daf89SAlexander V. Chernikov return 0;
123c2dbb0deSJaakko Heinonen exit(1);
1249b50d902SRodney W. Grimes }
1259b50d902SRodney W. Grimes if (ch >= 'A' && ch <= 'Z')
1269b50d902SRodney W. Grimes ch += 'a' - 'A';
1279b50d902SRodney W. Grimes if (col == 0) {
1289b50d902SRodney W. Grimes if (ch == CTRL('l')) {
1299b50d902SRodney W. Grimes wrefresh(curscr);
1308b3daf89SAlexander V. Chernikov return 0;
1319b50d902SRodney W. Grimes }
1329b50d902SRodney W. Grimes if (ch == CTRL('g')) {
1339b50d902SRodney W. Grimes status();
1348b3daf89SAlexander V. Chernikov return 0;
1359b50d902SRodney W. Grimes }
1369b50d902SRodney W. Grimes if (ch != ':')
1378b3daf89SAlexander V. Chernikov return 0;
1389b50d902SRodney W. Grimes move(CMDLINE, 0);
1399b50d902SRodney W. Grimes clrtoeol();
1409b50d902SRodney W. Grimes }
1419b50d902SRodney W. Grimes if (ch == erasechar() && col > 0) {
1429b50d902SRodney W. Grimes if (col == 1 && line[0] == ':')
1438b3daf89SAlexander V. Chernikov return 0;
1449b50d902SRodney W. Grimes col--;
1459b50d902SRodney W. Grimes goto doerase;
1469b50d902SRodney W. Grimes }
1479b50d902SRodney W. Grimes if (ch == CTRL('w') && col > 0) {
1489b50d902SRodney W. Grimes while (--col >= 0 && isspace(line[col]))
1499b50d902SRodney W. Grimes ;
1509b50d902SRodney W. Grimes col++;
1519b50d902SRodney W. Grimes while (--col >= 0 && !isspace(line[col]))
1529b50d902SRodney W. Grimes if (col == 0 && line[0] == ':')
1538b3daf89SAlexander V. Chernikov return 1;
1549b50d902SRodney W. Grimes col++;
1559b50d902SRodney W. Grimes goto doerase;
1569b50d902SRodney W. Grimes }
1579b50d902SRodney W. Grimes if (ch == killchar() && col > 0) {
1589b50d902SRodney W. Grimes col = 0;
1599b50d902SRodney W. Grimes if (line[0] == ':')
1609b50d902SRodney W. Grimes col++;
1619b50d902SRodney W. Grimes doerase:
1629b50d902SRodney W. Grimes move(CMDLINE, col);
1639b50d902SRodney W. Grimes clrtoeol();
1648b3daf89SAlexander V. Chernikov return 0;
1659b50d902SRodney W. Grimes }
1669b50d902SRodney W. Grimes if (isprint(ch) || ch == ' ') {
1679b50d902SRodney W. Grimes line[col] = ch;
1689b50d902SRodney W. Grimes mvaddch(CMDLINE, col, ch);
1699b50d902SRodney W. Grimes col++;
1709b50d902SRodney W. Grimes }
1718b3daf89SAlexander V. Chernikov
1728b3daf89SAlexander V. Chernikov if (col == 0 || (ch != '\r' && ch != '\n'))
1738b3daf89SAlexander V. Chernikov return 0;
1748b3daf89SAlexander V. Chernikov
1758b3daf89SAlexander V. Chernikov return 1;
1769b50d902SRodney W. Grimes }
177