Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Pair up all the pkg_xprintf() functions with a pkg_vxprintf() equivalent that takes a va_list structure instead of a variable argument list.
Matthew Seaman committed 12 years ago
commit f49654ddb81e69ba75e68da1549129f8638c1301
parent b97fd12
3 files changed +194 -87
modified libpkg/pkg.h.in
@@ -1376,90 +1376,125 @@ struct pkg_repo *pkg_repo_find_name(const char *name);
 */

/**
-
 * print to stdout data from pkg as indicated by the format code fmt
+
 * print to stdout data from pkg as indicated by the format code format
 * @param ... Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to print
+
 * @param format String with embedded %-escapes indicating what to print
 * @return count of the number of characters printed
 */
-
int pkg_printf(const char * restrict fmt, ...);
+
int pkg_printf(const char * restrict format, ...);

/**
-
 * print to stdout data from pkg as indicated by the format code fmt
+
 * print to stdout data from pkg as indicated by the format code format
 * @param ap Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to print
+
 * @param format String with embedded %-escapes indicating what to print
 * @return count of the number of characters printed
 */
-
int pkg_vprintf(const char * restrict fmt, va_list ap);
+
int pkg_vprintf(const char * restrict format, va_list ap);

/**
-
 * print to named stream from pkg as indicated by the format code fmt
+
 * print to named stream from pkg as indicated by the format code format
 * @param ... Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to output
+
 * @param format String with embedded %-escapes indicating what to output
 * @return count of the number of characters printed
 */
-
int pkg_fprintf(FILE * restrict stream, const char * restrict fmt, ...);
+
int pkg_fprintf(FILE * restrict stream, const char * restrict format, ...);

/**
-
 * print to named stream from pkg as indicated by the format code fmt
+
 * print to named stream from pkg as indicated by the format code format
 * @param ap varargs arglist
-
 * @param fmt String with embedded %-escapes indicating what to output
+
 * @param format String with embedded %-escapes indicating what to output
 * @return count of the number of characters printed
 */
-
int pkg_vfprintf(FILE * restrict stream, const char * restrict fmt, va_list ap);
+
int pkg_vfprintf(FILE * restrict stream, const char * restrict format,
+
	va_list ap);

/**
-
 * print to file descriptor d data from pkg as indicated by the format
-
 * code fmt
-
 * @param d Previously opened file descriptor to print to
+
 * print to file descriptor fd data from pkg as indicated by the format
+
 * code format
+
 * @param fd Previously opened file descriptor to print to
 * @param ... Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to print
+
 * @param format String with embedded %-escapes indicating what to print
 * @return count of the number of characters printed
 */
-
int pkg_dprintf(int fd, const char * restrict fmt, ...);
+
int pkg_dprintf(int fd, const char * restrict format, ...);
+

+
/**
+
 * print to file descriptor fd data from pkg as indicated by the format
+
 * code format
+
 * @param fd Previously opened file descriptor to print to
+
 * @param ap Varargs list of struct pkg etc. supplying the data
+
 * @param format String with embedded %-escapes indicating what to print
+
 * @return count of the number of characters printed
+
 */
+
int pkg_vdprintf(int fd, const char * restrict format, va_list ap);

/**
 * print to buffer str of given size data from pkg as indicated by the
-
 * format code fmt as a NULL-terminated string
+
 * format code format as a NULL-terminated string
 * @param str Character array buffer to receive output
 * @param size Length of the buffer str
 * @param ... Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to output
+
 * @param format String with embedded %-escapes indicating what to output
+
 * @return count of the number of characters that would have been output
+
 * disregarding truncation to fit size
+
 */
+
int pkg_snprintf(char * restrict str, size_t size,
+
	const char * restrict format, ...);
+

+
/**
+
 * print to buffer str of given size data from pkg as indicated by the
+
 * format code format as a NULL-terminated string
+
 * @param str Character array buffer to receive output
+
 * @param size Length of the buffer str
+
 * @param ap Varargs list of struct pkg etc. supplying the data
+
 * @param format String with embedded %-escapes indicating what to output
 * @return count of the number of characters that would have been output
 * disregarding truncation to fit size
 */
-
int pkg_snprintf(char * restrict str, size_t size, const char * restrict fmt,
-
		 ...);
+
int pkg_vsnprintf(char * restrict str, size_t size,
+
	const char * restrict format,va_list ap);

/**
 * Allocate a string buffer ret sufficiently big to contain formatted
-
 * data data from pkg as indicated by the format code fmt
+
 * data data from pkg as indicated by the format code format
 * @param ret location of pointer to be set to point to buffer containing
 * result 
 * @param ... Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to output
+
 * @param format String with embedded %-escapes indicating what to output
+
 * @return count of the number of characters printed
+
 */
+
int pkg_asprintf(char **ret, const char * restrict format, ...);
+

+
/**
+
 * Allocate a string buffer ret sufficiently big to contain formatted
+
 * data data from pkg as indicated by the format code format
+
 * @param ret location of pointer to be set to point to buffer containing
+
 * result 
+
 * @param ap Varargs list of struct pkg etc. supplying the data
+
 * @param format String with embedded %-escapes indicating what to output
 * @return count of the number of characters printed
 */
-
int pkg_asprintf(char **ret, const char * restrict fmt, ...);
+
int pkg_vasprintf(char **ret, const char * restrict format, va_list ap);

/**
-
 * store data from pkg into sbuf as indicated by the format code fmt.
+
 * store data from pkg into sbuf as indicated by the format code format.
 * @param sbuf contains the result
 * @param ... Varargs list of struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to output
+
 * @param format String with embedded %-escapes indicating what to output
 * @return count of the number of characters in the result
 */
struct sbuf *pkg_sbuf_printf(struct sbuf * restrict sbuf,
-
			     const char * restrict fmt, ...);
+
	const char * restrict format, ...);

/**
-
 * store data from pkg into sbuf as indicated by the format code fmt.
+
 * store data from pkg into sbuf as indicated by the format code format.
 * This is the core function called by all the other pkg_printf() family.
 * @param sbuf contains the result
 * @param ap Arglist with struct pkg etc. supplying the data
-
 * @param fmt String with embedded %-escapes indicating what to output
+
 * @param format String with embedded %-escapes indicating what to output
 * @return count of the number of characters in the result
 */
struct sbuf *pkg_sbuf_vprintf(struct sbuf * restrict sbuf,
-
			      const char * restrict fmt, va_list ap);
+
	const char * restrict format, va_list ap);

#endif
modified libpkg/pkg_printf.3
@@ -35,7 +35,9 @@
.Os
.Sh NAME
.Nm pkg_printf , pkg_fprintf , pkg_dprintf , pkg_snprintf , pkg_asprintf ,
-
.Nm pkg_sbuf_printf , pkg_sbuf_vprintf
+
.Nm pkg_sbuf_printf ,
+
.Nm pkg_vprintf , pkg_vfprintf , pkg_vdprintf , pkg_vsnprintf , pkg_vasprintf ,
+
.Nm pkg_sbuf_vprintf
.Nd formatted output of package data
.Sh LIBRARY
.Lb libpkg
@@ -44,12 +46,8 @@
.Ft int
.Fn pkg_printf "const char * restrict format" ...
.Ft int
-
.Fn pkg_vprintf "const char * restrict format" "va_list ap"
-
.Ft int
.Fn pkg_fprintf "FILE * restrict stream" "const char * restrict format" ...
.Ft int
-
.Fn pkg_vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
-
.Ft int
.Fn pkg_dprintf "int fd" "const char * restrict format" ...
.Ft int
.Fn pkg_snprintf "char * restrict str" "size_t size" "const char * restrict format" ...
@@ -57,6 +55,17 @@
.Fn pkg_asprintf "char **ret" "const char * restrict format" ...
.Ft struct sbuf *
.Fn pkg_sbuf_printf "struct sbuf * restrict sbuf" "const char * restrict format" ...
+
.In stdarg.h
+
.Ft int
+
.Fn pkg_vprintf "const char * restrict format" "va_list ap"
+
.Ft int
+
.Fn pkg_vfprintf "FILE * restrict stream" "const char * restrict format" "va_list ap"
+
.Ft int
+
.Fn pkg_vdprintf "int fd" "const char * restrict format" "va_list ap"
+
.Ft int
+
.Fn pkg_vsnprintf "char * restrict str" "size_t size" "const char * restrict format" "va_list ap"
+
.Ft int
+
.Fn pkg_vasprintf "char **ret" "const char * restrict format" "va_list ap"
.Ft struct sbuf *
.Fn pkg_sbuf_vprintf "struct sbuf * restrict sbuf" "const char * restrict format" "va_list ap"
.Sh DESCRIPTION
@@ -81,13 +90,20 @@ and
write output to the given output
.Fa stream ;
.Fn pkg_dprintf
-
writes output to the given file descriptor;
-
.Fn pkg_snprintf ,
-
writes to the character string
+
and
+
.Fn pkg_vdprintf
+
write output to the given file descriptor;
+
.Fn pkg_snprintf
+
and
+
.Fn pkg_vsnprintf
+
write to the character string
.Fa str ;
.Fn pkg_asprintf
-
dynamically allocates a new string with
-
.Xr malloc 3 ;
+
and
+
.Fn pkg_vasprintf
+
dynamically allocate a new string with
+
.Xr malloc 3
+
to write to;
.Fn pkg_sbuf_printf
and
.Fn pkg_sbuf_vprintf
@@ -106,7 +122,9 @@ These functions return the number of characters printed
used to end output to strings) or a negative value if an output error occurs,
except for
.Fn pkg_snprintf
-
which returns the number of characters that would have been printed if the
+
or
+
.Fn pkg_vsnprintf
+
which return the number of characters that would have been printed if the
.Fa size
were unlimited
(again, not including the final
@@ -121,8 +139,9 @@ in the case of errors.
.Pp
The
.Fn pkg_asprintf
-
function
-
sets
+
and
+
.Fn pkg_vasprintf
+
functions set
.Fa *ret
to be a pointer to a buffer sufficiently large to hold the formatted string.
This pointer should be passed to
@@ -130,6 +149,8 @@ This pointer should be passed to
to release the allocated storage when it is no longer needed.
If sufficient space cannot be allocated,
.Fn pkg_asprintf
+
and
+
.Fn pkg_vasprintf
will return \-1 and set
.Fa ret
to be a
@@ -138,8 +159,9 @@ pointer.
.Pp
The
.Fn pkg_snprintf
-
function
-
will write at most
+
and
+
.Fn pkg_vsnprintf
+
functions will write at most
.Fa size Ns \-1
of the characters printed into the output string
(the
@@ -211,7 +233,12 @@ and
generate either
.Dq yes
or
-
.Dq no .
+
.Dq no
+
for
+
.Sq true
+
and
+
.Sq false
+
respectively.
.Pp
For the licence logic format
.Cm (l)
@@ -260,7 +287,11 @@ generate either
.Dq (*)
or
.Dq \^
-
(empty).
+
(empty) for
+
.Sq true
+
and
+
.Sq false
+
respectively.
.Pp
For the licence logic format
.Cm (l)
@@ -334,7 +365,7 @@ See the
section for details.
.El
.Ss SCALE FACTORS
-
Humanized number conversions (using the scale the number to lie within
+
Humanized number conversions scale the number to lie within
the range 1 \- 1000 (power of ten conversions using the
.Cm ?
format modifier) or 1 \- 1024 (power of two conversions using the
@@ -394,7 +425,8 @@ and, for array values only, may be optionally divided into two by the
character sequence
.Cm %| .
For array values, it contains one or two strings containing any number
-
of the format conversions described here.
+
of a context sensitive subset of format conversions from those
+
described here.
For timestamp values it contains any number of format conversion
specifiers with meanings as described in
.Xr strftime 3 .
@@ -409,7 +441,9 @@ default format, detailed below.
Within a
.Dq row format
string, you may use any of the single-character non-array valued
-
format codes, but only the two-character format codes which correspond
+
format codes except for
+
.Cm %S ,
+
but only the two-character format codes which correspond
to the parent item and have the same first character.
Array valued format codes may not be used within row formats,
nor may you embed one
modified libpkg/pkg_printf.c
@@ -2648,23 +2648,13 @@ process_format_main(struct sbuf *sbuf, struct percent_esc *p, const char *f,
int
pkg_printf(const char * restrict format, ...)
{
-
	struct sbuf	*sbuf;
	int		 count;
	va_list		 ap;

-
	sbuf  = sbuf_new_auto();
-

	va_start(ap, format);
-
	if (sbuf)
-
		sbuf = pkg_sbuf_vprintf(sbuf, format, ap);
+
	count = pkg_vprintf(format, ap);
	va_end(ap);
-
	if (sbuf && sbuf_len(sbuf) >= 0) {
-
		sbuf_finish(sbuf);
-
		count = printf("%s", sbuf_data(sbuf));
-
	} else
-
		count = -1;
-
	if (sbuf)
-
		sbuf_delete(sbuf);
+

	return (count);
}

@@ -2703,23 +2693,13 @@ pkg_vprintf(const char * restrict format, va_list ap)
int
pkg_fprintf(FILE * restrict stream, const char * restrict format, ...)
{
-
	struct sbuf	*sbuf;
	int		 count;
	va_list		 ap;

-
	sbuf  = sbuf_new_auto();
-

	va_start(ap, format);
-
	if (sbuf)
-
		sbuf = pkg_sbuf_vprintf(sbuf, format, ap);
+
	count = pkg_vfprintf(stream, format, ap);
	va_end(ap);
-
	if (sbuf && sbuf_len(sbuf) >= 0) {
-
		sbuf_finish(sbuf);
-
		count = fprintf(stream, "%s", sbuf_data(sbuf));
-
	} else
-
		count = -1;
-
	if (sbuf)
-
		sbuf_delete(sbuf);
+

	return (count);
}

@@ -2750,9 +2730,9 @@ pkg_vfprintf(FILE * restrict stream, const char * restrict format, va_list ap)
}

/**
-
 * print to file descriptor d data from pkg as indicated by the format
+
 * print to file descriptor fd data from pkg as indicated by the format
 * code format
-
 * @param d Previously opened file descriptor to print to
+
 * @param fd Previously opened file descriptor to print to
 * @param ... Varargs list of struct pkg etc. supplying the data
 * @param format String with embedded %-escapes indicating what to print
 * @return count of the number of characters printed
@@ -2760,16 +2740,34 @@ pkg_vfprintf(FILE * restrict stream, const char * restrict format, va_list ap)
int
pkg_dprintf(int fd, const char * restrict format, ...)
{
-
	struct sbuf	*sbuf;
	int		 count;
	va_list		 ap;

+
	va_start(ap, format);
+
	count = pkg_vdprintf(fd, format, ap);
+
	va_end(ap);
+

+
	return (count);
+
}
+

+
/**
+
 * print to file descriptor fd data from pkg as indicated by the format
+
 * code format
+
 * @param fd Previously opened file descriptor to print to
+
 * @param ap Varargs list of struct pkg etc. supplying the data
+
 * @param format String with embedded %-escapes indicating what to print
+
 * @return count of the number of characters printed
+
 */
+
int
+
pkg_vdprintf(int fd, const char * restrict format, va_list ap)
+
{
+
	struct sbuf	*sbuf;
+
	int		 count;
+

	sbuf  = sbuf_new_auto();

-
	va_start(ap, format);
	if (sbuf)
		sbuf = pkg_sbuf_vprintf(sbuf, format, ap);
-
	va_end(ap);
	if (sbuf && sbuf_len(sbuf) >= 0) {
		sbuf_finish(sbuf);
		count = dprintf(fd, "%s", sbuf_data(sbuf));
@@ -2791,19 +2789,39 @@ pkg_dprintf(int fd, const char * restrict format, ...)
 * disregarding truncation to fit size
 */
int
-
pkg_snprintf(char * restrict str, size_t size, const char * restrict format,
-
	     ...)
+
pkg_snprintf(char * restrict str, size_t size, const char * restrict format, ...)
{
-
	struct sbuf	*sbuf;
	int		 count;
	va_list		 ap;

+
	va_start(ap, format);
+
	count = pkg_vsnprintf(str, size, format, ap);
+
	va_end(ap);
+

+
	return (count);
+
}
+

+
/**
+
 * print to buffer str of given size data from pkg as indicated by the
+
 * format code format as a NULL-terminated string
+
 * @param str Character array buffer to receive output
+
 * @param size Length of the buffer str
+
 * @param ap Varargs list of struct pkg etc. supplying the data
+
 * @param format String with embedded %-escapes indicating what to output
+
 * @return count of the number of characters that would have been output
+
 * disregarding truncation to fit size
+
 */
+
int
+
pkg_vsnprintf(char * restrict str, size_t size, const char * restrict format,
+
	     va_list ap)
+
{
+
	struct sbuf	*sbuf;
+
	int		 count;
+

	sbuf  = sbuf_new_auto();

-
	va_start(ap, format);
	if (sbuf)
		sbuf = pkg_sbuf_vprintf(sbuf, format, ap);
-
	va_end(ap);
	if (sbuf && sbuf_len(sbuf) >= 0) {
		sbuf_finish(sbuf);
		count = snprintf(str, size, "%s", sbuf_data(sbuf));
@@ -2811,6 +2829,7 @@ pkg_snprintf(char * restrict str, size_t size, const char * restrict format,
		count = -1;
	if (sbuf)
		sbuf_delete(sbuf);
+

	return (count);
}

@@ -2826,16 +2845,35 @@ pkg_snprintf(char * restrict str, size_t size, const char * restrict format,
int
pkg_asprintf(char **ret, const char * restrict format, ...)
{
-
	struct sbuf	*sbuf;
	int		 count;
	va_list		 ap;

+
	va_start(ap, format);
+
	count = pkg_vasprintf(ret, format, ap);
+
	va_end(ap);
+

+
	return (count);
+
}
+

+
/**
+
 * Allocate a string buffer ret sufficiently big to contain formatted
+
 * data data from pkg as indicated by the format code format
+
 * @param ret location of pointer to be set to point to buffer containing
+
 * result 
+
 * @param ap Varargs list of struct pkg etc. supplying the data
+
 * @param format String with embedded %-escapes indicating what to output
+
 * @return count of the number of characters printed
+
 */
+
int
+
pkg_vasprintf(char **ret, const char * restrict format, va_list ap)
+
{
+
	struct sbuf	*sbuf;
+
	int		 count;
+

	sbuf  = sbuf_new_auto();

-
	va_start(ap, format);
	if (sbuf)
		sbuf = pkg_sbuf_vprintf(sbuf, format, ap);
-
	va_end(ap);
	if (sbuf && sbuf_len(sbuf) >= 0) {
		sbuf_finish(sbuf);
		count = asprintf(ret, "%s", sbuf_data(sbuf));