aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cla_parse.c (renamed from src/cla_parse.c)71
-rw-r--r--include/files.h30
-rw-r--r--include/globs.h137
-rw-r--r--include/help.h26
-rw-r--r--include/lua_globs.h42
-rw-r--r--include/lua_vars.h27
-rw-r--r--include/pkg_create.h25
-rw-r--r--include/str.h518
-rw-r--r--inl0
-rw-r--r--src/files.c34
-rw-r--r--src/globs.c55
-rw-r--r--src/help.c79
-rw-r--r--src/lua_globs.c106
-rw-r--r--src/lua_vars.c68
-rw-r--r--src/main.c11
-rw-r--r--src/pkg_create.c120
-rw-r--r--src/str.c776
17 files changed, 1948 insertions, 177 deletions
diff --git a/src/cla_parse.c b/cla_parse.c
index c2f31fd..59c8bda 100644
--- a/src/cla_parse.c
+++ b/cla_parse.c
@@ -25,13 +25,12 @@
#include "globs.h"
#include "str.h"
-
//#include "declare.h"
//#include "deps_resolve.h"
-//#include "help.h"
+#include "help.h"
//#include "name_from_url.h"
//#include "pkg_build.h"
-//#include "pkg_create.h"
+#include "pkg_create.h"
//#include "pkg_search.h"
//#include "pkg_install.h"
//#include "pkg_list.h"
@@ -40,20 +39,19 @@
//#include "repo_add.h"
//#include "update.h"
-#define COMMAND(large, small, code) \
- if (slc_eq(slc_from_cstr(argv[i]), slc_from_cstr(large)) \
- || slc_eq(slc_from_cstr(argv[i]), slc_from_cstr(small))) code
+#define COMMAND(arg, large, small, code) if ( \
+ strcmp(arg, large) == 0 || strcmp(arg, small) == 0) code
#define NOT_ENOUGH_ARGS(arg, next) \
- printf("%.*s Not enough arguments! Try: `pkgit %s [%s]`\n", \
+ printf("%s Not enough arguments! Try: `pkgit %s [%s]`\n", \
PRINT_ERROR, arg, next)
void cmd_add(char **argv, int i) {
if (argv[i + 1]) {
- printf("add repo %s", argv[i + 1]);
+ printf("add repo %s\n", argv[i + 1]);
//repo_add(argv[i + 1], name_from_url(argv[i + 1]));
} else {
-// NOT_ENOUGH_ARGS(argv[i], "url");
+ NOT_ENOUGH_ARGS(argv[i], "url");
}
}
@@ -61,12 +59,12 @@ void cmd_build(int argc, char **argv, int i, package pkg) {
if (argv[i + 1]) {
for (int j = i + 1; j < argc; j++) {
if (argv[j][0] == '-') continue;
- printf("build pkg %s", argv[j]);
+ printf("build pkg %s\n", argv[j]);
//pkg = pkg_create(argv[j]);
//pkg_build(pkg);
}
} else {
- printf("build pkg .");
+ printf("build pkg .\n");
//pkg = pkg_create(".");
//pkg_build(pkg);
}
@@ -76,7 +74,7 @@ void cmd_install(int argc, char **argv, int i, package pkg) {
if (argv[i + 1]) {
for (int j = i + 1; j < argc; j++) {
if (argv[j][0] == '-') continue;
- printf("install pkg %s", argv[j]);
+ printf("install pkg %s\n", argv[j]);
//pkg = pkg_create(argv[j]);
//pkg_install(pkg);
}
@@ -89,7 +87,7 @@ void cmd_remove(int argc, char **argv, int i, package pkg) {
if (argv[i + 1]) {
for (int j = i + 1; j < argc; j++) {
if (argv[j][0] == '-') continue;
- printf("remove pkg %s", argv[j]);
+ printf("remove pkg %s\n", argv[j]);
//pkg = pkg_create(argv[j]);
//pkg_remove(pkg);
}
@@ -121,43 +119,44 @@ void flags_cmd(int argc, char **argv, int i, package pkg) {
case 'l': /*pkgs_list();*/ printf("list\n"); break;
case 's': /*search(argv[i + 1]);*/ printf("search\n"); break;
case 'v': printf("%s\n", VERSION); break;
- case 'h': /*help();*/ printf("help\n"); break;
+ case 'h': help(); break;
default: break;
}
}
}
void flags_parse(int argc, char **argv, package pkg) {
- for (int i = 1; i < argc; i++) {
- if (argv[i][0] != '-') continue;
- if (argv[i][1] == '-') {
- COMMAND("--quiet", "-q", { is_verbose = 0; });
- COMMAND("--force", "-f", { is_forced = 1; });
- } else {
- flags_mod(argv, i);
- flags_cmd(argc, argv, i, pkg);
- }
- }
+ for (int i = 1; i < argc; i++) {
+ if (argv[i][0] != '-') continue;
+ str_slc arg = str_slc_from_cstr(argv[i]);
+ if (argv[i][1] == '-') {
+ COMMAND(argv[i], "--quiet", "-q", { is_verbose = 0; });
+ COMMAND(argv[i], "--force", "-f", { is_forced = 1; });
+ } else {
+ flags_mod(argv, i);
+ flags_cmd(argc, argv, i, pkg);
+ }
+ }
}
void cmds_parse(int argc, char **argv, package pkg) {
for (int i = 1; i < argc; i++) {
- COMMAND("--add", "a", { cmd_add(argv, i); });
- COMMAND("--build", "b", { cmd_build(argc, argv, i, pkg); });
- COMMAND("--install", "i", { cmd_install(argc, argv, i, pkg); });
- COMMAND("--remove", "r", { cmd_remove(argc, argv, i, pkg); });
- COMMAND("--update", "u", { /*update();*/ });
- COMMAND("--declare", "d", { /*declare();*/ });
- COMMAND("--list", "l", { /*pkgs_list();*/ });
- COMMAND("--search", "s", { /*search(argv[i + 1]);*/ });
- COMMAND("--version", "v", { printf("%s\n", VERSION); });
- COMMAND("--help", "h", { /*help();*/ });
- COMMAND("--check", "c", { /*deps_resolve();*/ });
+ COMMAND(argv[i], "--add", "a", { cmd_add(argv, i); });
+ COMMAND(argv[i], "--build", "b", { cmd_build(argc, argv, i, pkg); });
+ COMMAND(argv[i], "--install", "i", { cmd_install(argc, argv, i, pkg); });
+ COMMAND(argv[i], "--remove", "r", { cmd_remove(argc, argv, i, pkg); });
+ COMMAND(argv[i], "--update", "u", { /*update();*/ });
+ COMMAND(argv[i], "--declare", "d", { /*declare();*/ });
+ COMMAND(argv[i], "--list", "l", { /*pkgs_list();*/ });
+ COMMAND(argv[i], "--search", "s", { /*search(argv[i + 1]);*/ });
+ COMMAND(argv[i], "--version", "v", { printf("%s\n", VERSION); });
+ COMMAND(argv[i], "--help", "h", { help(); });
+ COMMAND(argv[i], "--check", "c", { /*deps_resolve();*/ });
}
}
void cla_parse(int argc, char **argv) {
- if (!argv[1]) { /*help();*/ return; }
+ if (!argv[1]) { help(); return; }
package pkg = {0};
flags_parse(argc, argv, pkg);
cmds_parse(argc, argv, pkg);
diff --git a/include/files.h b/include/files.h
new file mode 100644
index 0000000..9fd1a71
--- /dev/null
+++ b/include/files.h
@@ -0,0 +1,30 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PKGIT_FILES_H
+#define PKGIT_FILES_H
+
+#include <sys/stat.h>
+#include <stdbool.h>
+
+bool file_exists(const char *path);
+bool is_directory(const char *path);
+
+#endif
diff --git a/include/globs.h b/include/globs.h
index 69fee60..f44ab85 100644
--- a/include/globs.h
+++ b/include/globs.h
@@ -21,53 +21,94 @@
#ifndef PKGIT_GLOBALS_H
#define PKGIT_GLOBALS_H
-#define VERSION "1.4.0_INDEV"
-#define RED "\x1b[0;31m"
-#define GREEN "\x1b[0;32m"
-#define YELLOW "\x1b[0;33m"
-#define BLUE "\x1b[0;34m"
-#define MAGENTA "\x1b[0;35m"
-#define CYAN "\x1b[0;36m"
-#define GRAY "\x1b[0;37m"
-#define BRIGHT_RED "\x1b[0;91m"
-#define BRIGHT_GREEN "\x1b[0;92m"
-#define BRIGHT_YELLOW "\x1b[0;93m"
-#define BRIGHT_BLUE "\x1b[0;94m"
-#define BRIGHT_MAGENTA "\x1b[0;95m"
-#define BRIGHT_CYAN "\x1b[0;96m"
-#define BRIGHT_GRAY "\x1b[0;97m"
-#define BOLD_RED "\x1b[1;31m"
-#define BOLD_GREEN "\x1b[1;32m"
-#define BOLD_YELLOW "\x1b[1;33m"
-#define BOLD_BLUE "\x1b[1;34m"
-#define BOLD_MAGENTA "\x1b[1;35m"
-#define BOLD_CYAN "\x1b[1;36m"
-#define BOLD_GRAY "\x1b[1;37m"
-#define BOLD_WHITE "\x1b[1;38m"
-#define BOLD_BRIGHT_RED "\x1b[1;91m"
-#define BOLD_BRIGHT_GREEN "\x1b[1;92m"
-#define BOLD_BRIGHT_YELLOW "\x1b[1;93m"
-#define BOLD_BRIGHT_BLUE "\x1b[1;94m"
-#define BOLD_BRIGHT_MAGENTA "\x1b[1;95m"
-#define BOLD_BRIGHT_CYAN "\x1b[1;96m"
-#define BOLD_BRIGHT_GRAY "\x1b[1;97m"
-#define ITALIC "\x1b[3m"
-#define COLOR_RESET "\x1b[0m"
-
-#define PRINT_PKGIT BOLD_YELLOW "[" BOLD_MAGENTA "pkgit" BOLD_YELLOW "]" COLOR_RESET
+#define VERSION "1.4.0_INDEV"
+#define RED "\x1b[0;31m"
+#define GREEN "\x1b[0;32m"
+#define YELLOW "\x1b[0;33m"
+#define BLUE "\x1b[0;34m"
+#define MAGENTA "\x1b[0;35m"
+#define CYAN "\x1b[0;36m"
+#define GRAY "\x1b[0;37m"
+#define BRIGHT_RED "\x1b[0;91m"
+#define BRIGHT_GREEN "\x1b[0;92m"
+#define BRIGHT_YELLOW "\x1b[0;93m"
+#define BRIGHT_BLUE "\x1b[0;94m"
+#define BRIGHT_MAGENTA "\x1b[0;95m"
+#define BRIGHT_CYAN "\x1b[0;96m"
+#define BRIGHT_GRAY "\x1b[0;97m"
+#define BOLD "\x1b[1m"
+#define BOLD_RED "\x1b[1;31m"
+#define BOLD_GREEN "\x1b[1;32m"
+#define BOLD_YELLOW "\x1b[1;33m"
+#define BOLD_BLUE "\x1b[1;34m"
+#define BOLD_MAGENTA "\x1b[1;35m"
+#define BOLD_CYAN "\x1b[1;36m"
+#define BOLD_GRAY "\x1b[1;37m"
+#define BOLD_WHITE "\x1b[1;38m"
+#define BOLD_BRIGHT_RED "\x1b[1;91m"
+#define BOLD_BRIGHT_GREEN "\x1b[1;92m"
+#define BOLD_BRIGHT_YELLOW "\x1b[1;93m"
+#define BOLD_BRIGHT_BLUE "\x1b[1;94m"
+#define BOLD_BRIGHT_MAGENTA "\x1b[1;95m"
+#define BOLD_BRIGHT_CYAN "\x1b[1;96m"
+#define BOLD_BRIGHT_GRAY "\x1b[1;97m"
+#define ITALIC "\x1b[3m"
+#define COLOR_RESET "\x1b[0m"
+
+#define eprintf(...) fprintf(stderr, __VA_ARGS__);
+
+#define check_alloc(ptr) \
+ do { \
+ if (ptr == NULL) \
+ panic("allocation of `%s` failed", #ptr); \
+ } while (0)
+
+#include <stdlib.h>
+#define panic(...) \
+ do { \
+ eprintf(BOLD RED "panic:" COLOR_RESET " line %d, func \"%s\" in file " \
+ "\"%s\": ", \
+ __LINE__, __func__, __FILE_NAME__); \
+ eprintf(__VA_ARGS__); \
+ eprintf("\n"); \
+ fflush(stderr); \
+ abort(); \
+ } while (0)
+
+#ifdef PKGIT_DEBUG
+#define assert(expr) \
+ do { \
+ if (!(expr)) \
+ panic("Assertion `%s` failed", #expr); \
+ } while (0)
+#else
+#define assert(expr) (void)(expr)
+#endif
+
+#ifdef unreachable
+#undef unreachable
+#endif
+#define unreachable panic("reached unreachable code")
+
+#define PRINT_PKGIT \
+ BOLD_YELLOW "[" BOLD_MAGENTA "pkgit" BOLD_YELLOW "]" COLOR_RESET
#define PRINT_SUCCESS PRINT_PKGIT GREEN " [SUCCESS]" COLOR_RESET
#define PRINT_SKIPPED PRINT_PKGIT BLUE " [SKIPPED]" COLOR_RESET
#define PRINT_WARNING PRINT_PKGIT YELLOW " [WARNING]" COLOR_RESET
#define PRINT_ERROR PRINT_PKGIT RED " [ERROR]" COLOR_RESET
+#define MAX_REPOS 1000
+#define MAX_DIRS 100
+#define MAX_PATH_LEN 1024
+
#include "str.h"
typedef struct {
- str_slc name;
- str_slc url;
- str_slc version;
- str_slc target;
- str_slc src;
+ str name;
+ str url;
+ str version;
+ str target;
+ str src;
int is_local;
} package;
@@ -75,8 +116,24 @@ extern int is_verbose;
extern int is_forced;
extern int config_exists;
-extern char root_config_file[20];
+extern str home_config_dir;
+extern str home_config_file;
+extern str home_repos_file;
+extern str root_config_dir;
+extern str root_config_file;
+extern str root_repos_file;
+extern int is_root_config;
+
+extern str config_dir;
+extern str config_file;
+extern str repos_file;
+
+extern str bin;
+extern str lib;
+extern str include;
+extern str src;
void init_vars(void);
+void free_vars(void);
#endif
diff --git a/include/help.h b/include/help.h
new file mode 100644
index 0000000..c194be4
--- /dev/null
+++ b/include/help.h
@@ -0,0 +1,26 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef HELP_H
+#define HELP_H
+
+void help(void);
+
+#endif \ No newline at end of file
diff --git a/include/lua_globs.h b/include/lua_globs.h
new file mode 100644
index 0000000..0a5426f
--- /dev/null
+++ b/include/lua_globs.h
@@ -0,0 +1,42 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PKGIT_LUA_GLOBALS_H
+#define PKGIT_LUA_GLOBALS_H
+
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#include <stdbool.h>
+
+extern lua_State *L;
+extern lua_State *B;
+extern bool config_loaded;
+
+void push_lua_path(lua_State *L, const char *new_path);
+void init_lua_state(void);
+void init_bldit(void);
+void free_lua_state(void);
+
+lua_State *get_lua_state(void);
+
+void lua_isnt_type(char* variable, char* type);
+
+#endif
diff --git a/include/lua_vars.h b/include/lua_vars.h
new file mode 100644
index 0000000..dfc62d0
--- /dev/null
+++ b/include/lua_vars.h
@@ -0,0 +1,27 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PKGIT_LUA_VARS_H
+#define PKGIT_LUA_VARS_H
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+void init_install_directories(void);
+#endif
diff --git a/include/pkg_create.h b/include/pkg_create.h
new file mode 100644
index 0000000..908b6fd
--- /dev/null
+++ b/include/pkg_create.h
@@ -0,0 +1,25 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#ifndef PKGIT_PKG_CREATE_H
+#define PKGIT_PKG_CREATE_H
+#include "globs.h"
+package pkg_create(str_slc *arg);
+#endif
diff --git a/include/str.h b/include/str.h
index c81bcbd..f5c79dd 100644
--- a/include/str.h
+++ b/include/str.h
@@ -18,33 +18,511 @@
*/
-#ifndef PKGIT_STRING_H
-#define PKGIT_STRING_H
+#ifndef PKGIT_STR_H
+#define PKGIT_STR_H
+
+#include <stdarg.h>
+#include <stdbool.h>
#include <stddef.h>
+#include <stdint.h>
+#include <stdio.h>
+/**
+ * Heap-allocated, owned character string.
+ *
+ * All library functions assert that pointers passed in are valid and point to
+ * valid data. All references to "C strings" infer null-terminated pointers to
+ * characters.
+ *
+ * When data is NULL, it is deemed invalid.
+ */
typedef struct {
- char* data;
+ char *data;
+ size_t cap;
size_t len;
-} str_slc;
+} str;
+/**
+ * A view into any character buffer that may not be
+ * resized, but may be heap-allocated.
+ *
+ * When data is NULL, it is deemed invalid.
+ */
typedef struct {
- char* data;
- size_t cap;
+ const char *data;
size_t len;
-} str;
+} str_slc;
+
+#define str_is_valid(str) ((str)->data != NULL)
+
+// for printf formatting
+#define str_fmt(s) (int)((s)->len), (s)->data
+
+/**
+ * Creates and initializes an empty, valid str.
+ */
+str str_new(void);
+
+/**
+ * Creates and initializes an empty str with a specified capacity.
+ */
+str str_new_with_capacity(size_t cap);
+
+/**
+ * Make a str from a C string.
+ */
+str str_from_cstr(const char *s);
+
+/**
+ * Equivalent to str_from_cstr
+ */
+str mstr(const char *s);
+
+/**
+ * Make a str from a string slice.
+ */
+str str_from_str_slc(const str_slc s);
+
+/**
+ * Return a str from the result of an sprintf-style call.
+ */
+str str_format(const char *format, ...);
+
+/**
+ * Saves the result of an sprintf-style call into s.
+ */
+void str_format_into(str *s, const char *format, ...);
+
+/**
+ * Reserves 1.5*new_cap bytes of space in the string.
+ */
+void str_reserve(str *s, size_t new_cap);
+
+/**
+ * Reserves EXACTLY new_cap of bytes of space in the string.
+ */
+void str_reserve_exact(str *s, size_t new_cap);
+
+/**
+ * Clears the entire string buffer to zeroes and resets the length.
+ */
+void str_clear(str *s);
+
+/**
+ * Frees the internal heap-allocated buffer.
+ */
+void str_free(str *s);
+
+/**
+ * Copies the entirety of src into dest, resizing it if necessary, while
+ * overwriting all existing data.
+ */
+void str_copy_into(str *dest, const str *src);
+
+/**
+ * Copies a source C string at src into an already initialized
+ * str, resizing the buffer if necessary, overwriting all existing data.
+ */
+void str_copy_cstr_into(str *dest, const char *src);
+
+/**
+ * Copies a source string slice at src into an already initialized
+ * str, resizing the buffer if necessary, overwriting all existing data.
+ */
+void str_copy_str_slc_into(str *dest, const str_slc src);
+
+/**
+ * Copies n bytes of src into an already initialized str dest,
+ * resizing it if necessary, while overwriting all existing data.
+ */
+void str_ncopy_into(str *dest, const str *src, size_t count);
+
+/**
+ * Copies n bytes of a C string src into an already initialized str dest,
+ * resizing it if necessary, while overwriting all existing data.
+ */
+void str_ncopy_cstr_into(str *dest, const char *src, size_t count);
+
+/**
+ * Copies n bytes of a string slice src into an already initialized str dest,
+ * resizing it if necessary, while overwriting all existing data.
+ */
+void str_ncopy_str_slc_into(str *dest, const str_slc src, size_t count);
+
+/**
+ * Duplicates a str exactly, retaining its original capacity.
+ */
+str str_dupe(const str *src);
+
+/**
+ * Appends a str into an existing str, resizing the buffer if necessary.
+ */
+void str_append(str *src, const str *new);
+
+/**
+ * Appends a single character new into src, resizing the buffer if necessary.
+ */
+void str_append_char(str *src, char new);
+
+/**
+ * Appends a C string into an existing str, resizing the buffer if necessary.
+ */
+void str_append_cstr(str *src, const char *new);
+
+/**
+ * Appends a string slice into an existing str, resizing the buffer if
+ * necessary.
+ */
+void str_append_str_slc(str *src, const str_slc new);
+
+/**
+ * Removes the last character from src, returning it.
+ */
+char str_pop_last(str *src);
+
+/**
+ * Concatenates lhs and rhs together into a new string.
+ */
+str str_concat(const str *lhs, const str *rhs);
+
+/**
+ * Concatenates lhs and a C string rhs together into a new string.
+ */
+str str_concat_cstr(const str *lhs, const char *rhs);
+
+/**
+ * Concatenates lhs and a str_slc rhs together into a new string.
+ */
+str str_concat_str_slc(const str *lhs, const str_slc rhs);
+
+/**
+ * Trims all whitespace characters in s away only on the left, and returns the
+ * result.
+ */
+str str_trim_left(const str *s);
+
+/**
+ * Trims all whitespace characters in s away only on the left, and returns the
+ * result.
+ */
+str str_trim_right(const str *s);
+
+/**
+ * Trims all whitespace characters in s away on the left and right, and returns
+ * the result.
+ */
+str str_trim(const str *s);
+
+/**
+ * Converts all characters in s to uppercase and returns the result.
+ */
+str str_to_upper(const str *s);
+
+/**
+ * Converts all characters in s to lowercase and returns the result
+ */
+str str_to_lower(const str *s);
+
+/**
+ * Trims all whitespace characters in s away only on the left, modifying the
+ * string s.
+ */
+void str_trim_left_in_place(str *s);
+
+/**
+ * Trims all whitespace characters in s away only on the right, modifying the
+ * string s.
+ */
+void str_trim_right_in_place(str *s);
+
+/**
+ * Trims all whitespace characters in s away on the left and right, modifying
+ * the string s.
+ */
+void str_trim_in_place(str *s);
+
+/**
+ * Converts all characters in s to uppercase, modifying the string s.
+ */
+void str_to_upper_in_place(str *s);
+
+/**
+ * Converts all characters in s to lowercase, modifying the string s.
+ */
+void str_to_lower_in_place(str *s);
+
+/**
+ * Returns if lhs and rhs are equal.
+ */
+bool str_equal(const str *lhs, const str *rhs);
+
+/**
+ * Returns if lhs and rhs (a C string) are equal.
+ */
+bool str_equal_cstr(const str *lhs, const char *rhs);
+
+/**
+ * Returns if lhs and rhs (a string slice) are equal.
+ */
+bool str_equal_str_slc(const str *lhs, const str_slc rhs);
+
+/**
+ * Compares the strings lhs and rhs lexicographically, similar to strcmp.
+ */
+int str_compare(const str *lhs, const str *rhs);
+
+/**
+ * Compares the strings lhs and rhs (a C string) lexicographically, similar to
+ * strcmp.
+ */
+int str_compare_cstr(const str *lhs, const char *rhs);
+
+/**
+ * Compares the strings lhs and rhs (a string slice) lexicographically, similar
+ * to strcmp.
+ */
+int str_compare_str_slc(const str *lhs, const str_slc rhs);
+
+/**
+ * Returns if lhs and rhs are equal, ignoring case.
+ */
+bool str_equal_case_insensitive(const str *lhs, const str *rhs);
+
+/**
+ * Returns if lhs and rhs (a C string) are equal, ignoring case.
+ */
+bool str_equal_cstr_case_insensitive(const str *lhs, const char *rhs);
+
+/**
+ * Returns if lhs and rhs (a string slice) are equal, ignoring case.
+ */
+bool str_equal_str_slc_case_insensitive(const str *lhs, const str_slc rhs);
+
+/**
+ * Compares the strings lhs and rhs lexicographically, ignoring case, similar to
+ * strcasecmp.
+ */
+int str_compare_case_insensitive(const str *lhs, const str *rhs);
+
+/**
+ * Compares the strings lhs and rhs (a C string) lexicographically, ignoring
+ * case, similar to strcasecmp.
+ */
+int str_compare_cstr_case_insensitive(const str *lhs, const char *rhs);
+
+/**
+ * Compares the strings lhs and rhs (a String slice) lexicographically, ignoring
+ * case, similar to strcasecmp.
+ */
+int str_compare_str_slc_case_insensitive(const str *lhs, const str_slc rhs);
+
+/**
+ * Slices a string from `begin` to `end`, clamping values to [0, src->len] if
+ * needed, and returning the result.
+ * ptrdiff_t's are used, as slicing functions may return negative values.
+ */
+str str_slice_to_str(const str *src, ptrdiff_t begin, ptrdiff_t end);
+
+/**
+ * Slices a C string from `begin` to `end`, clamping values to [0, src->len] if
+ * needed, returning the result as a str.
+ * ptrdiff_t's are used, as slicing functions may return negative values.
+ */
+str str_slice_from_cstr(const char *src, ptrdiff_t begin, ptrdiff_t end);
+
+/**
+ * Slices a string from `begin` to `end`, clamping values to [0, src->len] if
+ * needed, returning the result as a str_slc.
+ * ptrdiff_t's are used, as slicing functions may return negative values.
+ */
+str_slc str_slice(const str *src, ptrdiff_t begin, ptrdiff_t end);
+
+/**
+ * Slices a string slice from beginning to end, clamping values to [0, src->len]
+ * if needed Python, returning the result as a str.
+ * ptrdiff_t's are used, as slicing functions may return negative values.
+ */
+str str_slice_from_str_slc(const str_slc src, ptrdiff_t begin, ptrdiff_t end);
+
+/**
+ * Converts a str to a double with strtod(3), returning how many bytes of src
+ * were converted to a double.
+ */
+size_t str_to_double_pro(const str *src, double *res);
+
+/**
+ * Converts a str to a int64_t with strtoll(3), returning how many bytes of src
+ * were converted to a double.
+ */
+size_t str_to_int64_pro(const str *src, int64_t *res, int base);
+
+/**
+ * Converts a str to a double with strtod(3), returning a success or failure.
+ */
+bool str_to_double(const str *src, double *res);
+
+/**
+ * Converts a str to a int64_t with strtoll(3), returning a success or failure.
+ */
+bool str_to_int64(const str *src, int64_t *res);
+
+/**
+ * Reads a single line from f into a str, and returns it.
+ * Panics on failure.
+ */
+str str_read_line_from_file(FILE *f);
+
+/**
+ * Reads count characters from f into a str, and returns it.
+ * Panics on failure.
+ */
+str str_read_chars_from_file(FILE *f, size_t count);
+
+/**
+ * Reads the entirety of f into a str, and returns it.
+ * Panics on failure.
+ */
+str str_read_entire_file(FILE *f);
+
+/**
+ * Writes the entirety of a str to a file, returning a success or failure.
+ * Panics on failure.
+ */
+bool str_write_to_file(const str *s, FILE *f);
+
+/**
+ * Gets the nth character of a str, with bounds checking.
+ */
+char str_at(const str *s, size_t i);
+
+/**
+ * Gets the first character of a str, with bounds checking.
+ */
+char str_first(const str *s);
+
+/**
+ * Gets the last character of a str, with bounds checking.
+ */
+char str_last(const str *s);
+
+/**
+ * Finds a character c in s, from the left to the right.
+ * Returns -1 on failure
+ */
+size_t str_find_char(const str *s, char c);
+
+/**
+ * Finds a character c in s from the right to the left.
+ * Returns s->len on failure.
+ */
+size_t str_find_char_right(const str *s, char c);
+
+void str_print(const str *s);
+
+void str_println(const str *s);
+
+void str_fprint(const str *s, FILE *f);
+
+void str_fprintln(const str *s, FILE *f);
+
+/**
+ * Converts a str to a str_slc.
+ */
+str_slc str_slc_from_str(const str *s);
+
+/**
+ * Converts a C string to a str_slc.
+ */
+str_slc str_slc_from_cstr(const char *s);
+
+/**
+ * Equivalent to str_slc_from_cstr.
+ */
+str_slc mstrslc(const char *s);
+
+str str_slc_concat(const str_slc lhs, const str_slc rhs);
+
+str str_slc_concat_cstr(const str_slc lhs, const char *rhs);
+
+bool str_slc_equal(const str_slc lhs, const str_slc rhs);
+
+bool str_slc_equal_cstr(const str_slc lhs, const char *rhs);
+
+bool str_slc_equal_case_insensitive(const str_slc lhs, const str_slc rhs);
+
+bool str_slc_equal_cstr_case_insensitive(const str_slc lhs, const char *rhs);
+
+int str_slc_compare(const str_slc lhs, const str_slc rhs);
+
+int str_slc_compare_cstr(const str_slc lhs, const char *rhs);
+
+int str_slc_compare_case_insensitive(const str_slc lhs, const str_slc rhs);
+
+int str_slc_compare_cstr_case_insensitive(const str_slc lhs, const char *rhs);
+
+/**
+ * Slices a string slice from `begin` to `end`, clamping values to [0, src->len]
+ * if needed, and returning the result.
+ * ptrdiff_t's are used, as slicing functions may return negative values.
+ */
+str_slc str_slc_slice(const str_slc s, ptrdiff_t begin, ptrdiff_t end);
+
+/**
+ * Slices a string slice from `begin` to `end`, clamping values to [0, src->len]
+ * if needed, and returning the result.
+ * ptrdiff_t's are used, as slicing functions may return negative values.
+ */
+str str_slc_slice_to_str(const str_slc s, ptrdiff_t begin, ptrdiff_t end);
+
+str str_slc_to_lower(const str_slc s);
+
+str str_slc_to_upper(const str_slc s);
+
+str_slc str_slc_trim_left(const str_slc s);
+
+str_slc str_slc_trim_right(const str_slc s);
+
+str_slc str_slc_trim(const str_slc s);
+
+size_t str_slc_to_double_pro(const str_slc src, double *res);
+
+size_t str_slc_to_int64_pro(const str_slc src, int64_t *res, int base);
+
+bool str_slc_to_double(const str_slc src, double *res);
+
+bool str_slc_to_int64(const str_slc src, int64_t *res);
+
+bool str_slc_write_to_file(const str_slc src, FILE *f);
+
+char str_slc_first(const str_slc s);
+
+char str_slc_last(const str_slc s);
+
+char str_slc_at(const str_slc s, size_t i);
+
+void str_slc_print(const str_slc s);
+
+void str_slc_println(const str_slc s);
+
+void str_slc_fprint(const str_slc s, FILE *f);
+
+void str_slc_fprintln(const str_slc s, FILE *f);
+
+/**
+ * Finds a character c in s, from the left to the right.
+ * Returns -1 on failure
+ */
+ptrdiff_t str_slc_find_char(const str_slc s, char c);
+
+/**
+ * Finds a character c in s from the right to the left.
+ * Returns s->len on failure.
+ */
+ptrdiff_t str_slc_find_char_right(const str_slc s, char c);
-#define str_fmt(slice) (int)((slice).len), (slice).data
-// Usage: printf("%.*s", format_string(slice));
-
-void print_slice(str_slc s);
-str_slc slc_from_cstr(char* cstr);
-#define slc_from_cstr(cstr) slc_from_cstr(cstr)
-int slc_eq(str_slc a, str_slc b);
-int slc_starts_with(str_slc slice, str_slc prefix);
-str_slc slc_take(str_slc slice, size_t n);
-str_slc slc_drop(str_slc slice, size_t n);
-str_slc slc_trim(str_slc slice);
-void slc_cat(str_slc first, str_slc second, str_slc *new_slc);
-//#define slc_cat(first, second, new_slc) slc_cat(first, second, *new_slc)
+str str_from_after_delim(str *arg, char delimiter);
+str str_from_before_delim(str *arg, char delimiter);
+str_slc str_slc_from_after_delim(str_slc arg, char delimiter);
+str_slc str_slc_from_before_delim(str_slc arg, char delimiter);
#endif
diff --git a/inl b/inl
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/inl
diff --git a/src/files.c b/src/files.c
new file mode 100644
index 0000000..8903557
--- /dev/null
+++ b/src/files.c
@@ -0,0 +1,34 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include "files.h"
+
+bool file_exists(const char *path) {
+ struct stat buffer;
+ return (stat(path, &buffer) == 0);
+}
+
+bool is_directory(const char *path) {
+ struct stat statbuf;
+ if (stat(path, &statbuf) != 0) {
+ return false;
+ }
+ return S_ISDIR(statbuf.st_mode);
+}
diff --git a/src/globs.c b/src/globs.c
index bc4315e..61d3ea6 100644
--- a/src/globs.c
+++ b/src/globs.c
@@ -18,23 +18,65 @@
*/
+#include <stdlib.h>
#include <stdio.h>
#include "globs.h"
+
+#include "files.h"
+#include "lua_vars.h"
#include "str.h"
int is_verbose = 1;
int is_forced = 0;
-char root_config_file[20] = "/etc/pkgit/init.lua";
-char root_config_dir[15] = "/etc/pkgit";
+
+int is_root_config = 0;
+str home_config_dir;
+str home_config_file;
+str home_repos_file;
+str root_config_dir;
+str root_config_file;
+str root_repos_file;
+str config_dir;
+str config_file;
+str repos_file;
+
+str prefix_dir;
+
+str bin;
+str lib;
+str include;
+str src;
void init_vars(void) {
-// char *home = getenv("HOME");
-// if (home) snprintf(home_dir, MAX_PATH_LEN, "%s", home);
-// else snprintf(home_dir, MAX_PATH_LEN, "/root");
+ home_config_dir = str_format("%s/.config/pkgit", getenv("HOME"));
+ home_config_file = str_format("%s/init.lua", home_config_dir.data);
+ home_repos_file = str_format("%s/repos.lua", home_config_dir.data);
+ root_config_dir = str_from_cstr("/etc/pkgit");
+ root_config_file = str_format("%s/init.lua", root_config_dir.data);
+ root_repos_file = str_format("%s/repos.lua", root_config_dir.data);
+ str_copy_into(&config_dir, &home_config_dir);
+ if (file_exists(root_config_file.data)) str_copy_into(&config_dir, &root_config_dir);
+ config_file = str_format("%s/init.lua", config_dir.data);
+ repos_file = str_format("%s/repos.lua", config_dir.data);
+ init_install_directories();
+}
+
+void free_vars(void) {
+ str_free(&home_config_dir);
+ str_free(&home_config_file);
+ str_free(&home_repos_file);
+ str_free(&root_config_dir);
+ str_free(&root_config_file);
+ str_free(&root_repos_file);
+ str_free(&config_dir);
+ str_free(&config_file);
+ str_free(&repos_file);
+}
+
+// old init_vars()
//
// is_root_config = file_exists(root_config);
-//
// if (is_root_config) snprintf(config_dir, MAX_PATH_LEN, "/etc/pkgit");
// else snprintf(config_dir, MAX_PATH_LEN, "%s/.config/pkgit", home_dir);
//
@@ -53,4 +95,3 @@ void init_vars(void) {
// install_directories[2] = strdup(get_install_dir("lib"));
// install_directories[3] = strdup(get_install_dir("inc"));
// install_directories[4] = strdup(get_install_dir("src"));
-}
diff --git a/src/help.c b/src/help.c
new file mode 100644
index 0000000..9bd05a2
--- /dev/null
+++ b/src/help.c
@@ -0,0 +1,79 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can REDistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <stdio.h>
+
+#include "help.h"
+
+#include "globs.h"
+
+void help(void) {
+ typedef struct {
+ const char *short_flag;
+ const char *long_flag;
+ const char *arg;
+ const char *desc;
+ } flag_t;
+
+ static const flag_t cmd_flags[] = {
+ {"-a", "--add", "[url]", "add a repo"},
+ {"-b", "--build", "[path]", "build a package"},
+ {"-d", "--declare", "", "install all packages"},
+ {"-s", "--search", "[pkgs]", "find a package from your repos"},
+ {"-i", "--install", "[pkgs, urls]", "install a package/repo"},
+ {"-r", "--remove", "[pkgs]", "remove an installed package"},
+ {"-l", "--list", "", "list all installed packages"},
+ {"-u", "--update", "", "update all installed packages"},
+ {"-h", "--help", "", "display this help message"},
+ {"-v", "--version", "", "display version number"},
+ {"-c", "--check", "", "run package checks"},
+ };
+
+ static const flag_t mod_flags[] = {
+ {"-q", "--quiet", "", "add a repo"},
+ {"-f", "--force", "", "build a package"},
+ };
+
+ if (is_verbose) {
+ printf(BOLD_MAGENTA " , \n");
+ printf(BOLD_MAGENTA " / \\ \n");
+ printf(BOLD_MAGENTA "_.--' '--._ \n");
+ printf(BOLD_MAGENTA "`'--, ,--'` " COLOR_RESET "pkgit " ITALIC GRAY "- package it!"COLOR_RESET"\n");
+ printf(BOLD_YELLOW " _- " BOLD_MAGENTA "\\ /" BOLD_YELLOW " -_ " MAGENTA VERSION COLOR_RESET "\n");
+ printf(BOLD_YELLOW "'-_ " BOLD_MAGENTA "'" BOLD_YELLOW " _-' \n");
+ printf(BOLD_YELLOW " `'-.-'` " COLOR_RESET "\n");
+ } else {
+ printf("pkgit %s%s- package it!%s\n", ITALIC, GRAY, COLOR_RESET);
+ printf("%s%s%s\n", MAGENTA, VERSION, COLOR_RESET);
+ }
+ printf("\n");
+
+ printf(RED "subcommand flags" COLOR_RESET ":\n");
+ for (size_t i = 0; i < sizeof(cmd_flags)/sizeof(cmd_flags[0]); i++) {
+ printf(COLOR_RESET "\t" GREEN "%-2s" COLOR_RESET ", " YELLOW "%-12s" BLUE "%-16s" GRAY "# %s\n" COLOR_RESET,
+ cmd_flags[i].short_flag, cmd_flags[i].long_flag, cmd_flags[i].arg, cmd_flags[i].desc);
+ }
+
+ printf("\n" RED "modifier flags" COLOR_RESET ":\n");
+ for (size_t i = 0; i < sizeof(mod_flags)/sizeof(mod_flags[0]); i++) {
+ printf(COLOR_RESET "\t" GREEN "%-2s" COLOR_RESET ", " YELLOW "%-12s" BLUE "%-16s" GRAY "# %s\n" COLOR_RESET,
+ mod_flags[i].short_flag, mod_flags[i].long_flag, mod_flags[i].arg, mod_flags[i].desc);
+ }
+}
diff --git a/src/lua_globs.c b/src/lua_globs.c
new file mode 100644
index 0000000..f030acb
--- /dev/null
+++ b/src/lua_globs.c
@@ -0,0 +1,106 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <stdio.h>
+
+#include "lua_globs.h"
+
+#include "globs.h"
+#include "files.h"
+
+lua_State *L = NULL;
+lua_State *B = NULL;
+bool config_loaded = false;
+bool bldit_loaded = false;
+
+void push_lua_path(lua_State *L, const char *new_path) {
+ lua_getglobal(L, "package");
+ lua_getfield(L, -1, "path");
+ const char *current_path = lua_tostring(L, -1);
+ if (!current_path) current_path = "";
+ lua_pop(L, 1);
+ lua_pushfstring(L, "%s;%s", current_path, new_path);
+ lua_setfield(L, -2, "path");
+ lua_pop(L, 1);
+}
+
+void init_lua_state(void) {
+ if (L != NULL) return;
+ L = luaL_newstate();
+ luaL_openlibs(L);
+ char lua_path[MAX_PATH_LEN + 20];
+ snprintf(lua_path, sizeof(lua_path), "%s/?.lua", config_dir.data);
+ push_lua_path(L, lua_path);
+ if (luaL_loadfile(L, config_file.data) || lua_pcall(L, 0, 0, 0)) {
+ printf(
+ "%s cannot run configuration script: %s\n",
+ PRINT_ERROR, lua_tostring(L, -1)
+ );
+ printf(
+ "%s to generate a configuration file, head into the",
+ PRINT_PKGIT
+ );
+ printf(
+ " root directory of the pkgit source and run `make defconfig`\n"
+ );
+ exit(EXIT_FAILURE);
+ }
+ if (file_exists(repos_file.data)) {
+ if (luaL_loadfile(L, repos_file.data) || lua_pcall(L, 0, 0, 0)) {
+ printf(
+ "%s cannot load repository file: %s\n",
+ PRINT_ERROR, lua_tostring(L, -1)
+ );
+ lua_pop(L, 1);
+ }
+ }
+ config_loaded = true;
+}
+
+void init_bldit(void) {
+ if (B != NULL) return;
+ B = luaL_newstate();
+ luaL_openlibs(B);
+ if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) {
+ if (is_verbose) printf(
+ "%s cannot run bldit script: %s\n",
+ PRINT_WARNING, lua_tostring(B, -1)
+ );
+ return;
+ }
+ bldit_loaded = true;
+}
+
+void free_lua_state(void) {
+ if (L != NULL) {
+ lua_close(L);
+ L = NULL;
+ }
+ config_loaded = false;
+}
+
+lua_State *get_lua_state(void) { return L; }
+
+void lua_isnt_type(char* variable, char* type) {
+ if (is_verbose) printf(
+ "%s init.lua: '%s' is not a %s.\n",
+ PRINT_ERROR, variable, type
+ );
+}
diff --git a/src/lua_vars.c b/src/lua_vars.c
new file mode 100644
index 0000000..3519d7a
--- /dev/null
+++ b/src/lua_vars.c
@@ -0,0 +1,68 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include "lua_vars.h"
+
+#include "lua_globs.h"
+#include "globs.h"
+
+void init_install_directories(void) {
+ init_lua_state();
+ if (!lua_istable(L, -1)) {
+ lua_getglobal(L, "install_directories");
+ }
+ if (!lua_istable(L, -1)) {
+ lua_isnt_type("install_directories", "table");
+ return;
+ }
+ //lua_pop(L, 1);
+
+ lua_getfield(L, -1, "bin");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.bin", "string");
+ } else {
+ str_copy_cstr_into(&bin, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "lib");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.lib", "string");
+ } else {
+ str_copy_cstr_into(&lib, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "include");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.include", "string");
+ } else {
+ str_copy_cstr_into(&include, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+
+ lua_getfield(L, -1, "src");
+ if (!lua_isstring(L, -1)) {
+ lua_isnt_type("install_directories.src", "string");
+ } else {
+ str_copy_cstr_into(&src, lua_tostring(L, -1));
+ }
+ lua_pop(L, 1);
+}
diff --git a/src/main.c b/src/main.c
index c531487..893e944 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,12 +18,17 @@
*/
-#include <stdio.h>
-
-#include "cla_parse.h"
+//#include "cla_parse.h"
+#include "pkg_create.h"
#include "str.h"
#include "globs.h"
int main(int argc, char** argv) {
+ (void)argc;
+ init_vars();
+ str_slc arg = str_slc_from_cstr(argv[1]);
+ pkg_create(&arg);
+ //cla_parse(argc, argv);
+ free_vars();
return 0;
}
diff --git a/src/pkg_create.c b/src/pkg_create.c
new file mode 100644
index 0000000..79b4a78
--- /dev/null
+++ b/src/pkg_create.c
@@ -0,0 +1,120 @@
+/*
+
+ pkgit - package it!
+
+ Copyright (C) 2026 dacctal
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+*/
+
+#include <unistd.h>
+#include <string.h>
+
+#include "pkg_create.h"
+
+#include "files.h"
+#include "globs.h"
+#include "str.h"
+
+static str get_destdir(str_slc cwd, str_slc arg) {
+ str destdir;
+ if (str_slc_first(arg) == '.') {
+ str_format_into(
+ &destdir, "%s/%s",
+ src, str_slc_from_after_delim(cwd, '/')
+ );
+ } else {
+ str_format_into(
+ &destdir, "%s/%s",
+ src, arg
+ );
+ }
+ return destdir;
+}
+
+static str get_pkgsrc(package pkg) {
+ str src_dir;
+ if (pkg.is_local == 1) {
+ str_format_into(
+ &src_dir, "%s/%s",
+ src, pkg.name
+ );
+ } else {
+ str_format_into(
+ &src_dir, "%s/%s/%s",
+ src, pkg.name, pkg.version
+ );
+ }
+ return src_dir;
+}
+
+package pkg_create(str_slc *arg) {
+ package pkg;
+ pkg.version = (str) {
+ .data = "HEAD",
+ .len = 4,
+ .cap = 4,
+ };
+ pkg.is_local = false;
+ char cwd[MAX_PATH_LEN];
+ getcwd(cwd, MAX_PATH_LEN);
+ str_slc cwd_slc = mstrslc(cwd);
+ str cwd_str = mstr(cwd);
+ str_slc new_arg;
+ new_arg.data = arg->data;
+ new_arg.len = arg->len;
+ if (!new_arg.data) exit(EXIT_FAILURE);
+ str dest_dir = get_destdir(cwd_slc, new_arg);
+ bool is_installed_locally = is_directory(dest_dir.data);
+
+ str new_arg_str = str_from_str_slc(new_arg);
+ pkg.version = str_from_after_delim(&new_arg_str, '@');
+ str_println(&pkg.version);
+ pkg.target = str_from_after_delim(&new_arg_str, ',');
+ str_println(&pkg.target);
+
+ bool is_in_repos = false;
+ //for (size_t i = 0; i < cached_repos_count; i++) {
+ // if (strcmp(new_arg, cached_repos[i].source_key) == 0) {
+ // is_in_repos = true;
+ // break;
+ // }
+ //}
+
+ if (strncmp(new_arg.data, "http", 4) == 0 || strncmp(new_arg.data, "ssh", 3) == 0) {
+ pkg.url = new_arg_str;
+ pkg.name = str_from_after_delim(&new_arg_str, '/');
+ } else if (strcmp(new_arg.data, ".") == 0) {
+ pkg.url = str_from_cstr("");
+ pkg.name = str_from_after_delim(&cwd_str, '/');
+ pkg.is_local = true;
+ } else if (is_in_repos) {
+ //for (size_t i = 0; i < cached_repos_count; i++) {
+ // if (strcmp(new_arg, cached_repos[i].source_key) == 0) {
+ // pkg.url = strdup(cached_repos[i].source_value);
+ // break;
+ // }
+ //}
+ pkg.name = new_arg_str;
+ } else if (is_installed_locally) {
+ pkg.url = str_from_cstr("");
+ pkg.name = str_from_after_delim(&dest_dir, '/');
+ pkg.is_local = true;
+ } else {
+ printf("%s '%.*s' is not a valid package\n", PRINT_ERROR, str_fmt(&new_arg));
+ exit(EXIT_FAILURE);
+ }
+ pkg.src = get_pkgsrc(pkg);
+ return pkg;
+}
diff --git a/src/str.c b/src/str.c
index 7bb424e..9e67b4a 100644
--- a/src/str.c
+++ b/src/str.c
@@ -1,109 +1,743 @@
/*
- pkgit - package it!
+pkgit - package it!
- Copyright (C) 2026 dacctal
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
+Copyright (C) 2026 dacctal
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
+GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <https://www.gnu.org/licenses/>.
+You should have received a copy of the GNU General Public License
+along with this program.If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stddef.h>
+#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "globs.h"
#include "str.h"
-void slc_print(str_slc slc) {
- for (size_t i = 0; i < slc.len; i++) {
- putchar(slc.data[i]);
+// NOTE: only use this assertion in functions where you don't depend
+// on any other str_ ones, to prevent checking twice
+#ifdef PKGIT_DEBUG
+#define assert_str_is_valid(str) \
+ do { \
+ if (!str_is_valid(str)) \
+ panic("Cannot operate on str `%s`, which is invalid!", #str); \
+ } while (0)
+#else
+#define assert_str_is_valid(str)
+#endif
+
+// XXX: INTERNAL USE ONLY
+#define _strslc(str) \
+ (str_slc) { str->data, str->len }
+#define _cstrslc(str) \
+ (str_slc) { str, strlen(str) }
+// XXX: since msvc/tcc and I presume cproc aren't smart enough to inline calls
+// where a str calls a str_slc function and vice versa, we optimize things
+// ourselves+reduce boilerplate
+#define _STRINGS_EQUAL
+
+str str_new(void) { return str_new_with_capacity(16); }
+
+str str_new_with_capacity(size_t cap) {
+ str res = {0};
+ res.cap = cap;
+ res.data = calloc(res.cap, 1);
+ check_alloc(res.data);
+ return res;
+}
+
+str str_from_cstr(const char *s) { return str_from_str_slc(_cstrslc(s)); }
+
+str mstr(const char *s) { return str_from_str_slc(_cstrslc(s)); }
+
+str str_from_str_slc(const str_slc s) {
+ str res = str_new_with_capacity(s.len + 1);
+ str_copy_str_slc_into(&res, s);
+ return res;
+}
+
+str str_format(const char *format, ...) {
+ va_list args, args_copy;
+ va_start(args, format);
+ va_copy(args_copy, args);
+
+ size_t len = vsnprintf(NULL, 0, format, args_copy);
+ str res = str_new_with_capacity(len + 1);
+ res.len = len;
+ vsnprintf(res.data, res.cap, format, args);
+
+ va_end(args);
+ return res;
+}
+
+void str_format_into(str *s, const char *format, ...) {
+ va_list args, args_copy;
+ va_start(args, format);
+ va_copy(args_copy, args);
+
+ size_t len = vsnprintf(NULL, 0, format, args_copy);
+
+ if (!str_is_valid(s)) {
+ *s = str_new_with_capacity(len + 1);
+ } else {
+ if (s->cap < len + 1)
+ str_reserve_exact(s, len + 1);
+ str_clear(s);
}
+ s->len = len;
+ vsnprintf(s->data, s->cap, format, args);
+
+ va_end(args);
}
-str_slc slc_from_cstr(char* cstr) {
- return (str_slc) {
- .data = cstr,
- .len = strlen(cstr),
- };
+void str_reserve(str *s, size_t new_cap) {
+ str_reserve_exact(s, new_cap + (new_cap >> 2));
+}
+
+void str_reserve_exact(str *s, size_t new_cap) {
+ assert_str_is_valid(s);
+ if (s->cap == new_cap)
+ return;
+ s->data = realloc(s->data, new_cap);
+ s->cap = new_cap;
+ check_alloc(s->data);
+}
+
+void str_clear(str *s) {
+ assert_str_is_valid(s);
+ memset(s->data, 0, s->cap);
+ s->len = 0;
+}
+
+void str_free(str *s) {
+ // let multiple frees be no-ops
+ if (!s->data)
+ return;
+ free(s->data);
+ s->data = NULL;
+}
+
+void str_copy_into(str *dest, const str *src) {
+ str_ncopy_cstr_into(dest, src->data, src->len);
}
-int slc_eq(str_slc a, str_slc b) {
- if (a.len != b.len) return 0;
- for (size_t i = 0; i < a.len; i++) {
- if (a.data[i] != b.data[i]) return 0;
+void str_copy_cstr_into(str *dest, const char *src) {
+ str_ncopy_cstr_into(dest, src, strlen(src));
+}
+
+void str_copy_str_slc_into(str *dest, const str_slc src) {
+ str_ncopy_cstr_into(dest, src.data, src.len);
+}
+
+void str_ncopy_into(str *dest, const str *src, size_t count) {
+ str_ncopy_cstr_into(dest, src->data, count);
+}
+
+void str_ncopy_cstr_into(str *dest, const char *src, size_t count) {
+ assert_str_is_valid(dest);
+ assert(src);
+ dest->len = count;
+ if (dest->cap < count + 1)
+ str_reserve_exact(dest, count + 1);
+ memcpy(dest->data, src, count);
+ dest->data[count] = 0;
+}
+
+void str_ncopy_str_slc_into(str *dest, const str_slc src, size_t count) {
+ str_ncopy_cstr_into(dest, src.data, count);
+}
+
+str str_dupe(const str *src) {
+ assert_str_is_valid(src);
+ str res = str_new_with_capacity(src->cap);
+ str_copy_into(&res, src);
+ return res;
+}
+
+void str_append(str *src, const str *new) {
+ str_append_str_slc(src, _strslc(new));
+}
+
+void str_append_char(str *src, char new) {
+ assert_str_is_valid(src);
+ // new char + nullterm
+ if (src->len + 2 < src->cap) {
+ // this will reserve 1.5*src->cap
+ str_reserve(src, src->cap);
}
- return 1;
+ src->data[src->len++] = new;
+ src->data[src->len] = 0;
+}
+
+void str_append_cstr(str *src, const char *new) {
+ assert(new);
+ str_append_str_slc(src, _cstrslc(new));
}
-int slc_starts_with(str_slc slc, str_slc prefix) {
- if (prefix.len > slc.len) return 0;
- for (size_t i = 0; i < prefix.len; i++) {
- if (slc.data[i] != prefix.data[i]) return 0;
+void str_append_str_slc(str *src, const str_slc new) {
+ assert_str_is_valid(src);
+ assert_str_is_valid(&new);
+ size_t len_after_append = src->len + new.len;
+ if (len_after_append + 1 > src->cap) {
+ src->cap = len_after_append + 1;
+ str_reserve(src, src->cap);
}
- return 1;
+ memcpy(src->data + src->len, new.data, new.len + 1);
+ src->len = len_after_append;
+ src->data[len_after_append] = 0;
}
-str_slc slc_take(str_slc slc, size_t n) {
- if (n > slc.len) n = slc.len;
- return (str_slc) {
- .data = slc.data,
- .len = n
- };
+char str_pop_last(str *src) {
+ char c = str_last(src);
+ src->len--;
+ return c;
+}
+
+str str_concat(const str *lhs, const str *rhs) {
+ str res = str_dupe(lhs);
+ str_append(&res, rhs);
+ return res;
+}
+
+str str_concat_cstr(const str *lhs, const char *rhs) {
+ str res = str_dupe(lhs);
+ str_append_cstr(&res, rhs);
+ return res;
+}
+
+str str_concat_str_slc(const str *lhs, const str_slc rhs) {
+ str res = str_dupe(lhs);
+ str_append_str_slc(&res, rhs);
+ return res;
+}
+
+str str_trim_left(const str *s) {
+ str res = str_dupe(s);
+ str_trim_left_in_place(&res);
+ return res;
+}
+
+str str_trim_right(const str *s) {
+ str res = str_dupe(s);
+ str_trim_right_in_place(&res);
+ return res;
+}
+
+str str_trim(const str *s) {
+ str res = str_dupe(s);
+ str_trim_in_place(&res);
+ return res;
+}
+
+str str_to_upper(const str *s) {
+ str res = str_dupe(s);
+ str_to_upper_in_place(&res);
+ return res;
+}
+
+str str_to_lower(const str *s) {
+ str res = str_dupe(s);
+ str_to_lower_in_place(&res);
+ return res;
+}
+
+void str_trim_left_in_place(str *s) {
+ assert_str_is_valid(s);
+ size_t i;
+ for (i = 0; i < s->len && isspace(s->data[i]); i++)
+ continue;
+ s->len -= i;
+ memmove(s->data, s->data + i, s->len);
+ s->data[s->len] = 0;
+}
+
+void str_trim_right_in_place(str *s) {
+ assert_str_is_valid(s);
+ if (s->len == 0)
+ return;
+ size_t end;
+ for (end = s->len - 1; isspace(s->data[end]); end--)
+ continue;
+ end++;
+ s->len = end;
+ s->data[s->len] = 0;
+}
+
+void str_trim_in_place(str *s) {
+ assert_str_is_valid(s);
+ if (s->len == 0)
+ return;
+ size_t begin, end;
+ for (begin = 0; begin < s->len && isspace(s->data[begin]); begin++)
+ continue;
+ for (end = s->len - 1; isspace(s->data[end]); end--)
+ continue;
+ end++;
+ s->len = end - begin;
+ memmove(s->data, s->data + begin, s->len);
+ s->data[s->len] = 0;
+}
+
+void str_to_upper_in_place(str *s) {
+ assert_str_is_valid(s);
+ for (size_t i = 0; i < s->len; i++)
+ s->data[i] = toupper(s->data[i]);
+}
+
+void str_to_lower_in_place(str *s) {
+ assert_str_is_valid(s);
+ for (size_t i = 0; i < s->len; i++)
+ s->data[i] = tolower(s->data[i]);
+}
+
+bool str_equal(const str *lhs, const str *rhs) {
+ return str_slc_equal(_strslc(lhs), _strslc(rhs));
+}
+
+bool str_equal_cstr(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_equal(_strslc(lhs), _cstrslc(rhs));
+}
+
+bool str_equal_str_slc(const str *lhs, const str_slc rhs) {
+ return str_slc_equal(_strslc(lhs), rhs);
+}
+
+int str_compare(const str *lhs, const str *rhs) {
+ return str_slc_compare(_strslc(lhs), _strslc(rhs));
+}
+
+int str_compare_cstr(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_compare(_strslc(lhs), _cstrslc(rhs));
+}
+
+int str_compare_str_slc(const str *lhs, const str_slc rhs) {
+ return str_slc_compare(_strslc(lhs), rhs);
+}
+
+bool str_equal_case_insensitive(const str *lhs, const str *rhs) {
+ return str_slc_equal_case_insensitive(_strslc(lhs), _strslc(rhs));
+}
+
+bool str_equal_cstr_case_insensitive(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_equal_case_insensitive(_strslc(lhs), _cstrslc(rhs));
+}
+
+bool str_equal_str_slc_case_insensitive(const str *lhs, const str_slc rhs) {
+ return str_slc_equal_case_insensitive(_strslc(lhs), rhs);
+}
+
+int str_compare_case_insensitive(const str *lhs, const str *rhs) {
+ return str_slc_compare_case_insensitive(_strslc(lhs), _strslc(rhs));
+}
+
+int str_compare_cstr_case_insensitive(const str *lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_compare_case_insensitive(_strslc(lhs), _cstrslc(rhs));
+}
+
+int str_compare_str_slc_case_insensitive(const str *lhs, const str_slc rhs) {
+ return str_slc_compare_case_insensitive(_strslc(lhs), rhs);
+}
+
+str str_slice_to_str(const str *src, ptrdiff_t begin, ptrdiff_t end) {
+ str_slc slc = str_slc_slice(_strslc(src), begin, end);
+ return str_from_str_slc(slc);
+}
+
+str str_slice_from_cstr(const char *src, ptrdiff_t begin, ptrdiff_t end) {
+ str_slc slc = str_slc_slice(_cstrslc(src), begin, end);
+ return str_from_str_slc(slc);
+}
+
+str_slc str_slice(const str *src, ptrdiff_t begin, ptrdiff_t end) {
+ return str_slc_slice(_strslc(src), begin, end);
+}
+
+str str_slice_from_str_slc(const str_slc src, ptrdiff_t begin, ptrdiff_t end) {
+ str_slc slc = str_slc_slice(src, begin, end);
+ return str_from_str_slc(slc);
}
-str_slc slc_drop(str_slc slc, size_t n) {
- if (n > slc.len) n = slc.len;
- return (str_slc) {
- .data = slc.data + n,
- .len = slc.len - n
+size_t str_to_double_pro(const str *src, double *res) {
+ return str_slc_to_double_pro(_strslc(src), res);
+}
+
+size_t str_to_int64_pro(const str *src, int64_t *res, int base) {
+ return str_slc_to_int64_pro(_strslc(src), res, base);
+}
+
+bool str_to_double(const str *src, double *res) {
+ return str_to_double_pro(src, res) == src->len;
+}
+
+bool str_to_int64(const str *src, int64_t *res) {
+ return str_to_int64_pro(src, res, 10) == src->len;
+}
+
+char str_at(const str *s, size_t i) {
+ assert_str_is_valid(s);
+#ifndef PKGIT_DEBUG
+ if (i >= s->len)
+ panic("Out of bounds string access at index %zu (length: %zu)", i,
+ s->len);
+#endif
+ return s->data[i];
+}
+
+char str_first(const str *s) {
+ assert_str_is_valid(s);
+#ifndef PKGIT_DEBUG
+ if (!s->len)
+ panic("Attempted to get the first character of an empty string");
+#endif
+ return *s->data;
+}
+
+char str_last(const str *s) {
+ assert_str_is_valid(s);
+#ifndef PKGIT_DEBUG
+ if (!s->len)
+ panic("Attempted to get the last character of an empty string");
+#endif
+ return s->data[s->len - 1];
+}
+
+size_t str_find_char(const str *s, char c) {
+ return str_slc_find_char(_strslc(s), c);
+}
+
+size_t str_find_char_right(const str *s, char c) {
+ return str_slc_find_char_right(_strslc(s), c);
+}
+
+void str_print(const str *s) { printf("%.*s", str_fmt(s)); }
+
+void str_println(const str *s) { printf("%.*s\n", str_fmt(s)); }
+
+void str_fprint(const str *s, FILE *f) { fprintf(f, "%.*s", str_fmt(s)); }
+
+void str_fprintln(const str *s, FILE *f) { fprintf(f, "%.*s\n", str_fmt(s)); }
+
+str str_read_line_from_file(FILE *f) {
+ size_t len = 0;
+ char c;
+ for (len = 0; (c = fgetc(f)) != EOF && c != '\n'; len++)
+ continue;
+ fseek(f, -len - 1, SEEK_SET);
+ str res = str_new_with_capacity(len + 1);
+ size_t bytes_read = fread(res.data, 1, len, f);
+ assert(bytes_read == len);
+ res.len = len;
+ return res;
+}
+
+str str_read_chars_from_file(FILE *f, size_t count) {
+ str res = str_new_with_capacity(count + 1);
+ size_t bytes_read = fread(res.data, 1, count, f);
+ assert(bytes_read == count);
+ res.len = count;
+ return res;
+}
+
+str str_read_entire_file(FILE *f) {
+ fseek(f, 0, SEEK_END);
+ size_t len = ftell(f);
+ rewind(f);
+ str res = str_new_with_capacity(len + 1);
+ size_t bytes_read = fread(res.data, 1, len, f);
+ assert(bytes_read == len);
+ res.len = len;
+ return res;
+}
+
+bool str_write_to_file(const str *s, FILE *f) {
+ return str_slc_write_to_file(_strslc(s), f);
+}
+
+// === str_slc implementation ===
+
+str_slc str_slc_from_str(const str *s) { return _strslc(s); }
+
+str_slc str_slc_from_cstr(const char *s) { return _cstrslc(s); }
+
+str_slc mstrslc(const char *s) { return (str_slc){s, strlen(s)}; }
+
+str str_slc_concat(const str_slc lhs, const str_slc rhs) {
+ str res = str_new();
+ str_append_str_slc(&res, lhs);
+ str_append_str_slc(&res, rhs);
+ return res;
+}
+
+str str_slc_concat_cstr(const str_slc lhs, const char *rhs) {
+ str res = str_new();
+ str_append_str_slc(&res, lhs);
+ str_append_cstr(&res, rhs);
+ return res;
+}
+
+bool str_slc_equal(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ return (lhs.len == rhs.len) && memcmp(lhs.data, rhs.data, lhs.len) == 0;
+}
+
+bool str_slc_equal_cstr(const str_slc lhs, const char *rhs) {
+ assert(rhs);
+ return str_slc_equal(lhs, _cstrslc(rhs));
+}
+
+bool str_slc_equal_case_insensitive(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ if (lhs.len != rhs.len)
+ return false;
+ for (size_t i = 0; i < lhs.len; i++)
+ if (tolower(lhs.data[i]) != tolower(rhs.data[i]))
+ return false;
+ return true;
+}
+
+bool str_slc_equal_cstr_case_insensitive(const str_slc lhs, const char *rhs) {
+ return str_slc_equal_case_insensitive(lhs, _cstrslc(rhs));
+}
+
+int str_slc_compare(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ if (lhs.len < rhs.len)
+ return -1;
+ else if (lhs.len > rhs.len)
+ return 1;
+ for (size_t i = 0; i < lhs.len; i++)
+ if (lhs.data[i] != rhs.data[i])
+ return (uint8_t)lhs.data[i] - (uint8_t)rhs.data[i];
+ return 0;
+}
+
+int str_slc_compare_cstr(const str_slc lhs, const char *rhs) {
+ return str_slc_compare(lhs, _cstrslc(rhs));
+}
+
+int str_slc_compare_case_insensitive(const str_slc lhs, const str_slc rhs) {
+ assert_str_is_valid(&lhs);
+ assert_str_is_valid(&rhs);
+ if (lhs.len < rhs.len)
+ return -1;
+ else if (lhs.len > rhs.len)
+ return 1;
+ char cl, cr;
+ for (size_t i = 0; i < lhs.len; i++)
+ if ((cl = tolower(lhs.data[i])) != (cr = tolower(rhs.data[i])))
+ return (uint8_t)cl - (uint8_t)cr;
+ return 0;
+}
+
+int str_slc_compare_cstr_case_insensitive(const str_slc lhs, const char *rhs) {
+ return str_slc_compare_case_insensitive(lhs, _cstrslc(rhs));
+}
+
+str_slc str_slc_slice(const str_slc src, ptrdiff_t begin, ptrdiff_t end) {
+ assert_str_is_valid(&src);
+ if (begin > end)
+ return (str_slc){src.data, 0};
+ if (end < 0)
+ end = 0;
+ else if (end > (ptrdiff_t)src.len)
+ end = src.len;
+ if (begin < 0)
+ begin = 0;
+ else if (begin > (ptrdiff_t)src.len)
+ begin = src.len;
+ str_slc res = {
+ .data = src.data + begin,
+ .len = end - begin,
};
+ return res;
}
-str_slc slc_trim(str_slc slc) {
- while (slc.len > 0 && isspace((unsigned char)slc.data[0])) {
- slc = slc_drop(slc, 1);
- }
- while (slc.len > 0 && isspace((unsigned char)slc.data[slc.len - 1])) {
- slc = slc_take(slc, slc.len - 1);
- }
- return slc;
+// Equivalent to str_slice_from_str_slc
+str str_slc_slice_to_str(const str_slc s, ptrdiff_t begin, ptrdiff_t end) {
+ return str_slice_from_str_slc(s, begin, end);
}
-str_slc slc_split(str_slc *slc, char delimiter) {
- size_t i = 0;
- while(i < slc->len && slc->data[i] != delimiter) {
- i++;
- }
+str str_slc_to_lower(const str_slc s) {
+ str res = str_from_str_slc(s);
+ str_to_lower_in_place(&res);
+ return res;
+}
- str_slc result = slc_take(*slc, i);
- if (i < slc->len) {
- *slc = slc_drop(*slc, i + i);
- } else {
- *slc = slc_drop(*slc, i);
- }
- return result;
+str str_slc_to_upper(const str_slc s) {
+ str res = str_from_str_slc(s);
+ str_to_upper_in_place(&res);
+ return res;
}
-void slc_cat(str_slc first, str_slc second, str_slc *new_slc) {
- if (new_slc->len < first.len + second.len) {
- printf("fatal: slc_cat length of new slice is too small\n");
- exit(EXIT_FAILURE);
- }
- for (size_t i = 0; i < first.len; i++) {
- new_slc->data[i] = first.data[i];
- }
- for (size_t i = first.len; i < new_slc->len; i++) {
- new_slc->data[i] = second.data[i-first.len];
+str_slc str_slc_trim_left(const str_slc s) {
+ assert_str_is_valid(&s);
+ size_t i;
+ for (i = 0; i < s.len && isspace(s.data[i]); i++)
+ continue;
+ return (str_slc){s.data + i, s.len - i};
+}
+
+str_slc str_slc_trim_right(const str_slc s) {
+ if (s.len == 0)
+ return s;
+ size_t end;
+ for (end = s.len - 1; isspace(s.data[end]); end--)
+ continue;
+ end++;
+ return (str_slc){s.data, end};
+}
+
+str_slc str_slc_trim(const str_slc s) {
+ if (s.len == 0)
+ return s;
+ size_t begin, end;
+ for (begin = 0; begin < s.len && isspace(s.data[begin]); begin++)
+ continue;
+ for (end = s.len - 1; isspace(s.data[end]); end--)
+ continue;
+ end++;
+ return (str_slc){s.data + begin, end - begin};
+}
+
+size_t str_slc_to_double_pro(const str_slc src, double *res) {
+ assert_str_is_valid(&src);
+ errno = 0;
+ char *endptr = NULL;
+ const char *nptr = src.data;
+ double num = strtod(nptr, &endptr);
+ size_t diff = endptr - nptr;
+ *res = num;
+ return diff;
+}
+
+size_t str_slc_to_int64_pro(const str_slc src, int64_t *res, int base) {
+ assert_str_is_valid(&src);
+ errno = 0;
+ char *endptr = NULL;
+ const char *nptr = src.data;
+ int64_t num = strtoll(nptr, &endptr, base);
+ size_t diff = endptr - nptr;
+ *res = num;
+ return diff;
+}
+
+bool str_slc_to_double(const str_slc src, double *res) {
+ return str_slc_to_double_pro(src, res) == src.len;
+}
+
+bool str_slc_to_int64(const str_slc src, int64_t *res) {
+ return str_slc_to_int64_pro(src, res, 10) == src.len;
+}
+
+bool str_slc_write_to_file(const str_slc s, FILE *f) {
+ return s.len == fwrite(s.data, 1, s.len, f);
+}
+
+char str_slc_first(const str_slc s) {
+ assert_str_is_valid(&s);
+#ifndef PKGIT_DEBUG
+ if (!s.len)
+ panic("Attempted to get the first character of an empty string");
+#endif
+ return *s.data;
+}
+
+char str_slc_last(const str_slc s) {
+ assert_str_is_valid(&s);
+#ifndef PKGIT_DEBUG
+ if (!s.len)
+ panic("Attempted to get the last character of an empty string");
+#endif
+ return s.data[s.len - 1];
+}
+
+char str_slc_at(const str_slc s, size_t i) {
+ assert_str_is_valid(&s);
+#ifndef PKGIT_DEBUG
+ if (i >= s.len)
+ panic("Out of bounds string access at index %zu (length: %zu)", i,
+ s.len);
+#endif
+ return s.data[i];
+}
+
+void str_slc_print(const str_slc s) { printf("%.*s", str_fmt(&s)); }
+
+void str_slc_println(const str_slc s) { printf("%.*s\n", str_fmt(&s)); }
+
+void str_slc_fprint(const str_slc s, FILE *f) {
+ fprintf(f, "%.*s", str_fmt(&s));
+}
+
+void str_slc_fprintln(const str_slc s, FILE *f) {
+ fprintf(f, "%.*s\n", str_fmt(&s));
+}
+
+ptrdiff_t str_slc_find_char(const str_slc s, char c) {
+ size_t i;
+ for (i = 0; i < s.len; i++)
+ if (s.data[i] == c)
+ break;
+ return (i == s.len) ? 0 : i;
+}
+
+ptrdiff_t str_slc_find_char_right(const str_slc s, char c) {
+ if (s.len == 0)
+ return 0;
+ for (ptrdiff_t i = s.len - 1; i >= 0; i--) {
+ if (s.data[i] == c)
+ return (size_t)i;
}
+ return s.len;
+}
+
+str str_from_after_delim(str *arg, char delimiter) {
+ size_t i = str_find_char_right(arg, delimiter);
+ str res = str_slice_to_str(arg, i + 1, arg->len);
+ str_println(&res);
+ return res;
+}
+
+str str_from_before_delim(str *arg, char delimiter) {
+ size_t i = str_find_char(arg, delimiter);
+ str res = str_slice_to_str(arg, i + 1, arg->len);
+ str_println(&res);
+ return res;
+}
+
+str_slc str_slc_from_after_delim(str_slc arg, char delimiter) {
+ size_t i = str_slc_find_char_right(arg, delimiter);
+ str_slc res = str_slc_slice(arg, i + 1, arg.len);
+ str_slc_println(res);
+ return res;
+}
+
+str_slc str_slc_from_before_delim(str_slc arg, char delimiter) {
+ size_t i = str_slc_find_char(arg, delimiter);
+ str_slc res = str_slc_slice(arg, i + 1, arg.len);
+ str_slc_println(res);
+ return res;
}