build/parseSpec.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 #include <rpmio_internal.h>
00009 #include <rpmbuild.h>
00010 #include "rpmds.h"
00011 #include "rpmts.h"
00012 #include "debug.h"
00013 
00014 /*@access FD_t @*/      /* compared with NULL */
00015 
00018 /*@unchecked@*/
00019 static struct PartRec {
00020     int part;
00021     int len;
00022 /*@observer@*/ /*@null@*/
00023     const char * token;
00024 } partList[] = {
00025     { PART_PREAMBLE,      0, "%package"},
00026     { PART_PREP,          0, "%prep"},
00027     { PART_BUILD,         0, "%build"},
00028     { PART_INSTALL,       0, "%install"},
00029     { PART_CHECK,         0, "%check"},
00030     { PART_CLEAN,         0, "%clean"},
00031     { PART_PREUN,         0, "%preun"},
00032     { PART_POSTUN,        0, "%postun"},
00033     { PART_PRETRANS,      0, "%pretrans"},
00034     { PART_POSTTRANS,     0, "%posttrans"},
00035     { PART_PRE,           0, "%pre"},
00036     { PART_POST,          0, "%post"},
00037     { PART_FILES,         0, "%files"},
00038     { PART_CHANGELOG,     0, "%changelog"},
00039     { PART_DESCRIPTION,   0, "%description"},
00040     { PART_TRIGGERPOSTUN, 0, "%triggerpostun"},
00041     { PART_TRIGGERPREIN,  0, "%triggerprein"},
00042     { PART_TRIGGERUN,     0, "%triggerun"},
00043     { PART_TRIGGERIN,     0, "%triggerin"},
00044     { PART_TRIGGERIN,     0, "%trigger"},
00045     { PART_VERIFYSCRIPT,  0, "%verifyscript"},
00046     {0, 0, 0}
00047 };
00048 
00051 static inline void initParts(struct PartRec *p)
00052         /*@modifies p->len @*/
00053 {
00054     for (; p->token != NULL; p++)
00055         p->len = strlen(p->token);
00056 }
00057 
00058 rpmParseState isPart(const char *line)
00059 {
00060     struct PartRec *p;
00061 
00062 /*@-boundsread@*/
00063     if (partList[0].len == 0)
00064         initParts(partList);
00065 /*@=boundsread@*/
00066     
00067     for (p = partList; p->token != NULL; p++) {
00068         char c;
00069         if (xstrncasecmp(line, p->token, p->len))
00070             continue;
00071 /*@-boundsread@*/
00072         c = *(line + p->len);
00073 /*@=boundsread@*/
00074         if (c == '\0' || xisspace(c))
00075             break;
00076     }
00077 
00078     return (p->token ? p->part : PART_NONE);
00079 }
00080 
00083 static int matchTok(const char *token, const char *line)
00084         /*@*/
00085 {
00086     const char *b, *be = line;
00087     size_t toklen = strlen(token);
00088     int rc = 0;
00089 
00090 /*@-boundsread@*/
00091     while ( *(b = be) != '\0' ) {
00092         SKIPSPACE(b);
00093         be = b;
00094         SKIPNONSPACE(be);
00095         if (be == b)
00096             break;
00097         if (toklen != (be-b) || xstrncasecmp(token, b, (be-b)))
00098             continue;
00099         rc = 1;
00100         break;
00101     }
00102 /*@=boundsread@*/
00103 
00104     return rc;
00105 }
00106 
00107 /*@-boundswrite@*/
00108 void handleComments(char *s)
00109 {
00110     SKIPSPACE(s);
00111     if (*s == '#')
00112         *s = '\0';
00113 }
00114 /*@=boundswrite@*/
00115 
00118 static void forceIncludeFile(Spec spec, const char * fileName)
00119         /*@modifies spec->fileStack @*/
00120 {
00121     OFI_t * ofi;
00122 
00123     ofi = newOpenFileInfo();
00124     ofi->fileName = xstrdup(fileName);
00125     ofi->next = spec->fileStack;
00126     spec->fileStack = ofi;
00127 }
00128 
00131 /*@-boundswrite@*/
00132 static int copyNextLine(Spec spec, OFI_t *ofi, int strip)
00133         /*@globals rpmGlobalMacroContext, h_errno,
00134                 fileSystem @*/
00135         /*@modifies spec->nextline, spec->nextpeekc, spec->lbuf, spec->line,
00136                 ofi->readPtr,
00137                 rpmGlobalMacroContext, fileSystem @*/
00138 {
00139     char *last;
00140     char ch;
00141 
00142     /* Restore 1st char in (possible) next line */
00143     if (spec->nextline != NULL && spec->nextpeekc != '\0') {
00144         *spec->nextline = spec->nextpeekc;
00145         spec->nextpeekc = '\0';
00146     }
00147     /* Expand next line from file into line buffer */
00148     if (!(spec->nextline && *spec->nextline)) {
00149         int pc = 0, bc = 0, nc = 0;
00150         char *from, *to, *p;
00151         to = spec->lbufPtr ? spec->lbufPtr : spec->lbuf;
00152         from = ofi->readPtr;
00153         ch = ' ';
00154         while (*from && ch != '\n')
00155             ch = *to++ = *from++;
00156 /*@-mods@*/
00157         spec->lbufPtr = to;
00158 /*@=mods@*/
00159         *to++ = '\0';
00160         ofi->readPtr = from;
00161 
00162         /* Check if we need another line before expanding the buffer. */
00163         for (p = spec->lbuf; *p; p++) {
00164             switch (*p) {
00165                 case '\\':
00166                     switch (*(p+1)) {
00167                         case '\n': p++, nc = 1; /*@innerbreak@*/ break;
00168                         case '\0': /*@innerbreak@*/ break;
00169                         default: p++; /*@innerbreak@*/ break;
00170                     }
00171                     /*@switchbreak@*/ break;
00172                 case '\n': nc = 0; /*@switchbreak@*/ break;
00173                 case '%':
00174                     switch (*(p+1)) {
00175                         case '{': p++, bc++; /*@innerbreak@*/ break;
00176                         case '(': p++, pc++; /*@innerbreak@*/ break;
00177                         case '%': p++; /*@innerbreak@*/ break;
00178                     }
00179                     /*@switchbreak@*/ break;
00180                 case '{': if (bc > 0) bc++; /*@switchbreak@*/ break;
00181                 case '}': if (bc > 0) bc--; /*@switchbreak@*/ break;
00182                 case '(': if (pc > 0) pc++; /*@switchbreak@*/ break;
00183                 case ')': if (pc > 0) pc--; /*@switchbreak@*/ break;
00184             }
00185         }
00186         
00187         /* If it doesn't, ask for one more line. We need a better
00188          * error code for this. */
00189         if (pc || bc || nc ) {
00190 /*@-observertrans -readonlytrans@*/
00191             spec->nextline = "";
00192 /*@=observertrans =readonlytrans@*/
00193             return RPMERR_UNMATCHEDIF;
00194         }
00195 /*@-mods@*/
00196         spec->lbufPtr = spec->lbuf;
00197 /*@=mods@*/
00198 
00199         /* Don't expand macros (eg. %define) in false branch of %if clause */
00200         /* Also don't expand macros in %changelog where we set STRIP_NOEXPAND flag */
00201         /* (first line is ommited, so if there is e.g. %date macro, it will be expanded */
00202         if (!(strip & STRIP_NOEXPAND)) {
00203         if (spec->readStack->reading &&
00204             expandMacros(spec, spec->macros, spec->lbuf, sizeof(spec->lbuf))) {
00205                 rpmError(RPMERR_BADSPEC, _("line %d: %s\n"),
00206                         spec->lineNum, spec->lbuf);
00207                 return RPMERR_BADSPEC;
00208         }
00209         }       
00210         spec->nextline = spec->lbuf;
00211     }
00212 
00213     /* Find next line in expanded line buffer */
00214     spec->line = last = spec->nextline;
00215     ch = ' ';
00216     while (*spec->nextline && ch != '\n') {
00217         ch = *spec->nextline++;
00218         if (!xisspace(ch))
00219             last = spec->nextline;
00220     }
00221 
00222     /* Save 1st char of next line in order to terminate current line. */
00223     if (*spec->nextline != '\0') {
00224         spec->nextpeekc = *spec->nextline;
00225         *spec->nextline = '\0';
00226     }
00227     
00228     if (strip & STRIP_COMMENTS)
00229         handleComments(spec->line);
00230     
00231     if (strip & STRIP_TRAILINGSPACE)
00232         *last = '\0';
00233 
00234     return 0;
00235 }
00236 /*@=boundswrite@*/
00237 
00238 /*@-boundswrite@*/
00239 int readLine(Spec spec, int strip)
00240 {
00241 #ifdef  DYING
00242     const char *arch;
00243     const char *os;
00244 #endif
00245     char  *s;
00246     int match;
00247     struct ReadLevelEntry *rl;
00248     OFI_t *ofi = spec->fileStack;
00249     int rc;
00250 
00251 retry:
00252     /* Make sure the current file is open */
00253     /*@-branchstate@*/
00254     if (ofi->fd == NULL) {
00255         ofi->fd = Fopen(ofi->fileName, "r.fpio");
00256         if (ofi->fd == NULL || Ferror(ofi->fd)) {
00257             /* XXX Fstrerror */
00258             rpmError(RPMERR_BADSPEC, _("Unable to open %s: %s\n"),
00259                      ofi->fileName, Fstrerror(ofi->fd));
00260             return RPMERR_BADSPEC;
00261         }
00262         spec->lineNum = ofi->lineNum = 0;
00263     }
00264     /*@=branchstate@*/
00265 
00266     /* Make sure we have something in the read buffer */
00267     if (!(ofi->readPtr && *(ofi->readPtr))) {
00268         /*@-type@*/ /* FIX: cast? */
00269         FILE * f = fdGetFp(ofi->fd);
00270         /*@=type@*/
00271         if (f == NULL || !fgets(ofi->readBuf, BUFSIZ, f)) {
00272             /* EOF */
00273             if (spec->readStack->next) {
00274                 rpmError(RPMERR_UNMATCHEDIF, _("Unclosed %%if\n"));
00275                 return RPMERR_UNMATCHEDIF;
00276             }
00277 
00278             /* remove this file from the stack */
00279             spec->fileStack = ofi->next;
00280             (void) Fclose(ofi->fd);
00281             ofi->fileName = _free(ofi->fileName);
00282             ofi = _free(ofi);
00283 
00284             /* only on last file do we signal EOF to caller */
00285             ofi = spec->fileStack;
00286             if (ofi == NULL)
00287                 return 1;
00288 
00289             /* otherwise, go back and try the read again. */
00290             goto retry;
00291         }
00292         ofi->readPtr = ofi->readBuf;
00293         ofi->lineNum++;
00294         spec->lineNum = ofi->lineNum;
00295         if (spec->sl) {
00296             speclines sl = spec->sl;
00297             if (sl->sl_nlines == sl->sl_nalloc) {
00298                 sl->sl_nalloc += 100;
00299                 sl->sl_lines = (char **) xrealloc(sl->sl_lines, 
00300                         sl->sl_nalloc * sizeof(*(sl->sl_lines)));
00301             }
00302             sl->sl_lines[sl->sl_nlines++] = xstrdup(ofi->readBuf);
00303         }
00304     }
00305     
00306     /* Copy next file line into the spec line buffer */
00307     if ((rc = copyNextLine(spec, ofi, strip)) != 0) {
00308         if (rc == RPMERR_UNMATCHEDIF)
00309             goto retry;
00310         return rc;
00311     }
00312 
00313     s = spec->line;
00314     SKIPSPACE(s);
00315 
00316     match = -1;
00317     if (! (strip & STRIP_NOEXPAND)) {
00318     if (!spec->readStack->reading && !strncmp("%if", s, sizeof("%if")-1)) {
00319         match = 0;
00320     } else if (! strncmp("%ifarch", s, sizeof("%ifarch")-1)) {
00321         const char *arch = rpmExpand("%{_target_cpu}", NULL);
00322         s += 7;
00323         match = matchTok(arch, s);
00324         arch = _free(arch);
00325     } else if (! strncmp("%ifnarch", s, sizeof("%ifnarch")-1)) {
00326         const char *arch = rpmExpand("%{_target_cpu}", NULL);
00327         s += 8;
00328         match = !matchTok(arch, s);
00329         arch = _free(arch);
00330     } else if (! strncmp("%ifos", s, sizeof("%ifos")-1)) {
00331         const char *os = rpmExpand("%{_target_os}", NULL);
00332         s += 5;
00333         match = matchTok(os, s);
00334         os = _free(os);
00335     } else if (! strncmp("%ifnos", s, sizeof("%ifnos")-1)) {
00336         const char *os = rpmExpand("%{_target_os}", NULL);
00337         s += 6;
00338         match = !matchTok(os, s);
00339         os = _free(os);
00340     } else if (! strncmp("%if", s, sizeof("%if")-1)) {
00341         s += 3;
00342         match = parseExpressionBoolean(spec, s);
00343         if (match < 0) {
00344             rpmError(RPMERR_UNMATCHEDIF,
00345                         _("%s:%d: parseExpressionBoolean returns %d\n"),
00346                         ofi->fileName, ofi->lineNum, match);
00347             return RPMERR_BADSPEC;
00348         }
00349     } else if (! strncmp("%else", s, sizeof("%else")-1)) {
00350         s += 5;
00351         if (! spec->readStack->next) {
00352             /* Got an else with no %if ! */
00353             rpmError(RPMERR_UNMATCHEDIF,
00354                         _("%s:%d: Got a %%else with no %%if\n"),
00355                         ofi->fileName, ofi->lineNum);
00356             return RPMERR_UNMATCHEDIF;
00357         }
00358         spec->readStack->reading =
00359             spec->readStack->next->reading && ! spec->readStack->reading;
00360         spec->line[0] = '\0';
00361     } else if (! strncmp("%endif", s, sizeof("%endif")-1)) {
00362         s += 6;
00363         if (! spec->readStack->next) {
00364             /* Got an end with no %if ! */
00365             rpmError(RPMERR_UNMATCHEDIF,
00366                         _("%s:%d: Got a %%endif with no %%if\n"),
00367                         ofi->fileName, ofi->lineNum);
00368             return RPMERR_UNMATCHEDIF;
00369         }
00370         rl = spec->readStack;
00371         spec->readStack = spec->readStack->next;
00372         free(rl);
00373         spec->line[0] = '\0';
00374     } else if (! strncmp("%include", s, sizeof("%include")-1)) {
00375         char *fileName, *endFileName, *p;
00376 
00377         s += 8;
00378         fileName = s;
00379         if (! xisspace(*fileName)) {
00380             rpmError(RPMERR_BADSPEC, _("malformed %%include statement\n"));
00381             return RPMERR_BADSPEC;
00382         }
00383         SKIPSPACE(fileName);
00384         endFileName = fileName;
00385         SKIPNONSPACE(endFileName);
00386         p = endFileName;
00387         SKIPSPACE(p);
00388         if (*p != '\0') {
00389             rpmError(RPMERR_BADSPEC, _("malformed %%include statement\n"));
00390             return RPMERR_BADSPEC;
00391         }
00392         *endFileName = '\0';
00393 
00394         forceIncludeFile(spec, fileName);
00395 
00396         ofi = spec->fileStack;
00397         goto retry;
00398     }
00399     }
00400 
00401     if (match != -1) {
00402         rl = xmalloc(sizeof(*rl));
00403         rl->reading = spec->readStack->reading && match;
00404         rl->next = spec->readStack;
00405         spec->readStack = rl;
00406         spec->line[0] = '\0';
00407     }
00408 
00409     if (! spec->readStack->reading) {
00410         spec->line[0] = '\0';
00411     }
00412 
00413     /*@-compmempass@*/ /* FIX: spec->readStack->next should be dependent */
00414     return 0;
00415     /*@=compmempass@*/
00416 }
00417 /*@=boundswrite@*/
00418 
00419 void closeSpec(Spec spec)
00420 {
00421     OFI_t *ofi;
00422 
00423     while (spec->fileStack) {
00424         ofi = spec->fileStack;
00425         spec->fileStack = spec->fileStack->next;
00426         if (ofi->fd) (void) Fclose(ofi->fd);
00427         ofi->fileName = _free(ofi->fileName);
00428         ofi = _free(ofi);
00429     }
00430 }
00431 
00432 /*@-redecl@*/
00433 /*@unchecked@*/
00434 extern int noLang;              /* XXX FIXME: pass as arg */
00435 /*@=redecl@*/
00436 
00437 /*@todo Skip parse recursion if os is not compatible. @*/
00438 /*@-boundswrite@*/
00439 int parseSpec(rpmts ts, const char *specFile, const char *rootURL,
00440                 int recursing, const char *passPhrase,
00441                 char *cookie, int anyarch, int force, int verify)
00442 {
00443     rpmParseState parsePart = PART_PREAMBLE;
00444     int initialPackage = 1;
00445     Package pkg;
00446     Spec spec;
00447     
00448     /* Set up a new Spec structure with no packages. */
00449     spec = newSpec();
00450 
00451     /*
00452      * Note: rpmGetPath should guarantee a "canonical" path. That means
00453      * that the following pathologies should be weeded out:
00454      *          //bin//sh
00455      *          //usr//bin/
00456      *          /.././../usr/../bin//./sh (XXX FIXME: dots not handled yet)
00457      */
00458     spec->specFile = rpmGetPath(specFile, NULL);
00459     spec->fileStack = newOpenFileInfo();
00460     spec->fileStack->fileName = xstrdup(spec->specFile);
00461 
00462     spec->recursing = recursing;
00463     spec->anyarch = anyarch;
00464     spec->force = force;
00465 
00466     if (rootURL)
00467         spec->rootURL = xstrdup(rootURL);
00468     if (passPhrase)
00469         spec->passPhrase = xstrdup(passPhrase);
00470     if (cookie)
00471         spec->cookie = xstrdup(cookie);
00472 
00473     spec->timeCheck = rpmExpandNumeric("%{_timecheck}");
00474 
00475     /* XXX %_docdir should be set somewhere else. */
00476     addMacro(NULL, "_docdir", NULL, "%{_defaultdocdir}", RMIL_SPEC);
00477 
00478     /* All the parse*() functions expect to have a line pre-read */
00479     /* in the spec's line buffer.  Except for parsePreamble(),   */
00480     /* which handles the initial entry into a spec file.         */
00481     
00482     /*@-infloops@*/     /* LCL: parsePart is modified @*/
00483     while (parsePart < PART_LAST && parsePart != PART_NONE) {
00484         switch (parsePart) {
00485         case PART_PREAMBLE:
00486             parsePart = parsePreamble(spec, initialPackage);
00487             initialPackage = 0;
00488             /*@switchbreak@*/ break;
00489         case PART_PREP:
00490             parsePart = parsePrep(spec, verify);
00491             /*@switchbreak@*/ break;
00492         case PART_BUILD:
00493         case PART_INSTALL:
00494         case PART_CHECK:
00495         case PART_CLEAN:
00496             parsePart = parseBuildInstallClean(spec, parsePart);
00497             /*@switchbreak@*/ break;
00498         case PART_CHANGELOG:
00499             parsePart = parseChangelog(spec);
00500             /*@switchbreak@*/ break;
00501         case PART_DESCRIPTION:
00502             parsePart = parseDescription(spec);
00503             /*@switchbreak@*/ break;
00504 
00505         case PART_PRE:
00506         case PART_POST:
00507         case PART_PREUN:
00508         case PART_POSTUN:
00509         case PART_PRETRANS:
00510         case PART_POSTTRANS:
00511         case PART_VERIFYSCRIPT:
00512         case PART_TRIGGERPREIN:
00513         case PART_TRIGGERIN:
00514         case PART_TRIGGERUN:
00515         case PART_TRIGGERPOSTUN:
00516             parsePart = parseScript(spec, parsePart);
00517             /*@switchbreak@*/ break;
00518 
00519         case PART_FILES:
00520             parsePart = parseFiles(spec);
00521             /*@switchbreak@*/ break;
00522 
00523         case PART_NONE:         /* XXX avoid gcc whining */
00524         case PART_LAST:
00525         case PART_BUILDARCHITECTURES:
00526             /*@switchbreak@*/ break;
00527         }
00528 
00529         if (parsePart >= PART_LAST) {
00530             spec = freeSpec(spec);
00531             return parsePart;
00532         }
00533 
00534         if (parsePart == PART_BUILDARCHITECTURES) {
00535             int index;
00536             int x;
00537 
00538             closeSpec(spec);
00539 
00540             /* LCL: sizeof(spec->BASpecs[0]) -nullderef whine here */
00541             spec->BASpecs = xcalloc(spec->BACount, sizeof(*spec->BASpecs));
00542             index = 0;
00543             if (spec->BANames != NULL)
00544             for (x = 0; x < spec->BACount; x++) {
00545 
00546                 /* XXX DIEDIEDIE: filter irrelevant platforms here. */
00547 
00548                 /* XXX there's more to do than set the macro. */
00549                 addMacro(NULL, "_target_cpu", NULL, spec->BANames[x], RMIL_RPMRC);
00550                 spec->BASpecs[index] = NULL;
00551                 if (parseSpec(ts, specFile, spec->rootURL, 1,
00552                                   passPhrase, cookie, anyarch, force, verify)
00553                  || (spec->BASpecs[index] = rpmtsSetSpec(ts, NULL)) == NULL)
00554                 {
00555                         spec->BACount = index;
00556 /*@-nullstate@*/
00557                         spec = freeSpec(spec);
00558                         return RPMERR_BADSPEC;
00559 /*@=nullstate@*/
00560                 }
00561 
00562                 /* XXX there's more to do than delete the macro. */
00563                 delMacro(NULL, "_target_cpu");
00564                 index++;
00565             }
00566 
00567             spec->BACount = index;
00568             if (! index) {
00569                 rpmError(RPMERR_BADSPEC,
00570                         _("No compatible architectures found for build\n"));
00571 /*@-nullstate@*/
00572                 spec = freeSpec(spec);
00573                 return RPMERR_BADSPEC;
00574 /*@=nullstate@*/
00575             }
00576 
00577             /*
00578              * Return the 1st child's fully parsed Spec structure.
00579              * The restart of the parse when encountering BuildArch
00580              * causes problems for "rpm -q --specfile". This is
00581              * still a hack because there may be more than 1 arch
00582              * specified (unlikely but possible.) There's also the
00583              * further problem that the macro context, particularly
00584              * %{_target_cpu}, disagrees with the info in the header.
00585              */
00586             /*@-branchstate@*/
00587             if (spec->BACount >= 1) {
00588                 Spec nspec = spec->BASpecs[0];
00589                 spec->BASpecs = _free(spec->BASpecs);
00590                 spec = freeSpec(spec);
00591                 spec = nspec;
00592             }
00593             /*@=branchstate@*/
00594 
00595             (void) rpmtsSetSpec(ts, spec);
00596             return 0;
00597         }
00598     }
00599     /*@=infloops@*/     /* LCL: parsePart is modified @*/
00600 
00601     /* Check for description in each package and add arch and os */
00602   {
00603     const char *platform = rpmExpand("%{_target_platform}", NULL);
00604     const char *arch = rpmExpand("%{_target_cpu}", NULL);
00605     const char *os = rpmExpand("%{_target_os}", NULL);
00606 
00607     for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
00608         if (!headerIsEntry(pkg->header, RPMTAG_DESCRIPTION)) {
00609             const char * name;
00610             (void) headerNVR(pkg->header, &name, NULL, NULL);
00611             rpmError(RPMERR_BADSPEC, _("Package has no %%description: %s\n"),
00612                         name);
00613             spec = freeSpec(spec);
00614             return RPMERR_BADSPEC;
00615         }
00616 
00617         (void) headerAddEntry(pkg->header, RPMTAG_OS, RPM_STRING_TYPE, os, 1);
00618         (void) headerAddEntry(pkg->header, RPMTAG_ARCH,
00619                 RPM_STRING_TYPE, arch, 1);
00620         (void) headerAddEntry(pkg->header, RPMTAG_PLATFORM,
00621                 RPM_STRING_TYPE, platform, 1);
00622 
00623         pkg->ds = rpmdsThis(pkg->header, RPMTAG_REQUIRENAME, RPMSENSE_EQUAL);
00624 
00625     }
00626 
00627     platform = _free(platform);
00628     arch = _free(arch);
00629     os = _free(os);
00630   }
00631 
00632     closeSpec(spec);
00633     (void) rpmtsSetSpec(ts, spec);
00634 
00635     return 0;
00636 }
00637 /*@=boundswrite@*/

Generated on Wed Feb 6 22:32:28 2008 for rpm by  doxygen 1.5.1