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