Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add support for %A -- Annotations -- to pkg_printf
Matthew Seaman committed 12 years ago
commit 7de5f47a94b6744d841434981698d203f727e934
parent 8d1ac98
2 files changed +85 -16
modified libpkg/pkg_printf.c
@@ -43,7 +43,9 @@
/*
 * Format codes
 *    Arg Type     What
-
 * A
+
 * A  pkg          Package annotations
+
 * An pkg_note     Annotation tag name
+
 * Av pkg_note     Annotation value
 *
 * B  pkg          List of required shared libraries
 * Bn pkg_shlib    Shared library name
@@ -170,6 +172,12 @@ struct pkg_printf_fmt {
 */

static const struct pkg_printf_fmt	fmt[] = {
+
	[PP_PKG_ANNOTATION_NAME] =
+
	{ 'A', 'n',   false, PP_PKG|PP_A,       &format_annotation_name, },
+
	[PP_PKG_ANNOTATION_VALUE] =
+
	{ 'A', 'v',   false, PP_PKG|PP_A,       &format_annotation_value, },
+
	[PP_PKG_ANNOTATIONS] =
+
	{ 'A', '\0',  true,  PP_PKG,            &format_annotations, },
	[PP_PKG_SHLIB_REQUIRED_NAME] =
	{ 'B', 'n',   false, PP_PKG|PP_B,	&format_shlib_name, },
	[PP_PKG_SHLIBS_REQUIRED] =
@@ -288,7 +296,7 @@ static const struct pkg_printf_fmt fmt[] = {

/*
 * Note: List values -- special behaviour with ? and # modifiers.
-
 * Affects %B %C %D %F %G %L %O %U %b %d %r
+
 * Affects %A %B %C %D %F %G %L %O %U %b %d %r
 *
 * With ? -- Flag values.  Boolean.  %?X returns 0 if the %X list is
 * empty, 1 otherwise.
@@ -298,9 +306,63 @@ static const struct pkg_printf_fmt fmt[] = {
 */

/*
+
 * %A -- Annotations.  Free-form tag+value text that can be added to
+
 * packages.  Optionally accepts per-field format in %{ %| %} Default
+
 * %{%An: %Av\n%|%}
+
 */  
+
struct sbuf *
+
format_annotations(struct sbuf *sbuf, const void *data, struct percent_esc *p)
+
{
+
	const struct pkg	*pkg = data;
+

+
	if (p->flags & (PP_ALTERNATE_FORM1|PP_ALTERNATE_FORM2))
+
		return (list_count(sbuf, pkg_list_count(pkg, PKG_ANNOTATIONS), p));
+
	else {
+
		struct pkg_note	*note;
+
		int		 count;
+

+
		set_list_defaults(p, "%An: %Av\n", "");
+

+
		count = 1;
+
		while (pkg_annotations(pkg, &note) == EPKG_OK) {
+
			if (count > 1)
+
				iterate_item(sbuf, pkg, sbuf_data(p->sep_fmt),
+
					     note, count, PP_A);
+

+
			iterate_item(sbuf, pkg, sbuf_data(p->item_fmt),
+
				     note, count, PP_A);
+
			count++;
+
		}
+
	}
+
	return (sbuf);
+
}
+

+
/*
+
 * %An -- Annotation tag name.
+
 */
+
struct sbuf *
+
format_annotation_name(struct sbuf *sbuf, const void *data, struct percent_esc *p)
+
{
+
	const struct pkg_note	*note = data;
+

+
	return (string_val(sbuf, pkg_annotation_tag(note), p));
+
}
+

+
/*
+
 * %Av -- Annotation value.
+
 */
+
struct sbuf *
+
format_annotation_value(struct sbuf *sbuf, const void *data, struct percent_esc *p)
+
{
+
	const struct pkg_note	*note = data;
+

+
	return (string_val(sbuf, pkg_annotation_value(note), p));
+
}
+

+
/*
 * %B -- Required Shared Libraries.  List of shlibs required by
 * binaries in the pkg.  Optionally accepts per-field format in %{ %|
-
 * %}, where %n is replaced by the shlib name.  Default %{%Bn\n%|%}
+
 * %}.  Default %{%Bn\n%|%}
 */
struct sbuf *
format_shlibs_required(struct sbuf *sbuf, const void *data, struct percent_esc *p)
modified libpkg/private/pkg_printf.h
@@ -47,23 +47,24 @@

/* Contexts for option parsing */
#define PP_PKG	(1U << 0)	/* Any pkg scalar value */
-
#define PP_B	(1U << 1)	/* shlib required */
-
#define PP_C	(1U << 2)	/* category */
-
#define PP_D	(1U << 3)	/* directory */
-
#define PP_F	(1U << 4)	/* file */
-
#define PP_G	(1U << 5)	/* group */
-
#define PP_L	(1U << 6)	/* licence */
-
#define PP_O	(1U << 7)	/* option */
-
#define PP_U	(1U << 8)	/* user */
-
#define PP_b    (1U << 9)	/* shlib provided */
-
#define PP_d	(1U << 10)	/* dependency */
-
#define PP_r	(1U << 11)	/* requirement */
+
#define PP_A	(1U << 1)	/* annotations */
+
#define PP_B	(1U << 2)	/* shlib required */
+
#define PP_C	(1U << 3)	/* category */
+
#define PP_D	(1U << 4)	/* directory */
+
#define PP_F	(1U << 5)	/* file */
+
#define PP_G	(1U << 6)	/* group */
+
#define PP_L	(1U << 7)	/* licence */
+
#define PP_O	(1U << 8)	/* option */
+
#define PP_U	(1U << 9)	/* user */
+
#define PP_b	(1U << 10)	/* shlib provided */
+
#define PP_d	(1U << 11)	/* dependency */
+
#define PP_r	(1U << 12)	/* requirement */

#define _PP_last	PP_r
#define PP_ALL	((_PP_last << 1) - 1) /* All contexts */

/*  %{ %| %} trailer context */
-
#define PP_TRAILER	(PP_B|PP_C|PP_D|PP_F|PP_G|PP_L|PP_O|PP_U|PP_b|PP_d|PP_r)
+
#define PP_TRAILER	(PP_A|PP_B|PP_C|PP_D|PP_F|PP_G|PP_L|PP_O|PP_U|PP_b|PP_d|PP_r)

/* Licence logic types */
#define PP_LIC_SINGLE	0
@@ -73,7 +74,10 @@
/* These are in alphabetical order of format code with A-Z sorting
 * before a-z */
typedef enum _fmt_code_t {
-
	PP_PKG_SHLIB_REQUIRED_NAME = 0,
+
	PP_PKG_ANNOTATION_NAME = 0,
+
	PP_PKG_ANNOTATION_VALUE,
+
	PP_PKG_ANNOTATIONS,
+
	PP_PKG_SHLIB_REQUIRED_NAME,
	PP_PKG_SHLIBS_REQUIRED,
	PP_PKG_CATEGORY_NAME,
	PP_PKG_CATEGORIES,
@@ -143,6 +147,9 @@ struct percent_esc {

/* Format handler function prototypes */

+
_static struct sbuf *format_annotation_name(struct sbuf *, const void *, struct percent_esc *);
+
_static struct sbuf *format_annotation_value(struct sbuf *, const void *, struct percent_esc *);
+
_static struct sbuf *format_annotations(struct sbuf *, const void *, struct percent_esc *);
_static struct sbuf *format_shlibs_required(struct sbuf *, const void *, struct percent_esc *);
_static struct sbuf *format_shlib_name(struct sbuf *, const void *, struct percent_esc *);
_static struct sbuf *format_categories(struct sbuf *, const void *, struct percent_esc *);