[gjs/ewlsh/whatwg-console: 2/3] util: Add console handling utility
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/ewlsh/whatwg-console: 2/3] util: Add console handling utility
- Date: Tue, 17 Aug 2021 01:13:30 +0000 (UTC)
commit da5eacdd1a808ad004253481222446696648625b
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 | 42 ++++++++++++++++++++++++++++++++++++++++++
util/console.h | 18 ++++++++++++++++++
4 files changed, 65 insertions(+), 11 deletions(-)
---
diff --git a/gjs/debugger.cpp b/gjs/debugger.cpp
index 585024d3..8bf2d676 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(Gjs::Console::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(Gjs::Console::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..bf558e3e
--- /dev/null
+++ b/util/console.cpp
@@ -0,0 +1,42 @@
+// 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"
+
+namespace Gjs {
+namespace Console {
+
+#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 is_tty(int fd) {
+#ifdef HAVE_UNISTD_H
+ return isatty(fd);
+#elif defined(_WIN32)
+ return _isatty(fd);
+#else
+ return false;
+#endif
+}
+
+} // namespace Console
+} // namespace Gjs
diff --git a/util/console.h b/util/console.h
new file mode 100644
index 00000000..275c6c7c
--- /dev/null
+++ b/util/console.h
@@ -0,0 +1,18 @@
+// 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_
+
+namespace Gjs {
+namespace Console {
+extern const int stdout_fd;
+extern const int stdin_fd;
+extern const int stderr_fd;
+
+[[nodiscard]] bool is_tty(int fd = stdout_fd);
+
+}; // namespace Console
+}; // namespace Gjs
+
+#endif // UTIL_CONSOLE_H_
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]