diff options
| author | dacctal <dacctal@symlinx.net> | 2026-06-21 01:33:58 +0000 |
|---|---|---|
| committer | dacctal <dacctal@symlinx.net> | 2026-06-21 01:33:58 +0000 |
| commit | 7cde9aeb5dccce578c1eed13cf1ed2c61e446800 (patch) | |
| tree | 3323e34bbec0a87a4bb659a90816b28a6a241f5a /src | |
| parent | 6482da05e96630d622f7deea02e31cb0ff07c89e (diff) | |
huge code cleanup
Diffstat (limited to 'src')
| -rw-r--r-- | src/add_repo.c | 1 | ||||
| -rw-r--r-- | src/build.c | 13 | ||||
| -rw-r--r-- | src/cla_parse.c | 184 | ||||
| -rw-r--r-- | src/create_pkg.c | 117 | ||||
| -rw-r--r-- | src/fetch_git.c | 3 | ||||
| -rw-r--r-- | src/fetch_pwd.c | 68 | ||||
| -rw-r--r-- | src/fetch_src.c | 68 | ||||
| -rw-r--r-- | src/help.c | 53 | ||||
| -rw-r--r-- | src/install_pkg.c | 5 | ||||
| -rw-r--r-- | src/is_updated.c | 15 | ||||
| -rw-r--r-- | src/lua_build.c | 56 | ||||
| -rw-r--r-- | src/lua_state.c | 10 | ||||
| -rw-r--r-- | src/main.c | 14 | ||||
| -rw-r--r-- | src/resolve_deps.c | 6 | ||||
| -rw-r--r-- | src/search.c (renamed from src/find.c) | 6 | ||||
| -rw-r--r-- | src/set_install_directories.c | 90 | ||||
| -rw-r--r-- | src/setup_dirs.c | 1 | ||||
| -rw-r--r-- | src/setup_pkgit.c | 3 | ||||
| -rw-r--r-- | src/update_all.c | 2 | ||||
| -rw-r--r-- | src/update_pkg.c | 1 | ||||
| -rw-r--r-- | src/vars.c | 195 |
21 files changed, 464 insertions, 447 deletions
diff --git a/src/add_repo.c b/src/add_repo.c index db8c46a..71c3dda 100644 --- a/src/add_repo.c +++ b/src/add_repo.c @@ -1,5 +1,4 @@ #include <stdio.h> -#include <stdlib.h> #include <string.h> #include <stdbool.h> diff --git a/src/build.c b/src/build.c index 549fe64..e5be9b2 100644 --- a/src/build.c +++ b/src/build.c @@ -8,12 +8,11 @@ #include "vars.h" void build(Pkg pkg) { - char original_dir[MAX_PATH_LEN]; - getcwd(original_dir, MAX_PATH_LEN); + char cwd[MAX_PATH_LEN]; + getcwd(cwd, MAX_PATH_LEN); + if (strcmp(pkg.src, cwd) != 0 && !pkg.is_local) chdir(pkg.src); - if (strcmp(pkg.src, original_dir) != 0) chdir(pkg.src); - if (lua_build(pkg.name, pkg.target, pkg.src)) return; - - printf("%s no usable build system was found\n", print_error); - exit(EXIT_FAILURE); + 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 index 4eae6f6..964a38b 100644 --- a/src/cla_parse.c +++ b/src/cla_parse.c @@ -1,5 +1,4 @@ #include <stdio.h> -#include <stdlib.h> #include <string.h> #include "cla_parse.h" @@ -8,106 +7,129 @@ #include "build.h" #include "create_pkg.h" #include "declare.h" -#include "find.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" -#include "resolve_deps.h" +#define COMMAND(large, small, code) \ + if (strcmp(argv[i], large) == 0 || strcmp(argv[i], small) == 0) code -#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) -#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 cla_parse(int argc, char **argv) { - Pkg pkg = {0}; +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"); + } +} - if (!argv[1]) { - help(); - return; +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 (int 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 (int 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++) { - COMMAND("--link", "-l", { is_symlink_install = true; }); - COMMAND("--quiet", "-q", { is_verbose = false; }); - COMMAND("--force", "-f", { is_forced = true; }); + if (argv[i][0] != '-') continue; + if (argv[i][1] == '-') { + COMMAND("--quiet", "-q", { is_verbose = false; }); + COMMAND("--force", "-f", { is_forced = true; }); + COMMAND("--version", "-v", { printf("%s\n", version); }); + COMMAND("--help", "-h", { help(); }); + COMMAND("--check", "-c", { resolve_deps(); }); + } 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", { - 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 (!is_verbose) { - for (int j = i + 1; j < argc; j++) { - if (argv[j][0] == '-') continue; - pkg = create_pkg(argv[j]); - build(pkg); - } - } else { - 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); - } - }); - COMMAND("install", "i", { - if (argv[i + 1]) { - if (!is_verbose) { - for (int j = i + 1; j < argc; j++) { - if (argv[j][0] == '-') continue; - pkg = create_pkg(argv[j]); - install_pkg(pkg); - } - } else { - 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"); - } - }); - COMMAND("remove", "r", { - 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"); - } - }); + 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("find", "f", { find(argv[i + 1]); }); - COMMAND("--version", "-v", { printf("%s\n", version); }); - COMMAND("--help", "-h", { help(); }); - COMMAND("--check", "-c", { - resolve_deps(); - return; - }); + COMMAND("search", "s", { search(argv[i + 1]); }); } +} + +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/create_pkg.c b/src/create_pkg.c index cfb388f..2ce3588 100644 --- a/src/create_pkg.c +++ b/src/create_pkg.c @@ -4,66 +4,100 @@ #include <unistd.h> #include "create_pkg.h" -#include "files.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 = false; + if (is_directory(dest_dir)) is_installed_locally = true; + char *verptr = strchr(new_arg, '@'); + char *trgptr = strchr(new_arg, ','); for (int i = 0; i < cached_repos_count; i++) { - if (strcmp(cached_repos[i].source_key, arg) == 0) { - pkg.ver = cached_repos[i].version; - } + if (strcmp(cached_repos[i].source_key, new_arg) != 0) continue; + pkg.ver = cached_repos[i].version; } - - char *new_arg = strdup(arg); - if (!new_arg) exit(EXIT_FAILURE); - char *argver = strchr(new_arg, '@'); - char *argtrg = strchr(new_arg, ','); - if (argver) { - pkg.ver = argver + 1; - *argver = '\0'; + if (verptr) { + pkg.ver = verptr + 1; + *verptr = '\0'; } - if (argtrg) { - pkg.target = argtrg + 1; - *argtrg = '\0'; + if (trgptr) { + pkg.target = trgptr + 1; + *trgptr = '\0'; } else { pkg.target = "default"; } 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) { + if (strcmp(arg, cached_repos[i].source_key) == 0) { is_in_repos = true; break; } } - cache_install_directories(); - bool is_installed_locally = false; - char dest_dir[MAX_PATH_LEN]; - if (new_arg[0] == '.') { - char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); - 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"), new_arg); - } - if (is_directory(dest_dir)) is_installed_locally = true; - if (strncmp(new_arg, "http", 4) == 0) { pkg.url = strdup(new_arg); pkg.name = name_from_url(new_arg); } else if (strcmp(new_arg, ".") == 0) { pkg.url = ""; - char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); pkg.name = name_from_url(cwd); pkg.is_local = true; } else if (is_in_repos) { @@ -73,7 +107,7 @@ Pkg create_pkg(const char *arg) { break; } } - pkg.name = strdup(new_arg); + pkg.name = new_arg; } else if (is_installed_locally) { pkg.url = ""; pkg.name = name_from_url(dest_dir); @@ -82,20 +116,7 @@ Pkg create_pkg(const char *arg) { printf("%s '%s' is not a valid package\n", print_error, new_arg); exit(EXIT_FAILURE); } - - if (strlen(pkg.name) > 4 && - strncmp(pkg.name + strlen(pkg.name) - 4, ".git", 4) == 0) - pkg.name[strlen(pkg.name) - 4] = '\0'; - - 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); - } - snprintf(pkg.src, MAX_PATH_LEN, "%s", src_dir); - + rmdotgit(pkg); + snprintf(pkg.src, MAX_PATH_LEN, "%s", get_pkgsrc(pkg)); return pkg; }
\ No newline at end of file diff --git a/src/fetch_git.c b/src/fetch_git.c index 2d9e9a2..d7d1741 100644 --- a/src/fetch_git.c +++ b/src/fetch_git.c @@ -1,6 +1,5 @@ #include <fcntl.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> #include <sys/wait.h> #include <unistd.h> @@ -41,7 +40,7 @@ int fetch_git(Pkg pkg) { waitpid(pid, &status, 0); int result = WIFEXITED(status) ? WEXITSTATUS(status) : -1; if (result != 0) { - printf("%s git clone failed\n", print_warning); + printf("%s git clone failed: %d\n", print_warning, result); } return result; diff --git a/src/fetch_pwd.c b/src/fetch_pwd.c index 84cd54d..7e2f2e4 100644 --- a/src/fetch_pwd.c +++ b/src/fetch_pwd.c @@ -1,6 +1,4 @@ #include <stdio.h> -#include <stdlib.h> -#include <string.h> #include <sys/stat.h> #include <fcntl.h> #include <ftw.h> @@ -12,41 +10,41 @@ 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); + (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; } - return 0; + } + + close(src_fd); + close(dst_fd); + } + return 0; } void fetch_pwd(Pkg pkg) { - pwd_dst = pkg.src; - nftw(".", copy_entry, 64, FTW_PHYS); + 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 index 8ad1ff5..55ce135 100644 --- a/src/fetch_src.c +++ b/src/fetch_src.c @@ -1,55 +1,47 @@ +#include <ftw.h> #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 "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)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 - ); - 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); + 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); + 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 @@ -1,39 +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(" %sgizver_%s%s\n", magenta, version, color_reset); + 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("%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...... %sd%s, %sdeclare %s# install all packages\n", color_reset, green, color_reset, yellow, gray); - printf("%s...... %sf%s, %sfind %s[pkgs] %s# find a package from your repos\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 all 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("%s %sa%s, %sadd %s[url] %s# add a repo\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 %sd%s, %sdeclare %s# install all packages\n", color_reset, green, color_reset, yellow, gray); + printf("%s %ss%s, %ssearch %s[pkgs] %s# find a package from your repos\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 all 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-q%s, %s--quiet %s# run without logging to terminal\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("%s %s-h%s, %s--help %s# display this help message\n", color_reset, green, color_reset, yellow, gray); + 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); + 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/install_pkg.c b/src/install_pkg.c index e92eab2..32a84cd 100644 --- a/src/install_pkg.c +++ b/src/install_pkg.c @@ -27,10 +27,11 @@ void install_pkg(Pkg pkg) { ); } } + char cwd[MAX_PATH_LEN]; + getcwd(cwd, MAX_PATH_LEN); + if (strcmp(pkg.src, cwd) != 0) chdir(pkg.src); if (pkg.is_local) { - char cwd[MAX_PATH_LEN]; - getcwd(cwd, MAX_PATH_LEN); cpdir(cwd, pkg.src); } else { printf( diff --git a/src/is_updated.c b/src/is_updated.c index 5fce96b..68e6d2e 100644 --- a/src/is_updated.c +++ b/src/is_updated.c @@ -6,15 +6,14 @@ #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; - } + 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; + 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/lua_build.c b/src/lua_build.c index 1e90d3a..269482d 100644 --- a/src/lua_build.c +++ b/src/lua_build.c @@ -6,35 +6,35 @@ #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 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 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 - ); + 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; + return false; }
\ No newline at end of file diff --git a/src/lua_state.c b/src/lua_state.c index 623c87b..4d3c4b5 100644 --- a/src/lua_state.c +++ b/src/lua_state.c @@ -40,10 +40,8 @@ void init_lua_state() { "%s cannot run configuration script: %s\n", print_error, lua_tostring(L, -1) ); - printf( - "%s to generate a configration file, head into the root directory of the pkgit source and run `make defconfig`\n", - print_pkgit - ); + printf("%s to generate a configration 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)) { @@ -130,7 +128,9 @@ void install_dependencies(lua_State *L) { } } -bool target_loop_build(lua_State *L, const char* lua_file, const char *target) { +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( @@ -1,5 +1,4 @@ #include <stdio.h> -#include <stdlib.h> #include "cla_parse.h" #include "lua_state.h" @@ -7,10 +6,11 @@ #include "vars.h" int main(int argc, char *argv[]) { - init_vars(); - setup_pkgit(); - cache_repos(); - cla_parse(argc, argv); - free_lua_state(); - return 0; + init_vars(); + init_lua_state(); + setup_pkgit(); + cache_repos(); + cla_parse(argc, argv); + free_lua_state(); + return 0; }
\ No newline at end of file diff --git a/src/resolve_deps.c b/src/resolve_deps.c index 65e5ac9..ed728d4 100644 --- a/src/resolve_deps.c +++ b/src/resolve_deps.c @@ -4,9 +4,9 @@ #include "resolve_deps.h" void resolve_deps(void) { - const char *frame_top = " (C_C)"; - const char *frame1_bot = "_/ \\-"; - const char *frame2_bot = "-/ \\_"; + 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"); diff --git a/src/find.c b/src/search.c index 4387d3c..72fe350 100644 --- a/src/find.c +++ b/src/search.c @@ -1,12 +1,10 @@ #include <stdio.h> #include <string.h> -#include "list_pkgs.h" #include "lua_state.h" +#include "search.h" -void find(const char* arg) { - init_lua_state(); - cache_repos(); +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); diff --git a/src/set_install_directories.c b/src/set_install_directories.c index 2df6aaf..95bd505 100644 --- a/src/set_install_directories.c +++ b/src/set_install_directories.c @@ -2,68 +2,66 @@ #include <stdlib.h> #include <string.h> -#include "set_install_directories.h" #include "lua_state.h" +#include "set_install_directories.h" #include "vars.h" void set_install_directories() { - init_lua_state(); - lua_State *L = get_lua_state(); - - lua_getglobal(L, "install_directories"); + 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 + ); + } - 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); + 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; + 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; - } + 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; - } + 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++; + 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; - } +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; + } + return NULL; }
\ No newline at end of file diff --git a/src/setup_dirs.c b/src/setup_dirs.c index 413f02d..ee2196d 100644 --- a/src/setup_dirs.c +++ b/src/setup_dirs.c @@ -1,5 +1,4 @@ #include <stdio.h> -#include <stdlib.h> #include <string.h> #include <sys/stat.h> diff --git a/src/setup_pkgit.c b/src/setup_pkgit.c index b58149b..fdcdb56 100644 --- a/src/setup_pkgit.c +++ b/src/setup_pkgit.c @@ -1,11 +1,8 @@ #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(); diff --git a/src/update_all.c b/src/update_all.c index 521b1d1..16b8636 100644 --- a/src/update_all.c +++ b/src/update_all.c @@ -7,11 +7,9 @@ #include <unistd.h> #include "update_all.h" -#include "files.h" #include "create_pkg.h" #include "update_pkg.h" #include "lua_state.h" -#include "set_install_directories.h" #include "vars.h" void update_all() { diff --git a/src/update_pkg.c b/src/update_pkg.c index 7511b54..c926d4b 100644 --- a/src/update_pkg.c +++ b/src/update_pkg.c @@ -1,5 +1,4 @@ #include <stdio.h> -#include <string.h> #include "update_pkg.h" #include "is_updated.h" @@ -1,10 +1,10 @@ +#include <errno.h> +#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdbool.h> -#include <unistd.h> #include <sys/stat.h> -#include <errno.h> +#include <unistd.h> #include "vars.h" @@ -35,21 +35,22 @@ 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; +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; - } +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 ""; + } + return ""; } -const char *version = "1.0.0"; +const char *version = "1.0.0_INDEV"; const char *red = "\e[0;31m"; const char *green = "\e[0;32m"; @@ -90,100 +91,100 @@ 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) { + 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; + return 0; } bool file_exists(const char *path) { - struct stat buffer; - return (stat(path, &buffer) == 0); + 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); + 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(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; + 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; } |
