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

View File

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

View File

@ -169,7 +169,7 @@ static int restoreFirstChar(rpmSpec spec)
static int expandMacrosInSpecBuf(rpmSpec spec, int strip)
{
char *lbuf = NULL;
int rc = 0, isComment = 0;
int isComment = 0;
/* Don't expand macros (eg. %define) in false branch of %if clause */
if (!spec->readStack->reading)
@ -180,17 +180,17 @@ static int expandMacrosInSpecBuf(rpmSpec spec, int strip)
if (lbuf[0] == '#')
isComment = 1;
lbuf = spec->lbuf;
spec->lbuf = rpmExpandMacros(spec->macros, lbuf, 0);
if (spec->lbuf == NULL) {
if(rpmExpandMacros(spec->macros, spec->lbuf, &lbuf, 0) < 0) {
rpmlog(RPMLOG_ERR, _("line %d: %s\n"),
spec->lineNum, lbuf);
goto exit;
} else {
spec->lbufSize = strlen(spec->lbuf) + 1;
spec->lineNum, spec->lbuf);
return 1;
}
free(spec->lbuf);
spec->lbuf = lbuf;
spec->lbufSize = strlen(spec->lbuf) + 1;
if (strip & STRIP_COMMENTS && isComment) {
char *bufA = lbuf;
char *bufB = spec->lbuf;
@ -212,10 +212,7 @@ static int expandMacrosInSpecBuf(rpmSpec spec, int strip)
spec->lineNum, lbuf);
}
exit:
free(lbuf);
return rc;
return 0;
}
/* 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;
}
char *rpmExpandMacros(rpmMacroContext mc, const char * sbuf, int flags)
int rpmExpandMacros(rpmMacroContext mc, const char * sbuf, char ** obuf, int flags)
{
char *target = NULL;
int rc;
@ -1482,10 +1482,11 @@ char *rpmExpandMacros(rpmMacroContext mc, const char * sbuf, int flags)
if (rc) {
free(target);
target = NULL;
return -1;
} else {
*obuf = target;
return 1;
}
return target;
}
void

View File

@ -69,11 +69,12 @@ int expandMacros (void * spec, rpmMacroContext mc,
* Expand macro into buffer.
* @param mc macro context (NULL uses global context).
* @param sbuf input macro to expand
* @param obuf macro expansion (malloc'ed)
* @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 flags);
int rpmExpandMacros (rpmMacroContext mc, const char * sbuf,
char ** obuf, int flags);
/** \ingroup rpmmacro
* Add macro to context.