rpmio/macro.c

Go to the documentation of this file.
00001 /*@-boundsread@*/
00006 #include "system.h"
00007 #include <stdarg.h>
00008 
00009 #if !defined(isblank)
00010 #define isblank(_c)     ((_c) == ' ' || (_c) == '\t')
00011 #endif
00012 #define iseol(_c)       ((_c) == '\n' || (_c) == '\r')
00013 
00014 #define STREQ(_t, _f, _fn)      ((_fn) == (sizeof(_t)-1) && !strncmp((_t), (_f), (_fn)))
00015 
00016 #ifdef DEBUG_MACROS
00017 #include <sys/types.h>
00018 #include <errno.h>
00019 #include <fcntl.h>
00020 #include <getopt.h>
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #define rpmError fprintf
00025 #define RPMERR_BADSPEC stderr
00026 #undef  _
00027 #define _(x)    x
00028 
00029 #define vmefail()               (exit(1), NULL)
00030 #define urlPath(_xr, _r)        *(_r) = (_xr)
00031 
00032 typedef FILE * FD_t;
00033 #define Fopen(_path, _fmode)    fopen(_path, "r");
00034 #define Ferror                  ferror
00035 #define Fstrerror(_fd)          strerror(errno)
00036 #define Fread                   fread
00037 #define Fclose                  fclose
00038 
00039 #define fdGetFILE(_fd)          (_fd)
00040 
00041 #else
00042 
00043 /*@observer@*/ /*@checked@*/
00044 const char * rpmMacrofiles = MACROFILES;
00045 
00046 #include <rpmio_internal.h>
00047 #include <rpmmessages.h>
00048 #include <rpmerr.h>
00049 
00050 #ifdef  WITH_LUA
00051 #include <rpmlua.h>
00052 #endif
00053 
00054 #endif
00055 
00056 #include <rpmmacro.h>
00057 
00058 #include "debug.h"
00059 
00060 #if defined(__LCLINT__)
00061 /*@-exportheader@*/
00062 extern const unsigned short int **__ctype_b_loc (void) /*@*/;
00063 /*@=exportheader@*/
00064 #endif
00065 
00066 /*@access FD_t@*/               /* XXX compared with NULL */
00067 /*@access MacroContext@*/
00068 /*@access MacroEntry@*/
00069 /*@access rpmlua @*/
00070 
00071 static struct MacroContext_s rpmGlobalMacroContext_s;
00072 /*@-compmempass@*/
00073 MacroContext rpmGlobalMacroContext = &rpmGlobalMacroContext_s;
00074 /*@=compmempass@*/
00075 
00076 static struct MacroContext_s rpmCLIMacroContext_s;
00077 /*@-compmempass@*/
00078 MacroContext rpmCLIMacroContext = &rpmCLIMacroContext_s;
00079 /*@=compmempass@*/
00080 
00084 typedef /*@abstract@*/ struct MacroBuf_s {
00085 /*@kept@*/ /*@exposed@*/
00086     const char * s;             
00087 /*@shared@*/
00088     char * t;                   
00089     size_t nb;                  
00090     int depth;                  
00091     int macro_trace;            
00092     int expand_trace;           
00093 /*@kept@*/ /*@exposed@*/ /*@null@*/
00094     void * spec;                
00095 /*@kept@*/ /*@exposed@*/
00096     MacroContext mc;
00097 } * MacroBuf;
00098 
00099 #define SAVECHAR(_mb, _c) { *(_mb)->t = (_c), (_mb)->t++, (_mb)->nb--; }
00100 
00101 /*@-exportlocal -exportheadervar@*/
00102 
00103 #define _MAX_MACRO_DEPTH        16
00104 /*@unchecked@*/
00105 int max_macro_depth = _MAX_MACRO_DEPTH;
00106 
00107 #define _PRINT_MACRO_TRACE      0
00108 /*@unchecked@*/
00109 int print_macro_trace = _PRINT_MACRO_TRACE;
00110 
00111 #define _PRINT_EXPAND_TRACE     0
00112 /*@unchecked@*/
00113 int print_expand_trace = _PRINT_EXPAND_TRACE;
00114 /*@=exportlocal =exportheadervar@*/
00115 
00116 #define MACRO_CHUNK_SIZE        16
00117 
00118 /* forward ref */
00119 static int expandMacro(MacroBuf mb)
00120         /*@globals rpmGlobalMacroContext,
00121                 print_macro_trace, print_expand_trace, h_errno, fileSystem @*/
00122         /*@modifies mb, rpmGlobalMacroContext,
00123                 print_macro_trace, print_expand_trace, fileSystem @*/;
00124 
00130 /*@unused@*/ static inline /*@null@*/ void *
00131 _free(/*@only@*/ /*@null@*/ const void * p)
00132         /*@modifies p@*/
00133 {
00134     if (p != NULL)      free((void *)p);
00135     return NULL;
00136 }
00137 
00138 /* =============================================================== */
00139 
00146 static int
00147 compareMacroName(const void * ap, const void * bp)
00148         /*@*/
00149 {
00150     MacroEntry ame = *((MacroEntry *)ap);
00151     MacroEntry bme = *((MacroEntry *)bp);
00152 
00153     if (ame == NULL && bme == NULL)
00154         return 0;
00155     if (ame == NULL)
00156         return 1;
00157     if (bme == NULL)
00158         return -1;
00159     return strcmp(ame->name, bme->name);
00160 }
00161 
00166 /*@-boundswrite@*/
00167 static void
00168 expandMacroTable(MacroContext mc)
00169         /*@modifies mc @*/
00170 {
00171     if (mc->macroTable == NULL) {
00172         mc->macrosAllocated = MACRO_CHUNK_SIZE;
00173         mc->macroTable = (MacroEntry *)
00174             xmalloc(sizeof(*(mc->macroTable)) * mc->macrosAllocated);
00175         mc->firstFree = 0;
00176     } else {
00177         mc->macrosAllocated += MACRO_CHUNK_SIZE;
00178         mc->macroTable = (MacroEntry *)
00179             xrealloc(mc->macroTable, sizeof(*(mc->macroTable)) *
00180                         mc->macrosAllocated);
00181     }
00182     memset(&mc->macroTable[mc->firstFree], 0, MACRO_CHUNK_SIZE * sizeof(*(mc->macroTable)));
00183 }
00184 /*@=boundswrite@*/
00185 
00190 static void
00191 sortMacroTable(MacroContext mc)
00192         /*@modifies mc @*/
00193 {
00194     int i;
00195 
00196     if (mc == NULL || mc->macroTable == NULL)
00197         return;
00198 
00199     qsort(mc->macroTable, mc->firstFree, sizeof(*(mc->macroTable)),
00200                 compareMacroName);
00201 
00202     /* Empty pointers are now at end of table. Reset first free index. */
00203     for (i = 0; i < mc->firstFree; i++) {
00204         if (mc->macroTable[i] != NULL)
00205             continue;
00206         mc->firstFree = i;
00207         break;
00208     }
00209 }
00210 
00211 void
00212 rpmDumpMacroTable(MacroContext mc, FILE * fp)
00213 {
00214     int nempty = 0;
00215     int nactive = 0;
00216 
00217     if (mc == NULL) mc = rpmGlobalMacroContext;
00218     if (fp == NULL) fp = stderr;
00219     
00220     fprintf(fp, "========================\n");
00221     if (mc->macroTable != NULL) {
00222         int i;
00223         for (i = 0; i < mc->firstFree; i++) {
00224             MacroEntry me;
00225             if ((me = mc->macroTable[i]) == NULL) {
00226                 /* XXX this should never happen */
00227                 nempty++;
00228                 continue;
00229             }
00230             fprintf(fp, "%3d%c %s", me->level,
00231                         (me->used > 0 ? '=' : ':'), me->name);
00232             if (me->opts && *me->opts)
00233                     fprintf(fp, "(%s)", me->opts);
00234             if (me->body && *me->body)
00235                     fprintf(fp, "\t%s", me->body);
00236             fprintf(fp, "\n");
00237             nactive++;
00238         }
00239     }
00240     fprintf(fp, _("======================== active %d empty %d\n"),
00241                 nactive, nempty);
00242 }
00243 
00251 /*@-boundswrite@*/
00252 /*@dependent@*/ /*@null@*/
00253 static MacroEntry *
00254 findEntry(MacroContext mc, const char * name, size_t namelen)
00255         /*@*/
00256 {
00257     MacroEntry key, *ret;
00258     char namebuf[1024];
00259 
00260 /*@-globs@*/
00261     if (mc == NULL) mc = rpmGlobalMacroContext;
00262 /*@=globs@*/
00263     if (mc->macroTable == NULL || mc->firstFree == 0)
00264         return NULL;
00265 
00266 /*@-branchstate@*/
00267     if (namelen > 0) {
00268         strncpy(namebuf, name, namelen);
00269         namebuf[namelen] = '\0';
00270         name = namebuf;
00271     }
00272 /*@=branchstate@*/
00273     
00274     key = memset(alloca(sizeof(*key)), 0, sizeof(*key));
00275     /*@-temptrans -assignexpose@*/
00276     key->name = (char *)name;
00277     /*@=temptrans =assignexpose@*/
00278     ret = (MacroEntry *) bsearch(&key, mc->macroTable, mc->firstFree,
00279                         sizeof(*(mc->macroTable)), compareMacroName);
00280     /* XXX TODO: find 1st empty slot and return that */
00281     return ret;
00282 }
00283 /*@=boundswrite@*/
00284 
00285 /* =============================================================== */
00286 
00294 /*@-boundswrite@*/
00295 /*@null@*/
00296 static char *
00297 rdcl(/*@returned@*/ char * buf, size_t size, FD_t fd)
00298         /*@globals fileSystem @*/
00299         /*@modifies buf, fileSystem @*/
00300 {
00301     char *q = buf - 1;          /* initialize just before buffer. */
00302     size_t nb = 0;
00303     size_t nread = 0;
00304     FILE * f = fdGetFILE(fd);
00305     int pc = 0, bc = 0;
00306     char *p = buf;
00307 
00308     if (f != NULL)
00309     do {
00310         *(++q) = '\0';                  /* terminate and move forward. */
00311         if (fgets(q, size, f) == NULL)  /* read next line. */
00312             break;
00313         nb = strlen(q);
00314         nread += nb;                    /* trim trailing \r and \n */
00315         for (q += nb - 1; nb > 0 && iseol(*q); q--)
00316             nb--;
00317         for (; p <= q; p++) {
00318             switch (*p) {
00319                 case '\\':
00320                     switch (*(p+1)) {
00321                         case '\0': /*@switchbreak@*/ break;
00322                         default: p++; /*@switchbreak@*/ break;
00323                     }
00324                     /*@switchbreak@*/ break;
00325                 case '%':
00326                     switch (*(p+1)) {
00327                         case '{': p++, bc++; /*@switchbreak@*/ break;
00328                         case '(': p++, pc++; /*@switchbreak@*/ break;
00329                         case '%': p++; /*@switchbreak@*/ break;
00330                     }
00331                     /*@switchbreak@*/ break;
00332                 case '{': if (bc > 0) bc++; /*@switchbreak@*/ break;
00333                 case '}': if (bc > 0) bc--; /*@switchbreak@*/ break;
00334                 case '(': if (pc > 0) pc++; /*@switchbreak@*/ break;
00335                 case ')': if (pc > 0) pc--; /*@switchbreak@*/ break;
00336             }
00337         }
00338         if (nb == 0 || (*q != '\\' && !bc && !pc) || *(q+1) == '\0') {
00339             *(++q) = '\0';              /* trim trailing \r, \n */
00340             break;
00341         }
00342         q++; p++; nb++;                 /* copy newline too */
00343         size -= nb;
00344         if (*q == '\r')                 /* XXX avoid \r madness */
00345             *q = '\n';
00346     } while (size > 0);
00347     return (nread > 0 ? buf : NULL);
00348 }
00349 /*@=boundswrite@*/
00350 
00358 /*@null@*/
00359 static const char *
00360 matchchar(const char * p, char pl, char pr)
00361         /*@*/
00362 {
00363     int lvl = 0;
00364     char c;
00365 
00366     while ((c = *p++) != '\0') {
00367         if (c == '\\') {                /* Ignore escaped chars */
00368             p++;
00369             continue;
00370         }
00371         if (c == pr) {
00372             if (--lvl <= 0)     return --p;
00373         } else if (c == pl)
00374             lvl++;
00375     }
00376     return (const char *)NULL;
00377 }
00378 
00385 static void
00386 printMacro(MacroBuf mb, const char * s, const char * se)
00387         /*@globals fileSystem @*/
00388         /*@modifies fileSystem @*/
00389 {
00390     const char *senl;
00391     const char *ellipsis;
00392     int choplen;
00393 
00394     if (s >= se) {      /* XXX just in case */
00395         fprintf(stderr, _("%3d>%*s(empty)"), mb->depth,
00396                 (2 * mb->depth + 1), "");
00397         return;
00398     }
00399 
00400     if (s[-1] == '{')
00401         s--;
00402 
00403     /* Print only to first end-of-line (or end-of-string). */
00404     for (senl = se; *senl && !iseol(*senl); senl++)
00405         {};
00406 
00407     /* Limit trailing non-trace output */
00408     choplen = 61 - (2 * mb->depth);
00409     if ((senl - s) > choplen) {
00410         senl = s + choplen;
00411         ellipsis = "...";
00412     } else
00413         ellipsis = "";
00414 
00415     /* Substitute caret at end-of-macro position */
00416     fprintf(stderr, "%3d>%*s%%%.*s^", mb->depth,
00417         (2 * mb->depth + 1), "", (int)(se - s), s);
00418     if (se[1] != '\0' && (senl - (se+1)) > 0)
00419         fprintf(stderr, "%-.*s%s", (int)(senl - (se+1)), se+1, ellipsis);
00420     fprintf(stderr, "\n");
00421 }
00422 
00429 static void
00430 printExpansion(MacroBuf mb, const char * t, const char * te)
00431         /*@globals fileSystem @*/
00432         /*@modifies fileSystem @*/
00433 {
00434     const char *ellipsis;
00435     int choplen;
00436 
00437     if (!(te > t)) {
00438         fprintf(stderr, _("%3d<%*s(empty)\n"), mb->depth, (2 * mb->depth + 1), "");
00439         return;
00440     }
00441 
00442     /* Shorten output which contains newlines */
00443     while (te > t && iseol(te[-1]))
00444         te--;
00445     ellipsis = "";
00446     if (mb->depth > 0) {
00447         const char *tenl;
00448 
00449         /* Skip to last line of expansion */
00450         while ((tenl = strchr(t, '\n')) && tenl < te)
00451             t = ++tenl;
00452 
00453         /* Limit expand output */
00454         choplen = 61 - (2 * mb->depth);
00455         if ((te - t) > choplen) {
00456             te = t + choplen;
00457             ellipsis = "...";
00458         }
00459     }
00460 
00461     fprintf(stderr, "%3d<%*s", mb->depth, (2 * mb->depth + 1), "");
00462     if (te > t)
00463         fprintf(stderr, "%.*s%s", (int)(te - t), t, ellipsis);
00464     fprintf(stderr, "\n");
00465 }
00466 
00467 #define SKIPBLANK(_s, _c)       \
00468         /*@-globs@*/    /* FIX: __ctype_b */ \
00469         while (((_c) = *(_s)) && isblank(_c)) \
00470                 (_s)++;         \
00471         /*@=globs@*/
00472 
00473 #define SKIPNONBLANK(_s, _c)    \
00474         /*@-globs@*/    /* FIX: __ctype_b */ \
00475         while (((_c) = *(_s)) && !(isblank(_c) || iseol(_c))) \
00476                 (_s)++;         \
00477         /*@=globs@*/
00478 
00479 #define COPYNAME(_ne, _s, _c)   \
00480     {   SKIPBLANK(_s,_c);       \
00481         /*@-boundswrite@*/      \
00482         while(((_c) = *(_s)) && (xisalnum(_c) || (_c) == '_')) \
00483                 *(_ne)++ = *(_s)++; \
00484         *(_ne) = '\0';          \
00485         /*@=boundswrite@*/      \
00486     }
00487 
00488 #define COPYOPTS(_oe, _s, _c)   \
00489     {   /*@-boundswrite@*/      \
00490         while(((_c) = *(_s)) && (_c) != ')') \
00491                 *(_oe)++ = *(_s)++; \
00492         *(_oe) = '\0';          \
00493         /*@=boundswrite@*/      \
00494     }
00495 
00503 static int
00504 expandT(MacroBuf mb, const char * f, size_t flen)
00505         /*@globals rpmGlobalMacroContext, h_errno, fileSystem@*/
00506         /*@modifies mb, rpmGlobalMacroContext, fileSystem @*/
00507 {
00508     char *sbuf;
00509     const char *s = mb->s;
00510     int rc;
00511 
00512     sbuf = alloca(flen + 1);
00513     memset(sbuf, 0, (flen + 1));
00514 
00515     strncpy(sbuf, f, flen);
00516     sbuf[flen] = '\0';
00517     mb->s = sbuf;
00518     rc = expandMacro(mb);
00519     mb->s = s;
00520     return rc;
00521 }
00522 
00523 #if 0
00524 
00531 static int
00532 expandS(MacroBuf mb, char * tbuf, size_t tbuflen)
00533         /*@globals rpmGlobalMacroContext, fileSystem@*/
00534         /*@modifies mb, *tbuf, rpmGlobalMacroContext, fileSystem @*/
00535 {
00536     const char *t = mb->t;
00537     size_t nb = mb->nb;
00538     int rc;
00539 
00540     mb->t = tbuf;
00541     mb->nb = tbuflen;
00542     rc = expandMacro(mb);
00543     mb->t = t;
00544     mb->nb = nb;
00545     return rc;
00546 }
00547 #endif
00548 
00556 /*@-boundswrite@*/
00557 static int
00558 expandU(MacroBuf mb, char * u, size_t ulen)
00559         /*@globals rpmGlobalMacroContext, h_errno, fileSystem@*/
00560         /*@modifies mb, *u, rpmGlobalMacroContext, fileSystem @*/
00561 {
00562     const char *s = mb->s;
00563     char *t = mb->t;
00564     size_t nb = mb->nb;
00565     char *tbuf;
00566     int rc;
00567 
00568     tbuf = alloca(ulen + 1);
00569     memset(tbuf, 0, (ulen + 1));
00570 
00571     mb->s = u;
00572     mb->t = tbuf;
00573     mb->nb = ulen;
00574     rc = expandMacro(mb);
00575 
00576     tbuf[ulen] = '\0';  /* XXX just in case */
00577     if (ulen > mb->nb)
00578         strncpy(u, tbuf, (ulen - mb->nb + 1));
00579 
00580     mb->s = s;
00581     mb->t = t;
00582     mb->nb = nb;
00583 
00584     return rc;
00585 }
00586 /*@=boundswrite@*/
00587 
00595 /*@-boundswrite@*/
00596 static int
00597 doShellEscape(MacroBuf mb, const char * cmd, size_t clen)
00598         /*@globals rpmGlobalMacroContext, h_errno, fileSystem @*/
00599         /*@modifies mb, rpmGlobalMacroContext, fileSystem @*/
00600 {
00601     char pcmd[BUFSIZ];
00602     FILE *shf;
00603     int rc;
00604     int c;
00605 
00606     strncpy(pcmd, cmd, clen);
00607     pcmd[clen] = '\0';
00608     rc = expandU(mb, pcmd, sizeof(pcmd));
00609     if (rc)
00610         return rc;
00611 
00612     if ((shf = popen(pcmd, "r")) == NULL)
00613         return 1;
00614     while(mb->nb > 0 && (c = fgetc(shf)) != EOF)
00615         SAVECHAR(mb, c);
00616     (void) pclose(shf);
00617 
00618     /* XXX delete trailing \r \n */
00619     while (iseol(mb->t[-1])) {
00620         *(mb->t--) = '\0';
00621         mb->nb++;
00622     }
00623     return 0;
00624 }
00625 /*@=boundswrite@*/
00626 
00635 /*@dependent@*/ static const char *
00636 doDefine(MacroBuf mb, /*@returned@*/ const char * se, int level, int expandbody)
00637         /*@globals rpmGlobalMacroContext, h_errno @*/
00638         /*@modifies mb, rpmGlobalMacroContext @*/
00639 {
00640     const char *s = se;
00641     char buf[BUFSIZ], *n = buf, *ne;
00642     char *o = NULL, *oe;
00643     char *b, *be;
00644     int c;
00645     int oc = ')';
00646 
00647     SKIPBLANK(s, c);
00648     if (c == '.')               /* XXX readonly macros */
00649         *n++ = c = *s++;
00650     if (c == '.')               /* XXX readonly macros */
00651         *n++ = c = *s++;
00652     ne = n;
00653 
00654     /* Copy name */
00655     COPYNAME(ne, s, c);
00656 
00657     /* Copy opts (if present) */
00658     oe = ne + 1;
00659     if (*s == '(') {
00660         s++;    /* skip ( */
00661         o = oe;
00662         COPYOPTS(oe, s, oc);
00663         s++;    /* skip ) */
00664     }
00665 
00666     /* Copy body, skipping over escaped newlines */
00667     b = be = oe + 1;
00668     SKIPBLANK(s, c);
00669     if (c == '{') {     /* XXX permit silent {...} grouping */
00670         if ((se = matchchar(s, c, '}')) == NULL) {
00671             rpmError(RPMERR_BADSPEC,
00672                 _("Macro %%%s has unterminated body\n"), n);
00673             se = s;     /* XXX W2DO? */
00674             return se;
00675         }
00676         s++;    /* XXX skip { */
00677 /*@-boundswrite@*/
00678         strncpy(b, s, (se - s));
00679         b[se - s] = '\0';
00680 /*@=boundswrite@*/
00681         be += strlen(b);
00682         se++;   /* XXX skip } */
00683         s = se; /* move scan forward */
00684     } else {    /* otherwise free-field */
00685 /*@-boundswrite@*/
00686         int bc = 0, pc = 0;
00687         while (*s && (bc || pc || !iseol(*s))) {
00688             switch (*s) {
00689                 case '\\':
00690                     switch (*(s+1)) {
00691                         case '\0': /*@switchbreak@*/ break;
00692                         default: s++; /*@switchbreak@*/ break;
00693                     }
00694                     /*@switchbreak@*/ break;
00695                 case '%':
00696                     switch (*(s+1)) {
00697                         case '{': *be++ = *s++; bc++; /*@switchbreak@*/ break;
00698                         case '(': *be++ = *s++; pc++; /*@switchbreak@*/ break;
00699                         case '%': *be++ = *s++; /*@switchbreak@*/ break;
00700                     }
00701                     /*@switchbreak@*/ break;
00702                 case '{': if (bc > 0) bc++; /*@switchbreak@*/ break;
00703                 case '}': if (bc > 0) bc--; /*@switchbreak@*/ break;
00704                 case '(': if (pc > 0) pc++; /*@switchbreak@*/ break;
00705                 case ')': if (pc > 0) pc--; /*@switchbreak@*/ break;
00706             }
00707             *be++ = *s++;
00708         }
00709         *be = '\0';
00710 
00711         if (bc || pc) {
00712             rpmError(RPMERR_BADSPEC,
00713                 _("Macro %%%s has unterminated body\n"), n);
00714             se = s;     /* XXX W2DO? */
00715             return se;
00716         }
00717 
00718         /* Trim trailing blanks/newlines */
00719 /*@-globs@*/
00720         while (--be >= b && (c = *be) && (isblank(c) || iseol(c)))
00721             {};
00722 /*@=globs@*/
00723         *(++be) = '\0'; /* one too far */
00724 /*@=boundswrite@*/
00725     }
00726 
00727     /* Move scan over body */
00728     while (iseol(*s))
00729         s++;
00730     se = s;
00731 
00732     /* Names must start with alphabetic or _ and be at least 3 chars */
00733     if (!((c = *n) && (xisalpha(c) || c == '_') && (ne - n) > 2)) {
00734         rpmError(RPMERR_BADSPEC,
00735                 _("Macro %%%s has illegal name (%%define)\n"), n);
00736         return se;
00737     }
00738 
00739     /* Options must be terminated with ')' */
00740     if (o && oc != ')') {
00741         rpmError(RPMERR_BADSPEC, _("Macro %%%s has unterminated opts\n"), n);
00742         return se;
00743     }
00744 
00745     if ((be - b) < 1) {
00746         rpmError(RPMERR_BADSPEC, _("Macro %%%s has empty body\n"), n);
00747         return se;
00748     }
00749 
00750 /*@-modfilesys@*/
00751     if (expandbody && expandU(mb, b, (&buf[sizeof(buf)] - b))) {
00752         rpmError(RPMERR_BADSPEC, _("Macro %%%s failed to expand\n"), n);
00753         return se;
00754     }
00755 /*@=modfilesys@*/
00756 
00757     if (n != buf)               /* XXX readonly macros */
00758         n--;
00759     if (n != buf)               /* XXX readonly macros */
00760         n--;
00761     addMacro(mb->mc, n, o, b, (level - 1));
00762 
00763     return se;
00764 }
00765 
00772 /*@dependent@*/ static const char *
00773 doUndefine(MacroContext mc, /*@returned@*/ const char * se)
00774         /*@globals rpmGlobalMacroContext @*/
00775         /*@modifies mc, rpmGlobalMacroContext @*/
00776 {
00777     const char *s = se;
00778     char buf[BUFSIZ], *n = buf, *ne = n;
00779     int c;
00780 
00781     COPYNAME(ne, s, c);
00782 
00783     /* Move scan over body */
00784     while (iseol(*s))
00785         s++;
00786     se = s;
00787 
00788     /* Names must start with alphabetic or _ and be at least 3 chars */
00789     if (!((c = *n) && (xisalpha(c) || c == '_') && (ne - n) > 2)) {
00790         rpmError(RPMERR_BADSPEC,
00791                 _("Macro %%%s has illegal name (%%undefine)\n"), n);
00792         return se;
00793     }
00794 
00795     delMacro(mc, n);
00796 
00797     return se;
00798 }
00799 
00800 #ifdef  DYING
00801 static void
00802 dumpME(const char * msg, MacroEntry me)
00803         /*@globals fileSystem @*/
00804         /*@modifies fileSystem @*/
00805 {
00806     if (msg)
00807         fprintf(stderr, "%s", msg);
00808     fprintf(stderr, "\tme %p", me);
00809     if (me)
00810         fprintf(stderr,"\tname %p(%s) prev %p",
00811                 me->name, me->name, me->prev);
00812     fprintf(stderr, "\n");
00813 }
00814 #endif
00815 
00824 static void
00825 pushMacro(/*@out@*/ MacroEntry * mep, const char * n, /*@null@*/ const char * o,
00826                 /*@null@*/ const char * b, int level)
00827         /*@modifies *mep @*/
00828 {
00829     MacroEntry prev = (mep && *mep ? *mep : NULL);
00830     MacroEntry me = (MacroEntry) xmalloc(sizeof(*me));
00831     const char *name = n;
00832 
00833     if (*name == '.')           /* XXX readonly macros */
00834         name++;
00835     if (*name == '.')           /* XXX readonly macros */
00836         name++;
00837 
00838     /*@-assignexpose@*/
00839     me->prev = prev;
00840     /*@=assignexpose@*/
00841     me->name = (prev ? prev->name : xstrdup(name));
00842     me->opts = (o ? xstrdup(o) : NULL);
00843     me->body = xstrdup(b ? b : "");
00844     me->used = 0;
00845     me->level = level;
00846     me->flags = (name != n);
00847 /*@-boundswrite@*/
00848 /*@-branchstate@*/
00849     if (mep)
00850         *mep = me;
00851     else
00852         me = _free(me);
00853 /*@=branchstate@*/
00854 /*@=boundswrite@*/
00855 }
00856 
00861 static void
00862 popMacro(MacroEntry * mep)
00863         /*@modifies *mep @*/
00864 {
00865         MacroEntry me = (*mep ? *mep : NULL);
00866 
00867 /*@-branchstate@*/
00868         if (me) {
00869                 /* XXX cast to workaround const */
00870                 /*@-onlytrans@*/
00871 /*@-boundswrite@*/
00872                 if ((*mep = me->prev) == NULL)
00873                         me->name = _free(me->name);
00874 /*@=boundswrite@*/
00875                 me->opts = _free(me->opts);
00876                 me->body = _free(me->body);
00877                 me = _free(me);
00878                 /*@=onlytrans@*/
00879         }
00880 /*@=branchstate@*/
00881 }
00882 
00887 static void
00888 freeArgs(MacroBuf mb)
00889         /*@modifies mb @*/
00890 {
00891     MacroContext mc = mb->mc;
00892     int ndeleted = 0;
00893     int i;
00894 
00895     if (mc == NULL || mc->macroTable == NULL)
00896         return;
00897 
00898     /* Delete dynamic macro definitions */
00899     for (i = 0; i < mc->firstFree; i++) {
00900         MacroEntry *mep, me;
00901         int skiptest = 0;
00902         mep = &mc->macroTable[i];
00903         me = *mep;
00904 
00905         if (me == NULL)         /* XXX this should never happen */
00906             continue;
00907         if (me->level < mb->depth)
00908             continue;
00909         if (strlen(me->name) == 1 && strchr("#*0", *me->name)) {
00910             if (*me->name == '*' && me->used > 0)
00911                 skiptest = 1; /* XXX skip test for %# %* %0 */
00912         } else if (!skiptest && me->used <= 0) {
00913 #if NOTYET
00914             rpmError(RPMERR_BADSPEC,
00915                         _("Macro %%%s (%s) was not used below level %d\n"),
00916                         me->name, me->body, me->level);
00917 #endif
00918         }
00919         popMacro(mep);
00920         if (!(mep && *mep))
00921             ndeleted++;
00922     }
00923 
00924     /* If any deleted macros, sort macro table */
00925     if (ndeleted)
00926         sortMacroTable(mc);
00927 }
00928 
00938 /*@-bounds@*/
00939 /*@dependent@*/ static const char *
00940 grabArgs(MacroBuf mb, const MacroEntry me, /*@returned@*/ const char * se,
00941                 const char * lastc)
00942         /*@globals rpmGlobalMacroContext @*/
00943         /*@modifies mb, rpmGlobalMacroContext @*/
00944 {
00945     char buf[BUFSIZ], *b, *be;
00946     char aname[16];
00947     const char *opts, *o;
00948     int argc = 0;
00949     const char **argv;
00950     int c;
00951 
00952     /* Copy macro name as argv[0], save beginning of args.  */
00953     buf[0] = '\0';
00954     b = be = stpcpy(buf, me->name);
00955 
00956     addMacro(mb->mc, "0", NULL, buf, mb->depth);
00957     
00958     argc = 1;   /* XXX count argv[0] */
00959 
00960     /* Copy args into buf until lastc */
00961     *be++ = ' ';
00962     while ((c = *se++) != '\0' && (se-1) != lastc) {
00963 /*@-globs@*/
00964         if (!isblank(c)) {
00965             *be++ = c;
00966             continue;
00967         }
00968 /*@=globs@*/
00969         /* c is blank */
00970         if (be[-1] == ' ')
00971             continue;
00972         /* a word has ended */
00973         *be++ = ' ';
00974         argc++;
00975     }
00976     if (c == '\0') se--;        /* one too far */
00977     if (be[-1] != ' ')
00978         argc++, be++;           /* last word has not trailing ' ' */
00979     be[-1] = '\0';
00980     if (*b == ' ') b++;         /* skip the leading ' ' */
00981 
00982 /*
00983  * The macro %* analoguous to the shell's $* means "Pass all non-macro
00984  * parameters." Consequently, there needs to be a macro that means "Pass all
00985  * (including macro parameters) options". This is useful for verifying
00986  * parameters during expansion and yet transparently passing all parameters
00987  * through for higher level processing (e.g. %description and/or %setup).
00988  * This is the (potential) justification for %{**} ...
00989  */
00990     /* Add unexpanded args as macro */
00991     addMacro(mb->mc, "**", NULL, b, mb->depth);
00992 
00993 #ifdef NOTYET
00994     /* XXX if macros can be passed as args ... */
00995     expandU(mb, buf, sizeof(buf));
00996 #endif
00997 
00998     /* Build argv array */
00999     argv = (const char **) alloca((argc + 1) * sizeof(*argv));
01000     be[-1] = ' '; /* assert((be - 1) == (b + strlen(b) == buf + strlen(buf))) */
01001     be[0] = '\0';
01002     b = buf;
01003     for (c = 0; c < argc; c++) {
01004         argv[c] = b;
01005         b = strchr(b, ' ');
01006         *b++ = '\0';
01007     }
01008     /* assert(b == be);  */
01009     argv[argc] = NULL;
01010 
01011     /* Citation from glibc/posix/getopt.c:
01012      *    Index in ARGV of the next element to be scanned.
01013      *    This is used for communication to and from the caller
01014      *    and for communication between successive calls to `getopt'.
01015      *
01016      *    On entry to `getopt', zero means this is the first call; initialize.
01017      *
01018      *    When `getopt' returns -1, this is the index of the first of the
01019      *    non-option elements that the caller should itself scan.
01020      *
01021      *    Otherwise, `optind' communicates from one call to the next
01022      *    how much of ARGV has been scanned so far.
01023      */
01024     /* 1003.2 says this must be 1 before any call.  */
01025 
01026 #ifdef __GLIBC__
01027     /*@-mods@*/
01028     optind = 0;         /* XXX but posix != glibc */
01029     /*@=mods@*/
01030 #else
01031     optind = 1;
01032 #endif
01033 
01034     opts = me->opts;
01035 
01036     /* Define option macros. */
01037 /*@-nullstate@*/ /* FIX: argv[] can be NULL */
01038     while((c = getopt(argc, (char **)argv, opts)) != -1)
01039 /*@=nullstate@*/
01040     {
01041         if (c == '?' || (o = strchr(opts, c)) == NULL) {
01042             rpmError(RPMERR_BADSPEC, _("Unknown option %c in %s(%s)\n"),
01043                         (char)c, me->name, opts);
01044             return se;
01045         }
01046         *be++ = '-';
01047         *be++ = c;
01048         if (o[1] == ':') {
01049             *be++ = ' ';
01050             be = stpcpy(be, optarg);
01051         }
01052         *be++ = '\0';
01053         aname[0] = '-'; aname[1] = c; aname[2] = '\0';
01054         addMacro(mb->mc, aname, NULL, b, mb->depth);
01055         if (o[1] == ':') {
01056             aname[0] = '-'; aname[1] = c; aname[2] = '*'; aname[3] = '\0';
01057             addMacro(mb->mc, aname, NULL, optarg, mb->depth);
01058         }
01059         be = b; /* reuse the space */
01060     }
01061 
01062     /* Add arg count as macro. */
01063     sprintf(aname, "%d", (argc - optind));
01064     addMacro(mb->mc, "#", NULL, aname, mb->depth);
01065 
01066     /* Add macro for each arg. Concatenate args for %*. */
01067     if (be) {
01068         *be = '\0';
01069         for (c = optind; c < argc; c++) {
01070             sprintf(aname, "%d", (c - optind + 1));
01071             addMacro(mb->mc, aname, NULL, argv[c], mb->depth);
01072             if (be != b) *be++ = ' '; /* Add space between args */
01073 /*@-nullpass@*/ /* FIX: argv[] can be NULL */
01074             be = stpcpy(be, argv[c]);
01075 /*@=nullpass@*/
01076         }
01077     }
01078 
01079     /* Add unexpanded args as macro. */
01080     addMacro(mb->mc, "*", NULL, b, mb->depth);
01081 
01082     return se;
01083 }
01084 /*@=bounds@*/
01085 
01093 static void
01094 doOutput(MacroBuf mb, int waserror, const char * msg, size_t msglen)
01095         /*@globals rpmGlobalMacroContext, h_errno, fileSystem @*/
01096         /*@modifies mb, rpmGlobalMacroContext, fileSystem @*/
01097 {
01098     char buf[BUFSIZ];
01099 
01100     strncpy(buf, msg, msglen);
01101     buf[msglen] = '\0';
01102     (void) expandU(mb, buf, sizeof(buf));
01103     if (waserror)
01104         rpmError(RPMERR_BADSPEC, "%s\n", buf);
01105     else
01106         fprintf(stderr, "%s", buf);
01107 }
01108 
01118 static void
01119 doFoo(MacroBuf mb, int negate, const char * f, size_t fn,
01120                 /*@null@*/ const char * g, size_t gn)
01121         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
01122         /*@modifies mb, rpmGlobalMacroContext, fileSystem, internalState @*/
01123 {
01124     char buf[BUFSIZ], *b = NULL, *be;
01125     int c;
01126 
01127     buf[0] = '\0';
01128     if (g != NULL) {
01129         strncpy(buf, g, gn);
01130         buf[gn] = '\0';
01131         (void) expandU(mb, buf, sizeof(buf));
01132     }
01133     if (STREQ("basename", f, fn)) {
01134         if ((b = strrchr(buf, '/')) == NULL)
01135             b = buf;
01136         else
01137             b++;
01138 #if NOTYET
01139     /* XXX watchout for conflict with %dir */
01140     } else if (STREQ("dirname", f, fn)) {
01141         if ((b = strrchr(buf, '/')) != NULL)
01142             *b = '\0';
01143         b = buf;
01144 #endif
01145     } else if (STREQ("suffix", f, fn)) {
01146         if ((b = strrchr(buf, '.')) != NULL)
01147             b++;
01148     } else if (STREQ("expand", f, fn)) {
01149         b = buf;
01150     } else if (STREQ("verbose", f, fn)) {
01151         if (negate)
01152             b = (rpmIsVerbose() ? NULL : buf);
01153         else
01154             b = (rpmIsVerbose() ? buf : NULL);
01155     } else if (STREQ("url2path", f, fn) || STREQ("u2p", f, fn)) {
01156         (void)urlPath(buf, (const char **)&b);
01157 /*@-branchstate@*/
01158         if (*b == '\0') b = "/";
01159 /*@=branchstate@*/
01160     } else if (STREQ("uncompress", f, fn)) {
01161         rpmCompressedMagic compressed = COMPRESSED_OTHER;
01162 /*@-globs@*/
01163         for (b = buf; (c = *b) && isblank(c);)
01164             b++;
01165         for (be = b; (c = *be) && !isblank(c);)
01166             be++;
01167 /*@=globs@*/
01168         *be++ = '\0';
01169 #ifndef DEBUG_MACROS
01170         (void) isCompressed(b, &compressed);
01171 #endif
01172         switch(compressed) {
01173         default:
01174         case 0: /* COMPRESSED_NOT */
01175             sprintf(be, "%%__cat %s", b);
01176             break;
01177         case 1: /* COMPRESSED_OTHER */
01178             sprintf(be, "%%__gzip -dc %s", b);
01179             break;
01180         case 2: /* COMPRESSED_BZIP2 */
01181             sprintf(be, "%%__bzip2 -dc %s", b);
01182             break;
01183         case 3: /* COMPRESSED_ZIP */
01184             sprintf(be, "%%__unzip -qq %s", b);
01185             break;
01186         case 4: /* COMPRESSED_LZOP */
01187             sprintf(be, "%%__lzop %s", b);
01188             break;
01189         }
01190         b = be;
01191     } else if (STREQ("S", f, fn)) {
01192         for (b = buf; (c = *b) && xisdigit(c);)
01193             b++;
01194         if (!c) {       /* digit index */
01195             b++;
01196             sprintf(b, "%%SOURCE%s", buf);
01197         } else
01198             b = buf;
01199     } else if (STREQ("P", f, fn)) {
01200         for (b = buf; (c = *b) && xisdigit(c);)
01201             b++;
01202         if (!c) {       /* digit index */
01203             b++;
01204             sprintf(b, "%%PATCH%s", buf);
01205         } else
01206                         b = buf;
01207     } else if (STREQ("F", f, fn)) {
01208         b = buf + strlen(buf) + 1;
01209         sprintf(b, "file%s.file", buf);
01210     }
01211 
01212     if (b) {
01213         (void) expandT(mb, b, strlen(b));
01214     }
01215 }
01216 
01223 static int
01224 expandMacro(MacroBuf mb)
01225         /*@globals rpmGlobalMacroContext,
01226                 print_macro_trace, print_expand_trace, h_errno, fileSystem @*/
01227         /*@modifies mb, rpmGlobalMacroContext,
01228                 print_macro_trace, print_expand_trace, fileSystem @*/
01229 {
01230     MacroEntry *mep;
01231     MacroEntry me;
01232     const char *s = mb->s, *se;
01233     const char *f, *fe;
01234     const char *g, *ge;
01235     size_t fn, gn;
01236     char *t = mb->t;    /* save expansion pointer for printExpand */
01237     int c;
01238     int rc = 0;
01239     int negate;
01240     const char * lastc;
01241     int chkexist;
01242 
01243     if (++mb->depth > max_macro_depth) {
01244         rpmError(RPMERR_BADSPEC,
01245                 _("Recursion depth(%d) greater than max(%d)\n"),
01246                 mb->depth, max_macro_depth);
01247         mb->depth--;
01248         mb->expand_trace = 1;
01249         return 1;
01250     }
01251 
01252 /*@-branchstate@*/
01253     while (rc == 0 && mb->nb > 0 && (c = *s) != '\0') {
01254         s++;
01255         /* Copy text until next macro */
01256         switch(c) {
01257         case '%':
01258                 if (*s) {       /* Ensure not end-of-string. */
01259                     if (*s != '%')
01260                         /*@switchbreak@*/ break;
01261                     s++;        /* skip first % in %% */
01262                 }
01263                 /*@fallthrough@*/
01264         default:
01265                 SAVECHAR(mb, c);
01266                 continue;
01267                 /*@notreached@*/ /*@switchbreak@*/ break;
01268         }
01269 
01270         /* Expand next macro */
01271         f = fe = NULL;
01272         g = ge = NULL;
01273         if (mb->depth > 1)      /* XXX