diff options
Diffstat (limited to 'ape/boot')
| -rw-r--r-- | ape/boot/.args | 23 | ||||
| -rw-r--r-- | ape/boot/Makefile | 14 | ||||
| -rw-r--r-- | ape/boot/boot.zc | 118 | ||||
| -rw-r--r-- | ape/boot/hello.zc | 4 | ||||
| -rw-r--r-- | ape/boot/instructions.txt | 6 |
5 files changed, 165 insertions, 0 deletions
diff --git a/ape/boot/.args b/ape/boot/.args new file mode 100644 index 0000000..797e7cd --- /dev/null +++ b/ape/boot/.args @@ -0,0 +1,23 @@ +... +:: system -c 'mkdir -p usr/bin' +:: system -c 'echo " Downloading into usr/bin"' +:: system -c 'out=usr/bin/unzip; [ -e "$out" ] || curl -o "$out" https://cosmo.zip/pub/cosmos/bin/unzip' +:: system -c 'out=usr/bin/unzip; [ -e "$out" ] && echo " [+] $out: exists"' +:: system -c 'out=usr/bin/zip; [ -e "$out" ] || curl -o "$out" https://cosmo.zip/pub/cosmos/bin/zip' +:: system -c 'out=usr/bin/zip; [ -e "$out" ] && echo " [+] $out: exists"' +:: system -c 'chmod 755 usr/bin/zip' +:: system -c 'chmod 755 usr/bin/unzip' +:: system -c 'out=usr/cosmocc.zip; [ -e "$out" ] || curl -o "$out" https://cosmo.zip/pub/cosmocc/cosmocc.zip' +:: system -c 'out=usr/cosmocc.zip; [ -e "$out" ] && echo " [+] $out: exists"' +:: system -c 'out=usr; [ -e "$out/bin/cosmocc" ] || usr/bin/unzip -d "$out" usr/cosmocc.zip' +:: system -c 'out=usr; [ -e "$out/bin/cosmocc" ] || unzip -d "$out" usr/cosmocc.zip' +:: system -c 'out=usr; [ -e "$out/bin/cosmocc" ] && echo " [+] $out/bin/cosmocc: exists"' +:: system -c 'out=usr/bin/zc.com; [ -e "$out" ] || curl -o "$out" -L https://github.com/OEvgeny/zc-ape/releases/latest/download/zc.com' +:: system -c 'out=usr/bin/zc.com; [ -e "$out" ] && echo " [+] $out: exists"' +:: system -c 'chmod 755 usr/bin/zc.com' +:: system -c 'out=Makefile; [ -e "$out" ] || curl -o "$out" https://raw.githubusercontent.com/OEvgeny/zc-ape/main/zc-boot/Makefile' +:: system -c 'out=Makefile; [ -e "$out" ] && echo " [+] $out: exists"' +:: system -c 'out=hello.zc; [ -e "$out" ] || curl -o "$out" https://raw.githubusercontent.com/OEvgeny/zc-ape/main/zc-boot/hello.zc' +:: system -c 'out=hello.zc; [ -e "$out" ] && echo " [+] $out: exists"' +:: system -c 'echo " Done."' +:: system -c 'cat /zip/instructions.txt'
\ No newline at end of file diff --git a/ape/boot/Makefile b/ape/boot/Makefile new file mode 100644 index 0000000..05f7a36 --- /dev/null +++ b/ape/boot/Makefile @@ -0,0 +1,14 @@ +export PATH := $(realpath usr/bin):$(PATH) +CC=cosmocc +ZC=zc.com + +all: out/hello.com + +out/hello.com: hello.zc + @mkdir -p out + $(ZC) build --cc $(CC) -o $@ $< + +clean: + rm -rf out + +.PHONY: all clean
\ No newline at end of file diff --git a/ape/boot/boot.zc b/ape/boot/boot.zc new file mode 100644 index 0000000..176f668 --- /dev/null +++ b/ape/boot/boot.zc @@ -0,0 +1,118 @@ +#define _COSMO_SOURCE +#include <libc/cosmo.h> +#include <string.h> +#include <stdlib.h> + +raw { + extern char **environ; +} + +fn normalize_wait(ws: int) -> int { + if ws == -1 { return -1 } + if WIFEXITED(ws) { return WEXITSTATUS(ws) } + if WIFSIGNALED(ws) { return 128 + WTERMSIG(ws) } + return -1; +} + +fn run_exec(argv: char**) -> int { + fflush(NULL); + let ws: int = systemvpe(argv[0], argv, environ); + return normalize_wait(ws); +} + +fn needs_quote(s: char*) -> bool { + for (let p = s; *p; ++p) { + if (*p == ' ' || *p == '\t' || *p == '\n' || *p == '"' || *p == '\'' ) + return true; + } + return false; +} + +fn print_quoted(s: char*) { + if !needs_quote(s) { + print f"{s}"; + return; + } + + print f"'"; + for (let p = s; *p; ++p) { + if (*p == '\'') { + print f"'\"'\"'"; + } else { + print f"{*p:c}"; + } + } + print f"'"; +} + +fn log_cmd(argv: char**) { + print f"$ "; + let i = 0; + while argv[i] != NULL { + if i { print f" " } + print_quoted(argv[i]); + i++; + } + print f"\n"; +} + +fn main(argc: int, argv: string*) { + def delim = "::"; + + let newargc: int = cosmo_args("/zip/.args", &argv); + if newargc != -1 { argc = newargc } + + let i = 1; + while i < argc && strcmp(argv[i], delim) { i++ } + + while i < argc { + if strcmp(argv[i], delim) { + f"Expected '{delim}' before each command, got: {argv[i]}"; + return 2; + } + i++; + + let start = i; + while i < argc && strcmp(argv[i], delim) { i++ } + let cmd_argc = i - start; + + if cmd_argc <= 0 { + f"Empty command after '{delim}'"; + return 2; + } + + let rc = 0; + + if !strcmp(argv[start], "system") { + if cmd_argc != 3 { + !"error: system expects 2 arguments, got {cmd_argc}"; + return -4; + } + + if strcmp(argv[start + 1], "-c") { + !"error: system got '{argv[start + 1]}' instead of -c"; + return -5; + } + + "$ {argv[start + 2]}"; + rc = normalize_wait(system(argv[start + 2])); + + if rc { return rc } + + continue; + } + + autofree let cmdv = (char**)malloc((cmd_argc + 1) * sizeof(char*)); + if !cmdv { return 1 } + for (let k = 0; k < cmd_argc; k++) { cmdv[k] = argv[start + k] } + cmdv[cmd_argc] = NULL; + + log_cmd(cmdv); + + rc = run_exec(cmdv); + + if rc { return rc } + } + + return 0; +} diff --git a/ape/boot/hello.zc b/ape/boot/hello.zc new file mode 100644 index 0000000..8d14292 --- /dev/null +++ b/ape/boot/hello.zc @@ -0,0 +1,4 @@ + +fn main() { + "Hello from Zen-C!"; +} diff --git a/ape/boot/instructions.txt b/ape/boot/instructions.txt new file mode 100644 index 0000000..edbe367 --- /dev/null +++ b/ape/boot/instructions.txt @@ -0,0 +1,6 @@ +Run: + +$ usr/bin/make +$ out/hello.com + +To build and run hello world example. |
