int rpmLoadMacroFile(rpmMacroContext mc, const char * fn) { FILE *fd = fopen(fn, "r"); size_t blen = MACROBUFSIZ; char *buf = xmalloc(blen); int rc = -1; if (fd == NULL || ferror(fd)) { if (fd) (void) fclose(fd); goto exit; } /* XXX Assume new fangled macro expansion */ max_macro_depth = 16; buf[0] = '\0'; while(rdcl(buf, blen, fd) != NULL) { char c, *n; n = buf; SKIPBLANK(n, c); if (c != '%') continue; n++; /* skip % */ rc = rpmDefineMacro(mc, n, RMIL_MACROFILES); } rc = fclose(fd); exit: _free(buf); return rc; }
int breakLine(char *buffer, char *buff1, char *buff2, int buff2size) { char *cp,*dp; cp= buffer; dp= buff1; while( *cp && *cp!=' ' && *cp!='\t') { *dp++ = *cp++; } *dp= 0x0; if(dp== buff1 || !*cp) { return 0; } dp = buff2; SKIPBLANK(cp); if( *cp!='\"') { return 0; } cp++; cp = processstring(buff2, cp, buff2size,'\"'); if(!buff2[0]||*cp!='\"') { return 0; } return 1; }
/** * Parse (and execute) new macro definition. * @param mb macro expansion state * @param se macro definition to parse * @param slen length of se argument * @param level macro recursion level * @param expandbody should body be expanded? * @return address to continue parsing */ static const char * doDefine(MacroBuf mb, const char * se, size_t slen, int level, int expandbody) { const char *s = se; char *buf = xmalloc(slen + 3); /* Some leeway for termination issues... */ char *n = buf, *ne = n; char *o = NULL, *oe; char *b, *be, *ebody = NULL; int c; int oc = ')'; const char *sbody; /* as-is body start */ /* Copy name */ COPYNAME(ne, s, c); /* Copy opts (if present) */ oe = ne + 1; if (*s == '(') { s++; /* skip ( */ /* Options must be terminated with ')' */ if (strchr(s, ')')) { o = oe; COPYOPTS(oe, s, oc); s++; /* skip ) */ } else { rpmlog(RPMLOG_ERR, _("Macro %%%s has unterminated opts\n"), n); goto exit; } } /* Copy body, skipping over escaped newlines */ b = be = oe + 1; sbody = s; SKIPBLANK(s, c); if (c == '{') { /* XXX permit silent {...} grouping */ if ((se = matchchar(s, c, '}')) == NULL) { rpmlog(RPMLOG_ERR, _("Macro %%%s has unterminated body\n"), n); se = s; /* XXX W2DO? */ goto exit; } s++; /* XXX skip { */ strncpy(b, s, (se - s)); b[se - s] = '\0'; be += strlen(b); se++; /* XXX skip } */ s = se; /* move scan forward */ } else { /* otherwise free-field */ int bc = 0, pc = 0; while (*s && (bc || pc || !iseol(*s))) { switch (*s) { case '\\': switch (*(s+1)) { case '\0': break; default: s++; break; } break; case '%': switch (*(s+1)) { case '{': *be++ = *s++; bc++; break; case '(': *be++ = *s++; pc++; break; case '%': *be++ = *s++; break; } break; case '{': if (bc > 0) bc++; break; case '}': if (bc > 0) bc--; break; case '(': if (pc > 0) pc++; break; case ')': if (pc > 0) pc--; break; } *be++ = *s++; } *be = '\0'; if (bc || pc) { rpmlog(RPMLOG_ERR, _("Macro %%%s has unterminated body\n"), n); se = s; /* XXX W2DO? */ goto exit; } /* Trim trailing blanks/newlines */ while (--be >= b && (c = *be) && (isblank(c) || iseol(c))) {}; *(++be) = '\0'; /* one too far */ } /* Move scan over body */ while (iseol(*s)) s++; se = s; /* Names must start with alphabetic or _ and be at least 3 chars */ if (!((c = *n) && (risalpha(c) || c == '_') && (ne - n) > 2)) { rpmlog(RPMLOG_ERR, _("Macro %%%s has illegal name (%%define)\n"), n); goto exit; } if ((be - b) < 1) { rpmlog(RPMLOG_ERR, _("Macro %%%s has empty body\n"), n); goto exit; } if (!isblank(*sbody) && !(*sbody == '\\' && iseol(sbody[1]))) rpmlog(RPMLOG_WARNING, _("Macro %%%s needs whitespace before body\n"), n); if (expandbody) { if (expandThis(mb, b, 0, &ebody)) { rpmlog(RPMLOG_ERR, _("Macro %%%s failed to expand\n"), n); goto exit; } b = ebody; } pushMacro(mb->mc, n, o, b, (level - 1), ME_NONE); exit: _free(buf); _free(ebody); return se; }
/** * Parse (and execute) new macro definition. * @param mb macro expansion state * @param se macro definition to parse * @param level macro recursion level * @param expandbody should body be expanded? * @return address to continue parsing */ static const char * doDefine(MacroBuf mb, const char * se, int level, int expandbody) { const char *s = se; size_t blen = MACROBUFSIZ; char *buf = xmalloc(blen); char *n = buf, *ne = n; char *o = NULL, *oe; char *b, *be; int c; int oc = ')'; /* Copy name */ COPYNAME(ne, s, c); /* Copy opts (if present) */ oe = ne + 1; if (*s == '(') { s++; /* skip ( */ o = oe; COPYOPTS(oe, s, oc); s++; /* skip ) */ } /* Copy body, skipping over escaped newlines */ b = be = oe + 1; SKIPBLANK(s, c); if (c == '{') { /* XXX permit silent {...} grouping */ if ((se = matchchar(s, c, '}')) == NULL) { rpmlog(RPMLOG_ERR, _("Macro %%%s has unterminated body\n"), n); se = s; /* XXX W2DO? */ return se; } s++; /* XXX skip { */ strncpy(b, s, (se - s)); b[se - s] = '\0'; be += strlen(b); se++; /* XXX skip } */ s = se; /* move scan forward */ } else { /* otherwise free-field */ int bc = 0, pc = 0; while (*s && (bc || pc || !iseol(*s))) { switch (*s) { case '\\': switch (*(s+1)) { case '\0': break; default: s++; break; } break; case '%': switch (*(s+1)) { case '{': *be++ = *s++; bc++; break; case '(': *be++ = *s++; pc++; break; case '%': *be++ = *s++; break; } break; case '{': if (bc > 0) bc++; break; case '}': if (bc > 0) bc--; break; case '(': if (pc > 0) pc++; break; case ')': if (pc > 0) pc--; break; } *be++ = *s++; } *be = '\0'; if (bc || pc) { rpmlog(RPMLOG_ERR, _("Macro %%%s has unterminated body\n"), n); se = s; /* XXX W2DO? */ return se; } /* Trim trailing blanks/newlines */ while (--be >= b && (c = *be) && (isblank(c) || iseol(c))) {}; *(++be) = '\0'; /* one too far */ } /* Move scan over body */ while (iseol(*s)) s++; se = s; /* Names must start with alphabetic or _ and be at least 3 chars */ if (!((c = *n) && (risalpha(c) || c == '_') && (ne - n) > 2)) { rpmlog(RPMLOG_ERR, _("Macro %%%s has illegal name (%%define)\n"), n); return se; } /* Options must be terminated with ')' */ if (o && oc != ')') { rpmlog(RPMLOG_ERR, _("Macro %%%s has unterminated opts\n"), n); goto exit; } if ((be - b) < 1) { rpmlog(RPMLOG_ERR, _("Macro %%%s has empty body\n"), n); goto exit; } if (expandbody && expandU(mb, b, (&buf[blen] - b))) { rpmlog(RPMLOG_ERR, _("Macro %%%s failed to expand\n"), n); goto exit; } addMacro(mb->mc, n, o, b, (level - 1)); exit: _free(buf); return se; }
qboolean ReadCheckVarFile(char *checkvarname) { FILE *checkvarfile; unsigned int uptoLine = 0; if (maxcheckvars >= CHECKVAR_MAX) { return FALSE; } checkvarfile = fopen(checkvarname, "rt"); if (!checkvarfile) { return FALSE; } while (fgets(buffer, 256, checkvarfile)) { char *cp = buffer; int len; // remove '\n' len = q2a_strlen(buffer); len -= 1; if (buffer[len] == '\n') { buffer[len] = 0x0; } SKIPBLANK(cp); uptoLine++; if (startContains(cp, "CT:") || startContains(cp, "RG:")) { // looks ok, add... if (*cp == 'C') { checkvarList[maxcheckvars].type = CV_CONSTANT; continue; } else if (*cp == 'R') { checkvarList[maxcheckvars].type = CV_RANGE; continue; } cp += 3; SKIPBLANK(cp); if (*cp != '\"') { gi.dprintf("Error loading CHECKVAR from line %d in file %s, \" not found\n", uptoLine, checkvarname); continue; } cp++; cp = processstring(checkvarList[maxcheckvars].variablename, cp, sizeof (checkvarList[maxcheckvars].variablename) - 1, '\"'); // make sure you are at the end quote while (*cp && *cp != '\"') { cp++; } if (*cp == 0) { gi.dprintf("Error loading CHECKVAR from line %d in file %s, unexcepted end of line\n", uptoLine, checkvarname); continue; } cp++; if (isBlank(checkvarList[maxcheckvars].variablename)) { gi.dprintf("Error loading CHECKVAR from line %d in file %s, blank variable name\n", uptoLine, checkvarname); continue; } SKIPBLANK(cp); if (checkvarList[maxcheckvars].type == CV_CONSTANT) { if (*cp != '\"') { gi.dprintf("Error loading CHECKVAR from line %d in file %s, \" not found\n", uptoLine, checkvarname); continue; } cp++; cp = processstring(checkvarList[maxcheckvars].value, cp, sizeof (checkvarList[maxcheckvars].value) - 1, '\"'); continue; } else if (checkvarList[maxcheckvars].type == CV_RANGE) { char rangevalue[50]; if (*cp != '\"') { gi.dprintf("Error loading CHECKVAR from line %d in file %s, \" not found\n", uptoLine, checkvarname); continue; } cp++; cp = processstring(rangevalue, cp, sizeof (rangevalue) - 1, '\"'); checkvarList[maxcheckvars].lower = q2a_atof(rangevalue); while (*cp && *cp != '\"') { cp++; } if (*cp == 0) { gi.dprintf("Error loading CHECKVAR from line %d in file %s, unexcepted end of line\n", uptoLine, checkvarname); continue; } cp++; SKIPBLANK(cp); if (*cp != '\"') { gi.dprintf("Error loading CHECKVAR from line %d in file %s, \" not found\n", uptoLine, checkvarname); continue; } cp++; cp = processstring(rangevalue, cp, sizeof (rangevalue) - 1, '\"'); checkvarList[maxcheckvars].upper = q2a_atof(rangevalue); continue; } maxcheckvars++; if (maxcheckvars >= CHECKVAR_MAX) { break; } } else if (!(cp[0] == ';' || cp[0] == '\n' || isBlank(cp))) { gi.dprintf("Error loading CHECKVAR from line %d in file %s\n", uptoLine, checkvarname); } } fclose(checkvarfile); return TRUE; }