Files
lush/linit.c
Cormac Shannon 1766e40a68 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.
2026-03-18 09:29:51 +00:00

90 lines
2.4 KiB
C

/*
** $Id: linit.c $
** Initialization of libraries for lua.c and other clients
** See Copyright Notice in lua.h
*/
#define linit_c
#define LUA_LIB
#include "lprefix.h"
#include <stddef.h>
#include <stdlib.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "llimits.h"
#include "lcmd.h"
#include "lbuiltin.h"
/*
** Standard Libraries. (Must be listed in the same ORDER of their
** respective constants LUA_<libname>K.)
*/
static const luaL_Reg stdlibs[] = {
{LUA_GNAME, luaopen_base},
{LUA_LOADLIBNAME, luaopen_package},
{LUA_COLIBNAME, luaopen_coroutine},
{LUA_DBLIBNAME, luaopen_debug},
{LUA_IOLIBNAME, luaopen_io},
{LUA_MATHLIBNAME, luaopen_math},
{LUA_OSLIBNAME, luaopen_os},
{LUA_STRLIBNAME, luaopen_string},
{LUA_TABLIBNAME, luaopen_table},
{LUA_UTF8LIBNAME, luaopen_utf8},
{NULL, NULL}
};
/*
** Store shell functions in a registry table at LUA_RIDX_LUSH.
** Integer keys 1..5 hold the C functions (accessed by OP_LUSH).
** String key "builtins" holds the builtins table (set by luaopen_builtins).
*/
static void opencommand (lua_State *L) {
lua_createtable(L, LUSH_OP_COUNT, 1);
lua_pushcfunction(L, lushCmd_command);
lua_rawseti(L, -2, LUSH_OP_COMMAND + 1);
lua_pushcfunction(L, lushCmd_interactive);
lua_rawseti(L, -2, LUSH_OP_INTERACTIVE + 1);
lua_pushcfunction(L, lushCmd_getenv);
lua_rawseti(L, -2, LUSH_OP_GETENV + 1);
lua_pushcfunction(L, lushCmd_setenv);
lua_rawseti(L, -2, LUSH_OP_SETENV + 1);
lua_pushcfunction(L, lushCmd_subcmd);
lua_rawseti(L, -2, LUSH_OP_SUBCMD + 1);
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
}
/*
** require and preload selected standard libraries
*/
LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
int mask;
const luaL_Reg *lib;
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
for (lib = stdlibs, mask = 1; lib->name != NULL; lib++, mask <<= 1) {
if (load & mask) { /* selected? */
luaL_requiref(L, lib->name, lib->func, 1); /* require library */
lua_pop(L, 1); /* remove result from the stack */
}
else if (preload & mask) { /* selected? */
lua_pushcfunction(L, lib->func);
lua_setfield(L, -2, lib->name); /* add library to PRELOAD table */
}
}
lua_assert((mask >> 1) == LUA_UTF8LIBK);
lua_pop(L, 1); /* remove PRELOAD table */
opencommand(L); /* register __command global */
luaopen_builtins(L); /* register __builtins table */
}