Pass their own macro entry as an argument (ie self) to all builtins

No intended functional changes, but it's one argument less to pass
around, brings builtins one step closer to how user defined
macros are called and is a step towards supporting options to
builtins as well.
This commit is contained in:
Panu Matilainen 2020-11-03 14:27:46 +02:00
parent ded0282b05
commit fc31fec661
1 changed files with 51 additions and 55 deletions

View File

@ -15,8 +15,6 @@
#endif #endif
#define iseol(_c) ((_c) == '\n' || (_c) == '\r') #define iseol(_c) ((_c) == '\n' || (_c) == '\r')
#define STREQ(_t, _f, _fn) ((_fn) == (sizeof(_t)-1) && rstreqn((_t), (_f), (_fn)))
#define MACROBUFSIZ (BUFSIZ * 2) #define MACROBUFSIZ (BUFSIZ * 2)
#include <rpm/rpmio.h> #include <rpm/rpmio.h>
@ -136,8 +134,8 @@ static int print_macro_trace = _PRINT_MACRO_TRACE;
static int print_expand_trace = _PRINT_EXPAND_TRACE; static int print_expand_trace = _PRINT_EXPAND_TRACE;
typedef void (*macroFunc)(MacroBuf mb, int chkexist, int negate, typedef void (*macroFunc)(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
typedef const char *(*parseFunc)(MacroBuf mb, const char * se); typedef const char *(*parseFunc)(MacroBuf mb, rpmMacroEntry me, const char * se);
/* forward ref */ /* forward ref */
static int expandMacro(MacroBuf mb, const char *src, size_t slen); static int expandMacro(MacroBuf mb, const char *src, size_t slen);
@ -146,30 +144,30 @@ static void pushMacro(rpmMacroContext mc,
static void popMacro(rpmMacroContext mc, const char * n); static void popMacro(rpmMacroContext mc, const char * n);
static int loadMacroFile(rpmMacroContext mc, const char * fn); static int loadMacroFile(rpmMacroContext mc, const char * fn);
static void doBody(MacroBuf mb, int chkexist, int negate, static void doBody(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doExpand(MacroBuf mb, int chkexist, int negate, static void doExpand(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doFoo(MacroBuf mb, int chkexist, int negate, static void doFoo(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doLoad(MacroBuf mb, int chkexist, int negate, static void doLoad(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doLua(MacroBuf mb, int chkexist, int negate, static void doLua(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doOutput(MacroBuf mb, int chkexist, int negate, static void doOutput(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doSP(MacroBuf mb, int chkexist, int negate, static void doSP(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doTrace(MacroBuf mb, int chkexist, int negate, static void doTrace(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doUncompress(MacroBuf mb, int chkexist, int negate, static void doUncompress(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static void doVerbose(MacroBuf mb, int chkexist, int negate, static void doVerbose(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn); rpmMacroEntry me, const char * g, size_t gn);
static const char * doDef(MacroBuf mb, const char * se); static const char * doDef(MacroBuf mb, rpmMacroEntry me, const char * se);
static const char * doGlobal(MacroBuf mb, const char * se); static const char * doGlobal(MacroBuf mb, rpmMacroEntry me, const char * se);
static const char * doDump(MacroBuf mb, const char * se); static const char * doDump(MacroBuf mb, rpmMacroEntry me, const char * se);
static const char * doUndefine(MacroBuf mb, const char * se); static const char * doUndefine(MacroBuf mb, rpmMacroEntry me, const char * se);
/* =============================================================== */ /* =============================================================== */
static rpmMacroContext rpmmctxAcquire(rpmMacroContext mc) static rpmMacroContext rpmmctxAcquire(rpmMacroContext mc)
@ -529,7 +527,7 @@ static void mbAppendStr(MacroBuf mb, const char *str)
mb->nb -= len; mb->nb -= len;
} }
static const char * doDnl(MacroBuf mb, const char * se) static const char * doDnl(MacroBuf mb, rpmMacroEntry me, const char * se)
{ {
const char *s = se; const char *s = se;
while (*s && !iseol(*s)) while (*s && !iseol(*s))
@ -813,7 +811,7 @@ exit:
* @return address to continue parsing * @return address to continue parsing
*/ */
static const char * static const char *
doUndefine(MacroBuf mb, const char * se) doUndefine(MacroBuf mb, rpmMacroEntry me, const char * se)
{ {
const char *s = se; const char *s = se;
char *buf = xmalloc(strlen(s) + 1); char *buf = xmalloc(strlen(s) + 1);
@ -839,17 +837,17 @@ exit:
return se; return se;
} }
static const char * doDef(MacroBuf mb, const char * se) static const char * doDef(MacroBuf mb, rpmMacroEntry me, const char * se)
{ {
return doDefine(mb, se, mb->level, 0); return doDefine(mb, se, mb->level, 0);
} }
static const char * doGlobal(MacroBuf mb, const char * se) static const char * doGlobal(MacroBuf mb, rpmMacroEntry me, const char * se)
{ {
return doDefine(mb, se, RMIL_GLOBAL, 1); return doDefine(mb, se, RMIL_GLOBAL, 1);
} }
static const char * doDump(MacroBuf mb, const char * se) static const char * doDump(MacroBuf mb, rpmMacroEntry me, const char * se)
{ {
rpmDumpMacroTable(mb->mc, NULL); rpmDumpMacroTable(mb->mc, NULL);
while (iseol(*se)) while (iseol(*se))
@ -1038,7 +1036,7 @@ grabArgs(MacroBuf mb, ARGV_t *argvp, const char * se,
} }
static void doBody(MacroBuf mb, int chkexist, int negate, static void doBody(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
if (gn > 0) { if (gn > 0) {
char *buf = NULL; char *buf = NULL;
@ -1054,14 +1052,14 @@ static void doBody(MacroBuf mb, int chkexist, int negate,
} }
} }
static void doOutput(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn, const char * g, size_t gn) static void doOutput(MacroBuf mb, int chkexist, int negate, rpmMacroEntry me, const char * g, size_t gn)
{ {
char *buf = NULL; char *buf = NULL;
int loglevel = RPMLOG_NOTICE; /* assume echo */ int loglevel = RPMLOG_NOTICE; /* assume echo */
if (STREQ("error", f, fn)) { if (rstreq("error", me->name)) {
loglevel = RPMLOG_ERR; loglevel = RPMLOG_ERR;
mb->error = 1; mb->error = 1;
} else if (STREQ("warn", f, fn)) { } else if (rstreq("warn", me->name)) {
loglevel = RPMLOG_WARNING; loglevel = RPMLOG_WARNING;
} }
if (gn == 0) if (gn == 0)
@ -1072,23 +1070,23 @@ static void doOutput(MacroBuf mb, int chkexist, int negate, const char * f, size
_free(buf); _free(buf);
} }
static void doLua(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn, const char * g, size_t gn) static void doLua(MacroBuf mb, int chkexist, int negate, rpmMacroEntry me, const char * g, size_t gn)
{ {
#ifdef WITH_LUA #ifdef WITH_LUA
rpmlua lua = NULL; /* Global state. */ rpmlua lua = NULL; /* Global state. */
char *scriptbuf = xmalloc(gn + 1); char *scriptbuf = xmalloc(gn + 1);
char *printbuf; char *printbuf;
rpmMacroContext mc = mb->mc; rpmMacroContext mc = mb->mc;
rpmMacroEntry me = mb->me; rpmMacroEntry mbme = mb->me;
int odepth = mc->depth; int odepth = mc->depth;
int olevel = mc->level; int olevel = mc->level;
const char *opts = NULL; const char *opts = NULL;
const char *name = NULL; const char *name = NULL;
ARGV_t args = NULL; ARGV_t args = NULL;
if (me) { if (mbme) {
opts = me->opts; opts = mbme->opts;
name = me->name; name = mbme->name;
if (mb->args) if (mb->args)
args = mb->args; args = mb->args;
} }
@ -1116,7 +1114,7 @@ static void doLua(MacroBuf mb, int chkexist, int negate, const char * f, size_t
static void static void
doSP(MacroBuf mb, int chkexist, int negate, doSP(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
const char *b = ""; const char *b = "";
char *buf = NULL; char *buf = NULL;
@ -1127,14 +1125,14 @@ doSP(MacroBuf mb, int chkexist, int negate,
b = buf; b = buf;
} }
s = rstrscat(NULL, (*f == 'S') ? "%SOURCE" : "%PATCH", b, NULL); s = rstrscat(NULL, (*(me->name) == 'S') ? "%SOURCE" : "%PATCH", b, NULL);
expandMacro(mb, s, 0); expandMacro(mb, s, 0);
free(s); free(s);
free(buf); free(buf);
} }
static void doUncompress(MacroBuf mb, int chkexist, int negate, static void doUncompress(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
rpmCompressedMagic compressed = COMPRESSED_OTHER; rpmCompressedMagic compressed = COMPRESSED_OTHER;
char *b, *be, *buf = NULL; char *b, *be, *buf = NULL;
@ -1193,7 +1191,7 @@ exit:
} }
static void doExpand(MacroBuf mb, int chkexist, int negate, static void doExpand(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
if (gn > 0) { if (gn > 0) {
char *buf; char *buf;
@ -1204,7 +1202,7 @@ static void doExpand(MacroBuf mb, int chkexist, int negate,
} }
static void doVerbose(MacroBuf mb, int chkexist, int negate, static void doVerbose(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
int verbose = (rpmIsVerbose() != 0); int verbose = (rpmIsVerbose() != 0);
/* Don't expand %{verbose:...} argument on false condition */ /* Don't expand %{verbose:...} argument on false condition */
@ -1221,13 +1219,11 @@ static void doVerbose(MacroBuf mb, int chkexist, int negate,
* @param mb macro expansion state * @param mb macro expansion state
* @param chkexist unused * @param chkexist unused
* @param negate should logic be inverted? * @param negate should logic be inverted?
* @param f beginning of field f
* @param fn length of field f
* @param g beginning of field g * @param g beginning of field g
* @param gn length of field g * @param gn length of field g
*/ */
static void static void
doFoo(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn, doFoo(MacroBuf mb, int chkexist, int negate, rpmMacroEntry me,
const char * g, size_t gn) const char * g, size_t gn)
{ {
char *buf = NULL; char *buf = NULL;
@ -1237,19 +1233,19 @@ doFoo(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn,
if (expand) { if (expand) {
(void) expandThis(mb, g, gn, &buf); (void) expandThis(mb, g, gn, &buf);
} else { } else {
buf = xmalloc(MACROBUFSIZ + fn + gn); buf = xmalloc(MACROBUFSIZ + strlen(me->name) + gn);
buf[0] = '\0'; buf[0] = '\0';
} }
if (STREQ("basename", f, fn)) { if (rstreq("basename", me->name)) {
if ((b = strrchr(buf, '/')) == NULL) if ((b = strrchr(buf, '/')) == NULL)
b = buf; b = buf;
else else
b++; b++;
} else if (STREQ("dirname", f, fn)) { } else if (rstreq("dirname", me->name)) {
if ((b = strrchr(buf, '/')) != NULL) if ((b = strrchr(buf, '/')) != NULL)
*b = '\0'; *b = '\0';
b = buf; b = buf;
} else if (STREQ("shrink", f, fn)) { } else if (rstreq("shrink", me->name)) {
/* /*
* shrink body by removing all leading and trailing whitespaces and * shrink body by removing all leading and trailing whitespaces and
* reducing intermediate whitespaces to a single space character. * reducing intermediate whitespaces to a single space character.
@ -1271,15 +1267,15 @@ doFoo(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn,
} }
buf[j] = '\0'; buf[j] = '\0';
b = buf; b = buf;
} else if (STREQ("quote", f, fn)) { } else if (rstreq("quote", me->name)) {
char *quoted = NULL; char *quoted = NULL;
rasprintf(&quoted, "%c%s%c", 0x1f, buf, 0x1f); rasprintf(&quoted, "%c%s%c", 0x1f, buf, 0x1f);
free(buf); free(buf);
b = buf = quoted; b = buf = quoted;
} else if (STREQ("suffix", f, fn)) { } else if (rstreq("suffix", me->name)) {
if ((b = strrchr(buf, '.')) != NULL) if ((b = strrchr(buf, '.')) != NULL)
b++; b++;
} else if (STREQ("expr", f, fn)) { } else if (rstreq("expr", me->name)) {
char *expr = rpmExprStrFlags(buf, 0); char *expr = rpmExprStrFlags(buf, 0);
if (expr) { if (expr) {
free(buf); free(buf);
@ -1287,15 +1283,15 @@ doFoo(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn,
} else { } else {
mb->error = 1; mb->error = 1;
} }
} else if (STREQ("url2path", f, fn) || STREQ("u2p", f, fn)) { } else if (rstreq("url2path", me->name) || rstreq("u2p", me->name)) {
(void)urlPath(buf, (const char **)&b); (void)urlPath(buf, (const char **)&b);
if (*b == '\0') b = "/"; if (*b == '\0') b = "/";
} else if (STREQ("getenv", f, fn)) { } else if (rstreq("getenv", me->name)) {
b = getenv(buf); b = getenv(buf);
} else if (STREQ("getconfdir", f, fn)) { } else if (rstreq("getconfdir", me->name)) {
sprintf(buf, "%s", rpmConfigDir()); sprintf(buf, "%s", rpmConfigDir());
b = buf; b = buf;
} else if (STREQ("getncpus", f, fn)) { } else if (rstreq("getncpus", me->name)) {
sprintf(buf, "%u", getncpus()); sprintf(buf, "%u", getncpus());
b = buf; b = buf;
} }
@ -1307,7 +1303,7 @@ doFoo(MacroBuf mb, int chkexist, int negate, const char * f, size_t fn,
} }
static void doLoad(MacroBuf mb, int chkexist, int negate, static void doLoad(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
char *arg = NULL; char *arg = NULL;
if (g && gn > 0 && expandThis(mb, g, gn, &arg) == 0) { if (g && gn > 0 && expandThis(mb, g, gn, &arg) == 0) {
@ -1320,7 +1316,7 @@ static void doLoad(MacroBuf mb, int chkexist, int negate,
} }
static void doTrace(MacroBuf mb, int chkexist, int negate, static void doTrace(MacroBuf mb, int chkexist, int negate,
const char * f, size_t fn, const char * g, size_t gn) rpmMacroEntry me, const char * g, size_t gn)
{ {
mb->expand_trace = mb->macro_trace = (negate ? 0 : mb->depth); mb->expand_trace = mb->macro_trace = (negate ? 0 : mb->depth);
if (mb->depth == 1) { if (mb->depth == 1) {
@ -1552,10 +1548,10 @@ expandMacro(MacroBuf mb, const char *src, size_t slen)
} }
if (me->flags & ME_PARSE) { if (me->flags & ME_PARSE) {
parseFunc parse = me->func; parseFunc parse = me->func;
s = parse(mb, se); s = parse(mb, me, se);
} else { } else {
macroFunc func = me->func; macroFunc func = me->func;
func(mb, chkexist, negate, f, fn, g, gn); func(mb, chkexist, negate, me, g, gn);
s = se; s = se;
} }
continue; continue;