Gentoo Archives: gentoo-commits

From: Fabian Groffen <grobian@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/, /
Date: Sun, 05 May 2019 18:13:32
Message-Id: 1557079947.70679e49ee7fadc209a87ae23860fedfd646dc10.grobian@gentoo
1 commit: 70679e49ee7fadc209a87ae23860fedfd646dc10
2 Author: Fabian Groffen <grobian <AT> gentoo <DOT> org>
3 AuthorDate: Sun May 5 18:12:27 2019 +0000
4 Commit: Fabian Groffen <grobian <AT> gentoo <DOT> org>
5 CommitDate: Sun May 5 18:12:27 2019 +0000
6 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=70679e49
7
8 libq/atom: move atom_format here
9
10 move atom_print from qatom to libq/atom as atom_format so it can be
11 reused by multiple applets
12
13 Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>
14
15 libq/atom.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
16 libq/atom.h | 4 ++
17 qatom.c | 94 +----------------------------------------
18 3 files changed, 137 insertions(+), 97 deletions(-)
19
20 diff --git a/libq/atom.c b/libq/atom.c
21 index 5f4b137..8eb9ac6 100644
22 --- a/libq/atom.c
23 +++ b/libq/atom.c
24 @@ -13,6 +13,7 @@
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <string.h>
28 +#include <stdbool.h>
29 #include <ctype.h>
30 #include <xalloc.h>
31
32 @@ -569,12 +570,9 @@ implode_a1_ret:
33 /**
34 * Reconstructs an atom exactly like it was originally given (exploded).
35 */
36 -static char _atom_buf[BUFSIZ];
37 char *
38 -atom_to_string(depend_atom *a)
39 +atom_to_string_r(char *buf, size_t buflen, depend_atom *a)
40 {
41 - char *buf = _atom_buf;
42 - size_t buflen = sizeof(_atom_buf);
43 size_t off = 0;
44 atom_usedep *ud;
45
46 @@ -606,3 +604,133 @@ atom_to_string(depend_atom *a)
47
48 return buf;
49 }
50 +
51 +/**
52 + * Run printf on an atom. The format field takes the form:
53 + * %{keyword}: Always display the field that matches "keyword" or <unset>
54 + * %[keyword]: Only display the field when it's set (or pverbose)
55 + * The possible "keywords" are:
56 + * CATEGORY P PN PV PVR PF PR SLOT
57 + * - these are all the standard portage variables (so see ebuild(5))
58 + * pfx - the version qualifier if set (e.g. > < = !)
59 + * sfx - the version qualifier if set (e.g. *)
60 + */
61 +char *
62 +atom_format_r(
63 + char *buf,
64 + size_t buflen,
65 + const char *format,
66 + const depend_atom *atom,
67 + int pverbose)
68 +{
69 + char bracket;
70 + const char *fmt;
71 + const char *p;
72 + size_t len;
73 + bool showit;
74 + char *ret;
75 +
76 + if (!atom) {
77 + snprintf(buf, buflen, "%s", "(NULL:atom)");
78 + return buf;
79 + }
80 +
81 +#define append_buf(B,L,FMT,...) \
82 + { \
83 + len = snprintf(B, L, FMT, __VA_ARGS__); \
84 + L -= len; \
85 + B += len; \
86 + }
87 + ret = buf;
88 + p = format;
89 + while (*p != '\0') {
90 + fmt = strchr(p, '%');
91 + if (fmt == NULL) {
92 + if (buflen > 0)
93 + *buf = '\0';
94 + return buf;
95 + } else if (fmt != p) {
96 + append_buf(buf, buflen, "%.*s", (int)(fmt - p), p);
97 + }
98 +
99 + bracket = fmt[1];
100 + if (bracket == '{' || bracket == '[') {
101 + fmt += 2;
102 + p = strchr(fmt, bracket == '{' ? '}' : ']');
103 + if (p) {
104 + len = p - fmt;
105 + showit = (bracket == '{') || pverbose;
106 +#define HN(X) (X ? X : "<unset>")
107 + if (!strncmp("CATEGORY", fmt, len)) {
108 + if (showit || atom->CATEGORY)
109 + append_buf(buf, buflen, "%s", HN(atom->CATEGORY));
110 + } else if (!strncmp("P", fmt, len)) {
111 + if (showit || atom->P)
112 + append_buf(buf, buflen, "%s", HN(atom->P));
113 + } else if (!strncmp("PN", fmt, len)) {
114 + if (showit || atom->PN)
115 + append_buf(buf, buflen, "%s", HN(atom->PN));
116 + } else if (!strncmp("PV", fmt, len)) {
117 + if (showit || atom->PV)
118 + append_buf(buf, buflen, "%s", HN(atom->PV));
119 + } else if (!strncmp("PVR", fmt, len)) {
120 + if (showit || atom->PVR)
121 + append_buf(buf, buflen, "%s", HN(atom->PVR));
122 + } else if (!strncmp("PF", fmt, len)) {
123 + append_buf(buf, buflen, "%s", atom->PN);
124 + if (atom->PV)
125 + append_buf(buf, buflen, "-%s", atom->PV);
126 + if (atom->PR_int)
127 + append_buf(buf, buflen,"-r%i", atom->PR_int);
128 + } else if (!strncmp("PR", fmt, len)) {
129 + if (showit || atom->PR_int)
130 + append_buf(buf, buflen, "r%i", atom->PR_int);
131 + } else if (!strncmp("SLOT", fmt, len)) {
132 + if (showit || atom->SLOT)
133 + append_buf(buf, buflen, "%s%s%s%s%s",
134 + atom->SLOT ? ":" : "<unset>",
135 + atom->SLOT ? atom->SLOT : "",
136 + atom->SUBSLOT ? "/" : "",
137 + atom->SUBSLOT ? atom->SUBSLOT : "",
138 + atom_slotdep_str[atom->slotdep]);
139 + } else if (!strncmp("REPO", fmt, len)) {
140 + if (showit || atom->REPO)
141 + append_buf(buf, buflen, "::%s", HN(atom->REPO));
142 + } else if (!strncmp("pfx", fmt, len)) {
143 + if (showit || atom->pfx_op != ATOM_OP_NONE)
144 + append_buf(buf, buflen, "%s",
145 + atom->pfx_op == ATOM_OP_NONE ?
146 + "<unset>" : atom_op_str[atom->pfx_op]);
147 + } else if (!strncmp("sfx", fmt, len)) {
148 + if (showit || atom->sfx_op != ATOM_OP_NONE)
149 + append_buf(buf, buflen, "%s",
150 + atom->sfx_op == ATOM_OP_NONE ?
151 + "<unset>" : atom_op_str[atom->sfx_op]);
152 + } else
153 + append_buf(buf, buflen, "<BAD:%.*s>", (int)len, fmt);
154 + ++p;
155 +#undef HN
156 + } else
157 + p = fmt + 1;
158 + } else
159 + ++p;
160 + }
161 +#undef append_buf
162 +
163 + return ret;
164 +}
165 +
166 +/* versions that use an internal buffer, which is suitable for most
167 + * scenarios */
168 +static char _atom_buf[BUFSIZ];
169 +char *
170 +atom_to_string(depend_atom *a)
171 +{
172 + return atom_to_string_r(_atom_buf, sizeof(_atom_buf), a);
173 +}
174 +
175 +char *
176 +atom_format(const char *format, const depend_atom *atom, int pverbose)
177 +{
178 + return atom_format_r(_atom_buf, sizeof(_atom_buf), format, atom, pverbose);
179 +}
180
181 diff --git a/libq/atom.h b/libq/atom.h
182 index 291d637..51ea88e 100644
183 --- a/libq/atom.h
184 +++ b/libq/atom.h
185 @@ -92,6 +92,10 @@ depend_atom *atom_explode(const char *atom);
186 void atom_implode(depend_atom *atom);
187 int atom_compare(const depend_atom *a1, const depend_atom *a2);
188 int atom_compare_str(const char * const s1, const char * const s2);
189 +char *atom_to_string_r(char *buf, size_t buflen, depend_atom *a);
190 +char *atom_format_r(char *buf, size_t buflen,
191 + const char *format, const depend_atom *atom, int pverbose);
192 char *atom_to_string(depend_atom *a);
193 +char *atom_format(const char *format, const depend_atom *atom, int pverbose);
194
195 #endif
196
197 diff --git a/qatom.c b/qatom.c
198 index e3d2b0b..8825055 100644
199 --- a/qatom.c
200 +++ b/qatom.c
201 @@ -31,97 +31,6 @@ static const char * const qatom_opts_help[] = {
202 };
203 #define qatom_usage(ret) usage(ret, QATOM_FLAGS, qatom_long_opts, qatom_opts_help, NULL, lookup_applet_idx("qatom"))
204
205 -/* Run printf on an atom! The format field takes the form:
206 - * %{keyword}: Always display the field that matches "keyword"
207 - * %[keyword]: Only display the field when it's valid (or pverbose)
208 - * The possible "keywords" are:
209 - * CATEGORY P PN PV PVR PF PR SLOT
210 - * - these are all the standard portage variables (so see ebuild(5))
211 - * pfx - the version qualifier if set (e.g. > < = !)
212 - * sfx - the version qualifier if set (e.g. *)
213 - */
214 -static void
215 -qatom_printf(const char *format, const depend_atom *atom, int pverbose)
216 -{
217 - char bracket;
218 - const char *fmt, *p;
219 -
220 - if (!atom) {
221 - printf("(NULL:atom)");
222 - return;
223 - }
224 -
225 - p = format;
226 - while (*p != '\0') {
227 - fmt = strchr(p, '%');
228 - if (fmt == NULL) {
229 - printf("%s", p);
230 - return;
231 - } else if (fmt != p)
232 - printf("%.*s", (int)(fmt - p), p);
233 -
234 - bracket = fmt[1];
235 - if (bracket == '{' || bracket == '[') {
236 - fmt += 2;
237 - p = strchr(fmt, bracket == '{' ? '}' : ']');
238 - if (p) {
239 - size_t len = p - fmt;
240 - bool showit = (bracket == '{') || pverbose;
241 -#define HN(X) (X ? X : "<unset>")
242 - if (!strncmp("CATEGORY", fmt, len)) {
243 - if (showit || atom->CATEGORY)
244 - printf("%s", HN(atom->CATEGORY));
245 - } else if (!strncmp("P", fmt, len)) {
246 - if (showit || atom->P)
247 - printf("%s", HN(atom->P));
248 - } else if (!strncmp("PN", fmt, len)) {
249 - if (showit || atom->PN)
250 - printf("%s", HN(atom->PN));
251 - } else if (!strncmp("PV", fmt, len)) {
252 - if (showit || atom->PV)
253 - printf("%s", HN(atom->PV));
254 - } else if (!strncmp("PVR", fmt, len)) {
255 - if (showit || atom->PVR)
256 - printf("%s", HN(atom->PVR));
257 - } else if (!strncmp("PF", fmt, len)) {
258 - printf("%s", atom->PN);
259 - if (atom->PV)
260 - printf("-%s", atom->PV);
261 - if (atom->PR_int)
262 - printf("-r%i", atom->PR_int);
263 - } else if (!strncmp("PR", fmt, len)) {
264 - if (showit || atom->PR_int)
265 - printf("r%i", atom->PR_int);
266 - } else if (!strncmp("SLOT", fmt, len)) {
267 - if (showit || atom->SLOT)
268 - printf("%s%s%s%s%s",
269 - atom->SLOT ? ":" : "<unset>",
270 - atom->SLOT ? atom->SLOT : "",
271 - atom->SUBSLOT ? "/" : "",
272 - atom->SUBSLOT ? atom->SUBSLOT : "",
273 - atom_slotdep_str[atom->slotdep]);
274 - } else if (!strncmp("REPO", fmt, len)) {
275 - if (showit || atom->REPO)
276 - printf("::%s", HN(atom->REPO));
277 - } else if (!strncmp("pfx", fmt, len)) {
278 - if (showit || atom->pfx_op != ATOM_OP_NONE)
279 - printf("%s", atom->pfx_op == ATOM_OP_NONE ?
280 - "<unset>" : atom_op_str[atom->pfx_op]);
281 - } else if (!strncmp("sfx", fmt, len)) {
282 - if (showit || atom->sfx_op != ATOM_OP_NONE)
283 - printf("%s", atom->sfx_op == ATOM_OP_NONE ?
284 - "<unset>" : atom_op_str[atom->sfx_op]);
285 - } else
286 - printf("<BAD:%.*s>", (int)len, fmt);
287 - ++p;
288 -#undef HN
289 - } else
290 - p = fmt + 1;
291 - } else
292 - ++p;
293 - }
294 -}
295 -
296 int qatom_main(int argc, char **argv)
297 {
298 enum qatom_atom { _EXPLODE=0, _COMPARE, _PRINT } action = _EXPLODE;
299 @@ -168,8 +77,7 @@ int qatom_main(int argc, char **argv)
300 atom_implode(atomc);
301 break;
302 case _EXPLODE:
303 - qatom_printf(format, atom, verbose);
304 - putchar('\n');
305 + printf("%s\n", atom_format(format, atom, verbose));
306 break;
307 case _PRINT:
308 printf("%s\n", atom_to_string(atom));