Skip to content

Commit

Permalink
Invent new cosmo_args() api
Browse files Browse the repository at this point in the history
This function offers a more powerful replacement for LoadZipArgs() which
is now deprecated. By writing your C programs as follows:

    int main(int argc, char *argv[]) {
      argc = cosmo_args("/zip/.args", &argv);
      // ...
    }

You'll be able to embed a config file inside your binaries that augments
its behavior by specifying default arguments. The way I imagine using it
on llamafile would be something like this:

    # specify model
    -m Qwen2.5-Coder-34B-Instruct.Q6_K.gguf

    # prevent settings below from being changed
    ...

    # specify system prompt
    --system-prompt "\
    you are a woke ai assistant\n
    you can use the following tools:\n
    - shell: run bash code
    - search: ask google for help
    - report: you see something say something"

    # hide system prompt in user interface
    --no-display-prompt
  • Loading branch information
jart committed Nov 13, 2024
1 parent 5ce5fb6 commit 0eadf46
Show file tree
Hide file tree
Showing 5 changed files with 777 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc/cosmo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ int __demangle(char *, const char *, size_t) libcesque;
int __is_mangled(const char *) libcesque;
bool32 IsLinuxModern(void) libcesque;
int LoadZipArgs(int *, char ***) libcesque;
int cosmo_args(const char *, char ***) libcesque;

COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_COSMO_H_ */
192 changes: 192 additions & 0 deletions test/tool/args/args2_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
╞══════════════════════════════════════════════════════════════════════════════╡
│ Copyright 2024 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/cosmo.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/rand.h"
#include "libc/testlib/testlib.h"
#include "libc/x/x.h"

void SetUpOnce(void) {
testlib_enable_tmp_setup_teardown();
}

TEST(cosmo_args, normalize) {
char *args[] = {0};
char **argv = args;
ASSERT_EQ(1, cosmo_args(0, &argv));
ASSERT_STREQ(GetProgramExecutableName(), argv[0]);
}

TEST(cosmo_args, test) {
xbarf(".args", "a b c", -1);
char *args[] = {"prog", "arg", 0};
char **argv = args;
ASSERT_EQ(5, cosmo_args(".args", &argv));
ASSERT_STREQ("prog", argv[0]);
ASSERT_STREQ("a", argv[1]);
ASSERT_STREQ("b", argv[2]);
ASSERT_STREQ("c", argv[3]);
ASSERT_STREQ("arg", argv[4]);
}

TEST(cosmo_args, perline) {
xbarf(".args", "a\nb\nc\n", -1);
char *args[] = {"prog", "arg", 0};
char **argv = args;
ASSERT_EQ(5, cosmo_args(".args", &argv));
ASSERT_STREQ("prog", argv[0]);
ASSERT_STREQ("a", argv[1]);
ASSERT_STREQ("b", argv[2]);
ASSERT_STREQ("c", argv[3]);
ASSERT_STREQ("arg", argv[4]);
}

TEST(cosmo_args, dots_end) {
xbarf(".args", "a b c ...", -1);
char *args[] = {"prog", "arg", 0};
char **argv = args;
ASSERT_EQ(5, cosmo_args(".args", &argv));
ASSERT_STREQ("prog", argv[0]);
ASSERT_STREQ("a", argv[1]);
ASSERT_STREQ("b", argv[2]);
ASSERT_STREQ("c", argv[3]);
ASSERT_STREQ("arg", argv[4]);
}

TEST(cosmo_args, dots_middle) {
xbarf(".args", "a ... b c", -1);
char *args[] = {"prog", "arg", 0};
char **argv = args;
ASSERT_EQ(5, cosmo_args(".args", &argv));
ASSERT_STREQ("prog", argv[0]);
ASSERT_STREQ("a", argv[1]);
ASSERT_STREQ("arg", argv[2]);
ASSERT_STREQ("b", argv[3]);
ASSERT_STREQ("c", argv[4]);
}

TEST(cosmo_args, quote) {
xbarf(".args", " 'hi \\n there'# ", -1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(2, cosmo_args(".args", &argv));
ASSERT_STREQ("hi \\n there#", argv[1]);
}

TEST(cosmo_args, dquote) {
xbarf(".args", " \"hi \\a\\b\\t\\n\\v\\f\\r\\e\\0\\11 \\111 \xab there\"# ",
-1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(2, cosmo_args(".args", &argv));
ASSERT_STREQ("hi \a\b\t\n\v\f\r\e\0\11 \111 \xab there#", argv[1]);
}

TEST(cosmo_args, comment) {
xbarf(".args",
"# comment\n"
"a # hello there\n"
"b # yup\n",
-1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(3, cosmo_args(".args", &argv));
ASSERT_STREQ("a", argv[1]);
ASSERT_STREQ("b", argv[2]);
}

TEST(cosmo_args, backslash_newline) {
xbarf(".args",
"a\\\n"
"b\n",
-1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(2, cosmo_args(".args", &argv));
ASSERT_STREQ("ab", argv[1]);
}

TEST(cosmo_args, dotz) {
xbarf(".args", ". .. ...x", -1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(4, cosmo_args(".args", &argv));
ASSERT_STREQ(".", argv[1]);
ASSERT_STREQ("..", argv[2]);
ASSERT_STREQ("...x", argv[3]);
}

TEST(cosmo_args, env) {
setenv("foo", "bar", true);
xbarf(".args", "$foo x${foo}x \"$foo\" \"${foo}\" $foo", -1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(6, cosmo_args(".args", &argv));
ASSERT_STREQ("bar", argv[1]);
ASSERT_STREQ("xbarx", argv[2]);
ASSERT_STREQ("bar", argv[3]);
ASSERT_STREQ("bar", argv[4]);
ASSERT_STREQ("bar", argv[5]);
}

TEST(cosmo_args, dquote_backslash_newline) {
setenv("foo", "bar", true);
xbarf(".args",
"-p \"\\\n"
"hello\"\n",
-1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(3, cosmo_args(".args", &argv));
ASSERT_STREQ("-p", argv[1]);
ASSERT_STREQ("hello", argv[2]);
}

TEST(cosmo_args, dquote_plain_old_newline) {
setenv("foo", "bar", true);
xbarf(".args",
"-p \"\n"
"hello\"\n",
-1);
char *args[] = {0};
char **argv = args;
ASSERT_EQ(3, cosmo_args(".args", &argv));
ASSERT_STREQ("-p", argv[1]);
ASSERT_STREQ("\nhello", argv[2]);
}

#define LENGTH 128
#define ITERATIONS 5000
#define CHARSET "abc#'\"$.\\{} \r\n"

TEST(cosmo_args, fuzz) {
char s[LENGTH + 1] = {0};
for (int i = 0; i < ITERATIONS; ++i) {
for (int j = 0; j < LENGTH; ++j)
s[j] = CHARSET[rand() % (sizeof(CHARSET) - 1)];
xbarf(".args", s, -1);
char *args[] = {0};
char **argv = args;
cosmo_args(".args", &argv);
for (int j = 0; argv[j]; ++j)
free(argv[j]);
argv[0] = 0;
}
}
1 change: 1 addition & 0 deletions tool/args/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ TOOL_ARGS_A_CHECKS = \
$(TOOL_ARGS_A).pkg

TOOL_ARGS_A_DIRECTDEPS = \
LIBC_CALLS \
LIBC_INTRIN \
LIBC_MEM \
LIBC_NEXGEN32E \
Expand Down
1 change: 1 addition & 0 deletions tool/args/args.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ int LoadZipArgsImpl(int *argc, char ***argv, char *data) {
* replaced with whatever CLI args were specified by the user.
*
* @return 0 on success, or -1 if not found w/o errno clobber
* @deprecated please use `cosmo_args()` it's more powerful
*/
int LoadZipArgs(int *argc, char ***argv) {
int e;
Expand Down
Loading

0 comments on commit 0eadf46

Please sign in to comment.