update: split arguments with no values

This commit is contained in:
ngn 2024-07-19 22:33:44 +03:00
parent cc6125ac4c
commit afb4122f97
12 changed files with 248 additions and 99 deletions

View File

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-19 00:28+0300\n"
"POT-Creation-Date: 2024-07-19 22:32+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@ -72,241 +72,265 @@ msgstr ""
msgid "Please answer with y/n"
msgstr ""
#: src/main.c:72
#: src/main.c:74
msgid "Failed to access the root directory, are you running as root?"
msgstr ""
#: src/main.c:74
#: src/main.c:76
#, fuzzy, c-format
msgid "Bad root directory (%s): %s"
msgstr "Havuz dizini kullanmak başarısız oldu: %s"
#: src/main.c:82
#: src/main.c:84
#, c-format
msgid "Bad datadir (%s): %s"
msgstr ""
#: src/main.c:87
#: src/main.c:89
#, c-format
msgid "Bad tmpdir (%s): %s"
msgstr ""
#: src/main.c:94
#: src/main.c:96
#, fuzzy
msgid "Failed to add pool "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/main.c:107
#: src/main.c:109
msgid "Command not found: "
msgstr ""
#: src/main.c:109
#: src/main.c:111
#, c-format
msgid "MatterLinux package manager (version %s)"
msgstr ""
#: src/main.c:110
#: src/main.c:112
msgid "Usage: "
msgstr ""
#: src/main.c:112
#: src/main.c:114
msgid "Here is a list of available commands:"
msgstr ""
#: src/main.c:118
#: src/main.c:120
msgid "To list different options, use commands with "
msgstr ""
#: src/main.c:119
#: src/main.c:121
msgid "Here is a list of available global options:"
msgstr ""
#: src/main.c:122 src/main.c:123 src/cmd/info.c:14 src/cmd/info.c:15
#: src/cmd/info.c:16 src/cmd/install.c:22 src/cmd/install.c:23
#: src/cmd/list.c:10 src/cmd/list.c:11 src/cmd/remove.c:17 src/cmd/remove.c:18
#: src/cmd/update.c:23
#: src/main.c:124 src/main.c:125 src/cmd/info.c:19 src/cmd/info.c:20
#: src/cmd/info.c:21 src/cmd/install.c:25 src/cmd/install.c:26
#: src/cmd/list.c:13 src/cmd/list.c:14 src/cmd/remove.c:20 src/cmd/remove.c:21
#: src/cmd/update.c:25
#, c-format
msgid " "
msgstr ""
#: src/main.c:125
#: src/main.c:127
msgid ""
"Licensed under GPLv3, see https://www.gnu.org/licenses/ for more information"
msgstr ""
#: src/cmd/info.c:13 src/cmd/list.c:9
#: src/util.c:83
msgid "No available editor to open "
msgstr ""
#: src/util.c:90
#, fuzzy, c-format
msgid "Failed to run the editor: %s"
msgstr "Sunucuyu başlatmak başarısız oldu: %s"
#: src/util.c:95
#, fuzzy, c-format
msgid "Failed to open the editor: %s"
msgstr "Sunucuyu başlatmak başarısız oldu: %s"
#: src/cmd/info.c:18 src/cmd/list.c:12
msgid "Listing options for the list command:"
msgstr ""
#: src/cmd/info.c:36
#: src/cmd/info.c:41
msgid "Please specify only a single package name"
msgstr ""
#: src/cmd/info.c:42
#: src/cmd/info.c:47
#, fuzzy
msgid "Please specify a single package"
msgstr "Lütfen konfigürasyonda en az bir havuz belirtin"
#: src/cmd/info.c:48 src/cmd/info.c:54 src/cmd/install.c:56 src/cmd/remove.c:42
#: src/cmd/info.c:53 src/cmd/info.c:59 src/cmd/install.c:59 src/cmd/remove.c:45
msgid "Package "
msgstr ""
#: src/cmd/info.c:59
#: src/cmd/info.c:64
#, fuzzy
msgid "Failed to get the changes file for "
msgstr "%s: paket listesini yüklemek başarısız oldu (%s)"
#: src/cmd/info.c:91
#: src/cmd/info.c:70
#, fuzzy
msgid "Failed to access the changes file, is the package installed?"
msgstr "%s: paket listesini yüklemek başarısız oldu (%s)"
#: src/cmd/info.c:77
#, fuzzy
msgid "Failed to get the depends list for "
msgstr "%s: paket listesini yüklemek başarısız oldu (%s)"
#: src/cmd/info.c:104
#, c-format
msgid "Name "
msgstr ""
#: src/cmd/info.c:92
#: src/cmd/info.c:105
#, c-format
msgid "Version "
msgstr ""
#: src/cmd/info.c:93
#: src/cmd/info.c:106
#, c-format
msgid "Desc "
msgstr ""
#: src/cmd/info.c:94
#: src/cmd/info.c:107
#, c-format
msgid "Size "
msgstr ""
#: src/cmd/info.c:97
#: src/cmd/info.c:110
#, c-format
msgid "Depends "
msgstr ""
#: src/cmd/info.c:100
#: src/cmd/info.c:113
#, c-format
msgid "Changes "
msgstr ""
#: src/cmd/info.c:104 src/cmd/info.c:105
#: src/cmd/info.c:117 src/cmd/info.c:118
#, c-format
msgid "Installed "
msgstr ""
#: src/cmd/info.c:107 src/cmd/info.c:108
#: src/cmd/info.c:120 src/cmd/info.c:121
#, c-format
msgid "Up-to-date "
msgstr ""
#: src/cmd/install.c:21
#: src/cmd/install.c:24
msgid "Listing options for the install command:"
msgstr ""
#: src/cmd/install.c:28 src/cmd/update.c:28
#: src/cmd/install.c:31 src/cmd/update.c:30
msgid "There are no avaliable pools, have you synced yet?"
msgstr ""
#: src/cmd/install.c:48 src/cmd/remove.c:44
#: src/cmd/install.c:51 src/cmd/remove.c:47
#, fuzzy
msgid "Failed to find "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/install.c:62
#: src/cmd/install.c:65
#, fuzzy
msgid "Failed to resolve "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/install.c:68
#: src/cmd/install.c:71
msgid "Nothing to do (no packages to install)"
msgstr ""
#: src/cmd/install.c:77 src/cmd/remove.c:64 src/cmd/update.c:48
#: src/cmd/install.c:80 src/cmd/remove.c:67 src/cmd/update.c:50
#, c-format
msgid "Following %d package will be "
msgstr ""
#: src/cmd/install.c:79 src/cmd/remove.c:66 src/cmd/update.c:50
#: src/cmd/install.c:82 src/cmd/remove.c:69 src/cmd/update.c:52
#, c-format
msgid "Following %d packages will be "
msgstr ""
#: src/cmd/install.c:85 src/cmd/remove.c:73
#: src/cmd/install.c:88 src/cmd/remove.c:76
msgid "Total of "
msgstr ""
#: src/cmd/install.c:86 src/cmd/remove.c:74 src/cmd/update.c:56
#: src/cmd/install.c:89 src/cmd/remove.c:77 src/cmd/update.c:58
msgid "Continue?"
msgstr ""
#: src/cmd/install.c:87 src/cmd/remove.c:75 src/cmd/update.c:57
#: src/cmd/install.c:90 src/cmd/remove.c:78 src/cmd/update.c:59
msgid "Operation cancelled"
msgstr ""
#: src/cmd/install.c:93 src/cmd/update.c:67
#: src/cmd/install.c:96 src/cmd/update.c:69
#, c-format
msgid "(%d/%d) Downloading "
msgstr ""
#: src/cmd/install.c:99 src/cmd/update.c:72
#: src/cmd/install.c:102 src/cmd/update.c:74
#, fuzzy
msgid "Failed to download "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/install.c:108 src/cmd/update.c:85
#: src/cmd/install.c:111 src/cmd/update.c:87
#, c-format
msgid "(%d/%d) Installing "
msgstr ""
#: src/cmd/install.c:114 src/cmd/update.c:90
#: src/cmd/install.c:117 src/cmd/update.c:92
#, fuzzy
msgid "Failed to install "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/install.c:120
#: src/cmd/install.c:123
#, c-format
msgid "Installed %d package"
msgstr ""
#: src/cmd/install.c:120
#: src/cmd/install.c:123
#, c-format
msgid "Installed all of the %d packages"
msgstr ""
#: src/cmd/list.c:24
#: src/cmd/list.c:27
msgid "There no installed packages"
msgstr ""
#: src/cmd/list.c:29
#: src/cmd/list.c:32
msgid "Listing "
msgstr ""
#: src/cmd/remove.c:16
#: src/cmd/remove.c:19
msgid "Listing options for the remove command:"
msgstr ""
#: src/cmd/remove.c:49
#: src/cmd/remove.c:52
#, fuzzy
msgid "Cannot remove "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/remove.c:57
#: src/cmd/remove.c:60
msgid "Nothing to do (no packages to remove)"
msgstr ""
#: src/cmd/remove.c:82 src/cmd/update.c:76
#: src/cmd/remove.c:85 src/cmd/update.c:78
#, c-format
msgid "(%d/%d) Removing "
msgstr ""
#: src/cmd/remove.c:88 src/cmd/update.c:81
#: src/cmd/remove.c:91 src/cmd/update.c:83
#, fuzzy
msgid "Failed to remove "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/remove.c:94
#: src/cmd/remove.c:97
#, c-format
msgid "Removed %d package"
msgstr ""
#: src/cmd/remove.c:94
#: src/cmd/remove.c:97
#, c-format
msgid "Removed all of the %d packages"
msgstr ""
@ -335,20 +359,20 @@ msgstr "Lütfen konfigürasyonda en az bir havuz belirtin"
msgid "Failed to sync pools: %s"
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#: src/cmd/update.c:22
#: src/cmd/update.c:24
msgid "Listing options for the update command:"
msgstr ""
#: src/cmd/update.c:38
#: src/cmd/update.c:40
#, fuzzy, c-format
msgid "Failed to get the package update list: %s"
msgstr "%s: paket listesini yüklemek başarısız oldu (%s)"
#: src/cmd/update.c:43
#: src/cmd/update.c:45
msgid "All packages are up-to-date"
msgstr ""
#: src/cmd/update.c:63
#: src/cmd/update.c:65
#, fuzzy
msgid "Failed to update "
msgstr "Havuzları sekronize etmek başarısız oldu: %s"
@ -393,7 +417,3 @@ msgstr "Havuzları sekronize etmek başarısız oldu: %s"
#, c-format
#~ msgid "Serving %lu pools on %s"
#~ msgstr "%lu tane havuz %s adresinde sunuluyor"
#, c-format
#~ msgid "Failed to start the server: %s"
#~ msgstr "Sunucuyu başlatmak başarısız oldu: %s"

View File

@ -7,7 +7,7 @@
#include "args.h"
#include "util.h"
void args_add(args_t *args, char *name) {
void args_add_name(args_t *args, char *name) {
if (NULL == args->list)
args->list = malloc(sizeof(arg_t) * (args->count + 1));
else
@ -18,11 +18,6 @@ void args_add(args_t *args, char *name) {
}
void args_add_value(args_t *args, char *value) {
if (NULL != args->list && args->list[args->count - 1].value == NULL) {
args->list[args->count - 1].value = value;
return;
}
if (NULL == args->list)
args->list = malloc(sizeof(arg_t) * (args->count + 1));
else
@ -32,6 +27,34 @@ void args_add_value(args_t *args, char *value) {
args->list[args->count++].value = value;
}
void args_set_value(args_t *args, char *value) {
if (NULL != args->list && args->list[args->count - 1].value == NULL) {
args->list[args->count - 1].value = value;
return;
}
args_add_value(args, value);
}
bool args_split(args_t *args, char *name) {
char *value = NULL;
for (int i = 0; i < args->count; i++) {
if (NULL == args->list[i].name || !eq(args->list[i].name, name))
continue;
value = args->list[i].value;
args->list[i].value = NULL;
break;
}
if (NULL == value)
return false;
args_add_value(args, value);
return true;
}
args_t *args_parse(int argc, char *argv[]) {
args_t *args = malloc(sizeof(args_t));
bzero(args, sizeof(args_t));
@ -41,9 +64,9 @@ args_t *args_parse(int argc, char *argv[]) {
for (int i = 1; i < argc; i++) {
if (startswith(argv[i], "--"))
args_add(args, argv[i] + 2);
args_add_name(args, argv[i] + 2);
else
args_add_value(args, argv[i]);
args_set_value(args, argv[i]);
}
return args;

View File

@ -15,4 +15,5 @@ args_t *args_parse(int argc, char *argv[]);
char *args_get_string(args_t *args, char *name);
bool args_get_bool(args_t *args, char *name);
int args_get_int(args_t *args, char *name);
bool args_split(args_t *args, char *name);
void args_free(args_t *args);

View File

@ -3,12 +3,17 @@
#include <libmp/error.h>
#include <libmp/package.h>
#include <stdio.h>
#include <stdlib.h>
#include "../util.h"
#include "../cmd.h"
#include "../log.h"
bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args){
args_split(args, "grep");
args_split(args, "changes");
args_split(args, "database");
if(args_get_bool(args, "help")){
info(_("Listing options for the list command:"));
printf(_(" "FG_BOLD"--grep"FG_RESET":\tmakes the output \"grepable\"\n"));
@ -20,7 +25,7 @@ bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args){
if(!args_get_bool(args, "database"))
lm_ctx_sync(ctx, false, NULL, NULL);
char *name = NULL, *changes = NULL;
char *name = NULL, *depends = NULL, *changes = NULL;
lm_pkg_t pkg, *pkgp = &pkg;
for(size_t i = 0; i < args->count; i++){
@ -57,13 +62,21 @@ bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args){
if((changes = lm_ctx_database_changes(ctx, pkgp)) == NULL && LM_ERR_DbChangesNotExists != lm_error()){
error(_("Failed to get the changes file for "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), pkgp->name, pkgp->version, lm_strerror());
return false;
goto end;
}
// TODO: open the changes file with the preferred editor if --changes is set
if(args_get_bool(args, "changes")){
if(NULL == changes)
error(_("Failed to access the changes file, is the package installed?"));
else
editor_open(changes);
goto end;
}
char depends[lm_package_depend_strlen(pkgp)+1];
lm_package_depend_tostr(pkgp, depends);
if((depends = lm_package_depend_tostr(pkgp, NULL)) == NULL){
error(_("Failed to get the depends list for "FG_BOLD"%s"FG_RESET"_"FG_BOLD FG_GREEN"%s"FG_RESET": %s"), pkgp->name, pkgp->version, lm_strerror());
goto end;
}
if(args_get_bool(args, "grep")){
printf("NAME:%s\n", pkgp->name);
@ -111,5 +124,9 @@ bool cmd_info(lm_ctx_t *ctx, config_t *config, args_t *args){
end:
if(args_get_bool(args, "database"))
lm_package_free(pkgp);
free(changes);
free(depends);
return true;
}

View File

@ -17,6 +17,9 @@ bool cmd_download_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, bool is_archive, size_t
}
bool cmd_install(lm_ctx_t *ctx, config_t *config, args_t *args){
args_split(args, "continue");
args_split(args, "yes");
if(args_get_bool(args, "help")){
info(_("Listing options for the install command:"));
printf(_(" "FG_BOLD"--continue"FG_RESET":\tcontinue even if package is not found\n"));

View File

@ -5,6 +5,9 @@
#include "../log.h"
bool cmd_list(lm_ctx_t *ctx, config_t *config, args_t *args){
args_split(args, "grep");
args_split(args, "desc");
if(args_get_bool(args, "help")){
info(_("Listing options for the list command:"));
printf(_(" "FG_BOLD"--grep"FG_RESET":\tmakes the output \"grepable\"\n"));

View File

@ -12,6 +12,9 @@ bool cmd_remove_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, char *file, size_t curren
}
bool cmd_remove(lm_ctx_t *ctx, config_t *config, args_t *args){
args_split(args, "break");
args_split(args, "yes");
if(args_get_bool(args, "help")){
info(_("Listing options for the remove command:"));
printf(_(" "FG_BOLD"--break"FG_RESET":\tbreak other package depends\n"));

View File

@ -18,6 +18,8 @@ bool cmd_update_download_callback(lm_ctx_t *ctx, lm_pkg_t *pkg, bool is_archive,
}
bool cmd_update(lm_ctx_t *ctx, config_t *config, args_t *args){
args_split(args, "yes");
if(args_get_bool(args, "help")){
info(_("Listing options for the update command:"));
printf(_(" "FG_BOLD"--yes"FG_RESET":\tdon't ask for confirmation\n"));

View File

@ -34,8 +34,9 @@
#include "config.h"
#include "intl.h"
#include "log.h"
#include "util.h"
int main(int argc, char *argv[]) {
int main(int argc, char *argv[], char *envp[]) {
cmd_t commands[] = {
{.name = "info", .desc = "show information about a single package", .func = cmd_info },
{.name = "list", .desc = "list all the installed packages", .func = cmd_list },
@ -52,6 +53,7 @@ int main(int argc, char *argv[]) {
config_t config;
lm_ctx_t ctx;
set_env(envp);
lm_ctx_init(&ctx);
args = args_parse(argc, argv);

View File

@ -1,9 +1,21 @@
#include <errno.h>
#include <libmp/util.h>
#include <spawn.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <wait.h>
#include "log.h"
#include "util.h"
char **env = NULL;
void set_env(char **envp) {
env = envp;
}
bool startswith(char *str, char *sub) {
size_t strl = strlen(str);
size_t subl = strlen(sub);
@ -28,3 +40,63 @@ void size_to_human(char *buf, long size) {
sprintf(buf, "%.02lf%s", cursize, suffix[i]);
}
char *exists_in_path(char *file) {
if (NULL == file)
return NULL;
char *path = NULL, *part = NULL, *save = NULL;
size_t filesz = strlen(file);
if ((path = getenv("PATH")) == NULL)
path = "/bin";
if ((part = strtok_r(path, ":", &save)) == NULL)
return false;
do {
char fp[strlen(part) + filesz + 1];
join(fp, part, file);
if (exists(fp))
return strdup(fp);
} while ((part = strtok_r(NULL, ":", &save)) != NULL);
return NULL;
}
void editor_open(char *file) {
if (NULL == file)
return;
char *editors[] = {getenv("EDITOR"), "nano", "vi", "vim"};
int status = 0, i = 0;
char *editor = NULL;
pid_t pid = 0;
for (; i < sizeof(editors) / sizeof(char *); i++) {
if ((editor = exists_in_path(editors[i])) != NULL)
break;
}
if (NULL == editor) {
error(_("No available editor to open " FG_BOLD "%s" FG_RESET), file);
return;
}
char *argv[] = {editor, file, NULL};
if (posix_spawnp(&pid, argv[0], NULL, NULL, argv, env) != 0) {
error(_("Failed to run the editor: %s"), strerror(errno));
goto end;
}
if (waitpid(pid, &status, 0) < 0) {
error(_("Failed to open the editor: %s"), strerror(errno));
goto end;
}
end:
free(editor);
return;
}

View File

@ -3,5 +3,8 @@
#define LONGSTR_MAX 30
bool startswith(char *str, char *sub);
void size_to_human(char *buf, long size);
void set_env(char **envp);
bool startswith(char *str, char *sub);
void size_to_human(char *buf, long size);
char *exists_in_path(char *file);
void editor_open(char *file);