From fe3caeefac7a2081f5c9db90bf5c821624a71082 Mon Sep 17 00:00:00 2001 From: dacctal Date: Wed, 1 Jul 2026 11:08:21 +0000 Subject: checkpoint! --- src/.clangd | 4 -- src/cla_parse.c | 197 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/log.c | 19 ++++++ src/main.c | 2 +- src/pkg/.clangd | 4 ++ src/pkg/create.c | 98 +++++++++++++++++++++++++++ src/pkg_create.c | 98 --------------------------- 7 files changed, 319 insertions(+), 103 deletions(-) delete mode 100644 src/.clangd create mode 100644 src/cla_parse.c create mode 100644 src/log.c create mode 100644 src/pkg/.clangd create mode 100644 src/pkg/create.c delete mode 100644 src/pkg_create.c (limited to 'src') diff --git a/src/.clangd b/src/.clangd deleted file mode 100644 index 5022e8f..0000000 --- a/src/.clangd +++ /dev/null @@ -1,4 +0,0 @@ -CompileFlags: - Add: [-I/usr/include/luajit-2.1, -I../include] ---- -UseTab: Always diff --git a/src/cla_parse.c b/src/cla_parse.c new file mode 100644 index 0000000..ab1e44c --- /dev/null +++ b/src/cla_parse.c @@ -0,0 +1,197 @@ +/* + + 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 "cla_parse.h" + +#include "globs.h" +#include "log.h" +#include "str.h" +// #include "declare.h" +// #include "deps_resolve.h" +#include "help.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" + +#define COMMAND(arg, large, small, code) \ + if (strcmp(arg, large) == 0 || strcmp(arg, small) == 0) \ + code + +#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])); + } else { + NOT_ENOUGH_ARGS(argv[i], "url"); + } +} + +void cmd_build(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') continue; + printf("build pkg %s\n", argv[j]); + // str_slc arg = mstrslc(argv[j]); + // package_t pkg = pkg_create(arg); + // pkg_build(pkg); + } + } else { + printf("build pkg .\n"); + // str_slc arg = mstrslc("."); + // package_t pkg = pkg_create(arg); + // pkg_build(pkg); + } +} + +void cmd_install(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') continue; + printf("install pkg %s\n", argv[j]); + // str_slc arg = mstrslc(argv[j]); + // package_t pkg = pkg_create(arg); + // pkg_install(pkg); + } + } else { + NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } +} + +void cmd_remove(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') continue; + printf("remove pkg %s\n", argv[j]); + // str_slc arg = mstrslc(argv[j]); + // package_t pkg = pkg_create(arg); + // pkg_remove(pkg); + } + } else { + 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': + flags.verbose = false; + break; + case 'f': + flags.force = true; + break; + default: + break; + } + } +} + +void flags_cmd(int argc, char **argv, int i) { + 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); + break; + case 'c': /*deps_resolve();*/ + printf("deps_resolve\n"); + break; + case 'd': /*declare();*/ + printf("declare\n"); + break; + case 'i': + cmd_install(argc, argv, i); + break; + case 'r': + cmd_remove(argc, argv, i); + 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) { + for (int i = 1; i < argc; i++) { + if (argv[i][0] != '-') continue; + if (argv[i][1] == '-') { + COMMAND(argv[i], "--quiet", "-q", { flags.verbose = false; }); + COMMAND(argv[i], "--force", "-f", { flags.force = true; }); + } else { + flags_mod(argv, i); + flags_cmd(argc, argv, i); + } + } +} + +void cmds_parse(int argc, char **argv) { + 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); }); + COMMAND(argv[i], "--install", "i", { cmd_install(argc, argv, i); }); + COMMAND(argv[i], "--remove", "r", { cmd_remove(argc, argv, i); }); + 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; + } + flags_parse(argc, argv); + cmds_parse(argc, argv); +} diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..078afde --- /dev/null +++ b/src/log.c @@ -0,0 +1,19 @@ + +#include +#include + +#include "globs.h" +#include "log.h" + +#define X(n, N) \ + void log_##n(const char *format, ...) { \ + va_list args; \ + va_start(args, format); \ + eprintf(PKGIT_PREFIX_##N); \ + vfprintf(stderr, format, args); \ + eprintf("\n"); \ + va_end(args); \ + } + +LOG_FUNCTIONS +#undef X diff --git a/src/main.c b/src/main.c index b8b9735..5ac5bde 100644 --- a/src/main.c +++ b/src/main.c @@ -26,7 +26,7 @@ int main(int argc, char **argv) { (void)argc; init_vars(); - pkg_create(mstrslc(argv[1])); + package_t pkg = pkg_create(mstrslc(argv[1])); // cla_parse(argc, argv); free_vars(); return 0; diff --git a/src/pkg/.clangd b/src/pkg/.clangd new file mode 100644 index 0000000..5022e8f --- /dev/null +++ b/src/pkg/.clangd @@ -0,0 +1,4 @@ +CompileFlags: + Add: [-I/usr/include/luajit-2.1, -I../include] +--- +UseTab: Always diff --git a/src/pkg/create.c b/src/pkg/create.c new file mode 100644 index 0000000..9e3e765 --- /dev/null +++ b/src/pkg/create.c @@ -0,0 +1,98 @@ +/* + + pkgit - package_t 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 "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_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_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_t pkg_create(str_slc arg) { + package_t pkg = { + .version = mstr("HEAD"), + .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 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); + + pkg.version = str_from_after_delim(&new_arg_str, '@'); + pkg.target = str_from_after_delim(&new_arg_str, ','); + + 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(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 (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; + } else if (is_installed_locally) { + pkg.url = mstr(""); + pkg.name = str_from_after_delim(&dest_dir, '/'); + pkg.is_local = true; + } else { + log_error("'%.*s' is not a valid package", str_fmt(&arg)); + exit(EXIT_FAILURE); + } + pkg.src = get_pkgsrc(pkg); + return pkg; +} diff --git a/src/pkg_create.c b/src/pkg_create.c deleted file mode 100644 index 9e3e765..0000000 --- a/src/pkg_create.c +++ /dev/null @@ -1,98 +0,0 @@ -/* - - pkgit - package_t 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 "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_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_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_t pkg_create(str_slc arg) { - package_t pkg = { - .version = mstr("HEAD"), - .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 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); - - pkg.version = str_from_after_delim(&new_arg_str, '@'); - pkg.target = str_from_after_delim(&new_arg_str, ','); - - 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(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 (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; - } else if (is_installed_locally) { - pkg.url = mstr(""); - pkg.name = str_from_after_delim(&dest_dir, '/'); - pkg.is_local = true; - } else { - log_error("'%.*s' is not a valid package", str_fmt(&arg)); - exit(EXIT_FAILURE); - } - pkg.src = get_pkgsrc(pkg); - return pkg; -} -- cgit v1.2.3