00001
00002 #include "system.h"
00003
00004 #include <stdarg.h>
00005 #if defined(__linux__) && defined(__powerpc__)
00006 #include <setjmp.h>
00007 #endif
00008
00009 #include <ctype.h>
00010
00011 #if HAVE_SYS_SYSTEMCFG_H
00012 #include <sys/systemcfg.h>
00013 #else
00014 #define __power_pc() 0
00015 #endif
00016
00017 #include <rpmlib.h>
00018 #include <rpmmacro.h>
00019 #include <rpmlua.h>
00020
00021 #include "misc.h"
00022 #include "debug.h"
00023
00024
00025 static const char *defrcfiles = LIBRPMRC_FILENAME ":" VENDORRPMRC_FILENAME ":/etc/rpmrc:~/.rpmrc";
00026
00027
00028 const char * macrofiles = MACROFILES;
00029
00030
00031 static const char * platform = "/etc/rpm/platform";
00032
00033 static const char ** platpat = NULL;
00034
00035 static int nplatpat = 0;
00036
00037 typedef const char * cptr_t;
00038
00039 typedef struct machCacheEntry_s {
00040 const char * name;
00041 int count;
00042 cptr_t * equivs;
00043 int visited;
00044 } * machCacheEntry;
00045
00046 typedef struct machCache_s {
00047 machCacheEntry cache;
00048 int size;
00049 } * machCache;
00050
00051 typedef struct machEquivInfo_s {
00052 const char * name;
00053 int score;
00054 } * machEquivInfo;
00055
00056 typedef struct machEquivTable_s {
00057 int count;
00058 machEquivInfo list;
00059 } * machEquivTable;
00060
00061 struct rpmvarValue {
00062 const char * value;
00063
00064 const char * arch;
00065 struct rpmvarValue * next;
00066 };
00067
00068 struct rpmOption {
00069 const char * name;
00070 int var;
00071 int archSpecific;
00072 int required;
00073 int macroize;
00074 int localize;
00075 struct rpmOptionValue * value;
00076 };
00077
00078 typedef struct defaultEntry_s {
00079 const char * name;
00080 const char * defName;
00081 } * defaultEntry;
00082
00083 typedef struct canonEntry_s {
00084 const char * name;
00085 const char * short_name;
00086 short num;
00087 } * canonEntry;
00088
00089
00090
00091
00092
00093 typedef struct tableType_s {
00094 const char * const key;
00095 const int hasCanon;
00096 const int hasTranslate;
00097 struct machEquivTable_s equiv;
00098 struct machCache_s cache;
00099 defaultEntry defaults;
00100 canonEntry canons;
00101 int defaultsLength;
00102 int canonsLength;
00103 } * tableType;
00104
00105
00106
00107 static struct tableType_s tables[RPM_MACHTABLE_COUNT] = {
00108 { "arch", 1, 0 },
00109 { "os", 1, 0 },
00110 { "buildarch", 0, 1 },
00111 { "buildos", 0, 1 }
00112 };
00113
00114
00115
00116
00117
00118 static struct rpmOption optionTable[] = {
00119 { "include", RPMVAR_INCLUDE, 0, 1, 0, 2 },
00120 { "macrofiles", RPMVAR_MACROFILES, 0, 0, 0, 1 },
00121 { "optflags", RPMVAR_OPTFLAGS, 1, 0, 1, 0 },
00122 { "provides", RPMVAR_PROVIDES, 0, 0, 0, 0 },
00123 };
00124
00125
00126
00127 static int optionTableSize = sizeof(optionTable) / sizeof(*optionTable);
00128
00129 #define OS 0
00130 #define ARCH 1
00131
00132
00133 static cptr_t current[2];
00134
00135
00136 static int currTables[2] = { RPM_MACHTABLE_INSTOS, RPM_MACHTABLE_INSTARCH };
00137
00138
00139 static struct rpmvarValue values[RPMVAR_NUM];
00140
00141
00142 static int defaultsInitialized = 0;
00143
00144
00145 static int doReadRC( FD_t fd, const char * urlfn)
00146
00147 ;
00148
00149 static void rpmSetVarArch(int var, const char * val,
00150 const char * arch)
00151
00152 ;
00153
00154 static void rebuildCompatTables(int type, const char * name)
00155
00156 ;
00157
00158 static void rpmRebuildTargetVars( const char **target, const char ** canontarget)
00159
00160
00161 ;
00162
00163 static int optionCompare(const void * a, const void * b)
00164
00165 {
00166 return xstrcasecmp(((struct rpmOption *) a)->name,
00167 ((struct rpmOption *) b)->name);
00168 }
00169
00170 static machCacheEntry
00171 machCacheFindEntry(const machCache cache, const char * key)
00172
00173 {
00174 int i;
00175
00176 for (i = 0; i < cache->size; i++)
00177 if (!strcmp(cache->cache[i].name, key)) return cache->cache + i;
00178
00179 return NULL;
00180 }
00181
00182 static int machCompatCacheAdd(char * name, const char * fn, int linenum,
00183 machCache cache)
00184
00185
00186 {
00187 machCacheEntry entry = NULL;
00188 char * chptr;
00189 char * equivs;
00190 int delEntry = 0;
00191 int i;
00192
00193 while (*name && xisspace(*name)) name++;
00194
00195 chptr = name;
00196 while (*chptr && *chptr != ':') chptr++;
00197 if (!*chptr) {
00198 rpmError(RPMERR_RPMRC, _("missing second ':' at %s:%d\n"), fn, linenum);
00199 return 1;
00200 } else if (chptr == name) {
00201 rpmError(RPMERR_RPMRC, _("missing architecture name at %s:%d\n"), fn,
00202 linenum);
00203 return 1;
00204 }
00205
00206 while (*chptr == ':' || xisspace(*chptr)) chptr--;
00207 *(++chptr) = '\0';
00208 equivs = chptr + 1;
00209 while (*equivs && xisspace(*equivs)) equivs++;
00210 if (!*equivs) {
00211 delEntry = 1;
00212 }
00213
00214 if (cache->size) {
00215 entry = machCacheFindEntry(cache, name);
00216 if (entry) {
00217 for (i = 0; i < entry->count; i++)
00218 entry->equivs[i] = _free(entry->equivs[i]);
00219 entry->equivs = _free(entry->equivs);
00220 entry->count = 0;
00221 }
00222 }
00223
00224 if (!entry) {
00225 cache->cache = xrealloc(cache->cache,
00226 (cache->size + 1) * sizeof(*cache->cache));
00227 entry = cache->cache + cache->size++;
00228 entry->name = xstrdup(name);
00229 entry->count = 0;
00230 entry->visited = 0;
00231 }
00232
00233 if (delEntry) return 0;
00234
00235 while ((chptr = strtok(equivs, " ")) != NULL) {
00236 equivs = NULL;
00237 if (chptr[0] == '\0')
00238 continue;
00239 if (entry->count)
00240 entry->equivs = xrealloc(entry->equivs, sizeof(*entry->equivs)
00241 * (entry->count + 1));
00242 else
00243 entry->equivs = xmalloc(sizeof(*entry->equivs));
00244
00245 entry->equivs[entry->count] = xstrdup(chptr);
00246 entry->count++;
00247 }
00248
00249 return 0;
00250 }
00251
00252 static machEquivInfo
00253 machEquivSearch(const machEquivTable table, const char * name)
00254
00255 {
00256 int i;
00257
00258 for (i = 0; i < table->count; i++)
00259 if (!xstrcasecmp(table->list[i].name, name))
00260 return table->list + i;
00261
00262 return NULL;
00263 }
00264
00265 static void machAddEquiv(machEquivTable table, const char * name,
00266 int distance)
00267
00268 {
00269 machEquivInfo equiv;
00270
00271 equiv = machEquivSearch(table, name);
00272 if (!equiv) {
00273 if (table->count)
00274 table->list = xrealloc(table->list, (table->count + 1)
00275 * sizeof(*table->list));
00276 else
00277 table->list = xmalloc(sizeof(*table->list));
00278
00279 table->list[table->count].name = xstrdup(name);
00280 table->list[table->count++].score = distance;
00281 }
00282 }
00283
00284 static void machCacheEntryVisit(machCache cache,
00285 machEquivTable table, const char * name, int distance)
00286
00287 {
00288 machCacheEntry entry;
00289 int i;
00290
00291 entry = machCacheFindEntry(cache, name);
00292 if (!entry || entry->visited) return;
00293
00294 entry->visited = 1;
00295
00296 for (i = 0; i < entry->count; i++) {
00297 machAddEquiv(table, entry->equivs[i], distance);
00298 }
00299
00300 for (i = 0; i < entry->count; i++) {
00301 machCacheEntryVisit(cache, table, entry->equivs[i], distance + 1);
00302 }
00303 }
00304
00305 static void machFindEquivs(machCache cache, machEquivTable table,
00306 const char * key)
00307
00308 {
00309 int i;
00310
00311 for (i = 0; i < cache->size; i++)
00312 cache->cache[i].visited = 0;
00313
00314 while (table->count > 0) {
00315 --table->count;
00316 table->list[table->count].name = _free(table->list[table->count].name);
00317 }
00318 table->count = 0;
00319 table->list = _free(table->list);
00320
00321
00322
00323
00324
00325
00326
00327 machAddEquiv(table, key, 1);
00328 machCacheEntryVisit(cache, table, key, 2);
00329 return;
00330
00331 }
00332
00333 static int addCanon(canonEntry * table, int * tableLen, char * line,
00334 const char * fn, int lineNum)
00335
00336
00337 {
00338 canonEntry t;
00339 char *s, *s1;
00340 const char * tname;
00341 const char * tshort_name;
00342 int tnum;
00343
00344 (*tableLen) += 2;
00345
00346 *table = xrealloc(*table, sizeof(**table) * (*tableLen));
00347
00348
00349 t = & ((*table)[*tableLen - 2]);
00350
00351 tname = strtok(line, ": \t");
00352 tshort_name = strtok(NULL, " \t");
00353 s = strtok(NULL, " \t");
00354 if (! (tname && tshort_name && s)) {
00355 rpmError(RPMERR_RPMRC, _("Incomplete data line at %s:%d\n"),
00356 fn, lineNum);
00357 return RPMERR_RPMRC;
00358 }
00359 if (strtok(NULL, " \t")) {
00360 rpmError(RPMERR_RPMRC, _("Too many args in data line at %s:%d\n"),
00361 fn, lineNum);
00362 return RPMERR_RPMRC;
00363 }
00364
00365
00366 tnum = strtoul(s, &s1, 10);
00367 if ((*s1) || (s1 == s) || (tnum == ULONG_MAX)) {
00368 rpmError(RPMERR_RPMRC, _("Bad arch/os number: %s (%s:%d)\n"), s,
00369 fn, lineNum);
00370 return(RPMERR_RPMRC);
00371 }
00372
00373
00374 t[0].name = xstrdup(tname);
00375 t[0].short_name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
00376 t[0].num = tnum;
00377
00378
00379
00380 t[1].name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
00381 t[1].short_name = (tshort_name ? xstrdup(tshort_name) : xstrdup(""));
00382 t[1].num = tnum;
00383
00384 return 0;
00385 }
00386
00387 static int addDefault(defaultEntry * table, int * tableLen, char * line,
00388 const char * fn, int lineNum)
00389
00390
00391 {
00392 defaultEntry t;
00393
00394 (*tableLen)++;
00395
00396 *table = xrealloc(*table, sizeof(**table) * (*tableLen));
00397
00398
00399 t = & ((*table)[*tableLen - 1]);
00400
00401
00402 t->name = strtok(line, ": \t");
00403 t->defName = strtok(NULL, " \t");
00404 if (! (t->name && t->defName)) {
00405 rpmError(RPMERR_RPMRC, _("Incomplete default line at %s:%d\n"),
00406 fn, lineNum);
00407 return RPMERR_RPMRC;
00408 }
00409 if (strtok(NULL, " \t")) {
00410 rpmError(RPMERR_RPMRC, _("Too many args in default line at %s:%d\n"),
00411 fn, lineNum);
00412 return RPMERR_RPMRC;
00413 }
00414
00415 t->name = xstrdup(t->name);
00416 t->defName = (t->defName ? xstrdup(t->defName) : NULL);
00417
00418
00419 return 0;
00420 }
00421
00422 static canonEntry lookupInCanonTable(const char * name,
00423 const canonEntry table, int tableLen)
00424
00425 {
00426 while (tableLen) {
00427 tableLen--;
00428 if (strcmp(name, table[tableLen].name))
00429 continue;
00430
00431 return &(table[tableLen]);
00432
00433 }
00434
00435 return NULL;
00436 }
00437
00438 static
00439 const char * lookupInDefaultTable(const char * name,
00440 const defaultEntry table, int tableLen)
00441
00442 {
00443 while (tableLen) {
00444 tableLen--;
00445 if (table[tableLen].name && !strcmp(name, table[tableLen].name))
00446 return table[tableLen].defName;
00447 }
00448
00449 return name;
00450 }
00451
00452 static void setVarDefault(int var, const char * macroname, const char * val,
00453 const char * body)
00454
00455
00456 {
00457 if (var >= 0) {
00458 if (rpmGetVar(var)) return;
00459 rpmSetVar(var, val);
00460 }
00461 if (body == NULL)
00462 body = val;
00463 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
00464 }
00465
00466 static void setPathDefault(int var, const char * macroname, const char * subdir)
00467
00468
00469 {
00470
00471 if (var >= 0) {
00472 const char * topdir;
00473 char * fn;
00474
00475 if (rpmGetVar(var)) return;
00476
00477 topdir = rpmGetPath("%{_topdir}", NULL);
00478
00479 fn = alloca(strlen(topdir) + strlen(subdir) + 2);
00480 strcpy(fn, topdir);
00481 if (fn[strlen(topdir) - 1] != '/')
00482 strcat(fn, "/");
00483 strcat(fn, subdir);
00484
00485 rpmSetVar(var, fn);
00486 topdir = _free(topdir);
00487 }
00488
00489 if (macroname != NULL) {
00490 #define _TOPDIRMACRO "%{_topdir}/"
00491 char *body = alloca(sizeof(_TOPDIRMACRO) + strlen(subdir));
00492 strcpy(body, _TOPDIRMACRO);
00493 strcat(body, subdir);
00494 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
00495 #undef _TOPDIRMACRO
00496 }
00497 }
00498
00499
00500 static const char * prescriptenviron = "\n\
00501 RPM_SOURCE_DIR=\"%{_sourcedir}\"\n\
00502 RPM_BUILD_DIR=\"%{_builddir}\"\n\
00503 RPM_OPT_FLAGS=\"%{optflags}\"\n\
00504 RPM_ARCH=\"%{_arch}\"\n\
00505 RPM_OS=\"%{_os}\"\n\
00506 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS\n\
00507 RPM_DOC_DIR=\"%{_docdir}\"\n\
00508 export RPM_DOC_DIR\n\
00509 RPM_PACKAGE_NAME=\"%{name}\"\n\
00510 RPM_PACKAGE_VERSION=\"%{version}\"\n\
00511 RPM_PACKAGE_RELEASE=\"%{release}\"\n\
00512 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\n\
00513 %{?buildroot:RPM_BUILD_ROOT=\"%{buildroot}\"\n\
00514 export RPM_BUILD_ROOT\n}\
00515 ";
00516
00517 static void setDefaults(void)
00518
00519
00520 {
00521
00522 addMacro(NULL, "_usr", NULL, "/usr", RMIL_DEFAULT);
00523 addMacro(NULL, "_var", NULL, "/var", RMIL_DEFAULT);
00524
00525 addMacro(NULL, "_preScriptEnvironment",NULL, prescriptenviron,RMIL_DEFAULT);
00526
00527 setVarDefault(-1, "_topdir",
00528 "/usr/src/redhat", "%{_usr}/src/redhat");
00529 setVarDefault(-1, "_tmppath",
00530 "/var/tmp", "%{_var}/tmp");
00531 setVarDefault(-1, "_dbpath",
00532 "/var/lib/rpm", "%{_var}/lib/rpm");
00533 setVarDefault(-1, "_defaultdocdir",
00534 "/usr/doc", "%{_usr}/doc");
00535
00536 setVarDefault(-1, "_rpmfilename",
00537 "%%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",NULL);
00538
00539 setVarDefault(RPMVAR_OPTFLAGS, "optflags",
00540 "-O2", NULL);
00541 setVarDefault(-1, "sigtype",
00542 "none", NULL);
00543 setVarDefault(-1, "_buildshell",
00544 "/bin/sh", NULL);
00545
00546 setPathDefault(-1, "_builddir", "BUILD");
00547 setPathDefault(-1, "_rpmdir", "RPMS");
00548 setPathDefault(-1, "_srcrpmdir", "SRPMS");
00549 setPathDefault(-1, "_sourcedir", "SOURCES");
00550 setPathDefault(-1, "_specdir", "SPECS");
00551
00552 }
00553
00554
00555 static int doReadRC( FD_t fd, const char * urlfn)
00556
00557
00558 {
00559 const char *s;
00560 char *se, *next;
00561 int linenum = 0;
00562 struct rpmOption searchOption, * option;
00563 int rc;
00564
00565
00566 { off_t size = fdSize(fd);
00567 size_t nb = (size >= 0 ? size : (8*BUFSIZ - 2));
00568 if (nb == 0) {
00569 (void) Fclose(fd);
00570 return 0;
00571 }
00572 next = alloca(nb + 2);
00573 next[0] = '\0';
00574 rc = Fread(next, sizeof(*next), nb, fd);
00575 if (Ferror(fd) || (size > 0 && rc != nb)) {
00576 rpmError(RPMERR_RPMRC, _("Failed to read %s: %s.\n"), urlfn,
00577 Fstrerror(fd));
00578 rc = 1;
00579 } else
00580 rc = 0;
00581 (void) Fclose(fd);
00582 if (rc) return rc;
00583 next[nb] = '\n';
00584 next[nb + 1] = '\0';
00585 }
00586
00587
00588 while (*next != '\0') {
00589 linenum++;
00590
00591 s = se = next;
00592
00593
00594 while (*se && *se != '\n') se++;
00595 if (*se != '\0') *se++ = '\0';
00596 next = se;
00597
00598
00599 while (*s && xisspace(*s)) s++;
00600
00601
00602 if (*s == '#' || *s == '\0') continue;
00603
00604
00605 se = (char *)s;
00606 while (*se && !xisspace(*se) && *se != ':') se++;
00607
00608 if (xisspace(*se)) {
00609 *se++ = '\0';
00610 while (*se && xisspace(*se) && *se != ':') se++;
00611 }
00612
00613 if (*se != ':') {
00614 rpmError(RPMERR_RPMRC, _("missing ':' (found 0x%02x) at %s:%d\n"),
00615 (unsigned)(0xff & *se), urlfn, linenum);
00616 return 1;
00617 }
00618 *se++ = '\0';
00619 while (*se && xisspace(*se)) se++;
00620
00621
00622 searchOption.name = s;
00623 option = bsearch(&searchOption, optionTable, optionTableSize,
00624 sizeof(optionTable[0]), optionCompare);
00625
00626 if (option) {
00627 const char *arch, *val, *fn;
00628
00629 arch = val = fn = NULL;
00630 if (*se == '\0') {
00631 rpmError(RPMERR_RPMRC, _("missing argument for %s at %s:%d\n"),
00632 option->name, urlfn, linenum);
00633 return 1;
00634 }
00635
00636 switch (option->var) {
00637 case RPMVAR_INCLUDE:
00638 { FD_t fdinc;
00639
00640 s = se;
00641 while (*se && !xisspace(*se)) se++;
00642 if (*se != '\0') *se++ = '\0';
00643
00644 rpmRebuildTargetVars(NULL, NULL);
00645
00646 fn = rpmGetPath(s, NULL);
00647 if (fn == NULL || *fn == '\0') {
00648 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\"\n"),
00649 option->name, urlfn, linenum, s);
00650 fn = _free(fn);
00651 return 1;
00652
00653 }
00654
00655 fdinc = Fopen(fn, "r.fpio");
00656 if (fdinc == NULL || Ferror(fdinc)) {
00657 rpmError(RPMERR_RPMRC, _("cannot open %s at %s:%d: %s\n"),
00658 fn, urlfn, linenum, Fstrerror(fdinc));
00659 rc = 1;
00660 } else {
00661 rc = doReadRC(fdinc, fn);
00662 }
00663 fn = _free(fn);
00664 if (rc) return rc;
00665 continue;
00666 } break;
00667 case RPMVAR_MACROFILES:
00668 fn = rpmGetPath(se, NULL);
00669 if (fn == NULL || *fn == '\0') {
00670 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\"\n"),
00671 option->name, urlfn, linenum, fn);
00672 fn = _free(fn);
00673 return 1;
00674 }
00675 se = (char *)fn;
00676 break;
00677 case RPMVAR_PROVIDES:
00678 { char *t;
00679 s = rpmGetVar(RPMVAR_PROVIDES);
00680 if (s == NULL) s = "";
00681 fn = t = xmalloc(strlen(s) + strlen(se) + 2);
00682 while (*s != '\0') *t++ = *s++;
00683 *t++ = ' ';
00684 while (*se != '\0') *t++ = *se++;
00685 *t++ = '\0';
00686 se = (char *)fn;
00687 } break;
00688 default:
00689 break;
00690 }
00691
00692 if (option->archSpecific) {
00693 arch = se;
00694 while (*se && !xisspace(*se)) se++;
00695 if (*se == '\0') {
00696 rpmError(RPMERR_RPMRC,
00697 _("missing architecture for %s at %s:%d\n"),
00698 option->name, urlfn, linenum);
00699 return 1;
00700 }
00701 *se++ = '\0';
00702 while (*se && xisspace(*se)) se++;
00703 if (*se == '\0') {
00704 rpmError(RPMERR_RPMRC,
00705 _("missing argument for %s at %s:%d\n"),
00706 option->name, urlfn, linenum);
00707 return 1;
00708 }
00709 }
00710
00711 val = se;
00712
00713
00714 if (option->macroize &&
00715 (arch == NULL || !strcmp(arch, current[ARCH]))) {
00716 char *n, *name;
00717 n = name = xmalloc(strlen(option->name)+2);
00718 if (option->localize)
00719 *n++ = '_';
00720 strcpy(n, option->name);
00721 addMacro(NULL, name, NULL, val, RMIL_RPMRC);
00722 free(name);
00723 }
00724 rpmSetVarArch(option->var, val, arch);
00725 fn = _free(fn);
00726
00727 } else {
00728 int gotit;
00729 int i;
00730
00731 gotit = 0;
00732
00733 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
00734 if (!strncmp(tables[i].key, s, strlen(tables[i].key)))
00735 break;
00736 }
00737
00738 if (i < RPM_MACHTABLE_COUNT) {
00739 const char *rest = s + strlen(tables[i].key);
00740 if (*rest == '_') rest++;
00741
00742 if (!strcmp(rest, "compat")) {
00743 if (machCompatCacheAdd(se, urlfn, linenum,
00744 &tables[i].cache))
00745 return 1;
00746 gotit = 1;
00747 } else if (tables[i].hasTranslate &&
00748 !strcmp(rest, "translate")) {
00749 if (addDefault(&tables[i].defaults,
00750 &tables[i].defaultsLength,
00751 se, urlfn, linenum))
00752 return 1;
00753 gotit = 1;
00754 } else if (tables[i].hasCanon &&
00755 !strcmp(rest, "canon")) {
00756 if (addCanon(&tables[i].canons, &tables[i].canonsLength,
00757 se, urlfn, linenum))
00758 return 1;
00759 gotit = 1;
00760 }
00761 }
00762
00763 if (!gotit) {
00764 rpmError(RPMERR_RPMRC, _("bad option '%s' at %s:%d\n"),
00765 s, urlfn, linenum);
00766 }
00767 }
00768 }
00769
00770
00771 return 0;
00772 }
00773
00774
00775
00778
00779 static int rpmPlatform(const char * platform)
00780
00781
00782
00783
00784 {
00785 char *cpu = NULL, *vendor = NULL, *os = NULL, *gnu = NULL;
00786 char * b = NULL;
00787 ssize_t blen = 0;
00788 int init_platform = 0;
00789 char * p, * pe;
00790 int rc;
00791
00792 rc = rpmioSlurp(platform, &b, &blen);
00793
00794 if (rc || b == NULL || blen <= 0) {
00795 rc = -1;
00796 goto exit;
00797 }
00798
00799 p = b;
00800 for (pe = p; p && *p; p = pe) {
00801 pe = strchr(p, '\n');
00802 if (pe)
00803 *pe++ = '\0';
00804
00805 while (*p && isspace(*p))
00806 p++;
00807 if (*p == '\0' || *p == '#')
00808 continue;
00809
00810 if (init_platform) {
00811 char * t = p + strlen(p);
00812
00813 while (--t > p && isspace(*t))
00814 *t = '\0';
00815 if (t > p) {
00816 platpat = xrealloc(platpat, (nplatpat + 2) * sizeof(*platpat));
00817
00818 platpat[nplatpat] = xstrdup(p);
00819 nplatpat++;
00820 platpat[nplatpat] = NULL;
00821
00822 }
00823 continue;
00824 }
00825
00826 cpu = p;
00827 vendor = "unknown";
00828 os = "unknown";
00829 gnu = NULL;
00830 while (*p && !(*p == '-' || isspace(*p)))
00831 p++;
00832 if (*p != '\0') *p++ = '\0';
00833
00834 vendor = p;
00835 while (*p && !(*p == '-' || isspace(*p)))
00836 p++;
00837
00838 if (*p != '-') {
00839 if (*p != '\0') *p++ = '\0';
00840 os = vendor;
00841 vendor = "unknown";
00842 } else {
00843 if (*p != '\0') *p++ = '\0';
00844
00845 os = p;
00846 while (*p && !(*p == '-' || isspace(*p)))
00847 p++;
00848 if (*p == '-') {
00849 *p++ = '\0';
00850
00851 gnu = p;
00852 while (*p && !(*p == '-' || isspace(*p)))
00853 p++;
00854 }
00855 if (*p != '\0') *p++ = '\0';
00856 }
00857
00858
00859 addMacro(NULL, "_host_cpu", NULL, cpu, -1);
00860 addMacro(NULL, "_host_vendor", NULL, vendor, -1);
00861 addMacro(NULL, "_host_os", NULL, os, -1);
00862
00863 platpat = xrealloc(platpat, (nplatpat + 2) * sizeof(*platpat));
00864
00865 platpat[nplatpat] = rpmExpand("%{_host_cpu}-%{_host_vendor}-%{_host_os}", (gnu && *gnu ? "-" : NULL), gnu, NULL);
00866 nplatpat++;
00867 platpat[nplatpat] = NULL;
00868
00869
00870 init_platform++;
00871 }
00872 rc = (init_platform ? 0 : -1);
00873
00874 exit:
00875
00876 b = _free(b);
00877
00878 return rc;
00879 }
00880
00881
00882
00883 # if defined(__linux__) && defined(__i386__)
00884 #include <setjmp.h>
00885 #include <signal.h>
00886
00887
00888
00889
00890 static inline void cpuid(unsigned int op, int *eax, int *ebx, int *ecx, int *edx)
00891
00892 {
00893 #ifdef __LCLINT__
00894 *eax = *ebx = *ecx = *edx = 0;
00895 #endif
00896 asm volatile (
00897 "pushl %%ebx \n"
00898 "cpuid \n"
00899 "movl %%ebx, %%esi \n"
00900 "popl %%ebx \n"
00901 : "=a" (*eax), "=S" (*ebx), "=c" (*ecx), "=d" (*edx)
00902 : "a" (op));
00903 }
00904
00905
00906
00907
00908 static inline unsigned int cpuid_eax(unsigned int op)
00909
00910 {
00911 unsigned int tmp, val;
00912 cpuid(op, &val, &tmp, &tmp, &tmp);
00913 return val;
00914 }
00915
00916 static inline unsigned int cpuid_ebx(unsigned int op)
00917
00918 {
00919 unsigned int tmp, val;
00920 cpuid(op, &tmp, &val, &tmp, &tmp);
00921 return val;
00922 }
00923
00924 static inline unsigned int cpuid_ecx(unsigned int op)
00925
00926 {
00927 unsigned int tmp, val;
00928 cpuid(op, &tmp, &tmp, &val, &tmp);
00929 return val;
00930 }
00931
00932 static inline unsigned int cpuid_edx(unsigned int op)
00933
00934 {
00935 unsigned int tmp, val;
00936 cpuid(op, &tmp, &tmp, &tmp, &val);
00937 return val;
00938 }
00939
00940
00941 static sigjmp_buf jenv;
00942
00943 static inline void model3(int _unused)
00944
00945
00946 {
00947 siglongjmp(jenv, 1);
00948 }
00949
00950 static inline int RPMClass(void)
00951
00952
00953 {
00954 int cpu;
00955 unsigned int tfms, junk, cap, capamd;
00956 struct sigaction oldsa;
00957
00958 sigaction(SIGILL, NULL, &oldsa);
00959 signal(SIGILL, model3);
00960
00961 if (sigsetjmp(jenv, 1)) {
00962 sigaction(SIGILL, &oldsa, NULL);
00963 return 3;
00964 }
00965
00966 if (cpuid_eax(0x000000000)==0) {
00967 sigaction(SIGILL, &oldsa, NULL);
00968 return 4;
00969 }
00970
00971 cpuid(0x00000001, &tfms, &junk, &junk, &cap);
00972 cpuid(0x80000001, &junk, &junk, &junk, &capamd);
00973
00974 cpu = (tfms>>8)&15;
00975
00976 sigaction(SIGILL, &oldsa, NULL);
00977
00978 if (cpu < 6)
00979 return cpu;
00980
00981 if (cap & (1<<15)) {
00982
00983 if (capamd & (1<<30))
00984 return 7;
00985 return 6;
00986 }
00987
00988 return 5;
00989 }
00990
00991
00992 static int is_athlon(void)
00993
00994 {
00995 unsigned int eax, ebx, ecx, edx;
00996 char vendor[16];
00997 int i;
00998
00999 cpuid (0, &eax, &ebx, &ecx, &edx);
01000
01001
01002
01003 memset(vendor, 0, sizeof(vendor));
01004
01005 for (i=0; i<4; i++)
01006 vendor[i] = (unsigned char) (ebx >>(8*i));
01007 for (i=0; i<4; i++)
01008 vendor[4+i] = (unsigned char) (edx >>(8*i));
01009 for (i=0; i<4; i++)
01010 vendor[8+i] = (unsigned char) (ecx >>(8*i));
01011
01012 if (strncmp(vendor, "AuthenticAMD", 12) != 0)
01013 return 0;
01014
01015 return 1;
01016 }
01017
01018 static int is_pentium3()
01019 {
01020 unsigned int eax, ebx, ecx, edx, family, model;
01021 char vendor[16];
01022 cpuid(0, &eax, &ebx, &ecx, &edx);
01023 memset(vendor, 0, sizeof(vendor));
01024 *((unsigned int *)&vendor[0]) = ebx;
01025 *((unsigned int *)&vendor[4]) = edx;
01026 *((unsigned int *)&vendor[8]) = ecx;
01027 if (strncmp(vendor, "GenuineIntel", 12) != 0)
01028 return 0;
01029 cpuid(1, &eax, &ebx, &ecx, &edx);
01030 family = (eax >> 8) & 0x0f;
01031 model = (eax >> 4) & 0x0f;
01032 if (family == 6)
01033 switch (model)
01034 {
01035 case 7:
01036 case 8:
01037 case 9:
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050 case 10:
01051 case 11:
01052 return 1;
01053 }
01054 return 0;
01055 }
01056
01057 static int is_pentium4()
01058 {
01059 unsigned int eax, ebx, ecx, edx, family, model;
01060 char vendor[16];
01061 cpuid(0, &eax, &ebx, &ecx, &edx);
01062 memset(vendor, 0, sizeof(vendor));
01063 *((unsigned int *)&vendor[0]) = ebx;
01064 *((unsigned int *)&vendor[4]) = edx;
01065 *((unsigned int *)&vendor[8]) = ecx;
01066 if (strncmp(vendor, "GenuineIntel", 12) != 0)
01067 return 0;
01068 cpuid(1, &eax, &ebx, &ecx, &edx);
01069 family = (eax >> 8) & 0x0f;
01070 model = (eax >> 4) & 0x0f;
01071 if (family == 15)
01072 switch (model)
01073 {
01074 case 0:
01075 case 1:
01076 case 2:
01077
01078
01079 case 3:
01080 return 1;
01081 }
01082 return 0;
01083 }
01084
01085 #endif
01086
01087 #if defined(__linux__) && defined(__powerpc__)
01088 static jmp_buf mfspr_jmpbuf;
01089
01090 static void mfspr_ill(int notused)
01091 {
01092 longjmp(mfspr_jmpbuf, -1);
01093 }
01094 #endif
01095
01098 static void defaultMachine( const char ** arch,
01099 const char ** os)
01100
01101
01102 {
01103 static struct utsname un;
01104 static int gotDefaults = 0;
01105 char * chptr;
01106 canonEntry canon;
01107 int rc;
01108
01109 while (!gotDefaults) {
01110 if (!rpmPlatform(platform)) {
01111 const char * s;
01112 s = rpmExpand("%{_host_cpu}", NULL);
01113 if (s) {
01114 strncpy(un.machine, s, sizeof(un.machine));
01115 un.machine[sizeof(un.machine)-1] = '\0';
01116 s = _free(s);
01117 }
01118 s = rpmExpand("%{_host_os}", NULL);
01119 if (s) {
01120 strncpy(un.sysname, s, sizeof(un.sysname));
01121 un.sysname[sizeof(un.sysname)-1] = '\0';
01122 s = _free(s);
01123 }
01124 gotDefaults = 1;
01125 break;
01126 }
01127 rc = uname(&un);
01128 if (rc < 0) return;
01129
01130 #if !defined(__linux__)
01131 #ifdef SNI
01132
01133
01134
01135 strncpy(un.sysname, "SINIX", sizeof(un.sysname));
01136 #endif
01137
01138 if (!strcmp(un.sysname, "AIX")) {
01139 strcpy(un.machine, __power_pc() ? "ppc" : "rs6000");
01140 sprintf(un.sysname,"aix%s.%s", un.version, un.release);
01141 }
01142 else if(!strcmp(un.sysname, "Darwin")) {
01143 #ifdef __ppc__
01144 strcpy(un.machine, "ppc");
01145 #else ifdef __i386__
01146 strcpy(un.machine, "i386");
01147 #endif
01148 }
01149 else if (!strcmp(un.sysname, "SunOS")) {
01150 if (!strncmp(un.release,"4", 1)) {
01151 int fd;
01152 for (fd = 0;
01153 (un.release[fd] != 0 && (fd < sizeof(un.release)));
01154 fd++) {
01155 if (!xisdigit(un.release[fd]) && (un.release[fd] != '.')) {
01156 un.release[fd] = 0;
01157 break;
01158 }
01159 }
01160 sprintf(un.sysname,"sunos%s",un.release);
01161 }
01162
01163 else
01164 sprintf(un.sysname, "solaris%1d%s", atoi(un.release)-3,
01165 un.release+1+(atoi(un.release)/10));
01166
01167
01168
01169
01170 if (!strcmp(un.machine, "i86pc"))
01171 sprintf(un.machine, "i386");
01172 }
01173 else if (!strcmp(un.sysname, "HP-UX"))
01174
01175 sprintf(un.sysname, "hpux%s", strpbrk(un.release, "123456789"));
01176 else if (!strcmp(un.sysname, "OSF1"))
01177
01178 sprintf(un.sysname, "osf%s", strpbrk(un.release, "123456789"));
01179 else if (!strncmp(un.sysname, "IP", 2))
01180 un.sysname[2] = '\0';
01181 else if (!strncmp(un.sysname, "SINIX", 5)) {
01182 sprintf(un.sysname, "sinix%s",un.release);
01183 if (!strncmp(un.machine, "RM", 2))
01184 sprintf(un.machine, "mips");
01185 }
01186 else if ((!strncmp(un.machine, "34", 2) ||
01187 !strncmp(un.machine, "33", 2)) && \
01188 !strncmp(un.release, "4.0", 3))
01189 {
01190
01191 char * prelid = NULL;
01192 FD_t fd = Fopen("/etc/.relid", "r.fdio");
01193 int gotit = 0;
01194
01195 if (fd != NULL && !Ferror(fd)) {
01196 chptr = xcalloc(1, 256);
01197 { int irelid = Fread(chptr, sizeof(*chptr), 256, fd);
01198 (void) Fclose(fd);
01199
01200 if (irelid > 0) {
01201 if ((prelid = strstr(chptr, "RELEASE "))){
01202 prelid += strlen("RELEASE ")+1;
01203 sprintf(un.sysname,"ncr-sysv4.%.*s",1,prelid);
01204 gotit = 1;
01205 }
01206 }
01207 }
01208 chptr = _free (chptr);
01209 }
01210
01211 if (!gotit)
01212 strcpy(un.sysname,"ncr-sysv4");
01213
01214 strcpy(un.machine,"i486");
01215 }
01216
01217 #endif
01218
01219
01220 for (chptr = un.machine; *chptr != '\0'; chptr++)
01221 if (*chptr == '/') *chptr = '-';
01222
01223 # if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
01224
01225 strcpy(un.machine, "mipsel");
01226 # elif defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB)
01227
01228 strcpy(un.machine, "mips");
01229 # endif
01230
01231 # if defined(__hpux) && defined(_SC_CPU_VERSION)
01232 {
01233 # if !defined(CPU_PA_RISC1_2)
01234 # define CPU_PA_RISC1_2 0x211
01235 # endif
01236 # if !defined(CPU_PA_RISC2_0)
01237 # define CPU_PA_RISC2_0 0x214
01238 # endif
01239 int cpu_version = sysconf(_SC_CPU_VERSION);
01240
01241 # if defined(CPU_HP_MC68020)
01242 if (cpu_version == CPU_HP_MC68020)
01243 strcpy(un.machine, "m68k");
01244 # endif
01245 # if defined(CPU_HP_MC68030)
01246 if (cpu_version == CPU_HP_MC68030)
01247 strcpy(un.machine, "m68k");
01248 # endif
01249 # if defined(CPU_HP_MC68040)
01250 if (cpu_version == CPU_HP_MC68040)
01251 strcpy(un.machine, "m68k");
01252 # endif
01253
01254 # if defined(CPU_PA_RISC1_0)
01255 if (cpu_version == CPU_PA_RISC1_0)
01256 strcpy(un.machine, "hppa1.0");
01257 # endif
01258 # if defined(CPU_PA_RISC1_1)
01259 if (cpu_version == CPU_PA_RISC1_1)
01260 strcpy(un.machine, "hppa1.1");
01261 # endif
01262 # if defined(CPU_PA_RISC1_2)
01263 if (cpu_version == CPU_PA_RISC1_2)
01264 strcpy(un.machine, "hppa1.2");
01265 # endif
01266 # if defined(CPU_PA_RISC2_0)
01267 if (cpu_version == CPU_PA_RISC2_0)
01268 strcpy(un.machine, "hppa2.0");
01269 # endif
01270 }
01271 # endif
01272
01273 # if defined(__linux__) && defined(__sparc__)
01274 if (!strcmp(un.machine, "sparc")) {
01275 #define PERS_LINUX 0x00000000
01276 #define PERS_LINUX_32BIT 0x00800000
01277 #define PERS_LINUX32 0x00000008
01278
01279 extern int personality(unsigned long);
01280 int oldpers;
01281
01282 oldpers = personality(PERS_LINUX_32BIT);
01283 if (oldpers != -1) {
01284 if (personality(PERS_LINUX) != -1) {
01285 uname(&un);
01286 if (! strcmp(un.machine, "sparc64")) {
01287 strcpy(un.machine, "sparcv9");
01288 oldpers = PERS_LINUX32;
01289 }
01290 }
01291 personality(oldpers);
01292 }
01293 }
01294 # endif
01295
01296 # if defined(__GNUC__) && defined(__alpha__)
01297 {
01298 unsigned long amask, implver;
01299 register long v0 __asm__("$0") = -1;
01300 __asm__ (".long 0x47e00c20" : "=r"(v0) : "0"(v0));
01301 amask = ~v0;
01302 __asm__ (".long 0x47e03d80" : "=r"(v0));
01303 implver = v0;
01304 switch (implver) {
01305 case 1:
01306 switch (amask) {
01307 case 0: strcpy(un.machine, "alphaev5"); break;
01308 case 1: strcpy(un.machine, "alphaev56"); break;
01309 case 0x101: strcpy(un.machine, "alphapca56"); break;
01310 }
01311 break;
01312 case 2:
01313 switch (amask) {
01314 case 0x303: strcpy(un.machine, "alphaev6"); break;
01315 case 0x307: strcpy(un.machine, "alphaev67"); break;
01316 }
01317 break;
01318 }
01319 }
01320 # endif
01321
01322 # if defined(__linux__) && defined(__i386__)
01323 {
01324 char class = (char) (RPMClass() | '0');
01325
01326 if ((class == '6' && is_athlon()) || class == '7')
01327 strcpy(un.machine, "athlon");
01328 else if (is_pentium4())
01329 strcpy(un.machine, "pentium4");
01330 else if (is_pentium3())
01331 strcpy(un.machine, "pentium3");
01332 else if (strchr("3456", un.machine[1]) && un.machine[1] != class)
01333 un.machine[1] = class;
01334 }
01335 # endif
01336
01337
01338 canon = lookupInCanonTable(un.machine,
01339 tables[RPM_MACHTABLE_INSTARCH].canons,
01340 tables[RPM_MACHTABLE_INSTARCH].canonsLength);
01341 if (canon)
01342 strcpy(un.machine, canon->short_name);
01343
01344 canon = lookupInCanonTable(un.sysname,
01345 tables[RPM_MACHTABLE_INSTOS].canons,
01346 tables[RPM_MACHTABLE_INSTOS].canonsLength);
01347 if (canon)
01348 strcpy(un.sysname, canon->short_name);
01349 gotDefaults = 1;
01350 break;
01351 }
01352
01353 if (arch) *arch = un.machine;
01354 if (os) *os = un.sysname;
01355 }
01356
01357 static
01358 const char * rpmGetVarArch(int var, const char * arch)
01359
01360 {
01361 const struct rpmvarValue * next;
01362
01363 if (arch == NULL) arch = current[ARCH];
01364
01365 if (arch) {
01366 next = &values[var];
01367 while (next) {
01368 if (next->arch && !strcmp(next->arch, arch)) return next->value;
01369 next = next->next;
01370 }
01371 }
01372
01373 next = values + var;
01374 while (next && next->arch) next = next->next;
01375
01376 return next ? next->value : NULL;
01377 }
01378
01379 const char *rpmGetVar(int var)
01380 {
01381 return rpmGetVarArch(var, NULL);
01382 }
01383
01384
01385 static void freeRpmVar( struct rpmvarValue * orig)
01386
01387 {
01388 struct rpmvarValue * next, * var = orig;
01389
01390 while (var) {
01391 next = var->next;
01392 var->arch = _free(var->arch);
01393 var->value = _free(var->value);
01394
01395
01396 if (var != orig) var = _free(var);
01397
01398 var = next;
01399 }
01400 }
01401
01402 void rpmSetVar(int var, const char * val)
01403
01404
01405 {
01406
01407 freeRpmVar(&values[var]);
01408
01409 values[var].value = (val ? xstrdup(val) : NULL);
01410 }
01411
01412 static void rpmSetVarArch(int var, const char * val, const char * arch)
01413 {
01414 struct rpmvarValue * next = values + var;
01415
01416 if (next->value) {
01417 if (arch) {
01418 while (next->next) {
01419 if (next->arch && !strcmp(next->arch, arch)) break;
01420 next = next->next;
01421 }
01422 } else {
01423 while (next->next) {
01424 if (!next->arch) break;
01425 next = next->next;
01426 }
01427 }
01428
01429
01430 if (next->arch && arch && !strcmp(next->arch, arch)) {
01431
01432 next->value = _free(next->value);
01433 next->arch = _free(next->arch);
01434 } else if (next->arch || arch) {
01435 next->next = xmalloc(sizeof(*next->next));
01436 next = next->next;
01437 next->value = NULL;
01438 next->arch = NULL;
01439 next->next = NULL;
01440 }
01441 }
01442
01443 next->value = _free(next->value);
01444 next->value = xstrdup(val);
01445 next->arch = (arch ? xstrdup(arch) : NULL);
01446 }
01447
01448 void rpmSetTables(int archTable, int osTable)
01449
01450
01451 {
01452 const char * arch, * os;
01453
01454 defaultMachine(&arch, &os);
01455
01456 if (currTables[ARCH] != archTable) {
01457 currTables[ARCH] = archTable;
01458 rebuildCompatTables(ARCH, arch);
01459 }
01460
01461 if (currTables[OS] != osTable) {
01462 currTables[OS] = osTable;
01463 rebuildCompatTables(OS, os);
01464 }
01465 }
01466
01467 int rpmMachineScore(int type, const char * name)
01468 {
01469 machEquivInfo info = machEquivSearch(&tables[type].equiv, name);
01470 return (info != NULL ? info->score : 0);
01471 }
01472
01473 void rpmGetMachine(const char ** arch, const char ** os)
01474 {
01475 if (arch)
01476 *arch = current[ARCH];
01477
01478 if (os)
01479 *os = current[OS];
01480 }
01481
01482 void rpmSetMachine(const char * arch, const char * os)
01483
01484
01485 {
01486 const char * host_cpu, * host_os;
01487
01488 defaultMachine(&host_cpu, &host_os);
01489
01490 if (arch == NULL) {
01491 arch = host_cpu;
01492 if (tables[currTables[ARCH]].hasTranslate)
01493 arch = lookupInDefaultTable(arch,
01494 tables[currTables[ARCH]].defaults,
01495 tables[currTables[ARCH]].defaultsLength);
01496 }
01497 if (arch == NULL) return;
01498
01499 if (os == NULL) {
01500 os = host_os;
01501 if (tables[currTables[OS]].hasTranslate)
01502 os = lookupInDefaultTable(os,
01503 tables[currTables[OS]].defaults,
01504 tables[currTables[OS]].defaultsLength);
01505 }
01506 if (os == NULL) return;
01507
01508 if (!current[ARCH] || strcmp(arch, current[ARCH])) {
01509 current[ARCH] = _free(current[ARCH]);
01510 current[ARCH] = xstrdup(arch);
01511 rebuildCompatTables(ARCH, host_cpu);
01512 }
01513
01514 if (!current[OS] || strcmp(os, current[OS])) {
01515 char * t = xstrdup(os);
01516 current[OS] = _free(current[OS]);
01517
01518
01519
01520
01521
01522
01523
01524
01525 if (!strcmp(t, "linux"))
01526 *t = 'L';
01527 current[OS] = t;
01528
01529 rebuildCompatTables(OS, host_os);
01530 }
01531 }
01532
01533 static void rebuildCompatTables(int type, const char * name)
01534
01535 {
01536 machFindEquivs(&tables[currTables[type]].cache,
01537 &tables[currTables[type]].equiv,
01538 name);
01539 }
01540
01541 static void getMachineInfo(int type, const char ** name,
01542 int * num)
01543
01544 {
01545 canonEntry canon;
01546 int which = currTables[type];
01547
01548
01549 if (which >= 2) which -= 2;
01550
01551 canon = lookupInCanonTable(current[type],
01552 tables[which].canons,
01553 tables[which].canonsLength);
01554
01555 if (canon) {
01556 if (num) *num = canon->num;
01557 if (name) *name = canon->short_name;
01558 } else {
01559 if (num) *num = 255;
01560 if (name) *name = current[type];
01561
01562 if (tables[currTables[type]].hasCanon) {
01563 rpmMessage(RPMMESS_WARNING, _("Unknown system: %s\n"), current[type]);
01564 rpmMessage(RPMMESS_WARNING, _("Please contact rpm-maint@lists.rpm.org\n"));
01565 }
01566 }
01567 }
01568
01569 void rpmGetArchInfo(const char ** name, int * num)
01570 {
01571 getMachineInfo(ARCH, name, num);
01572 }
01573
01574 void rpmGetOsInfo(const char ** name, int * num)
01575 {
01576 getMachineInfo(OS, name, num);
01577 }
01578
01579 static void rpmRebuildTargetVars(const char ** target, const char ** canontarget)
01580 {
01581
01582 char *ca = NULL, *co = NULL, *ct = NULL;
01583 int x;
01584
01585
01586
01587 rpmSetMachine(NULL, NULL);
01588 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
01589 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
01590
01591
01592 if (target && *target) {
01593 char *c;
01594
01595 ca = xstrdup(*target);
01596 if ((c = strchr(ca, '-')) != NULL) {
01597 *c++ = '\0';
01598
01599 if ((co = strrchr(c, '-')) == NULL) {
01600 co = c;
01601 } else {
01602 if (!xstrcasecmp(co, "-gnu"))
01603 *co = '\0';
01604 if ((co = strrchr(c, '-')) == NULL)
01605 co = c;
01606 else
01607 co++;
01608 }
01609 if (co != NULL) co = xstrdup(co);
01610 }
01611 } else {
01612 const char *a = NULL;
01613 const char *o = NULL;
01614
01615 rpmGetArchInfo(&a, NULL);
01616 ca = (a) ? xstrdup(a) : NULL;
01617 rpmGetOsInfo(&o, NULL);
01618 co = (o) ? xstrdup(o) : NULL;
01619 }
01620
01621
01622
01623 if (ca == NULL) {
01624 const char *a = NULL;
01625 defaultMachine(&a, NULL);
01626 ca = (a) ? xstrdup(a) : NULL;
01627 }
01628 for (x = 0; ca[x] != '\0'; x++)
01629 ca[x] = xtolower(ca[x]);
01630
01631 if (co == NULL) {
01632 const char *o = NULL;
01633 defaultMachine(NULL, &o);
01634 co = (o) ? xstrdup(o) : NULL;
01635 }
01636 for (x = 0; co[x] != '\0'; x++)
01637 co[x] = xtolower(co[x]);
01638
01639
01640 if (ct == NULL) {
01641 ct = xmalloc(strlen(ca) + sizeof("-") + strlen(co));
01642 sprintf(ct, "%s-%s", ca, co);
01643 }
01644
01645
01646
01647
01648
01649 delMacro(NULL, "_target");
01650 addMacro(NULL, "_target", NULL, ct, RMIL_RPMRC);
01651 delMacro(NULL, "_target_cpu");
01652 addMacro(NULL, "_target_cpu", NULL, ca, RMIL_RPMRC);
01653 delMacro(NULL, "_target_os");
01654 addMacro(NULL, "_target_os", NULL, co, RMIL_RPMRC);
01655
01656
01657
01658 { const char *optflags = rpmGetVarArch(RPMVAR_OPTFLAGS, ca);
01659 if (optflags != NULL) {
01660 delMacro(NULL, "optflags");
01661 addMacro(NULL, "optflags", NULL, optflags, RMIL_RPMRC);
01662 }
01663 }
01664
01665
01666 if (canontarget)
01667 *canontarget = ct;
01668 else
01669 ct = _free(ct);
01670
01671 ca = _free(ca);
01672
01673 co = _free(co);
01674
01675 }
01676
01677 void rpmFreeRpmrc(void)
01678
01679
01680
01681
01682 {
01683 int i, j, k;
01684
01685
01686 if (platpat)
01687 for (i = 0; i < nplatpat; i++)
01688 platpat[i] = _free(platpat[i]);
01689 platpat = _free(platpat);
01690
01691 nplatpat = 0;
01692
01693 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
01694 tableType t;
01695 t = tables + i;
01696 if (t->equiv.list) {
01697 for (j = 0; j < t->equiv.count; j++)
01698 t->equiv.list[j].name = _free(t->equiv.list[j].name);
01699 t->equiv.list = _free(t->equiv.list);
01700 t->equiv.count = 0;
01701 }
01702 if (t->cache.cache) {
01703 for (j = 0; j < t->cache.size; j++) {
01704 machCacheEntry e;
01705 e = t->cache.cache + j;
01706 if (e == NULL)
01707 continue;
01708 e->name = _free(e->name);
01709 if (e->equivs) {
01710 for (k = 0; k < e->count; k++)
01711 e->equivs[k] = _free(e->equivs[k]);
01712 e->equivs = _free(e->equivs);
01713 }
01714 }
01715 t->cache.cache = _free(t->cache.cache);
01716 t->cache.size = 0;
01717 }
01718 if (t->defaults) {
01719 for (j = 0; j < t->defaultsLength; j++) {
01720 t->defaults[j].name = _free(t->defaults[j].name);
01721 t->defaults[j].defName = _free(t->defaults[j].defName);
01722 }
01723 t->defaults = _free(t->defaults);
01724 t->defaultsLength = 0;
01725 }
01726 if (t->canons) {
01727 for (j = 0; j < t->canonsLength; j++) {
01728 t->canons[j].name = _free(t->canons[j].name);
01729 t->canons[j].short_name = _free(t->canons[j].short_name);
01730 }
01731 t->canons = _free(t->canons);
01732 t->canonsLength = 0;
01733 }
01734 }
01735
01736 for (i = 0; i < RPMVAR_NUM; i++) {
01737 struct rpmvarValue * vp;
01738 while ((vp = values[i].next) != NULL) {
01739 values[i].next = vp->next;
01740 vp->value = _free(vp->value);
01741 vp->arch = _free(vp->arch);
01742 vp = _free(vp);
01743 }
01744 values[i].value = _free(values[i].value);
01745 values[i].arch = _free(values[i].arch);
01746 }
01747 current[OS] = _free(current[OS]);
01748 current[ARCH] = _free(current[ARCH]);
01749 defaultsInitialized = 0;
01750
01751 return;
01752
01753 }
01754
01760 static int rpmReadRC( const char * rcfiles)
01761
01762
01763
01764
01765 {
01766 char *myrcfiles, *r, *re;
01767 int rc;
01768
01769 if (!defaultsInitialized) {
01770 setDefaults();
01771 defaultsInitialized = 1;
01772 }
01773
01774 if (rcfiles == NULL)
01775 rcfiles = defrcfiles;
01776
01777
01778 rc = 0;
01779 for (r = myrcfiles = xstrdup(rcfiles); r && *r != '\0'; r = re) {
01780 char fn[4096];
01781 FD_t fd;
01782
01783
01784 for (re = r; (re = strchr(re, ':')) != NULL; re++) {
01785 if (!(re[1] == '/' && re[2] == '/'))
01786 break;
01787 }
01788 if (re && *re == ':')
01789 *re++ = '\0';
01790 else
01791 re = r + strlen(r);
01792
01793
01794 fn[0] = '\0';
01795 if (r[0] == '~' && r[1] == '/') {
01796 const char * home = getenv("HOME");
01797 if (home == NULL) {
01798
01799 if (rcfiles == defrcfiles && myrcfiles != r)
01800 continue;
01801 rpmError(RPMERR_RPMRC, _("Cannot expand %s\n"), r);
01802 rc = 1;
01803 break;
01804 }
01805 if (strlen(home) > (sizeof(fn) - strlen(r))) {
01806 rpmError(RPMERR_RPMRC, _("Cannot read %s, HOME is too large.\n"),
01807 r);
01808 rc = 1;
01809 break;
01810 }
01811 strcpy(fn, home);
01812 r++;
01813 }
01814 strncat(fn, r, sizeof(fn) - (strlen(fn) + 1));
01815 fn[sizeof(fn)-1] = '\0';
01816
01817
01818 fd = Fopen(fn, "r.fpio");
01819 if (fd == NULL || Ferror(fd)) {
01820
01821 if (rcfiles == defrcfiles && myrcfiles != r)
01822 continue;
01823 rpmError(RPMERR_RPMRC, _("Unable to open %s for reading: %s.\n"),
01824 fn, Fstrerror(fd));
01825 rc = 1;
01826 break;
01827 } else {
01828 rc = doReadRC(fd, fn);
01829 }
01830 if (rc) break;
01831 }
01832 myrcfiles = _free(myrcfiles);
01833 if (rc)
01834 return rc;
01835
01836 rpmSetMachine(NULL, NULL);
01837
01838 { const char *mfpath;
01839
01840 if ((mfpath = rpmGetVar(RPMVAR_MACROFILES)) != NULL) {
01841 mfpath = xstrdup(mfpath);
01842 rpmInitMacros(NULL, mfpath);
01843 mfpath = _free(mfpath);
01844 }
01845
01846 }
01847
01848 return rc;
01849 }
01850
01851 int rpmReadConfigFiles(const char * file, const char * target)
01852 {
01853
01854
01855
01856 rpmRebuildTargetVars(&target, NULL);
01857
01858
01859 if (rpmReadRC(file)) return -1;
01860
01861
01862 rpmRebuildTargetVars(&target, NULL);
01863
01864
01865
01866 { const char *cpu = rpmExpand("%{_target_cpu}", NULL);
01867 const char *os = rpmExpand("%{_target_os}", NULL);
01868 rpmSetMachine(cpu, os);
01869 cpu = _free(cpu);
01870 os = _free(os);
01871 }
01872
01873
01874 #ifdef WITH_LUA
01875 (void)rpmluaGetPrintBuffer(NULL);
01876 #endif
01877
01878 return 0;
01879 }
01880
01881 int rpmShowRC(FILE * fp)
01882 {
01883 struct rpmOption *opt;
01884 int i;
01885 machEquivTable equivTable;
01886
01887
01888 fprintf(fp, "ARCHITECTURE AND OS:\n");
01889 fprintf(fp, "build arch : %s\n", current[ARCH]);
01890
01891 fprintf(fp, "compatible build archs:");
01892 equivTable = &tables[RPM_MACHTABLE_BUILDARCH].equiv;
01893 for (i = 0; i < equivTable->count; i++)
01894 fprintf(fp," %s", equivTable->list[i].name);
01895 fprintf(fp, "\n");
01896
01897 fprintf(fp, "build os : %s\n", current[OS]);
01898
01899 fprintf(fp, "compatible build os's :");
01900 equivTable = &tables[RPM_MACHTABLE_BUILDOS].equiv;
01901 for (i = 0; i < equivTable->count; i++)
01902 fprintf(fp," %s", equivTable->list[i].name);
01903 fprintf(fp, "\n");
01904
01905 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
01906 rpmSetMachine(NULL, NULL);
01907
01908 fprintf(fp, "install arch : %s\n", current[ARCH]);
01909 fprintf(fp, "install os : %s\n", current[OS]);
01910
01911 fprintf(fp, "compatible archs :");
01912 equivTable = &tables[RPM_MACHTABLE_INSTARCH].equiv;
01913 for (i = 0; i < equivTable->count; i++)
01914 fprintf(fp," %s", equivTable->list[i].name);
01915 fprintf(fp, "\n");
01916
01917 fprintf(fp, "compatible os's :");
01918 equivTable = &tables[RPM_MACHTABLE_INSTOS].equiv;
01919 for (i = 0; i < equivTable->count; i++)
01920 fprintf(fp," %s", equivTable->list[i].name);
01921 fprintf(fp, "\n");
01922
01923 fprintf(fp, "\nRPMRC VALUES:\n");
01924 for (i = 0, opt = optionTable; i < optionTableSize; i++, opt++) {
01925 const char *s = rpmGetVar(opt->var);
01926 if (s != NULL || rpmIsVerbose())
01927 fprintf(fp, "%-21s : %s\n", opt->name, s ? s : "(not set)");
01928 }
01929 fprintf(fp, "\n");
01930
01931 fprintf(fp, "Features supported by rpmlib:\n");
01932 rpmShowRpmlibProvides(fp);
01933 fprintf(fp, "\n");
01934
01935 rpmDumpMacroTable(NULL, fp);
01936
01937 return 0;
01938 }
01939