Remove common code from expandMacro() and expandThisMacro()
Add a mbInit() function that allocates the buffer, increases the expansion depth and stashes some values into a MacroExpansionData area. Add a mbFini() function that decreases the depth again, restores the values and optionally prints the expansion result.
This commit is contained in:
parent
a2e60c0a50
commit
98b71f7a92
|
@ -110,6 +110,15 @@ typedef struct MacroBuf_s {
|
||||||
rpmMacroContext mc;
|
rpmMacroContext mc;
|
||||||
} * MacroBuf;
|
} * MacroBuf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expansion data for a scoping level
|
||||||
|
*/
|
||||||
|
typedef struct MacroExpansionData_s {
|
||||||
|
size_t tpos;
|
||||||
|
int macro_trace;
|
||||||
|
int expand_trace;
|
||||||
|
} MacroExpansionData;
|
||||||
|
|
||||||
#define _MAX_MACRO_DEPTH 64
|
#define _MAX_MACRO_DEPTH 64
|
||||||
static int max_macro_depth = _MAX_MACRO_DEPTH;
|
static int max_macro_depth = _MAX_MACRO_DEPTH;
|
||||||
|
|
||||||
|
@ -463,6 +472,33 @@ static void mbAllocBuf(MacroBuf mb, size_t slen)
|
||||||
mb->nb = blen;
|
mb->nb = blen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int mbInit(MacroBuf mb, MacroExpansionData *med, size_t slen)
|
||||||
|
{
|
||||||
|
if (mb->buf == NULL)
|
||||||
|
mbAllocBuf(mb, slen);
|
||||||
|
if (++mb->depth > max_macro_depth) {
|
||||||
|
mbErr(mb, 1,
|
||||||
|
_("Too many levels of recursion in macro expansion. It is likely caused by recursive macro declaration.\n"));
|
||||||
|
mb->depth--;
|
||||||
|
mb->expand_trace = 1;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
med->tpos = mb->tpos; /* save expansion pointer for printExpand */
|
||||||
|
med->macro_trace = mb->macro_trace;
|
||||||
|
med->expand_trace = mb->expand_trace;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void mbFini(MacroBuf mb, MacroExpansionData *med)
|
||||||
|
{
|
||||||
|
mb->buf[mb->tpos] = '\0';
|
||||||
|
mb->depth--;
|
||||||
|
if (mb->error != 0 || mb->expand_trace)
|
||||||
|
printExpansion(mb, mb->buf + med->tpos, mb->buf + mb->tpos);
|
||||||
|
mb->macro_trace = med->macro_trace;
|
||||||
|
mb->expand_trace = med->expand_trace;
|
||||||
|
}
|
||||||
|
|
||||||
static void mbAppend(MacroBuf mb, char c)
|
static void mbAppend(MacroBuf mb, char c)
|
||||||
{
|
{
|
||||||
if (mb->nb < 1) {
|
if (mb->nb < 1) {
|
||||||
|
@ -1402,14 +1438,13 @@ expandMacro(MacroBuf mb, const char *src, size_t slen)
|
||||||
const char *s = src, *se;
|
const char *s = src, *se;
|
||||||
const char *f, *fe;
|
const char *f, *fe;
|
||||||
const char *g, *ge;
|
const char *g, *ge;
|
||||||
size_t fn, gn, tpos;
|
size_t fn, gn;
|
||||||
int c;
|
int c;
|
||||||
int negate;
|
int negate;
|
||||||
const char * lastc;
|
const char * lastc;
|
||||||
int chkexist;
|
int chkexist;
|
||||||
char *source = NULL;
|
char *source = NULL;
|
||||||
int store_macro_trace;
|
MacroExpansionData med;
|
||||||
int store_expand_trace;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Always make a (terminated) copy of the source string.
|
* Always make a (terminated) copy of the source string.
|
||||||
|
@ -1424,19 +1459,8 @@ expandMacro(MacroBuf mb, const char *src, size_t slen)
|
||||||
source[slen] = '\0';
|
source[slen] = '\0';
|
||||||
s = source;
|
s = source;
|
||||||
|
|
||||||
if (mb->buf == NULL)
|
if (mbInit(mb, &med, slen) != 0)
|
||||||
mbAllocBuf(mb, slen);
|
|
||||||
tpos = mb->tpos; /* save expansion pointer for printExpand */
|
|
||||||
store_macro_trace = mb->macro_trace;
|
|
||||||
store_expand_trace = mb->expand_trace;
|
|
||||||
|
|
||||||
if (++mb->depth > max_macro_depth) {
|
|
||||||
mbErr(mb, 1,
|
|
||||||
_("Too many levels of recursion in macro expansion. It is likely caused by recursive macro declaration.\n"));
|
|
||||||
mb->depth--;
|
|
||||||
mb->expand_trace = 1;
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
|
||||||
|
|
||||||
while (mb->error == 0 && (c = *s) != '\0') {
|
while (mb->error == 0 && (c = *s) != '\0') {
|
||||||
const struct builtins_s* builtin = NULL;
|
const struct builtins_s* builtin = NULL;
|
||||||
|
@ -1459,7 +1483,7 @@ expandMacro(MacroBuf mb, const char *src, size_t slen)
|
||||||
f = fe = NULL;
|
f = fe = NULL;
|
||||||
g = ge = NULL;
|
g = ge = NULL;
|
||||||
if (mb->depth > 1) /* XXX full expansion for outermost level */
|
if (mb->depth > 1) /* XXX full expansion for outermost level */
|
||||||
tpos = mb->tpos; /* save expansion pointer for printExpand */
|
med.tpos = mb->tpos; /* save expansion pointer for printExpand */
|
||||||
lastc = NULL;
|
lastc = NULL;
|
||||||
if ((se = findMacroEnd(s)) == NULL) {
|
if ((se = findMacroEnd(s)) == NULL) {
|
||||||
mbErr(mb, 1, _("Unterminated %c: %s\n"), (char)*s, s);
|
mbErr(mb, 1, _("Unterminated %c: %s\n"), (char)*s, s);
|
||||||
|
@ -1592,12 +1616,7 @@ expandMacro(MacroBuf mb, const char *src, size_t slen)
|
||||||
s = se;
|
s = se;
|
||||||
}
|
}
|
||||||
|
|
||||||
mb->buf[mb->tpos] = '\0';
|
mbFini(mb, &med);
|
||||||
mb->depth--;
|
|
||||||
if (mb->error != 0 || mb->expand_trace)
|
|
||||||
printExpansion(mb, mb->buf+tpos, mb->buf+mb->tpos);
|
|
||||||
mb->macro_trace = store_macro_trace;
|
|
||||||
mb->expand_trace = store_expand_trace;
|
|
||||||
exit:
|
exit:
|
||||||
_free(source);
|
_free(source);
|
||||||
return mb->error;
|
return mb->error;
|
||||||
|
@ -1614,21 +1633,11 @@ exit:
|
||||||
static int
|
static int
|
||||||
expandThisMacro(MacroBuf mb, rpmMacroEntry me, ARGV_const_t args, int flags)
|
expandThisMacro(MacroBuf mb, rpmMacroEntry me, ARGV_const_t args, int flags)
|
||||||
{
|
{
|
||||||
int store_macro_trace;
|
MacroExpansionData med;
|
||||||
int store_expand_trace;
|
|
||||||
size_t tpos;
|
|
||||||
ARGV_t optargs = NULL;
|
ARGV_t optargs = NULL;
|
||||||
|
|
||||||
if (mb->buf == NULL)
|
if (mbInit(mb, &med, 0) != 0)
|
||||||
mbAllocBuf(mb, 0);
|
|
||||||
|
|
||||||
if (++mb->depth > max_macro_depth) {
|
|
||||||
mbErr(mb, 1,
|
|
||||||
_("Too many levels of recursion in macro expansion. It is likely caused by recursive macro declaration.\n"));
|
|
||||||
mb->depth--;
|
|
||||||
mb->expand_trace = 1;
|
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
|
||||||
|
|
||||||
if (mb->macro_trace) {
|
if (mb->macro_trace) {
|
||||||
ARGV_const_t av = args;
|
ARGV_const_t av = args;
|
||||||
|
@ -1638,10 +1647,6 @@ expandThisMacro(MacroBuf mb, rpmMacroEntry me, ARGV_const_t args, int flags)
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
tpos = mb->tpos; /* save expansion pointer for printExpand */
|
|
||||||
store_macro_trace = mb->macro_trace;
|
|
||||||
store_expand_trace = mb->expand_trace;
|
|
||||||
|
|
||||||
/* prepare arguments for parametric macros */
|
/* prepare arguments for parametric macros */
|
||||||
if (me->opts) {
|
if (me->opts) {
|
||||||
argvAdd(&optargs, me->name);
|
argvAdd(&optargs, me->name);
|
||||||
|
@ -1662,13 +1667,7 @@ expandThisMacro(MacroBuf mb, rpmMacroEntry me, ARGV_const_t args, int flags)
|
||||||
if (optargs)
|
if (optargs)
|
||||||
argvFree(optargs);
|
argvFree(optargs);
|
||||||
|
|
||||||
mb->buf[mb->tpos] = '\0';
|
mbFini(mb, &med);
|
||||||
mb->depth--;
|
|
||||||
if (mb->error != 0 || mb->expand_trace)
|
|
||||||
printExpansion(mb, mb->buf+tpos, mb->buf+mb->tpos);
|
|
||||||
|
|
||||||
mb->macro_trace = store_macro_trace;
|
|
||||||
mb->expand_trace = store_expand_trace;
|
|
||||||
exit:
|
exit:
|
||||||
return mb->error;
|
return mb->error;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue