diff options
| author | dacctal <dacctal@symlinx.net> | 2026-05-24 10:23:38 +0000 |
|---|---|---|
| committer | dacctal <dacctal@symlinx.net> | 2026-05-24 10:23:38 +0000 |
| commit | aa0d78815004ae6b0c4a42b0e024f5c4ef555ae2 (patch) | |
| tree | 62f97b4c5acfdb98003568466f878fbd3d6b9dfb /src | |
| parent | 83d471f2c1d1b1fa6be51f41e4f1c36ab19d7094 (diff) | |
c rewrite
Diffstat (limited to 'src')
53 files changed, 1495 insertions, 1000 deletions
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/add_repo.c b/src/add_repo.c new file mode 100644 index 0000000..049347c --- /dev/null +++ b/src/add_repo.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <stdlib.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)) { + strcat(rfile_contents, rfile_line); + } + 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); + } + + printf("%s%sAdded %s%s\n", print_pkgit, green, repo_name, color_reset); +}
\ No newline at end of file diff --git a/src/add_repo.cc b/src/add_repo.cc deleted file mode 100644 index 1c66c94..0000000 --- a/src/add_repo.cc +++ /dev/null @@ -1,29 +0,0 @@ -#include <fstream> -#include <iostream> -#include <string> - -#include "add_repo.hh" -#include "vars.hh" - -void add_repo(std::string repo, std::string repo_name) { - bool is_previous_repos = false; - std::string rfile_line; - std::string rfile_contents; - if (std::filesystem::exists(repo_file)) { - std::ifstream rfile(repo_file); - while (getline(rfile, rfile_line)) { - rfile_contents += rfile_line + "\n"; - } - rfile.close(); - is_previous_repos = true; - } - - std::string previous_repos = is_previous_repos ? rfile_contents : ""; - - std::ofstream wfile; - wfile.open(repo_file); - wfile << previous_repos << "repositories." << repo_name << " = { url = \"" << repo << "\" }" << std::endl; - wfile.close(); - - std::cout << print_pkgit << green << "Added " << repo_name << color_reset << std::endl; -} diff --git a/src/build.c b/src/build.c new file mode 100644 index 0000000..a84a13a --- /dev/null +++ b/src/build.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "build.h" +#include "lua_build.h" +#include "vars.h" + +void build(Pkg pkg) { + char original_dir[MAX_PATH_LEN]; + getcwd(original_dir, MAX_PATH_LEN); + + if (strcmp(pkg.src, original_dir) != 0) { + chdir(pkg.src); + } + + if (lua_build(pkg.name, pkg.target, pkg.src)) { + return; + } + + printf("%sno usable build system was found\n", print_error); +}
\ No newline at end of file diff --git a/src/build.cc b/src/build.cc deleted file mode 100644 index a78faf9..0000000 --- a/src/build.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include <filesystem> -#include <iostream> - -#include "build.hh" -#include "lua_build.hh" -#include "vars.hh" - -void build(Pkg pkg) { - if (pkg.src != std::filesystem::current_path().string()) { - std::filesystem::current_path(pkg.src); - } - if (lua_build(pkg.name.c_str(), pkg.target, pkg.src.c_str())) { - return; - } else { - std::cout << print_error << "no usable build system was found\n"; - } -}
\ No newline at end of file diff --git a/src/cla_parse.c b/src/cla_parse.c new file mode 100644 index 0000000..18a9ece --- /dev/null +++ b/src/cla_parse.c @@ -0,0 +1,85 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "cla_parse.h" + +#include "add_repo.h" +#include "build.h" +#include "create_pkg.h" +#include "help.h" +#include "install_pkg.h" +#include "list_pkgs.h" +#include "name_from_url.h" +#include "remove_pkg.h" +#include "update_all.h" +#include "vars.h" + +#include "resolve_deps.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("%sNot enough arguments! Try: `pkgit %s [%s]`\n", print_error, arg, next) + +void cla_parse(int argc, char **argv) { + Pkg pkg = {0}; + + if (!argv[1]) { + help(); + return; + } + + for (int i = 1; i < argc; i++) { + COMMAND("--large", "-l", { is_symlink_install = true; }); + COMMAND("add", "a", { + if (argv[i + 1]) { + add_repo(argv[i + 1], name_from_url(argv[i + 1])); + } else { + NOT_ENOUGH_ARGS(argv[i], "url"); + } + }); + COMMAND("build", "b", { + if (argv[i + 1]) { + if (argv[i + 2]) { + pkg = create_pkg(argv[i + 1], argv[i + 2]); + build(pkg); + } else { + pkg = create_pkg(argv[i + 1], "default"); + build(pkg); + } + } else { + pkg = create_pkg(".", "default"); + build(pkg); + } + }); + COMMAND("install", "i", { + if (argv[i + 1]) { + if (argv[i + 2]) { + pkg = create_pkg(argv[i + 1], argv[i + 2]); + install_pkg(pkg); + } else { + pkg = create_pkg(argv[i + 1], "default"); + install_pkg(pkg); + } + } else { + NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } + }); + COMMAND("remove", "r", { + if (argv[i + 1]) { + pkg = create_pkg(argv[i + 1], "default"); + remove_pkg(pkg); + } else { + NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } + }); + COMMAND("update", "u", { update_all(); }); + COMMAND("list", "l", { list_pkgs(); }); + COMMAND("--version", "-v", { printf("%s\n", version); }); + COMMAND("--help", "-h", { help(); }); + COMMAND("--check", "-c", { resolve_deps(); return; }); + } +}
\ No newline at end of file diff --git a/src/cla_parse.cc b/src/cla_parse.cc deleted file mode 100644 index 76500fd..0000000 --- a/src/cla_parse.cc +++ /dev/null @@ -1,83 +0,0 @@ -#include <cstring> -#include <filesystem> -#include <iostream> -#include <string> - -#include "cla_parse.hh" - -#include "add_repo.hh" -#include "build.hh" -#include "create_pkg.hh" -#include "help.hh" -#include "install_pkg.hh" -#include "list_pkgs.hh" -#include "name_from_url.hh" -#include "remove_pkg.hh" -#include "update_all.hh" -#include "vars.hh" - -#define COMMAND(large, small, code) \ - if (strcmp(argv[i], large) == 0 || strcmp(argv[i], small) == 0) \ - code -#define NOT_ENOUGH_ARGS(arg, next) \ - std::cout << print_error << "Not enough arguments! Try: `pkgit " << arg \ - << " [" << next << "]`" << std::endl - -void cla_parse(int argc, char **argv) { - Pkg pkg; - - if (!argv[1]) { - help(); - return; - } - - for (int i = 1; i < argc; i++) { - COMMAND("--large", "-l", { is_symlink_install = true; }); - COMMAND("add", "a", { - if (argv[i + 1]) { - add_repo(argv[i + 1], name_from_url(argv[i + 1])); - } else { - NOT_ENOUGH_ARGS(argv[i], "url"); - } - }); - COMMAND("build", "b", { - if (argv[i + 1]) { - if (argv[i + 2]) { - pkg = create_pkg(argv[i + 1], argv[i + 2]); - build(pkg); - } else { - pkg = create_pkg(argv[i + 1], "default"); - build(pkg); - } - } else { - pkg = create_pkg(".", "default"); - build(pkg); - } - }); - COMMAND("install", "i", { - if (argv[i + 1]) { - if (argv[i + 2]) { - pkg = create_pkg(argv[i + 1], argv[i + 2]); - install_pkg(pkg); - } else { - pkg = create_pkg(argv[i + 1], "default"); - install_pkg(pkg); - } - } else { - NOT_ENOUGH_ARGS(argv[i], "url/pkg"); - } - }); - COMMAND("remove", "r", { - pkg = create_pkg(argv[i + 1]); - if (argv[i + 1]) { - remove_pkg(pkg); - } else { - NOT_ENOUGH_ARGS(argv[i], "url/pkg"); - } - }); - COMMAND("update", "u", { update_all(); }); - COMMAND("list", "l", { list_pkgs(); }); - COMMAND("--version", "-v", { std::cout << version << std::endl; }); - COMMAND("--help", "-h", { help(); }); - } -} diff --git a/src/cmd_out.c b/src/cmd_out.c new file mode 100644 index 0000000..b800149 --- /dev/null +++ b/src/cmd_out.c @@ -0,0 +1,42 @@ +#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); + 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; + } + strcat(result, buffer); + total_size += len; + } + + pclose(pipe); + return result; +}
\ No newline at end of file diff --git a/src/cmd_out.cc b/src/cmd_out.cc deleted file mode 100644 index 3a59f2e..0000000 --- a/src/cmd_out.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include <cstdio> -#include <memory> -#include <stdexcept> -#include <string> -#include <array> - -#include "cmd_out.hh" - -std::string cmd_out(const char* cmd) { - std::array<char, 128> buffer; - std::string result; - std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose); - if (!pipe) { - throw std::runtime_error("popen() failed!"); - } - while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) { - result += buffer.data(); - } - return result; -} diff --git a/src/copy_install.c b/src/copy_install.c new file mode 100644 index 0000000..b1b56ff --- /dev/null +++ b/src/copy_install.c @@ -0,0 +1,124 @@ +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <unistd.h> + +#include "copy_install.h" +#include "vars.h" + +static int is_executable(const char *path) { return (access(path, X_OK) == 0); } + +static int is_dir(const char *path) { + struct stat st; + if (stat(path, &st) == 0) { + return S_ISDIR(st.st_mode); + } + return 0; +} + +static const char *get_extension(const char *path) { + const char *dot = strrchr(path, '.'); + return dot ? dot : ""; +} + +static int copy_file(const char *src, const char *dst) { + struct stat st; + if (stat(src, &st) != 0) + return -1; + + int src_fd = open(src, O_RDONLY); + if (src_fd < 0) + return -1; + + int dst_fd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, st.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 n == 0 ? 0 : -1; +} + +static void copy_install_recursive(const char *dir_path) { + DIR *dir = opendir(dir_path); + if (!dir) + return; + + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; + } + + char src_path[MAX_PATH_LEN]; + snprintf(src_path, sizeof(src_path), "%s/%s", dir_path, entry->d_name); + + if (is_dir(src_path)) { + if (strcmp(entry->d_name, ".git") != 0) { + copy_install_recursive(src_path); + } + continue; + } + + const char *ext = get_extension(entry->d_name); + + if (strncmp(ext, ".so", 3) == 0) { + char dest_path[MAX_PATH_LEN]; + snprintf(dest_path, sizeof(dest_path), "%s/%s", lib, entry->d_name); + if (!file_exists(dest_path)) { + copy_file(src_path, dest_path); + if (is_verbose) { + printf("%scopied library: %s\n", print_pkgit, src_path); + } + } + } else if (is_executable(src_path)) { + if (strcmp(ext, ".sample") != 0 && strcmp(entry->d_name, "bldit") != 0 && + strcmp(entry->d_name, "build.sh") != 0 && + strcmp(entry->d_name, "compile.sh") != 0) { + char dest_path[MAX_PATH_LEN]; + snprintf(dest_path, sizeof(dest_path), "%s/%s", bin, entry->d_name); + if (!file_exists(dest_path)) { + copy_file(src_path, dest_path); + if (is_verbose) { + printf("%scopied executable: %s\n", print_pkgit, src_path); + } + } + } + } else if (strcmp(ext, ".h") == 0) { + char dest_path[MAX_PATH_LEN]; + snprintf(dest_path, sizeof(dest_path), "%s/%s", inc, entry->d_name); + if (!file_exists(dest_path)) { + copy_file(src_path, dest_path); + if (is_verbose) { + printf("%scopied include: %s\n", print_pkgit, src_path); + } + } + } + } + + closedir(dir); +} + +void copy_install(const char *build_dir) { + copy_install_recursive(build_dir); +} diff --git a/src/copy_install.cc b/src/copy_install.cc deleted file mode 100644 index a87fa21..0000000 --- a/src/copy_install.cc +++ /dev/null @@ -1,34 +0,0 @@ -#include <filesystem> -#include <iostream> -#include <string> -#include <unistd.h> - -#include "copy_install.hh" -#include "vars.hh" - -void copy_install(std::filesystem::path build_dir) { - for (auto const& dir_entry : std::filesystem::recursive_directory_iterator(build_dir)) { - if (dir_entry.path().extension().string().rfind(".so", 0) == 0) { - if (!std::filesystem::exists(install_directories["lib"]+"/"+dir_entry.path().filename().string())) { - copy(dir_entry, install_directories["lib"]+"/"+dir_entry.path().filename().string()); - if (is_verbose) { std::cout << print_pkgit << "copied library: " << dir_entry << "\n"; } - } else { /*std::cout << print_pkgit << "library already exists: " << dir_entry << "\n";*/ } - - } else if (!access(dir_entry.path().c_str(), X_OK) && !is_directory(dir_entry.path())) { - if (!std::filesystem::exists(install_directories["bin"]+"/"+dir_entry.path().filename().string()) && - dir_entry.path().extension() != ".sample" && - dir_entry.path().filename().string() != "bldit" && - dir_entry.path().filename().string() != "build.sh" && - dir_entry.path().filename().string() != "compile.sh") { - copy(dir_entry, install_directories["bin"]+"/"+dir_entry.path().filename().string()); - if (is_verbose) { std::cout << print_pkgit << "copied executable: " << dir_entry << "\n"; } - } else { /*std::cout << print_pkgit << "executable already exists: " << dir_entry << "\n";*/ } - - } else if (dir_entry.path().extension() == ".h") { - if (!std::filesystem::exists(install_directories["include"]+"/"+dir_entry.path().filename().string())) { - copy(dir_entry, install_directories["include"]+"/"+dir_entry.path().filename().string()); - if (is_verbose) { std::cout << print_pkgit << "copied include: " << dir_entry << "\n"; } - } else { /*std::cout << print_pkgit << "include already exists: " << dir_entry << "\n";*/ } - } - } -} diff --git a/src/create_pkg.c b/src/create_pkg.c new file mode 100644 index 0000000..eed9ef1 --- /dev/null +++ b/src/create_pkg.c @@ -0,0 +1,58 @@ +#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" + +Pkg create_pkg(const char *arg, const char *target) { + Pkg pkg = {0}; + pkg.target = target; + pkg.ver = "HEAD"; + pkg.is_local = false; + + init_lua_state(); + cache_repos(); + + bool is_in_repos = false; + for (size_t i = 0; i < cached_repos_count; i++) { + if (strcmp(arg, cached_repos[i].source_key) == 0) { + is_in_repos = true; + break; + } + } + + if (strncmp(arg, "http", 4) == 0) { + pkg.url = strdup(arg); + pkg.name = name_from_url(arg); + } else if (strcmp(arg, ".") == 0) { + pkg.url = ""; + getcwd(pkg.src, MAX_PATH_LEN); + pkg.name = name_from_url(pkg.src); + pkg.is_local = true; + } else if (is_in_repos) { + 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 = strdup(arg); + } else { + printf("%s'%s' is not a valid package\n", print_error, arg); + exit(1); + } + + cache_install_directories(); + + if (!pkg.is_local) { + char src_dir[MAX_PATH_LEN]; + snprintf(src_dir, sizeof(src_dir), "%s/%s/%s", get_install_dir("src"), pkg.name, pkg.ver); + snprintf(pkg.src, MAX_PATH_LEN, "%s", src_dir); + } + + return pkg; +}
\ No newline at end of file diff --git a/src/create_pkg.cc b/src/create_pkg.cc deleted file mode 100644 index 4208208..0000000 --- a/src/create_pkg.cc +++ /dev/null @@ -1,45 +0,0 @@ -#include <iostream> -#include <string> -#include <filesystem> - -#include "create_pkg.hh" -#include "lua_state.hh" -#include "name_from_url.hh" -#include "vars.hh" - -Pkg create_pkg(std::string arg, const char* target) { - Pkg pkg; - pkg.target = target; - pkg.ver = "HEAD"; - pkg.is_local = false; - bool is_in_repos = false; - - init_lua_state(); - cache_repos(); - for (auto repo : cached_repos) { - if (arg == repo.first) { is_in_repos = true; } - } - - if (arg.rfind("http", 0) == 0) { - pkg.url = arg; - pkg.name = name_from_url(arg); - } else if (arg == ".") { - pkg.url = ""; - pkg.src = std::filesystem::current_path().string(); - pkg.name = name_from_url(std::filesystem::current_path().string()); - pkg.is_local = true; - } else if (is_in_repos) { - pkg.url = cached_repos[arg].source.value; - pkg.name = arg; - } else { - std::cout << print_error << "'" << arg << "'" << " is not a valid package" << std::endl; - exit(1); - } - - cache_install_directories(); - if (!pkg.is_local) { - pkg.src = install_directories["src"] + "/" + pkg.name + "/" + pkg.ver; - } - - return pkg; -} diff --git a/src/ensure_repo.cc b/src/ensure_repo.cc deleted file mode 100644 index 2a33c2c..0000000 --- a/src/ensure_repo.cc +++ /dev/null @@ -1,24 +0,0 @@ -#include <iostream> -#include <fstream> - -#include "ensure_repo.hh" -#include "vars.hh" - -void ensure_repo() { - if (!std::filesystem::exists(repo_file)) { - std::cout << "repo file does not exist" << std::endl; - std::ofstream file; - file.open(repo_file); - - if (!file.is_open()) { - std::cout << print_error << "repo file not created" << std::endl; - return; - } - - file << "repos = {}" << std::endl; - file << "repos[\"pkgit\"] = https://git.symlinx.net/pkgit" << std::endl; - - std::cout << repo_file << std::endl; - file.close(); - } -} diff --git a/src/fetch_git.c b/src/fetch_git.c new file mode 100644 index 0000000..04f6428 --- /dev/null +++ b/src/fetch_git.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "fetch_git.h" +#include "vars.h" + +int fetch_git(Pkg pkg) { + pid_t pid = fork(); + if (pid == 0) { + const char *argv[8]; + 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++] = 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) { + printf("clone failed\n"); + } + + return result; +}
\ No newline at end of file diff --git a/src/fetch_git.cc b/src/fetch_git.cc deleted file mode 100644 index dc5015b..0000000 --- a/src/fetch_git.cc +++ /dev/null @@ -1,22 +0,0 @@ -#include <cstring> -#include <iostream> - -#include "fetch_git.hh" -#include "vars.hh" - -int fetch_git(Pkg pkg) { - std::string clone_cmds[] = { - "git -c advice.detachedHead=false clone " + pkg.url + " " + pkg.src.c_str(), - "git -c advice.detachedHead=false clone --branch " + pkg.ver + " " + pkg.url + " " + pkg.src.c_str() - }; - if (strcmp(pkg.ver.c_str(), "HEAD") == 0) { - if (system(clone_cmds[0].c_str()) != 0) { - std::cout << "clone failed" << std::endl; - } - } else { - if (system(clone_cmds[1].c_str()) != 0) { - std::cout << "clone failed" << std::endl; - } - } - return 0; -} diff --git a/src/fetch_pwd.c b/src/fetch_pwd.c new file mode 100644 index 0000000..84cd54d --- /dev/null +++ b/src/fetch_pwd.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.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_pwd.cc b/src/fetch_pwd.cc deleted file mode 100644 index aa61063..0000000 --- a/src/fetch_pwd.cc +++ /dev/null @@ -1,6 +0,0 @@ -#include "fetch_pwd.hh" -#include "vars.hh" - -void fetch_pwd(Pkg pkg) { - std::filesystem::copy(std::filesystem::current_path(), pkg.src); -} diff --git a/src/fetch_src.c b/src/fetch_src.c new file mode 100644 index 0000000..044ef12 --- /dev/null +++ b/src/fetch_src.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> +#include <ftw.h> +#include <unistd.h> + +#include "fetch_src.h" +#include "fetch_git.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) { + printf("%starget source directory: %s\n", print_pkgit, pkg.src); + + if (file_exists(pkg.src)) { + printf("%s%s already exists. deleting...\n", print_pkgit, pkg.src); + nftw(pkg.src, remove_tree, 64, FTW_DEPTH | FTW_PHYS); + } + + if (strcmp(pkg.url, "") == 0) { + printf("%screating directory %s...\n", print_pkgit, pkg.src); + mkdir_p(pkg.src); + return; + } + + if (fetch_git(pkg) == 0) { + printf("%scloned into %s...\n", print_pkgit, pkg.src); + return; + } + + printf("%sno fetch methods worked.\n", print_error); + exit(EXIT_FAILURE); +}
\ No newline at end of file diff --git a/src/fetch_src.cc b/src/fetch_src.cc deleted file mode 100644 index 626e798..0000000 --- a/src/fetch_src.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include <stdlib.h> -#include <iostream> -#include "fetch_src.hh" -#include "fetch_git.hh" - -void fetch_src(Pkg pkg) { - std::cout << print_pkgit << "target source directory: " << pkg.src << std::endl; - if (std::filesystem::exists(pkg.src)) { - std::cout << print_pkgit << pkg.src << " already exists. deleting..." << std::endl; - std::filesystem::remove_all(pkg.src); - } - if (pkg.url == "") { - std::cout << print_pkgit << "creating directory " << pkg.src << "..." << std::endl; - std::filesystem::create_directories(pkg.src); - return; - } - else if (fetch_git(pkg) == 0) { - std::cout << print_pkgit << "cloned into " << pkg.src << "..." << std::endl; - return; - } - else { - std::cout << print_error << "no fetch methods worked." << std::endl; - exit(EXIT_FAILURE); - } -} diff --git a/src/help.c b/src/help.c new file mode 100644 index 0000000..0c0325d --- /dev/null +++ b/src/help.c @@ -0,0 +1,36 @@ +#include <stdio.h> +#include <string.h> + +#include "help.h" +#include "vars.h" + +void help() { + printf("%s , \n", bold_magenta); + printf("%s / \\ \n", bold_magenta); + printf("%s / \\ \n", bold_magenta); + printf("%s __-' '-__ \n", bold_magenta); + printf("%s ''--__ __--'' %s\n", bold_magenta, bold_yellow); + printf(" %s_--%s\\ /%s--_ \n", bold_yellow, bold_magenta, bold_yellow); + printf(" %s_--' %s\\ /%s '--_ \n", bold_yellow, bold_magenta, bold_yellow); + printf(" %s'-__ %s'%s __-'\n", bold_yellow, bold_magenta, bold_yellow); + printf(" %s'-__ __-' \n", bold_yellow); + printf(" %s'-_-' %s\n", bold_yellow, color_reset); + printf("\n"); + printf(" pkgit\n"); + printf(" %s%s- package it! -%s\n", italic, gray, color_reset); + printf(" %sv%s%s\n", magenta, version, color_reset); + printf("\n"); + printf("%ssubcommands%s:\n", red, color_reset); + printf("%s...... %sa%s, %sadd %s[url, file] %s# add a repo/repopkg\n", color_reset, green, color_reset, yellow, blue, gray); + printf("%s...... %sb%s, %sbuild %s[path] %s# build a package\n", color_reset, green, color_reset, yellow, blue, gray); + printf("%s...... %si%s, %sinstall %s[pkgs, urls] %s# install a package/repo\n", color_reset, green, color_reset, yellow, blue, gray); + printf("%s...... %sr%s, %sremove %s[pkgs] %s# remove an installed package\n", color_reset, green, color_reset, yellow, blue, gray); + printf("%s...... %sl%s, %slist %s# list installed packages\n", color_reset, green, color_reset, yellow, gray); + printf("%s...... %su%s, %supdate %s# update all installed packages\n", color_reset, green, color_reset, yellow, gray); + printf("\n"); + printf("%sflags%s:\n", red, color_reset); + 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); +} + diff --git a/src/help.cc b/src/help.cc deleted file mode 100644 index e6a590f..0000000 --- a/src/help.cc +++ /dev/null @@ -1,37 +0,0 @@ -#include <iostream> - -#include "vars.hh" -#include "help.hh" - -void help() { - std::cout << ""+bold_magenta+" , \n"; - std::cout << " "+bold_magenta+"/ \\ \n"; - std::cout << " "+bold_magenta+"/ \\ \n"; - std::cout << " "+bold_magenta+"__-' '-__ \n"; - std::cout << " "+bold_magenta+"''--__ __--'' "+bold_yellow+"\n"; - std::cout << " "+bold_yellow+"_--"+bold_magenta+"\\ /"+bold_yellow+"--_ \n"; - std::cout << " "+bold_yellow+"_--' "+bold_magenta+"\\ /"+bold_yellow+" '--_ \n"; - std::cout << " "+bold_yellow+"'-__ "+bold_magenta+"'"+bold_yellow+" __-'\n"; - std::cout << " "+bold_yellow+"'-__ __-' \n"; - std::cout << " "+bold_yellow+"'-_-' "+color_reset+"\n"; - std::cout << "\n"; - std::cout << " pkgit\n"; - std::cout << " "+italic+""+gray+"- package it! -"+color_reset+"\n"; - std::cout << " "+magenta+"v"+version+""+color_reset+"\n"; - std::cout << "\n"; - std::cout << ""+red+"subcommands"+color_reset+":\n"; - std::cout << ""+color_reset+"├─ "+green+"a"+color_reset+", "+yellow+"add "+blue+"[url, file] "+gray+"# add a repo/repopkg\n"; - std::cout << ""+color_reset+"├─ "+green+"b"+color_reset+", "+yellow+"build "+blue+"[path] "+gray+"# build a package\n"; - std::cout << ""+color_reset+"├┬ "+green+"i"+color_reset+", "+yellow+"install "+blue+"[pkgs, urls] "+gray+"# install a package/repo\n"; - //std::cout << ""+color_reset+"│├── "+green+"-t:"+color_reset+", "+yellow+"--tag:"+blue+"[tag] "+gray+"# specify a version\n"; - //std::cout << ""+color_reset+"│└── "+green+"-l:"+color_reset+", "+yellow+"--list:"+blue+"[filename] "+gray+"# install from a package list\n"; - std::cout << ""+color_reset+"├─ "+green+"r"+color_reset+", "+yellow+"remove "+blue+"[pkgs] "+gray+"# remove an installed package\n"; - //std::cout << ""+color_reset+"├─ "+green+"f"+color_reset+", "+yellow+"files "+blue+"[pkgs] "+gray+"# list all files of a package\n"; - //std::cout << ""+color_reset+"├─ "+green+"s"+color_reset+", "+yellow+"search "+blue+"[pkgs] "+gray+"# search for packages\n"; - std::cout << ""+color_reset+"├─ "+green+"l"+color_reset+", "+yellow+"list "+gray+"# list installed packages\n"; - std::cout << ""+color_reset+"└─ "+green+"u"+color_reset+", "+yellow+"update "+gray+"# update all installed packages\n"; - std::cout << "\n"; - std::cout << ""+red+"flags"+color_reset+":\n"; - std::cout << ""+color_reset+"├─ "+green+"-h"+color_reset+", "+yellow+"--help "+gray+"# display this help message\n"; - std::cout << ""+color_reset+"└─ "+green+"-v"+color_reset+", "+yellow+"--version "+gray+"# display version number\n"; -} diff --git a/src/install_pkg.c b/src/install_pkg.c new file mode 100644 index 0000000..d599de0 --- /dev/null +++ b/src/install_pkg.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "install_pkg.h" +#include "add_repo.h" +#include "fetch_src.h" +#include "build.h" +#include "copy_install.h" +#include "link_install.h" +#include "lua_state.h" +#include "name_from_url.h" +#include "vars.h" + +void install_pkg(Pkg pkg) { + if (!pkg.is_local) { + printf("%sfetching source...\n", print_pkgit); + fetch_src(pkg); + printf("%ssource fetched!\n", print_pkgit); + } + + printf("%sbuilding...\n", print_pkgit); + build(pkg); + printf("%sbuild complete!\n", print_pkgit); + + printf("%sinstalling...\n", print_pkgit); + if (is_symlink_install) { + link_install(pkg.src); + } else { + copy_install(pkg.src); + } + printf("%sinstalled!\n", print_pkgit); + + bool repo_exists = false; + for (size_t i = 0; i < cached_repos_count; i++) { + char *repo_name = name_from_url(cached_repos[i].source_value); + if (strcmp(repo_name, pkg.name) == 0) { + repo_exists = true; + } + free(repo_name); + } + + if (!repo_exists) { + printf("%sadding repository...\n", print_pkgit); + if (pkg.url && strlen(pkg.url) > 0) { + add_repo(pkg.url, pkg.name); + } + printf("%sdone!\n", print_pkgit); + } else { + printf("%srepo already exists, done!\n", print_pkgit); + } +}
\ No newline at end of file diff --git a/src/install_pkg.cc b/src/install_pkg.cc deleted file mode 100644 index 208c397..0000000 --- a/src/install_pkg.cc +++ /dev/null @@ -1,43 +0,0 @@ -#include <iostream> - -#include "add_repo.hh" -#include "fetch_src.hh" -#include "build.hh" -#include "copy_install.hh" -#include "install_pkg.hh" -#include "link_install.hh" -#include "lua_state.hh" -#include "name_from_url.hh" -#include "vars.hh" - -void install_pkg(Pkg pkg) { - if (!pkg.is_local) { - std::cout << print_pkgit << "fetching source..." << std::endl; - fetch_src(pkg); - std::cout << print_pkgit << "source fetched!" << std::endl; - } - - std::cout << print_pkgit << "building..." << std::endl; - build(pkg); - std::cout << print_pkgit << "build complete!" << std::endl; - - std::cout << print_pkgit << "installing..." << std::endl; - if (is_symlink_install) { link_install(pkg.src); } - else { copy_install(pkg.src); } - std::cout << print_pkgit << "installed!" << std::endl; - - bool repo_exists = false; - for (auto repo : cached_repos) { - if (name_from_url(repo.second.source.value) == pkg.name) { - repo_exists = true; - } - } - - if (!repo_exists) { - std::cout << print_pkgit << "adding repository..." << std::endl; - add_repo(pkg.url, pkg.name); - std::cout << print_pkgit << "done!" << std::endl; - } else { - std::cout << print_pkgit << "repo already exists, done!" << std::endl; - } -} diff --git a/src/is_updated.c b/src/is_updated.c new file mode 100644 index 0000000..29b482d --- /dev/null +++ b/src/is_updated.c @@ -0,0 +1,20 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <unistd.h> + +#include "is_updated.h" +#include "cmd_out.h" +#include "vars.h" + +bool is_updated(const char *src) { + if (src && strlen(src) > 0 && chdir(src) != 0) { + return false; + } + + char *output = cmd_out("git pull"); + bool result = (strstr(output, "Already up to date.") != NULL); + free(output); + return result; +}
\ No newline at end of file diff --git a/src/is_updated.cc b/src/is_updated.cc deleted file mode 100644 index b2c3b43..0000000 --- a/src/is_updated.cc +++ /dev/null @@ -1,13 +0,0 @@ -#include <filesystem> - -#include "is_updated.hh" -#include "cmd_out.hh" - -bool is_updated(std::string src) { - if (src != std::filesystem::current_path().string()) { - if (!std::filesystem::exists(src)) { return false; } - std::filesystem::current_path(src); - } - if (cmd_out("git pull") == "Already up to date.") { return true; } - return false; -} diff --git a/src/link_install.c b/src/link_install.c new file mode 100644 index 0000000..9f658d4 --- /dev/null +++ b/src/link_install.c @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> +#include <dirent.h> + +#include "link_install.h" +#include "vars.h" + +static int is_executable(const char *path) { + return (access(path, X_OK) == 0); +} + +static int is_dir(const char *path) { + struct stat st; + if (stat(path, &st) == 0) { + return S_ISDIR(st.st_mode); + } + return 0; +} + +static const char* get_extension(const char *path) { + const char *dot = strrchr(path, '.'); + return dot ? dot : ""; +} + +void link_install(const char *build_dir) { + DIR *dir = opendir(build_dir); + if (!dir) return; + + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; + } + + char src_path[MAX_PATH_LEN]; + char link_path[MAX_PATH_LEN]; + snprintf(src_path, sizeof(src_path), "%s/%s", build_dir, entry->d_name); + + const char *ext = get_extension(entry->d_name); + + if (strncmp(ext, ".so", 3) == 0) { + snprintf(link_path, sizeof(link_path), "%s/%s", lib, entry->d_name); + if (!file_exists(link_path)) { + symlink(src_path, link_path); + if (is_verbose) { + printf("%slinked library: %s\n", print_pkgit, src_path); + } + } + } else if (is_executable(src_path) && !is_dir(src_path)) { + if (strcmp(entry->d_name, "bldit") != 0 && + strcmp(entry->d_name, "build.sh") != 0 && + strcmp(entry->d_name, "compile.sh") != 0) { + snprintf(link_path, sizeof(link_path), "%s/%s", bin, entry->d_name); + if (!file_exists(link_path)) { + symlink(src_path, link_path); + if (is_verbose) { + printf("%slinked executable: %s\n", print_pkgit, src_path); + } + } + } + } else if (strcmp(ext, ".h") == 0) { + snprintf(link_path, sizeof(link_path), "%s/%s", inc, entry->d_name); + if (!file_exists(link_path)) { + symlink(src_path, link_path); + if (is_verbose) { + printf("%slinked include: %s\n", print_pkgit, src_path); + } + } + } + } + + closedir(dir); +}
\ No newline at end of file diff --git a/src/link_install.cc b/src/link_install.cc deleted file mode 100644 index 3eee1ee..0000000 --- a/src/link_install.cc +++ /dev/null @@ -1,36 +0,0 @@ -#include <filesystem> -#include <iostream> -#include <string> -#include <unistd.h> - -#include "link_install.hh" -#include "vars.hh" - -void link_install(std::filesystem::path build_dir) { - for (auto const& dir_entry : std::filesystem::recursive_directory_iterator(build_dir)) { - std::string lib_link = install_directories["lib"]+"/"+dir_entry.path().filename().string(); - std::string bin_link = install_directories["bin"]+"/"+dir_entry.path().filename().string(); - std::string include_link = install_directories["include"]+"/"+dir_entry.path().filename().string(); - if (dir_entry.path().extension().string().rfind(".so", 0) == 0) { - if (!std::filesystem::exists(lib_link)) { - create_symlink(dir_entry, lib_link); - if (is_verbose) { std::cout << print_pkgit << "copied library: " << dir_entry << "\n"; } - } else { /*std::cout << print_pkgit << "library already exists: " << dir_entry << "\n";*/ } - - } else if (!access(dir_entry.path().c_str(), X_OK) && !is_directory(dir_entry.path())) { - if (!std::filesystem::exists(bin_link) && - dir_entry.path().filename().string() != "bldit" && - dir_entry.path().filename().string() != "build.sh" && - dir_entry.path().filename().string() != "compile.sh") { - create_symlink(dir_entry, bin_link); - if (is_verbose) { std::cout << print_pkgit << "copied executable: " << dir_entry << "\n"; } - } else { /*std::cout << print_pkgit << "executable already exists: " << dir_entry << "\n";*/ } - - } else if (dir_entry.path().extension() == ".h") { - if (!std::filesystem::exists(include_link)) { - create_symlink(dir_entry, include_link); - if (is_verbose) { std::cout << print_pkgit << "copied include: " << dir_entry << "\n"; } - } else { /*std::cout << print_pkgit << "include already exists: " << dir_entry << "\n";*/ } - } - } -} diff --git a/src/list_pkgs.c b/src/list_pkgs.c new file mode 100644 index 0000000..5ac293a --- /dev/null +++ b/src/list_pkgs.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +#include "list_pkgs.h" +#include "lua_state.h" + +void list_pkgs() { + init_lua_state(); + cache_repos(); + for (size_t i = 0; i < cached_repos_count; i++) { + printf("%s\n", cached_repos[i].source_key); + } +}
\ No newline at end of file diff --git a/src/list_pkgs.cc b/src/list_pkgs.cc deleted file mode 100644 index a64c753..0000000 --- a/src/list_pkgs.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include <iostream> - -#include "list_pkgs.hh" -#include "lua_state.hh" -#include "vars.hh" - -void list_pkgs() { - for (auto repo : cached_repos) { - std::cout << repo.first << std::endl; - } -} diff --git a/src/lua_build.c b/src/lua_build.c new file mode 100644 index 0000000..6f7c571 --- /dev/null +++ b/src/lua_build.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include <stdbool.h> + +#include "lua_build.h" +#include "lua_state.h" +#include "vars.h" + +bool lua_build(const char *repository, const char *target, const char *path) { + printf("%sattempting to use build function specified in 'repositories.%s'...\n", + print_pkgit, repository); + + if (repo_build(repository)) { + return true; + } + + printf("%sattempting to use build function specified in 'bldit.lua'...\n", print_pkgit); + if (bldit(target)) { + return true; + } + + printf("%sattempting to use build functions specified in 'build_systems'...\n", print_pkgit); + if (config_build(path)) { + return true; + } + + return false; +}
\ No newline at end of file diff --git a/src/lua_build.cc b/src/lua_build.cc deleted file mode 100644 index fd07184..0000000 --- a/src/lua_build.cc +++ /dev/null @@ -1,25 +0,0 @@ -#include "lua_build.hh" -#include "lua_state.hh" -#include "vars.hh" -#include <filesystem> -#include <iostream> -#include <unordered_map> - -bool lua_build(const char *repository, const char *target, const char *path) { - std::cout << print_pkgit << "attempting to use build function specified in 'repositories." << repository << "'..." << std::endl; - if (repo_build(repository)) { - return true; - } - - std::cout << print_pkgit << "attempting to use build function specified in 'bldit.lua'..." << std::endl; - if (bldit(target)) { - return true; - } - - std::cout << print_pkgit << "attempting to use build functions specified in 'build_systems'..." << std::endl; - if (config_build(path)) { - return true; - } - - return false; -}
\ No newline at end of file diff --git a/src/lua_state.c b/src/lua_state.c new file mode 100644 index 0000000..c5ba738 --- /dev/null +++ b/src/lua_state.c @@ -0,0 +1,320 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.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() { + 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("%scannot run configuration script: %s\n", print_error, lua_tostring(L, -1)); + return; + } + + if (file_exists(repo_file)) { + if (luaL_loadfile(L, repo_file) || lua_pcall(L, 0, 0, 0)) { + printf("%scannot load repository file: %s\n", print_error, lua_tostring(L, -1)); + lua_pop(L, 1); + } + } + + config_loaded = true; +} + +void init_bldit() { + if (B != NULL) + return; + + B = luaL_newstate(); + luaL_openlibs(B); + + if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { + printf("%scannot run bldit script: %s\n", print_warning, lua_tostring(B, -1)); + return; + } + bldit_loaded = true; +} + +void free_lua_state() { + if (L != NULL) { + lua_close(L); + L = NULL; + } + config_loaded = false; +} + +lua_State* get_lua_state() { + return L; +} + +void cache_install_directories() { + if (!config_loaded || !lua_istable(L, -1)) { + lua_getglobal(L, "install_directories"); + } + + if (!lua_istable(L, -1)) { + printf("%slua variable '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); +} + +bool repo_build(const char *repository) { + lua_getglobal(L, "repositories"); + if (!config_loaded || !lua_istable(L, -1)) { + printf("%slua variable 'repositories' is not a table.\n", print_warning); + lua_pop(L, 1); + return false; + } + + printf("%slua variable 'repositories' used successfully.\n", print_pkgit); + + lua_getfield(L, -1, repository); + if (!lua_istable(L, -1)) { + printf("%s'repositories' lua variable '%s' is not a table.\n", print_warning, repository); + lua_pop(L, 2); + return false; + } + + printf("%s'repositories' lua variable '%s' used successfully.\n", print_pkgit, repository); + + lua_getfield(L, -1, "build"); + if (!lua_isfunction(L, -1)) { + printf("%s'repositories' lua variable 'build' is not a function.\n", print_warning); + lua_pop(L, 3); + return false; + } + + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + printf("'repositories' build failed: %s\n", lua_tostring(L, -1)); + lua_pop(L, 1); + lua_pop(L, 2); + return false; + } + + lua_pop(L, 2); + printf("%s'repositories' lua function 'build' ran successfully.\n", print_pkgit); + return true; +} + +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)) { + printf("%scannot run bldit script: %s\n", print_warning, lua_tostring(B, -1)); + lua_close(B); + return false; + } + + lua_getglobal(B, "targets"); + if (!lua_istable(B, -1)) { + printf("%sbldit variable 'targets' is not a table.\n", print_warning); + lua_close(B); + return false; + } + + printf("%sbldit variable 'targets' used successfully.\n", print_pkgit); + + lua_getfield(B, -1, target); + if (!lua_istable(B, -1)) { + printf("%sbldit variable '%s' is not a table.\n", print_warning, target); + lua_pop(B, 2); + lua_close(B); + return false; + } + + printf("%sbldit variable '%s' used successfully.\n", print_pkgit, target); + + lua_getfield(B, -1, "build"); + if (!lua_isfunction(B, -1)) { + printf("%s'repositories' lua variable 'build' is not a function.\n", print_warning); + lua_pop(B, 3); + lua_close(B); + return false; + } + + if (lua_pcall(B, 0, 0, 0) != LUA_OK) { + printf("build failed: %s\n", lua_tostring(B, -1)); + lua_pop(B, 1); + lua_pop(B, 2); + lua_close(B); + return false; + } + + lua_pop(B, 2); + printf("%sbldit function 'build' ran successfully.\n", print_pkgit); + lua_close(B); + return true; +} + +void cache_repos() { + cached_repos_count = 0; + lua_getglobal(L, "repositories"); + if (!config_loaded || !lua_istable(L, -1)) { + printf("%slua variable '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("%srepository key is not a table\n", print_error); + 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, "dependencies"); + if (!lua_istable(L, -1)) { + lua_pop(L, 2); + cached_repos_count++; + continue; + } + + lua_pushnil(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); + + Dependency *dep = realloc(repo->dependencies, (repo->dep_count + 1) * sizeof(Dependency)); + if (dep) { + repo->dependencies = dep; + repo->dependencies[repo->dep_count].url = dep_url ? strdup(dep_url) : strdup(""); + repo->dependencies[repo->dep_count].version = dep_version ? strdup(dep_version) : strdup(""); + repo->dep_count++; + } + } + lua_pop(L, 1); + } + + lua_pop(L, 1); + lua_pop(L, 1); + cached_repos_count++; + } + lua_pop(L, 1); +} + +void cache_build_systems() { + 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("%slua variable '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("%sbuild value is not a function\n", print_error); + 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) { + lua_getglobal(L, "build_systems"); + if (!config_loaded || !lua_istable(L, -1)) { + lua_pop(L, 1); + return 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, "build"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 1); + continue; + } + + if (lua_pcall(L, 0, 0, 0) != LUA_OK) { + lua_pop(L, 1); + continue; + } + + lua_pop(L, 1); + lua_pop(L, 1); + return true; + } + lua_pop(L, 1); + return false; +}
\ No newline at end of file diff --git a/src/lua_state.cc b/src/lua_state.cc deleted file mode 100644 index c9bcb12..0000000 --- a/src/lua_state.cc +++ /dev/null @@ -1,322 +0,0 @@ -#include "lua_state.hh" -#include "vars.hh" -#include <iostream> -#include <unordered_map> - -static lua_State *L = nullptr; -static bool config_loaded = false; - -static lua_State *B = nullptr; -static bool bldit_loaded = false; - -std::unordered_map<std::string, std::string> cached_install_directories; -std::unordered_map<std::string, repo> cached_repos; -std::unordered_map<std::string, int> cached_build_systems; - -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() { - if (L != nullptr) - return; - L = luaL_newstate(); - luaL_openlibs(L); - push_lua_path(L, (config_dir + "/?.lua").c_str()); - if (luaL_loadfile(L, config_file.c_str()) || lua_pcall(L, 0, 0, 0)) { - std::cout << print_error << "cannot run configuration script: " << lua_tostring(L, -1) << "\n"; - return; - } - config_loaded = true; -} - -void init_bldit() { - if (B != nullptr) - return; - B = luaL_newstate(); - luaL_openlibs(B); - if (luaL_loadfile(B, "bldit.lua") || lua_pcall(B, 0, 0, 0)) { - std::cout << print_warning - << "cannot run bldit script: " << lua_tostring(B, -1) << "\n"; - return; - } - bldit_loaded = true; -} - -void free_lua_state() { - if (L != nullptr) { - lua_close(L); - L = nullptr; - } - config_loaded = false; -} - -lua_State *get_lua_state() { return L; } - -void cache_install_directories() { - if (!config_loaded || !lua_istable(L, -1)) { - lua_getglobal(L, "install_directories"); - } - if (!lua_istable(L, -1)) { - std::cout << print_error - << "lua variable 'install_directories' is not a table.\n"; - return; - } - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - const char *value = lua_tostring(L, -1); - cached_install_directories[key] = value; - lua_pop(L, 1); - } - lua_pop(L, 1); -} - -bool repo_build(const char *repository) { - lua_getglobal(L, "repositories"); - if (!config_loaded || !lua_istable(L, -1)) { - std::cout << print_warning - << "lua variable 'repositories' is not a table.\n"; - lua_pop(L, 1); - return false; - } - std::cout << print_pkgit - << "lua variable 'repositories' used successfully.\n"; - lua_getfield(L, -1, repository); - if (!lua_istable(L, -1)) { - std::cout << print_warning << "'repositories' lua variable '" << repository - << "' is not a table.\n"; - lua_pop(L, 2); - return false; - } - std::cout << print_pkgit << "'repositories' lua variable '" << repository - << "' used successfully.\n"; - lua_getfield(L, -1, "build"); - if (!lua_isfunction(L, -1)) { - std::cout << print_warning - << "'repositories' lua variable 'build' is not a function." - << std::endl; - lua_pop(L, 3); - return false; - } - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - std::cout << "'repositories' build failed: " << lua_tostring(L, -1) - << std::endl; - lua_pop(L, 1); - lua_pop(L, 2); - return false; - } - lua_pop(L, 2); - std::cout << print_pkgit - << "'repositories' lua function 'build' ran successfully.\n"; - return true; -} - -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)) { - std::cout << print_warning - << "cannot run bldit script: " << lua_tostring(B, -1) << "\n"; - return false; - } - lua_getglobal(B, "targets"); - if (!lua_istable(B, -1)) { - std::cout << print_warning << "bldit variable 'targets' is not a table.\n"; - return false; - } - std::cout << print_pkgit << "bldit variable 'targets' used successfully.\n"; - lua_getfield(B, -1, target); - if (!lua_istable(B, -1)) { - std::cout << print_warning << "bldit variable '" << target - << "' is not a table.\n"; - lua_pop(B, 2); - return false; - } - std::cout << print_pkgit << "bldit variable '" << target - << "' used successfully.\n"; - lua_getfield(B, -1, "build"); - if (!lua_isfunction(B, -1)) { - std::cout << "'repositories' lua variable 'build' is not a function." - << std::endl; - lua_pop(B, 3); - return false; - } - if (lua_pcall(B, 0, 0, 0) != LUA_OK) { - std::cout << "build failed: " << lua_tostring(B, -1) << std::endl; - lua_pop(B, 1); - lua_pop(B, 2); - return false; - } - lua_pop(B, 2); - std::cout << print_pkgit << "bldit function 'build' ran successfully.\n"; - return true; -} - -// bool config_build(const char* path) { -// free_lua_state(); -// init_lua_state(); -// lua_State* L = get_lua_state(); -// std::unordered_map<std::string, int> build_files; -// lua_getglobal(L, "build_systems"); -// if (!config_loaded || !lua_istable(L, -1)) { -// lua_pop(L, 1); -// std::cout << print_warning << "config lua variable 'build_systems' is not -// a table.\n"; return false; -// } -// lua_pushnil(L); -// bool build_found = false; -// while (lua_next(L, -2) != 0) { -// const char *key = lua_tostring(L, -2); -// int value = lua_type(L, -1); -// if (lua_isfunction(L, -1) == 0) { -// std::cout << print_warning << "config build value " << key << " is not -// a function\n"; lua_pop(L, 1); continue; -// } -// build_files[key] = value; -// for (auto const& dir_entry : -// std::filesystem::directory_iterator(std::filesystem::current_path().string())) -// { -// std::string string_key = key; -// if (dir_entry.path().filename() != string_key) { continue; } -// build_found = true; -// lua_pushvalue(L, -1); -// lua_pushstring(L, std::filesystem::current_path().string().c_str()); -// std::cout << "calling config lua build function according to key -// filename '" -// << key << "'...\n"; -// if (lua_pcall(L, 1, 0, 0) != 0) { -// std::cout << print_warning << "config lua build function failed to -// run\n"; break; -// } -// if (build_found) { break; } -// } -// lua_pop(L, 1); -// } -// return build_found; -// } - -void cache_repos() { - lua_getglobal(L, "repositories"); - if (!config_loaded || !lua_istable(L, -1)) { - std::cout << print_error << "lua variable 'repositories' is not a table.\n"; - 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)) { - std::cout << print_error << "repository key is not a table\n"; - lua_pop(L, 1); - continue; - } - lua_getfield(L, -1, "url"); - const char *url = lua_tostring(L, -1); - lua_pop(L, 1); - cached_repos[repo_name].source.key = "url"; - cached_repos[repo_name].source.value = url ? url : ""; - lua_getfield(L, -1, "dependencies"); - if (!lua_istable(L, -1)) { - // std::cout << print_warning << repo_name << ": \tlua variable - // 'dependencies' is not a table.\n"; - lua_pop(L, 2); - continue; - } - lua_pushnil(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); - cached_repos[repo_name].dependencies[depname].url = - dep_url ? dep_url : ""; - cached_repos[repo_name].dependencies[depname].version = - dep_version ? dep_version : ""; - } - lua_pop(L, 1); - } - lua_pop(L, 1); - lua_pop(L, 1); - } - lua_pop(L, 1); -} - -void cache_build_systems() { - if (!config_loaded) { - lua_getglobal(L, "build_systems"); - } else if (!lua_istable(L, -1)) { - lua_getglobal(L, "build_systems"); - } - if (!lua_istable(L, -1)) { - std::cout << print_error - << "lua variable 'build_systems' is not a table.\n"; - return; - } - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - if (lua_isfunction(L, -1) == 0) { - std::cout << print_error << "build value is not a function\n"; - lua_pop(L, 1); - continue; - } - cached_build_systems[key] = lua_gettop(L); - lua_pop(L, 1); - } - lua_pop(L, 1); -} - -bool config_build(const char *path) { - lua_getglobal(L, "build_systems"); - if (!config_loaded || !lua_istable(L, -1)) { - std::cout << print_warning - << "lua variable 'build_systems' is not a table.\n"; - lua_pop(L, 1); - return false; - } - std::cout << print_pkgit - << "lua variable 'build_systems' used successfully.\n"; - lua_pushnil(L); - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - if (!lua_istable(L, -1)) { - std::cout << print_error << "build value " << key << " is not a table\n"; - lua_pop(L, 1); - continue; - } - for (auto const &dir_entry : std::filesystem::directory_iterator( - std::filesystem::current_path().string())) { - std::string string_key = key; - if (dir_entry.path().filename() != string_key) { - continue; - } - lua_getfield(L, -1, "build"); - if (lua_isfunction(L, -1) == 0) { - std::cout << print_error << "no build function for " << key << "\n"; - lua_pop(L, 1); - continue; - } - if (lua_pcall(L, 0, 0, 0) != LUA_OK) { - std::cout << "'" << key << "' build failed: " << lua_tostring(L, -1) - << std::endl; - lua_pop(L, 1); - continue; - } - } - lua_pop(L, 1); - } - lua_pop(L, 1); - return true; -}
\ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..534e45c --- /dev/null +++ b/src/main.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "cla_parse.h" +#include "lua_state.h" +#include "setup_pkgit.h" +#include "vars.h" + +int main(int argc, char *argv[]) { + init_vars(); + setup_pkgit(); + cla_parse(argc, argv); + free_lua_state(); + return 0; +}
\ No newline at end of file diff --git a/src/main.cc b/src/main.cc deleted file mode 100644 index d94a4d8..0000000 --- a/src/main.cc +++ /dev/null @@ -1,10 +0,0 @@ -#include "cla_parse.hh" -#include "lua_state.hh" -#include "setup_pkgit.hh" - -int main(int argc, char* argv[]) { - setup_pkgit(); - cla_parse(argc, argv); - free_lua_state(); - return 0; -} diff --git a/src/name_from_url.c b/src/name_from_url.c new file mode 100644 index 0000000..121fddd --- /dev/null +++ b/src/name_from_url.c @@ -0,0 +1,22 @@ +#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); + const char *end = url + len; + while (end > url && *(end-1) == '/') { + end--; + } + const char *last_slash = end; + while (last_slash > url && *(last_slash-1) != '/') { + last_slash--; + } + if (last_slash < end) { + return strndup(last_slash, end - last_slash); + } else { + return strndup(url, end - url); + } +} diff --git a/src/name_from_url.cc b/src/name_from_url.cc deleted file mode 100644 index 56a2830..0000000 --- a/src/name_from_url.cc +++ /dev/null @@ -1,6 +0,0 @@ -#include <string> -#include "name_from_url.hh" - -std::string name_from_url(std::string url) { - return url.substr(url.find_last_of('/') + 1); -} diff --git a/src/remove_pkg.c b/src/remove_pkg.c new file mode 100644 index 0000000..31627f9 --- /dev/null +++ b/src/remove_pkg.c @@ -0,0 +1,72 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/stat.h> +#include <ftw.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 (!file_exists(pkg.src)) { + printf("%s%s is not installed!\n", print_pkgit, pkg.name); + return; + } + + 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(target, remove_tree, 64, FTW_DEPTH | FTW_PHYS); + + printf("%sremoved %s\n", print_pkgit, pkg.name); +}
\ No newline at end of file diff --git a/src/remove_pkg.cc b/src/remove_pkg.cc deleted file mode 100644 index 0138f38..0000000 --- a/src/remove_pkg.cc +++ /dev/null @@ -1,30 +0,0 @@ -#include <filesystem> -#include <iostream> -#include <string> -#include <unistd.h> - -#include "remove_pkg.hh" -#include "vars.hh" - -void remove_pkg(Pkg pkg) { - if (!std::filesystem::exists(pkg.src)) { - std::cout << print_pkgit << pkg.name << " is not installed!" << std::endl; - return; - } - for (auto const& dir_entry : std::filesystem::recursive_directory_iterator(pkg.src)) { - if (dir_entry.path().extension().string().rfind(".so", 0) == 0) { - std::filesystem::remove(install_directories["lib"]+"/"+dir_entry.path().filename().string()); - //std::cout << print_pkgit << "removed library: " << dir_entry << "\n"; - - } else if (!access(dir_entry.path().c_str(), X_OK) && !is_directory(dir_entry.path())) { - std::filesystem::remove(install_directories["bin"]+"/"+dir_entry.path().filename().string()); - //std::cout << print_pkgit << "removed executable: " << dir_entry << "\n"; - - } else if (dir_entry.path().extension() == ".h") { - std::filesystem::remove(install_directories["include"]+"/"+dir_entry.path().filename().string()); - //std::cout << print_pkgit << "removed include: " << dir_entry << "\n"; - } - } - remove_all(pkg.src.parent_path()); - std::cout << print_pkgit << "removed " << pkg.name << "\n"; -} diff --git a/src/resolve_deps.c b/src/resolve_deps.c new file mode 100644 index 0000000..e4d50f6 --- /dev/null +++ b/src/resolve_deps.c @@ -0,0 +1,28 @@ +#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("67! "); + } + printf("\n"); + fflush(stdout); + usleep(300000); + if (i < 15) { + printf("\033[3A"); + } + } + printf("\n"); + printf("Dependencies resolved! 1 pregnancy found.\n"); +} diff --git a/src/set_install_directories.c b/src/set_install_directories.c new file mode 100644 index 0000000..2df6aaf --- /dev/null +++ b/src/set_install_directories.c @@ -0,0 +1,69 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "set_install_directories.h" +#include "lua_state.h" +#include "vars.h" + +void set_install_directories() { + init_lua_state(); + lua_State *L = get_lua_state(); + + 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) { + return map->items[i].value; + } + } + return NULL; +}
\ No newline at end of file diff --git a/src/set_install_directories.cc b/src/set_install_directories.cc deleted file mode 100644 index 9163480..0000000 --- a/src/set_install_directories.cc +++ /dev/null @@ -1,26 +0,0 @@ -#include <iostream> -#include "lua_state.hh" -#include "set_install_directories.hh" -#include "vars.hh" - -void set_install_directories() { - init_lua_state(); - lua_State *L = get_lua_state(); - - lua_getglobal(L, "install_directories"); - - if (!lua_istable(L, -1)) { - std::cout << print_error << "lua variable 'install_directories' is not a table.\n"; - } - - lua_pushnil(L); - - while (lua_next(L, -2) != 0) { - const char *key = lua_tostring(L, -2); - const char *value = lua_tostring(L, -1); - - install_directories[key] = value; - - lua_pop(L, 1); - } -}
\ No newline at end of file diff --git a/src/setup_dirs.c b/src/setup_dirs.c new file mode 100644 index 0000000..c3ec272 --- /dev/null +++ b/src/setup_dirs.c @@ -0,0 +1,19 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> + +#include "setup_dirs.h" +#include "vars.h" + +void setup_dirs() { + mkdir_p(config_dir); + + for (int i = 0; i < 5; i++) { + if (install_directories[i] && strlen(install_directories[i]) > 0) { + if (!file_exists(install_directories[i])) { + mkdir_p(install_directories[i]); + } + } + } +}
\ No newline at end of file diff --git a/src/setup_dirs.cc b/src/setup_dirs.cc deleted file mode 100644 index 54b9eb0..0000000 --- a/src/setup_dirs.cc +++ /dev/null @@ -1,17 +0,0 @@ -#include <filesystem> -#include <iostream> - -#include "setup_dirs.hh" -#include "vars.hh" - -void setup_dirs() { - std::filesystem::create_directories(config_dir); - for (auto i: install_directories) { - if (std::filesystem::exists(i.second)) continue; - if (i.second == "") continue; - std::filesystem::create_directories(i.second); - } - //for(unsigned int i = 0; i < sizeof(all_dirs)/sizeof(all_dirs[0]); i++) { - // std::filesystem::create_directories(all_dirs[i]); - //} -} diff --git a/src/setup_pkgit.c b/src/setup_pkgit.c new file mode 100644 index 0000000..b58149b --- /dev/null +++ b/src/setup_pkgit.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "set_install_directories.h" +#include "setup_dirs.h" +#include "vars.h" + +void setup_pkgit() { + set_install_directories(); + setup_dirs(); +}
\ No newline at end of file diff --git a/src/setup_pkgit.cc b/src/setup_pkgit.cc deleted file mode 100644 index df7e87f..0000000 --- a/src/setup_pkgit.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include <iostream> - -#include "set_install_directories.hh" -#include "setup_pkgit.hh" -#include "setup_dirs.hh" -#include "vars.hh" - -void setup_pkgit() { - set_install_directories(); - setup_dirs(); -} diff --git a/src/update_all.c b/src/update_all.c new file mode 100644 index 0000000..1018f65 --- /dev/null +++ b/src/update_all.c @@ -0,0 +1,17 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "update_all.h" +#include "create_pkg.h" +#include "update_pkg.h" +#include "lua_state.h" + +void update_all() { + init_lua_state(); + cache_repos(); + for (size_t i = 0; i < cached_repos_count; i++) { + Pkg pkg = create_pkg(cached_repos[i].source_value, "default"); + update_pkg(pkg); + } +}
\ No newline at end of file diff --git a/src/update_all.cc b/src/update_all.cc deleted file mode 100644 index 6f55543..0000000 --- a/src/update_all.cc +++ /dev/null @@ -1,12 +0,0 @@ -#include "update_all.hh" -#include "create_pkg.hh" -#include "lua_state.hh" -#include "update_pkg.hh" -#include "vars.hh" - -void update_all() { - for (auto repo : cached_repos) { - Pkg pkg = create_pkg(repo.second.source.value); - update_pkg(pkg); - } -} diff --git a/src/update_pkg.c b/src/update_pkg.c new file mode 100644 index 0000000..c0fdd92 --- /dev/null +++ b/src/update_pkg.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include <string.h> + +#include "update_pkg.h" +#include "is_updated.h" +#include "install_pkg.h" + +void update_pkg(Pkg pkg) { + if (is_updated(pkg.src)) { + printf("%s%s is already up to date.\n", print_skipped, pkg.name); + return; + } + install_pkg(pkg); +}
\ No newline at end of file diff --git a/src/update_pkg.cc b/src/update_pkg.cc deleted file mode 100644 index 881beff..0000000 --- a/src/update_pkg.cc +++ /dev/null @@ -1,11 +0,0 @@ -#include <filesystem> -#include <iostream> - -#include "update_pkg.hh" -#include "is_updated.hh" -#include "install_pkg.hh" - -void update_pkg(Pkg pkg) { - if (is_updated(pkg.src)) { std::cout << print_skipped << pkg.name << " is already up to date."; return; } - install_pkg(pkg); -} diff --git a/src/vars.c b/src/vars.c new file mode 100644 index 0000000..0568e57 --- /dev/null +++ b/src/vars.c @@ -0,0 +1,181 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <unistd.h> +#include <sys/stat.h> +#include <errno.h> + +#include "vars.h" + +bool is_symlink_install = false; +bool is_verbose = false; +bool config_exists = false; + +char home_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 pkgblds[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() { + 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 = "gizmodic pkgit v67.67.67 (non-pregtal edition)"; //changed from "0.0.0" muhahahah + +const char *red = "\e[0;31m"; +const char *green = "\e[0;32m"; +const char *yellow = "\e[0;33m"; +const char *blue = "\e[0;34m"; +const char *magenta = "\e[0;35m"; +const char *cyan = "\e[0;36m"; +const char *gray = "\e[0;37m"; +const char *bright_red = "\e[0;91m"; +const char *bright_green = "\e[0;92m"; +const char *bright_yellow = "\e[0;93m"; +const char *bright_blue = "\e[0;94m"; +const char *bright_magenta = "\e[0;95m"; +const char *bright_cyan = "\e[0;96m"; +const char *bright_gray = "\e[0;97m"; +const char *bold_red = "\e[1;31m"; +const char *bold_green = "\e[1;32m"; +const char *bold_yellow = "\e[1;33m"; +const char *bold_blue = "\e[1;34m"; +const char *bold_magenta = "\e[1;35m"; +const char *bold_cyan = "\e[1;36m"; +const char *bold_gray = "\e[1;37m"; +const char *bold_white = "\e[1;38m"; +const char *bold_bright_red = "\e[1;91m"; +const char *bold_bright_green = "\e[1;92m"; +const char *bold_bright_yellow = "\e[1;93m"; +const char *bold_bright_blue = "\e[1;94m"; +const char *bold_bright_magenta = "\e[1;95m"; +const char *bold_bright_cyan = "\e[1;96m"; +const char *bold_bright_gray = "\e[1;97m"; +const char *italic = "\e[3m"; +const char *color_reset = "\e[0m"; + +const char *print_pkgit; +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() { + 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(pkgblds, MAX_PATH_LEN, "%s/.local/share/pkgit", home_dir); + + install_directories[0] = config_dir; + install_directories[1] = bin; + install_directories[2] = lib; + install_directories[3] = inc; + install_directories[4] = pkgblds; + + static char print_pkgit_buf[256]; + snprintf(print_pkgit_buf, sizeof(print_pkgit_buf), "%s[%s%s%s]\t%s", + bold_yellow, bold_magenta, "pkgit", bold_yellow, color_reset); + print_pkgit = print_pkgit_buf; + + static char print_skipped_buf[256]; + snprintf(print_skipped_buf, sizeof(print_skipped_buf), "%s%s[SKIP]\t%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]\t%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]\t%s", + print_pkgit, red, color_reset); + print_error = print_error_buf; +} diff --git a/src/vars.cc b/src/vars.cc deleted file mode 100644 index b619e15..0000000 --- a/src/vars.cc +++ /dev/null @@ -1,81 +0,0 @@ -#include <filesystem> -#include <unordered_map> -#include <string> - -#include "vars.hh" - -//std::unordered_map<std::string, std::string> repos; -std::unordered_map<std::string, std::string> install_directories; - -bool is_symlink_install = false; -bool is_verbose = false; - -const std::string home_dir = std::getenv("HOME"); - -const std::string root_config = "/etc/pkgit/init.lua"; -const std::string home_config = home_dir + "/.config/pkgit"; -bool is_root_config = std::filesystem::exists(root_config); - -const std::string config_dir = is_root_config ? "/etc/pkgit" : home_dir + "/.config/pkgit"; -const std::string config_file = config_dir + "/init.lua"; -const std::string repo_file = config_dir + "/repos.lua"; - -bool config_exists = std::filesystem::exists(root_config) || std::filesystem::exists(home_config); - -const std::string bin = config_exists ? install_directories["bin"] : home_dir + "/.local/bin"; -const std::string lib = config_exists ? install_directories["lib"] : home_dir + "/.local/lib"; -const std::string include = config_exists ? install_directories["include"] : home_dir + "/.local/include"; -const std::string pkgblds = config_exists ? install_directories["pkgblds"] : home_dir + "/.local/share/pkgit"; -const std::string all_dirs[] = { - config_dir, - install_directories["bin"], - install_directories["lib"], - install_directories["include"], - install_directories["pkgblds"] -}; - -// version -const std::string version = "0.0.0"; - -// colors -const std::string red = "\e[0;31m"; -const std::string green = "\e[0;32m"; -const std::string yellow = "\e[0;33m"; -const std::string blue = "\e[0;34m"; -const std::string magenta = "\e[0;35m"; -const std::string cyan = "\e[0;36m"; -const std::string gray = "\e[0;37m"; -// bright -const std::string bright_red = "\e[0;91m"; -const std::string bright_green = "\e[0;92m"; -const std::string bright_yellow = "\e[0;93m"; -const std::string bright_blue = "\e[0;94m"; -const std::string bright_magenta = "\e[0;95m"; -const std::string bright_cyan = "\e[0;96m"; -const std::string bright_gray = "\e[0;97m"; -// bold -const std::string bold_red = "\e[1;31m"; -const std::string bold_green = "\e[1;32m"; -const std::string bold_yellow = "\e[1;33m"; -const std::string bold_blue = "\e[1;34m"; -const std::string bold_magenta = "\e[1;35m"; -const std::string bold_cyan = "\e[1;36m"; -const std::string bold_gray = "\e[1;37m"; -const std::string bold_white = "\e[1;38m"; -// bold bright -const std::string bold_bright_red = "\e[1;91m"; -const std::string bold_bright_green = "\e[1;92m"; -const std::string bold_bright_yellow = "\e[1;93m"; -const std::string bold_bright_blue = "\e[1;94m"; -const std::string bold_bright_magenta = "\e[1;95m"; -const std::string bold_bright_cyan = "\e[1;96m"; -const std::string bold_bright_gray = "\e[1;97m"; -// italic -const std::string italic = "\e[3m"; -// reset -const std::string color_reset = "\e[0m"; - -const std::string print_pkgit = bold_yellow + "[" + bold_magenta + "pkgit" + bold_yellow + "]\t" + color_reset; -const std::string print_skipped = print_pkgit + blue + "[SKIP]\t" + color_reset; -const std::string print_warning = print_pkgit + yellow + "[WARN]\t" + color_reset; -const std::string print_error = print_pkgit + red + "[ERROR]\t" + color_reset; |
