[gjs/ewlsh/whatwg-console: 1/4] util: Add Console handling utility
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/ewlsh/whatwg-console: 1/4] util: Add Console handling utility
- Date: Sat, 7 Aug 2021 07:26:33 +0000 (UTC)
commit 93f48d22edfd7fbc3ec0fae1baf7411467d37db5
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 +
modules/console.cpp | 1 +
util/console.cpp | 35 +++++++++++++++++++++++++++++++++++
util/console.h | 18 ++++++++++++++++++
5 files changed, 59 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 9214a5fe..dfd5a935 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/modules/console.cpp b/modules/console.cpp
index 2ef511aa..70d0b7a1 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -42,6 +42,7 @@
#include "gjs/context-private.h"
#include "gjs/jsapi-util.h"
#include "modules/console.h"
+#include "util/console.h"
namespace mozilla {
union Utf8Unit;
diff --git a/util/console.cpp b/util/console.cpp
new file mode 100644
index 00000000..48af951c
--- /dev/null
+++ b/util/console.cpp
@@ -0,0 +1,35 @@
+#include <config.h>
+
+#include <stdio.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 Gjs::Console::stdin_fd = STDIN_FILENO;
+const int Gjs::Console::stdout_fd = STDOUT_FILENO;
+const int Gjs::Console::stderr_fd = STDERR_FILENO;
+#elif defined(_WIN32)
+const int Gjs::Console::stdin_fd = _fileno(stdin);
+const int Gjs::Console::stdout_fd = _fileno(stdout);
+const int Gjs::Console::stderr_fd = _fileno(stderr);
+#else
+const int Gjs::Console::stdin_fd = 0;
+const int Gjs::Console::stdout_fd = 1;
+const int Gjs::Console::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..442006d0
--- /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_
\ No newline at end of file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]