aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/lxdrgen.awk29
-rw-r--r--tools/wdye/CMakeLists.txt4
-rw-r--r--tools/wdye/test.lua2
-rw-r--r--tools/wdye/wdye.adoc6
-rw-r--r--tools/wdye/wdye.c82
5 files changed, 78 insertions, 45 deletions
diff --git a/tools/lxdrgen.awk b/tools/lxdrgen.awk
index 5a51d2d..b3bb696 100644
--- a/tools/lxdrgen.awk
+++ b/tools/lxdrgen.awk
@@ -1,6 +1,6 @@
# lxdrgen.awk: an XDR-derived code generator for network protocols.
#
-# Copyright (c) 2022 - 2023, Přemysl Eric Janouch <p@janouch.name>
+# Copyright (c) 2022 - 2025, Přemysl Eric Janouch <p@janouch.name>
# SPDX-License-Identifier: 0BSD
#
# Usage: env LC_ALL=C awk -f lxdrgen.awk -f lxdrgen-{c,go,mjs}.awk \
@@ -218,7 +218,7 @@ function defstruct( name, d, cg) {
}
function defunion( name, tag, tagtype, tagvalue, cg, scg, d, a, i,
- unseen, exhaustive) {
+ unseen, defaulted, exhaustive) {
delete cg[0]
delete scg[0]
delete d[0]
@@ -249,9 +249,22 @@ function defunion( name, tag, tagtype, tagvalue, cg, scg, d, a, i,
if (!unseen[tagvalue]--)
fatal("no such value or duplicate case: " tagtype "." tagvalue)
codegen_struct_tag(tag, scg)
+ } else if (accept("default")) {
+ if (tagvalue)
+ codegen_union_struct(name, tagvalue, cg, scg)
+
+ expect(accept(":"))
+ if (defaulted)
+ fatal("duplicate default")
+
+ tagvalue = ""
+ defaulted = 1
} else if (tagvalue) {
if (readfield(d))
codegen_struct_field(d, scg)
+ } else if (defaulted) {
+ if (readfield(d))
+ fatal("default must not contain fields")
} else {
fatal("union fields must fall under a case")
}
@@ -259,11 +272,17 @@ function defunion( name, tag, tagtype, tagvalue, cg, scg, d, a, i,
if (tagvalue)
codegen_union_struct(name, tagvalue, cg, scg)
- # Unseen cases are simply not recognized/allowed.
+ # Unseen cases are only recognized/allowed when default is present.
exhaustive = 1
for (i in unseen)
- if (i && unseen[i])
- exhaustive = 0
+ if (i && unseen[i]) {
+ if (defaulted) {
+ codegen_struct_tag(tag, scg)
+ codegen_union_struct(name, i, cg, scg)
+ } else {
+ exhaustive = 0
+ }
+ }
Types[name] = "union"
codegen_union(name, cg, exhaustive)
diff --git a/tools/wdye/CMakeLists.txt b/tools/wdye/CMakeLists.txt
index a69f7ee..7dbf09e 100644
--- a/tools/wdye/CMakeLists.txt
+++ b/tools/wdye/CMakeLists.txt
@@ -22,7 +22,7 @@ option (WITH_CURSES "Offer terminal sequences using Curses" "${CURSES_FOUND}")
# -liconv may or may not be a part of libc
find_path (iconv_INCLUDE_DIRS iconv.h)
-include_directories ("${PROJECT_BINARY_DIR}" ${iconv_INCLUDE_DIRS})
+include_directories (BEFORE "${PROJECT_BINARY_DIR}" ${iconv_INCLUDE_DIRS})
file (CONFIGURE OUTPUT "${PROJECT_BINARY_DIR}/config.h" CONTENT [[
#define PROGRAM_NAME "${PROJECT_NAME}"
#define PROGRAM_VERSION "${PROJECT_VERSION}"
@@ -38,5 +38,5 @@ if (WITH_CURSES)
target_link_libraries (wdye PUBLIC ${CURSES_LIBRARIES})
endif ()
-add_test (NAME simple COMMAND wdye "${PROJECT_SOURCE_DIR}/test.lua")
+add_test (NAME wdye COMMAND wdye "${PROJECT_SOURCE_DIR}/test.lua")
include (CTest)
diff --git a/tools/wdye/test.lua b/tools/wdye/test.lua
index c255c72..add8488 100644
--- a/tools/wdye/test.lua
+++ b/tools/wdye/test.lua
@@ -25,7 +25,9 @@ cat:send("Closing...\r"):send("\004")
local v = expect(cat:eof {true},
cat:default {.5, function (p) error "expected EOF, got a timeout" end})
+assert(cat.pid > 0, "process has no ID")
local s1, exit, signal = cat:wait ()
assert(s1 == 0 and exit == 0 and not signal, "unexpected exit status")
+assert(cat.pid < 0, "process still has an ID")
local s2 = cat:wait (true)
assert(s1 == s2, "exit status not remembered")
diff --git a/tools/wdye/wdye.adoc b/tools/wdye/wdye.adoc
index 98e251e..2609849 100644
--- a/tools/wdye/wdye.adoc
+++ b/tools/wdye/wdye.adoc
@@ -31,7 +31,7 @@ The *env* map may be used to override environment variables, notably _TERM_.
Variables evaluating to _false_ will be removed from the environment.
The program's whole process group receives SIGKILL when the _process_
-is garbage-collected.
+is garbage-collected, unless *wait* has collected the process group leader.
wdye.expect ([pattern1, ...])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -57,6 +57,10 @@ PROCESS.buffer
~~~~~~~~~~~~~~
A string with the _process_' current read buffer contents.
+PROCESS.pid
+~~~~~~~~~~~
+An integer with the _process_' process ID, or -1 if *wait* has collected it.
+
PROCESS.term
~~~~~~~~~~~~
A table with the _process_' *terminfo*(5) capabilities,
diff --git a/tools/wdye/wdye.c b/tools/wdye/wdye.c
index b7fccd6..82de70f 100644
--- a/tools/wdye/wdye.c
+++ b/tools/wdye/wdye.c
@@ -175,7 +175,7 @@ pty_fork (int *ptrfdm, char **slave_name,
char *pts_name = NULL;
if ((fdm = ptym_open (&pts_name)) < 0)
{
- error_set (e, "can’t open master pty: %s", strerror (errno));
+ error_set (e, "can't open master pty: %s", strerror (errno));
return -1;
}
if (slave_name != NULL)
@@ -194,7 +194,7 @@ pty_fork (int *ptrfdm, char **slave_name,
if (setsid () < 0)
exit_fatal ("setsid: %s", strerror (errno));
if ((fds = ptys_open (pts_name)) < 0)
- exit_fatal ("can’t open slave pty: %s", strerror (errno));
+ exit_fatal ("can't open slave pty: %s", strerror (errno));
xclose (fdm);
#if defined BSD
@@ -401,7 +401,7 @@ xlua_pattern_index (lua_State *L)
if (!strcmp (key, "process"))
lua_rawgeti (L, LUA_REGISTRYINDEX, self->ref_process);
else
- return luaL_argerror (L, 2, "not a readable property");
+ return luaL_error (L, "not a readable property: %s", key);
return 1;
}
@@ -411,8 +411,8 @@ xlua_pattern_index (lua_State *L)
case PATTERN_REGEX:
{
const regmatch_t *m = self->matches + group;
- if (group < 0 || group > self->regex->re_nsub
- || m->rm_so < 0 || m->rm_eo < 0 || m->rm_eo > self->input.len)
+ if (group < 0 || (size_t) group > self->regex->re_nsub
+ || m->rm_so < 0 || m->rm_eo < 0 || (size_t) m->rm_eo > self->input.len)
lua_pushnil (L);
else
lua_pushlstring (L,
@@ -428,7 +428,7 @@ xlua_pattern_index (lua_State *L)
lua_pushlstring (L, self->input.str, self->input.len);
return 1;
default:
- return luaL_argerror (L, 2, "indexing unavailable for this pattern");
+ return luaL_argerror (L, 1, "indexing unavailable for this pattern");
}
}
@@ -519,10 +519,12 @@ xlua_process_index (lua_State *L)
if (!strcmp (key, "buffer"))
lua_pushlstring (L, self->buffer.str, self->buffer.len);
+ else if (!strcmp (key, "pid"))
+ lua_pushinteger (L, self->pid);
else if (!strcmp (key, "term"))
lua_rawgeti (L, LUA_REGISTRYINDEX, self->ref_term);
else
- return luaL_argerror (L, 2, "not a readable property");
+ return luaL_error (L, "not a readable property: %s", key);
return 1;
}
@@ -542,7 +544,7 @@ xlua_process_send (lua_State *L)
ssize_t written = write (self->terminal_fd, arg, len);
if (written == -1)
return luaL_error (L, "write failed: %s", strerror (errno));
- else if (written != len)
+ else if (written != (ssize_t) len)
return luaL_error (L, "write failed: %s", "short write");
if (self->asciicast)
@@ -560,7 +562,7 @@ xlua_process_send (lua_State *L)
static int
xlua_process_regex (lua_State *L)
{
- struct process *self = luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
+ (void) luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
luaL_checktype (L, 2, LUA_TTABLE);
if (lua_gettop (L) != 2)
return luaL_error (L, "too many arguments");
@@ -593,7 +595,7 @@ xlua_process_regex (lua_State *L)
static int
xlua_process_exact (lua_State *L)
{
- struct process *self = luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
+ (void) luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
luaL_checktype (L, 2, LUA_TTABLE);
if (lua_gettop (L) != 2)
return luaL_error (L, "too many arguments");
@@ -618,7 +620,7 @@ xlua_process_exact (lua_State *L)
static int
xlua_process_eof (lua_State *L)
{
- struct process *self = luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
+ (void) luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
luaL_checktype (L, 2, LUA_TTABLE);
if (lua_gettop (L) != 2)
return luaL_error (L, "too many arguments");
@@ -634,7 +636,7 @@ xlua_process_eof (lua_State *L)
static int
xlua_process_default (lua_State *L)
{
- struct process *self = luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
+ (void) luaL_checkudata (L, 1, XLUA_PROCESS_METATABLE);
luaL_checktype (L, 2, LUA_TTABLE);
if (lua_gettop (L) != 2)
return luaL_error (L, "too many arguments");
@@ -888,10 +890,11 @@ environ_map_serialize (struct str_map *env, struct strv *envv)
static int
spawn_protected (lua_State *L)
{
- struct spawn_context *ctx = lua_touserdata (L, 1);
+ struct spawn_context *ctx = lua_touserdata (L, lua_upvalueindex (1));
+ luaL_checktype (L, 1, LUA_TTABLE);
// Step 1: Prepare process environment.
- if (xlua_getfield (L, 2, "environ", LUA_TTABLE, true))
+ if (xlua_getfield (L, 1, "environ", LUA_TTABLE, true))
{
environ_map_update (&ctx->env, L);
lua_pop (L, 1);
@@ -911,11 +914,11 @@ spawn_protected (lua_State *L)
#endif
// Step 3: Prepare process command line.
- size_t argc = lua_rawlen (L, 2);
+ size_t argc = lua_rawlen (L, 1);
for (size_t i = 1; i <= argc; i++)
{
lua_pushinteger (L, i);
- lua_rawget (L, 2);
+ lua_rawget (L, 1);
const char *arg = lua_tostring (L, -1);
if (!arg)
return luaL_error (L, "spawn arguments must be strings");
@@ -1002,23 +1005,24 @@ spawn_protected (lua_State *L)
static int
xlua_spawn (lua_State *L)
{
- luaL_checktype (L, 1, LUA_TTABLE);
-
lua_pushcfunction (L, xlua_error_handler);
- lua_pushcfunction (L, spawn_protected);
+ lua_insert (L, 1);
+
+ struct spawn_context ctx = {};
+ lua_pushlightuserdata (L, &ctx);
+ lua_pushcclosure (L, spawn_protected, 1);
+ lua_insert (L, 2);
// There are way too many opportunities for Lua to throw,
// so maintain a context to clean up in one go.
- struct spawn_context ctx = spawn_context_make ();
- lua_pushlightuserdata (L, &ctx);
- lua_rotate (L, 1, -1);
- int result = lua_pcall (L, 2, 1, -4);
+ ctx = spawn_context_make ();
+ int result = lua_pcall (L, lua_gettop (L) - 2, 1, 1);
spawn_context_free (&ctx);
if (result)
return lua_error (L);
// Remove the error handler ("good programming practice").
- lua_remove (L, -2);
+ lua_remove (L, 1);
return 1;
}
@@ -1081,7 +1085,7 @@ expect_prepare_pattern (struct expect_context *ctx, struct pattern *p)
{
str_reset (&p->input);
if (p->kind == PATTERN_REGEX)
- for (int i = 0; i <= p->regex->re_nsub; i++)
+ for (size_t i = 0; i <= p->regex->re_nsub; i++)
p->matches[i] = (regmatch_t) { .rm_so = -1, .rm_eo = -1 };
if (p->kind == PATTERN_REGEX
@@ -1168,7 +1172,7 @@ pattern_match (struct pattern *self)
if (regexec (self->regex, buffer->str,
self->regex->re_nsub + 1, self->matches, flags))
{
- for (int i = 0; i <= self->regex->re_nsub; i++)
+ for (size_t i = 0; i <= self->regex->re_nsub; i++)
self->matches[i] = (regmatch_t) { .rm_so = -1, .rm_eo = -1 };
return false;
}
@@ -1374,9 +1378,9 @@ xlua_panic (lua_State *L)
int
main (int argc, char *argv[])
{
- if (argc != 2)
+ if (argc < 2)
{
- fprintf (stderr, "Usage: %s program.lua\n", argv[0]);
+ fprintf (stderr, "Usage: %s program.lua [args...]\n", argv[0]);
return 1;
}
@@ -1397,16 +1401,20 @@ main (int argc, char *argv[])
luaL_setfuncs (g.L, xlua_pattern_table, 0);
lua_pop (g.L, 1);
- const char *path = argv[1];
+ luaL_checkstack (g.L, argc, NULL);
+
lua_pushcfunction (g.L, xlua_error_handler);
- if (luaL_loadfile (g.L, path)
- || lua_pcall (g.L, 0, 0, -2))
- {
- print_error ("%s", lua_tostring (g.L, -1));
- lua_pop (g.L, 1);
- lua_close (g.L);
- return 1;
- }
+ if (luaL_loadfile (g.L, strcmp (argv[1], "-") ? argv[1] : NULL))
+ goto error;
+ for (int i = 2; i < argc; i++)
+ lua_pushstring (g.L, argv[i]);
+ if (lua_pcall (g.L, argc - 2, 0, 1))
+ goto error;
lua_close (g.L);
return 0;
+
+error:
+ print_error ("%s", lua_tostring (g.L, -1));
+ lua_close (g.L);
+ return 1;
}