From: "Fabian Groffen" <grobian@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/, /
Date: Sun, 5 May 2019 18:13:27 +0000 (UTC) [thread overview]
Message-ID: <1557079947.70679e49ee7fadc209a87ae23860fedfd646dc10.grobian@gentoo> (raw)
commit: 70679e49ee7fadc209a87ae23860fedfd646dc10
Author: Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Sun May 5 18:12:27 2019 +0000
Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Sun May 5 18:12:27 2019 +0000
URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=70679e49
libq/atom: move atom_format here
move atom_print from qatom to libq/atom as atom_format so it can be
reused by multiple applets
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>
libq/atom.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
libq/atom.h | 4 ++
qatom.c | 94 +----------------------------------------
3 files changed, 137 insertions(+), 97 deletions(-)
diff --git a/libq/atom.c b/libq/atom.c
index 5f4b137..8eb9ac6 100644
--- a/libq/atom.c
+++ b/libq/atom.c
@@ -13,6 +13,7 @@
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
#include <ctype.h>
#include <xalloc.h>
@@ -569,12 +570,9 @@ implode_a1_ret:
/**
* Reconstructs an atom exactly like it was originally given (exploded).
*/
-static char _atom_buf[BUFSIZ];
char *
-atom_to_string(depend_atom *a)
+atom_to_string_r(char *buf, size_t buflen, depend_atom *a)
{
- char *buf = _atom_buf;
- size_t buflen = sizeof(_atom_buf);
size_t off = 0;
atom_usedep *ud;
@@ -606,3 +604,133 @@ atom_to_string(depend_atom *a)
return buf;
}
+
+/**
+ * Run printf on an atom. The format field takes the form:
+ * %{keyword}: Always display the field that matches "keyword" or <unset>
+ * %[keyword]: Only display the field when it's set (or pverbose)
+ * The possible "keywords" are:
+ * CATEGORY P PN PV PVR PF PR SLOT
+ * - these are all the standard portage variables (so see ebuild(5))
+ * pfx - the version qualifier if set (e.g. > < = !)
+ * sfx - the version qualifier if set (e.g. *)
+ */
+char *
+atom_format_r(
+ char *buf,
+ size_t buflen,
+ const char *format,
+ const depend_atom *atom,
+ int pverbose)
+{
+ char bracket;
+ const char *fmt;
+ const char *p;
+ size_t len;
+ bool showit;
+ char *ret;
+
+ if (!atom) {
+ snprintf(buf, buflen, "%s", "(NULL:atom)");
+ return buf;
+ }
+
+#define append_buf(B,L,FMT,...) \
+ { \
+ len = snprintf(B, L, FMT, __VA_ARGS__); \
+ L -= len; \
+ B += len; \
+ }
+ ret = buf;
+ p = format;
+ while (*p != '\0') {
+ fmt = strchr(p, '%');
+ if (fmt == NULL) {
+ if (buflen > 0)
+ *buf = '\0';
+ return buf;
+ } else if (fmt != p) {
+ append_buf(buf, buflen, "%.*s", (int)(fmt - p), p);
+ }
+
+ bracket = fmt[1];
+ if (bracket == '{' || bracket == '[') {
+ fmt += 2;
+ p = strchr(fmt, bracket == '{' ? '}' : ']');
+ if (p) {
+ len = p - fmt;
+ showit = (bracket == '{') || pverbose;
+#define HN(X) (X ? X : "<unset>")
+ if (!strncmp("CATEGORY", fmt, len)) {
+ if (showit || atom->CATEGORY)
+ append_buf(buf, buflen, "%s", HN(atom->CATEGORY));
+ } else if (!strncmp("P", fmt, len)) {
+ if (showit || atom->P)
+ append_buf(buf, buflen, "%s", HN(atom->P));
+ } else if (!strncmp("PN", fmt, len)) {
+ if (showit || atom->PN)
+ append_buf(buf, buflen, "%s", HN(atom->PN));
+ } else if (!strncmp("PV", fmt, len)) {
+ if (showit || atom->PV)
+ append_buf(buf, buflen, "%s", HN(atom->PV));
+ } else if (!strncmp("PVR", fmt, len)) {
+ if (showit || atom->PVR)
+ append_buf(buf, buflen, "%s", HN(atom->PVR));
+ } else if (!strncmp("PF", fmt, len)) {
+ append_buf(buf, buflen, "%s", atom->PN);
+ if (atom->PV)
+ append_buf(buf, buflen, "-%s", atom->PV);
+ if (atom->PR_int)
+ append_buf(buf, buflen,"-r%i", atom->PR_int);
+ } else if (!strncmp("PR", fmt, len)) {
+ if (showit || atom->PR_int)
+ append_buf(buf, buflen, "r%i", atom->PR_int);
+ } else if (!strncmp("SLOT", fmt, len)) {
+ if (showit || atom->SLOT)
+ append_buf(buf, buflen, "%s%s%s%s%s",
+ atom->SLOT ? ":" : "<unset>",
+ atom->SLOT ? atom->SLOT : "",
+ atom->SUBSLOT ? "/" : "",
+ atom->SUBSLOT ? atom->SUBSLOT : "",
+ atom_slotdep_str[atom->slotdep]);
+ } else if (!strncmp("REPO", fmt, len)) {
+ if (showit || atom->REPO)
+ append_buf(buf, buflen, "::%s", HN(atom->REPO));
+ } else if (!strncmp("pfx", fmt, len)) {
+ if (showit || atom->pfx_op != ATOM_OP_NONE)
+ append_buf(buf, buflen, "%s",
+ atom->pfx_op == ATOM_OP_NONE ?
+ "<unset>" : atom_op_str[atom->pfx_op]);
+ } else if (!strncmp("sfx", fmt, len)) {
+ if (showit || atom->sfx_op != ATOM_OP_NONE)
+ append_buf(buf, buflen, "%s",
+ atom->sfx_op == ATOM_OP_NONE ?
+ "<unset>" : atom_op_str[atom->sfx_op]);
+ } else
+ append_buf(buf, buflen, "<BAD:%.*s>", (int)len, fmt);
+ ++p;
+#undef HN
+ } else
+ p = fmt + 1;
+ } else
+ ++p;
+ }
+#undef append_buf
+
+ return ret;
+}
+
+/* versions that use an internal buffer, which is suitable for most
+ * scenarios */
+static char _atom_buf[BUFSIZ];
+char *
+atom_to_string(depend_atom *a)
+{
+ return atom_to_string_r(_atom_buf, sizeof(_atom_buf), a);
+}
+
+char *
+atom_format(const char *format, const depend_atom *atom, int pverbose)
+{
+ return atom_format_r(_atom_buf, sizeof(_atom_buf), format, atom, pverbose);
+}
diff --git a/libq/atom.h b/libq/atom.h
index 291d637..51ea88e 100644
--- a/libq/atom.h
+++ b/libq/atom.h
@@ -92,6 +92,10 @@ depend_atom *atom_explode(const char *atom);
void atom_implode(depend_atom *atom);
int atom_compare(const depend_atom *a1, const depend_atom *a2);
int atom_compare_str(const char * const s1, const char * const s2);
+char *atom_to_string_r(char *buf, size_t buflen, depend_atom *a);
+char *atom_format_r(char *buf, size_t buflen,
+ const char *format, const depend_atom *atom, int pverbose);
char *atom_to_string(depend_atom *a);
+char *atom_format(const char *format, const depend_atom *atom, int pverbose);
#endif
diff --git a/qatom.c b/qatom.c
index e3d2b0b..8825055 100644
--- a/qatom.c
+++ b/qatom.c
@@ -31,97 +31,6 @@ static const char * const qatom_opts_help[] = {
};
#define qatom_usage(ret) usage(ret, QATOM_FLAGS, qatom_long_opts, qatom_opts_help, NULL, lookup_applet_idx("qatom"))
-/* Run printf on an atom! The format field takes the form:
- * %{keyword}: Always display the field that matches "keyword"
- * %[keyword]: Only display the field when it's valid (or pverbose)
- * The possible "keywords" are:
- * CATEGORY P PN PV PVR PF PR SLOT
- * - these are all the standard portage variables (so see ebuild(5))
- * pfx - the version qualifier if set (e.g. > < = !)
- * sfx - the version qualifier if set (e.g. *)
- */
-static void
-qatom_printf(const char *format, const depend_atom *atom, int pverbose)
-{
- char bracket;
- const char *fmt, *p;
-
- if (!atom) {
- printf("(NULL:atom)");
- return;
- }
-
- p = format;
- while (*p != '\0') {
- fmt = strchr(p, '%');
- if (fmt == NULL) {
- printf("%s", p);
- return;
- } else if (fmt != p)
- printf("%.*s", (int)(fmt - p), p);
-
- bracket = fmt[1];
- if (bracket == '{' || bracket == '[') {
- fmt += 2;
- p = strchr(fmt, bracket == '{' ? '}' : ']');
- if (p) {
- size_t len = p - fmt;
- bool showit = (bracket == '{') || pverbose;
-#define HN(X) (X ? X : "<unset>")
- if (!strncmp("CATEGORY", fmt, len)) {
- if (showit || atom->CATEGORY)
- printf("%s", HN(atom->CATEGORY));
- } else if (!strncmp("P", fmt, len)) {
- if (showit || atom->P)
- printf("%s", HN(atom->P));
- } else if (!strncmp("PN", fmt, len)) {
- if (showit || atom->PN)
- printf("%s", HN(atom->PN));
- } else if (!strncmp("PV", fmt, len)) {
- if (showit || atom->PV)
- printf("%s", HN(atom->PV));
- } else if (!strncmp("PVR", fmt, len)) {
- if (showit || atom->PVR)
- printf("%s", HN(atom->PVR));
- } else if (!strncmp("PF", fmt, len)) {
- printf("%s", atom->PN);
- if (atom->PV)
- printf("-%s", atom->PV);
- if (atom->PR_int)
- printf("-r%i", atom->PR_int);
- } else if (!strncmp("PR", fmt, len)) {
- if (showit || atom->PR_int)
- printf("r%i", atom->PR_int);
- } else if (!strncmp("SLOT", fmt, len)) {
- if (showit || atom->SLOT)
- printf("%s%s%s%s%s",
- atom->SLOT ? ":" : "<unset>",
- atom->SLOT ? atom->SLOT : "",
- atom->SUBSLOT ? "/" : "",
- atom->SUBSLOT ? atom->SUBSLOT : "",
- atom_slotdep_str[atom->slotdep]);
- } else if (!strncmp("REPO", fmt, len)) {
- if (showit || atom->REPO)
- printf("::%s", HN(atom->REPO));
- } else if (!strncmp("pfx", fmt, len)) {
- if (showit || atom->pfx_op != ATOM_OP_NONE)
- printf("%s", atom->pfx_op == ATOM_OP_NONE ?
- "<unset>" : atom_op_str[atom->pfx_op]);
- } else if (!strncmp("sfx", fmt, len)) {
- if (showit || atom->sfx_op != ATOM_OP_NONE)
- printf("%s", atom->sfx_op == ATOM_OP_NONE ?
- "<unset>" : atom_op_str[atom->sfx_op]);
- } else
- printf("<BAD:%.*s>", (int)len, fmt);
- ++p;
-#undef HN
- } else
- p = fmt + 1;
- } else
- ++p;
- }
-}
-
int qatom_main(int argc, char **argv)
{
enum qatom_atom { _EXPLODE=0, _COMPARE, _PRINT } action = _EXPLODE;
@@ -168,8 +77,7 @@ int qatom_main(int argc, char **argv)
atom_implode(atomc);
break;
case _EXPLODE:
- qatom_printf(format, atom, verbose);
- putchar('\n');
+ printf("%s\n", atom_format(format, atom, verbose));
break;
case _PRINT:
printf("%s\n", atom_to_string(atom));
next reply other threads:[~2019-05-05 18:13 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-05-05 18:13 Fabian Groffen [this message]
-- strict thread matches above, loose matches on Subject: below --
2024-06-27 19:19 [gentoo-commits] proj/portage-utils:master commit in: libq/, / Fabian Groffen
2024-03-29 10:57 Fabian Groffen
2024-01-02 7:57 Fabian Groffen
2023-02-07 8:25 Fabian Groffen
2023-02-07 8:10 Fabian Groffen
2021-08-16 13:23 Fabian Groffen
2020-02-21 8:18 Fabian Groffen
2020-01-05 13:28 Fabian Groffen
2020-01-02 11:19 Fabian Groffen
2020-01-01 19:52 Fabian Groffen
2019-12-31 9:05 Fabian Groffen
2019-12-30 17:24 Fabian Groffen
2019-12-29 13:26 Fabian Groffen
2019-12-27 16:57 Fabian Groffen
2019-07-13 10:04 Fabian Groffen
2019-06-19 10:44 Fabian Groffen
2019-06-05 9:15 Fabian Groffen
2019-05-09 20:19 Fabian Groffen
2019-04-28 15:20 Fabian Groffen
2019-03-27 20:18 Fabian Groffen
2019-03-27 10:55 Fabian Groffen
2019-03-22 9:57 Fabian Groffen
2019-03-19 20:32 Fabian Groffen
2019-03-19 20:32 Fabian Groffen
2019-03-09 18:58 Fabian Groffen
2018-03-23 11:56 Fabian Groffen
2016-12-29 2:25 Mike Frysinger
2016-11-26 23:17 Mike Frysinger
2015-11-28 2:44 Mike Frysinger
2015-02-24 1:26 Mike Frysinger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1557079947.70679e49ee7fadc209a87ae23860fedfd646dc10.grobian@gentoo \
--to=grobian@gentoo.org \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox