5 #if HAVE_SYS_SYSTEMCFG_H
6 #include <sys/systemcfg.h>
16 static const char *defrcfiles = LIBRPMRC_FILENAME ":/etc/rpmrc:~/.rpmrc";
18 struct machCacheEntry {
26 struct machCacheEntry * cache;
30 struct machEquivInfo {
35 struct machEquivTable {
37 struct machEquivInfo * list;
42 /* eventually, this arch will be replaced with a generic condition */
44 struct rpmvarValue * next;
50 int archSpecific, required, macroize, localize;
51 struct rpmOptionValue * value;
65 /* tags are 'key'canon, 'key'translate, 'key'compat
67 for giggles, 'key'_canon, 'key'_compat, and 'key'_canon will also work */
70 int hasCanon, hasTranslate;
71 struct machEquivTable equiv;
72 struct machCache cache;
73 struct defaultEntry * defaults;
74 struct canonEntry * canons;
80 static struct tableType tables[RPM_MACHTABLE_COUNT] = {
83 { "buildarch", 0, 1 },
87 /* this *must* be kept in alphabetical order */
88 /* The order of the flags is archSpecific, required, macroize, localize */
90 static struct rpmOption optionTable[] = {
91 { "include", RPMVAR_INCLUDE, 0, 1, 0, 2 },
92 { "macrofiles", RPMVAR_MACROFILES, 0, 0, 0, 1 },
93 { "optflags", RPMVAR_OPTFLAGS, 1, 0, 1, 0 },
94 { "provides", RPMVAR_PROVIDES, 0, 0, 0, 0 },
97 static int optionTableSize = sizeof(optionTable) / sizeof(*optionTable);
102 static char * current[2];
103 static int currTables[2] = { RPM_MACHTABLE_INSTOS, RPM_MACHTABLE_INSTARCH };
104 static struct rpmvarValue values[RPMVAR_NUM];
107 static void defaultMachine(/*@out@*/char ** arch, /*@out@*/char ** os);
108 static int doReadRC(FD_t fd, const char * filename);
109 static int optionCompare(const void * a, const void * b);
110 static int addCanon(struct canonEntry **table, int *tableLen, char *line,
111 const char *fn, int lineNum);
112 static int addDefault(struct defaultEntry **table, int *tableLen, char *line,
113 const char *fn, int lineNum);
114 static void freeRpmVar(struct rpmvarValue * orig);
115 static void rpmSetVarArch(int var, const char * val, const char * arch);
116 static struct canonEntry *lookupInCanonTable(char *name,
117 struct canonEntry *table,
119 static const char *lookupInDefaultTable(const char *name,
120 struct defaultEntry *table,
123 static void setVarDefault(int var, const char *macroname, const char *val, const char *body);
124 static void setPathDefault(int var, const char *macroname, const char *subdir);
125 static void setDefaults(void);
127 static void rebuildCompatTables(int type, char *name);
129 /* compatiblity tables */
130 static int machCompatCacheAdd(char * name, const char * fn, int linenum,
131 struct machCache * cache);
132 static struct machCacheEntry * machCacheFindEntry(struct machCache * cache,
134 static struct machEquivInfo * machEquivSearch(
135 struct machEquivTable * table, char * name);
136 static void machAddEquiv(struct machEquivTable * table, char * name,
138 static void machCacheEntryVisit(struct machCache * cache,
139 struct machEquivTable * table,
142 static void machFindEquivs(struct machCache * cache,
143 struct machEquivTable * table,
146 static int optionCompare(const void * a, const void * b) {
147 return strcasecmp(((struct rpmOption *) a)->name,
148 ((struct rpmOption *) b)->name);
151 static void rpmRebuildTargetVars(const char **target, const char ** canontarget);
153 static struct machCacheEntry * machCacheFindEntry(struct machCache * cache,
158 for (i = 0; i < cache->size; i++)
159 if (!strcmp(cache->cache[i].name, key)) return cache->cache + i;
164 static int machCompatCacheAdd(char * name, const char * fn, int linenum,
165 struct machCache * cache)
167 char * chptr, * equivs;
170 struct machCacheEntry * entry = NULL;
172 while (*name && isspace(*name)) name++;
175 while (*chptr && *chptr != ':') chptr++;
177 rpmError(RPMERR_RPMRC, _("missing second ':' at %s:%d"), fn, linenum);
179 } else if (chptr == name) {
180 rpmError(RPMERR_RPMRC, _("missing architecture name at %s:%d"), fn,
185 while (*chptr == ':' || isspace(*chptr)) chptr--;
188 while (*equivs && isspace(*equivs)) equivs++;
194 entry = machCacheFindEntry(cache, name);
196 for (i = 0; i < entry->count; i++)
197 free(entry->equivs[i]);
198 if (entry->count) free(entry->equivs);
204 cache->cache = xrealloc(cache->cache,
205 (cache->size + 1) * sizeof(*cache->cache));
206 entry = cache->cache + cache->size++;
207 entry->name = xstrdup(name);
212 if (delEntry) return 0;
214 while ((chptr = strtok(equivs, " ")) != NULL) {
216 if (chptr[0] == '\0') /* does strtok() return "" ever?? */
219 entry->equivs = xrealloc(entry->equivs, sizeof(*entry->equivs)
220 * (entry->count + 1));
222 entry->equivs = xmalloc(sizeof(*entry->equivs));
224 entry->equivs[entry->count] = xstrdup(chptr);
231 static struct machEquivInfo * machEquivSearch(
232 struct machEquivTable * table, char * name)
237 * XXX The strcasecmp below is necessary so the old (rpm < 2.90) style
238 * XXX os-from-uname (e.g. "Linux") is compatible with the new
239 * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
240 * XXX A copy of this string is embedded in headers and is
241 * XXX used by rpmInstallPackage->{os,arch}Okay->rpmMachineScore->.
242 * XXX to verify correct arch/os from headers.
244 for (i = 0; i < table->count; i++)
245 if (!strcasecmp(table->list[i].name, name))
246 return table->list + i;
251 static void machAddEquiv(struct machEquivTable * table, char * name,
254 struct machEquivInfo * equiv;
256 equiv = machEquivSearch(table, name);
259 table->list = xrealloc(table->list, (table->count + 1)
260 * sizeof(*table->list));
262 table->list = xmalloc(sizeof(*table->list)); /* XXX memory leak */
264 table->list[table->count].name = xstrdup(name); /* XXX memory leak */
265 table->list[table->count++].score = distance;
269 static void machCacheEntryVisit(struct machCache * cache,
270 struct machEquivTable * table,
274 struct machCacheEntry * entry;
277 entry = machCacheFindEntry(cache, name);
278 if (!entry || entry->visited) return;
282 for (i = 0; i < entry->count; i++) {
283 machAddEquiv(table, entry->equivs[i], distance);
286 for (i = 0; i < entry->count; i++) {
287 machCacheEntryVisit(cache, table, entry->equivs[i], distance + 1);
291 static void machFindEquivs(struct machCache * cache,
292 struct machEquivTable * table,
297 for (i = 0; i < cache->size; i++)
298 cache->cache[i].visited = 0;
302 /* We have a general graph built using strings instead of pointers.
303 Yuck. We have to start at a point at traverse it, remembering how
304 far away everything is. */
305 machAddEquiv(table, key, 1);
306 machCacheEntryVisit(cache, table, key, 2);
309 static int addCanon(struct canonEntry **table, int *tableLen, char *line,
310 const char *fn, int lineNum)
312 struct canonEntry *t;
317 *table = xmalloc(2 * sizeof(struct canonEntry));
320 *table = xrealloc(*table, sizeof(struct canonEntry) * (*tableLen));
322 t = & ((*table)[*tableLen - 2]);
324 t->name = strtok(line, ": \t");
325 t->short_name = strtok(NULL, " \t");
326 s = strtok(NULL, " \t");
327 if (! (t->name && t->short_name && s)) {
328 rpmError(RPMERR_RPMRC, _("Incomplete data line at %s:%d"), fn, lineNum);
331 if (strtok(NULL, " \t")) {
332 rpmError(RPMERR_RPMRC, _("Too many args in data line at %s:%d"),
337 t->num = strtoul(s, &s1, 10);
338 if ((*s1) || (s1 == s) || (t->num == ULONG_MAX)) {
339 rpmError(RPMERR_RPMRC, _("Bad arch/os number: %s (%s:%d)"), s,
341 return(RPMERR_RPMRC);
344 t->name = xstrdup(t->name);
345 t->short_name = xstrdup(t->short_name);
347 /* From A B C entry */
348 /* Add B B C entry */
349 t[1].name = xstrdup(t->short_name);
350 t[1].short_name = xstrdup(t->short_name);
356 static int addDefault(struct defaultEntry **table, int *tableLen, char *line,
357 const char *fn, int lineNum)
359 struct defaultEntry *t;
363 *table = xmalloc(sizeof(struct defaultEntry));
366 *table = xrealloc(*table, sizeof(struct defaultEntry) * (*tableLen));
368 t = & ((*table)[*tableLen - 1]);
370 t->name = strtok(line, ": \t");
371 t->defName = strtok(NULL, " \t");
372 if (! (t->name && t->defName)) {
373 rpmError(RPMERR_RPMRC, _("Incomplete default line at %s:%d"),
377 if (strtok(NULL, " \t")) {
378 rpmError(RPMERR_RPMRC, _("Too many args in default line at %s:%d"),
383 t->name = xstrdup(t->name);
384 t->defName = xstrdup(t->defName);
389 static struct canonEntry *lookupInCanonTable(char *name,
390 struct canonEntry *table,
394 if (!strcmp(name, table[tableLen].name)) {
395 return &(table[tableLen]);
402 static const char *lookupInDefaultTable(const char *name, struct defaultEntry *table,
406 if (!strcmp(name, table[tableLen].name)) {
407 return table[tableLen].defName;
414 int rpmReadConfigFiles(const char * file, const char * target)
417 /* Preset target macros */
418 rpmRebuildTargetVars(&target, NULL);
421 if (rpmReadRC(file)) return -1;
423 /* Reset target macros */
424 rpmRebuildTargetVars(&target, NULL);
426 /* Finally set target platform */
427 { const char *cpu = rpmExpand("%{_target_cpu}", NULL);
428 const char *os = rpmExpand("%{_target_os}", NULL);
429 rpmSetMachine(cpu, os);
437 static void setVarDefault(int var, const char *macroname, const char *val, const char *body)
439 if (var >= 0) { /* XXX Dying ... */
440 if (rpmGetVar(var)) return;
445 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
448 static void setPathDefault(int var, const char *macroname, const char *subdir)
451 if (var >= 0) { /* XXX Dying ... */
455 if (rpmGetVar(var)) return;
457 topdir = rpmGetPath("%{_topdir}", NULL);
459 fn = alloca(strlen(topdir) + strlen(subdir) + 2);
461 if (fn[strlen(topdir) - 1] != '/')
466 if (topdir) xfree(topdir);
469 if (macroname != NULL) {
470 #define _TOPDIRMACRO "%{_topdir}/"
471 char *body = alloca(sizeof(_TOPDIRMACRO) + strlen(subdir));
472 strcpy(body, _TOPDIRMACRO);
473 strcat(body, subdir);
474 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
479 static const char *prescriptenviron = "\n\
480 RPM_SOURCE_DIR=\"%{_sourcedir}\"\n\
481 RPM_BUILD_DIR=\"%{_builddir}\"\n\
482 RPM_OPT_FLAGS=\"%{optflags}\"\n\
483 RPM_ARCH=\"%{_arch}\"\n\
485 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS\n\
486 RPM_DOC_DIR=\"%{_docdir}\"\n\
487 export RPM_DOC_DIR\n\
488 RPM_PACKAGE_NAME=\"%{name}\"\n\
489 RPM_PACKAGE_VERSION=\"%{version}\"\n\
490 RPM_PACKAGE_RELEASE=\"%{release}\"\n\
491 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\n\
492 %{?buildroot:RPM_BUILD_ROOT=\"%{buildroot}\"\n\
493 export RPM_BUILD_ROOT\n}\
496 static void setDefaults(void) {
498 addMacro(NULL, "_usr", NULL, "/usr", RMIL_DEFAULT);
499 addMacro(NULL, "_var", NULL, "/var", RMIL_DEFAULT);
501 addMacro(NULL, "_preScriptEnvironment",NULL, prescriptenviron,RMIL_DEFAULT);
503 setVarDefault(-1, "_topdir",
504 "/usr/src/redhat", "%{_usr}/src/redhat");
505 setVarDefault(-1, "_tmppath",
506 "/var/tmp", "%{_var}/tmp");
507 setVarDefault(-1, "_dbpath",
508 "/var/lib/rpm", "%{_var}/lib/rpm");
509 setVarDefault(-1, "_defaultdocdir",
510 "/usr/doc", "%{_usr}/doc");
512 setVarDefault(-1, "_rpmfilename",
513 "%%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",NULL);
515 setVarDefault(RPMVAR_OPTFLAGS, "optflags",
517 setVarDefault(-1, "sigtype",
519 setVarDefault(-1, "_buildshell",
522 setPathDefault(-1, "_builddir", "BUILD");
523 setPathDefault(-1, "_rpmdir", "RPMS");
524 setPathDefault(-1, "_srcrpmdir", "SRPMS");
525 setPathDefault(-1, "_sourcedir", "SOURCES");
526 setPathDefault(-1, "_specdir", "SPECS");
530 int rpmReadRC(const char * rcfiles)
532 char *myrcfiles, *r, *re;
534 static int first = 1;
542 rcfiles = defrcfiles;
544 /* Read each file in rcfiles. */
546 for (r = myrcfiles = xstrdup(rcfiles); *r != '\0'; r = re) {
550 /* Get pointer to rest of files */
551 if ((re = strchr(r, ':')) != NULL)
556 /* Expand ~/ to $HOME/ */
558 if (r[0] == '~' && r[1] == '/') {
559 char *home = getenv("HOME");
561 rpmError(RPMERR_RPMRC, _("Cannot expand %s"), r);
570 /* Read another rcfile */
571 fd = fdOpen(fn, O_RDONLY, 0);
572 if (fdFileno(fd) < 0) {
573 /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */
574 if (rcfiles == defrcfiles && myrcfiles != r)
576 rpmError(RPMERR_RPMRC, _("Unable to open %s for reading: %s."),
577 fn, strerror(errno));
581 rc = doReadRC(fd, fn);
585 if (myrcfiles) free(myrcfiles);
589 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
591 if ((r = rpmGetVar(RPMVAR_MACROFILES)) != NULL)
597 static int doReadRC(FD_t fd, const char * filename)
601 struct rpmOption searchOption, * option;
605 fstat(fdFileno(fd), &sb);
606 next = alloca(sb.st_size + 2);
607 if (fdRead(fd, next, sb.st_size) != sb.st_size) {
608 rpmError(RPMERR_RPMRC, _("Failed to read %s: %s."), filename,
612 next[sb.st_size] = '\n';
613 next[sb.st_size + 1] = '\0';
620 while (*se && *se != '\n') se++;
621 if (*se) *se++ = '\0';
624 while (*s && isspace(*s)) s++;
626 /* we used to allow comments to begin anywhere, but not anymore */
627 if (*s == '#' || *s == '\0') continue;
630 while (*se && !isspace(*se) && *se != ':') se++;
634 while (*se && isspace(*se) && *se != ':') se++;
638 rpmError(RPMERR_RPMRC, _("missing ':' at %s:%d"),
643 while (*se && isspace(*se)) se++;
645 /* Find keyword in table */
646 searchOption.name = s;
647 option = bsearch(&searchOption, optionTable, optionTableSize,
648 sizeof(struct rpmOption), optionCompare);
650 if (option) { /* For configuration variables ... */
651 const char *arch, *val, *fn;
653 arch = val = fn = NULL;
655 rpmError(RPMERR_RPMRC, _("missing argument for %s at %s:%d"),
656 option->name, filename, linenum);
660 switch (option->var) {
665 while (*se && !isspace(*se)) se++;
666 if (*se) *se++ = '\0';
668 rpmRebuildTargetVars(NULL, NULL);
670 fn = rpmGetPath(s, NULL);
671 if (fn == NULL || *fn == '\0') {
672 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\""),
673 option->name, filename, linenum, s);
679 fdinc = fdOpen(fn, O_RDONLY, 0);
680 if (fdFileno(fdinc) < 0) {
681 rpmError(RPMERR_RPMRC, _("cannot open %s at %s:%d"),
682 fn, filename, linenum);
685 rc = doReadRC(fdinc, fn);
690 continue; /* XXX don't save include value as var/macro */
691 } /*@notreached@*/ break;
692 case RPMVAR_MACROFILES:
693 fn = rpmGetPath(se, NULL);
694 if (fn == NULL || *fn == '\0') {
695 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\""),
696 option->name, filename, linenum, fn);
702 case RPMVAR_PROVIDES:
704 s = rpmGetVar(RPMVAR_PROVIDES);
705 if (s == NULL) s = "";
706 fn = t = xmalloc(strlen(s) + strlen(se) + 2);
707 while (*s) *t++ = *s++;
709 while (*se) *t++ = *se++;
717 if (option->archSpecific) {
719 while (*se && !isspace(*se)) se++;
721 rpmError(RPMERR_RPMRC,
722 _("missing architecture for %s at %s:%d"),
723 option->name, filename, linenum);
727 while (*se && isspace(*se)) se++;
729 rpmError(RPMERR_RPMRC,
730 _("missing argument for %s at %s:%d"),
731 option->name, filename, linenum);
738 /* Only add macros if appropriate for this arch */
739 if (option->macroize &&
740 (arch == NULL || !strcmp(arch, current[ARCH]))) {
742 n = name = xmalloc(strlen(option->name)+2);
743 if (option->localize)
745 strcpy(n, option->name);
746 addMacro(NULL, name, NULL, val, RMIL_RPMRC);
749 rpmSetVarArch(option->var, val, arch);
752 } else { /* For arch/os compatibilty tables ... */
758 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
759 if (!strncmp(tables[i].key, s, strlen(tables[i].key)))
763 if (i < RPM_MACHTABLE_COUNT) {
764 char *rest = s + strlen(tables[i].key);
765 if (*rest == '_') rest++;
767 if (!strcmp(rest, "compat")) {
768 if (machCompatCacheAdd(se, filename, linenum,
772 } else if (tables[i].hasTranslate &&
773 !strcmp(rest, "translate")) {
774 if (addDefault(&tables[i].defaults,
775 &tables[i].defaultsLength,
776 se, filename, linenum))
779 } else if (tables[i].hasCanon &&
780 !strcmp(rest, "canon")) {
781 if (addCanon(&tables[i].canons, &tables[i].canonsLength,
782 se, filename, linenum))
789 rpmError(RPMERR_RPMRC, _("bad option '%s' at %s:%d"),
790 s, filename, linenum);
798 static void defaultMachine(char ** arch, char ** os) {
799 static struct utsname un;
800 static int gotDefaults = 0;
802 struct canonEntry * canon;
807 #if !defined(__linux__)
809 /* USUALLY un.sysname on sinix does start with the word "SINIX"
810 * let's be absolutely sure
812 sprintf(un.sysname,"SINIX");
814 if (!strcmp(un.sysname, "AIX")) {
815 strcpy(un.machine, __power_pc() ? "ppc" : "rs6000");
816 sprintf(un.sysname,"aix%s.%s",un.version,un.release);
818 else if (!strcmp(un.sysname, "SunOS")) {
819 if (!strncmp(un.release,"4", 1)) /* SunOS 4.x */ {
821 for (fd=0;(un.release[fd] != 0 && (fd < sizeof(un.release)));fd++)
822 if (!isdigit(un.release[fd]) && (un.release[fd] != '.')) {
826 sprintf(un.sysname,"sunos%s",un.release);
829 else /* Solaris 2.x: n.x.x becomes n-3.x.x */
830 sprintf(un.sysname,"solaris%1d%s",atoi(un.release)-3,un.release+1+(atoi(un.release)/10));
832 else if (!strcmp(un.sysname, "HP-UX"))
833 /*make un.sysname look like hpux9.05 for example*/
834 sprintf(un.sysname,"hpux%s",strpbrk(un.release,"123456789"));
835 else if (!strcmp(un.sysname, "OSF1"))
836 /*make un.sysname look like osf3.2 for example*/
837 sprintf(un.sysname,"osf%s",strpbrk(un.release,"123456789"));
838 else if (!strncmp(un.sysname, "IP", 2))
839 un.sysname[2] = '\0';
840 else if (!strncmp(un.sysname, "SINIX", 5)) {
841 sprintf(un.sysname, "sinix%s",un.release);
842 if (!strncmp(un.machine, "RM", 2))
843 sprintf(un.machine, "mips");
845 else if ((!strncmp(un.machine, "34", 2) || \
846 !strncmp(un.machine, "33", 2)) && \
847 !strncmp(un.release, "4.0", 3)) {
848 /* we are on ncr-sysv4 */
850 FD_t fd = fdOpen("/etc/.relid", O_RDONLY, 0700);
851 if (fdFileno(fd) > 0) {
852 chptr = (char *) xcalloc(1, 256);
854 int irelid = fdRead(fd, (void *)chptr, 256);
856 /* example: "112393 RELEASE 020200 Version 01 OS" */
858 if ((prelid=strstr(chptr, "RELEASE "))){
859 prelid += strlen("RELEASE ")+1;
860 sprintf(un.sysname,"ncr-sysv4.%.*s",1,prelid);
866 if (prelid == NULL) /* parsing /etc/.relid file failed? */
867 strcpy(un.sysname,"ncr-sysv4");
868 /* wrong, just for now, find out how to look for i586 later*/
869 strcpy(un.machine,"i486");
871 #endif /* __linux__ */
873 /* get rid of the hyphens in the sysname */
874 for (chptr = un.machine; *chptr; chptr++)
875 if (*chptr == '/') *chptr = '-';
877 # if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
879 strcpy(un.machine, "mipsel");
880 # elif defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB)
882 strcpy(un.machine, "mipseb");
885 # if defined(__hpux) && defined(_SC_CPU_VERSION)
887 # if !defined(CPU_PA_RISC1_2)
888 # define CPU_PA_RISC1_2 0x211 /* HP PA-RISC1.2 */
890 # if !defined(CPU_PA_RISC2_0)
891 # define CPU_PA_RISC2_0 0x214 /* HP PA-RISC2.0 */
893 int cpu_version = sysconf(_SC_CPU_VERSION);
895 # if defined(CPU_HP_MC68020)
896 if (cpu_version == CPU_HP_MC68020)
897 strcpy(un.machine, "m68k");
899 # if defined(CPU_HP_MC68030)
900 if (cpu_version == CPU_HP_MC68030)
901 strcpy(un.machine, "m68k");
903 # if defined(CPU_HP_MC68040)
904 if (cpu_version == CPU_HP_MC68040)
905 strcpy(un.machine, "m68k");
908 # if defined(CPU_PA_RISC1_0)
909 if (cpu_version == CPU_PA_RISC1_0)
910 strcpy(un.machine, "hppa1.0");
912 # if defined(CPU_PA_RISC1_1)
913 if (cpu_version == CPU_PA_RISC1_1)
914 strcpy(un.machine, "hppa1.1");
916 # if defined(CPU_PA_RISC1_2)
917 if (cpu_version == CPU_PA_RISC1_2)
918 strcpy(un.machine, "hppa1.2");
920 # if defined(CPU_PA_RISC2_0)
921 if (cpu_version == CPU_PA_RISC2_0)
922 strcpy(un.machine, "hppa2.0");
927 # if defined(__linux__) && defined(__sparc__)
928 if (!strcmp(un.machine, "sparc")) {
929 #define PERS_LINUX 0x00000000
930 #define PERS_LINUX_32BIT 0x00800000
931 #define PERS_LINUX32 0x00000008
933 extern int personality(unsigned long);
936 oldpers = personality(PERS_LINUX_32BIT);
938 if (personality(PERS_LINUX) != -1) {
940 if (! strcmp(un.machine, "sparc64")) {
941 strcpy(un.machine, "sparcv9");
942 oldpers = PERS_LINUX32;
945 personality(oldpers);
948 # endif /* sparc*-linux */
950 /* the uname() result goes through the arch_canon table */
951 canon = lookupInCanonTable(un.machine,
952 tables[RPM_MACHTABLE_INSTARCH].canons,
953 tables[RPM_MACHTABLE_INSTARCH].canonsLength);
955 strcpy(un.machine, canon->short_name);
957 canon = lookupInCanonTable(un.sysname,
958 tables[RPM_MACHTABLE_INSTOS].canons,
959 tables[RPM_MACHTABLE_INSTOS].canonsLength);
961 strcpy(un.sysname, canon->short_name);
965 if (arch) *arch = un.machine;
966 if (os) *os = un.sysname;
969 static char * rpmGetVarArch(int var, char * arch) {
970 struct rpmvarValue * next;
972 if (!arch) arch = current[ARCH];
977 if (next->arch && !strcmp(next->arch, arch)) return next->value;
983 while (next && next->arch) next = next->next;
985 return next ? next->value : NULL;
988 char *rpmGetVar(int var)
990 return rpmGetVarArch(var, NULL);
993 /* this doesn't free the passed pointer! */
994 static void freeRpmVar(struct rpmvarValue * orig) {
995 struct rpmvarValue * next, * var = orig;
1008 if (var != orig) free(var);
1013 void rpmSetVar(int var, const char *val) {
1014 freeRpmVar(&values[var]);
1015 values[var].value = (val ? xstrdup(val) : NULL);
1018 static void rpmSetVarArch(int var, const char * val, const char * arch) {
1019 struct rpmvarValue * next = values + var;
1023 while (next->next) {
1024 if (next->arch && !strcmp(next->arch, arch)) break;
1028 while (next->next) {
1029 if (!next->arch) break;
1034 if (next->arch && arch && !strcmp(next->arch, arch)) {
1035 if (next->value) free(next->value);
1036 if (next->arch) free(next->arch);
1037 } else if (next->arch || arch) {
1038 next->next = xmalloc(sizeof(*next->next));
1044 next->value = xstrdup(val);
1045 next->arch = (arch ? xstrdup(arch) : NULL);
1048 void rpmSetTables(int archTable, int osTable) {
1051 defaultMachine(&arch, &os);
1053 if (currTables[ARCH] != archTable) {
1054 currTables[ARCH] = archTable;
1055 rebuildCompatTables(ARCH, arch);
1058 if (currTables[OS] != osTable) {
1059 currTables[OS] = osTable;
1060 rebuildCompatTables(OS, os);
1064 int rpmMachineScore(int type, char * name) {
1065 struct machEquivInfo * info = machEquivSearch(&tables[type].equiv, name);
1066 return (info != NULL ? info->score : 0);
1069 void rpmGetMachine(char **arch, char **os)
1072 *arch = current[ARCH];
1078 void rpmSetMachine(const char * arch, const char * os) {
1079 char * host_cpu, * host_os;
1081 defaultMachine(&host_cpu, &host_os);
1085 if (tables[currTables[ARCH]].hasTranslate)
1086 arch = lookupInDefaultTable(arch,
1087 tables[currTables[ARCH]].defaults,
1088 tables[currTables[ARCH]].defaultsLength);
1093 if (tables[currTables[OS]].hasTranslate)
1094 os = lookupInDefaultTable(os,
1095 tables[currTables[OS]].defaults,
1096 tables[currTables[OS]].defaultsLength);
1099 if (!current[ARCH] || strcmp(arch, current[ARCH])) {
1100 if (current[ARCH]) free(current[ARCH]);
1101 current[ARCH] = xstrdup(arch);
1102 rebuildCompatTables(ARCH, host_cpu);
1105 if (!current[OS] || strcmp(os, current[OS])) {
1106 if (current[OS]) free(current[OS]);
1107 current[OS] = xstrdup(os);
1109 * XXX Capitalizing the 'L' is needed to insure that old
1110 * XXX os-from-uname (e.g. "Linux") is compatible with the new
1111 * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
1112 * XXX A copy of this string is embedded in headers and is
1113 * XXX used by rpmInstallPackage->{os,arch}Okay->rpmMachineScore->
1114 * XXX to verify correct arch/os from headers.
1116 if (!strcmp(current[OS], "linux"))
1118 rebuildCompatTables(OS, host_os);
1122 static void rebuildCompatTables(int type, char * name) {
1123 machFindEquivs(&tables[currTables[type]].cache,
1124 &tables[currTables[type]].equiv,
1128 static void getMachineInfo(int type, /*@only@*/ /*@out@*/ char ** name,
1131 struct canonEntry * canon;
1132 int which = currTables[type];
1134 /* use the normal canon tables, even if we're looking up build stuff */
1135 if (which >= 2) which -= 2;
1137 canon = lookupInCanonTable(current[type],
1138 tables[which].canons,
1139 tables[which].canonsLength);
1142 if (num) *num = canon->num;
1143 if (name) *name = canon->short_name;
1145 if (num) *num = 255;
1146 if (name) *name = current[type];
1148 if (tables[currTables[type]].hasCanon) {
1149 rpmMessage(RPMMESS_WARNING, _("Unknown system: %s\n"), current[type]);
1150 rpmMessage(RPMMESS_WARNING, _("Please contact rpm-list@redhat.com\n"));
1155 void rpmGetArchInfo(char ** name, int * num) {
1156 getMachineInfo(ARCH, name, num);
1159 void rpmGetOsInfo(char ** name, int * num) {
1160 getMachineInfo(OS, name, num);
1163 void rpmRebuildTargetVars(const char **buildtarget, const char ** canontarget)
1166 char *ca = NULL, *co = NULL, *ct = NULL;
1169 /* Rebuild the compat table to recalculate the current target arch. */
1171 rpmSetMachine(NULL, NULL);
1172 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1173 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
1175 if (buildtarget && *buildtarget) {
1177 /* Set arch and os from specified build target */
1178 ca = xstrdup(*buildtarget);
1179 if ((c = strchr(ca, '-')) != NULL) {
1182 if ((co = strrchr(c, '-')) == NULL) {
1185 if (!strcasecmp(co, "-gnu"))
1187 if ((co = strrchr(c, '-')) == NULL)
1192 if (co != NULL) co = xstrdup(co);
1195 /* Set build target from rpm arch and os */
1196 rpmGetArchInfo(&ca,NULL);
1197 if (ca) ca = xstrdup(ca);
1198 rpmGetOsInfo(&co,NULL);
1199 if (co) co = xstrdup(co);
1202 /* If still not set, Set target arch/os from default uname(2) values */
1204 defaultMachine(&ca, NULL);
1207 for (x = 0; ca[x]; x++)
1208 ca[x] = tolower(ca[x]);
1211 defaultMachine(NULL, &co);
1214 for (x = 0; co[x]; x++)
1215 co[x] = tolower(co[x]);
1217 /* XXX For now, set canonical target to arch-os */
1219 ct = xmalloc(strlen(ca) + sizeof("-") + strlen(co));
1220 sprintf(ct, "%s-%s", ca, co);
1224 * XXX All this macro pokery/jiggery could be achieved by doing a delayed
1225 * initMacros(NULL, PER-PLATFORM-MACRO-FILE-NAMES);
1227 delMacro(NULL, "_target");
1228 addMacro(NULL, "_target", NULL, ct, RMIL_RPMRC);
1229 delMacro(NULL, "_target_cpu");
1230 addMacro(NULL, "_target_cpu", NULL, ca, RMIL_RPMRC);
1231 delMacro(NULL, "_target_os");
1232 addMacro(NULL, "_target_os", NULL, co, RMIL_RPMRC);
1234 * XXX Make sure that per-arch optflags is initialized correctly.
1236 { char *optflags = rpmGetVarArch(RPMVAR_OPTFLAGS, ca);
1237 if (optflags != NULL) {
1238 delMacro(NULL, "optflags");
1239 addMacro(NULL, "optflags", NULL, optflags, RMIL_RPMRC);
1251 void rpmFreeRpmrc(void)
1255 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
1256 struct tableType *t;
1258 if (t->equiv.list) {
1259 for (j = 0; j < t->equiv.count; j++) {
1260 if (t->equiv.list[j].name) xfree(t->equiv.list[j].name);
1262 xfree(t->equiv.list);
1264 if (t->cache.cache) {
1265 for (j = 0; j < t->cache.size; j++) {
1266 struct machCacheEntry *e;
1267 e = t->cache.cache + j;
1268 if (e == NULL) continue;
1269 if (e->name) xfree(e->name);
1271 for (k = 0; k < e->count; k++) {
1272 if (e->equivs[k]) xfree(e->equivs[k]);
1277 xfree(t->cache.cache);
1280 for (j = 0; j < t->defaultsLength; j++) {
1281 if (t->defaults[j].name) xfree(t->defaults[j].name);
1282 if (t->defaults[j].defName) xfree(t->defaults[j].defName);
1287 for (j = 0; j < t->canonsLength; j++) {
1288 if (t->canons[j].name) xfree(t->canons[j].name);
1289 if (t->canons[j].short_name) xfree(t->canons[j].short_name);
1295 for (i = 0; i < RPMVAR_NUM; i++) {
1296 struct rpmvarValue *this;
1297 while ((this = values[i].next) != NULL) {
1298 values[i].next = this->next;
1299 if (this->value) xfree(this->value);
1300 if (this->arch) xfree(this->arch);
1303 if (values[i].value) xfree(values[i].value);
1304 if (values[i].arch) xfree(values[i].arch);
1306 if (current[OS]) xfree(current[OS]);
1307 if (current[ARCH]) xfree(current[ARCH]);
1311 int rpmShowRC(FILE *f)
1313 struct rpmOption *opt;
1315 struct machEquivTable * equivTable;
1317 /* the caller may set the build arch which should be printed here */
1318 fprintf(f, "ARCHITECTURE AND OS:\n");
1319 fprintf(f, "build arch : %s\n", current[ARCH]);
1321 fprintf(f, "compatible build archs:");
1322 equivTable = &tables[RPM_MACHTABLE_BUILDARCH].equiv;
1323 for (i = 0; i < equivTable->count; i++)
1324 fprintf(f," %s", equivTable->list[i].name);
1327 fprintf(f, "build os : %s\n", current[OS]);
1329 fprintf(f, "compatible build os's :");
1330 equivTable = &tables[RPM_MACHTABLE_BUILDOS].equiv;
1331 for (i = 0; i < equivTable->count; i++)
1332 fprintf(f," %s", equivTable->list[i].name);
1335 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1336 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
1338 fprintf(f, "install arch : %s\n", current[ARCH]);
1339 fprintf(f, "install os : %s\n", current[OS]);
1341 fprintf(f, "compatible archs :");
1342 equivTable = &tables[RPM_MACHTABLE_INSTARCH].equiv;
1343 for (i = 0; i < equivTable->count; i++)
1344 fprintf(f," %s", equivTable->list[i].name);
1347 fprintf(f, "compatible os's :");
1348 equivTable = &tables[RPM_MACHTABLE_INSTOS].equiv;
1349 for (i = 0; i < equivTable->count; i++)
1350 fprintf(f," %s", equivTable->list[i].name);
1353 fprintf(f, "\nRPMRC VALUES:\n");
1354 for (i = 0, opt = optionTable; i < optionTableSize; i++, opt++) {
1355 char *s = rpmGetVar(opt->var);
1356 if (s != NULL || rpmGetVerbosity() < RPMMESS_NORMAL)
1357 fprintf(f, "%-21s : %s\n", opt->name, s ? s : "(not set)");
1360 dumpMacroTable(NULL, f);