diff options
Diffstat (limited to 'refactor.patch')
| -rw-r--r-- | refactor.patch | 1060 |
1 files changed, 1060 insertions, 0 deletions
diff --git a/refactor.patch b/refactor.patch new file mode 100644 index 0000000..6f57749 --- /dev/null +++ b/refactor.patch @@ -0,0 +1,1060 @@ +diff --git a/cla_parse.c b/cla_parse.c +index 59c8bda..8d5db4f 100644 +--- a/cla_parse.c ++++ b/cla_parse.c +@@ -25,31 +25,31 @@ + + #include "globs.h" + #include "str.h" +-//#include "declare.h" +-//#include "deps_resolve.h" ++// #include "declare.h" ++// #include "deps_resolve.h" + #include "help.h" +-//#include "name_from_url.h" +-//#include "pkg_build.h" ++// #include "name_from_url.h" ++// #include "pkg_build.h" + #include "pkg_create.h" +-//#include "pkg_search.h" +-//#include "pkg_install.h" +-//#include "pkg_list.h" +-//#include "pkg_remove.h" +-//#include "pkgit_globals.h" +-//#include "repo_add.h" +-//#include "update.h" ++// #include "pkg_search.h" ++// #include "pkg_install.h" ++// #include "pkg_list.h" ++// #include "pkg_remove.h" ++// #include "pkgit_globals.h" ++// #include "repo_add.h" ++// #include "update.h" + +-#define COMMAND(arg, large, small, code) if ( \ +- strcmp(arg, large) == 0 || strcmp(arg, small) == 0) 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", \ +- PRINT_ERROR, arg, next) ++#define NOT_ENOUGH_ARGS(arg, next) \ ++ log_error("Not enough arguments! Try: pkgit %s [%s]", (arg), (next)) + + void cmd_add(char **argv, int i) { + if (argv[i + 1]) { + printf("add repo %s\n", argv[i + 1]); +- //repo_add(argv[i + 1], name_from_url(argv[i + 1])); ++ // repo_add(argv[i + 1], name_from_url(argv[i + 1])); + } else { + NOT_ENOUGH_ARGS(argv[i], "url"); + } +@@ -58,50 +58,58 @@ void cmd_add(char **argv, int i) { + 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; ++ if (argv[j][0] == '-') ++ continue; + printf("build pkg %s\n", argv[j]); +- //pkg = pkg_create(argv[j]); +- //pkg_build(pkg); ++ // pkg = pkg_create(argv[j]); ++ // pkg_build(pkg); + } + } else { + printf("build pkg .\n"); +- //pkg = pkg_create("."); +- //pkg_build(pkg); ++ // pkg = pkg_create("."); ++ // pkg_build(pkg); + } + } + + 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; ++ if (argv[j][0] == '-') ++ continue; + printf("install pkg %s\n", argv[j]); +- //pkg = pkg_create(argv[j]); +- //pkg_install(pkg); ++ // pkg = pkg_create(argv[j]); ++ // pkg_install(pkg); + } + } else { +- //NOT_ENOUGH_ARGS(argv[i], "url/pkg"); ++ // NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } + } + + 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; ++ if (argv[j][0] == '-') ++ continue; + printf("remove pkg %s\n", argv[j]); +- //pkg = pkg_create(argv[j]); +- //pkg_remove(pkg); ++ // pkg = pkg_create(argv[j]); ++ // pkg_remove(pkg); + } + } else { +- //NOT_ENOUGH_ARGS(argv[i], "url/pkg"); ++ // NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } + } + + void flags_mod(char **argv, int i) { + for (size_t j = 1; j < strlen(argv[i]); j++) { + switch (argv[i][j]) { +- case 'q': is_verbose = 0; break; +- case 'f': is_forced = 1; break; +- default: break; ++ case 'q': ++ is_verbose = 0; ++ break; ++ case 'f': ++ is_forced = 1; ++ break; ++ default: ++ break; + } + } + } +@@ -109,25 +117,49 @@ void flags_mod(char **argv, int i) { + void flags_cmd(int argc, char **argv, int i, package pkg) { + for (size_t j = 1; j < strlen(argv[i]); j++) { + switch (argv[i][j]) { +- case 'a': cmd_add(argv, i); break; +- case 'b': cmd_build(argc, argv, i, pkg); break; +- case 'c': /*deps_resolve();*/ printf("deps_resolve\n"); break; +- case 'd': /*declare();*/ printf("declare\n"); break; +- case 'i': cmd_install(argc, argv, i, pkg); break; +- case 'r': cmd_remove(argc, argv, i, pkg); break; +- case 'u': /*update();*/ printf("update\n"); break; +- 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(); break; +- default: break; ++ case 'a': ++ cmd_add(argv, i); ++ break; ++ case 'b': ++ cmd_build(argc, argv, i, pkg); ++ break; ++ case 'c': /*deps_resolve();*/ ++ printf("deps_resolve\n"); ++ break; ++ case 'd': /*declare();*/ ++ printf("declare\n"); ++ break; ++ case 'i': ++ cmd_install(argc, argv, i, pkg); ++ break; ++ case 'r': ++ cmd_remove(argc, argv, i, pkg); ++ break; ++ case 'u': /*update();*/ ++ printf("update\n"); ++ break; ++ 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(); ++ 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][0] != '-') ++ continue; + str_slc arg = str_slc_from_cstr(argv[i]); + if (argv[i][1] == '-') { + COMMAND(argv[i], "--quiet", "-q", { is_verbose = 0; }); +@@ -141,22 +173,26 @@ void flags_parse(int argc, char **argv, package pkg) { + + void cmds_parse(int argc, char **argv, package pkg) { + for (int i = 1; i < argc; i++) { +- 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();*/ }); ++ 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 +index 9fd1a71..24712e9 100644 +--- a/include/files.h ++++ b/include/files.h +@@ -21,7 +21,6 @@ + #ifndef PKGIT_FILES_H + #define PKGIT_FILES_H + +-#include <sys/stat.h> + #include <stdbool.h> + + bool file_exists(const char *path); +diff --git a/include/globs.h b/include/globs.h +index f44ab85..4611216 100644 +--- a/include/globs.h ++++ b/include/globs.h +@@ -21,6 +21,9 @@ + #ifndef PKGIT_GLOBALS_H + #define PKGIT_GLOBALS_H + ++#include "str.h" ++#include <stdint.h> ++ + #define VERSION "1.4.0_INDEV" + #define RED "\x1b[0;31m" + #define GREEN "\x1b[0;32m" +@@ -90,48 +93,38 @@ + #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 PKGIT_PREFIX \ ++ BOLD_YELLOW "[" BOLD_MAGENTA "pkgit" BOLD_YELLOW "] " COLOR_RESET ++#define PKGIT_PREFIX_SUCCESS PKGIT_PREFIX GREEN "[SUCCESS] " COLOR_RESET ++#define PKGIT_PREFIX_INFO PKGIT_PREFIX BLUE "[INFO] " COLOR_RESET ++#define PKGIT_PREFIX_WARNING PKGIT_PREFIX YELLOW "[WARNING] " COLOR_RESET ++#define PKGIT_PREFIX_ERROR PKGIT_PREFIX RED "[ERROR] " COLOR_RESET + + #define MAX_REPOS 1000 + #define MAX_DIRS 100 + #define MAX_PATH_LEN 1024 + +-#include "str.h" ++typedef struct { ++ str name, url, version, target, src; ++ bool is_local; ++} package_t; + + typedef struct { +- str name; +- str url; +- str version; +- str target; +- str src; +- int is_local; +-} package; +- +-extern int is_verbose; +-extern int is_forced; +-extern int config_exists; +- +-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; ++ bool verbose, force; ++} cli_flags_t; ++ ++typedef struct { ++ str dir, name, content, repos; ++ bool is_root_config; ++} config_t; ++ ++typedef struct { ++ str prefix, bin, lib, include, src; ++} install_dirs_t; ++ ++extern cli_flags_t flags; ++extern config_t cfg; ++extern install_dirs_t inst_dirs; + + void init_vars(void); + void free_vars(void); +diff --git a/include/lua_vars.h b/include/lua_vars.h +index dfc62d0..439a335 100644 +--- a/include/lua_vars.h ++++ b/include/lua_vars.h +@@ -20,8 +20,10 @@ + + #ifndef PKGIT_LUA_VARS_H + #define PKGIT_LUA_VARS_H +-#include <lua.h> ++ + #include <lauxlib.h> ++#include <lua.h> + #include <lualib.h> ++ + void init_install_directories(void); + #endif +diff --git a/include/pkg_create.h b/include/pkg_create.h +index 908b6fd..95e12c6 100644 +--- a/include/pkg_create.h ++++ b/include/pkg_create.h +@@ -21,5 +21,5 @@ + #ifndef PKGIT_PKG_CREATE_H + #define PKGIT_PKG_CREATE_H + #include "globs.h" +-package pkg_create(str_slc *arg); ++package_t pkg_create(str_slc arg); + #endif +diff --git a/inl b/inl +deleted file mode 100644 +index e69de29..0000000 +diff --git a/src/files.c b/src/files.c +index 8903557..7bfc507 100644 +--- a/src/files.c ++++ b/src/files.c +@@ -19,6 +19,7 @@ + */ + + #include "files.h" ++#include <sys/stat.h> + + bool file_exists(const char *path) { + struct stat buffer; +diff --git a/src/globs.c b/src/globs.c +index 61d3ea6..519e5f9 100644 +--- a/src/globs.c ++++ b/src/globs.c +@@ -19,79 +19,46 @@ + */ + + #include <stdlib.h> +-#include <stdio.h> + + #include "globs.h" + + #include "files.h" ++#include "log.h" + #include "lua_vars.h" + #include "str.h" + +-int is_verbose = 1; +-int is_forced = 0; ++install_dirs_t inst_dirs = {0}; ++cli_flags_t flags = {0}; ++config_t cfg = {0}; + +-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; ++void init_vars(void) { ++ if (file_exists("/etc/pkgit/repos.lua")) { ++ cfg.dir = mstr("/etc/pkgit"); ++ cfg.name = mstr("/etc/pkgit/repos.lua"); + +-str bin; +-str lib; +-str include; +-str src; ++ } else { ++ const char *tmp = getenv("XDG_CONFIG_HOME"); ++ if (tmp) { ++ cfg.dir = str_format("%s/pkgit", tmp); ++ } else { ++ cfg.dir = str_format("%s/.config/pkgit", getenv("HOME")); ++ } ++ cfg.name = str_concat_cstr(&cfg.dir, "/init.lua"); ++ } ++ // TODO: read into cfg.content + +-void init_vars(void) { +- 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); ++ cfg.repos = str_format("%.*s/repos.lua", str_fmt(&cfg.dir)); + 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); ++ str_free(&cfg.dir); ++ str_free(&cfg.name); ++ str_free(&cfg.content); ++ str_free(&cfg.repos); ++ str_free(&inst_dirs.prefix); ++ str_free(&inst_dirs.bin); ++ str_free(&inst_dirs.lib); ++ str_free(&inst_dirs.include); ++ str_free(&inst_dirs.src); + } +- +-// 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); +-// +-// snprintf(config_file, MAX_PATH_LEN, "%s/init.lua", config_dir); +-// snprintf(repo_file, MAX_PATH_LEN, "%s/repos.lua", config_dir); +-// +-// config_exists = file_exists(root_config) || file_exists(home_config()); +-// +-// snprintf(bin, MAX_PATH_LEN, "%s/.local/bin", home_dir); +-// snprintf(lib, MAX_PATH_LEN, "%s/.local/lib", home_dir); +-// snprintf(inc, MAX_PATH_LEN, "%s/.local/include", home_dir); +-// snprintf(src, MAX_PATH_LEN, "%s/.local/share/pkgit", home_dir); +-// +-// install_directories[0] = config_dir; +-// install_directories[1] = strdup(get_install_dir("bin")); +-// 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 +index 9bd05a2..8580844 100644 +--- a/src/help.c ++++ b/src/help.c +@@ -33,30 +33,32 @@ void help(void) { + } 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"}, ++ {"-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"}, ++ {"-q", "--quiet", "", "add a repo"}, ++ {"-f", "--force", "", "build a package"}, + }; + +- if (is_verbose) { ++ if (flags.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_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 { +@@ -66,14 +68,18 @@ void help(void) { + 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); ++ 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); ++ 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 +index f030acb..5a8e51c 100644 +--- a/src/lua_globs.c ++++ b/src/lua_globs.c +@@ -18,12 +18,11 @@ + + */ + +-#include <stdio.h> +- + #include "lua_globs.h" +- +-#include "globs.h" + #include "files.h" ++#include "globs.h" ++#include "log.h" ++#include "str.h" + + lua_State *L = NULL; + lua_State *B = NULL; +@@ -34,7 +33,8 @@ 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 = ""; ++ if (!current_path) ++ current_path = ""; + lua_pop(L, 1); + lua_pushfstring(L, "%s;%s", current_path, new_path); + lua_setfield(L, -2, "path"); +@@ -42,47 +42,37 @@ void push_lua_path(lua_State *L, const char *new_path) { + } + + void init_lua_state(void) { +- if (L != NULL) return; ++ 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" +- ); ++ str lua_path = str_format("%.*s/?.lua", str_fmt(&cfg.dir)); ++ push_lua_path(L, lua_path.data); ++ if (luaL_loadfile(L, cfg.name.data) || lua_pcall(L, 0, 0, 0)) { ++ log_error("cannot run configuration script: %s", lua_tostring(L, -1)); ++ log_pkgit("to generate a configuration file, head into the"); ++ log_pkgit( ++ "root directory of the pkgit source and run `make defconfig`"); + 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) +- ); ++ if (file_exists(cfg.repos.data)) { ++ if (luaL_loadfile(L, cfg.repos.data) || lua_pcall(L, 0, 0, 0)) { ++ log_warn("cannot load repository file: %s", lua_tostring(L, -1)); + lua_pop(L, 1); + } + } ++ str_free(&lua_path); + config_loaded = true; + } + + void init_bldit(void) { +- if (B != NULL) return; ++ 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) +- ); ++ if (flags.verbose) ++ log_warn("cannot run bldit: %s", lua_tostring(B, -1)); + return; + } + bldit_loaded = true; +@@ -96,11 +86,11 @@ void free_lua_state(void) { + config_loaded = false; + } + +-lua_State *get_lua_state(void) { return L; } ++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 +- ); ++void lua_isnt_type(char *variable, char *type) { ++ if (flags.verbose) ++ log_error("init.lua: '%s' is not a %s.", variable, type); + } +diff --git a/src/lua_vars.c b/src/lua_vars.c +index 3519d7a..3fd2ddb 100644 +--- a/src/lua_vars.c ++++ b/src/lua_vars.c +@@ -20,8 +20,8 @@ + + #include "lua_vars.h" + +-#include "lua_globs.h" + #include "globs.h" ++#include "lua_globs.h" + + void init_install_directories(void) { + init_lua_state(); +@@ -32,37 +32,26 @@ void init_install_directories(void) { + 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); ++#define SETUP_INST_DIRS \ ++ X(bin) \ ++ X(lib) \ ++ X(include) \ ++ X(src) ++ ++#define X(dir) \ ++ do { \ ++ lua_getfield(L, -1, #dir); \ ++ if (!lua_isstring(L, -1)) { \ ++ lua_isnt_type("install_directories." #dir, "string"); \ ++ } else { \ ++ str_copy_cstr_into(&inst_dirs.dir, lua_tostring(L, -1)); \ ++ } \ ++ lua_pop(L, 1); \ ++ } while (0); ++ ++ SETUP_INST_DIRS ++#undef X + +- 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 893e944..b8b9735 100644 +--- a/src/main.c ++++ b/src/main.c +@@ -18,17 +18,16 @@ + + */ + +-//#include "cla_parse.h" ++// #include "cla_parse.h" ++#include "globs.h" + #include "pkg_create.h" + #include "str.h" +-#include "globs.h" + +-int main(int argc, char** argv) { ++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); ++ pkg_create(mstrslc(argv[1])); ++ // cla_parse(argc, argv); + free_vars(); + return 0; + } +diff --git a/src/pkg_create.c b/src/pkg_create.c +index 79b4a78..9e3e765 100644 +--- a/src/pkg_create.c ++++ b/src/pkg_create.c +@@ -1,6 +1,6 @@ + /* + +- pkgit - package it! ++ pkgit - package_t it! + + Copyright (C) 2026 dacctal + This program is free software: you can redistribute it and/or modify +@@ -18,101 +18,79 @@ + + */ + +-#include <unistd.h> + #include <string.h> +- +-#include "pkg_create.h" ++#include <unistd.h> + + #include "files.h" + #include "globs.h" ++#include "log.h" ++#include "pkg_create.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; ++ str_slc name = str_slc_from_after_delim(arg, '/'); ++ if (str_slc_first(arg) == '.') ++ return str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), str_fmt(&cwd)); ++ else ++ return str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), str_fmt(&name)); + } + +-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; ++static str get_pkgsrc(package_t pkg) { ++ if (pkg.is_local == 1) ++ return str_format("%.*s/%.*s", str_fmt(&inst_dirs.src), pkg.name); ++ else ++ return str_format("%.*s/%.*s/%.*s", str_fmt(&inst_dirs.src), ++ str_fmt(&pkg.name), str_fmt(&pkg.version)); + } + +-package pkg_create(str_slc *arg) { +- package pkg; +- pkg.version = (str) { +- .data = "HEAD", +- .len = 4, +- .cap = 4, ++package_t pkg_create(str_slc arg) { ++ package_t pkg = { ++ .version = mstr("HEAD"), ++ .is_local = false, + }; +- 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); ++ str dest_dir = get_destdir(cwd_slc, arg); ++ str new_arg_str = str_from_str_slc(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++) { ++ // 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) { ++ if (strncmp(arg.data, "http", 4) == 0 || strncmp(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(""); ++ } else if (str_slc_equal_cstr(arg, ".")) { ++ pkg.url = mstr(""); + 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; ++ // 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.url = mstr(""); + 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)); ++ log_error("'%.*s' is not a valid package", str_fmt(&arg)); + exit(EXIT_FAILURE); + } + pkg.src = get_pkgsrc(pkg); +diff --git a/src/str.c b/src/str.c +index 9e67b4a..c845ed5 100644 +--- a/src/str.c ++++ b/src/str.c +@@ -44,15 +44,21 @@ along with this program.If not, see <https://www.gnu.org/licenses/>. + + // XXX: INTERNAL USE ONLY + #define _strslc(str) \ +- (str_slc) { str->data, str->len } ++ (str_slc) { \ ++ str->data, str->len \ ++ } + #define _cstrslc(str) \ +- (str_slc) { str, strlen(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(void) { ++ return str_new_with_capacity(16); ++} + + str str_new_with_capacity(size_t cap) { + str res = {0}; +@@ -62,9 +68,13 @@ str str_new_with_capacity(size_t cap) { + return res; + } + +-str str_from_cstr(const char *s) { return str_from_str_slc(_cstrslc(s)); } ++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 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); +@@ -429,13 +439,21 @@ 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_print(const str *s) { ++ printf("%.*s", str_fmt(s)); ++} + +-void str_println(const str *s) { printf("%.*s\n", 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_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)); } ++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; +@@ -475,11 +493,17 @@ bool str_write_to_file(const str *s, FILE *f) { + + // === str_slc implementation === + +-str_slc str_slc_from_str(const str *s) { return _strslc(s); } ++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 str_slc_from_cstr(const char *s) { ++ return _cstrslc(s); ++} + +-str_slc mstrslc(const char *s) { return (str_slc){s, strlen(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(); +@@ -684,9 +708,13 @@ char str_slc_at(const str_slc s, size_t i) { + return s.data[i]; + } + +-void str_slc_print(const str_slc s) { printf("%.*s", str_fmt(&s)); } ++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_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)); +@@ -717,27 +745,23 @@ ptrdiff_t str_slc_find_char_right(const str_slc s, char c) { + 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; + } |
