diff --git a/NEWS b/NEWS index b66a2eca..e39dd986 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,15 @@ -xbps-0.6.3 (?) +xbps-0.7.0 (?) + + * xbps-{bin,repo}(8): indent uniformly all lines while listing packages + in xbps-bin or while searching for packages in xbps-repo. + + * xbps-repo(8): while searching for packages also try to find strings + in the short description object. + + * xbps-{bin,repo}(8): added -d arg to enable debugging output to stderr. + + * Cleaned up the API. Still not finished and there's a long way to have this + finished and ready for public consumption. * xbps-dgraph: new utility to generate graphviz' dot(1) graphs for package metadata properties, such as dependencies, reverse dependencies, etc. diff --git a/bin/xbps-bin/main.c b/bin/xbps-bin/main.c index 10dc33fa..ef605f71 100644 --- a/bin/xbps-bin/main.c +++ b/bin/xbps-bin/main.c @@ -36,6 +36,11 @@ #include "defs.h" #include "../xbps-repo/defs.h" +struct list_pkgver_cb { + pkg_state_t state; + size_t pkgver_len; +}; + static void usage(void) { @@ -76,8 +81,11 @@ usage(void) static int list_pkgs_in_dict(prop_object_t obj, void *arg, bool *loop_done) { + struct list_pkgver_cb *lpc = arg; const char *pkgver, *short_desc; - pkg_state_t curstate, *wantstate = (pkg_state_t *)arg; + char *tmp = NULL; + pkg_state_t curstate, *wantstate = &lpc->state; + size_t i = 0; (void)loop_done; @@ -96,7 +104,18 @@ list_pkgs_in_dict(prop_object_t obj, void *arg, bool *loop_done) if (!pkgver && !short_desc) return EINVAL; - printf("%s\t%s\n", pkgver, short_desc); + tmp = malloc(lpc->pkgver_len + 1); + if (tmp == NULL) + return errno; + + memcpy(tmp, pkgver, lpc->pkgver_len); + for (i = strlen(tmp); i < lpc->pkgver_len; i++) + tmp[i] = ' '; + + tmp[lpc->pkgver_len + 1] = '\0'; + printf("%s %s\n", tmp, short_desc); + free(tmp); + return 0; } @@ -130,7 +149,7 @@ int main(int argc, char **argv) { prop_dictionary_t dict; - pkg_state_t pkgstate = 0; + struct list_pkgver_cb lpc; struct sigaction sa; int i = 0, c, flags = 0, rv = 0; bool yes, purge, with_debug; @@ -223,11 +242,11 @@ main(int argc, char **argv) if (argv[1]) { if (strcmp(argv[1], "installed") == 0) - pkgstate = XBPS_PKG_STATE_INSTALLED; + lpc.state = XBPS_PKG_STATE_INSTALLED; else if (strcmp(argv[1], "unpacked") == 0) - pkgstate = XBPS_PKG_STATE_UNPACKED; + lpc.state = XBPS_PKG_STATE_UNPACKED; else if (strcmp(argv[1], "config-files") == 0) - pkgstate = XBPS_PKG_STATE_CONFIG_FILES; + lpc.state = XBPS_PKG_STATE_CONFIG_FILES; else { fprintf(stderr, "E: invalid state `%s'. Accepted values: " @@ -238,8 +257,12 @@ main(int argc, char **argv) } } + /* + * Find the longest pkgver string to pretty print the output. + */ + lpc.pkgver_len = find_longest_pkgver(dict); rv = xbps_callback_array_iter_in_dict(dict, "packages", - list_pkgs_in_dict, &pkgstate); + list_pkgs_in_dict, &lpc); } else if (strcasecmp(argv[0], "install") == 0) { /* Installs a binary package and required deps. */ diff --git a/bin/xbps-repo/defs.h b/bin/xbps-repo/defs.h index 330ebcd0..045b5c6c 100644 --- a/bin/xbps-repo/defs.h +++ b/bin/xbps-repo/defs.h @@ -41,5 +41,11 @@ void show_pkg_info_only_repo(prop_dictionary_t); int show_pkg_namedesc(prop_object_t, void *, bool *); int list_strings_in_array(prop_object_t, void *, bool *); int list_strings_sep_in_array(prop_object_t, void *, bool *); +size_t find_longest_pkgver(prop_dictionary_t); + +struct repo_search_data { + char *pattern; + size_t pkgver_len; +}; #endif /* !_XBPS_REPO_DEFS_H_ */ diff --git a/bin/xbps-repo/main.c b/bin/xbps-repo/main.c index 17963f78..7fe6e224 100644 --- a/bin/xbps-repo/main.c +++ b/bin/xbps-repo/main.c @@ -74,18 +74,23 @@ repo_list_uri_cb(struct repository_pool_index *rpi, void *arg, bool *done) (void)done; printf("%s\n", rpi->rpi_uri); + return 0; } static int repo_search_pkgs_cb(struct repository_pool_index *rpi, void *arg, bool *done) { - char *pattern = arg; + struct repo_search_data rsd; (void)done; + rsd.pattern = arg; + rsd.pkgver_len = find_longest_pkgver(rpi->rpi_repod); + printf("From %s repository ...\n", rpi->rpi_uri); (void)xbps_callback_array_iter_in_dict(rpi->rpi_repod, - "packages", show_pkg_namedesc, pattern); + "packages", show_pkg_namedesc, &rsd); + return 0; } diff --git a/bin/xbps-repo/util.c b/bin/xbps-repo/util.c index 30d61dbb..8fde5ba1 100644 --- a/bin/xbps-repo/util.c +++ b/bin/xbps-repo/util.c @@ -178,29 +178,64 @@ show_pkg_files(prop_dictionary_t filesd) return 0; } +static int +_find_longest_pkgver_cb(prop_object_t obj, void *arg, bool *loop_done) +{ + size_t *len = arg; + const char *pkgver; + + (void)loop_done; + + prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); + if (*len == 0 || strlen(pkgver) > *len) + *len = strlen(pkgver); + + return 0; +} + +size_t +find_longest_pkgver(prop_dictionary_t d) +{ + size_t len = 0; + + (void)xbps_callback_array_iter_in_dict(d, "packages", + _find_longest_pkgver_cb, &len); + + return len; +} int show_pkg_namedesc(prop_object_t obj, void *arg, bool *loop_done) { + struct repo_search_data *rsd = arg; const char *pkgver, *pkgname, *desc; - char *pattern = arg; + char *tmp = NULL; + size_t i; (void)loop_done; assert(prop_object_type(obj) == PROP_TYPE_DICTIONARY); - assert(pattern != NULL); + assert(rsd->pattern != NULL); prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname); prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver); prop_dictionary_get_cstring_nocopy(obj, "short_desc", &desc); - if (xbps_pkgpattern_match(pkgver, pattern) == 1) - printf(" %s - %s\n", pkgver, desc); - else if (strcmp(pkgname, pattern) == 0) - printf(" %s - %s\n", pkgver, desc); - else if (xbps_pkgpattern_match(desc, pattern) == 1) - printf(" %s - %s\n", pkgver, desc); - else if (strstr(pkgver, pattern)) - printf(" %s - %s\n", pkgver, desc); + if ((xbps_pkgpattern_match(pkgver, rsd->pattern) == 1) || + (xbps_pkgpattern_match(desc, rsd->pattern) == 1) || + (strcmp(pkgname, rsd->pattern) == 0) || + (strstr(pkgver, rsd->pattern)) || (strstr(desc, rsd->pattern))) { + tmp = malloc(rsd->pkgver_len + 1); + if (tmp == NULL) + return errno; + + memcpy(tmp, pkgver, rsd->pkgver_len); + for (i = strlen(tmp); i < rsd->pkgver_len; i++) + tmp[i] = ' '; + + tmp[rsd->pkgver_len + 1] = '\0'; + printf(" %s %s\n", tmp, desc); + free(tmp); + } return 0; }