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 static int is_geode()
01086 {
01087 unsigned int eax, ebx, ecx, edx, family, model;
01088 char vendor[16];
01089
01090
01091 memset(vendor, 0, sizeof(vendor));
01092
01093 cpuid(0, &eax, &ebx, &ecx, &edx);
01094 memset(vendor, 0, sizeof(vendor));
01095 *((unsigned int *)&vendor[0]) = ebx;
01096 *((unsigned int *)&vendor[4]) = edx;
01097 *((unsigned int *)&vendor[8]) = ecx;
01098 if (strncmp(vendor, "AuthenticAMD", 12) != 0)
01099 return 0;
01100 cpuid(1, &eax, &ebx, &ecx, &edx);
01101 family = (eax >> 8) & 0x0f;
01102 model = (eax >> 4) & 0x0f;
01103 if (family == 5)
01104 switch (model)
01105 {
01106 case 10:
01107 return 1;
01108 }
01109 return 0;
01110 }
01111 #endif
01112
01113 #if defined(__linux__) && defined(__powerpc__)
01114 static jmp_buf mfspr_jmpbuf;
01115
01116 static void mfspr_ill(int notused)
01117 {
01118 longjmp(mfspr_jmpbuf, -1);
01119 }
01120 #endif
01121
01124 static void defaultMachine( const char ** arch,
01125 const char ** os)
01126
01127
01128 {
01129 static struct utsname un;
01130 static int gotDefaults = 0;
01131 char * chptr;
01132 canonEntry canon;
01133 int rc;
01134
01135 while (!gotDefaults) {
01136 if (!rpmPlatform(platform)) {
01137 const char * s;
01138 s = rpmExpand("%{_host_cpu}", NULL);
01139 if (s) {
01140 strncpy(un.machine, s, sizeof(un.machine));
01141 un.machine[sizeof(un.machine)-1] = '\0';
01142 s = _free(s);
01143 }
01144 s = rpmExpand("%{_host_os}", NULL);
01145 if (s) {
01146 strncpy(un.sysname, s, sizeof(un.sysname));
01147 un.sysname[sizeof(un.sysname)-1] = '\0';
01148 s = _free(s);
01149 }
01150 gotDefaults = 1;
01151 break;
01152 }
01153 rc = uname(&un);
01154 if (rc < 0) return;
01155
01156 #if !defined(__linux__)
01157 #ifdef SNI
01158
01159
01160
01161 strncpy(un.sysname, "SINIX", sizeof(un.sysname));
01162 #endif
01163
01164 if (!strcmp(un.sysname, "AIX")) {
01165 strcpy(un.machine, __power_pc() ? "ppc" : "rs6000");
01166 sprintf(un.sysname,"aix%s.%s", un.version, un.release);
01167 }
01168 else if(!strcmp(un.sysname, "Darwin")) {
01169 #ifdef __ppc__
01170 strcpy(un.machine, "ppc");
01171 #else ifdef __i386__
01172 strcpy(un.machine, "i386");
01173 #endif
01174 }
01175 else if (!strcmp(un.sysname, "SunOS")) {
01176 if (!strncmp(un.release,"4", 1)) {
01177 int fd;
01178 for (fd = 0;
01179 (un.release[fd] != 0 && (fd < sizeof(un.release)));
01180 fd++) {
01181 if (!xisdigit(un.release[fd]) && (un.release[fd] != '.')) {
01182 un.release[fd] = 0;
01183 break;
01184 }
01185 }
01186 sprintf(un.sysname,"sunos%s",un.release);
01187 }
01188
01189 else
01190 sprintf(un.sysname, "solaris%1d%s", atoi(un.release)-3,
01191 un.release+1+(atoi(un.release)/10));
01192
01193
01194
01195
01196 if (!strcmp(un.machine, "i86pc"))
01197 sprintf(un.machine, "i386");
01198 }
01199 else if (!strcmp(un.sysname, "HP-UX"))
01200
01201 sprintf(un.sysname, "hpux%s", strpbrk(un.release, "123456789"));
01202 else if (!strcmp(un.sysname, "OSF1"))
01203
01204 sprintf(un.sysname, "osf%s", strpbrk(un.release, "123456789"));
01205 else if (!strncmp(un.sysname, "IP", 2))
01206 un.sysname[2] = '\0';
01207 else if (!strncmp(un.sysname, "SINIX", 5)) {
01208 sprintf(un.sysname, "sinix%s",un.release);
01209 if (!strncmp(un.machine, "RM", 2))
01210 sprintf(un.machine, "mips");
01211 }
01212 else if ((!strncmp(un.machine, "34", 2) ||
01213 !strncmp(un.machine, "33", 2)) && \
01214 !strncmp(un.release, "4.0", 3))
01215 {
01216
01217 char * prelid = NULL;
01218 FD_t fd = Fopen("/etc/.relid", "r.fdio");
01219 int gotit = 0;
01220
01221 if (fd != NULL && !Ferror(fd)) {
01222 chptr = xcalloc(1, 256);
01223 { int irelid = Fread(chptr, sizeof(*chptr), 256, fd);
01224 (void) Fclose(fd);
01225
01226 if (irelid > 0) {
01227 if ((prelid = strstr(chptr, "RELEASE "))){
01228 prelid += strlen("RELEASE ")+1;
01229 sprintf(un.sysname,"ncr-sysv4.%.*s",1,prelid);
01230 gotit = 1;
01231 }
01232 }
01233 }
01234 chptr = _free (chptr);
01235 }
01236
01237 if (!gotit)
01238 strcpy(un.sysname,"ncr-sysv4");
01239
01240 strcpy(un.machine,"i486");
01241 }
01242
01243 #endif
01244
01245
01246 for (chptr = un.machine; *chptr != '\0'; chptr++)
01247 if (*chptr == '/') *chptr = '-';
01248
01249 # if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
01250
01251 strcpy(un.machine, "mipsel");
01252 # elif defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB)
01253
01254 strcpy(un.machine, "mips");
01255 # endif
01256
01257 # if defined(__hpux) && defined(_SC_CPU_VERSION)
01258 {
01259 # if !defined(CPU_PA_RISC1_2)
01260 # define CPU_PA_RISC1_2 0x211
01261 # endif
01262 # if !defined(CPU_PA_RISC2_0)
01263 # define CPU_PA_RISC2_0 0x214
01264 # endif
01265 int cpu_version = sysconf(_SC_CPU_VERSION);
01266
01267 # if defined(CPU_HP_MC68020)
01268 if (cpu_version == CPU_HP_MC68020)
01269 strcpy(un.machine, "m68k");
01270 # endif
01271 # if defined(CPU_HP_MC68030)
01272 if (cpu_version == CPU_HP_MC68030)
01273 strcpy(un.machine, "m68k");
01274 # endif
01275 # if defined(CPU_HP_MC68040)
01276 if (cpu_version == CPU_HP_MC68040)
01277 strcpy(un.machine, "m68k");
01278 # endif
01279
01280 # if defined(CPU_PA_RISC1_0)
01281 if (cpu_version == CPU_PA_RISC1_0)
01282 strcpy(un.machine, "hppa1.0");
01283 # endif
01284 # if defined(CPU_PA_RISC1_1)
01285 if (cpu_version == CPU_PA_RISC1_1)
01286 strcpy(un.machine, "hppa1.1");
01287 # endif
01288 # if defined(CPU_PA_RISC1_2)
01289 if (cpu_version == CPU_PA_RISC1_2)
01290 strcpy(un.machine, "hppa1.2");
01291 # endif
01292 # if defined(CPU_PA_RISC2_0)
01293 if (cpu_version == CPU_PA_RISC2_0)
01294 strcpy(un.machine, "hppa2.0");
01295 # endif
01296 }
01297 # endif
01298
01299 # if defined(__linux__) && defined(__sparc__)
01300 if (!strcmp(un.machine, "sparc")) {
01301 #define PERS_LINUX 0x00000000
01302 #define PERS_LINUX_32BIT 0x00800000
01303 #define PERS_LINUX32 0x00000008
01304
01305 extern int personality(unsigned long);
01306 int oldpers;
01307
01308 oldpers = personality(PERS_LINUX_32BIT);
01309 if (oldpers != -1) {
01310 if (personality(PERS_LINUX) != -1) {
01311 uname(&un);
01312 if (! strcmp(un.machine, "sparc64")) {
01313 strcpy(un.machine, "sparcv9");
01314 oldpers = PERS_LINUX32;
01315 }
01316 }
01317 personality(oldpers);
01318 }
01319 }
01320 # endif
01321
01322 # if defined(__GNUC__) && defined(__alpha__)
01323 {
01324 unsigned long amask, implver;
01325 register long v0 __asm__("$0") = -1;
01326 __asm__ (".long 0x47e00c20" : "=r"(v0) : "0"(v0));
01327 amask = ~v0;
01328 __asm__ (".long 0x47e03d80" : "=r"(v0));
01329 implver = v0;
01330 switch (implver) {
01331 case 1:
01332 switch (amask) {
01333 case 0: strcpy(un.machine, "alphaev5"); break;
01334 case 1: strcpy(un.machine, "alphaev56"); break;
01335 case 0x101: strcpy(un.machine, "alphapca56"); break;
01336 }
01337 break;
01338 case 2:
01339 switch (amask) {
01340 case 0x303: strcpy(un.machine, "alphaev6"); break;
01341 case 0x307: strcpy(un.machine, "alphaev67"); break;
01342 }
01343 break;
01344 }
01345 }
01346 # endif
01347
01348 # if defined(__linux__) && defined(__i386__)
01349 {
01350 char class = (char) (RPMClass() | '0');
01351
01352 if ((class == '6' && is_athlon()) || class == '7')
01353 strcpy(un.machine, "athlon");
01354 else if (is_pentium4())
01355 strcpy(un.machine, "pentium4");
01356 else if (is_pentium3())
01357 strcpy(un.machine, "pentium3");
01358 else if (is_geode())
01359 strcpy(un.machine, "geode");
01360 else if (strchr("3456", un.machine[1]) && un.machine[1] != class)
01361 un.machine[1] = class;
01362 }
01363 # endif
01364
01365
01366 canon = lookupInCanonTable(un.machine,
01367 tables[RPM_MACHTABLE_INSTARCH].canons,
01368 tables[RPM_MACHTABLE_INSTARCH].canonsLength);
01369 if (canon)
01370 strcpy(un.machine, canon->short_name);
01371
01372 canon = lookupInCanonTable(un.sysname,
01373 tables[RPM_MACHTABLE_INSTOS].canons,
01374 tables[RPM_MACHTABLE_INSTOS].canonsLength);
01375 if (canon)
01376 strcpy(un.sysname, canon->short_name);
01377 gotDefaults = 1;
01378 break;
01379 }
01380
01381 if (arch) *arch = un.machine;
01382 if (os) *os = un.sysname;
01383 }
01384
01385 static
01386 const char * rpmGetVarArch(int var, const char * arch)
01387
01388 {
01389 const struct rpmvarValue * next;
01390
01391 if (arch == NULL) arch = current[ARCH];
01392
01393 if (arch) {
01394 next = &values[var];
01395 while (next) {
01396 if (next->arch && !strcmp(next->arch, arch)) return next->value;
01397 next = next->next;
01398 }
01399 }
01400
01401 next = values + var;
01402 while (next && next->arch) next = next->next;
01403
01404 return next ? next->value : NULL;
01405 }
01406
01407 const char *rpmGetVar(int var)
01408 {
01409 return rpmGetVarArch(var, NULL);
01410 }
01411
01412
01413 static void freeRpmVar( struct rpmvarValue * orig)
01414
01415 {
01416 struct rpmvarValue * next, * var = orig;
01417
01418 while (var) {
01419 next = var->next;
01420 var->arch = _free(var->arch);
01421 var->value = _free(var->value);
01422
01423
01424 if (var != orig) var = _free(var);
01425
01426 var = next;
01427 }
01428 }
01429
01430 void rpmSetVar(int var, const char * val)
01431
01432
01433 {
01434
01435 freeRpmVar(&values[var]);
01436
01437 values[var].value = (val ? xstrdup(val) : NULL);
01438 }
01439
01440 static void rpmSetVarArch(int var, const char * val, const char * arch)
01441 {
01442 struct rpmvarValue * next = values + var;
01443
01444 if (next->value) {
01445 if (arch) {
01446 while (next->next) {
01447 if (next->arch && !strcmp(next->arch, arch)) break;
01448 next = next->next;
01449 }
01450 } else {
01451 while (next->next) {
01452 if (!next->arch) break;
01453 next = next->next;
01454 }
01455 }
01456
01457
01458 if (next->arch && arch && !strcmp(next->arch, arch)) {
01459
01460 next->value = _free(next->value);
01461 next->arch = _free(next->arch);
01462 } else if (next->arch || arch) {
01463 next->next = xmalloc(sizeof(*next->next));
01464 next = next->next;
01465 next->value = NULL;
01466 next->arch = NULL;
01467 next->next = NULL;
01468 }
01469 }
01470
01471 next->value = _free(next->value);
01472 next->value = xstrdup(val);
01473 next->arch = (arch ? xstrdup(arch) : NULL);
01474 }
01475
01476 void rpmSetTables(int archTable, int osTable)
01477
01478
01479 {
01480 const char * arch, * os;
01481
01482 defaultMachine(&arch, &os);
01483
01484 if (currTables[ARCH] != archTable) {
01485 currTables[ARCH] = archTable;
01486 rebuildCompatTables(ARCH, arch);
01487 }
01488
01489 if (currTables[OS] != osTable) {
01490 currTables[OS] = osTable;
01491 rebuildCompatTables(OS, os);
01492 }
01493 }
01494
01495 int rpmMachineScore(int type, const char * name)
01496 {
01497 machEquivInfo info = machEquivSearch(&tables[type].equiv, name);
01498 return (info != NULL ? info->score : 0);
01499 }
01500
01501 void rpmGetMachine(const char ** arch, const char ** os)
01502 {
01503 if (arch)
01504 *arch = current[ARCH];
01505
01506 if (os)
01507 *os = current[OS];
01508 }
01509
01510 void rpmSetMachine(const char * arch, const char * os)
01511
01512
01513 {
01514 const char * host_cpu, * host_os;
01515
01516 defaultMachine(&host_cpu, &host_os);
01517
01518 if (arch == NULL) {
01519 arch = host_cpu;
01520 if (tables[currTables[ARCH]].hasTranslate)
01521 arch = lookupInDefaultTable(arch,
01522 tables[currTables[ARCH]].defaults,
01523 tables[currTables[ARCH]].defaultsLength);
01524 }
01525 if (arch == NULL) return;
01526
01527 if (os == NULL) {
01528 os = host_os;
01529 if (tables[currTables[OS]].hasTranslate)
01530 os = lookupInDefaultTable(os,
01531 tables[currTables[OS]].defaults,
01532 tables[currTables[OS]].defaultsLength);
01533 }
01534 if (os == NULL) return;
01535
01536 if (!current[ARCH] || strcmp(arch, current[ARCH])) {
01537 current[ARCH] = _free(current[ARCH]);
01538 current[ARCH] = xstrdup(arch);
01539 rebuildCompatTables(ARCH, host_cpu);
01540 }
01541
01542 if (!current[OS] || strcmp(os, current[OS])) {
01543 char * t = xstrdup(os);
01544 current[OS] = _free(current[OS]);
01545
01546
01547
01548
01549
01550
01551
01552
01553 if (!strcmp(t, "linux"))
01554 *t = 'L';
01555 current[OS] = t;
01556
01557 rebuildCompatTables(OS, host_os);
01558 }
01559 }
01560
01561 static void rebuildCompatTables(int type, const char * name)
01562
01563 {
01564 machFindEquivs(&tables[currTables[type]].cache,
01565 &tables[currTables[type]].equiv,
01566 name);
01567 }
01568
01569 static void getMachineInfo(int type, const char ** name,
01570 int * num)
01571
01572 {
01573 canonEntry canon;
01574 int which = currTables[type];
01575
01576
01577 if (which >= 2) which -= 2;
01578
01579 canon = lookupInCanonTable(current[type],
01580 tables[which].canons,
01581 tables[which].canonsLength);
01582
01583 if (canon) {
01584 if (num) *num = canon->num;
01585 if (name) *name = canon->short_name;
01586 } else {
01587 if (num) *num = 255;
01588 if (name) *name = current[type];
01589
01590 if (tables[currTables[type]].hasCanon) {
01591 rpmMessage(RPMMESS_WARNING, _("Unknown system: %s\n"), current[type]);
01592 rpmMessage(RPMMESS_WARNING, _("Please contact rpm-maint@lists.rpm.org\n"));
01593 }
01594 }
01595 }
01596
01597 void rpmGetArchInfo(const char ** name, int * num)
01598 {
01599 getMachineInfo(ARCH, name, num);
01600 }
01601
01602 void rpmGetOsInfo(const char ** name, int * num)
01603 {
01604 getMachineInfo(OS, name, num);
01605 }
01606
01607 static void rpmRebuildTargetVars(const char ** target, const char ** canontarget)
01608 {
01609
01610 char *ca = NULL, *co = NULL, *ct = NULL;
01611 int x;
01612
01613
01614
01615 rpmSetMachine(NULL, NULL);
01616 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
01617 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
01618
01619
01620 if (target && *target) {
01621 char *c;
01622
01623 ca = xstrdup(*target);
01624 if ((c = strchr(ca, '-')) != NULL) {
01625 *c++ = '\0';
01626
01627 if ((co = strrchr(c, '-')) == NULL) {
01628 co = c;
01629 } else {
01630 if (!xstrcasecmp(co, "-gnu"))
01631 *co = '\0';
01632 if ((co = strrchr(c, '-')) == NULL)
01633 co = c;
01634 else
01635 co++;
01636 }
01637 if (co != NULL) co = xstrdup(co);
01638 }
01639 } else {
01640 const char *a = NULL;
01641 const char *o = NULL;
01642
01643 rpmGetArchInfo(&a, NULL);
01644 ca = (a) ? xstrdup(a) : NULL;
01645 rpmGetOsInfo(&o, NULL);
01646 co = (o) ? xstrdup(o) : NULL;
01647 }
01648
01649
01650
01651 if (ca == NULL) {
01652 const char *a = NULL;
01653 defaultMachine(&a, NULL);
01654 ca = (a) ? xstrdup(a) : NULL;
01655 }
01656 for (x = 0; ca[x] != '\0'; x++)
01657 ca[x] = xtolower(ca[x]);
01658
01659 if (co == NULL) {
01660 const char *o = NULL;
01661 defaultMachine(NULL, &o);
01662 co = (o) ? xstrdup(o) : NULL;
01663 }
01664 for (x = 0; co[x] != '\0'; x++)
01665 co[x] = xtolower(co[x]);
01666
01667
01668 if (ct == NULL) {
01669 ct = xmalloc(strlen(ca) + sizeof("-") + strlen(co));
01670 sprintf(ct, "%s-%s", ca, co);
01671 }
01672
01673
01674
01675
01676
01677 delMacro(NULL, "_target");
01678 addMacro(NULL, "_target", NULL, ct, RMIL_RPMRC);
01679 delMacro(NULL, "_target_cpu");
01680 addMacro(NULL, "_target_cpu", NULL, ca, RMIL_RPMRC);
01681 delMacro(NULL, "_target_os");
01682 addMacro(NULL, "_target_os", NULL, co, RMIL_RPMRC);
01683
01684
01685
01686 { const char *optflags = rpmGetVarArch(RPMVAR_OPTFLAGS, ca);
01687 if (optflags != NULL) {
01688 delMacro(NULL, "optflags");
01689 addMacro(NULL, "optflags", NULL, optflags, RMIL_RPMRC);
01690 }
01691 }
01692
01693
01694 if (canontarget)
01695 *canontarget = ct;
01696 else
01697 ct = _free(ct);
01698
01699 ca = _free(ca);
01700
01701 co = _free(co);
01702
01703 }
01704
01705 void rpmFreeRpmrc(void)
01706
01707
01708
01709
01710 {
01711 int i, j, k;
01712
01713
01714 if (platpat)
01715 for (i = 0; i < nplatpat; i++)
01716 platpat[i] = _free(platpat[i]);
01717 platpat = _free(platpat);
01718
01719 nplatpat = 0;
01720
01721 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
01722 tableType t;
01723 t = tables + i;
01724 if (t->equiv.list) {
01725 for (j = 0; j < t->equiv.count; j++)
01726 t->equiv.list[j].name = _free(t->equiv.list[j].name);
01727 t->equiv.list = _free(t->equiv.list);
01728 t->equiv.count = 0;
01729 }
01730 if (t->cache.cache) {
01731 for (j = 0; j < t->cache.size; j++) {
01732 machCacheEntry e;
01733 e = t->cache.cache + j;
01734 if (e == NULL)
01735 continue;
01736 e->name = _free(e->name);
01737 if (e->equivs) {
01738 for (k = 0; k < e->count; k++)
01739 e->equivs[k] = _free(e->equivs[k]);
01740 e->equivs = _free(e->equivs);
01741 }
01742 }
01743 t->cache.cache = _free(t->cache.cache);
01744 t->cache.size = 0;
01745 }
01746 if (t->defaults) {
01747 for (j = 0; j < t->defaultsLength; j++) {
01748 t->defaults[j].name = _free(t->defaults[j].name);
01749 t->defaults[j].defName = _free(t->defaults[j].defName);
01750 }
01751 t->defaults = _free(t->defaults);
01752 t->defaultsLength = 0;
01753 }
01754 if (t->canons) {
01755 for (j = 0; j < t->canonsLength; j++) {
01756 t->canons[j].name = _free(t->canons[j].name);
01757 t->canons[j].short_name = _free(t->canons[j].short_name);
01758 }
01759 t->canons = _free(t->canons);
01760 t->canonsLength = 0;
01761 }
01762 }
01763
01764 for (i = 0; i < RPMVAR_NUM; i++) {
01765 struct rpmvarValue * vp;
01766 while ((vp = values[i].next) != NULL) {
01767 values[i].next = vp->next;
01768 vp->value = _free(vp->value);
01769 vp->arch = _free(vp->arch);
01770 vp = _free(vp);
01771 }
01772 values[i].value = _free(values[i].value);
01773 values[i].arch = _free(values[i].arch);
01774 }
01775 current[OS] = _free(current[OS]);
01776 current[ARCH] = _free(current[ARCH]);
01777 defaultsInitialized = 0;
01778
01779 return;
01780
01781 }
01782
01788 static int rpmReadRC( const char * rcfiles)
01789
01790
01791
01792
01793 {
01794 char *myrcfiles, *r, *re;
01795 int rc;
01796
01797 if (!defaultsInitialized) {
01798 setDefaults();
01799 defaultsInitialized = 1;
01800 }
01801
01802 if (rcfiles == NULL)
01803 rcfiles = defrcfiles;
01804
01805
01806 rc = 0;
01807 for (r = myrcfiles = xstrdup(rcfiles); r && *r != '\0'; r = re) {
01808 char fn[4096];
01809 FD_t fd;
01810
01811
01812 for (re = r; (re = strchr(re, ':')) != NULL; re++) {
01813 if (!(re[1] == '/' && re[2] == '/'))
01814 break;
01815 }
01816 if (re && *re == ':')
01817 *re++ = '\0';
01818 else
01819 re = r + strlen(r);
01820
01821
01822 fn[0] = '\0';
01823 if (r[0] == '~' && r[1] == '/') {
01824 const char * home = getenv("HOME");
01825 if (home == NULL) {
01826
01827 if (rcfiles == defrcfiles && myrcfiles != r)
01828 continue;
01829 rpmError(RPMERR_RPMRC, _("Cannot expand %s\n"), r);
01830 rc = 1;
01831 break;
01832 }
01833 if (strlen(home) > (sizeof(fn) - strlen(r))) {
01834 rpmError(RPMERR_RPMRC, _("Cannot read %s, HOME is too large.\n"),
01835 r);
01836 rc = 1;
01837 break;
01838 }
01839 strcpy(fn, home);
01840 r++;
01841 }
01842 strncat(fn, r, sizeof(fn) - (strlen(fn) + 1));
01843 fn[sizeof(fn)-1] = '\0';
01844
01845
01846 fd = Fopen(fn, "r.fpio");
01847 if (fd == NULL || Ferror(fd)) {
01848
01849 if (rcfiles == defrcfiles && myrcfiles != r)
01850 continue;
01851 rpmError(RPMERR_RPMRC, _("Unable to open %s for reading: %s.\n"),
01852 fn, Fstrerror(fd));
01853 rc = 1;
01854 break;
01855 } else {
01856 rc = doReadRC(fd, fn);
01857 }
01858 if (rc) break;
01859 }
01860 myrcfiles = _free(myrcfiles);
01861 if (rc)
01862 return rc;
01863
01864 rpmSetMachine(NULL, NULL);
01865
01866 { const char *mfpath;
01867
01868 if ((mfpath = rpmGetVar(RPMVAR_MACROFILES)) != NULL) {
01869 mfpath = xstrdup(mfpath);
01870 rpmInitMacros(NULL, mfpath);
01871 mfpath = _free(mfpath);
01872 }
01873
01874 }
01875
01876 return rc;
01877 }
01878
01879 int rpmReadConfigFiles(const char * file, const char * target)
01880 {
01881
01882
01883
01884 rpmRebuildTargetVars(&target, NULL);
01885
01886
01887 if (rpmReadRC(file)) return -1;
01888
01889
01890 rpmRebuildTargetVars(&target, NULL);
01891
01892
01893
01894 { const char *cpu = rpmExpand("%{_target_cpu}", NULL);
01895 const char *os = rpmExpand("%{_target_os}", NULL);
01896 rpmSetMachine(cpu, os);
01897 cpu = _free(cpu);
01898 os = _free(os);
01899 }
01900
01901
01902 #ifdef WITH_LUA
01903 (void)rpmluaGetPrintBuffer(NULL);
01904 #endif
01905
01906 return 0;
01907 }
01908
01909 int rpmShowRC(FILE * fp)
01910 {
01911 struct rpmOption *opt;
01912 int i;
01913 machEquivTable equivTable;
01914
01915
01916 fprintf(fp, "ARCHITECTURE AND OS:\n");
01917 fprintf(fp, "build arch : %s\n", current[ARCH]);
01918
01919 fprintf(fp, "compatible build archs:");
01920 equivTable = &tables[RPM_MACHTABLE_BUILDARCH].equiv;
01921 for (i = 0; i < equivTable->count; i++)
01922 fprintf(fp," %s", equivTable->list[i].name);
01923 fprintf(fp, "\n");
01924
01925 fprintf(fp, "build os : %s\n", current[OS]);
01926
01927 fprintf(fp, "compatible build os's :");
01928 equivTable = &tables[RPM_MACHTABLE_BUILDOS].equiv;
01929 for (i = 0; i < equivTable->count; i++)
01930 fprintf(fp," %s", equivTable->list[i].name);
01931 fprintf(fp, "\n");
01932
01933 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
01934 rpmSetMachine(NULL, NULL);
01935
01936 fprintf(fp, "install arch : %s\n", current[ARCH]);
01937 fprintf(fp, "install os : %s\n", current[OS]);
01938
01939 fprintf(fp, "compatible archs :");
01940 equivTable = &tables[RPM_MACHTABLE_INSTARCH].equiv;
01941 for (i = 0; i < equivTable->count; i++)
01942 fprintf(fp," %s", equivTable->list[i].name);
01943 fprintf(fp, "\n");
01944
01945 fprintf(fp, "compatible os's :");
01946 equivTable = &tables[RPM_MACHTABLE_INSTOS].equiv;
01947 for (i = 0; i < equivTable->count; i++)
01948 fprintf(fp," %s", equivTable->list[i].name);
01949 fprintf(fp, "\n");
01950
01951 fprintf(fp, "\nRPMRC VALUES:\n");
01952 for (i = 0, opt = optionTable; i < optionTableSize; i++, opt++) {
01953 const char *s = rpmGetVar(opt->var);
01954 if (s != NULL || rpmIsVerbose())
01955 fprintf(fp, "%-21s : %s\n", opt->name, s ? s : "(not set)");
01956 }
01957 fprintf(fp, "\n");
01958
01959 fprintf(fp, "Features supported by rpmlib:\n");
01960 rpmShowRpmlibProvides(fp);
01961 fprintf(fp, "\n");
01962
01963 rpmDumpMacroTable(NULL, fp);
01964
01965 return 0;
01966 }
01967