Extend print-format with expac options
This patch series extends --print-format with a subset of the expac options. More options to be added in further patch series.
From: Jelle van der Waa <jelle@vdwaa.nl> Extend --print-format with all expac format strings which can be easily added without conversions and through a simple C macro. Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 7 +++++-- src/pacman/util.c | 33 +++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 785844ce..49e392cb 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -235,8 +235,11 @@ Transaction Options (apply to '-S', '-R' and '-U') *\--print-format* <format>:: Specify a printf-like format to control the output of the '\--print' - operation. The possible attributes are: "%a" for arch, "%n" for pkgname, - "%v" for pkgver, "%l" for location, "%r" for repository, and "%s" for size. + operation. The possible attributes are: "%a" for arch, "%d" for + description, "%e" for pkgbase, "%f" for filename, "%g" for base64 + encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for + packager, "%v" for pkgver, "%l" for location, "%r" for repository, and + "%s" for size. Implies '\--print'. diff --git a/src/pacman/util.c b/src/pacman/util.c index 53833d6f..3b92e678 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -61,6 +61,13 @@ enum { CELL_FREE = (1 << 3) }; +#define VAL_FROM_FORMAT_STR(temp, format, func) \ + if(strstr(temp, format)) { \ + string = strreplace(temp, format, func(pkg)); \ + free(temp); \ + temp = string; \ + } \ + int trans_init(int flags, int check_valid) { int ret; @@ -1156,18 +1163,22 @@ void print_packages(const alpm_list_t *packages) free(temp); temp = string; } + /* %d : description */ + VAL_FROM_FORMAT_STR(temp, "%d", alpm_pkg_get_desc) + /* %e : pkgbase */ + VAL_FROM_FORMAT_STR(temp, "%e", alpm_pkg_get_base) + /* %f : filename */ + VAL_FROM_FORMAT_STR(temp, "%f", alpm_pkg_get_filename) + /* %g : base64 encoded PGP signature */ + VAL_FROM_FORMAT_STR(temp, "%g", alpm_pkg_get_base64_sig) + /* %h : sha25sum */ + VAL_FROM_FORMAT_STR(temp, "%h", alpm_pkg_get_sha256sum) /* %n : pkgname */ - if(strstr(temp, "%n")) { - string = strreplace(temp, "%n", alpm_pkg_get_name(pkg)); - free(temp); - temp = string; - } + VAL_FROM_FORMAT_STR(temp, "%n", alpm_pkg_get_name) + /* %p : packager */ + VAL_FROM_FORMAT_STR(temp, "%p", alpm_pkg_get_packager) /* %v : pkgver */ - if(strstr(temp, "%v")) { - string = strreplace(temp, "%v", alpm_pkg_get_version(pkg)); - free(temp); - temp = string; - } + VAL_FROM_FORMAT_STR(temp, "%v", alpm_pkg_get_version) /* %l : location */ if(strstr(temp, "%l")) { char *pkgloc = pkg_get_location(pkg); @@ -1195,6 +1206,8 @@ void print_packages(const alpm_list_t *packages) free(size); free(temp); } + /* %u : url */ + VAL_FROM_FORMAT_STR(temp, "%u", alpm_pkg_get_url) printf("%s\n", string); free(string); } -- 2.35.1
On 27/3/22 01:54, Jelle van der Waa wrote:
From: Jelle van der Waa <jelle@vdwaa.nl>
Extend --print-format with all expac format strings which can be easily added without conversions and through a simple C macro.
OK.
Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 7 +++++-- src/pacman/util.c | 33 +++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-)
diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 785844ce..49e392cb 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -235,8 +235,11 @@ Transaction Options (apply to '-S', '-R' and '-U')
*\--print-format* <format>:: Specify a printf-like format to control the output of the '\--print' - operation. The possible attributes are: "%a" for arch, "%n" for pkgname, - "%v" for pkgver, "%l" for location, "%r" for repository, and "%s" for size. + operation. The possible attributes are: "%a" for arch, "%d" for + description, "%e" for pkgbase, "%f" for filename, "%g" for base64 + encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for + packager, "%v" for pkgver, "%l" for location, "%r" for repository, and + "%s" for size. Implies '\--print'.
diff --git a/src/pacman/util.c b/src/pacman/util.c index 53833d6f..3b92e678 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -61,6 +61,13 @@ enum { CELL_FREE = (1 << 3) };
+#define VAL_FROM_FORMAT_STR(temp, format, func) \ + if(strstr(temp, format)) { \ + string = strreplace(temp, format, func(pkg)); \ + free(temp); \ + temp = string; \ + } \ + int trans_init(int flags, int check_valid) { int ret; @@ -1156,18 +1163,22 @@ void print_packages(const alpm_list_t *packages) free(temp); temp = string; } + /* %d : description */ + VAL_FROM_FORMAT_STR(temp, "%d", alpm_pkg_get_desc) + /* %e : pkgbase */ + VAL_FROM_FORMAT_STR(temp, "%e", alpm_pkg_get_base) + /* %f : filename */ + VAL_FROM_FORMAT_STR(temp, "%f", alpm_pkg_get_filename) + /* %g : base64 encoded PGP signature */ + VAL_FROM_FORMAT_STR(temp, "%g", alpm_pkg_get_base64_sig) + /* %h : sha25sum */ + VAL_FROM_FORMAT_STR(temp, "%h", alpm_pkg_get_sha256sum) /* %n : pkgname */ - if(strstr(temp, "%n")) { - string = strreplace(temp, "%n", alpm_pkg_get_name(pkg)); - free(temp); - temp = string; - } + VAL_FROM_FORMAT_STR(temp, "%n", alpm_pkg_get_name) + /* %p : packager */ + VAL_FROM_FORMAT_STR(temp, "%p", alpm_pkg_get_packager) /* %v : pkgver */ - if(strstr(temp, "%v")) { - string = strreplace(temp, "%v", alpm_pkg_get_version(pkg)); - free(temp); - temp = string; - } + VAL_FROM_FORMAT_STR(temp, "%v", alpm_pkg_get_version) /* %l : location */ if(strstr(temp, "%l")) { char *pkgloc = pkg_get_location(pkg); @@ -1195,6 +1206,8 @@ void print_packages(const alpm_list_t *packages) free(size); free(temp); } + /* %u : url */ + VAL_FROM_FORMAT_STR(temp, "%u", alpm_pkg_get_url) printf("%s\n", string); free(string); }
From: Jelle van der Waa <jelle@vdwaa.nl> Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 10 +++++----- src/pacman/util.c | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 49e392cb..8a9294fc 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -235,11 +235,11 @@ Transaction Options (apply to '-S', '-R' and '-U') *\--print-format* <format>:: Specify a printf-like format to control the output of the '\--print' - operation. The possible attributes are: "%a" for arch, "%d" for - description, "%e" for pkgbase, "%f" for filename, "%g" for base64 - encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for - packager, "%v" for pkgver, "%l" for location, "%r" for repository, and - "%s" for size. + operation. The possible attributes are: "%a" for arch, "%b" for + builddate, "%d" for description, "%e" for pkgbase, "%f" for filename, + "%g" for base64 encoded PGP signature, "%h" for sha256sum, "%n" for + pkgname, "%p" for packager, "%v" for pkgver, "%l" for location, "%r" + for repository, and "%s" for size. Implies '\--print'. diff --git a/src/pacman/util.c b/src/pacman/util.c index 3b92e678..519765f1 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1163,6 +1163,17 @@ void print_packages(const alpm_list_t *packages) free(temp); temp = string; } + /* %b : build date */ + if(strstr(temp, "%b")) { + char bdatestr[50] = ""; + time_t bdate = (time_t)alpm_pkg_get_builddate(pkg); + if(bdate) { + strftime(bdatestr, 50, "%c", localtime(&bdate)); + string = strreplace(temp, "%b", bdatestr); + free(temp); + temp = string; + } + } /* %d : description */ VAL_FROM_FORMAT_STR(temp, "%d", alpm_pkg_get_desc) /* %e : pkgbase */ -- 2.35.1
Hi Jelle, Thanks for the series - would love to see it land and purse expac from my systems :-P On Sat, 26 Mar 2022 at 15:54, Jelle van der Waa <jelle@vdwaa.nl> wrote:
From: Jelle van der Waa <jelle@vdwaa.nl>
Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 10 +++++----- src/pacman/util.c | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 49e392cb..8a9294fc 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -235,11 +235,11 @@ Transaction Options (apply to '-S', '-R' and '-U')
*\--print-format* <format>:: Specify a printf-like format to control the output of the '\--print' - operation. The possible attributes are: "%a" for arch, "%d" for - description, "%e" for pkgbase, "%f" for filename, "%g" for base64 - encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for - packager, "%v" for pkgver, "%l" for location, "%r" for repository, and - "%s" for size. + operation. The possible attributes are: "%a" for arch, "%b" for + builddate, "%d" for description, "%e" for pkgbase, "%f" for filename, + "%g" for base64 encoded PGP signature, "%h" for sha256sum, "%n" for + pkgname, "%p" for packager, "%v" for pkgver, "%l" for location, "%r" + for repository, and "%s" for size. Implies '\--print'.
diff --git a/src/pacman/util.c b/src/pacman/util.c index 3b92e678..519765f1 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1163,6 +1163,17 @@ void print_packages(const alpm_list_t *packages) free(temp); temp = string; } + /* %b : build date */ + if(strstr(temp, "%b")) { + char bdatestr[50] = ""; + time_t bdate = (time_t)alpm_pkg_get_builddate(pkg); + if(bdate) {
AFAICT alpm_pkg_get_builddate() returns -1 on error, so this seems off. In case it's zero, a message like "00:00 hours, Jan 1, 1970 UTC" seems appropriate doesn't it? Grepping around - various in-tree code paths opt for the same assumption. Don't know if it's worth fixing or staying consistent. Either way, hope this helps o/ -Emil
On 27/3/22 01:54, Jelle van der Waa wrote:
From: Jelle van der Waa <jelle@vdwaa.nl>
Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 10 +++++----- src/pacman/util.c | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 49e392cb..8a9294fc 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -235,11 +235,11 @@ Transaction Options (apply to '-S', '-R' and '-U')
*\--print-format* <format>:: Specify a printf-like format to control the output of the '\--print' - operation. The possible attributes are: "%a" for arch, "%d" for - description, "%e" for pkgbase, "%f" for filename, "%g" for base64 - encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for - packager, "%v" for pkgver, "%l" for location, "%r" for repository, and - "%s" for size. + operation. The possible attributes are: "%a" for arch, "%b" for + builddate, "%d" for description, "%e" for pkgbase, "%f" for filename, + "%g" for base64 encoded PGP signature, "%h" for sha256sum, "%n" for + pkgname, "%p" for packager, "%v" for pkgver, "%l" for location, "%r" + for repository, and "%s" for size. Implies '\--print'.
diff --git a/src/pacman/util.c b/src/pacman/util.c index 3b92e678..519765f1 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -1163,6 +1163,17 @@ void print_packages(const alpm_list_t *packages) free(temp); temp = string; } + /* %b : build date */ + if(strstr(temp, "%b")) { + char bdatestr[50] = ""; + time_t bdate = (time_t)alpm_pkg_get_builddate(pkg); + if(bdate) {
Changing to if(bdate != -1) and applying. A
+ strftime(bdatestr, 50, "%c", localtime(&bdate)); + string = strreplace(temp, "%b", bdatestr); + free(temp); + temp = string; + } + } /* %d : description */ VAL_FROM_FORMAT_STR(temp, "%d", alpm_pkg_get_desc) /* %e : pkgbase */
From: Jelle van der Waa <jelle@archlinux.org> Extend print-format with checkdepends, depends and makedepends. Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 3 ++- src/pacman/util.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 8a9294fc..2040f98d 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -239,7 +239,8 @@ Transaction Options (apply to '-S', '-R' and '-U') builddate, "%d" for description, "%e" for pkgbase, "%f" for filename, "%g" for base64 encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for packager, "%v" for pkgver, "%l" for location, "%r" - for repository, and "%s" for size. + for repository, "%s" for size, "%C" for checkdepends, "%D" for depends + and "%M" for makedepends. Implies '\--print'. diff --git a/src/pacman/util.c b/src/pacman/util.c index 519765f1..cae024e4 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -405,6 +405,28 @@ char *strreplace(const char *str, const char *needle, const char *replace) return newstr; } +static char *concat_alpm_depends(alpm_list_t *lst) +{ + char *depends = NULL; + char *tmp = NULL; + for(alpm_list_t *i = lst; i; i = alpm_list_next(i)) { + alpm_depend_t *dep = i->data; + char *depstring = alpm_dep_compute_string(dep); + if(tmp) { + asprintf(&depends, "%s %s", tmp, depstring); + free(tmp); + } else { + asprintf(&depends, "%s", depstring); + } + tmp = depends; + free(depstring); + } + if(!depends) { + asprintf(&depends, "%s", ""); + } + return depends; +} + static size_t string_length(const char *s) { int len; @@ -1219,6 +1241,33 @@ void print_packages(const alpm_list_t *packages) } /* %u : url */ VAL_FROM_FORMAT_STR(temp, "%u", alpm_pkg_get_url) + /* %C : checkdepends */ + if(strstr(temp, "%C")) { + alpm_list_t *lst = alpm_pkg_get_checkdepends(pkg); + char *depends = concat_alpm_depends(lst); + string = strreplace(temp, "%C", lst ? depends : ""); + free(depends); + free(temp); + temp = string; + } + /* %D : depends */ + if(strstr(temp, "%D")) { + alpm_list_t *lst = alpm_pkg_get_depends(pkg); + char *depends = concat_alpm_depends(lst); + string = strreplace(temp, "%D", depends); + free(depends); + free(temp); + temp = string; + } + /* %M : makedepends */ + if(strstr(temp, "%M")) { + alpm_list_t *lst = alpm_pkg_get_makedepends(pkg); + char *depends = concat_alpm_depends(lst); + string = strreplace(temp, "%M", depends); + free(depends); + free(temp); + temp = string; + } printf("%s\n", string); free(string); } -- 2.35.1
On Sat, 26 Mar 2022 at 15:54, Jelle van der Waa <jelle@vdwaa.nl> wrote:
From: Jelle van der Waa <jelle@archlinux.org>
Extend print-format with checkdepends, depends and makedepends.
Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 3 ++- src/pacman/util.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 8a9294fc..2040f98d 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -239,7 +239,8 @@ Transaction Options (apply to '-S', '-R' and '-U') builddate, "%d" for description, "%e" for pkgbase, "%f" for filename, "%g" for base64 encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for packager, "%v" for pkgver, "%l" for location, "%r" - for repository, and "%s" for size. + for repository, "%s" for size, "%C" for checkdepends, "%D" for depends + and "%M" for makedepends. Implies '\--print'.
diff --git a/src/pacman/util.c b/src/pacman/util.c index 519765f1..cae024e4 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -405,6 +405,28 @@ char *strreplace(const char *str, const char *needle, const char *replace) return newstr; }
+static char *concat_alpm_depends(alpm_list_t *lst) +{ + char *depends = NULL; + char *tmp = NULL; + for(alpm_list_t *i = lst; i; i = alpm_list_next(i)) { + alpm_depend_t *dep = i->data; + char *depstring = alpm_dep_compute_string(dep); + if(tmp) { + asprintf(&depends, "%s %s", tmp, depstring); + free(tmp); + } else { + asprintf(&depends, "%s", depstring); + } + tmp = depends; + free(depstring);
Function does 2x allocation for each dependency. Where a bunch of packages easily have 20+. In case that matters and one wants to micro-optimise - a pre/re allocated 4k page should reduce that. Just throwing the idea - doubt it matters too much to care. -Emil
On 27/3/22 23:33, Emil Velikov wrote:
On Sat, 26 Mar 2022 at 15:54, Jelle van der Waa <jelle@vdwaa.nl> wrote:
From: Jelle van der Waa <jelle@archlinux.org>
Extend print-format with checkdepends, depends and makedepends.
Signed-off-by: Jelle van der Waa <jelle@archlinux.org> --- doc/pacman.8.asciidoc | 3 ++- src/pacman/util.c | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-)
diff --git a/doc/pacman.8.asciidoc b/doc/pacman.8.asciidoc index 8a9294fc..2040f98d 100644 --- a/doc/pacman.8.asciidoc +++ b/doc/pacman.8.asciidoc @@ -239,7 +239,8 @@ Transaction Options (apply to '-S', '-R' and '-U') builddate, "%d" for description, "%e" for pkgbase, "%f" for filename, "%g" for base64 encoded PGP signature, "%h" for sha256sum, "%n" for pkgname, "%p" for packager, "%v" for pkgver, "%l" for location, "%r" - for repository, and "%s" for size. + for repository, "%s" for size, "%C" for checkdepends, "%D" for depends + and "%M" for makedepends. Implies '\--print'.
diff --git a/src/pacman/util.c b/src/pacman/util.c index 519765f1..cae024e4 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -405,6 +405,28 @@ char *strreplace(const char *str, const char *needle, const char *replace) return newstr; }
+static char *concat_alpm_depends(alpm_list_t *lst) +{ + char *depends = NULL; + char *tmp = NULL; + for(alpm_list_t *i = lst; i; i = alpm_list_next(i)) { + alpm_depend_t *dep = i->data; + char *depstring = alpm_dep_compute_string(dep); + if(tmp) { + asprintf(&depends, "%s %s", tmp, depstring); + free(tmp); + } else { + asprintf(&depends, "%s", depstring); + } + tmp = depends; + free(depstring);
Function does 2x allocation for each dependency. Where a bunch of packages easily have 20+. In case that matters and one wants to micro-optimise - a pre/re allocated 4k page should reduce that.
Just throwing the idea - doubt it matters too much to care.
Given how long I took to review this patch, I will not chase this optimisation. But happy for someone to provide a patch to do this. Allan
participants (3)
-
Allan McRae
-
Emil Velikov
-
Jelle van der Waa