[1/3] util.c: extend --print-format with expac options

Message ID 20220326155422.28555-2-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@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(-)

Patch

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