diff options
Diffstat (limited to 'contrib/less/ttyin.c')
-rw-r--r-- | contrib/less/ttyin.c | 103 |
1 files changed, 46 insertions, 57 deletions
diff --git a/contrib/less/ttyin.c b/contrib/less/ttyin.c index 5bd3385ceadb..96486f53769b 100644 --- a/contrib/less/ttyin.c +++ b/contrib/less/ttyin.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1984-2021 Mark Nudelman + * Copyright (C) 1984-2023 Mark Nudelman * * You may distribute under the terms of either the GNU General Public * License or the Less License, as specified in the README file. @@ -30,38 +30,55 @@ public int tty; #endif #if LESSTEST public char *ttyin_name = NULL; -public int rstat_file = -1; #endif /*LESSTEST*/ extern int sigs; extern int utf_mode; extern int wheel_lines; -/* - * Get name of tty device. - */ #if !MSDOS_COMPILER - public char * -tty_device(VOID_PARAM) +static int open_tty_device(constant char* dev) { - char *dev = NULL; -#if HAVE_TTYNAME - dev = ttyname(2); +#if OS2 + /* The __open() system call translates "/dev/tty" to "con". */ + return __open(dev, OPEN_READ); +#else + return open(dev, OPEN_READ); #endif - if (dev == NULL) - dev = "/dev/tty"; +} + +/* + * Open the tty device. + * Try ttyname(), then try /dev/tty, then use file descriptor 2. + * In Unix, file descriptor 2 is usually attached to the screen, + * but also usually lets you read from the keyboard. + */ +public int open_tty(void) +{ + int fd = -1; #if LESSTEST if (ttyin_name != NULL) - dev = ttyin_name; + fd = open_tty_device(ttyin_name); #endif /*LESSTEST*/ - return dev; +#if HAVE_TTYNAME + if (fd < 0) + { + constant char *dev = ttyname(2); + if (dev != NULL) + fd = open_tty_device(dev); + } +#endif + if (fd < 0) + fd = open_tty_device("/dev/tty"); + if (fd < 0) + fd = 2; + return fd; } #endif /* MSDOS_COMPILER */ /* * Open keyboard for input. */ - public void -open_getchr(VOID_PARAM) +public void open_getchr(void) { #if MSDOS_COMPILER==WIN32C /* Need this to let child processes inherit our console handle */ @@ -93,20 +110,7 @@ open_getchr(VOID_PARAM) (void) __djgpp_set_ctrl_c(1); #endif #else - /* - * Try /dev/tty. - * If that doesn't work, use file descriptor 2, - * which in Unix is usually attached to the screen, - * but also usually lets you read from the keyboard. - */ -#if OS2 - /* The __open() system call translates "/dev/tty" to "con". */ - tty = __open(tty_device(), OPEN_READ); -#else - tty = open(tty_device(), OPEN_READ); -#endif - if (tty < 0) - tty = 2; + tty = open_tty(); #endif #endif } @@ -114,8 +118,7 @@ open_getchr(VOID_PARAM) /* * Close the keyboard. */ - public void -close_getchr(VOID_PARAM) +public void close_getchr(void) { #if MSDOS_COMPILER==WIN32C SetConsoleMode(tty, console_mode); @@ -127,9 +130,7 @@ close_getchr(VOID_PARAM) /* * Close the pipe, restoring the keyboard (CMD resets it, losing the mouse). */ - int -pclose(f) - FILE *f; +public int pclose(FILE *f) { int result; @@ -142,8 +143,7 @@ pclose(f) /* * Get the number of lines to scroll when mouse wheel is moved. */ - public int -default_wheel_lines(VOID_PARAM) +public int default_wheel_lines(void) { int lines = 1; #if MSDOS_COMPILER==WIN32C @@ -156,23 +156,10 @@ default_wheel_lines(VOID_PARAM) return lines; } -#if LESSTEST - public void -rstat(st) - char st; -{ - if (rstat_file < 0) - return; - lseek(rstat_file, SEEK_SET, 0); - write(rstat_file, &st, 1); -} -#endif /*LESSTEST*/ - /* * Get a character from the keyboard. */ - public int -getchr(VOID_PARAM) +public int getchr(void) { char c; int result; @@ -195,17 +182,11 @@ getchr(VOID_PARAM) if (c == '\003') return (READ_INTR); #else -#if LESSTEST - rstat('R'); -#endif /*LESSTEST*/ { unsigned char uc; result = iread(tty, &uc, sizeof(char)); c = (char) uc; } -#if LESSTEST - rstat('B'); -#endif /*LESSTEST*/ if (result == READ_INTR) return (READ_INTR); if (result < 0) @@ -217,6 +198,14 @@ getchr(VOID_PARAM) quit(QUIT_ERROR); } #endif +#if LESSTEST + if (c == LESS_DUMP_CHAR) + { + dump_screen(); + result = 0; + continue; + } +#endif #if 0 /* allow entering arbitrary hex chars for testing */ /* ctrl-A followed by two hex chars makes a byte */ { |