[3/3] util.c: extend --print-format with %C, %D, %M

Message ID 20220326155422.28555-4-jelle@archlinux.org
State New
Headers show
Series [1/3] util.c: extend --print-format with expac options | expand

Commit Message

Jelle van der Waa March 26, 2022, 3:54 p.m. UTC
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(-)

Comments

Emil Velikov March 27, 2022, 1:33 p.m. UTC | #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
Allan McRae July 21, 2022, 9:58 a.m. UTC | #2
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

Patch

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);
 	}