diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/add_repo.c | 57 | ||||
| -rw-r--r-- | src/build.c | 39 | ||||
| -rw-r--r-- | src/cla_parse.c | 155 | ||||
| -rw-r--r-- | src/cmd_out.c | 62 | ||||
| -rw-r--r-- | src/create_pkg.c | 143 | ||||
| -rw-r--r-- | src/declare.c | 37 | ||||
| -rw-r--r-- | src/fetch_git.c | 105 | ||||
| -rw-r--r-- | src/fetch_pwd.c | 73 | ||||
| -rw-r--r-- | src/fetch_src.c | 80 | ||||
| -rw-r--r-- | src/files.c | 163 | ||||
| -rw-r--r-- | src/help.c | 56 | ||||
| -rw-r--r-- | src/install_pkg.c | 126 | ||||
| -rw-r--r-- | src/is_updated.c | 59 | ||||
| -rw-r--r-- | src/list_pkgs.c | 50 | ||||
| -rw-r--r-- | src/lua_build.c | 62 | ||||
| -rw-r--r-- | src/lua_state.c | 915 | ||||
| -rw-r--r-- | src/main.c | 37 | ||||
| -rw-r--r-- | src/name_from_url.c | 40 | ||||
| -rw-r--r-- | src/pkgit_string.c | 60 | ||||
| -rw-r--r-- | src/remove_pkg.c | 118 | ||||
| -rw-r--r-- | src/resolve_deps.c | 44 | ||||
| -rw-r--r-- | src/search.c | 37 | ||||
| -rw-r--r-- | src/set_install_directories.c | 83 | ||||
| -rw-r--r-- | src/setup_dirs.c | 34 | ||||
| -rw-r--r-- | src/setup_pkgit.c | 29 | ||||
| -rw-r--r-- | src/update_all.c | 59 | ||||
| -rw-r--r-- | src/update_pkg.c | 44 | ||||
| -rw-r--r-- | src/vars.c | 220 |
28 files changed, 66 insertions, 2921 deletions
diff --git a/src/add_repo.c b/src/add_repo.c deleted file mode 100644 index 949c992..0000000 --- a/src/add_repo.c +++ /dev/null @@ -1,57 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <string.h> -#include <stdbool.h> - -#include "add_repo.h" -#include "vars.h" - -void add_repo(const char *repo, const char *repo_name) { - bool is_previous_repos = false; - char rfile_line[1024]; - char rfile_contents[8192] = {0}; - - if (file_exists(repo_file)) { - FILE *rfile = fopen(repo_file, "r"); - if (rfile) { - while (fgets(rfile_line, sizeof(rfile_line), rfile)) { - size_t current_len = strlen(rfile_contents); - size_t remaining = sizeof(rfile_contents) - current_len; - if (remaining > 1) { - snprintf(rfile_contents + current_len, remaining, "%s", rfile_line); - } else { - break; - } - } - fclose(rfile); - is_previous_repos = true; - } - } - - char *previous_repos = is_previous_repos ? rfile_contents : ""; - - FILE *wfile = fopen(repo_file, "w"); - if (wfile) { - fprintf(wfile, "%srepositories[\"%s\"] = { url = \"%s\" }\n", previous_repos, repo_name, repo); - fclose(wfile); - } -} diff --git a/src/build.c b/src/build.c deleted file mode 100644 index fd524ea..0000000 --- a/src/build.c +++ /dev/null @@ -1,39 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "build.h" - -#include "lua_build.h" -#include "vars.h" - -void build(Pkg pkg) { - char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); - if (strcmp(pkg.src, cwd) != 0 && !pkg.is_local) chdir(pkg.src); - - if (lua_build(pkg.name, pkg.target, pkg.is_local ? cwd : pkg.src)) return; - printf("%s no usable build system was found\n", print_error); - exit(EXIT_FAILURE); -}
\ No newline at end of file diff --git a/src/cla_parse.c b/src/cla_parse.c deleted file mode 100644 index b30202d..0000000 --- a/src/cla_parse.c +++ /dev/null @@ -1,155 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <string.h> - -#include "cla_parse.h" - -#include "add_repo.h" -#include "build.h" -#include "create_pkg.h" -#include "declare.h" -#include "search.h" -#include "help.h" -#include "install_pkg.h" -#include "list_pkgs.h" -#include "name_from_url.h" -#include "remove_pkg.h" -#include "resolve_deps.h" -#include "update_all.h" -#include "vars.h" - -#define COMMAND(large, small, code) \ - if (strcmp(argv[i], large) == 0 || strcmp(argv[i], small) == 0) code - -#define NOT_ENOUGH_ARGS(arg, next) \ - 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]) { - add_repo(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, Pkg pkg) { - if (argv[i + 1]) { - for (int j = i + 1; j < argc; j++) { - if (argv[j][0] == '-') continue; - pkg = create_pkg(argv[j]); - build(pkg); - } - } else { - pkg = create_pkg("."); - build(pkg); - } -} - -void cmd_install(int argc, char **argv, int i, Pkg pkg) { - if (argv[i + 1]) { - for (int j = i + 1; j < argc; j++) { - if (argv[j][0] == '-') continue; - pkg = create_pkg(argv[j]); - install_pkg(pkg); - } - } else { - NOT_ENOUGH_ARGS(argv[i], "url/pkg"); - } -} - -void cmd_remove(int argc, char **argv, int i, Pkg pkg) { - if (argv[i + 1]) { - for (int j = i + 1; j < argc; j++) { - if (argv[j][0] == '-') continue; - pkg = create_pkg(argv[j]); - remove_pkg(pkg); - } - } else { - NOT_ENOUGH_ARGS(argv[i], "url/pkg"); - } -} - -void mod_flags(char **argv, int i) { - for (size_t j = 1; j < strlen(argv[i]); j++) { - switch (argv[i][j]) { - case 'q': is_verbose = false; break; - case 'f': is_forced = true; break; - default: break; - } - } -} - -void cmd_flags(int argc, char **argv, int i, Pkg 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 'i': cmd_install(argc, argv, i, pkg); break; - case 'r': cmd_remove(argc, argv, i, pkg); break; - case 'u': update_all(); break; - case 'd': declare(); break; - case 'l': list_pkgs(); break; - case 's': search(argv[i + 1]); break; - case 'v': printf("%s\n", version); break; - case 'h': help(); break; - case 'c': resolve_deps(); break; - default: break; - } - } -} - -void parse_flags(int argc, char **argv, Pkg pkg) { - for (int i = 1; i < argc; i++) { - if (argv[i][0] != '-') continue; - if (argv[i][1] == '-') { - COMMAND("--quiet", "-q", { is_verbose = false; }); - COMMAND("--force", "-f", { is_forced = true; }); - } else { - mod_flags(argv, i); - cmd_flags(argc, argv, i, pkg); - } - } -} - -void parse_cmds(int argc, char **argv, Pkg 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_all(); }); - COMMAND("--declare", "d", { declare(); }); - COMMAND("--list", "l", { list_pkgs(); }); - COMMAND("--search", "s", { search(argv[i + 1]); }); - COMMAND("--version", "v", { printf("%s\n", version); }); - COMMAND("--help", "h", { help(); }); - COMMAND("--check", "c", { resolve_deps(); }); - } -} - -void cla_parse(int argc, char **argv) { - if (!argv[1]) { help(); return; } - Pkg pkg = {0}; - parse_flags(argc, argv, pkg); - parse_cmds(argc, argv, pkg); -}
\ No newline at end of file diff --git a/src/cmd_out.c b/src/cmd_out.c deleted file mode 100644 index f2d6bf4..0000000 --- a/src/cmd_out.c +++ /dev/null @@ -1,62 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdbool.h> - -#include "cmd_out.h" - -char* cmd_out(const char *cmd) { - FILE *pipe = popen(cmd, "r"); - if (!pipe) return strdup(""); - - char buffer[128]; - size_t total_size = 0; - size_t capacity = 256; - char *result = malloc(capacity); - if (!result) { - pclose(pipe); - return strdup(""); - } - result[0] = '\0'; - - while (fgets(buffer, sizeof(buffer), pipe) != NULL) { - size_t len = strlen(buffer); - // Ensure enough space for the new chunk + 1 byte for '\0' - if (total_size + len + 1 > capacity) { - capacity *= 2; - char *new_result = realloc(result, capacity); - if (!new_result) { - free(result); - pclose(pipe); - return strdup(""); - } - result = new_result; - } - - snprintf(result + total_size, capacity - total_size, "%s", buffer); - total_size += len; - } - - pclose(pipe); - return result; -} diff --git a/src/create_pkg.c b/src/create_pkg.c deleted file mode 100644 index 12b6a63..0000000 --- a/src/create_pkg.c +++ /dev/null @@ -1,143 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "create_pkg.h" -#include "lua_state.h" -#include "name_from_url.h" -#include "vars.h" - -bool is_in_repos(char *arg) { - for (size_t i = 0; i < cached_repos_count; i++) - if (strcmp(arg, cached_repos[i].source_key) == 0) - return true; - return false; -} - -Pkg pkg_from_repo(char* arg) { - Pkg pkg = {0}; - for (size_t i = 0; i < cached_repos_count; i++) { - if (strcmp(arg, cached_repos[i].source_key) == 0) { - pkg.url = strdup(cached_repos[i].source_value); - break; - } - } - pkg.name = arg; - return pkg; -} - -char* get_destdir(char* cwd, char* arg) { - char dest_dir[MAX_PATH_LEN]; - if (arg[0] == '.') { - snprintf(dest_dir, sizeof(dest_dir), "%s/%s", - get_install_dir("src"), name_from_url(cwd)); - } else { - snprintf(dest_dir, sizeof(dest_dir), "%s/%s", - get_install_dir("src"), arg); - } - return strdup(dest_dir); -} - -void rmdotgit(Pkg pkg) { - if ( - strlen(pkg.name) > 4 && - strncmp(pkg.name + strlen(pkg.name) - 4, ".git", 4) == 0 - ) pkg.name[strlen(pkg.name) - 4] = '\0'; -} - -char* get_pkgsrc(Pkg pkg) { - char src_dir[MAX_PATH_LEN]; - if (pkg.is_local) { - snprintf(src_dir, sizeof(src_dir), "%s/%s", - get_install_dir("src"), pkg.name); - } else { - snprintf(src_dir, sizeof(src_dir), "%s/%s/%s", - get_install_dir("src"), pkg.name, pkg.ver); - } - return strdup(src_dir); -} - -Pkg create_pkg(const char *arg) { - Pkg pkg = {0}; - pkg.ver = "HEAD"; - pkg.is_local = false; - char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); - char *new_arg = strdup(arg); - if (!new_arg) exit(EXIT_FAILURE); - char* dest_dir = get_destdir(cwd, new_arg); - bool is_installed_locally = is_directory(dest_dir); - - char *verptr = strchr(new_arg, '@'); - char *trgptr = strchr(new_arg, ','); - for (size_t i = 0; i < cached_repos_count; i++) { - if (strcmp(cached_repos[i].source_key, new_arg) != 0) continue; - pkg.ver = cached_repos[i].version; - } - if (verptr) { - pkg.ver = verptr + 1; - *verptr = '\0'; - } - if (trgptr) { - pkg.target = trgptr + 1; - *trgptr = '\0'; - } else { - pkg.target = is_verbose ? "default" : "quiet"; - } - - 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, "http", 4) == 0 || strncmp(new_arg, "ssh", 3) == 0) { - pkg.url = strdup(new_arg); - pkg.name = name_from_url(new_arg); - } else if (strcmp(new_arg, ".") == 0) { - pkg.url = ""; - pkg.name = name_from_url(cwd); - 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; - } else if (is_installed_locally) { - pkg.url = ""; - pkg.name = name_from_url(dest_dir); - pkg.is_local = true; - } else { - printf("%s '%s' is not a valid package\n", print_error, new_arg); - exit(EXIT_FAILURE); - } - rmdotgit(pkg); - snprintf(pkg.src, MAX_PATH_LEN, "%s", get_pkgsrc(pkg)); - return pkg; -}
\ No newline at end of file diff --git a/src/declare.c b/src/declare.c deleted file mode 100644 index 2841cf9..0000000 --- a/src/declare.c +++ /dev/null @@ -1,37 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "declare.h" -#include "create_pkg.h" -#include "update_pkg.h" -#include "lua_state.h" - -void declare(void) { - init_lua_state(); - cache_repos(); - for (size_t i = 0; i < cached_repos_count; i++) { - Pkg pkg = create_pkg(cached_repos[i].source_value); - update_pkg(pkg); - } -} diff --git a/src/fetch_git.c b/src/fetch_git.c deleted file mode 100644 index 2640f9c..0000000 --- a/src/fetch_git.c +++ /dev/null @@ -1,105 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <fcntl.h> -#include <stdio.h> -#include <string.h> -#include <sys/wait.h> -#include <unistd.h> - -#include "fetch_git.h" - -#include "lua_state.h" -#include "vars.h" - -int fetch_git(Pkg pkg) { - pid_t pid = fork(); - if (pid == 0) { - if (!is_verbose) { - int nullfd = open("/dev/null", O_WRONLY); - if (nullfd >= 0) { - dup2(nullfd, STDOUT_FILENO); - dup2(nullfd, STDERR_FILENO); - close(nullfd); - } - } - const char *argv[12]; - int i = 0; - argv[i++] = "git"; - argv[i++] = "-c"; - argv[i++] = "advice.detachedHead=false"; - argv[i++] = "clone"; - if (strcmp(pkg.ver, "HEAD") != 0) { - argv[i++] = "--branch"; - argv[i++] = pkg.ver; - } - argv[i++] = "--recursive"; - argv[i++] = "--depth"; - argv[i++] = "1"; - argv[i++] = pkg.url; - argv[i++] = pkg.src; - argv[i] = NULL; - execvp("git", (char *const *)argv); - _exit(127); - } - - int status; - waitpid(pid, &status, 0); - int result = WIFEXITED(status) ? WEXITSTATUS(status) : -1; - if (result != 0) { - pid_t pid = fork(); - if (pid == 0) { - if (!is_verbose) { - int nullfd = open("/dev/null", O_WRONLY); - if (nullfd >= 0) { - dup2(nullfd, STDOUT_FILENO); - dup2(nullfd, STDERR_FILENO); - close(nullfd); - } - } - const char *argv[12]; - int i = 0; - argv[i++] = "git"; - argv[i++] = "-c"; - argv[i++] = "advice.detachedHead=false"; - argv[i++] = "clone"; - if (strcmp(pkg.ver, "HEAD") != 0) { - argv[i++] = "--branch"; - argv[i++] = pkg.ver; - } - argv[i++] = "--recursive"; - argv[i++] = pkg.url; - argv[i++] = pkg.src; - argv[i] = NULL; - execvp("git", (char *const *)argv); - _exit(127); - } - - int status; - waitpid(pid, &status, 0); - result = WIFEXITED(status) ? WEXITSTATUS(status) : -1; - if (result != 0) printf( - "%s git clone failed: %d\n", - print_warning, result - ); - } - - return result; -}
\ No newline at end of file diff --git a/src/fetch_pwd.c b/src/fetch_pwd.c deleted file mode 100644 index 32edd79..0000000 --- a/src/fetch_pwd.c +++ /dev/null @@ -1,73 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <sys/stat.h> -#include <fcntl.h> -#include <ftw.h> -#include <unistd.h> - -#include "fetch_pwd.h" -#include "vars.h" - -static const char *pwd_dst; - -static int copy_entry( - const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf -) { - (void)ftwbuf; - const char *rel = fpath; - if (rel[0] == '.' && rel[1] == '/') rel += 2; - if (rel[0] == '\0') return 0; - - char dst[MAX_PATH_LEN]; - snprintf(dst, sizeof(dst), "%s/%s", pwd_dst, rel); - - if (typeflag == FTW_D) { - mkdir(dst, 0755); - } else if (typeflag == FTW_F) { - int src_fd = open(fpath, O_RDONLY); - if (src_fd < 0) return -1; - - int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, sb->st_mode & 07777); - if (dst_fd < 0) { close(src_fd); return -1; } - - char buf[8192]; - ssize_t n; - while ((n = read(src_fd, buf, sizeof(buf))) > 0) { - ssize_t written = 0; - while (written < n) { - ssize_t ret = write(dst_fd, buf + written, n - written); - if (ret < 0) { close(src_fd); close(dst_fd); return -1; } - written += ret; - } - } - - close(src_fd); - close(dst_fd); - } - return 0; -} - -void fetch_pwd(Pkg pkg) { - pwd_dst = pkg.src; - nftw(".", copy_entry, 64, FTW_PHYS); -}
\ No newline at end of file diff --git a/src/fetch_src.c b/src/fetch_src.c deleted file mode 100644 index 69b1ec9..0000000 --- a/src/fetch_src.c +++ /dev/null @@ -1,80 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <ftw.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <unistd.h> - -#include "fetch_git.h" -#include "fetch_src.h" -#include "vars.h" - -static int remove_tree( - const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf -) { - (void)sb; - (void)ftwbuf; - if (typeflag == FTW_F || typeflag == FTW_SL) { - unlink(fpath); - } else if (typeflag == FTW_DP) { - rmdir(fpath); - } - return 0; -} - -void fetch_src(Pkg pkg) { - if (is_verbose) - printf( - "%s target source directory: %s\n", - print_pkgit, pkg.src - ); - if (file_exists(pkg.src)) { - if (is_verbose) printf( - "%s %s already exists. deleting...\n", - print_pkgit, pkg.src - ); - chdir(".."); - nftw(pkg.src, remove_tree, 64, FTW_DEPTH | FTW_PHYS); - } - if (strcmp(pkg.url, "") == 0) { - if (is_verbose) printf( - "%s creating directory %s...\n", - print_pkgit, pkg.src - ); - mkdir_p(pkg.src); - return; - } - if (fetch_git(pkg) == 0) { - if (is_verbose) printf( - "%s cloned into %s\n", - print_pkgit, pkg.src - ); - return; - } - printf( - "%s no fetch methods worked.\n", - print_error - ); - exit(EXIT_FAILURE); -}
\ No newline at end of file diff --git a/src/files.c b/src/files.c deleted file mode 100644 index 9f13759..0000000 --- a/src/files.c +++ /dev/null @@ -1,163 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <dirent.h> -#include <errno.h> -#include <fcntl.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <unistd.h> - -const char *get_filename_ext(const char *filename) { - const char *dot = strrchr(filename, '.'); - if (!dot || dot == filename) return ""; - return dot + 1; -} - -static int write_all(int fd, const void *buf, size_t size) { - while (size) { - ssize_t written = write(fd, buf, size); - if (written < 0) { - return -1; - } - - size -= written; - buf = (const char *)buf + written; - } - - return 0; -} - -static int copy_file_content(int src_fd, int dst_fd) { - char buf[4096]; - ssize_t readed; - while ((readed = read(src_fd, buf, sizeof(buf))) > 0) { - if (write_all(dst_fd, buf, readed) < 0) { - perror("write_all"); - return -1; - } - } - - if (readed < 0) { - perror("read"); - return -1; - } - - return 0; -} - -static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path); - -static int copy_dir_content(int src_fd, int dst_fd) { - int ret = -1; - // duplicate the descriptor to prevent closedir from closing original one - src_fd = dup(src_fd); - if (src_fd < 0) { - perror("dup"); - goto cleanup0; - } - - DIR *src_dir = fdopendir(src_fd); - if (!src_dir) { - perror("fdopendir"); - close(src_fd); - goto cleanup0; - } - - for (struct dirent *ent; (errno = 0, ent = readdir(src_dir));) { - if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { - continue; - } - - if (copy_at(src_fd, ent->d_name, dst_fd, ent->d_name) < 0) { - goto cleanup1; - } - } - - if (errno != 0) { - perror("readdir"); - goto cleanup1; - } - - ret = 0; - - cleanup1: - closedir(src_dir); - cleanup0: - return ret; -} - -static int copy_at(int src_dir_fd, const char *src_path, int dst_dir_fd, const char *dst_path) { - int ret = -1; - int src_fd = openat(src_dir_fd, src_path, O_NOFOLLOW | O_RDONLY); - if (src_fd < 0) { - perror("openat"); - goto cleanup0; - } - - struct stat stat; - if (fstat(src_fd, &stat) < 0) { - perror("fstat"); - goto cleanup1; - } - - int dst_oflag = O_NOFOLLOW; - if (S_ISDIR(stat.st_mode)) { - if (mkdirat(dst_dir_fd, dst_path, stat.st_mode) < 0) { - perror("mkdirat"); - goto cleanup1; - } - - dst_oflag |= O_RDONLY | O_DIRECTORY; - } else { - dst_oflag |= O_WRONLY | O_CREAT | O_EXCL; - } - - int dst_fd = openat(dst_dir_fd, dst_path, dst_oflag, stat.st_mode); - if (dst_fd < 0) { - perror("openat"); - goto cleanup1; - } - - switch (stat.st_mode & S_IFMT) { - case S_IFDIR: - ret = copy_dir_content(src_fd, dst_fd); - break; - case S_IFREG: - ret = copy_file_content(src_fd, dst_fd); - break; - default: - fprintf(stderr, "copy_at: unsupported file inode type\n"); - } - goto cleanup2; - - cleanup2: - close(dst_fd); - cleanup1: - close(src_fd); - cleanup0: - return ret; -} - -void cpdir(const char *src_path, const char *dst_path) { - copy_at(AT_FDCWD, src_path, AT_FDCWD, dst_path); -} diff --git a/src/help.c b/src/help.c deleted file mode 100644 index 1cb4e6b..0000000 --- a/src/help.c +++ /dev/null @@ -1,56 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> - -#include "help.h" -#include "vars.h" - -void help(void) { - if (is_verbose) { - printf("%s , \n", bold_magenta); - printf("%s / \\ \n", bold_magenta); - printf("%s_.--' '--._ \n", bold_magenta); - printf("%s`'--, ,--'` %spkgit %s%s- package it!%s\n", bold_magenta, color_reset, italic, gray, color_reset); - printf("%s _- %s\\ /%s -_ %s%s%s\n", bold_yellow, bold_magenta, bold_yellow, magenta, version, color_reset); - printf("%s'-_ %s'%s _-' \n", bold_yellow, bold_magenta, bold_yellow); - printf("%s `'-.-'` %s\n", bold_yellow, color_reset); - } 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("%ssubcommand flags%s:\n", red, color_reset); - printf("%s %s-a%s, %s--add %s[url] %s# add a repo\n", color_reset, green, color_reset, yellow, blue, gray); - printf("%s %s-b%s, %s--build %s[path] %s# build a package\n", color_reset, green, color_reset, yellow, blue, gray); - printf("%s %s-d%s, %s--declare %s# install all packages\n", color_reset, green, color_reset, yellow, gray); - printf("%s %s-s%s, %s--search %s[pkgs] %s# find a package from your repos\n", color_reset, green, color_reset, yellow, blue, gray); - printf("%s %s-i%s, %s--install %s[pkgs, urls] %s# install a package/repo\n", color_reset, green, color_reset, yellow, blue, gray); - printf("%s %s-r%s, %s--remove %s[pkgs] %s# remove an installed package\n", color_reset, green, color_reset, yellow, blue, gray); - printf("%s %s-l%s, %s--list %s# list all installed packages\n", color_reset, green, color_reset, yellow, gray); - printf("%s %s-u%s, %s--update %s# update all installed packages\n", color_reset, green, color_reset, yellow, gray); - printf("%s %s-h%s, %s--help %s# display this help message\n", color_reset, green, color_reset, yellow, gray); - printf("%s %s-v%s, %s--version %s# display version number\n", color_reset, green, color_reset, yellow, gray); - printf("%s %s-c%s, %s--check %s# run package checks\n", color_reset, green, color_reset, yellow, gray); - printf("\n"); - printf("%smodifier flags%s:\n", red, color_reset); - printf("%s %s-q%s, %s--quiet %s# run without logging to terminal\n", color_reset, green, color_reset, yellow, gray); - printf("%s %s-f%s, %s--force %s# force an operation like 'install'\n", color_reset, green, color_reset, yellow, gray); -} diff --git a/src/install_pkg.c b/src/install_pkg.c deleted file mode 100644 index c233976..0000000 --- a/src/install_pkg.c +++ /dev/null @@ -1,126 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "add_repo.h" -#include "build.h" -#include "fetch_src.h" -#include "files.h" -#include "install_pkg.h" -#include "lua_state.h" -#include "name_from_url.h" -#include "vars.h" - -void install_pkg(Pkg pkg) { - if (is_directory(pkg.src)) { - if (!is_forced) { - if (is_verbose) printf( - "%s %s is already installed.\n", - print_skipped, pkg.name - ); - return; - } else { - if (is_verbose) printf( - "%s %s is already installed.\n", - print_warning, pkg.name - ); - } - } - char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); - if (strcmp(pkg.src, cwd) != 0) chdir(pkg.src); - - if (pkg.is_local) { - cpdir(cwd, pkg.src); - } else { - printf( - "%s fetching %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - fetch_src(pkg); - if (is_verbose) printf( - "%s fetched %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - } - - printf( - "%s building %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - build(pkg); - if (is_verbose) printf( - "%s built %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - - printf( - "%s installing %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - bool install_success = false; - if (!install_success && repo_install(pkg.name, pkg.target)) - install_success = true; - if (!install_success && bldit_install(pkg.target)) - install_success = true; - if (!install_success && config_install(pkg.src, pkg.target)) - install_success = true; - if (!install_success) { - printf( - "%s no install function availible for package: %s\n", - print_error, pkg.name - ); - return; - } - printf( - "%s installed %s%s%s\n", - print_success, green, pkg.name, color_reset - ); - - bool repo_exists = false; - for (size_t i = 0; i < cached_repos_count; i++) { - char *repo_name = name_from_url(cached_repos[i].source_key); - if (strcmp(repo_name, pkg.name) == 0) { repo_exists = true; } - free(repo_name); - } - - if (!repo_exists) { - printf( - "%s adding %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - if (pkg.url && strlen(pkg.url) > 0) { - add_repo(pkg.url, pkg.name); - } - printf( - "%s added %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - } else { - if (is_verbose) printf( - "%s repo already exists, done\n", - print_pkgit - ); - } -}
\ No newline at end of file diff --git a/src/is_updated.c b/src/is_updated.c deleted file mode 100644 index f9d31aa..0000000 --- a/src/is_updated.c +++ /dev/null @@ -1,59 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <stdbool.h> -#include <unistd.h> - -#include "is_updated.h" - -#include "cmd_out.h" -#include "lua_state.h" - -bool is_updated(const char *src) { - bool result = false; - if (src && strlen(src) > 0 && chdir(src) != 0) return result; - char *bldit_pkgver = bldit_pkg_getver(); - char *git_tag = cmd_out("git tag | tail -n 1"); - result = (strstr(git_tag, bldit_pkgver) != NULL && strlen(bldit_pkgver) != 0); - char *git_pull = cmd_out("git pull"); - result = (strstr(git_pull, "Already up to date.") != NULL); - free(git_tag); - free(git_pull); - free(bldit_pkgver); - return result; -} - -bool is_bldit_usable(void) { - char* bldit_version = bldit_getver(); - if (!bldit_version) return false; - if (strcmp(bldit_version, version) == 0) return true; - bool prev_pass = false; - for (size_t i = 0; i < strlen(bldit_version); i++) { - if (bldit_version[i] == '.') continue; - if ((bldit_version[i] - '0') <= (version[i] - '0')) { - prev_pass = ((bldit_version[i] - '0') != (version[i] - '0')); - continue; - } else return prev_pass; - } - return true; -}
\ No newline at end of file diff --git a/src/list_pkgs.c b/src/list_pkgs.c deleted file mode 100644 index 365e9b8..0000000 --- a/src/list_pkgs.c +++ /dev/null @@ -1,50 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <dirent.h> -#include <stdio.h> -#include <string.h> -#include <sys/stat.h> - -#include "lua_state.h" -#include "vars.h" - -void list_pkgs(void) { - char* src_code = map_get(&cached_install_directories, "src"); - struct dirent* dirent_ptr; - DIR* dir_ptr = opendir(src_code); - - if (dir_ptr == NULL) { - fprintf( - stderr, - "%s could not open %s\n", - print_pkgit, src_code - ); - return; - } - - while ((dirent_ptr = readdir(dir_ptr)) != NULL) { - if ( - strcmp(dirent_ptr->d_name, "..") == 0 || - strcmp(dirent_ptr->d_name, ".") == 0 - ) continue; - printf("%s\n", dirent_ptr->d_name); - } -}
\ No newline at end of file diff --git a/src/lua_build.c b/src/lua_build.c deleted file mode 100644 index 520117c..0000000 --- a/src/lua_build.c +++ /dev/null @@ -1,62 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdbool.h> - -#include "lua_build.h" - -#include "is_updated.h" -#include "lua_state.h" -#include "vars.h" - -bool lua_build(const char *repository, const char *target, const char *path) { - if (is_verbose) printf( - "%s attempting init.lua: 'repositories.%s.build'\n", - print_pkgit, repository - ); - if (repo_build(repository, target)) { return true; } - if (is_verbose) printf( - "%s failed init.lua: 'repositories.%s.build'\n", - print_warning, repository - ); - - if (is_verbose) printf( - "%s attempting bldit.lua\n", - print_pkgit - ); - if (bldit(target)) { return true; } - if (is_verbose) printf( - "%s failed bldit.lua\n", - print_warning - ); - - if (is_verbose) printf( - "%s attempting init.lua: 'build_systems'\n", - print_pkgit - ); - if (config_build(path, target)) { return true; } - if (is_verbose) printf( - "%s failed init.lua: 'build_systems'\n", - print_warning - ); - - return false; -}
\ No newline at end of file diff --git a/src/lua_state.c b/src/lua_state.c deleted file mode 100644 index 9ba71de..0000000 --- a/src/lua_state.c +++ /dev/null @@ -1,915 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <limits.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "create_pkg.h" -#include "install_pkg.h" -#include "is_updated.h" -#include "lua_state.h" -#include "vars.h" - -static lua_State *L = NULL; -static bool config_loaded = false; - -static lua_State *B = NULL; -static 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); - push_lua_path(L, lua_path); - if (luaL_loadfile(L, config_file) || 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(repo_file)) { - if (luaL_loadfile(L, repo_file) || 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; } - -const char* get_privilege_escalator(void) { - lua_getglobal(L, "privilege_escalator"); - if (!lua_isstring(L, -1)) { - printf( - "%s init.lua: 'privilege_escalator' is not a string.\n", - print_error - ); - return ""; - } - const char* privilege_escalator = lua_tostring(L, -1); - lua_pop(L, 1); - return privilege_escalator; -} - -void cache_prefix_directory(void) { - lua_getglobal(L, "prefix"); - if (!lua_isstring(L, -1)) { - printf( - "%s init.lua: 'prefix' is not a string.\n", - print_error - ); - return; - } - snprintf(prefix_dir, MAX_PATH_LEN, "%s", lua_tostring(L, -1)); - lua_pop(L, 1); -} - -void cache_install_directories(void) { - if (!config_loaded || !lua_istable(L, -1)) { - lua_getglobal(L, "install_directories"); - } - if (!lua_istable(L, -1)) { - printf( - "%s init.lua: 'install_directories' is not a table.\n", - print_error - ); - return; - } - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - const char *value = lua_tostring(L, -1); - if (key && value) { - map_put(&cached_install_directories, strdup(key), strdup(value)); - } - lua_pop(L, 1); - } - lua_pop(L, 1); -} - -void install_dependencies(lua_State *L) { - while (lua_next(L, -2) != 0) { - const char *depname = lua_tostring(L, -2); - if (depname && lua_istable(L, -1)) { - lua_getfield(L, -1, "url"); - const char *dep_url = lua_tostring(L, -1); - lua_pop(L, 1); - lua_getfield(L, -1, "version"); - const char *dep_version = lua_tostring(L, -1); - lua_pop(L, 1); - Pkg pkg = create_pkg(dep_url); - pkg.ver = strdup(dep_version); - const int top = lua_gettop(L); - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - install_pkg(pkg); - chdir(cwd); - } - lua_settop(L, top); - } - lua_pop(L, 1); - } -} - -bool on_update(lua_State *L) { - const char* lua_file = "init.lua"; - lua_getglobal(L, "on_update"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'on_update' is not a function.\n", - print_warning, lua_file - ); - lua_pop(L, 2); - return false; - } - if (lua_pcall(L, 0, 1, 0) != LUA_OK) { - printf( - "%s %s: 'on_update' function borked: %s\n", - print_error, lua_file, lua_tostring(L, -1) - ); - lua_pop(L, 2); - return false; - } - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'on_update' failed: %s\n", - print_error, lua_file, lua_tostring(L, -1) - ); - lua_pop(L, 2); - return false; - } - lua_pop(L, 2); - return true; -} - -bool target_loop_on_update( - lua_State *L, const char* lua_file, const char *target -) { - lua_getfield(L, -1, target); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s' is not a table.\n", - print_warning, lua_file, target - ); - lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) { - target_loop_on_update(L, lua_file, "default"); - lua_pop(L, 1); - } - return false; - } - lua_getfield(L, -1, "on_update"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s.on_update' is not a function.\n", - print_warning, lua_file, target - ); - lua_pop(L, 3); - return false; - } - if (lua_pcall(L, 0, 1, 0) != LUA_OK) { - printf( - "%s %s: 'targets.%s.on_update' function borked: %s\n", - print_error, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 3); - return false; - } - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'targets.%s.on_update' failed: %s\n", - print_error, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 3); - return false; - } - lua_pop(L, 3); - return true; -} - -bool target_loop_build( - lua_State *L, const char* lua_file, const char *target -) { - lua_getfield(L, -1, target); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s' is not a table.\n", - print_warning, lua_file, target - ); - lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) { - target_loop_build(L, lua_file, "default"); - lua_pop(L, 1); - } - return false; - } - lua_getfield(L, -1, "dependencies"); - if (lua_istable(L, -1)) { - lua_pushnil(L); - install_dependencies(L); - } - lua_pop(L, 1); - lua_getfield(L, -1, "build"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s.build' is not a function.\n", - print_warning, lua_file, target - ); - lua_pop(L, 3); - return false; - } - if (lua_pcall(L, 0, 1, 0) != LUA_OK) { - printf( - "%s %s: 'targets.%s.build' function borked: %s\n", - print_error, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 3); - return false; - } - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'targets.%s.build' failed: %s\n", - print_error, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 3); - return false; - } - lua_pop(L, 3); - return true; -} - -bool target_loop_install( - lua_State *L, const char* lua_file, - const char *target -) { - lua_getfield(L, -1, target); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s' is not a table.\n", - print_warning, lua_file, target - ); - lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) { - target_loop_install(L, lua_file, "default"); - lua_pop(L, 1); - } - return false; - } - lua_getfield(L, -1, "pre_install"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s.pre_install' is not a function.\n", - print_warning, lua_file, target - ); - } else if (lua_pcall(L, 0, 1, 0) != LUA_OK) { - if (is_verbose) printf( - "%s %s: 'targets.%s.pre_install' function borked: %s\n", - print_warning, lua_file, target, lua_tostring(L, -1) - ); - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'targets.%s.pre_install' failed: %d\n", - print_error, lua_file, target, (int)lua_tonumber(L, -1) - ); - return false; - } - } - lua_pop(L, 1); - lua_getfield(L, -1, "install"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s.install' is not a function.\n", - print_warning, lua_file, target - ); - lua_pop(L, 1); - return false; - } - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - if (is_verbose) printf( - "%s %s: 'targets.%s.install' function borked: %s\n", - print_warning, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 1); - return false; - } - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'targets.%s.install' failed: %d\n", - print_error, lua_file, target, (int)lua_tonumber(L, -1) - ); - lua_pop(L, 1); - return false; - } - lua_pop(L, 1); - lua_getfield(L, -1, "post_install"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s.post_install' is not a function.\n", - print_warning, lua_file, target - ); - } else if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - if (is_verbose) printf( - "%s %s: 'targets.%s.post_install' function borked: %s\n", - print_warning, lua_file, target, lua_tostring(L, -1) - ); - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'targets.%s.post_install' failed: %s\n", - print_error, lua_file, target, lua_tostring(L, -1) - ); - } - } - lua_pop(L, 1); - return true; -} - -bool target_loop_uninstall(lua_State *L, const char *lua_file, const char *target) { - lua_getfield(L, -1, target); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s' is not a table.\n", - print_warning, lua_file, target - ); - lua_pop(L, 1); - if (strcmp(target, "quiet") == 0) { - target_loop_uninstall(L, lua_file, "default"); - lua_pop(L, 1); - } - return false; - } - lua_getfield(L, -1, "uninstall"); - if (!lua_isfunction(L, -1)) { - if (is_verbose) printf( - "%s %s: 'targets.%s.uninstall' is not a function.\n", - print_warning, lua_file, target - ); - lua_pop(L, 1); - return false; - } - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - if (is_verbose) printf( - "%s %s: 'targets.%s.uninstall' function borked: %s\n", - print_warning, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 1); - return false; - } - if ((int)lua_tonumber(L, -1) != 0) if (is_verbose) { - printf( - "%s %s: 'targets.%s.uninstall' failed: %s\n", - print_error, lua_file, target, lua_tostring(L, -1) - ); - lua_pop(L, 1); - return false; - } - lua_pop(L, 1); - return true; -} - -bool repo_build(const char *repository, const char *target) { - lua_getglobal(L, "repositories"); - if (!config_loaded || !lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories' is not a table.\n", - print_warning - ); - lua_pop(L, 1); - return false; - } - lua_getfield(L, -1, repository); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s' is not a table.\n", - print_warning, repository - ); - lua_pop(L, 2); - return false; - } - - lua_getfield(L, -1, "dependencies"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s.dependencies' is not a table.\n", - print_warning, repository - ); - } else { - lua_pushnil(L); - install_dependencies(L); - } - lua_pop(L, 1); - - lua_getfield(L, -1, "targets"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s.targets' is not a table.\n", - print_warning, repository - ); - return false; - } - bool target_loop_success = target_loop_build(L, "init.lua", target); - lua_pop(L, 3); - return target_loop_success; -} - -bool repo_install(const char *repository, const char* target) { - if (!access(prefix_dir, W_OK)) { - lua_pushfstring(L, "%s", ""); - lua_setglobal(L, "privilege_escalator"); - lua_pop(L, 1); - } - lua_getglobal(L, "repositories"); - if (!config_loaded || !lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories' is not a table.\n", - print_warning - ); - lua_pop(L, 1); - return false; - } - lua_getfield(L, -1, repository); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s' is not a table.\n", - print_warning, repository - ); - lua_pop(L, 2); - return false; - } - lua_getfield(L, -1, "targets"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s.targets' is not a table.\n", - print_warning, repository - ); - return false; - } - bool target_loop_success = target_loop_install(L, "init.lua", target); - lua_pop(L, 3); - return target_loop_success; -} - -bool repo_uninstall(const char *repository, const char *target) { - if (!access(prefix_dir, W_OK)) { - lua_pushfstring(L, "%s", get_privilege_escalator()); - lua_setglobal(L, "privilege_escalator"); - lua_pop(L, 1); - } - lua_getglobal(L, "repositories"); - if (!config_loaded || !lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories' is not a table.\n", - print_warning - ); - lua_pop(L, 1); - return false; - } - lua_getfield(L, -1, repository); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s' is not a table.\n", - print_warning, repository - ); - lua_pop(L, 2); - return false; - } - lua_getfield(L, -1, "targets"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'repositories.%s.targets' is not a table.\n", - print_warning, repository - ); - return false; - } - bool target_loop_success = target_loop_uninstall(L, "init.lua", target); - lua_pop(L, 2); - return target_loop_success; -} - -char* bldit_getver(void) { - init_bldit(); - lua_getglobal(B, "bldit_version"); - if (!lua_isstring(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'bldit_version' is not a string.\n", - print_warning - ); - lua_pop(B, 1); - return NULL; - } - const char* bldit_version = lua_tostring(B, -1); - lua_pop(B, 1); - return strdup(bldit_version); -} - -char* bldit_pkg_getver(void) { - init_bldit(); - lua_getglobal(B, "package_version"); - if (!lua_isstring(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'package_version' is not a string.\n", - print_warning - ); - lua_pop(B, 1); - return strdup(""); - } - const char* package_version = lua_tostring(B, -1); - lua_pop(B, 1); - return strdup(package_version); -} - -bool bldit(const char *target) { - lua_State *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) - ); - lua_close(B); - return false; - } - if (!is_bldit_usable()) { - printf( - "%s bldit version is newer than the installed pkgit version\n", - print_error - ); - printf( - "%s consider updating pkgit\n", - print_pkgit - ); - if (!is_forced) return false; - } - lua_pushfstring(B, "%s", prefix_dir); - lua_setglobal(B, "prefix"); - lua_pop(B, 1); - lua_getglobal(B, "dependencies"); - if (!lua_istable(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'dependencies' is not a table.\n", print_warning - ); - } else { - lua_pushnil(B); - install_dependencies(B); - } - lua_pop(B, 1); - lua_getglobal(B, "targets"); - if (!lua_istable(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'targets' is not a table.\n", print_warning - ); - lua_close(B); - return false; - } - bool target_loop_success = target_loop_build(B, "bldit.lua", target); - lua_pop(B, 2); - lua_close(B); - return target_loop_success; -} - -bool bldit_install(const char *target) { - lua_State *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) - ); - lua_close(B); - return false; - } - if (!is_bldit_usable()) { - printf( - "%s bldit version is newer than the installed pkgit version\n", - print_error - ); - printf( - "%s consider updating pkgit\n", - print_pkgit - ); - if (!is_forced) return false; - } - lua_pushfstring(B, "%s", prefix_dir); - lua_setglobal(B, "prefix"); - lua_pop(B, 1); - if (!access(prefix_dir, W_OK)) { - lua_pushfstring(B, "%s", get_privilege_escalator()); - lua_setglobal(B, "privilege_escalator"); - lua_pop(B, 1); - } - lua_getglobal(B, "targets"); - if (!lua_istable(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'targets' is not a table.\n", - print_warning - ); - lua_pop(B, 1); - lua_close(B); - return false; - } - bool target_loop_success = target_loop_install(B, "bldit.lua", target); - lua_pop(B, 1); - lua_close(B); - return target_loop_success; -} - -bool bldit_uninstall(const char *target) { - lua_State *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) - ); - lua_close(B); - return false; - } - if (!is_bldit_usable()) { - printf( - "%s bldit version is newer than the installed pkgit version\n", - print_error - ); - printf( - "%s consider updating pkgit\n", - print_pkgit - ); - if (!is_forced) return false; - } - lua_pushfstring(B, "%s", prefix_dir); - lua_setglobal(B, "prefix"); - lua_pop(B, 1); - if (!access(prefix_dir, W_OK)) { - lua_pushfstring(B, "%s", get_privilege_escalator()); - lua_setglobal(B, "privilege_escalator"); - lua_pop(B, 1); - } - lua_getglobal(B, "targets"); - if (!lua_istable(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'targets' is not a table.\n", - print_warning - ); - lua_close(B); - return false; - } - lua_getfield(B, -1, target); - if (!lua_istable(B, -1)) { - if (is_verbose) printf( - "%s bldit.lua: 'targets.%s' is not a table.\n", - print_warning, target - ); - lua_pop(B, 2); - lua_close(B); - return false; - } - lua_getglobal(B, "targets"); - if (!lua_istable(B, -1)) { - if (is_verbose) printf("%s bldit.lua: 'targets' is not a table.\n", print_warning); - lua_close(B); - return false; - } - bool target_loop_success = target_loop_uninstall(B, "bldit.lua", target); - lua_pop(B, 1); - lua_close(B); - return target_loop_success; -} - -void cache_repos(void) { - cached_repos_count = 0; - lua_getglobal(L, "repositories"); - if (!config_loaded || !lua_istable(L, -1)) { - printf( - "%s init.lua: 'repositories' is not a table.\n", - print_error - ); - lua_pop(L, 1); - return; - } - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *repo_name = lua_tostring(L, -2); - if (!repo_name || !lua_istable(L, -1)) { - printf("%s init.lua: 'repositories.%s' is not a table\n", print_error, repo_name); - lua_pop(L, 1); - continue; - } - Repo *repo = &cached_repos[cached_repos_count]; - repo->source_key = strdup(repo_name); - lua_getfield(L, -1, "url"); - const char *url = lua_tostring(L, -1); - repo->source_value = url ? strdup(url) : strdup(""); - lua_pop(L, 1); - lua_getfield(L, -1, "version"); - const char *version = lua_tostring(L, -1); - repo->version = version ? strdup(version) : strdup("HEAD"); - lua_pop(L, 1); - lua_pop(L, 1); - cached_repos_count++; - } - lua_pop(L, 1); -} - -void cache_build_systems(void) { - if (!config_loaded) { - lua_getglobal(L, "build_systems"); - } else if (!lua_istable(L, -1)) { - lua_getglobal(L, "build_systems"); - } - if (!lua_istable(L, -1)) { - printf("%s init.lua: 'build_systems' is not a table.\n", print_error); - return; - } - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - if (lua_isfunction(L, -1) == 0) { - printf("%s init.lua: 'build_systems.%s' is not a function\n", print_error, key); - lua_pop(L, 1); - continue; - } - map_put(&cached_build_systems, strdup(key), ""); - lua_pop(L, 1); - } - lua_pop(L, 1); -} - -bool config_build(const char *path, const char *target) { - lua_getglobal(L, "build_systems"); - if (!config_loaded || !lua_istable(L, -1)) { - lua_pop(L, 1); - return false; - } - bool target_loop_success = false; - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - continue; - } - char file_path[MAX_PATH_LEN]; - snprintf(file_path, sizeof(file_path), "%s/%s", path, key); - if (access(file_path, F_OK) != 0) { - lua_pop(L, 1); - continue; - } - lua_getfield(L, -1, "targets"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf("%s init.lua: 'targets' is not a table.\n", print_warning); - lua_pop(L, 1); - continue; - } - target_loop_success = target_loop_build(L, "init.lua", target); - lua_pop(L, 1); - } - lua_pop(L, 1); - return target_loop_success; -} - -bool config_install(const char *path, const char *target) { - if (!access(prefix_dir, W_OK)) { - lua_pushfstring(L, "%s", get_privilege_escalator()); - lua_setglobal(L, "privilege_escalator"); - lua_pop(L, 1); - } - lua_getglobal(L, "build_systems"); - if (!config_loaded || !lua_istable(L, -1)) { - lua_pop(L, 1); - return false; - } - lua_pushnil(L); - bool target_loop_success = false; - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - continue; - } - char file_path[MAX_PATH_LEN]; - snprintf(file_path, sizeof(file_path), "%s/%s", path, key); - if (access(file_path, F_OK) != 0) { - lua_pop(L, 1); - continue; - } - lua_getfield(L, -1, "targets"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'build_systems.%s.targets' is not a table.\n", - print_warning, key - ); - lua_close(L); - return false; - } - target_loop_success = target_loop_install(L, "init.lua", target); - lua_pop(L, 2); - } - lua_pop(L, 1); - return target_loop_success; -} - -bool config_uninstall(const char *path, const char *target) { - if (!access(prefix_dir, W_OK)) { - lua_pushfstring(L, "%s", get_privilege_escalator()); - lua_setglobal(L, "privilege_escalator"); - lua_pop(L, 1); - } - lua_getglobal(L, "build_systems"); - if (!config_loaded || !lua_istable(L, -1)) { - lua_pop(L, 1); - return false; - } - lua_pushnil(L); - bool target_loop_success = false; - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - continue; - } - char file_path[MAX_PATH_LEN]; - snprintf(file_path, sizeof(file_path), "%s/%s", path, key); - if (access(file_path, F_OK) != 0) { - lua_pop(L, 1); - continue; - } - lua_getfield(L, -1, "targets"); - if (!lua_istable(L, -1)) { - if (is_verbose) printf( - "%s init.lua: 'build_systems.%s.targets' is not a table.\n", - print_warning, key - ); - lua_close(L); - return false; - } - target_loop_success = target_loop_uninstall(L, "init.lua", target); - lua_pop(L, 2); - } - lua_pop(L, 1); - return target_loop_success; -} @@ -1,34 +1,9 @@ -/* +#include <stdio.h> - pkgit - package it! +#include "pkgit_string.h" - 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 "cla_parse.h" -#include "lua_state.h" -#include "setup_pkgit.h" -#include "vars.h" - -int main(int argc, char *argv[]) { - init_vars(); - init_lua_state(); - setup_pkgit(); - cache_repos(); - cla_parse(argc, argv); - free_lua_state(); +int main(int argc, char** argv) { + str_s slice = slice_trim(slice_from_cstr(" hello world ")); + printf("[%.*s]\n", format_string(slice)); return 0; -}
\ No newline at end of file +} diff --git a/src/name_from_url.c b/src/name_from_url.c deleted file mode 100644 index c1efc6f..0000000 --- a/src/name_from_url.c +++ /dev/null @@ -1,40 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <string.h> -#include <stdlib.h> - -#include "name_from_url.h" - -char* name_from_url(const char *url) { - size_t len = strlen(url); - char* after_git = strdup(url); - const char *end = after_git + len; - while (end > after_git && *(end-1) == '/') - end--; - if (strncmp(end-4, ".git", 4) == 0) - end -= 4; - const char *last_slash = end; - while (last_slash > after_git && *(last_slash-1) != '/') - last_slash--; - if (last_slash < end) return strndup(last_slash, end - last_slash); - else return strndup(after_git, end - after_git); -} diff --git a/src/pkgit_string.c b/src/pkgit_string.c new file mode 100644 index 0000000..cbcc200 --- /dev/null +++ b/src/pkgit_string.c @@ -0,0 +1,60 @@ +#include <ctype.h> +#include <stdio.h> +#include <string.h> + +#include "pkgit_string.h" + +void print_slice(str_s slice) { + for (size_t i = 0; i < slice.len; i++) { + putchar(slice.data[i]); + } +} + +str_s slice_from_cstr(char* cstr) { + return (str_s) { + .data = cstr, + .len = strlen(cstr), + }; +} + +int slice_eq(str_s a, str_s 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 slice_starts_with(str_s slice, str_s prefix) { + if (prefix.len > slice.len) return 0; + for (size_t i = 0; i < prefix.len; i++) { + if (slice.data[i] != prefix.data[i]) return 0; + } + return 1; +} + +str_s slice_take(str_s slice, size_t n) { + if (n > slice.len) n = slice.len; + return (str_s) { + .data = slice.data, + .len = n + }; +} + +str_s slice_drop(str_s slice, size_t n) { + if (n > slice.len) n = slice.len; + return (str_s) { + .data = slice.data + n, + .len = slice.len - n + }; +} + +str_s slice_trim(str_s slice) { + while (slice.len > 0 && isspace((unsigned char)slice.data[0])) { + slice = slice_drop(slice, 1); + } + while (slice.len > 0 && isspace((unsigned char)slice.data[slice.len - 1])) { + slice = slice_take(slice, slice.len - 1); + } + return slice; +} diff --git a/src/remove_pkg.c b/src/remove_pkg.c deleted file mode 100644 index 71d2f2f..0000000 --- a/src/remove_pkg.c +++ /dev/null @@ -1,118 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <ftw.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <unistd.h> - -#include "lua_state.h" -#include "remove_pkg.h" -#include "vars.h" - -static int remove_installed( - const char *src_path, const struct stat *sb, - int typeflag, struct FTW *ftwbuf -) { - (void)sb; - (void)ftwbuf; - - if (typeflag == FTW_F) { - const char *filename = src_path + ftwbuf->base; - const char *ext = strrchr(filename, '.'); - if (!ext) - ext = ""; - - if (strncmp(ext, ".so", 3) == 0) { - char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("lib"), filename); - if (file_exists(dest)) - remove(dest); - } else if (access(src_path, X_OK) == 0) { - if ( - strcmp(ext, ".sample") != 0 && - strcmp(filename, "bldit") != 0 && - strcmp(filename, "build.sh") != 0 && - strcmp(filename, "compile.sh") != 0 - ) { - char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("bin"), filename); - if (file_exists(dest)) - remove(dest); - } - } else if (strcmp(ext, ".h") == 0) { - char dest[MAX_PATH_LEN]; - snprintf(dest, sizeof(dest), "%s/%s", get_install_dir("include"), - filename); - if (file_exists(dest)) - remove(dest); - } - } - return 0; -} - -static int remove_tree( - const char *fpath, const struct stat *sb, - int typeflag, struct FTW *ftwbuf -) { - (void)sb; - (void)ftwbuf; - if (typeflag == FTW_F || typeflag == FTW_SL) { - unlink(fpath); - } else if (typeflag == FTW_DP) { - rmdir(fpath); - } - return 0; -} - -void remove_pkg(Pkg pkg) { - if (!is_directory(pkg.src)) { - printf("%s %s is not installed!\n", print_pkgit, pkg.name); - return; - } - chdir(pkg.src); - - bool uninstall_available = false; - if (!uninstall_available) if (repo_uninstall(pkg.name, pkg.target)) - uninstall_available = true; - if (!uninstall_available) if (bldit_uninstall(pkg.target)) - uninstall_available = true; - if (!uninstall_available) if (config_uninstall(pkg.src, pkg.target)) - uninstall_available = true; - - if (!uninstall_available) printf( - "%s no uninstall function availible for package: %s\n", - print_error, pkg.name - ); - - nftw(pkg.src, remove_installed, 64, FTW_PHYS); - const char *last_slash = strrchr(pkg.src, '/'); - char target[MAX_PATH_LEN]; - if (last_slash && last_slash != pkg.src) { - size_t parent_len = last_slash - pkg.src; - snprintf(target, sizeof(target), "%.*s", (int)parent_len, pkg.src); - } else { - snprintf(target, sizeof(target), "%s", pkg.src); - } - nftw(pkg.src, remove_tree, 64, FTW_DEPTH | FTW_PHYS); - printf("%s removed %s\n", print_success, pkg.name); -}
\ No newline at end of file diff --git a/src/resolve_deps.c b/src/resolve_deps.c deleted file mode 100644 index 32118f0..0000000 --- a/src/resolve_deps.c +++ /dev/null @@ -1,44 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <unistd.h> - -#include "resolve_deps.h" - -void resolve_deps(void) { - const char *frame_top = " (c _c)"; - const char *frame1_bot = "_/ \\-"; - const char *frame2_bot = "-/ \\_"; - - printf("\033[2J\033[H"); - printf("Unfortunately due to budget issues, we could not afford a progress bar. Enjoy this instead:\n\n"); - - for (int i = 0; i < 16; i++) { - printf("%s\n%s\n", frame_top, i % 2 == 0 ? frame1_bot : frame2_bot); - for (int j = 0; j <= i; j++) printf("."); - printf("\n"); - fflush(stdout); - sleep(1); - if (i < 15) printf("\033[3A"); - } - printf("\n"); - printf("Dependencies resolved! (this does nothing)\n"); -} diff --git a/src/search.c b/src/search.c deleted file mode 100644 index 19f9963..0000000 --- a/src/search.c +++ /dev/null @@ -1,37 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <string.h> - -#include "lua_state.h" -#include "search.h" - -void search(const char* arg) { - if (!arg) { - for (size_t i = 0; i < cached_repos_count; i++) - printf("%s\n", cached_repos[i].source_key); - return; - } - for (size_t i = 0; i < cached_repos_count; i++) { - if (!strstr(cached_repos[i].source_key, arg)) continue; - printf("%s\n", cached_repos[i].source_key); - } -} diff --git a/src/set_install_directories.c b/src/set_install_directories.c deleted file mode 100644 index e7a62e0..0000000 --- a/src/set_install_directories.c +++ /dev/null @@ -1,83 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "lua_state.h" -#include "set_install_directories.h" -#include "vars.h" - -void set_install_directories(void) { - lua_State *L = get_lua_state(); - cache_prefix_directory(); - lua_getglobal(L, "install_directories"); - if (!lua_istable(L, -1)) { - printf( - "%slua variable 'install_directories' is not a table.\n", - print_error - ); - } - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - const char *value = lua_tostring(L, -1); - if (key && value) { - map_put(&cached_install_directories, strdup(key), strdup(value)); - } - lua_pop(L, 1); - } -} - -void map_init(Map *map) { - map->items = NULL; - map->size = 0; - map->capacity = 0; -} - -void map_put(Map *map, char *key, char *value) { - for (size_t i = 0; i < map->size; i++) { - if (strcmp(map->items[i].key, key) == 0) { - free(map->items[i].value); - map->items[i].value = value; - free(key); - return; - } - } - if (map->size >= map->capacity) { - size_t new_capacity = map->capacity == 0 ? 8 : map->capacity * 2; - MapItem *new_items = realloc(map->items, new_capacity * sizeof(MapItem)); - if (!new_items) return; - map->items = new_items; - map->capacity = new_capacity; - } - map->items[map->size].key = key; - map->items[map->size].value = value; - map->size++; -} - -char *map_get(Map *map, const char *key) { - for (size_t i = 0; i < map->size; i++) { - if (strcmp(map->items[i].key, key) != 0) continue; - return map->items[i].value; - } - return NULL; -}
\ No newline at end of file diff --git a/src/setup_dirs.c b/src/setup_dirs.c deleted file mode 100644 index e675f2d..0000000 --- a/src/setup_dirs.c +++ /dev/null @@ -1,34 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <string.h> -#include <sys/stat.h> - -#include "setup_dirs.h" -#include "vars.h" - -void setup_dirs(void) { - mkdir_p(config_dir); - for (int i = 0; i < 5; i++) { - if (install_directories[i] && strlen(install_directories[i]) <= 0) continue; - if (file_exists(install_directories[i])) continue; - mkdir_p(install_directories[i]); - } -}
\ No newline at end of file diff --git a/src/setup_pkgit.c b/src/setup_pkgit.c deleted file mode 100644 index fa7404e..0000000 --- a/src/setup_pkgit.c +++ /dev/null @@ -1,29 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <unistd.h> - -#include "set_install_directories.h" -#include "setup_dirs.h" - -void setup_pkgit(void) { - set_install_directories(); - setup_dirs(); -}
\ No newline at end of file diff --git a/src/update_all.c b/src/update_all.c deleted file mode 100644 index 4ff750d..0000000 --- a/src/update_all.c +++ /dev/null @@ -1,59 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <dirent.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - -#include "update_all.h" -#include "create_pkg.h" -#include "update_pkg.h" -#include "lua_state.h" -#include "vars.h" - -void update_all(void) { - init_lua_state(); - cache_repos(); - struct dirent* dirent_ptr; - DIR* dir_ptr; - if ((dir_ptr = opendir(get_install_dir("src"))) == NULL) { - fprintf( - stderr, "%s could not open %s\n", - print_pkgit, get_install_dir("src") - ); - } - while ((dirent_ptr = readdir(dir_ptr)) != NULL) { - if ( - strcmp(dirent_ptr->d_name, "..") == 0 || - strcmp(dirent_ptr->d_name, ".") == 0 - ) continue; - for (size_t i = 0; i < cached_repos_count; i++) { - if (strcmp(dirent_ptr->d_name, cached_repos[i].source_key) != 0) continue; - Pkg pkg = create_pkg(cached_repos[i].source_value); - update_pkg(pkg); - } - } - closedir(dir_ptr); - on_update(get_lua_state()); -}
\ No newline at end of file diff --git a/src/update_pkg.c b/src/update_pkg.c deleted file mode 100644 index cbe7521..0000000 --- a/src/update_pkg.c +++ /dev/null @@ -1,44 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <stdio.h> -#include <string.h> - -#include "update_pkg.h" - -#include "install_pkg.h" -#include "is_updated.h" -#include "lua_state.h" - -void update_pkg(Pkg pkg) { - if (is_updated(pkg.src)) { - printf( - "%s %s%s%s is already up to date.\n", - print_skipped, green, pkg.name, color_reset - ); - return; - } - printf( - "%s Updating package: %s%s%s\n", - print_pkgit, green, pkg.name, color_reset - ); - install_pkg(pkg); - target_loop_on_update(get_lua_state(), "init.lua", pkg.target); -}
\ No newline at end of file diff --git a/src/vars.c b/src/vars.c deleted file mode 100644 index ea573e1..0000000 --- a/src/vars.c +++ /dev/null @@ -1,220 +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 <https://www.gnu.org/licenses/>. - -*/ - -#include <errno.h> -#include <stdbool.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <sys/stat.h> -#include <unistd.h> - -#include "vars.h" - -bool is_symlink_install = false; -bool is_verbose = true; -bool is_auto_installed = true; -bool is_forced = false; -bool config_exists = false; - -char home_dir[MAX_PATH_LEN] = {0}; -char prefix_dir[MAX_PATH_LEN] = {0}; - -char root_config[MAX_PATH_LEN] = "/etc/pkgit/init.lua"; -bool is_root_config = false; -char config_dir[MAX_PATH_LEN] = {0}; -char config_file[MAX_PATH_LEN] = {0}; -char repo_file[MAX_PATH_LEN] = {0}; - -char bin[MAX_PATH_LEN] = {0}; -char lib[MAX_PATH_LEN] = {0}; -char inc[MAX_PATH_LEN] = {0}; -char src[MAX_PATH_LEN] = {0}; - -char *install_directories[5] = {NULL}; - -Map cached_install_directories = {0}; -Repo cached_repos[MAX_REPOS]; -size_t cached_repos_count = 0; -Map cached_build_systems = {0}; - -char home_config_buf[MAX_PATH_LEN] = {0}; -char *home_config(void) { - snprintf( - home_config_buf, MAX_PATH_LEN, - "%s/.config/pkgit/init.lua", - home_dir - ); - return home_config_buf; -} - -const char *get_install_dir(const char *key) { - for (size_t i = 0; i < cached_install_directories.size; i++) { - if (strcmp(cached_install_directories.items[i].key, key) == 0) { - return cached_install_directories.items[i].value; - } - } - return ""; -} - -const char *version = "1.4.0_INDEV"; - -const char *red = "\x1b[0;31m"; -const char *green = "\x1b[0;32m"; -const char *yellow = "\x1b[0;33m"; -const char *blue = "\x1b[0;34m"; -const char *magenta = "\x1b[0;35m"; -const char *cyan = "\x1b[0;36m"; -const char *gray = "\x1b[0;37m"; -const char *bright_red = "\x1b[0;91m"; -const char *bright_green = "\x1b[0;92m"; -const char *bright_yellow = "\x1b[0;93m"; -const char *bright_blue = "\x1b[0;94m"; -const char *bright_magenta = "\x1b[0;95m"; -const char *bright_cyan = "\x1b[0;96m"; -const char *bright_gray = "\x1b[0;97m"; -const char *bold_red = "\x1b[1;31m"; -const char *bold_green = "\x1b[1;32m"; -const char *bold_yellow = "\x1b[1;33m"; -const char *bold_blue = "\x1b[1;34m"; -const char *bold_magenta = "\x1b[1;35m"; -const char *bold_cyan = "\x1b[1;36m"; -const char *bold_gray = "\x1b[1;37m"; -const char *bold_white = "\x1b[1;38m"; -const char *bold_bright_red = "\x1b[1;91m"; -const char *bold_bright_green = "\x1b[1;92m"; -const char *bold_bright_yellow = "\x1b[1;93m"; -const char *bold_bright_blue = "\x1b[1;94m"; -const char *bold_bright_magenta = "\x1b[1;95m"; -const char *bold_bright_cyan = "\x1b[1;96m"; -const char *bold_bright_gray = "\x1b[1;97m"; -const char *italic = "\x1b[3m"; -const char *color_reset = "\x1b[0m"; - -const char *print_pkgit; -const char *print_success; -const char *print_skipped; -const char *print_warning; -const char *print_error; - -int mkdir_p(const char *path) { - char tmp[MAX_PATH_LEN]; - char *p = NULL; - size_t len; - snprintf(tmp, sizeof(tmp), "%s", path); - len = strlen(tmp); - if (tmp[len - 1] == '/') { - tmp[len - 1] = 0; - } - for (p = tmp + 1; *p; p++) { - if (*p == '/') { - *p = 0; - if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { - return -1; - } - *p = '/'; - } - } - if (mkdir(tmp, 0755) != 0 && errno != EEXIST) { - return -1; - } - return 0; -} - -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); -} - -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")); - - static char print_pkgit_buf[256]; - snprintf( - print_pkgit_buf, sizeof(print_pkgit_buf), - "%s[%s%s%s]%s", - bold_yellow, bold_magenta, "pkgit", - bold_yellow, color_reset - ); - print_pkgit = print_pkgit_buf; - - static char print_success_buf[256]; - snprintf( - print_success_buf, sizeof(print_success_buf), - "%s%s [SUCCESS]%s", - print_pkgit, green, color_reset - ); - print_success = print_success_buf; - - static char print_skipped_buf[256]; - snprintf( - print_skipped_buf, sizeof(print_skipped_buf), - "%s%s [SKIP]%s", - print_pkgit, blue, color_reset - ); - print_skipped = print_skipped_buf; - - static char print_warning_buf[256]; - snprintf( - print_warning_buf, sizeof(print_warning_buf), - "%s%s [WARN]%s", - print_pkgit, yellow, color_reset - ); - print_warning = print_warning_buf; - - static char print_error_buf[256]; - snprintf( - print_error_buf, sizeof(print_error_buf), - "%s%s [ERROR]%s", - print_pkgit, red, color_reset - ); - print_error = print_error_buf; -} |
