From 5f3951a942fdc4bf489d8d590bfc891ac9548a23 Mon Sep 17 00:00:00 2001 From: Thijs Date: Sun, 12 Nov 2023 23:53:26 +0100 Subject: feat(tty): add isatty() --- src/Makefile | 2 +- src/core.c | 2 ++ src/term.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/term.c (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 119f95e..b4ed16f 100644 --- a/src/Makefile +++ b/src/Makefile @@ -217,7 +217,7 @@ LUALIB= $(LUALIB_$(PLAT)) #------ # Objects # -OBJS=core.$(O) compat.$(O) time.$(O) environment.$(O) random.$(O) +OBJS=core.$(O) compat.$(O) time.$(O) environment.$(O) random.$(O) term.$(O) #------ # Targets diff --git a/src/core.c b/src/core.c index fffedd1..87fd105 100644 --- a/src/core.c +++ b/src/core.c @@ -15,6 +15,7 @@ void time_open(lua_State *L); void environment_open(lua_State *L); void random_open(lua_State *L); +void term_open(lua_State *L); /*------------------------------------------------------------------------- * Initializes all library modules. @@ -33,6 +34,7 @@ LUAEXPORT int luaopen_system_core(lua_State *L) { lua_rawset(L, -3); time_open(L); random_open(L); + term_open(L); environment_open(L); return 1; } diff --git a/src/term.c b/src/term.c new file mode 100644 index 0000000..2adb1e9 --- /dev/null +++ b/src/term.c @@ -0,0 +1,37 @@ +/// @submodule system +#include +#include +#include +#include "compat.h" + +#ifndef _MSC_VER +# include +#endif + + +/*** +Checks if a file-handle is a TTY. + +@function isatty +@tparam file file the file-handle to check +@treturn boolean true if the file is a tty +*/ +static int lua_isatty(lua_State* L) { + FILE **fh = (FILE **) luaL_checkudata(L, 1, LUA_FILEHANDLE); + lua_pushboolean(L, isatty(fileno(*fh))); + return 1; +} + + + +static luaL_Reg func[] = { + { "isatty", lua_isatty }, + { NULL, NULL } +}; + +/*------------------------------------------------------------------------- + * Initializes module + *-------------------------------------------------------------------------*/ +void term_open(lua_State *L) { + luaL_setfuncs(L, func, 0); +} -- cgit v1.2.3-55-g6feb