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
00015
00018
00019 static struct PartRec {
00020 int part;
00021 int len;
00022
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
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
00063 if (partList[0].len == 0)
00064 initParts(partList);
00065
00066
00067 for (p = partList; p->token != NULL; p++) {
00068 char c;
00069 if (xstrncasecmp(line, p->token, p->len))
00070 continue;
00071
00072 c = *(line + p->len);
00073
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
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
00103
00104 return rc;
00105 }
00106
00107
00108 void handleComments(char *s)
00109 {
00110 SKIPSPACE(s);
00111 if (*s == '#')
00112 *s = '\0';
00113 }
00114
00115
00118 static void forceIncludeFile(Spec spec, const char * fileName)
00119
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
00132 static int copyNextLine(Spec spec, OFI_t *ofi, int strip)
00133
00134
00135
00136
00137
00138 {
00139 char *last;
00140 char ch;
00141
00142
00143 if (spec->nextline != NULL && spec->nextpeekc != '\0') {
00144 *spec->nextline = spec->nextpeekc;
00145 spec->nextpeekc = '\0';
00146 }
00147
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
00157 spec->lbufPtr = to;
00158
00159 *to++ = '\0';
00160 ofi->readPtr = from;
00161
00162
00163 for (p = spec->lbuf; *p; p++) {
00164 switch (*p) {
00165 case '\\':
00166 switch (*(p+1)) {
00167 case '\n': p++, nc = 1; break;
00168 case '\0': break;
00169 default: p++; break;
00170 }
00171 break;
00172 case '\n': nc = 0; break;
00173 case '%':
00174 switch (*(p+1)) {
00175 case '{': p++, bc++; break;
00176 case '(': p++, pc++; break;
00177 case '%': p++; break;
00178 }
00179 break;
00180 case '{': if (bc > 0) bc++; break;
00181 case '}': if (bc > 0) bc--; break;
00182 case '(': if (pc > 0) pc++; break;
00183 case ')': if (pc > 0) pc--; break;
00184 }
00185 }
00186
00187
00188
00189 if (pc || bc || nc ) {
00190
00191 spec->nextline = "";
00192
00193 return RPMERR_UNMATCHEDIF;
00194 }
00195
00196 spec->lbufPtr = spec->lbuf;
00197
00198
00199
00200
00201
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
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
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
00237
00238
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
00253
00254 if (ofi->fd == NULL) {
00255 ofi->fd = Fopen(ofi->fileName, "r.fpio");
00256 if (ofi->fd == NULL || Ferror(ofi->fd)) {
00257
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
00265
00266
00267 if (!(ofi->readPtr && *(ofi->readPtr))) {
00268
00269 FILE * f = fdGetFp(ofi->fd);
00270
00271 if (f == NULL || !fgets(ofi->readBuf, BUFSIZ, f)) {
00272
00273 if (spec->readStack->next) {
00274 rpmError(RPMERR_UNMATCHEDIF, _("Unclosed %%if\n"));
00275 return RPMERR_UNMATCHEDIF;
00276 }
00277
00278
00279 spec->fileStack = ofi->next;
00280 (void) Fclose(ofi->fd);
00281 ofi->fileName = _free(ofi->fileName);
00282 ofi = _free(ofi);
00283
00284
00285 ofi = spec->fileStack;
00286 if (ofi == NULL)
00287 return 1;
00288
00289
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
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
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
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
00414 return 0;
00415
00416 }
00417
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
00433
00434 extern int noLang;
00435
00436
00437
00438
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
00449 spec = newSpec();
00450
00451
00452
00453
00454
00455
00456
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
00476 addMacro(NULL, "_docdir", NULL, "%{_defaultdocdir}", RMIL_SPEC);
00477
00478
00479
00480
00481
00482
00483 while (parsePart < PART_LAST && parsePart != PART_NONE) {
00484 switch (parsePart) {
00485 case PART_PREAMBLE:
00486 parsePart = parsePreamble(spec, initialPackage);
00487 initialPackage = 0;
00488 break;
00489 case PART_PREP:
00490 parsePart = parsePrep(spec, verify);
00491 break;
00492 case PART_BUILD:
00493 case PART_INSTALL:
00494 case PART_CHECK:
00495 case PART_CLEAN:
00496 parsePart = parseBuildInstallClean(spec, parsePart);
00497 break;
00498 case PART_CHANGELOG:
00499 parsePart = parseChangelog(spec);
00500 break;
00501 case PART_DESCRIPTION:
00502 parsePart = parseDescription(spec);
00503 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 break;
00518
00519 case PART_FILES:
00520 parsePart = parseFiles(spec);
00521 break;
00522
00523 case PART_NONE:
00524 case PART_LAST:
00525 case PART_BUILDARCHITECTURES:
00526 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
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
00547
00548
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
00557 spec = freeSpec(spec);
00558 return RPMERR_BADSPEC;
00559
00560 }
00561
00562
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
00572 spec = freeSpec(spec);
00573 return RPMERR_BADSPEC;
00574
00575 }
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
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
00594
00595 (void) rpmtsSetSpec(ts, spec);
00596 return 0;
00597 }
00598 }
00599
00600
00601
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