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:
Justine Tunney 2022-05-12 11:01:58 -07:00
parent 0f6251f4d2
commit 4499f98e76
19 changed files with 421 additions and 19 deletions

114
tool/args/args.c Normal file
View 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
View 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
View 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)