rpmExpandMacros() is modified to be able to return more return codes

Now rpmExpandMacros() returns integer as a return value. Negative return
value implies failure. Return values can be more utilized in the future.
E. g. return value could specify how many macros were expanded in given
string.
This commit is contained in:
Lubos Kardos 2015-11-26 15:20:05 +01:00
parent aee8446eb4
commit ddf9ec7bef
5 changed files with 22 additions and 23 deletions

View File

@ -1604,6 +1604,7 @@ static rpmRC readFilesManifest(rpmSpec spec, Package pkg, const char *path)
FILE *fd = NULL; FILE *fd = NULL;
rpmRC rc = RPMRC_FAIL; rpmRC rc = RPMRC_FAIL;
unsigned int nlines = 0; unsigned int nlines = 0;
char *expanded;
if (*path == '/') { if (*path == '/') {
fn = rpmGetPath(path, NULL); fn = rpmGetPath(path, NULL);
@ -1624,8 +1625,7 @@ static rpmRC readFilesManifest(rpmSpec spec, Package pkg, const char *path)
while (fgets(buf, sizeof(buf), fd)) { while (fgets(buf, sizeof(buf), fd)) {
if (handleComments(buf)) if (handleComments(buf))
continue; continue;
char *expanded = rpmExpandMacros(spec->macros, buf, 0); if(rpmExpandMacros(spec->macros, buf, &expanded, 0) < 0) {
if (expanded == NULL) {
rpmlog(RPMLOG_ERR, _("line: %s\n"), buf); rpmlog(RPMLOG_ERR, _("line: %s\n"), buf);
goto exit; goto exit;
} }

View File

@ -132,8 +132,8 @@ static rpmRC addFileToTag(rpmSpec spec, const char * file,
} }
while (fgets(buf, sizeof(buf), f)) { while (fgets(buf, sizeof(buf), f)) {
char *expanded = rpmExpandMacros(spec->macros, buf, 0); char *expanded;
if (expanded == NULL) { if(rpmExpandMacros(spec->macros, buf, &expanded, 0) < 0) {
rpmlog(RPMLOG_ERR, _("%s: line: %s\n"), fn, buf); rpmlog(RPMLOG_ERR, _("%s: line: %s\n"), fn, buf);
goto exit; goto exit;
} }

View File

@ -169,7 +169,7 @@ static int restoreFirstChar(rpmSpec spec)
static int expandMacrosInSpecBuf(rpmSpec spec, int strip) static int expandMacrosInSpecBuf(rpmSpec spec, int strip)
{ {
char *lbuf = NULL; char *lbuf = NULL;
int rc = 0, isComment = 0; int isComment = 0;
/* Don't expand macros (eg. %define) in false branch of %if clause */ /* Don't expand macros (eg. %define) in false branch of %if clause */
if (!spec->readStack->reading) if (!spec->readStack->reading)
@ -180,17 +180,17 @@ static int expandMacrosInSpecBuf(rpmSpec spec, int strip)
if (lbuf[0] == '#') if (lbuf[0] == '#')
isComment = 1; isComment = 1;
lbuf = spec->lbuf;
spec->lbuf = rpmExpandMacros(spec->macros, lbuf, 0); if(rpmExpandMacros(spec->macros, spec->lbuf, &lbuf, 0) < 0) {
if (spec->lbuf == NULL) {
rpmlog(RPMLOG_ERR, _("line %d: %s\n"), rpmlog(RPMLOG_ERR, _("line %d: %s\n"),
spec->lineNum, lbuf); spec->lineNum, spec->lbuf);
goto exit; return 1;
} else {
spec->lbufSize = strlen(spec->lbuf) + 1;
} }
free(spec->lbuf);
spec->lbuf = lbuf;
spec->lbufSize = strlen(spec->lbuf) + 1;
if (strip & STRIP_COMMENTS && isComment) { if (strip & STRIP_COMMENTS && isComment) {
char *bufA = lbuf; char *bufA = lbuf;
char *bufB = spec->lbuf; char *bufB = spec->lbuf;
@ -212,10 +212,7 @@ static int expandMacrosInSpecBuf(rpmSpec spec, int strip)
spec->lineNum, lbuf); spec->lineNum, lbuf);
} }
exit: return 0;
free(lbuf);
return rc;
} }
/* Return zero on success, 1 if we need to read more and -1 on errors. */ /* Return zero on success, 1 if we need to read more and -1 on errors. */

View File

@ -1471,7 +1471,7 @@ int expandMacros(void * spec, rpmMacroContext mc, char * sbuf, size_t slen)
return rc; return rc;
} }
char *rpmExpandMacros(rpmMacroContext mc, const char * sbuf, int flags) int rpmExpandMacros(rpmMacroContext mc, const char * sbuf, char ** obuf, int flags)
{ {
char *target = NULL; char *target = NULL;
int rc; int rc;
@ -1482,10 +1482,11 @@ char *rpmExpandMacros(rpmMacroContext mc, const char * sbuf, int flags)
if (rc) { if (rc) {
free(target); free(target);
target = NULL; return -1;
} else {
*obuf = target;
return 1;
} }
return target;
} }
void void

View File

@ -69,11 +69,12 @@ int expandMacros (void * spec, rpmMacroContext mc,
* Expand macro into buffer. * Expand macro into buffer.
* @param mc macro context (NULL uses global context). * @param mc macro context (NULL uses global context).
* @param sbuf input macro to expand * @param sbuf input macro to expand
* @param obuf macro expansion (malloc'ed)
* @param flags flags (currently unused) * @param flags flags (currently unused)
* @return macro expansion (malloc'ed) or NULL on failure * @return negative on failure
*/ */
char *rpmExpandMacros (rpmMacroContext mc, const char * sbuf, int rpmExpandMacros (rpmMacroContext mc, const char * sbuf,
int flags); char ** obuf, int flags);
/** \ingroup rpmmacro /** \ingroup rpmmacro
* Add macro to context. * Add macro to context.