From c5e098bc35d001cbf920bd9586ba21a23acbf90a Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 2 Mar 2019 22:27:39 +0100 Subject: [PATCH] mescc: Mes C Library: Fix isatty. * lib/posix/isatty.c (isatty): Test ioctl == 0. * mes/module/mes/boot-0.scm.in: Update: no tty?: read from stdin. --- lib/posix/isatty.c | 56 +++++++++++++++++++++++++++++++++--- mes/module/mes/boot-0.scm.in | 2 +- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/posix/isatty.c b/lib/posix/isatty.c index 6ee18077..41353146 100644 --- a/lib/posix/isatty.c +++ b/lib/posix/isatty.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -20,8 +20,56 @@ #include -int -isatty (int fd) +typedef unsigned char cc_t; +typedef unsigned int speed_t; +typedef unsigned int tcflag_t; + +// Use termio, termios2? +#define NCCS 19 +struct termios { - return ioctl (fd, TCGETS, 0) & 0xf0; + tcflag_t c_iflag; + tcflag_t c_oflag; + tcflag_t c_cflag; + tcflag_t c_lflag; + cc_t c_line; + cc_t c_cc[NCCS]; +}; + +struct ktermios +{ + tcflag_t c_iflag; + tcflag_t c_oflag; + tcflag_t c_cflag; + tcflag_t c_lflag; + cc_t c_line; + cc_t c_cc[NCCS]; + speed_t c_ispeed; + speed_t c_ospeed; +}; + +int +__tcgetattr (int filedes, struct termios *termios_p) +{ + struct ktermios kernel_termios; + int r = ioctl (filedes, TCGETS, &kernel_termios); + + termios_p->c_iflag = kernel_termios.c_iflag; + termios_p->c_oflag = kernel_termios.c_oflag; + termios_p->c_cflag = kernel_termios.c_cflag; + termios_p->c_lflag = kernel_termios.c_lflag; + termios_p->c_line = kernel_termios.c_line; +#if 0 + termios_p->c_ispeed = kernel_termios.c_ispeed; + termios_p->c_ospeed = kernel_termios.c_ospeed; +#endif + memcpy (&termios_p->c_cc[0], &kernel_termios.c_cc[0], NCCS * sizeof (cc_t)); + return r; +} + +int +isatty (int filedes) +{ + struct termios term; + return __tcgetattr (filedes, &term) == 0; } diff --git a/mes/module/mes/boot-0.scm.in b/mes/module/mes/boot-0.scm.in index 4ce8610f..b5629a6c 100644 --- a/mes/module/mes/boot-0.scm.in +++ b/mes/module/mes/boot-0.scm.in @@ -234,7 +234,7 @@ (files (if s-index (list-tail %argv (+ s-index 1)) (option-ref options '() '()))) (help? (option-ref options 'help #f)) - (usage? (and (not help?) (null? files) (not tty?) (not main))) + (usage? #f) (version? (option-ref options 'version #f))) (or (and version?