* Macro expansion state.
*/
typedef struct MacroBuf_s {
- const char * s; /*!< Text to expand. */
char * t; /*!< Expansion buffer. */
size_t nb; /*!< No. bytes remaining in expansion buffer. */
int depth; /*!< Current expansion depth. */
#define MACRO_CHUNK_SIZE 16
/* forward ref */
-static int expandMacro(MacroBuf mb);
+static int expandMacro(MacroBuf mb, const char *src);
/* =============================================================== */
static int
expandT(MacroBuf mb, const char * f, size_t flen)
{
- char *sbuf;
- const char *s = mb->s;
+ char *sbuf = xcalloc(flen + 1, sizeof(*sbuf));
int rc;
- sbuf = xcalloc(flen + 1, sizeof(*sbuf));
-
strncpy(sbuf, f, flen);
sbuf[flen] = '\0';
- mb->s = sbuf;
- rc = expandMacro(mb);
- mb->s = s;
+ rc = expandMacro(mb, sbuf);
_free(sbuf);
static int
expandU(MacroBuf mb, char * u, size_t ulen)
{
- const char *s = mb->s;
char *t = mb->t;
size_t nb = mb->nb;
char *tbuf;
tbuf = xcalloc(ulen + 1, sizeof(*tbuf));
- mb->s = u;
mb->t = tbuf;
mb->nb = ulen;
- rc = expandMacro(mb);
+ rc = expandMacro(mb, u);
tbuf[ulen] = '\0'; /* XXX just in case */
if (ulen > mb->nb)
strncpy(u, tbuf, (ulen - mb->nb + 1));
- mb->s = s;
mb->t = t;
mb->nb = nb;
* The main macro recursion loop.
* @todo Dynamically reallocate target buffer.
* @param mb macro expansion state
+ * @param src string to expand
* @return 0 on success, 1 on failure
*/
static int
-expandMacro(MacroBuf mb)
+expandMacro(MacroBuf mb, const char *src)
{
rpmMacroEntry *mep;
rpmMacroEntry me;
- const char *s = mb->s, *se;
+ const char *s = src, *se;
const char *f, *fe;
const char *g, *ge;
size_t fn, gn;
/* Recursively expand body of macro */
if (me->body && *me->body) {
- mb->s = me->body;
- rc = expandMacro(mb);
+ rc = expandMacro(mb, me->body);
if (rc == 0)
me->used++; /* Mark macro as used */
}
}
*mb->t = '\0';
- mb->s = s;
mb->depth--;
if (rc != 0 || mb->expand_trace)
printExpansion(mb, t, mb->t);
tbuf = xcalloc(slen + 1, sizeof(*tbuf));
- mb->s = sbuf;
mb->t = tbuf;
mb->nb = slen;
mb->depth = 0;
mb->spec = spec; /* (future) %file expansion info */
mb->mc = mc;
- rc = expandMacro(mb);
+ rc = expandMacro(mb, sbuf);
if (mb->nb == 0)
rpmlog(RPMLOG_ERR, _("Target buffer overflow\n"));