Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

build/parseSpec.c

Go to the documentation of this file.
00001 
00006 #include "system.h"
00007 
00008 static int _debug = 0;
00009 
00010 #include <rpmio_internal.h>
00011 #include <rpmbuild.h>
00012 #include "debug.h"
00013 
00014 /*@access FD_t @*/      /* compared with NULL */
00015 
00018 static struct PartRec {
00019     int part;
00020     int len;
00021     char *token;
00022 } partList[] = {
00023     { PART_PREAMBLE,      0, "%package"},
00024     { PART_PREP,          0, "%prep"},
00025     { PART_BUILD,         0, "%build"},
00026     { PART_INSTALL,       0, "%install"},
00027     { PART_CLEAN,         0, "%clean"},
00028     { PART_PREUN,         0, "%preun"},
00029     { PART_POSTUN,        0, "%postun"},
00030     { PART_PRE,           0, "%pre"},
00031     { PART_POST,          0, "%post"},
00032     { PART_FILES,         0, "%files"},
00033     { PART_CHANGELOG,     0, "%changelog"},
00034     { PART_DESCRIPTION,   0, "%description"},
00035     { PART_TRIGGERPOSTUN, 0, "%triggerpostun"},
00036     { PART_TRIGGERUN,     0, "%triggerun"},
00037     { PART_TRIGGERIN,     0, "%triggerin"},
00038     { PART_TRIGGERIN,     0, "%trigger"},
00039     { PART_VERIFYSCRIPT,  0, "%verifyscript"},
00040     {0, 0, 0}
00041 };
00042 
00045 static inline void initParts(struct PartRec *p)
00046 {
00047     for (; p->token != NULL; p++)
00048         p->len = strlen(p->token);
00049 }
00050 
00051 rpmParseState isPart(const char *line)
00052 {
00053     struct PartRec *p;
00054 
00055     if (partList[0].len == 0)
00056         initParts(partList);
00057     
00058     for (p = partList; p->token != NULL; p++) {
00059         char c;
00060         if (xstrncasecmp(line, p->token, p->len))
00061             continue;
00062         c = *(line + p->len);
00063         if (c == '\0' || isspace(c))
00064             break;
00065     }
00066 
00067     return (p->token ? p->part : PART_NONE);
00068 }
00069 
00072 static int matchTok(const char *token, const char *line)
00073 {
00074     const char *b, *be = line;
00075     size_t toklen = strlen(token);
00076     int rc = 0;
00077 
00078     while ( *(b = be) ) {
00079         SKIPSPACE(b);
00080         be = b;
00081         SKIPNONSPACE(be);
00082         if (be == b)
00083             break;
00084         if (toklen != (be-b) || xstrncasecmp(token, b, (be-b)))
00085             continue;
00086         rc = 1;
00087         break;
00088     }
00089 
00090     return rc;
00091 }
00092 
00093 void handleComments(char *s)
00094 {
00095     SKIPSPACE(s);
00096     if (*s == '#')
00097         *s = '\0';
00098 }
00099 
00102 static void forceIncludeFile(Spec spec, const char * fileName)
00103 {
00104     OFI_t * ofi;
00105 
00106     ofi = newOpenFileInfo();
00107     ofi->fileName = xstrdup(fileName);
00108     ofi->next = spec->fileStack;
00109     spec->fileStack = ofi;
00110 }
00111 
00114 static int copyNextLine(Spec spec, OFI_t *ofi, int strip)
00115 {
00116     char *last;
00117     char ch;
00118 
00119     /* Restore 1st char in (possible) next line */
00120     if (spec->nextline != NULL && spec->nextpeekc != '\0') {
00121         *spec->nextline = spec->nextpeekc;
00122         spec->nextpeekc = '\0';
00123     }
00124     /* Expand next line from file into line buffer */
00125     if (!(spec->nextline && *spec->nextline)) {
00126         char *from, *to;
00127         to = last = spec->lbuf;
00128         from = ofi->readPtr;
00129         ch = ' ';
00130         while (*from && ch != '\n')
00131             ch = *to++ = *from++;
00132         *to++ = '\0';
00133         ofi->readPtr = from;
00134 
00135         /* Don't expand macros (eg. %define) in false branch of %if clause */
00136         if (spec->readStack->reading &&
00137             expandMacros(spec, spec->macros, spec->lbuf, sizeof(spec->lbuf))) {
00138                 rpmError(RPMERR_BADSPEC, _("line %d: %s\n"),
00139                         spec->lineNum, spec->lbuf);
00140                 return RPMERR_BADSPEC;
00141         }
00142         spec->nextline = spec->lbuf;
00143     }
00144 
00145     /* Find next line in expanded line buffer */
00146     spec->line = last = spec->nextline;
00147     ch = ' ';
00148     while (*spec->nextline && ch != '\n') {
00149         ch = *spec->nextline++;
00150         if (!isspace(ch))
00151             last = spec->nextline;
00152     }
00153 
00154     /* Save 1st char of next line in order to terminate current line. */
00155     if (*spec->nextline) {
00156         spec->nextpeekc = *spec->nextline;
00157         *spec->nextline = '\0';
00158     }
00159     
00160     if (strip & STRIP_COMMENTS)
00161         handleComments(spec->line);
00162     
00163     if (strip & STRIP_TRAILINGSPACE)
00164         *last = '\0';
00165 
00166     return 0;
00167 }
00168 
00169 int readLine(Spec spec, int strip)
00170 {
00171 #ifdef  DYING
00172     const char *arch;
00173     const char *os;
00174 #endif
00175     char  *s;
00176     int match;
00177     struct ReadLevelEntry *rl;
00178     OFI_t *ofi = spec->fileStack;
00179     int rc;
00180 
00181 retry:
00182     /* Make sure the current file is open */
00183     if (ofi->fd == NULL) {
00184         ofi->fd = Fopen(ofi->fileName, "r.fpio");
00185         if (ofi->fd == NULL || Ferror(ofi->fd)) {
00186             /* XXX Fstrerror */
00187             rpmError(RPMERR_BADSPEC, _("Unable to open %s: %s\n"),
00188                      ofi->fileName, Fstrerror(ofi->fd));
00189             return RPMERR_BADSPEC;
00190         }
00191         spec->lineNum = ofi->lineNum = 0;
00192     }
00193 
00194     /* Make sure we have something in the read buffer */
00195     if (!(ofi->readPtr && *(ofi->readPtr))) {
00196         if (!fgets(ofi->readBuf, BUFSIZ, fdGetFp(ofi->fd))) {
00197             /* EOF */
00198             if (spec->readStack->next) {
00199                 rpmError(RPMERR_UNMATCHEDIF, _("Unclosed %%if\n"));
00200                 return RPMERR_UNMATCHEDIF;
00201             }
00202 
00203             /* remove this file from the stack */
00204             spec->fileStack = ofi->next;
00205             Fclose(ofi->fd);
00206             FREE(ofi->fileName);
00207             free(ofi);
00208 
00209             /* only on last file do we signal EOF to caller */
00210             ofi = spec->fileStack;
00211             if (ofi == NULL)
00212                 return 1;
00213 
00214             /* otherwise, go back and try the read again. */
00215             goto retry;
00216         }
00217         ofi->readPtr = ofi->readBuf;
00218         ofi->lineNum++;
00219         spec->lineNum = ofi->lineNum;
00220         if (spec->sl) {
00221             struct speclines *sl = spec->sl;
00222             if (sl->sl_nlines == sl->sl_nalloc) {
00223                 sl->sl_nalloc += 100;
00224                 sl->sl_lines = (char **) xrealloc(sl->sl_lines, 
00225                         sl->sl_nalloc * sizeof(*(sl->sl_lines)));
00226             }
00227             sl->sl_lines[sl->sl_nlines++] = xstrdup(ofi->readBuf);
00228         }
00229     }
00230     
00231 #ifdef  DYING
00232     arch = NULL;
00233     rpmGetArchInfo(&arch, NULL);
00234     os = NULL;
00235     rpmGetOsInfo(&os, NULL);
00236 #endif
00237 
00238     /* Copy next file line into the spec line buffer */
00239     if ((rc = copyNextLine(spec, ofi, strip)) != 0)
00240         return rc;
00241 
00242     s = spec->line;
00243     SKIPSPACE(s);
00244 
00245     match = -1;
00246     if (! strncmp("%ifarch", s, sizeof("%ifarch")-1)) {
00247         const char *arch = rpmExpand("%{_target_cpu}", NULL);
00248         s += 7;
00249         match = matchTok(arch, s);
00250         free((void *)arch);
00251     } else if (! strncmp("%ifnarch", s, sizeof("%ifnarch")-1)) {
00252         const char *arch = rpmExpand("%{_target_cpu}", NULL);
00253         s += 8;
00254         match = !matchTok(arch, s);
00255         free((void *)arch);
00256     } else if (! strncmp("%ifos", s, sizeof("%ifos")-1)) {
00257         const char *os = rpmExpand("%{_target_os}", NULL);
00258         s += 5;
00259         match = matchTok(os, s);
00260         free((void *)os);
00261     } else if (! strncmp("%ifnos", s, sizeof("%ifnos")-1)) {
00262         const char *os = rpmExpand("%{_target_os}", NULL);
00263         s += 6;
00264         match = !matchTok(os, s);
00265         free((void *)os);
00266     } else if (! strncmp("%if", s, sizeof("%if")-1)) {
00267         s += 3;
00268         match = parseExpressionBoolean(spec, s);
00269         if (match < 0) {
00270             rpmError(RPMERR_UNMATCHEDIF,
00271                         _("%s:%d: parseExpressionBoolean returns %d\n"),
00272                         ofi->fileName, ofi->lineNum, match);
00273             return RPMERR_BADSPEC;
00274         }
00275     } else if (! strncmp("%else", s, sizeof("%else")-1)) {
00276         s += 5;
00277         if (! spec->readStack->next) {
00278             /* Got an else with no %if ! */
00279             rpmError(RPMERR_UNMATCHEDIF,
00280                         _("%s:%d: Got a %%else with no %%if\n"),
00281                         ofi->fileName, ofi->lineNum);
00282             return RPMERR_UNMATCHEDIF;
00283         }
00284         spec->readStack->reading =
00285             spec->readStack->next->reading && ! spec->readStack->reading;
00286         spec->line[0] = '\0';
00287     } else if (! strncmp("%endif", s, sizeof("%endif")-1)) {
00288         s += 6;
00289         if (! spec->readStack->next) {
00290             /* Got an end with no %if ! */
00291             rpmError(RPMERR_UNMATCHEDIF,
00292                         _("%s:%d: Got a %%endif with no %%if\n"),
00293                         ofi->fileName, ofi->lineNum);
00294             return RPMERR_UNMATCHEDIF;
00295         }
00296         rl = spec->readStack;
00297         spec->readStack = spec->readStack->next;
00298         free(rl);
00299         spec->line[0] = '\0';
00300     } else if (! strncmp("%include", s, sizeof("%include")-1)) {
00301         char *fileName, *endFileName, *p;
00302 
00303         s += 8;
00304         fileName = s;
00305         if (! isspace(*fileName)) {
00306             rpmError(RPMERR_BADSPEC, _("malformed %%include statement\n"));
00307             return RPMERR_BADSPEC;
00308         }
00309         SKIPSPACE(fileName);
00310         endFileName = fileName;
00311         SKIPNONSPACE(endFileName);
00312         p = endFileName;
00313         SKIPSPACE(p);
00314         if (*p != '\0') {
00315             rpmError(RPMERR_BADSPEC, _("malformed %%include statement\n"));
00316             return RPMERR_BADSPEC;
00317         }
00318         *endFileName = '\0';
00319 
00320         forceIncludeFile(spec, fileName);
00321 
00322         ofi = spec->fileStack;
00323         goto retry;
00324     }
00325 
00326     if (match != -1) {
00327         rl = xmalloc(sizeof(struct ReadLevelEntry));
00328         rl->reading = spec->readStack->reading && match;
00329         rl->next = spec->readStack;
00330         spec->readStack = rl;
00331         spec->line[0] = '\0';
00332     }
00333 
00334     if (! spec->readStack->reading) {
00335         spec->line[0] = '\0';
00336     }
00337 
00338     return 0;
00339 }
00340 
00341 void closeSpec(Spec spec)
00342 {
00343     OFI_t *ofi;
00344 
00345     while (spec->fileStack) {
00346         ofi = spec->fileStack;
00347         spec->fileStack = spec->fileStack->next;
00348         if (ofi->fd) Fclose(ofi->fd);
00349         FREE(ofi->fileName);
00350         free(ofi);
00351     }
00352 }
00353 
00354 extern int noLang;              /* XXX FIXME: pass as arg */
00355 
00356 int parseSpec(Spec *specp, const char *specFile, const char *rootURL,
00357                 const char *buildRootURL, int inBuildArch, const char *passPhrase,
00358                 char *cookie, int anyarch, int force)
00359 {
00360     rpmParseState parsePart = PART_PREAMBLE;
00361     int initialPackage = 1;
00362 #ifdef  DYING
00363     const char *saveArch;
00364 #endif
00365     Package pkg;
00366     int x, index;
00367     Spec spec;
00368     
00369     /* Set up a new Spec structure with no packages. */
00370     spec = newSpec();
00371 
00372     /*
00373      * Note: rpmGetPath should guarantee a "canonical" path. That means
00374      * that the following pathologies should be weeded out:
00375      *          //bin//sh
00376      *          //usr//bin/
00377      *          /.././../usr/../bin//./sh (XXX FIXME: dots not handled yet)
00378      */
00379     spec->specFile = rpmGetPath(specFile, NULL);
00380     spec->fileStack = newOpenFileInfo();
00381     spec->fileStack->fileName = xstrdup(spec->specFile);
00382     if (buildRootURL) {
00383         const char * buildRoot;
00384         (void) urlPath(buildRootURL, &buildRoot);
00385         if (*buildRoot == '\0') buildRoot = "/";
00386         if (!strcmp(buildRoot, "/")) {
00387             rpmError(RPMERR_BADSPEC,
00388                      _("BuildRoot can not be \"/\": %s\n"), buildRootURL);
00389             return RPMERR_BADSPEC;
00390         }
00391         spec->gotBuildRootURL = 1;
00392         spec->buildRootURL = xstrdup(buildRootURL);
00393         addMacro(spec->macros, "buildroot", NULL, buildRoot, RMIL_SPEC);
00394 if (_debug)
00395 fprintf(stderr, "*** PS buildRootURL(%s) %p macro set to %s\n", spec->buildRootURL, spec->buildRootURL, buildRoot);
00396     }
00397     addMacro(NULL, "_docdir", NULL, "%{_defaultdocdir}", RMIL_SPEC);
00398     spec->inBuildArchitectures = inBuildArch;
00399     spec->anyarch = anyarch;
00400     spec->force = force;
00401 
00402     if (rootURL)
00403         spec->rootURL = xstrdup(rootURL);
00404     if (passPhrase)
00405         spec->passPhrase = xstrdup(passPhrase);
00406     if (cookie)
00407         spec->cookie = xstrdup(cookie);
00408 
00409     spec->timeCheck = rpmExpandNumeric("%{_timecheck}");
00410 
00411     /* All the parse*() functions expect to have a line pre-read */
00412     /* in the spec's line buffer.  Except for parsePreamble(),   */
00413     /* which handles the initial entry into a spec file.         */
00414     
00415     while (parsePart < PART_LAST && parsePart != PART_NONE) {
00416         switch (parsePart) {
00417           case PART_PREAMBLE:
00418             parsePart = parsePreamble(spec, initialPackage);
00419             initialPackage = 0;
00420             break;
00421           case PART_PREP:
00422             parsePart = parsePrep(spec);
00423             break;
00424           case PART_BUILD:
00425           case PART_INSTALL:
00426           case PART_CLEAN:
00427             parsePart = parseBuildInstallClean(spec, parsePart);
00428             break;
00429           case PART_CHANGELOG:
00430             parsePart = parseChangelog(spec);
00431             break;
00432           case PART_DESCRIPTION:
00433             parsePart = parseDescription(spec);
00434             break;
00435 
00436           case PART_PRE:
00437           case PART_POST:
00438           case PART_PREUN:
00439           case PART_POSTUN:
00440           case PART_VERIFYSCRIPT:
00441           case PART_TRIGGERIN:
00442           case PART_TRIGGERUN:
00443           case PART_TRIGGERPOSTUN:
00444             parsePart = parseScript(spec, parsePart);
00445             break;
00446 
00447           case PART_FILES:
00448             parsePart = parseFiles(spec);
00449             break;
00450 
00451           case PART_NONE:               /* XXX avoid gcc whining */
00452           case PART_LAST:
00453           case PART_BUILDARCHITECTURES:
00454             break;
00455         }
00456 
00457         if (parsePart >= PART_LAST) {
00458             freeSpec(spec);
00459             return parsePart;
00460         }
00461 
00462         if (parsePart == PART_BUILDARCHITECTURES) {
00463             spec->buildArchitectureSpecs =
00464                 xmalloc(sizeof(Spec) * spec->buildArchitectureCount);
00465             index = 0;
00466             for (x = 0; x < spec->buildArchitectureCount; x++) {
00467                 if (rpmMachineScore(RPM_MACHTABLE_BUILDARCH,
00468                                     spec->buildArchitectures[x])) {
00469 #ifdef  DYING
00470                     rpmGetMachine(&saveArch, NULL);
00471                     saveArch = xstrdup(saveArch);
00472                     rpmSetMachine(spec->buildArchitectures[x], NULL);
00473 #else
00474                     addMacro(NULL, "_target_cpu", NULL, spec->buildArchitectures[x], RMIL_RPMRC);
00475 #endif
00476                     if (parseSpec(&(spec->buildArchitectureSpecs[index]),
00477                                   specFile, spec->rootURL, buildRootURL, 1,
00478                                   passPhrase, cookie, anyarch, force)) {
00479                         spec->buildArchitectureCount = index;
00480                         freeSpec(spec);
00481                         return RPMERR_BADSPEC;
00482                     }
00483 #ifdef  DYING
00484                     rpmSetMachine(saveArch, NULL);
00485                     free((void *)saveArch);
00486 #else
00487                     delMacro(NULL, "_target_cpu");
00488 #endif
00489                     index++;
00490                 }
00491             }
00492             spec->buildArchitectureCount = index;
00493             if (! index) {
00494                 freeSpec(spec);
00495                 rpmError(RPMERR_BADSPEC, _("No buildable architectures\n"));
00496                 return RPMERR_BADSPEC;
00497             }
00498 
00499             /* XXX HACK: Swap sl/st with child.
00500              * The restart of the parse when encountering BuildArch
00501              * causes problems for "rpm -q --specfile --specedit"
00502              * which needs to keep track of the entire spec file.
00503              */
00504 
00505             if (spec->sl && spec->st) {
00506                 Spec nspec = *spec->buildArchitectureSpecs;
00507                 struct speclines *sl = spec->sl;
00508                 struct spectags *st = spec->st;
00509                 spec->sl = nspec->sl;
00510                 spec->st = nspec->st;
00511                 nspec->sl = sl;
00512                 nspec->st = st;
00513             }
00514 
00515             closeSpec(spec);
00516             *specp = spec;
00517             return 0;
00518         }
00519     }
00520 
00521     /* Check for description in each package and add arch and os */
00522   {
00523 #ifdef  DYING
00524     const char *arch = NULL;
00525     const char *os = NULL;
00526     char *myos = NULL;
00527 
00528     rpmGetArchInfo(&arch, NULL);
00529     rpmGetOsInfo(&os, NULL);
00530     /*
00531      * XXX Capitalizing the 'L' is needed to insure that old
00532      * XXX os-from-uname (e.g. "Linux") is compatible with the new
00533      * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
00534      * XXX A copy of this string is embedded in headers.
00535      */
00536     if (!strcmp(os, "linux")) {
00537         myos = xstrdup(os);
00538         *myos = 'L';
00539         os = myos;
00540     }
00541 #else
00542     const char *arch = rpmExpand("%{_target_cpu}", NULL);
00543     const char *os = rpmExpand("%{_target_os}", NULL);
00544 #endif
00545 
00546       for (pkg = spec->packages; pkg != NULL; pkg = pkg->next) {
00547         if (!headerIsEntry(pkg->header, RPMTAG_DESCRIPTION)) {
00548             const char * name;
00549             headerNVR(pkg->header, &name, NULL, NULL);
00550             rpmError(RPMERR_BADSPEC, _("Package has no %%description: %s\n"),
00551                         name);
00552             freeSpec(spec);
00553             return RPMERR_BADSPEC;
00554         }
00555 
00556         headerAddEntry(pkg->header, RPMTAG_OS, RPM_STRING_TYPE, os, 1);
00557         headerAddEntry(pkg->header, RPMTAG_ARCH, RPM_STRING_TYPE, arch, 1);
00558       }
00559 #ifdef  DYING
00560     FREE(myos);
00561 #else
00562     free((void *)arch);
00563     free((void *)os);
00564 #endif
00565   }
00566 
00567     closeSpec(spec);
00568     *specp = spec;
00569 
00570     return 0;
00571 }

Generated at Thu Apr 19 15:29:41 2001 for rpm by doxygen1.2.6-20010408 written by Dimitri van Heesch, © 1997-2001