aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--liberty-proto.c2
-rw-r--r--tools/wdye/CMakeLists.txt4
-rw-r--r--tools/wdye/test.lua2
-rw-r--r--tools/wdye/wdye.adoc12
-rw-r--r--tools/wdye/wdye.c122
5 files changed, 124 insertions, 18 deletions
diff --git a/liberty-proto.c b/liberty-proto.c
index d4355c7..fb0fc36 100644
--- a/liberty-proto.c
+++ b/liberty-proto.c
@@ -1594,6 +1594,8 @@ mpd_client_parse_kv (char *line, char **value)
static void
mpd_client_update_poller (struct mpd_client *self)
{
+ if (self->state != MPD_CONNECTED)
+ return;
poller_fd_set (&self->socket_event,
self->write_buffer.len ? (POLLIN | POLLOUT) : POLLIN);
}
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 d901400..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,
@@ -126,6 +130,12 @@ Example
rot13:send "Hello\r"
expect(rot13:exact {"Uryyb\r"})
+Environment
+-----------
+*WDYE_LOGGING*::
+ When this environment variable is present, *wdye* produces asciicast v2
+ files for every spawned program, in the current working directory.
+
Reporting bugs
--------------
Use https://git.janouch.name/p/liberty to report bugs, request features,
diff --git a/tools/wdye/wdye.c b/tools/wdye/wdye.c
index ed4f128..677d945 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
@@ -222,6 +222,45 @@ pty_fork (int *ptrfdm, char **slave_name,
return pid;
}
+// --- JSON --------------------------------------------------------------------
+
+static void
+write_json_string (FILE *output, const char *s, size_t len)
+{
+ fputc ('"', output);
+ for (const char *last = s, *end = s + len; s != end; last = s)
+ {
+ // Here is where you realize the asciicast format is retarded for using
+ // JSON at all. (Consider multibyte characters at read() boundaries.)
+ int32_t codepoint = utf8_decode (&s, end - s);
+ if (codepoint < 0)
+ {
+ s++;
+ fprintf (output, "\\uFFFD");
+ continue;
+ }
+
+ switch (codepoint)
+ {
+ break; case '"': fprintf (output, "\\\"");
+ break; case '\\': fprintf (output, "\\\\");
+ break; case '\b': fprintf (output, "\\b");
+ break; case '\f': fprintf (output, "\\f");
+ break; case '\n': fprintf (output, "\\n");
+ break; case '\r': fprintf (output, "\\r");
+ break; case '\t': fprintf (output, "\\t");
+ break; default:
+ if (!utf8_validate_cp (codepoint))
+ fprintf (output, "\\uFFFD");
+ else if (codepoint < 32)
+ fprintf (output, "\\u%04X", codepoint);
+ else
+ fwrite (last, 1, s - last, output);
+ }
+ }
+ fputc ('"', output);
+}
+
// --- Global state ------------------------------------------------------------
static struct
@@ -362,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;
}
@@ -372,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,
@@ -389,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");
}
}
@@ -435,6 +474,9 @@ struct process
int ref_term; ///< Terminal information
struct str buffer; ///< Terminal input buffer
int status; ///< Process status iff pid is -1
+
+ int64_t start; ///< Start timestamp (Unix msec)
+ FILE *asciicast; ///< asciicast script dump
};
static struct process *
@@ -462,6 +504,8 @@ xlua_process_gc (lua_State *L)
kill (-self->pid, SIGKILL);
luaL_unref (L, LUA_REGISTRYINDEX, self->ref_term);
str_free (&self->buffer);
+ if (self->asciicast)
+ fclose (self->asciicast);
return 0;
}
@@ -475,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;
}
@@ -498,8 +544,16 @@ 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)
+ {
+ double timestamp = (clock_msec () - self->start) / 1000.;
+ fprintf (self->asciicast, "[%f, \"i\", ", timestamp);
+ write_json_string (self->asciicast, arg, len);
+ fprintf (self->asciicast, "]\n");
+ }
}
lua_pushvalue (L, 1);
return 1;
@@ -508,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");
@@ -541,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");
@@ -566,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");
@@ -582,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");
@@ -667,6 +721,14 @@ process_feed (struct process *self)
return false;
}
+ if (self->asciicast)
+ {
+ double timestamp = (clock_msec () - self->start) / 1000.;
+ fprintf (self->asciicast, "[%f, \"o\", ", timestamp);
+ write_json_string (self->asciicast, buf, n);
+ fprintf (self->asciicast, "]\n");
+ }
+
// TODO(p): Add match_max processing, limiting the buffer size.
str_append_data (&self->buffer, buf, n);
return n > 0;
@@ -888,9 +950,17 @@ spawn_protected (lua_State *L)
}
process->ref_term = luaL_ref (L, LUA_REGISTRYINDEX);
+ struct winsize ws = { .ws_row = 24, .ws_col = 80 };
+ if ((entry = str_map_find (&ctx->term, "lines"))
+ && entry->kind == TERMINFO_NUMERIC)
+ ws.ws_row = entry->numeric;
+ if ((entry = str_map_find (&ctx->term, "columns"))
+ && entry->kind == TERMINFO_NUMERIC)
+ ws.ws_col = entry->numeric;
+
// Step 5: Spawn the process, which gets a new process group.
process->pid =
- pty_fork (&process->terminal_fd, NULL, NULL, NULL, &ctx->error);
+ pty_fork (&process->terminal_fd, NULL, NULL, &ws, &ctx->error);
if (process->pid < 0)
{
return luaL_error (L, "failed to spawn %s: %s",
@@ -905,6 +975,28 @@ spawn_protected (lua_State *L)
_exit (EXIT_FAILURE);
}
+ // Step 6: Create a log file.
+ if (getenv ("WDYE_LOGGING"))
+ {
+ const char *name = ctx->argv.vector[0];
+ const char *last_slash = strrchr (name, '/');
+ if (last_slash)
+ name = last_slash + 1;
+
+ char *path = xstrdup_printf ("%s-%s.%d.cast",
+ PROGRAM_NAME, name, (int) process->pid);
+ if (!(process->asciicast = fopen (path, "w")))
+ print_warning ("%s: %s", path, strerror (errno));
+ free (path);
+ }
+ process->start = clock_msec ();
+ if (process->asciicast)
+ {
+ fprintf (process->asciicast, "{\"version\": 2, "
+ "\"width\": %u, \"height\": %u, \"env\": {\"TERM\": \"%s\"}}\n",
+ ws.ws_col, ws.ws_row, term);
+ }
+
set_cloexec (process->terminal_fd);
return 1;
}
@@ -991,7 +1083,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
@@ -1078,7 +1170,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;
}