tree: reformat

Done automatically by clang-format
This commit is contained in:
Jeremy Baxter 2024-03-12 12:21:39 +13:00
parent 669eb3fd28
commit 30a1097bb0
8 changed files with 311 additions and 278 deletions

200
csto.c
View file

@ -4,10 +4,10 @@
* Copyright (c) 2023-2024 Jeremy Baxter.
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <lua/lauxlib.h>
@ -30,11 +30,12 @@ static lua_State *globalL = NULL;
static const char *progname = LUA_PROGNAME;
/*
* Use 'sigaction' when available.
*/
static void setsignal (int sig, void (*handler)(int)) {
static void
setsignal(int sig, void (*handler)(int))
{
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_flags = 0;
@ -42,31 +43,34 @@ static void setsignal (int sig, void (*handler)(int)) {
sigaction(sig, &sa, NULL);
}
/*
* 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. */
lua_sethook(L, NULL, 0, 0); /* reset hook */
luaL_error(L, "interrupted!");
}
/*
* Function to be called at a C signal. Because a C signal cannot
* just change a Lua state (as there is no proper synchronization),
* this function only sets a hook that, when called, will stop the
* interpreter.
*/
static void laction (int i) {
static void
laction(int i)
{
int flag = LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT;
setsignal(i, SIG_DFL); /* if another SIGINT happens, terminate process */
lua_sethook(globalL, lstop, flag, 1);
}
static void print_usage (const char *badoption) {
static void
print_usage(const char *badoption)
{
lua_writestringerror("%s: ", progname);
if (badoption[1] == 'e' || badoption[1] == 'l')
lua_writestringerror("'%s' needs argument\n", badoption);
@ -83,28 +87,30 @@ static void print_usage (const char *badoption) {
" -E ignore environment variables\n"
" -W turn warnings on\n"
" -- stop handling options\n"
" - stop handling options and execute stdin\n"
,
" - stop handling options and execute stdin\n",
progname);
}
/*
* Prints an error message, adding the program name in front of it
* (if present)
*/
static void l_message (const char *pname, const char *msg) {
if (pname) lua_writestringerror("%s: ", pname);
static void
l_message(const char *pname, const char *msg)
{
if (pname)
lua_writestringerror("%s: ", pname);
lua_writestringerror("%s\n", msg);
}
/*
* 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
* 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) {
const char *msg = lua_tostring(L, -1);
l_message(progname, msg);
@ -113,11 +119,12 @@ static int report (lua_State *L, int status) {
return status;
}
/*
* 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);
if (msg == NULL) { /* is error object not a string? */
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 */
}
/*
* Interface to 'lua_pcall', which sets appropriate message function
* 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 base = lua_gettop(L) - narg; /* function index */
lua_pushcfunction(L, msghandler); /* push message handler */
@ -149,13 +157,13 @@ static int docall (lua_State *L, int narg, int nres) {
return status;
}
static void print_version (void) {
static void
print_version(void)
{
lua_writestring(CALLISTO_COPYRIGHT, strlen(CALLISTO_COPYRIGHT));
lua_writeline();
}
/*
* Create the 'arg' table, which stores all arguments from the
* 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.
* 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;
if (script == argc) script = 0; /* no script name? */
if (script == argc)
script = 0; /* no script name? */
narg = argc - (script + 1); /* number of positive indices */
lua_createtable(L, narg, script + 1);
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");
}
static int dochunk (lua_State *L, int status) {
if (status == LUA_OK) status = docall(L, 0, 0);
static int
dochunk(lua_State *L, int status)
{
if (status == LUA_OK)
status = docall(L, 0, 0);
return report(L, status);
}
static int dofile (lua_State *L, const char *name) {
static int
dofile(lua_State *L, const char *name)
{
return dochunk(L, luaL_loadfile(L, name));
}
static int dostring (lua_State *L, const char *s, const char *name) {
static int
dostring(lua_State *L, const char *s, const char *name)
{
return dochunk(L, luaL_loadbuffer(L, s, strlen(s), name));
}
/*
* 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;
char *modname = strchr(globname, '=');
if (modname == NULL) /* no explicit name? */
@ -213,11 +229,12 @@ static int dolibrary (lua_State *L, char *globname) {
return report(L, status);
}
/*
* 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;
if (lua_getglobal(L, "arg") != LUA_TTABLE)
luaL_error(L, "'arg' is not a table");
@ -229,8 +246,9 @@ static int pushargs (lua_State *L) {
return n;
}
static int handle_script (lua_State *L, char **argv) {
static int
handle_script(lua_State *L, char **argv)
{
int status;
const char *fname = argv[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);
}
/* bits of various argument indicators in 'args' */
#define has_error 1 /* bad option */
#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 16 /* -E */
/*
* Traverses all arguments from 'argv', returning a mask with those
* needed before running any Lua code (or an error code if it finds
* any invalid argument). 'first' returns the first not-handled argument
* (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 i;
for (i = 1; argv[i] != NULL; i++) {
@ -306,27 +324,30 @@ static int collectargs (char **argv, int *first) {
return args;
}
/*
* Processes options 'e' and 'l', which involve running Lua code, and
* 'W', which also affects the state.
* 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;
for (i = 1; i < n; i++) {
int option = argv[i][1];
lua_assert(argv[i][0] == '-'); /* already checked */
switch (option) {
case 'e': case 'l': {
case 'e':
case 'l': {
int status;
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);
status = (option == 'e')
? dostring(L, extra, "=(command line)")
status = (option == 'e') ? dostring(L, extra, "=(command line)")
: dolibrary(L, extra);
if (status != LUA_OK) return 0;
if (status != LUA_OK)
return 0;
break;
}
case 'W':
@ -337,22 +358,23 @@ static int runargs (lua_State *L, char **argv, int n) {
return 1;
}
static int handle_luainit (lua_State *L) {
static int
handle_luainit(lua_State *L)
{
const char *name = "=" LUA_INITVARVERSION;
const char *init = getenv(name + 1);
if (init == NULL) {
name = "=" LUA_INIT_VAR;
init = getenv(name + 1); /* try alternative name */
}
if (init == NULL) return LUA_OK;
if (init == NULL)
return LUA_OK;
else if (init[0] == '@')
return dofile(L, init + 1);
else
return dostring(L, init, name);
}
/*
* {==================================================================
* Read-Eval-Print Loop (REPL)
@ -368,7 +390,6 @@ static int handle_luainit (lua_State *L) {
#define LUA_MAXINPUT 512
#endif
/*
* lua_stdin_is_tty detects whether the standard input is a 'tty' (that
* is, whether we're running lua interactively).
@ -379,7 +400,6 @@ static int handle_luainit (lua_State *L) {
#endif /* } */
/*
* lua_readline defines how to show a prompt and then read a line from
* the standard input.
@ -390,8 +410,8 @@ static int handle_luainit (lua_State *L) {
#if defined(LUA_USE_READLINE) /* { */
#include <readline/readline.h>
#include <readline/history.h>
#include <readline/readline.h>
#define lua_initreadline(L) ((void)L, rl_readline_name = "lua")
#define lua_readline(L, b, p) ((void)L, ((b) = readline(p)) != NULL)
#define lua_saveline(L, line) ((void)L, add_history(line))
@ -414,13 +434,14 @@ static int handle_luainit (lua_State *L) {
#endif /* } */
/*
* 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
* 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)
return (firstline ? LUA_PROMPT : LUA_PROMPT2); /* use the default */
else { /* apply 'tostring' over the value */
@ -434,13 +455,14 @@ static const char *get_prompt (lua_State *L, int firstline) {
#define EOFMARK "<eof>"
#define marklen (sizeof(EOFMARK) / sizeof(char) - 1)
/*
* Check whether 'status' signals a syntax error and the error
* message at the top of the stack ends with the above mark for
* incomplete statements.
*/
static int incomplete (lua_State *L, int status) {
static int
incomplete(lua_State *L, int status)
{
if (status == LUA_ERRSYNTAX) {
size_t lmsg;
const char *msg = lua_tolstring(L, -1, &lmsg);
@ -452,11 +474,12 @@ static int incomplete (lua_State *L, int status) {
return 0; /* else... */
}
/*
* 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 *b = buffer;
size_t l;
@ -476,12 +499,13 @@ static int pushline (lua_State *L, int firstline) {
return 1;
}
/*
* Try to compile line on the stack as 'return <line>;'; on return, stack
* 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 *retline = lua_pushfstring(L, "return %s;", line);
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 */
if (line[0] != '\0') /* non empty? */
lua_saveline(L, line); /* keep history */
}
else
} else
lua_pop(L, 2); /* pop result from 'luaL_loadbuffer' and modified line */
return status;
}
/*
* 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 */
size_t len;
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
* adding "return " in front of it) and second as a statement. Return
* the final status of load/call with the resulting function (if any)
* in the top of the stack.
*/
static int loadline (lua_State *L) {
static int
loadline(lua_State *L)
{
int status;
lua_settop(L, 0);
if (!pushline(L, 1))
@ -533,28 +558,31 @@ static int loadline (lua_State *L) {
return status;
}
/*
* 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);
if (n > 0) { /* any result to be printed? */
luaL_checkstack(L, LUA_MINSTACK, "too many results to print");
lua_getglobal(L, "print");
lua_insert(L, 1);
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)));
}
}
/*
* Do the REPL: repeatedly read (load) a line, evaluate (call) it, and
* print any results.
*/
static void doREPL (lua_State *L) {
static void
doREPL(lua_State *L)
{
int status;
const char *oldprogname = progname;
progname = NULL; /* no 'progname' on errors in interactive mode */
@ -562,8 +590,10 @@ static void doREPL (lua_State *L) {
while ((status = loadline(L)) != -1) {
if (status == LUA_OK)
status = docall(L, 0, LUA_MULTRET);
if (status == LUA_OK) l_print(L);
else report(L, status);
if (status == LUA_OK)
l_print(L);
else
report(L, status);
}
lua_settop(L, 0); /* clear stack */
lua_writeline();
@ -572,18 +602,20 @@ static void doREPL (lua_State *L) {
/* }================================================================== */
/*
* Main body of stand-alone interpreter (to be called in protected mode).
* 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);
char **argv = (char **)lua_touserdata(L, 2);
int script;
int args = collectargs(argv, &script);
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? */
print_usage(argv[script]); /* 'script' has index of bad arg. */
return 0;
@ -611,15 +643,16 @@ static int pmain (lua_State *L) {
if (lua_stdin_is_tty()) { /* running in interactive mode? */
print_version();
doREPL(L); /* do read-eval-print loop */
}
else dofile(L, NULL); /* executes stdin as a file */
} else
dofile(L, NULL); /* executes stdin as a file */
}
lua_pushboolean(L, 1); /* signal no errors */
return 1;
}
int main (int argc, char **argv) {
int
main(int argc, char **argv)
{
int status, result;
lua_State *L = callisto_newstate(); /* create state */
if (L == NULL) {
@ -635,4 +668,3 @@ int main (int argc, char **argv) {
lua_close(L);
return (result && status == LUA_OK) ? EXIT_SUCCESS : EXIT_FAILURE;
}

6
lcl.c
View file

@ -29,7 +29,8 @@ fmesgshift(lua_State *L, int fd, int shift)
lua_getglobal(L, "arg");
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;
}
@ -246,7 +247,8 @@ cl_options(lua_State *L)
}
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, ":");

View file

@ -217,7 +217,6 @@ local t = json.decode(j)
* sparse; *>1*: use ratio
*/
int luaopen_cjson(lua_State *);
int