[gjs/ewlsh/whatwg-console: 2/3] util: Add console handling utility




commit f06a570a069409ad0ebfd163fe1de263d898d09e
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Aug 7 00:21:15 2021 -0700

    util: Add console handling utility

 gjs/debugger.cpp | 15 ++++-----------
 meson.build      |  1 +
 util/console.cpp | 36 ++++++++++++++++++++++++++++++++++++
 util/console.h   | 29 +++++++++++++++++++++++++++++
 4 files changed, 70 insertions(+), 11 deletions(-)
---
diff --git a/gjs/debugger.cpp b/gjs/debugger.cpp
index 585024d3..5d815c8f 100644
--- a/gjs/debugger.cpp
+++ b/gjs/debugger.cpp
@@ -12,15 +12,6 @@
 #    include <readline/readline.h>
 #endif
 
-#ifdef HAVE_UNISTD_H
-#    include <unistd.h>  // for isatty, STDIN_FILENO
-#elif defined(_WIN32)
-#    include <io.h>
-#    ifndef STDIN_FILENO
-#        define STDIN_FILENO 0
-#    endif
-#endif
-
 #include <glib.h>
 
 #include <js/CallArgs.h>
@@ -39,6 +30,8 @@
 #include "gjs/jsapi-util.h"
 #include "gjs/macros.h"
 
+#include "util/console.h"
+
 GJS_JSAPI_RETURN_CONVENTION
 static bool quit(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
@@ -64,7 +57,7 @@ static bool do_readline(JSContext* cx, unsigned argc, JS::Value* vp) {
     do {
         const char* real_prompt = prompt ? prompt.get() : "db> ";
 #ifdef HAVE_READLINE_READLINE_H
-        if (isatty(STDIN_FILENO)) {
+        if (gjs_console_is_tty(stdin_fd)) {
             line = readline(real_prompt);
         } else {
 #else
@@ -77,7 +70,7 @@ static bool do_readline(JSContext* cx, unsigned argc, JS::Value* vp) {
                 buf[0] = '\0';
             line.reset(g_strdup(g_strchomp(buf)));
 
-            if (!isatty(STDIN_FILENO)) {
+            if (!gjs_console_is_tty(stdin_fd)) {
                 if (feof(stdin)) {
                     g_print("[quit due to end of input]\n");
                     line.reset(g_strdup("quit"));
diff --git a/meson.build b/meson.build
index 5b2bcdce..b6f6b40e 100644
--- a/meson.build
+++ b/meson.build
@@ -428,6 +428,7 @@ libgjs_jsapi_sources = [
     'gjs/jsapi-util-root.h',
     'gjs/jsapi-util-string.cpp',
     'gjs/jsapi-util.cpp', 'gjs/jsapi-util.h',
+    'util/console.cpp', 'util/console.h',
     'util/log.cpp', 'util/log.h',
     'util/misc.cpp', 'util/misc.h',
 ]
diff --git a/util/console.cpp b/util/console.cpp
new file mode 100644
index 00000000..49bf77e1
--- /dev/null
+++ b/util/console.cpp
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+#include <config.h>
+
+#ifdef HAVE_UNISTD_H
+#    include <unistd.h>
+#elif defined(_WIN32)
+#    include <io.h>
+#endif
+
+#include "util/console.h"
+
+#ifdef HAVE_UNISTD_H
+const int stdin_fd = STDIN_FILENO;
+const int stdout_fd = STDOUT_FILENO;
+const int stderr_fd = STDERR_FILENO;
+#elif defined(_WIN32)
+const int stdin_fd = _fileno(stdin);
+const int stdout_fd = _fileno(stdout);
+const int stderr_fd = _fileno(stderr);
+#else
+const int stdin_fd = 0;
+const int stdout_fd = 1;
+const int stderr_fd = 2;
+#endif
+
+bool gjs_console_is_tty(int fd) {
+#ifdef HAVE_UNISTD_H
+    return isatty(fd);
+#elif defined(_WIN32)
+    return _isatty(fd);
+#else
+    return false;
+#endif
+}
diff --git a/util/console.h b/util/console.h
new file mode 100644
index 00000000..d8acd5ed
--- /dev/null
+++ b/util/console.h
@@ -0,0 +1,29 @@
+/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
+/*
+ * SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+ * SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+ */
+
+#ifndef UTIL_CONSOLE_H_
+#define UTIL_CONSOLE_H_
+
+/* This file has to be valid C, because it's used in libgjs-private */
+
+#include <stdbool.h> /* IWYU pragma: keep */
+
+#include <glib.h>
+
+#include "gjs/macros.h"
+
+G_BEGIN_DECLS
+
+extern const int stdout_fd;
+extern const int stdin_fd;
+extern const int stderr_fd;
+
+GJS_USE
+bool gjs_console_is_tty(int fd);
+
+G_END_DECLS
+
+#endif  // UTIL_CONSOLE_H_


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]