commit 70453235b61e0189aececa36a211cce7817c5410
parent 923b3da7ddeb2e2781d721572a8c0c6d5c5b6d7d
Author: finwo <finwo@pm.me>
Date: Fri, 13 Mar 2026 11:54:19 +0100
Add license command
Diffstat:
6 files changed, 58 insertions(+), 15 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,8 @@
CC?=clang
FIND=$(shell which gfind find | head -1)
+OBJCOPY=$(shell which objcopy)
+
SRC:=
INCLUDES:=
CFLAGS:=
@@ -33,11 +35,16 @@ SRC+=lib/rxi/microtar/src/microtar.c
OBJ:=$(SRC:.c=.o)
OBJ:=$(OBJ:.cc=.o)
+OBJ+=license.o
+
CFLAGS+=${INCLUDES}
.PHONY: default
default: dep
+license.o: LICENSE.md
+ $(OBJCOPY) --input binary --output elf64-x86-64 --binary-architecture i386 $< $@
+
lib/cofyc/argparse:
mkdir -p lib/cofyc/argparse
curl -sL https://github.com/cofyc/argparse/archive/refs/heads/master.tar.gz | tar xzv --strip-components=1 -C lib/cofyc/argparse
@@ -61,3 +68,8 @@ lib/rxi/microtar:
dep: $(LIBS) $(OBJ)
${CC} ${OBJ} ${CFLAGS} ${LDFLAGS} -o $@
+
+.PHONY: clean
+clean:
+ rm -f $(OBJ)
+
diff --git a/src/command/command.h b/src/command/command.h
@@ -0,0 +1,7 @@
+struct cmd_struct {
+ void *next;
+ const char *cmd;
+ int (*fn) (int, const char **);
+};
+
+extern struct cmd_struct *commands;
diff --git a/src/command/license/license.h b/src/command/license/license.h
@@ -0,0 +1,7 @@
+#ifndef LICENSE_H
+#define LICENSE_H
+
+extern const unsigned char _binary_LICENSE_md_start[];
+extern const unsigned char _binary_LICENSE_md_end[];
+
+#endif // LICENSE_H
diff --git a/src/command/license/main.c b/src/command/license/main.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "command/command.h"
+#include "license.h"
+
+int cmd_license(int argc, const char **argv) {
+ (void)argc;
+ (void)argv;
+ const unsigned char *start = _binary_LICENSE_md_start;
+ const unsigned char *end = _binary_LICENSE_md_end;
+ size_t len = end - start;
+ fwrite(start, 1, len, stdout);
+ return 0;
+}
+
+void __attribute__((constructor)) cmd_license_setup() {
+ struct cmd_struct *cmd = calloc(1, sizeof(struct cmd_struct));
+ cmd->next = commands;
+ cmd->fn = cmd_license;
+ cmd->cmd = "license";
+ commands = cmd;
+}
+
diff --git a/src/command/license/main.h b/src/command/license/main.h
diff --git a/src/main.c b/src/main.c
@@ -6,18 +6,17 @@
#include "erkkah/naett.h"
#include "rxi/microtar.h"
+#include "command/command.h"
+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
+struct cmd_struct *commands = NULL;
+
static const char *const usages[] = {
"subcommands [options] [cmd] [args]",
NULL,
};
-struct cmd_struct {
- const char *cmd;
- int (*fn) (int, const char **);
-};
-
int
cmd_foo(int argc, const char **argv)
{
@@ -57,11 +56,6 @@ cmd_bar(int argc, const char **argv)
return 0;
}
-static struct cmd_struct commands[] = {
- {"foo", cmd_foo},
- {"bar", cmd_bar},
-};
-
int
main(int argc, const char **argv)
{
@@ -78,11 +72,10 @@ main(int argc, const char **argv)
}
/* Try to run command with args provided. */
- struct cmd_struct *cmd = NULL;
- for (int i = 0; i < ARRAY_SIZE(commands); i++) {
- if (!strcmp(commands[i].cmd, argv[0])) {
- cmd = &commands[i];
- }
+ struct cmd_struct *cmd = commands;
+ while(cmd) {
+ if (!strcmp(cmd->cmd, argv[0])) break;
+ cmd = cmd->next;
}
if (cmd) {
return cmd->fn(argc, argv);