3 * Handle spec data structure.
8 #include <rpm/header.h>
10 #include <rpm/rpmfi.h>
11 #include <rpm/rpmts.h>
12 #include <rpm/rpmlog.h>
13 #include <rpm/rpmfileutil.h>
15 #include "build/buildio.h"
16 #include "rpmio/rpmlua.h"
20 #define SKIPSPACE(s) { while (*(s) && risspace(*(s))) (s)++; }
21 #define SKIPWHITE(_x) {while(*(_x) && (risspace(*_x) || *(_x) == ',')) (_x)++;}
22 #define SKIPNONWHITE(_x){while(*(_x) &&!(risspace(*_x) || *(_x) == ',')) (_x)++;}
25 * @param p trigger entry chain
29 struct TriggerFileEntry * freeTriggerFiles(struct TriggerFileEntry * p)
31 struct TriggerFileEntry *o, *q = p;
36 o->fileName = _free(o->fileName);
37 o->script = _free(o->script);
38 o->prog = _free(o->prog);
45 * Destroy source component chain.
46 * @param s source component chain
50 struct Source * freeSources(struct Source * s)
52 struct Source *r, *t = s;
57 r->fullSource = _free(r->fullSource);
63 rpmRC lookupPackage(rpmSpec spec, const char *name, int flag,Package *pkg)
66 char *fullName = NULL;
72 *pkg = spec->packages;
76 /* Construct package name */
77 if (flag == PART_SUBNAME) {
78 (void) headerNVR(spec->packages->header, &pname, NULL, NULL);
79 rasprintf(&fullName, "%s-%s", pname, name);
81 fullName = xstrdup(name);
84 /* Locate package with fullName */
85 for (p = spec->packages; p != NULL; p = p->next) {
86 (void) headerNVR(p->header, &pname, NULL, NULL);
87 if (pname && (! strcmp(fullName, pname))) {
95 return ((p == NULL) ? RPMRC_FAIL : RPMRC_OK);
98 Package newPackage(rpmSpec spec)
103 p = xcalloc(1, sizeof(*p));
105 p->header = headerNew();
115 p->triggerScripts = NULL;
118 p->triggerFiles = NULL;
126 p->postInFile = NULL;
128 p->postUnFile = NULL;
129 p->verifyFile = NULL;
131 p->specialDoc = NULL;
132 p->specialDocDir = NULL;
134 if (spec->packages == NULL) {
137 /* Always add package to end of list */
138 for (pp = spec->packages; pp->next != NULL; pp = pp->next)
147 Package freePackage(Package pkg)
149 if (pkg == NULL) return NULL;
151 pkg->preInFile = _free(pkg->preInFile);
152 pkg->postInFile = _free(pkg->postInFile);
153 pkg->preUnFile = _free(pkg->preUnFile);
154 pkg->postUnFile = _free(pkg->postUnFile);
155 pkg->verifyFile = _free(pkg->verifyFile);
157 pkg->header = headerFree(pkg->header);
158 pkg->ds = rpmdsFree(pkg->ds);
159 pkg->fileList = freeStringBuf(pkg->fileList);
160 pkg->fileFile = _free(pkg->fileFile);
162 rpmfi fi = pkg->cpioList;
163 pkg->cpioList = NULL;
167 pkg->specialDoc = freeStringBuf(pkg->specialDoc);
168 pkg->specialDocDir = _free(pkg->specialDocDir);
169 pkg->icon = freeSources(pkg->icon);
170 pkg->triggerFiles = freeTriggerFiles(pkg->triggerFiles);
176 Package freePackages(Package packages)
180 while ((p = packages) != NULL) {
190 static inline struct Source *findSource(rpmSpec spec, uint32_t num, int flag)
194 for (p = spec->sources; p != NULL; p = p->next)
195 if ((num == p->num) && (p->flags & flag)) return p;
200 int parseNoSource(rpmSpec spec, const char * field, rpmTag tag)
207 if (tag == RPMTAG_NOSOURCE) {
208 flag = RPMBUILD_ISSOURCE;
211 flag = RPMBUILD_ISPATCH;
216 for (f = fe; *f != '\0'; f = fe) {
224 if (*fe != '\0') fe++;
226 if (parseUnsignedNum(f, &num)) {
227 rpmlog(RPMLOG_ERR, _("line %d: Bad number: %s\n"),
232 if (! (p = findSource(spec, num, flag))) {
233 rpmlog(RPMLOG_ERR, _("line %d: Bad no%s number: %u\n"),
234 spec->lineNum, name, num);
238 p->flags |= RPMBUILD_ISNO;
245 int addSource(rpmSpec spec, Package pkg, const char *field, rpmTag tag)
249 const char *name = NULL;
255 switch ((rpm_tag_t) tag) {
257 flag = RPMBUILD_ISSOURCE;
259 fieldp = spec->line + 6;
262 flag = RPMBUILD_ISPATCH;
264 fieldp = spec->line + 5;
267 flag = RPMBUILD_ISICON;
273 if (tag != RPMTAG_ICON) {
274 /* We already know that a ':' exists, and that there */
275 /* are no spaces before it. */
276 /* This also now allows for spaces and tabs between */
277 /* the number and the ':' */
279 char *fieldp_backup = fieldp;
281 while ((*fieldp != ':') && (*fieldp != ' ') && (*fieldp != '\t')) {
287 nump = fieldp_backup;
289 if (nump == NULL || *nump == '\0') {
290 num = flag == RPMBUILD_ISSOURCE ? 0 : INT_MAX;
292 if (parseUnsignedNum(fieldp_backup, &num)) {
293 rpmlog(RPMLOG_ERR, _("line %d: Bad %s number: %s\n"),
294 spec->lineNum, name, spec->line);
302 /* Create the entry and link it in */
303 p = xmalloc(sizeof(*p));
305 p->fullSource = xstrdup(field);
307 p->source = strrchr(p->fullSource, '/');
311 p->source = p->fullSource;
314 if (tag != RPMTAG_ICON) {
315 p->next = spec->sources;
324 if (tag != RPMTAG_ICON) {
325 char *body = rpmGetPath("%{_sourcedir}/", p->source, NULL);
327 rasprintf(&buf, "%s%d",
328 (flag & RPMBUILD_ISPATCH) ? "PATCH" : "SOURCE", num);
329 addMacro(spec->macros, buf, NULL, body, RMIL_SPEC);
331 rasprintf(&buf, "%sURL%d",
332 (flag & RPMBUILD_ISPATCH) ? "PATCH" : "SOURCE", num);
333 addMacro(spec->macros, buf, NULL, p->fullSource, RMIL_SPEC);
337 rpmlua lua = NULL; /* global state */
338 const char * what = (flag & RPMBUILD_ISPATCH) ? "patches" : "sources";
339 rpmluaPushTable(lua, what);
340 rpmluav var = rpmluavNew();
341 rpmluavSetListMode(var, 1);
342 rpmluavSetValue(var, RPMLUAV_STRING, body);
343 rpmluaSetVar(lua, var);
344 var = rpmluavFree(var);
356 static inline speclines freeSl(speclines sl)
359 if (sl == NULL) return NULL;
360 for (i = 0; i < sl->sl_nlines; i++)
361 sl->sl_lines[i] = _free(sl->sl_lines[i]);
362 sl->sl_lines = _free(sl->sl_lines);
368 static inline spectags freeSt(spectags st)
371 if (st == NULL) return NULL;
372 for (i = 0; i < st->st_ntags; i++) {
373 spectag t = st->st_t + i;
374 t->t_lang = _free(t->t_lang);
375 t->t_msgid = _free(t->t_msgid);
377 st->st_t = _free(st->st_t);
381 rpmSpec newSpec(void)
383 rpmSpec spec = xcalloc(1, sizeof(*spec));
385 spec->specFile = NULL;
390 spec->fileStack = NULL;
391 spec->lbuf[0] = '\0';
392 spec->line = spec->lbuf;
393 spec->nextline = NULL;
394 spec->nextpeekc = '\0';
396 spec->readStack = xcalloc(1, sizeof(*spec->readStack));
397 spec->readStack->next = NULL;
398 spec->readStack->reading = 1;
400 spec->rootDir = NULL;
403 spec->install = NULL;
407 spec->sources = NULL;
408 spec->packages = NULL;
410 spec->numSources = 0;
412 spec->sourceRpmName = NULL;
413 spec->sourcePkgId = NULL;
414 spec->sourceHeader = NULL;
415 spec->sourceCpioList = NULL;
417 spec->gotBuildRoot = 0;
418 spec->buildRoot = NULL;
419 spec->buildSubdir = NULL;
421 spec->passPhrase = NULL;
425 spec->buildRestrictions = headerNew();
426 spec->BANames = NULL;
429 spec->BASpecs = NULL;
434 spec->macros = rpmGlobalMacroContext;
438 /* make sure patches and sources tables always exist */
439 rpmlua lua = NULL; /* global state */
440 rpmluaPushTable(lua, "patches");
441 rpmluaPushTable(lua, "sources");
449 rpmSpec freeSpec(rpmSpec spec)
451 struct ReadLevelEntry *rl;
453 if (spec == NULL) return NULL;
455 spec->sl = freeSl(spec->sl);
456 spec->st = freeSt(spec->st);
458 spec->prep = freeStringBuf(spec->prep);
459 spec->build = freeStringBuf(spec->build);
460 spec->install = freeStringBuf(spec->install);
461 spec->check = freeStringBuf(spec->check);
462 spec->clean = freeStringBuf(spec->clean);
464 spec->buildRoot = _free(spec->buildRoot);
465 spec->buildSubdir = _free(spec->buildSubdir);
466 spec->rootDir = _free(spec->rootDir);
467 spec->specFile = _free(spec->specFile);
471 while (spec->readStack) {
472 rl = spec->readStack;
473 spec->readStack = rl->next;
478 spec->sourceRpmName = _free(spec->sourceRpmName);
479 spec->sourcePkgId = _free(spec->sourcePkgId);
480 spec->sourceHeader = headerFree(spec->sourceHeader);
482 if (spec->sourceCpioList) {
483 rpmfi fi = spec->sourceCpioList;
484 spec->sourceCpioList = NULL;
488 spec->buildRestrictions = headerFree(spec->buildRestrictions);
490 if (!spec->recursing) {
491 if (spec->BASpecs != NULL)
492 while (spec->BACount--) {
493 spec->BASpecs[spec->BACount] =
494 freeSpec(spec->BASpecs[spec->BACount]);
496 spec->BASpecs = _free(spec->BASpecs);
498 spec->BANames = _free(spec->BANames);
500 spec->passPhrase = _free(spec->passPhrase);
501 spec->cookie = _free(spec->cookie);
504 rpmlua lua = NULL; /* global state */
505 rpmluaDelVar(lua, "patches");
506 rpmluaDelVar(lua, "sources");
509 spec->sources = freeSources(spec->sources);
510 spec->packages = freePackages(spec->packages);
517 struct OpenFileInfo * newOpenFileInfo(void)
519 struct OpenFileInfo *ofi;
521 ofi = xmalloc(sizeof(*ofi));
523 ofi->fileName = NULL;
525 ofi->readBuf[0] = '\0';
532 int rpmspecQuery(rpmts ts, QVA_t qva, const char * arg)
536 char * buildRoot = NULL;
538 char * passPhrase = "";
545 if (qva->qva_showPackage == NULL)
548 /* FIX: make spec abstract */
549 if (parseSpec(ts, arg, "/", buildRoot, recursing, passPhrase,
550 cookie, anyarch, force)
551 || (spec = rpmtsSetSpec(ts, NULL)) == NULL)
554 _("query of specfile %s failed, can't parse\n"), arg);
559 for (pkg = spec->packages; pkg != NULL; pkg = pkg->next)
560 xx = qva->qva_showPackage(qva, ts, pkg->header);
563 spec = freeSpec(spec);