Implement $(cmd) subcommand syntax in commands (issue #27)

Add $() for inline subcommand substitution that runs a command and
inserts its stdout (trailing newlines stripped) into the outer command
string. Supports nesting, and mixing with ${expr} and $NAME.
This commit is contained in:
Cormac Shannon
2026-03-18 09:29:51 +00:00
parent 42baabde34
commit 1766e40a68
8 changed files with 143 additions and 7 deletions

24
lcmd.c
View File

@@ -1039,6 +1039,30 @@ int lushCmd_command (lua_State *L) {
}
/* ===== lushCmd_subcmd ===== */
/*
** Subcommand substitution: run a command, return stdout with trailing
** newlines stripped. Used for $(cmd) inside command strings.
*/
int lushCmd_subcmd (lua_State *L) {
size_t len;
const char *s;
/* run the command — pushes {code, stdout, stderr} table */
lushCmd_command(L);
/* extract .stdout from result table */
lua_getfield(L, -1, "stdout");
lua_remove(L, -2); /* remove the table, keep stdout string */
/* strip trailing newlines */
s = lua_tolstring(L, -1, &len);
while (len > 0 && s[len - 1] == '\n')
len--;
lua_pushlstring(L, s, len);
lua_remove(L, -2); /* remove original stdout string */
return 1;
}
/* ===== lushCmd_interactive ===== */
int lushCmd_interactive (lua_State *L) {