From 6f6353f846d40cd2d4a62b9810c991adf6dd620f Mon Sep 17 00:00:00 2001 From: dacctal Date: Tue, 30 Jun 2026 02:59:14 +0000 Subject: checkpoint! --- src/cla_parse.c | 10 ++--- src/globs.c | 36 +++++++++++++++++ src/main.c | 17 +------- src/pkgit_globals.c | 29 -------------- src/pkgit_string.c | 107 --------------------------------------------------- src/str.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+), 156 deletions(-) create mode 100644 src/globs.c delete mode 100644 src/pkgit_globals.c delete mode 100644 src/pkgit_string.c create mode 100644 src/str.c (limited to 'src') diff --git a/src/cla_parse.c b/src/cla_parse.c index a0f3d43..c2f31fd 100644 --- a/src/cla_parse.c +++ b/src/cla_parse.c @@ -23,8 +23,8 @@ #include "cla_parse.h" -#include "pkgit_globals.h" -#include "pkgit_string.h" +#include "globs.h" +#include "str.h" //#include "declare.h" //#include "deps_resolve.h" @@ -46,7 +46,7 @@ #define NOT_ENOUGH_ARGS(arg, next) \ printf("%.*s Not enough arguments! Try: `pkgit %s [%s]`\n", \ - str_fmt(PRINT_ERROR), arg, next) + PRINT_ERROR, arg, next) void cmd_add(char **argv, int i) { if (argv[i + 1]) { @@ -120,7 +120,7 @@ void flags_cmd(int argc, char **argv, int i, package pkg) { 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", str_fmt(VERSION)); break; + case 'v': printf("%s\n", VERSION); break; case 'h': /*help();*/ printf("help\n"); break; default: break; } @@ -150,7 +150,7 @@ void cmds_parse(int argc, char **argv, package pkg) { COMMAND("--declare", "d", { /*declare();*/ }); COMMAND("--list", "l", { /*pkgs_list();*/ }); COMMAND("--search", "s", { /*search(argv[i + 1]);*/ }); - COMMAND("--version", "v", { printf("%.*s\n", str_fmt(VERSION)); }); + COMMAND("--version", "v", { printf("%s\n", VERSION); }); COMMAND("--help", "h", { /*help();*/ }); COMMAND("--check", "c", { /*deps_resolve();*/ }); } diff --git a/src/globs.c b/src/globs.c new file mode 100644 index 0000000..6487f21 --- /dev/null +++ b/src/globs.c @@ -0,0 +1,36 @@ +#include + +#include "globs.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"; + +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"); +// +// 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/main.c b/src/main.c index eebd9ce..33f1854 100644 --- a/src/main.c +++ b/src/main.c @@ -20,22 +20,9 @@ #include #include "cla_parse.h" -#include "pkgit_string.h" -#include "pkgit_globals.h" +#include "str.h" +#include "globs.h" int main(int argc, char** argv) { - //slc_cat(BOLD_YELLOW, slc_from_cstr("[")) - //slc_cat(BOLD_MAGENTA, slc_from_cstr("pkgit")) - //slc_cat(BOLD_MAGENTA, slc_from_cstr("]")) - //COLOR_RESET - str_slc hello_world = {"", 0}; - slc_cat(slc_from_cstr("hello"), slc_from_cstr(" world"), &hello_world); - str_slc test = {"hello", 5}; - printf("%.*s\n", str_fmt(test)); - printf("%.*s%s%.*s\n", str_fmt(BOLD_YELLOW), "TEST", str_fmt(COLOR_RESET)); - printf("length: %zu\nstring: %.*s\n", hello_world.len, str_fmt(hello_world)); - str_slc hello_world_ext = {"", 0}; - slc_cat(slc_from_cstr(hello_world.data), slc_from_cstr("!!!"), &hello_world_ext); - printf("length: %zu\nstring: %.*s\n", hello_world_ext.len, str_fmt(hello_world_ext)); return 0; } diff --git a/src/pkgit_globals.c b/src/pkgit_globals.c deleted file mode 100644 index 094eef8..0000000 --- a/src/pkgit_globals.c +++ /dev/null @@ -1,29 +0,0 @@ -int is_verbose = 1; -int is_forced = 0; - -//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"); -// -// 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/pkgit_string.c b/src/pkgit_string.c deleted file mode 100644 index 6f8a0a6..0000000 --- a/src/pkgit_string.c +++ /dev/null @@ -1,107 +0,0 @@ -/* - - 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 . - -*/ - -#include -#include -#include - -#include "pkgit_string.h" - -void slc_print(str_slc slc) { - for (size_t i = 0; i < slc.len; i++) { - putchar(slc.data[i]); - } -} - -str_slc slc_from_cstr(char* cstr) { - return (str_slc) { - .data = cstr, - .len = strlen(cstr), - }; -} - -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; - } - return 1; -} - -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; - } - return 1; -} - -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 - }; -} - -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 - }; -} - -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; -} - -str_slc slc_split(str_slc *slc, char delimiter) { - size_t i = 0; - while(i < slc->len && slc->data[i] != delimiter) { - i++; - } - - 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; -} - -void slc_cat(str_slc first, str_slc second, str_slc *new_slc) { - new_slc->len = first.len + second.len + 1; - char buf[new_slc->len]; - for (size_t i = 0; i < first.len; i++) { - buf[i] = first.data[i]; - } - for (size_t i = first.len; i < new_slc->len; i++) { - buf[i] = second.data[i-first.len]; - } - new_slc->data = buf; -} diff --git a/src/str.c b/src/str.c new file mode 100644 index 0000000..7bb424e --- /dev/null +++ b/src/str.c @@ -0,0 +1,109 @@ +/* + + 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 . + +*/ + +#include +#include +#include +#include + +#include "str.h" + +void slc_print(str_slc slc) { + for (size_t i = 0; i < slc.len; i++) { + putchar(slc.data[i]); + } +} + +str_slc slc_from_cstr(char* cstr) { + return (str_slc) { + .data = cstr, + .len = strlen(cstr), + }; +} + +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; + } + return 1; +} + +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; + } + return 1; +} + +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 + }; +} + +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 + }; +} + +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; +} + +str_slc slc_split(str_slc *slc, char delimiter) { + size_t i = 0; + while(i < slc->len && slc->data[i] != delimiter) { + i++; + } + + 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; +} + +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]; + } +} -- cgit v1.2.3