tree: reformat
Done automatically by clang-format
This commit is contained in:
parent
669eb3fd28
commit
30a1097bb0
8 changed files with 311 additions and 278 deletions
200
csto.c
200
csto.c
|
@ -4,10 +4,10 @@
|
||||||
* Copyright (c) 2023-2024 Jeremy Baxter.
|
* Copyright (c) 2023-2024 Jeremy Baxter.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <signal.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <lua/lauxlib.h>
|
#include <lua/lauxlib.h>
|
||||||
|
@ -30,11 +30,12 @@ static lua_State *globalL = NULL;
|
||||||
|
|
||||||
static const char *progname = LUA_PROGNAME;
|
static const char *progname = LUA_PROGNAME;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Use 'sigaction' when available.
|
* Use 'sigaction' when available.
|
||||||
*/
|
*/
|
||||||
static void setsignal (int sig, void (*handler)(int)) {
|
static void
|
||||||
|
setsignal(int sig, void (*handler)(int))
|
||||||
|
{
|
||||||
struct sigaction sa;
|
struct sigaction sa;
|
||||||
sa.sa_handler = handler;
|
sa.sa_handler = handler;
|
||||||
sa.sa_flags = 0;
|
sa.sa_flags = 0;
|
||||||
|
@ -42,31 +43,34 @@ static void setsignal (int sig, void (*handler)(int)) {
|
||||||
sigaction(sig, &sa, NULL);
|
sigaction(sig, &sa, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Hook set by signal function to stop the interpreter.
|
* Hook set by signal function to stop the interpreter.
|
||||||
*/
|
*/
|
||||||
static void lstop (lua_State *L, lua_Debug *ar) {
|
static void
|
||||||
|
lstop(lua_State *L, lua_Debug *ar)
|
||||||
|
{
|
||||||
(void)ar; /* unused arg. */
|
(void)ar; /* unused arg. */
|
||||||
lua_sethook(L, NULL, 0, 0); /* reset hook */
|
lua_sethook(L, NULL, 0, 0); /* reset hook */
|
||||||
luaL_error(L, "interrupted!");
|
luaL_error(L, "interrupted!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Function to be called at a C signal. Because a C signal cannot
|
* Function to be called at a C signal. Because a C signal cannot
|
||||||
* just change a Lua state (as there is no proper synchronization),
|
* just change a Lua state (as there is no proper synchronization),
|
||||||
* this function only sets a hook that, when called, will stop the
|
* this function only sets a hook that, when called, will stop the
|
||||||
* interpreter.
|
* interpreter.
|
||||||
*/
|
*/
|
||||||
static void laction (int i) {
|
static void
|
||||||
|
laction(int i)
|
||||||
|
{
|
||||||
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
|
||||||
setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
|
||||||
lua_sethook(globalL, lstop, flag, 1);
|
lua_sethook(globalL, lstop, flag, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
static void print_usage (const char *badoption) {
|
print_usage(const char *badoption)
|
||||||
|
{
|
||||||
lua_writestringerror("%s: ", progname);
|
lua_writestringerror("%s: ", progname);
|
||||||
if (badoption[1] == 'e' || badoption[1] == 'l')
|
if (badoption[1] == 'e' || badoption[1] == 'l')
|
||||||
lua_writestringerror("'%s' needs argument\n", badoption);
|
lua_writestringerror("'%s' needs argument\n", badoption);
|
||||||
|
@ -83,28 +87,30 @@ static void print_usage (const char *badoption) {
|
||||||
" -E ignore environment variables\n"
|
" -E ignore environment variables\n"
|
||||||
" -W turn warnings on\n"
|
" -W turn warnings on\n"
|
||||||
" -- stop handling options\n"
|
" -- stop handling options\n"
|
||||||
" - stop handling options and execute stdin\n"
|
" - stop handling options and execute stdin\n",
|
||||||
,
|
|
||||||
progname);
|
progname);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prints an error message, adding the program name in front of it
|
* Prints an error message, adding the program name in front of it
|
||||||
* (if present)
|
* (if present)
|
||||||
*/
|
*/
|
||||||
static void l_message (const char *pname, const char *msg) {
|
static void
|
||||||
if (pname) lua_writestringerror("%s: ", pname);
|
l_message(const char *pname, const char *msg)
|
||||||
|
{
|
||||||
|
if (pname)
|
||||||
|
lua_writestringerror("%s: ", pname);
|
||||||
lua_writestringerror("%s\n", msg);
|
lua_writestringerror("%s\n", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check whether 'status' is not OK and, if so, prints the error
|
* Check whether 'status' is not OK and, if so, prints the error
|
||||||
* message on the top of the stack. It assumes that the error object
|
* message on the top of the stack. It assumes that the error object
|
||||||
* is a string, as it was either generated by Lua or by 'msghandler'.
|
* is a string, as it was either generated by Lua or by 'msghandler'.
|
||||||
*/
|
*/
|
||||||
static int report (lua_State *L, int status) {
|
static int
|
||||||
|
report(lua_State *L, int status)
|
||||||
|
{
|
||||||
if (status != LUA_OK) {
|
if (status != LUA_OK) {
|
||||||
const char *msg = lua_tostring(L, -1);
|
const char *msg = lua_tostring(L, -1);
|
||||||
l_message(progname, msg);
|
l_message(progname, msg);
|
||||||
|
@ -113,11 +119,12 @@ static int report (lua_State *L, int status) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Message handler used to run all chunks
|
* Message handler used to run all chunks
|
||||||
*/
|
*/
|
||||||
static int msghandler (lua_State *L) {
|
static int
|
||||||
|
msghandler(lua_State *L)
|
||||||
|
{
|
||||||
const char *msg = lua_tostring(L, 1);
|
const char *msg = lua_tostring(L, 1);
|
||||||
if (msg == NULL) { /* is error object not a string? */
|
if (msg == NULL) { /* is error object not a string? */
|
||||||
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
|
if (luaL_callmeta(L, 1, "__tostring") && /* does it have a metamethod */
|
||||||
|
@ -131,12 +138,13 @@ static int msghandler (lua_State *L) {
|
||||||
return 1; /* return the traceback */
|
return 1; /* return the traceback */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Interface to 'lua_pcall', which sets appropriate message function
|
* Interface to 'lua_pcall', which sets appropriate message function
|
||||||
* and C-signal handler. Used to run all chunks.
|
* and C-signal handler. Used to run all chunks.
|
||||||
*/
|
*/
|
||||||
static int docall (lua_State *L, int narg, int nres) {
|
static int
|
||||||
|
docall(lua_State *L, int narg, int nres)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
int base = lua_gettop(L) - narg; /* function index */
|
int base = lua_gettop(L) - narg; /* function index */
|
||||||
lua_pushcfunction(L, msghandler); /* push message handler */
|
lua_pushcfunction(L, msghandler); /* push message handler */
|
||||||
|
@ -149,13 +157,13 @@ static int docall (lua_State *L, int narg, int nres) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
static void print_version (void) {
|
print_version(void)
|
||||||
|
{
|
||||||
lua_writestring(CALLISTO_COPYRIGHT, strlen(CALLISTO_COPYRIGHT));
|
lua_writestring(CALLISTO_COPYRIGHT, strlen(CALLISTO_COPYRIGHT));
|
||||||
lua_writeline();
|
lua_writeline();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create the 'arg' table, which stores all arguments from the
|
* Create the 'arg' table, which stores all arguments from the
|
||||||
* command line ('argv'). It should be aligned so that, at index 0,
|
* command line ('argv'). It should be aligned so that, at index 0,
|
||||||
|
@ -164,9 +172,12 @@ static void print_version (void) {
|
||||||
* other arguments (before the script name) go to negative indices.
|
* other arguments (before the script name) go to negative indices.
|
||||||
* If there is no script name, assume interpreter's name as base.
|
* If there is no script name, assume interpreter's name as base.
|
||||||
*/
|
*/
|
||||||
static void createargtable (lua_State *L, char **argv, int argc, int script) {
|
static void
|
||||||
|
createargtable(lua_State *L, char **argv, int argc, int script)
|
||||||
|
{
|
||||||
int i, narg;
|
int i, narg;
|
||||||
if (script == argc) script = 0; /* no script name? */
|
if (script == argc)
|
||||||
|
script = 0; /* no script name? */
|
||||||
narg = argc - (script + 1); /* number of positive indices */
|
narg = argc - (script + 1); /* number of positive indices */
|
||||||
lua_createtable(L, narg, script + 1);
|
lua_createtable(L, narg, script + 1);
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
|
@ -176,27 +187,32 @@ static void createargtable (lua_State *L, char **argv, int argc, int script) {
|
||||||
lua_setglobal(L, "arg");
|
lua_setglobal(L, "arg");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
static int dochunk (lua_State *L, int status) {
|
dochunk(lua_State *L, int status)
|
||||||
if (status == LUA_OK) status = docall(L, 0, 0);
|
{
|
||||||
|
if (status == LUA_OK)
|
||||||
|
status = docall(L, 0, 0);
|
||||||
return report(L, status);
|
return report(L, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
static int dofile (lua_State *L, const char *name) {
|
dofile(lua_State *L, const char *name)
|
||||||
|
{
|
||||||
return dochunk(L, luaL_loadfile(L, name));
|
return dochunk(L, luaL_loadfile(L, name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
static int dostring (lua_State *L, const char *s, const char *name) {
|
dostring(lua_State *L, const char *s, const char *name)
|
||||||
|
{
|
||||||
return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
|
return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Receives 'globname[=modname]' and runs 'globname = require(modname)'.
|
* Receives 'globname[=modname]' and runs 'globname = require(modname)'.
|
||||||
*/
|
*/
|
||||||
static int dolibrary (lua_State *L, char *globname) {
|
static int
|
||||||
|
dolibrary(lua_State *L, char *globname)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
char *modname = strchr(globname, '=');
|
char *modname = strchr(globname, '=');
|
||||||
if (modname == NULL) /* no explicit name? */
|
if (modname == NULL) /* no explicit name? */
|
||||||
|
@ -213,11 +229,12 @@ static int dolibrary (lua_State *L, char *globname) {
|
||||||
return report(L, status);
|
return report(L, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Push on the stack the contents of table 'arg' from 1 to #arg
|
* Push on the stack the contents of table 'arg' from 1 to #arg
|
||||||
*/
|
*/
|
||||||
static int pushargs (lua_State *L) {
|
static int
|
||||||
|
pushargs(lua_State *L)
|
||||||
|
{
|
||||||
int i, n;
|
int i, n;
|
||||||
if (lua_getglobal(L, "arg") != LUA_TTABLE)
|
if (lua_getglobal(L, "arg") != LUA_TTABLE)
|
||||||
luaL_error(L, "'arg' is not a table");
|
luaL_error(L, "'arg' is not a table");
|
||||||
|
@ -229,8 +246,9 @@ static int pushargs (lua_State *L) {
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
static int handle_script (lua_State *L, char **argv) {
|
handle_script(lua_State *L, char **argv)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
const char *fname = argv[0];
|
const char *fname = argv[0];
|
||||||
if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
|
if (strcmp(fname, "-") == 0 && strcmp(argv[-1], "--") != 0)
|
||||||
|
@ -243,7 +261,6 @@ static int handle_script (lua_State *L, char **argv) {
|
||||||
return report(L, status);
|
return report(L, status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* bits of various argument indicators in 'args' */
|
/* bits of various argument indicators in 'args' */
|
||||||
#define has_error 1 /* bad option */
|
#define has_error 1 /* bad option */
|
||||||
#define has_i 2 /* -i */
|
#define has_i 2 /* -i */
|
||||||
|
@ -251,14 +268,15 @@ static int handle_script (lua_State *L, char **argv) {
|
||||||
#define has_e 8 /* -e */
|
#define has_e 8 /* -e */
|
||||||
#define has_E 16 /* -E */
|
#define has_E 16 /* -E */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Traverses all arguments from 'argv', returning a mask with those
|
* Traverses all arguments from 'argv', returning a mask with those
|
||||||
* needed before running any Lua code (or an error code if it finds
|
* needed before running any Lua code (or an error code if it finds
|
||||||
* any invalid argument). 'first' returns the first not-handled argument
|
* any invalid argument). 'first' returns the first not-handled argument
|
||||||
* (either the script name or a bad argument in case of error).
|
* (either the script name or a bad argument in case of error).
|
||||||
*/
|
*/
|
||||||
static int collectargs (char **argv, int *first) {
|
static int
|
||||||
|
collectargs(char **argv, int *first)
|
||||||
|
{
|
||||||
int args = 0;
|
int args = 0;
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; argv[i] != NULL; i++) {
|
for (i = 1; argv[i] != NULL; i++) {
|
||||||
|
@ -306,27 +324,30 @@ static int collectargs (char **argv, int *first) {
|
||||||
return args;
|
return args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Processes options 'e' and 'l', which involve running Lua code, and
|
* Processes options 'e' and 'l', which involve running Lua code, and
|
||||||
* 'W', which also affects the state.
|
* 'W', which also affects the state.
|
||||||
* Returns 0 if some code raises an error.
|
* Returns 0 if some code raises an error.
|
||||||
*/
|
*/
|
||||||
static int runargs (lua_State *L, char **argv, int n) {
|
static int
|
||||||
|
runargs(lua_State *L, char **argv, int n)
|
||||||
|
{
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < n; i++) {
|
for (i = 1; i < n; i++) {
|
||||||
int option = argv[i][1];
|
int option = argv[i][1];
|
||||||
lua_assert(argv[i][0] == '-'); /* already checked */
|
lua_assert(argv[i][0] == '-'); /* already checked */
|
||||||
switch (option) {
|
switch (option) {
|
||||||
case 'e': case 'l': {
|
case 'e':
|
||||||
|
case 'l': {
|
||||||
int status;
|
int status;
|
||||||
char *extra = argv[i] + 2; /* both options need an argument */
|
char *extra = argv[i] + 2; /* both options need an argument */
|
||||||
if (*extra == '\0') extra = argv[++i];
|
if (*extra == '\0')
|
||||||
|
extra = argv[++i];
|
||||||
lua_assert(extra != NULL);
|
lua_assert(extra != NULL);
|
||||||
status = (option == 'e')
|
status = (option == 'e') ? dostring(L, extra, "=(command line)")
|
||||||
? dostring(L, extra, "=(command line)")
|
|
||||||
: dolibrary(L, extra);
|
: dolibrary(L, extra);
|
||||||
if (status != LUA_OK) return 0;
|
if (status != LUA_OK)
|
||||||
|
return 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'W':
|
case 'W':
|
||||||
|
@ -337,22 +358,23 @@ static int runargs (lua_State *L, char **argv, int n) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
static int handle_luainit (lua_State *L) {
|
handle_luainit(lua_State *L)
|
||||||
|
{
|
||||||
const char *name = "=" LUA_INITVARVERSION;
|
const char *name = "=" LUA_INITVARVERSION;
|
||||||
const char *init = getenv(name + 1);
|
const char *init = getenv(name + 1);
|
||||||
if (init == NULL) {
|
if (init == NULL) {
|
||||||
name = "=" LUA_INIT_VAR;
|
name = "=" LUA_INIT_VAR;
|
||||||
init = getenv(name + 1); /* try alternative name */
|
init = getenv(name + 1); /* try alternative name */
|
||||||
}
|
}
|
||||||
if (init == NULL) return LUA_OK;
|
if (init == NULL)
|
||||||
|
return LUA_OK;
|
||||||
else if (init[0] == '@')
|
else if (init[0] == '@')
|
||||||
return dofile(L, init + 1);
|
return dofile(L, init + 1);
|
||||||
else
|
else
|
||||||
return dostring(L, init, name);
|
return dostring(L, init, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* {==================================================================
|
* {==================================================================
|
||||||
* Read-Eval-Print Loop (REPL)
|
* Read-Eval-Print Loop (REPL)
|
||||||
|
@ -368,7 +390,6 @@ static int handle_luainit (lua_State *L) {
|
||||||
#define LUA_MAXINPUT 512
|
#define LUA_MAXINPUT 512
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* lua_stdin_is_tty detects whether the standard input is a 'tty' (that
|
* lua_stdin_is_tty detects whether the standard input is a 'tty' (that
|
||||||
* is, whether we're running lua interactively).
|
* is, whether we're running lua interactively).
|
||||||
|
@ -379,7 +400,6 @@ static int handle_luainit (lua_State *L) {
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* lua_readline defines how to show a prompt and then read a line from
|
* lua_readline defines how to show a prompt and then read a line from
|
||||||
* the standard input.
|
* the standard input.
|
||||||
|
@ -390,8 +410,8 @@ static int handle_luainit (lua_State *L) {
|
||||||
|
|
||||||
#if defined(LUA_USE_READLINE) /* { */
|
#if defined(LUA_USE_READLINE) /* { */
|
||||||
|
|
||||||
#include <readline/readline.h>
|
|
||||||
#include <readline/history.h>
|
#include <readline/history.h>
|
||||||
|
#include <readline/readline.h>
|
||||||
#define lua_initreadline(L) ((void)L, rl_readline_name = "lua")
|
#define lua_initreadline(L) ((void)L, rl_readline_name = "lua")
|
||||||
#define lua_readline(L, b, p) ((void)L, ((b) = readline(p)) != NULL)
|
#define lua_readline(L, b, p) ((void)L, ((b) = readline(p)) != NULL)
|
||||||
#define lua_saveline(L, line) ((void)L, add_history(line))
|
#define lua_saveline(L, line) ((void)L, add_history(line))
|
||||||
|
@ -414,13 +434,14 @@ static int handle_luainit (lua_State *L) {
|
||||||
|
|
||||||
#endif /* } */
|
#endif /* } */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return the string to be used as a prompt by the interpreter. Leave
|
* Return the string to be used as a prompt by the interpreter. Leave
|
||||||
* the string (or nil, if using the default value) on the stack, to keep
|
* the string (or nil, if using the default value) on the stack, to keep
|
||||||
* it anchored.
|
* it anchored.
|
||||||
*/
|
*/
|
||||||
static const char *get_prompt (lua_State *L, int firstline) {
|
static const char *
|
||||||
|
get_prompt(lua_State *L, int firstline)
|
||||||
|
{
|
||||||
if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
|
if (lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2") == LUA_TNIL)
|
||||||
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
|
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
|
||||||
else { /* apply 'tostring' over the value */
|
else { /* apply 'tostring' over the value */
|
||||||
|
@ -434,13 +455,14 @@ static const char *get_prompt (lua_State *L, int firstline) {
|
||||||
#define EOFMARK "<eof>"
|
#define EOFMARK "<eof>"
|
||||||
#define marklen (sizeof(EOFMARK) / sizeof(char) - 1)
|
#define marklen (sizeof(EOFMARK) / sizeof(char) - 1)
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check whether 'status' signals a syntax error and the error
|
* Check whether 'status' signals a syntax error and the error
|
||||||
* message at the top of the stack ends with the above mark for
|
* message at the top of the stack ends with the above mark for
|
||||||
* incomplete statements.
|
* incomplete statements.
|
||||||
*/
|
*/
|
||||||
static int incomplete (lua_State *L, int status) {
|
static int
|
||||||
|
incomplete(lua_State *L, int status)
|
||||||
|
{
|
||||||
if (status == LUA_ERRSYNTAX) {
|
if (status == LUA_ERRSYNTAX) {
|
||||||
size_t lmsg;
|
size_t lmsg;
|
||||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||||
|
@ -452,11 +474,12 @@ static int incomplete (lua_State *L, int status) {
|
||||||
return 0; /* else... */
|
return 0; /* else... */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prompt the user, read a line, and push it into the Lua stack.
|
* Prompt the user, read a line, and push it into the Lua stack.
|
||||||
*/
|
*/
|
||||||
static int pushline (lua_State *L, int firstline) {
|
static int
|
||||||
|
pushline(lua_State *L, int firstline)
|
||||||
|
{
|
||||||
char buffer[LUA_MAXINPUT];
|
char buffer[LUA_MAXINPUT];
|
||||||
char *b = buffer;
|
char *b = buffer;
|
||||||
size_t l;
|
size_t l;
|
||||||
|
@ -476,12 +499,13 @@ static int pushline (lua_State *L, int firstline) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Try to compile line on the stack as 'return <line>;'; on return, stack
|
* Try to compile line on the stack as 'return <line>;'; on return, stack
|
||||||
* has either compiled chunk or original line (if compilation failed).
|
* has either compiled chunk or original line (if compilation failed).
|
||||||
*/
|
*/
|
||||||
static int addreturn (lua_State *L) {
|
static int
|
||||||
|
addreturn(lua_State *L)
|
||||||
|
{
|
||||||
const char *line = lua_tostring(L, -1); /* original line */
|
const char *line = lua_tostring(L, -1); /* original line */
|
||||||
const char *retline = lua_pushfstring(L, "return %s;", line);
|
const char *retline = lua_pushfstring(L, "return %s;", line);
|
||||||
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
int status = luaL_loadbuffer(L, retline, strlen(retline), "=stdin");
|
||||||
|
@ -489,17 +513,17 @@ static int addreturn (lua_State *L) {
|
||||||
lua_remove(L, -2); /* remove modified line */
|
lua_remove(L, -2); /* remove modified line */
|
||||||
if (line[0] != '\0') /* non empty? */
|
if (line[0] != '\0') /* non empty? */
|
||||||
lua_saveline(L, line); /* keep history */
|
lua_saveline(L, line); /* keep history */
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read multiple lines until a complete Lua statement
|
* Read multiple lines until a complete Lua statement
|
||||||
*/
|
*/
|
||||||
static int multiline (lua_State *L) {
|
static int
|
||||||
|
multiline(lua_State *L)
|
||||||
|
{
|
||||||
for (;;) { /* repeat until gets a complete statement */
|
for (;;) { /* repeat until gets a complete statement */
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
const char *line = lua_tolstring(L, 1, &len); /* get what it has */
|
||||||
|
@ -514,14 +538,15 @@ static int multiline (lua_State *L) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Read a line and try to load (compile) it first as an expression (by
|
* Read a line and try to load (compile) it first as an expression (by
|
||||||
* adding "return " in front of it) and second as a statement. Return
|
* adding "return " in front of it) and second as a statement. Return
|
||||||
* the final status of load/call with the resulting function (if any)
|
* the final status of load/call with the resulting function (if any)
|
||||||
* in the top of the stack.
|
* in the top of the stack.
|
||||||
*/
|
*/
|
||||||
static int loadline (lua_State *L) {
|
static int
|
||||||
|
loadline(lua_State *L)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
lua_settop(L, 0);
|
lua_settop(L, 0);
|
||||||
if (!pushline(L, 1))
|
if (!pushline(L, 1))
|
||||||
|
@ -533,28 +558,31 @@ static int loadline (lua_State *L) {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Prints (calling the Lua 'print' function) any values on the stack
|
* Prints (calling the Lua 'print' function) any values on the stack
|
||||||
*/
|
*/
|
||||||
static void l_print (lua_State *L) {
|
static void
|
||||||
|
l_print(lua_State *L)
|
||||||
|
{
|
||||||
int n = lua_gettop(L);
|
int n = lua_gettop(L);
|
||||||
if (n > 0) { /* any result to be printed? */
|
if (n > 0) { /* any result to be printed? */
|
||||||
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
|
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
|
||||||
lua_getglobal(L, "print");
|
lua_getglobal(L, "print");
|
||||||
lua_insert(L, 1);
|
lua_insert(L, 1);
|
||||||
if (lua_pcall(L, n, 0, 0) != LUA_OK)
|
if (lua_pcall(L, n, 0, 0) != LUA_OK)
|
||||||
l_message(progname, lua_pushfstring(L, "error calling 'print' (%s)",
|
l_message(progname,
|
||||||
|
lua_pushfstring(L, "error calling 'print' (%s)",
|
||||||
lua_tostring(L, -1)));
|
lua_tostring(L, -1)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
|
* Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
|
||||||
* print any results.
|
* print any results.
|
||||||
*/
|
*/
|
||||||
static void doREPL (lua_State *L) {
|
static void
|
||||||
|
doREPL(lua_State *L)
|
||||||
|
{
|
||||||
int status;
|
int status;
|
||||||
const char *oldprogname = progname;
|
const char *oldprogname = progname;
|
||||||
progname = NULL; /* no 'progname' on errors in interactive mode */
|
progname = NULL; /* no 'progname' on errors in interactive mode */
|
||||||
|
@ -562,8 +590,10 @@ static void doREPL (lua_State *L) {
|
||||||
while ((status = loadline(L)) != -1) {
|
while ((status = loadline(L)) != -1) {
|
||||||
if (status == LUA_OK)
|
if (status == LUA_OK)
|
||||||
status = docall(L, 0, LUA_MULTRET);
|
status = docall(L, 0, LUA_MULTRET);
|
||||||
if (status == LUA_OK) l_print(L);
|
if (status == LUA_OK)
|
||||||
else report(L, status);
|
l_print(L);
|
||||||
|
else
|
||||||
|
report(L, status);
|
||||||
}
|
}
|
||||||
lua_settop(L, 0); /* clear stack */
|
lua_settop(L, 0); /* clear stack */
|
||||||
lua_writeline();
|
lua_writeline();
|
||||||
|
@ -572,18 +602,20 @@ static void doREPL (lua_State *L) {
|
||||||
|
|
||||||
/* }================================================================== */
|
/* }================================================================== */
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Main body of stand-alone interpreter (to be called in protected mode).
|
* Main body of stand-alone interpreter (to be called in protected mode).
|
||||||
* Reads the options and handles them all.
|
* Reads the options and handles them all.
|
||||||
*/
|
*/
|
||||||
static int pmain (lua_State *L) {
|
static int
|
||||||
|
pmain(lua_State *L)
|
||||||
|
{
|
||||||
int argc = (int)lua_tointeger(L, 1);
|
int argc = (int)lua_tointeger(L, 1);
|
||||||
char **argv = (char **)lua_touserdata(L, 2);
|
char **argv = (char **)lua_touserdata(L, 2);
|
||||||
int script;
|
int script;
|
||||||
int args = collectargs(argv, &script);
|
int args = collectargs(argv, &script);
|
||||||
luaL_checkversion(L); /* check that interpreter has correct version */
|
luaL_checkversion(L); /* check that interpreter has correct version */
|
||||||
if (argv[0] && argv[0][0]) progname = argv[0];
|
if (argv[0] && argv[0][0])
|
||||||
|
progname = argv[0];
|
||||||
if (args == has_error) { /* bad arg? */
|
if (args == has_error) { /* bad arg? */
|
||||||
print_usage(argv[script]); /* 'script' has index of bad arg. */
|
print_usage(argv[script]); /* 'script' has index of bad arg. */
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -611,15 +643,16 @@ static int pmain (lua_State *L) {
|
||||||
if (lua_stdin_is_tty()) { /* running in interactive mode? */
|
if (lua_stdin_is_tty()) { /* running in interactive mode? */
|
||||||
print_version();
|
print_version();
|
||||||
doREPL(L); /* do read-eval-print loop */
|
doREPL(L); /* do read-eval-print loop */
|
||||||
}
|
} else
|
||||||
else dofile(L, NULL); /* executes stdin as a file */
|
dofile(L, NULL); /* executes stdin as a file */
|
||||||
}
|
}
|
||||||
lua_pushboolean(L, 1); /* signal no errors */
|
lua_pushboolean(L, 1); /* signal no errors */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
int main (int argc, char **argv) {
|
main(int argc, char **argv)
|
||||||
|
{
|
||||||
int status, result;
|
int status, result;
|
||||||
lua_State *L = callisto_newstate(); /* create state */
|
lua_State *L = callisto_newstate(); /* create state */
|
||||||
if (L == NULL) {
|
if (L == NULL) {
|
||||||
|
@ -635,4 +668,3 @@ int main (int argc, char **argv) {
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
|
return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
6
lcl.c
6
lcl.c
|
@ -29,7 +29,8 @@ fmesgshift(lua_State *L, int fd, int shift)
|
||||||
|
|
||||||
lua_getglobal(L, "arg");
|
lua_getglobal(L, "arg");
|
||||||
if (!lua_istable(L, -1)) {
|
if (!lua_istable(L, -1)) {
|
||||||
luaL_error(L, "arg table not present/inaccessible; cannot get script name");
|
luaL_error(L,
|
||||||
|
"arg table not present/inaccessible; cannot get script name");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,7 +247,8 @@ cl_options(lua_State *L)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (optstring[0] == ':')
|
if (optstring[0] == ':')
|
||||||
return luaL_argerror(L, 2, "option string must not start with a colon (:)");
|
return luaL_argerror(L, 2,
|
||||||
|
"option string must not start with a colon (:)");
|
||||||
|
|
||||||
strprepend(optstring, ":");
|
strprepend(optstring, ":");
|
||||||
|
|
||||||
|
|
1
ljson.c
1
ljson.c
|
@ -217,7 +217,6 @@ local t = json.decode(j)
|
||||||
* sparse; *>1*: use ratio
|
* sparse; *>1*: use ratio
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
int luaopen_cjson(lua_State *);
|
int luaopen_cjson(lua_State *);
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue