diff options
Diffstat (limited to 'build/macro.c')
-rw-r--r-- | build/macro.c | 168 |
1 files changed, 86 insertions, 82 deletions
diff --git a/build/macro.c b/build/macro.c index 19a4fd878..17319161c 100644 --- a/build/macro.c +++ b/build/macro.c @@ -4,48 +4,42 @@ #include <stdlib.h> #include <string.h> #include <ctype.h> + #include "macro.h" +#include "misc.h" -#ifdef DEBUG +#ifdef DEBUG_MACROS #include <stdio.h> #define rpmError fprintf #define RPMERR_BADSPEC stderr static void dumpTable(void); #else -#include "rpmlib.h" +#include "lib/rpmlib.h" #endif -static void expandMacroTable(void); +static void expandMacroTable(struct MacroContext *mc); static int compareMacros(const void *ap, const void *bp); -static struct macroEntry *findEntry(char *name); -static int handleDefine(char *buf); +static struct MacroEntry *findEntry(struct MacroContext *mc, char *name); +static int handleDefine(struct MacroContext *mc, char *buf); static int parseMacro(char *p, char **macro, char **next); -/* This should be a hash table, but I doubt anyone will ever notice */ +/* This should be a hash table, but I doubt anyone would ever */ +/* notice the increase is speed. */ #define MACRO_CHUNK_SIZE 16 -struct macroEntry { - char *name; - char *expansion; -}; - -static struct macroEntry *macroTable = NULL; -static int macrosAllocated = 0; -static int firstFree = 0; - /*************************************************************************/ /* */ /* Parsing routines */ /* */ /*************************************************************************/ -int expandMacros(char *buf) +int expandMacros(struct MacroContext *mc, char *buf) { char bufA[1024]; char *copyTo, *copyFrom; char *name, *rest, *first; - struct macroEntry *p; + struct MacroEntry *p; if (! buf) { return 0; @@ -57,6 +51,7 @@ int expandMacros(char *buf) first++; } if (*first == '#') { + buf[0] = '\0'; return 0; } @@ -70,8 +65,8 @@ int expandMacros(char *buf) if (parseMacro(copyFrom+1, &name, &rest)) { return 1; } - if (!strcmp(name, "define")) { - if (handleDefine(rest)) { + if (copyFrom == buf && !strcmp(name, "define")) { + if (handleDefine(mc, rest)) { return 1; } /* result is empty */ @@ -83,7 +78,7 @@ int expandMacros(char *buf) copyFrom = rest; } else { /* a real live macro! */ - p = findEntry(name); + p = findEntry(mc, name); if (! p) { /* undefined - just leave it */ *copyTo++ = '%'; @@ -116,7 +111,7 @@ static int parseMacro(char *p, char **macro, char **next) if (! p) { /* empty macro name */ - rpmError(RPMERR_BADSPEC, "Empty macro name\n"); + rpmError(RPMERR_BADSPEC, "Empty macro name"); return 2; } @@ -124,14 +119,14 @@ static int parseMacro(char *p, char **macro, char **next) *next = strchr(p, '}'); if (! *next) { /* unterminated */ - rpmError(RPMERR_BADSPEC, "Unterminated {: %s\n", p); + rpmError(RPMERR_BADSPEC, "Unterminated {: %s", p); return 1; } **next = '\0'; *macro = strtok(p+1, " \n\t"); if (! *macro) { /* empty macro name */ - rpmError(RPMERR_BADSPEC, "Empty macro name\n"); + rpmError(RPMERR_BADSPEC, "Empty macro name"); return 2; } (*next)++; @@ -146,7 +141,7 @@ static int parseMacro(char *p, char **macro, char **next) if (isspace(*p) || ! *p) { /* illegal % syntax */ - rpmError(RPMERR_BADSPEC, "Illegal %% syntax: %s\n", p); + rpmError(RPMERR_BADSPEC, "Illegal %% syntax: %s", p); return 3; } @@ -165,7 +160,7 @@ static int parseMacro(char *p, char **macro, char **next) return 0; } -static int handleDefine(char *buf) +static int handleDefine(struct MacroContext *mc, char *buf) { char *last, *name, *expansion; @@ -177,7 +172,7 @@ static int handleDefine(char *buf) } if (! *name) { /* missing macro name */ - rpmError(RPMERR_BADSPEC, "Unfinished %%define\n"); + rpmError(RPMERR_BADSPEC, "Unfinished %%define"); return 1; } expansion = name; @@ -201,106 +196,115 @@ static int handleDefine(char *buf) } } - expandMacros(expansion); - addMacro(name, expansion); + expandMacros(mc, expansion); + addMacro(mc, name, expansion); return 0; } -#ifdef DEBUG -static void dumpTable() -{ - int i; - - for (i = 0; i < firstFree; i++) { - printf("%s->%s.\n", macroTable[i].name, - macroTable[i].expansion); - } -} - -void main(void) -{ - char buf[1024]; - int x; - - while(gets(buf)) { - x = expandMacros(buf); - printf("%d->%s<-\n", x, buf); - } -} -#endif - /*************************************************************************/ /* */ /* Table handling routines */ /* */ /*************************************************************************/ -void resetMacros(void) +void initMacros(struct MacroContext *mc) +{ + mc->macrosAllocated = 0; + mc->firstFree = 0; + mc->macroTable = NULL; + expandMacroTable(mc); + +} + +void freeMacros(struct MacroContext *mc) { int i; - if (! macrosAllocated) { - expandMacroTable(); - return; - } - - for (i = 0; i < firstFree; i++) { - free(macroTable[i].name); - free(macroTable[i].expansion); + for (i = 0; i < mc->firstFree; i++) { + FREE(mc->macroTable[i].name); + FREE(mc->macroTable[i].expansion); } - firstFree = 0; + FREE(mc->macroTable); } -void addMacro(char *name, char *expansion) +void addMacro(struct MacroContext *mc, char *name, char *expansion) { - struct macroEntry *p; + struct MacroEntry *p; - p = findEntry(name); + p = findEntry(mc, name); if (p) { free(p->expansion); p->expansion = strdup(expansion); return; } - if (firstFree == macrosAllocated) { - expandMacroTable(); + if (mc->firstFree == mc->macrosAllocated) { + expandMacroTable(mc); } - p = macroTable + firstFree++; + p = mc->macroTable + mc->firstFree++; p->name = strdup(name); p->expansion = strdup(expansion); - qsort(macroTable, firstFree, sizeof(*macroTable), compareMacros); + qsort(mc->macroTable, mc->firstFree, sizeof(*(mc->macroTable)), + compareMacros); } -static struct macroEntry *findEntry(char *name) +static struct MacroEntry *findEntry(struct MacroContext *mc, char *name) { - struct macroEntry key; + struct MacroEntry key; - if (! firstFree) { + if (! mc->firstFree) { return NULL; } key.name = name; - return bsearch(&key, macroTable, firstFree, - sizeof(*macroTable), compareMacros); + return bsearch(&key, mc->macroTable, mc->firstFree, + sizeof(*(mc->macroTable)), compareMacros); } static int compareMacros(const void *ap, const void *bp) { - return strcmp(((struct macroEntry *)ap)->name, - ((struct macroEntry *)bp)->name); + return strcmp(((struct MacroEntry *)ap)->name, + ((struct MacroEntry *)bp)->name); } -static void expandMacroTable() +static void expandMacroTable(struct MacroContext *mc) { - macrosAllocated += MACRO_CHUNK_SIZE; - if (! macrosAllocated) { - macroTable = malloc(sizeof(*macroTable) * macrosAllocated); - firstFree = 0; + mc->macrosAllocated += MACRO_CHUNK_SIZE; + if (! mc->macrosAllocated) { + mc->macroTable = malloc(sizeof(*(mc->macroTable)) * + mc->macrosAllocated); + mc->firstFree = 0; } else { - macroTable = realloc(macroTable, - sizeof(*macroTable) * macrosAllocated); + mc->macroTable = realloc(mc->macroTable, sizeof(*(mc->macroTable)) * + mc->macrosAllocated); + } +} + +/***********************************************************************/ + +#ifdef DEBUG_MACROS +static void dumpTable() +{ + int i; + + for (i = 0; i < firstFree; i++) { + printf("%s->%s.\n", macroTable[i].name, + macroTable[i].expansion); + } +} + +void main(void) +{ + char buf[1024]; + int x; + + while(gets(buf)) { + x = expandMacros(buf); + printf("%d->%s<-\n", x, buf); } } +#endif + |