diff options
Diffstat (limited to 'src/cli.c')
| -rw-r--r-- | src/cli.c | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/src/cli.c b/src/cli.c new file mode 100644 index 0000000..c4499ff --- /dev/null +++ b/src/cli.c @@ -0,0 +1,246 @@ +/* + + pkgit - package it! + + Copyright (C) 2026 dacctal + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ + +#include <stdio.h> +#include <string.h> + +#include "cli.h" +#include "common.h" +#include "help.h" +#include "log.h" +#include "state.h" +#include "str.h" + +#define COMMAND(large, small) \ + (!strcmp(argv[i], large) || !strcmp(argv[i], small)) + +#define NOT_ENOUGH_ARGS(arg, next) \ + log_error("Not enough arguments! Try: pkgit %s [%s]", (arg), (next)) + +void cmd_add(char **argv, int i) { + if (argv[i + 1]) { + str arg = mstr(argv[i + 1]); + if (str_is_valid(&arg)) + str_free(&arg); + } else { + NOT_ENOUGH_ARGS(argv[i], "url"); + } +} + +void cmd_build(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') + continue; + str arg = mstr(argv[j]); + if (str_is_valid(&arg)) + str_free(&arg); + } + } else { + str arg = mstr("."); + if (str_is_valid(&arg)) + str_free(&arg); + } +} + +void cmd_install(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') + continue; + str arg = mstr(argv[j]); + if (str_is_valid(&arg)) + str_free(&arg); + } + } else { + NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } +} + +void cmd_update(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') + continue; + str arg = mstr(argv[j]); + if (str_is_valid(&arg)) + str_free(&arg); + } + } else { + } +} + +void cmd_remove(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') + continue; + str arg = mstr(argv[j]); + if (str_is_valid(&arg)) + str_free(&arg); + } + } else { + NOT_ENOUGH_ARGS(argv[i], "url/pkg"); + } +} + +void cmd_search(int argc, char **argv, int i) { + if (argv[i + 1]) { + for (int j = i + 1; j < argc; j++) { + if (argv[j][0] == '-') + continue; + } + } else { + } +} + +void flags_mod(char **argv, int i) { + for (size_t j = 1; j < strlen(argv[i]); j++) { + switch (argv[i][j]) { + case 'q': + flags.quiet = true; + break; + case 'f': + flags.force = true; + break; + default: + break; + } + } +} + +cli_result_t flags_cmd(int argc, char **argv, int i) { + for (size_t j = 1; j < strlen(argv[i]); j++) { + switch (argv[i][j]) { + case 'a': + cmd_add(argv, i); + break; + case 'b': + cmd_build(argc, argv, i); + break; + case 'c': + // check(); + break; + case 'd': + // declare(); + break; + case 'i': + cmd_install(argc, argv, i); + break; + case 'r': + cmd_remove(argc, argv, i); + break; + case 'u': + cmd_update(argc, argv, i); + break; + case 'l': + // list_installed(); + break; + case 's': + cmd_search(argc, argv, i); + break; + case 'v': + printf("%s\n", VERSION); + break; + case 'h': + print_help(); + return CLI_HELP_CALLED; + break; + default: + break; + } + } + return CLI_SUCCESS; +} + +cli_result_t parse_flags(int argc, char **argv) { + for (int i = 1; i < argc; i++) { + if (argv[i][0] != '-') + continue; + if (argv[i][1] == '-') { + if (COMMAND("--quiet", "-q")) + flags.quiet = true; + if (COMMAND("--force", "-f")) + flags.force = true; + } else { + flags_mod(argv, i); + cli_result_t status = flags_cmd(argc, argv, i); + if (status != CLI_SUCCESS) + return status; + } + } + return CLI_SUCCESS; +} + +cli_result_t parse_cmds(int argc, char **argv) { + for (int i = 1; i < argc; i++) { + if (COMMAND("--add", "a")) { + cmd_add(argv, i); + } + if (COMMAND("--build", "b")) { + cmd_build(argc, argv, i); + } + if (COMMAND("--install", "i")) { + cmd_install(argc, argv, i); + } + if (COMMAND("--remove", "r")) { + cmd_remove(argc, argv, i); + } + if (COMMAND("--update", "u")) { + cmd_update(argc, argv, i); + } + if (COMMAND("--declare", "d")) { + // declare(); + } + if (COMMAND("--list", "l")) { + // list_installed(); + } + if (COMMAND("--search", "s")) { + cmd_search(argc, argv, i); + } + if (COMMAND("--version", "v")) { + printf(VERSION "\n"); + } + if (COMMAND("--help", "h")) { + print_help(); + return CLI_HELP_CALLED; + } + if (COMMAND("--check", "c")) { + // check(); + } + } + return CLI_SUCCESS; +} + +cli_result_t cli(int argc, char **argv) { + flags.force = false; + flags.quiet = false; + if (argc == 1) { + print_help(); + return CLI_HELP_CALLED; + } + if (!parse_flags(argc, argv)) + return CLI_FAIL; + + if (!parse_cmds(argc, argv)) + return CLI_FAIL; + + return CLI_SUCCESS; +} |
