mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
Add /.args
feature to Redbean/Lua/SQLite/Python/QuickJS
You now have some ability to truly make an executable yours, by adding a `.args` file to the root of the zip structure. If this is specified, then you'll be overriding the default CLI args. This will be a great feature for folks who want to distribute their own apps, using the interpreter executable, but have the executable appears to be just your app rather than being the interpreter.
This commit is contained in:
parent
0f6251f4d2
commit
4499f98e76
19 changed files with 421 additions and 19 deletions
2
Makefile
2
Makefile
|
@ -140,6 +140,8 @@ include third_party/regex/regex.mk #─┘
|
|||
include third_party/third_party.mk
|
||||
include libc/testlib/testlib.mk
|
||||
include tool/viz/lib/vizlib.mk
|
||||
include tool/args/args.mk
|
||||
include test/tool/args/test.mk
|
||||
include third_party/linenoise/linenoise.mk
|
||||
include third_party/maxmind/maxmind.mk
|
||||
include third_party/lua/lua.mk
|
||||
|
|
101
test/tool/args/args_test.c
Normal file
101
test/tool/args/args_test.c
Normal file
|
@ -0,0 +1,101 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "tool/args/args.h"
|
||||
|
||||
void FreeZipArgs(void);
|
||||
int LoadZipArgsImpl(int *, char ***, char *);
|
||||
|
||||
void TearDown(void) {
|
||||
FreeZipArgs();
|
||||
}
|
||||
|
||||
TEST(LoadZipArgs, testNoFile_noCliArgs_doesNothing) {
|
||||
int argc = 1;
|
||||
char *args[] = {"prog", 0};
|
||||
char **argv = &args[0];
|
||||
EXPECT_EQ(0, LoadZipArgsImpl(&argc, &argv, 0));
|
||||
ASSERT_EQ(1, argc);
|
||||
EXPECT_STREQ("prog", argv[0]);
|
||||
EXPECT_EQ(NULL, argv[1]);
|
||||
}
|
||||
|
||||
TEST(LoadZipArgs, testDefaultArgs_noCliArgs_replacesArgv) {
|
||||
int argc = 1;
|
||||
char *args[] = {"prog", 0};
|
||||
char **argv = &args[0];
|
||||
EXPECT_EQ(0, LoadZipArgsImpl(&argc, &argv, strdup("\
|
||||
-x\r\n\
|
||||
hello\r\n\
|
||||
-y\r\n\
|
||||
world\r\n\
|
||||
")));
|
||||
ASSERT_EQ(5, argc);
|
||||
EXPECT_STREQ("prog", argv[0]);
|
||||
EXPECT_STREQ("-x", argv[1]);
|
||||
EXPECT_STREQ("hello", argv[2]);
|
||||
EXPECT_STREQ("-y", argv[3]);
|
||||
EXPECT_STREQ("world", argv[4]);
|
||||
EXPECT_EQ(NULL, argv[5]);
|
||||
EXPECT_EQ(5, __argc);
|
||||
EXPECT_STREQ("prog", __argv[0]);
|
||||
EXPECT_STREQ("-x", __argv[1]);
|
||||
EXPECT_STREQ("hello", __argv[2]);
|
||||
EXPECT_STREQ("-y", __argv[3]);
|
||||
EXPECT_STREQ("world", __argv[4]);
|
||||
EXPECT_EQ(NULL, __argv[5]);
|
||||
}
|
||||
|
||||
TEST(LoadZipArgs, testDefaultArgs_hasCliArgs_doesNothing) {
|
||||
int argc = 2;
|
||||
char *args[] = {"prog", "yo", 0};
|
||||
char **argv = &args[0];
|
||||
EXPECT_EQ(0, LoadZipArgsImpl(&argc, &argv, strdup("\
|
||||
-x\r\n\
|
||||
hello\r\n\
|
||||
-y\r\n\
|
||||
world\r\n\
|
||||
")));
|
||||
ASSERT_EQ(2, argc);
|
||||
EXPECT_STREQ("prog", argv[0]);
|
||||
EXPECT_STREQ("yo", argv[1]);
|
||||
EXPECT_EQ(NULL, argv[2]);
|
||||
}
|
||||
|
||||
TEST(LoadZipArgs, testDots_hasCliArgs_mergesThem) {
|
||||
int argc = 3;
|
||||
char *args[] = {"prog", "yo", "dawg", 0};
|
||||
char **argv = &args[0];
|
||||
EXPECT_EQ(0, LoadZipArgsImpl(&argc, &argv, strdup("\
|
||||
-x\r\n\
|
||||
hell o\r\n\
|
||||
-y\r\n\
|
||||
world\r\n\
|
||||
...\r\n\
|
||||
")));
|
||||
ASSERT_EQ(7, argc);
|
||||
EXPECT_STREQ("prog", argv[0]);
|
||||
EXPECT_STREQ("-x", argv[1]);
|
||||
EXPECT_STREQ("hell o", argv[2]);
|
||||
EXPECT_STREQ("-y", argv[3]);
|
||||
EXPECT_STREQ("world", argv[4]);
|
||||
EXPECT_STREQ("yo", argv[5]);
|
||||
EXPECT_STREQ("dawg", argv[6]);
|
||||
EXPECT_EQ(NULL, argv[7]);
|
||||
}
|
75
test/tool/args/test.mk
Normal file
75
test/tool/args/test.mk
Normal file
|
@ -0,0 +1,75 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
|
||||
|
||||
PKGS += TEST_TOOL_ARGS
|
||||
|
||||
TEST_TOOL_ARGS = $(TOOL_ARGS_A_DEPS) $(TOOL_ARGS_A)
|
||||
TEST_TOOL_ARGS_A = o/$(MODE)/test/tool/args/argstest.a
|
||||
TEST_TOOL_ARGS_FILES := $(wildcard test/tool/args/*)
|
||||
TEST_TOOL_ARGS_SRCS = $(filter %.c,$(TEST_TOOL_ARGS_FILES))
|
||||
TEST_TOOL_ARGS_SRCS_TEST = $(filter %_test.c,$(TEST_TOOL_ARGS_SRCS))
|
||||
TEST_TOOL_ARGS_HDRS = $(filter %.h,$(TEST_TOOL_ARGS_FILES))
|
||||
TEST_TOOL_ARGS_COMS = $(TEST_TOOL_ARGS_OBJS:%.o=%.com)
|
||||
|
||||
TEST_TOOL_ARGS_OBJS = \
|
||||
$(TEST_TOOL_ARGS_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TEST_TOOL_ARGS_COMS = \
|
||||
$(TEST_TOOL_ARGS_SRCS:%.c=o/$(MODE)/%.com)
|
||||
|
||||
TEST_TOOL_ARGS_BINS = \
|
||||
$(TEST_TOOL_ARGS_COMS) \
|
||||
$(TEST_TOOL_ARGS_COMS:%=%.dbg)
|
||||
|
||||
TEST_TOOL_ARGS_TESTS = \
|
||||
$(TEST_TOOL_ARGS_SRCS_TEST:%.c=o/$(MODE)/%.com.ok)
|
||||
|
||||
TEST_TOOL_ARGS_CHECKS = \
|
||||
$(TEST_TOOL_ARGS_HDRS:%=o/$(MODE)/%.ok) \
|
||||
$(TEST_TOOL_ARGS_SRCS_TEST:%.c=o/$(MODE)/%.com.runs)
|
||||
|
||||
TEST_TOOL_ARGS_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
LIBC_LOG \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_SYSV \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_UNICODE \
|
||||
LIBC_ZIPOS \
|
||||
LIBC_X \
|
||||
THIRD_PARTY_COMPILER_RT \
|
||||
TOOL_ARGS
|
||||
|
||||
TEST_TOOL_ARGS_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_TOOL_ARGS_DIRECTDEPS),$($(x))))
|
||||
|
||||
$(TEST_TOOL_ARGS_A): \
|
||||
test/tool/args/ \
|
||||
$(TEST_TOOL_ARGS_A).pkg \
|
||||
$(TEST_TOOL_ARGS_OBJS)
|
||||
|
||||
$(TEST_TOOL_ARGS_A).pkg: \
|
||||
$(TEST_TOOL_ARGS_OBJS) \
|
||||
$(foreach x,$(TEST_TOOL_ARGS_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
o/$(MODE)/test/tool/args/%.com.dbg: \
|
||||
$(TEST_TOOL_ARGS_DEPS) \
|
||||
$(TEST_TOOL_ARGS_A) \
|
||||
o/$(MODE)/test/tool/args/%.o \
|
||||
$(TEST_TOOL_ARGS_A).pkg \
|
||||
$(LIBC_TESTMAIN) \
|
||||
$(CRT) \
|
||||
$(APE)
|
||||
@$(APELINK)
|
||||
|
||||
.PHONY: o/$(MODE)/test/tool/args
|
||||
o/$(MODE)/test/tool/args: \
|
||||
$(TEST_TOOL_ARGS_BINS) \
|
||||
$(TEST_TOOL_ARGS_CHECKS)
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
.PHONY: o/$(MODE)/test/tool
|
||||
o/$(MODE)/test/tool: \
|
||||
o/$(MODE)/test/tool/args \
|
||||
o/$(MODE)/test/tool/build \
|
||||
o/$(MODE)/test/tool/plinko \
|
||||
o/$(MODE)/test/tool/net \
|
||||
|
|
4
third_party/lua/lua.main.c
vendored
4
third_party/lua/lua.main.c
vendored
|
@ -48,6 +48,7 @@
|
|||
#include "third_party/lua/lrepl.h"
|
||||
#include "third_party/lua/lua.h"
|
||||
#include "third_party/lua/lualib.h"
|
||||
#include "tool/args/args.h"
|
||||
// clang-format off
|
||||
|
||||
asm(".ident\t\"\\n\\n\
|
||||
|
@ -388,8 +389,9 @@ static int pmain (lua_State *L) {
|
|||
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int status, result;
|
||||
lua_State *L;
|
||||
int status, result;
|
||||
LoadZipArgs(&argc, &argv);
|
||||
if (IsModeDbg()) {
|
||||
ShowCrashReports();
|
||||
}
|
||||
|
|
3
third_party/lua/lua.mk
vendored
3
third_party/lua/lua.mk
vendored
|
@ -38,7 +38,8 @@ THIRD_PARTY_LUA_DIRECTDEPS = \
|
|||
LIBC_UNICODE \
|
||||
NET_HTTP \
|
||||
THIRD_PARTY_LINENOISE \
|
||||
THIRD_PARTY_GDTOA
|
||||
THIRD_PARTY_GDTOA \
|
||||
TOOL_ARGS
|
||||
|
||||
THIRD_PARTY_LUA_DEPS := \
|
||||
$(call uniq,$(foreach x,$(THIRD_PARTY_LUA_DIRECTDEPS),$($(x))))
|
||||
|
|
12
third_party/python/python.mk
vendored
12
third_party/python/python.mk
vendored
|
@ -461,7 +461,8 @@ THIRD_PARTY_PYTHON_STAGE1_A_DIRECTDEPS = \
|
|||
TOOL_BUILD_LIB \
|
||||
THIRD_PARTY_DLMALLOC \
|
||||
THIRD_PARTY_GETOPT \
|
||||
THIRD_PARTY_XED
|
||||
THIRD_PARTY_XED \
|
||||
TOOL_ARGS
|
||||
|
||||
THIRD_PARTY_PYTHON_STAGE1_A_DEPS = \
|
||||
$(call uniq,$(foreach x,$(THIRD_PARTY_PYTHON_STAGE1_A_DIRECTDEPS),$($(x))))
|
||||
|
@ -1149,7 +1150,8 @@ THIRD_PARTY_PYTHON_STAGE2_A_DIRECTDEPS = \
|
|||
THIRD_PARTY_PYTHON_STAGE1 \
|
||||
THIRD_PARTY_MBEDTLS \
|
||||
THIRD_PARTY_SQLITE3 \
|
||||
THIRD_PARTY_ZLIB
|
||||
THIRD_PARTY_ZLIB \
|
||||
TOOL_ARGS
|
||||
|
||||
THIRD_PARTY_PYTHON_STAGE2_A_DEPS = \
|
||||
$(call uniq,$(foreach x,$(THIRD_PARTY_PYTHON_STAGE2_A_DIRECTDEPS),$($(x))))
|
||||
|
@ -2099,7 +2101,8 @@ THIRD_PARTY_PYTHON_PYTEST_PYMAINS_DIRECTDEPS = \
|
|||
THIRD_PARTY_PYTHON_STAGE1 \
|
||||
THIRD_PARTY_PYTHON_STAGE2 \
|
||||
THIRD_PARTY_PYTHON_PYTEST \
|
||||
THIRD_PARTY_LINENOISE
|
||||
THIRD_PARTY_LINENOISE \
|
||||
TOOL_ARGS
|
||||
|
||||
THIRD_PARTY_PYTHON_PYTEST_PYMAINS_DEPS = \
|
||||
$(call uniq,$(foreach x,$(THIRD_PARTY_PYTHON_PYTEST_PYMAINS_DIRECTDEPS),$($(x))))
|
||||
|
@ -4176,7 +4179,8 @@ THIRD_PARTY_PYTHON_PYTHON_DIRECTDEPS = \
|
|||
THIRD_PARTY_LINENOISE \
|
||||
THIRD_PARTY_PYTHON_STAGE1 \
|
||||
THIRD_PARTY_PYTHON_STAGE2 \
|
||||
THIRD_PARTY_PYTHON_PYTEST
|
||||
THIRD_PARTY_PYTHON_PYTEST \
|
||||
TOOL_ARGS
|
||||
|
||||
o/$(MODE)/third_party/python/python.pkg: \
|
||||
$(THIRD_PARTY_PYTHON_PYTHON_OBJS) \
|
||||
|
|
2
third_party/python/repl.c
vendored
2
third_party/python/repl.c
vendored
|
@ -41,6 +41,7 @@
|
|||
#include "third_party/python/Include/pythonrun.h"
|
||||
#include "third_party/python/Include/unicodeobject.h"
|
||||
#include "third_party/python/Include/yoink.h"
|
||||
#include "tool/args/args.h"
|
||||
/* clang-format off */
|
||||
|
||||
STATIC_STACK_SIZE(0x100000);
|
||||
|
@ -348,5 +349,6 @@ RunPythonModule(int argc, char **argv)
|
|||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
LoadZipArgs(&argc, &argv);
|
||||
return RunPythonModule(argc, argv);
|
||||
}
|
||||
|
|
3
third_party/quickjs/qjs.c
vendored
3
third_party/quickjs/qjs.c
vendored
|
@ -35,6 +35,7 @@
|
|||
#include "third_party/gdtoa/gdtoa.h"
|
||||
#include "third_party/quickjs/cutils.h"
|
||||
#include "third_party/quickjs/quickjs-libc.h"
|
||||
#include "tool/args/args.h"
|
||||
|
||||
STATIC_STACK_SIZE(0x80000);
|
||||
|
||||
|
@ -329,6 +330,8 @@ int main(int argc, char **argv)
|
|||
#endif
|
||||
size_t stack_size = 0;
|
||||
|
||||
LoadZipArgs(&argc, &argv);
|
||||
|
||||
#if IsModeDbg()
|
||||
ShowCrashReports();
|
||||
#endif
|
||||
|
|
3
third_party/quickjs/quickjs.mk
vendored
3
third_party/quickjs/quickjs.mk
vendored
|
@ -91,7 +91,8 @@ THIRD_PARTY_QUICKJS_A_DIRECTDEPS = \
|
|||
THIRD_PARTY_COMPILER_RT \
|
||||
THIRD_PARTY_GDTOA \
|
||||
THIRD_PARTY_GETOPT \
|
||||
THIRD_PARTY_MUSL
|
||||
THIRD_PARTY_MUSL \
|
||||
TOOL_ARGS
|
||||
|
||||
THIRD_PARTY_QUICKJS_A_DEPS := \
|
||||
$(call uniq,$(foreach x,$(THIRD_PARTY_QUICKJS_A_DIRECTDEPS),$($(x))))
|
||||
|
|
12
third_party/sqlite3/shell.c
vendored
12
third_party/sqlite3/shell.c
vendored
|
@ -92,6 +92,8 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sigaction.h"
|
||||
#include "libc/calls/struct/stat.macros.h"
|
||||
#include "tool/args/args.h"
|
||||
#include "tool/args/args.h"
|
||||
#include "third_party/sqlite3/sqlite3.h"
|
||||
|
||||
typedef sqlite3_int64 i64;
|
||||
|
@ -20563,12 +20565,7 @@ static char *cmdline_option_value(int argc, char **argv, int i){
|
|||
# endif
|
||||
#endif
|
||||
|
||||
#if SQLITE_SHELL_IS_UTF8
|
||||
int SQLITE_CDECL main(int argc, char **argv){
|
||||
#else
|
||||
int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
|
||||
char **argv;
|
||||
#endif
|
||||
char *zErrMsg = 0;
|
||||
ShellState data;
|
||||
const char *zInitFile = 0;
|
||||
|
@ -20579,11 +20576,8 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
|
|||
int nCmd = 0;
|
||||
char **azCmd = 0;
|
||||
const char *zVfs = 0; /* Value of -vfs command-line option */
|
||||
#if !SQLITE_SHELL_IS_UTF8
|
||||
char **argvToFree = 0;
|
||||
int argcToFree = 0;
|
||||
#endif
|
||||
|
||||
LoadZipArgs(&argc, &argv);
|
||||
setBinaryMode(stdin, 0);
|
||||
setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
|
||||
setvbuf(stdin, (char *)NULL, _IONBF, BUFSIZ);
|
||||
|
|
3
third_party/sqlite3/sqlite3.mk
vendored
3
third_party/sqlite3/sqlite3.mk
vendored
|
@ -62,7 +62,8 @@ THIRD_PARTY_SQLITE3_A_DIRECTDEPS = \
|
|||
THIRD_PARTY_GDTOA \
|
||||
THIRD_PARTY_LINENOISE \
|
||||
THIRD_PARTY_MUSL \
|
||||
THIRD_PARTY_ZLIB
|
||||
THIRD_PARTY_ZLIB \
|
||||
TOOL_ARGS
|
||||
|
||||
THIRD_PARTY_SQLITE3_A_DEPS := \
|
||||
$(call uniq,$(foreach x,$(THIRD_PARTY_SQLITE3_A_DIRECTDEPS),$($(x))))
|
||||
|
|
114
tool/args/args.c
Normal file
114
tool/args/args.c
Normal file
|
@ -0,0 +1,114 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2022 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "libc/x/x.h"
|
||||
#include "tool/args/args.h"
|
||||
|
||||
STATIC_YOINK("zip_uri_support");
|
||||
|
||||
static struct ZipArgs {
|
||||
bool registered;
|
||||
bool loaded;
|
||||
int oldargc;
|
||||
char *data;
|
||||
char **args;
|
||||
char **oldargv;
|
||||
} g_zipargs;
|
||||
|
||||
static void AddZipArg(int *argc, char ***argv, char *arg) {
|
||||
*argv = xrealloc(*argv, (++(*argc) + 1) * sizeof(*(*argv)));
|
||||
(*argv)[*argc - 1] = arg;
|
||||
(*argv)[*argc - 0] = 0;
|
||||
}
|
||||
|
||||
void FreeZipArgs(void) {
|
||||
if (g_zipargs.loaded) {
|
||||
free(g_zipargs.data);
|
||||
free(g_zipargs.args);
|
||||
__argc = g_zipargs.oldargc;
|
||||
__argv = g_zipargs.oldargv;
|
||||
g_zipargs.loaded = false;
|
||||
}
|
||||
}
|
||||
|
||||
int LoadZipArgsImpl(int *argc, char ***argv, char *data) {
|
||||
int i, n, fd;
|
||||
bool founddots;
|
||||
char *arg, **args, *state, *start;
|
||||
assert(!g_zipargs.loaded);
|
||||
if (_chomp(data)) {
|
||||
n = 0;
|
||||
args = 0;
|
||||
start = data;
|
||||
founddots = false;
|
||||
AddZipArg(&n, &args, (*argv)[0]);
|
||||
while ((arg = strtok_r(start, "\r\n", &state))) {
|
||||
if (!strcmp(arg, "...")) {
|
||||
founddots = true;
|
||||
for (i = 1; i < *argc; ++i) {
|
||||
AddZipArg(&n, &args, (*argv)[i]);
|
||||
}
|
||||
} else {
|
||||
AddZipArg(&n, &args, arg);
|
||||
}
|
||||
start = 0;
|
||||
}
|
||||
if (founddots || *argc <= 1) {
|
||||
if (!g_zipargs.registered) {
|
||||
atexit(FreeZipArgs);
|
||||
g_zipargs.registered = true;
|
||||
}
|
||||
g_zipargs.loaded = true;
|
||||
g_zipargs.data = data;
|
||||
g_zipargs.args = args;
|
||||
g_zipargs.oldargc = *argc;
|
||||
g_zipargs.oldargv = *argv;
|
||||
*argc = n;
|
||||
*argv = args;
|
||||
__argc = n;
|
||||
__argv = args;
|
||||
} else {
|
||||
free(data);
|
||||
free(args);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces argument list with `/zip/.args` contents if it exists.
|
||||
*
|
||||
* Your `.args` file should have one argument per line.
|
||||
*
|
||||
* If the special argument `...` is *not* encountered, then the
|
||||
* replacement will only happen if *no* CLI args are specified.
|
||||
*
|
||||
* If the special argument `...` *is* encountered, then it'll be
|
||||
* replaced with whatever CLI args were specified by the user.
|
||||
*
|
||||
* @return 0 on success, or -1 w/ errno
|
||||
*/
|
||||
int LoadZipArgs(int *argc, char ***argv) {
|
||||
return LoadZipArgsImpl(argc, argv, xslurp("/zip/.args", 0));
|
||||
}
|
10
tool/args/args.h
Normal file
10
tool/args/args.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#ifndef COSMOPOLITAN_TOOL_ARGS_ARGS_H_
|
||||
#define COSMOPOLITAN_TOOL_ARGS_ARGS_H_
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
int LoadZipArgs(int *, char ***);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
|
||||
#endif /* COSMOPOLITAN_TOOL_ARGS_ARGS_H_ */
|
53
tool/args/args.mk
Normal file
53
tool/args/args.mk
Normal file
|
@ -0,0 +1,53 @@
|
|||
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
|
||||
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
|
||||
|
||||
PKGS += TOOL_ARGS
|
||||
|
||||
TOOL_ARGS_ARTIFACTS += TOOL_ARGS_A
|
||||
TOOL_ARGS = $(TOOL_ARGS_A_DEPS) $(TOOL_ARGS_A)
|
||||
TOOL_ARGS_A = o/$(MODE)/tool/args/args.a
|
||||
TOOL_ARGS_A_FILES := $(wildcard tool/args/*)
|
||||
TOOL_ARGS_A_HDRS = $(filter %.h,$(TOOL_ARGS_A_FILES))
|
||||
TOOL_ARGS_A_SRCS = $(filter %.c,$(TOOL_ARGS_A_FILES))
|
||||
TOOL_ARGS_A_OBJS = $(TOOL_ARGS_A_SRCS:%.c=o/$(MODE)/%.o)
|
||||
|
||||
TOOL_ARGS_A_CHECKS = \
|
||||
$(TOOL_ARGS_A_HDRS:%=o/$(MODE)/%.ok) \
|
||||
$(TOOL_ARGS_A).pkg
|
||||
|
||||
TOOL_ARGS_A_DIRECTDEPS = \
|
||||
LIBC_CALLS \
|
||||
LIBC_FMT \
|
||||
LIBC_INTRIN \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_RUNTIME \
|
||||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_X \
|
||||
LIBC_ZIPOS \
|
||||
NET_HTTPS \
|
||||
THIRD_PARTY_COMPILER_RT
|
||||
|
||||
TOOL_ARGS_A_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TOOL_ARGS_A_DIRECTDEPS),$($(x))))
|
||||
|
||||
$(TOOL_ARGS_A): tool/args/ \
|
||||
$(TOOL_ARGS_A).pkg \
|
||||
$(TOOL_ARGS_A_OBJS)
|
||||
|
||||
$(TOOL_ARGS_A).pkg: \
|
||||
$(TOOL_ARGS_A_OBJS) \
|
||||
$(foreach x,$(TOOL_ARGS_A_DIRECTDEPS),$($(x)_A).pkg)
|
||||
|
||||
TOOL_ARGS_LIBS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)))
|
||||
TOOL_ARGS_SRCS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)_SRCS))
|
||||
TOOL_ARGS_HDRS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)_HDRS))
|
||||
TOOL_ARGS_BINS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)_BINS))
|
||||
TOOL_ARGS_CHECKS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)_CHECKS))
|
||||
TOOL_ARGS_OBJS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)_OBJS))
|
||||
TOOL_ARGS_TESTS = $(foreach x,$(TOOL_ARGS_ARTIFACTS),$($(x)_TESTS))
|
||||
|
||||
.PHONY: o/$(MODE)/tool/args
|
||||
o/$(MODE)/tool/args: $(TOOL_ARGS_CHECKS)
|
|
@ -452,6 +452,41 @@ SPECIAL PATHS
|
|||
If you enable HTTPS client verification then redbean will check
|
||||
that HTTPS clients (a) have a certificate and (b) it was signed.
|
||||
|
||||
/.args
|
||||
Specifies default command-line arguments.
|
||||
|
||||
There's one argument per line. Trailing newline is ignored. If
|
||||
the special argument `...` is *not* encountered, then the
|
||||
replacement will only happen if *no* CLI args are specified.
|
||||
If the special argument `...` *is* encountered, then it'll be
|
||||
replaced with whatever CLI args were specified by the user.
|
||||
|
||||
For example, you might want to use redbean.com in interpreter
|
||||
mode, where your script file is inside the zip. Then, if your
|
||||
redbean is run, what you want is to have the default behavior
|
||||
be running your script. In that case, you might:
|
||||
|
||||
$ cat <<'EOF' >.args
|
||||
-i
|
||||
/zip/hello.lua
|
||||
EOF
|
||||
|
||||
$ cat <<'EOF' >hello.lua
|
||||
print("hello world")
|
||||
EOF
|
||||
|
||||
$ zip redbean.com .args hello.lua
|
||||
$ ./redbean.com
|
||||
hello world
|
||||
|
||||
Please note that if you ran:
|
||||
|
||||
$ ./redbean.com -vv
|
||||
|
||||
Then the default mode of redbean will kick back in. To prevent
|
||||
that from happening, simply add the magic arg `...` to the end
|
||||
of your `.args` file.
|
||||
|
||||
────────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
HOOKS
|
||||
|
|
|
@ -66,6 +66,7 @@ TOOL_NET_DIRECTDEPS = \
|
|||
THIRD_PARTY_REGEX \
|
||||
THIRD_PARTY_SQLITE3 \
|
||||
THIRD_PARTY_ZLIB \
|
||||
TOOL_ARGS \
|
||||
TOOL_BUILD_LIB \
|
||||
TOOL_DECODE_LIB
|
||||
|
||||
|
|
|
@ -146,6 +146,7 @@
|
|||
#include "third_party/mbedtls/x509_crt.h"
|
||||
#include "third_party/regex/regex.h"
|
||||
#include "third_party/zlib/zlib.h"
|
||||
#include "tool/args/args.h"
|
||||
#include "tool/build/lib/case.h"
|
||||
#include "tool/build/lib/psk.h"
|
||||
#include "tool/net/lfuncs.h"
|
||||
|
@ -3745,8 +3746,7 @@ static int LuaFetch(lua_State *L) {
|
|||
// skip them to avoid duplicates;
|
||||
// also allow unknown headers
|
||||
if ((hdridx = GetHttpHeader(key, keylen)) == -1 ||
|
||||
hdridx != kHttpContentLength &&
|
||||
hdridx != kHttpConnection) {
|
||||
hdridx != kHttpContentLength && hdridx != kHttpConnection) {
|
||||
if (hdridx == kHttpUserAgent) {
|
||||
agenthdr = hdr;
|
||||
} else if (hdridx == kHttpHost) {
|
||||
|
@ -7036,6 +7036,7 @@ void RedBean(int argc, char *argv[]) {
|
|||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
LoadZipArgs(&argc, &argv);
|
||||
if (!IsTiny()) {
|
||||
ShowCrashReports();
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
.PHONY: o/$(MODE)/tool
|
||||
o/$(MODE)/tool: \
|
||||
o/$(MODE)/tool/args \
|
||||
o/$(MODE)/tool/build \
|
||||
o/$(MODE)/tool/decode \
|
||||
o/$(MODE)/tool/hash \
|
||||
|
|
Loading…
Reference in a new issue