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 int doReadRC(FD_t fd, const char * filename);
108 static void rpmSetVarArch(int var, const char * val, const char * arch);
109 static void rebuildCompatTables(int type, const char *name);
111 static int optionCompare(const void * a, const void * b) {
112 return strcasecmp(((struct rpmOption *) a)->name,
113 ((struct rpmOption *) b)->name);
116 static void rpmRebuildTargetVars(const char **target, const char ** canontarget);
118 static struct machCacheEntry * machCacheFindEntry(struct machCache * cache,
123 for (i = 0; i < cache->size; i++)
124 if (!strcmp(cache->cache[i].name, key)) return cache->cache + i;
129 static int machCompatCacheAdd(char * name, const char * fn, int linenum,
130 struct machCache * cache)
132 char * chptr, * equivs;
135 struct machCacheEntry * entry = NULL;
137 while (*name && isspace(*name)) name++;
140 while (*chptr && *chptr != ':') chptr++;
142 rpmError(RPMERR_RPMRC, _("missing second ':' at %s:%d"), fn, linenum);
144 } else if (chptr == name) {
145 rpmError(RPMERR_RPMRC, _("missing architecture name at %s:%d"), fn,
150 while (*chptr == ':' || isspace(*chptr)) chptr--;
153 while (*equivs && isspace(*equivs)) equivs++;
159 entry = machCacheFindEntry(cache, name);
161 for (i = 0; i < entry->count; i++)
162 free(entry->equivs[i]);
163 if (entry->count) free(entry->equivs);
169 cache->cache = xrealloc(cache->cache,
170 (cache->size + 1) * sizeof(*cache->cache));
171 entry = cache->cache + cache->size++;
172 entry->name = xstrdup(name);
177 if (delEntry) return 0;
179 while ((chptr = strtok(equivs, " ")) != NULL) {
181 if (chptr[0] == '\0') /* does strtok() return "" ever?? */
184 entry->equivs = xrealloc(entry->equivs, sizeof(*entry->equivs)
185 * (entry->count + 1));
187 entry->equivs = xmalloc(sizeof(*entry->equivs));
189 entry->equivs[entry->count] = xstrdup(chptr);
196 static struct machEquivInfo * machEquivSearch(
197 struct machEquivTable * table, const char * name)
202 * XXX The strcasecmp below is necessary so the old (rpm < 2.90) style
203 * XXX os-from-uname (e.g. "Linux") is compatible with the new
204 * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
205 * XXX A copy of this string is embedded in headers and is
206 * XXX used by rpmInstallPackage->{os,arch}Okay->rpmMachineScore->.
207 * XXX to verify correct arch/os from headers.
209 for (i = 0; i < table->count; i++)
210 if (!strcasecmp(table->list[i].name, name))
211 return table->list + i;
216 static void machAddEquiv(struct machEquivTable * table, const char * name,
219 struct machEquivInfo * equiv;
221 equiv = machEquivSearch(table, name);
224 table->list = xrealloc(table->list, (table->count + 1)
225 * sizeof(*table->list));
227 table->list = xmalloc(sizeof(*table->list));
229 table->list[table->count].name = xstrdup(name);
230 table->list[table->count++].score = distance;
234 static void machCacheEntryVisit(struct machCache * cache,
235 struct machEquivTable * table,
239 struct machCacheEntry * entry;
242 entry = machCacheFindEntry(cache, name);
243 if (!entry || entry->visited) return;
247 for (i = 0; i < entry->count; i++) {
248 machAddEquiv(table, entry->equivs[i], distance);
251 for (i = 0; i < entry->count; i++) {
252 machCacheEntryVisit(cache, table, entry->equivs[i], distance + 1);
256 static void machFindEquivs(struct machCache * cache,
257 struct machEquivTable * table,
262 for (i = 0; i < cache->size; i++)
263 cache->cache[i].visited = 0;
265 while (table->count > 0) {
266 xfree(table->list[--table->count].name);
267 table->list[table->count].name = NULL;
270 if (table->list) xfree(table->list);
273 /* We have a general graph built using strings instead of pointers.
274 Yuck. We have to start at a point at traverse it, remembering how
275 far away everything is. */
276 machAddEquiv(table, key, 1);
277 machCacheEntryVisit(cache, table, key, 2);
280 static int addCanon(struct canonEntry **table, int *tableLen, char *line,
281 const char *fn, int lineNum)
283 struct canonEntry *t;
288 *table = xmalloc(2 * sizeof(struct canonEntry));
291 *table = xrealloc(*table, sizeof(struct canonEntry) * (*tableLen));
293 t = & ((*table)[*tableLen - 2]);
295 t->name = strtok(line, ": \t");
296 t->short_name = strtok(NULL, " \t");
297 s = strtok(NULL, " \t");
298 if (! (t->name && t->short_name && s)) {
299 rpmError(RPMERR_RPMRC, _("Incomplete data line at %s:%d"), fn, lineNum);
302 if (strtok(NULL, " \t")) {
303 rpmError(RPMERR_RPMRC, _("Too many args in data line at %s:%d"),
308 t->num = strtoul(s, &s1, 10);
309 if ((*s1) || (s1 == s) || (t->num == ULONG_MAX)) {
310 rpmError(RPMERR_RPMRC, _("Bad arch/os number: %s (%s:%d)"), s,
312 return(RPMERR_RPMRC);
315 t->name = xstrdup(t->name);
316 t->short_name = xstrdup(t->short_name);
318 /* From A B C entry */
319 /* Add B B C entry */
320 t[1].name = xstrdup(t->short_name);
321 t[1].short_name = xstrdup(t->short_name);
327 static int addDefault(struct defaultEntry **table, int *tableLen, char *line,
328 const char *fn, int lineNum)
330 struct defaultEntry *t;
334 *table = xmalloc(sizeof(struct defaultEntry));
337 *table = xrealloc(*table, sizeof(struct defaultEntry) * (*tableLen));
339 t = & ((*table)[*tableLen - 1]);
341 t->name = strtok(line, ": \t");
342 t->defName = strtok(NULL, " \t");
343 if (! (t->name && t->defName)) {
344 rpmError(RPMERR_RPMRC, _("Incomplete default line at %s:%d"),
348 if (strtok(NULL, " \t")) {
349 rpmError(RPMERR_RPMRC, _("Too many args in default line at %s:%d"),
354 t->name = xstrdup(t->name);
355 t->defName = xstrdup(t->defName);
360 static struct canonEntry *lookupInCanonTable(char *name,
361 struct canonEntry *table,
365 if (!strcmp(name, table[tableLen].name)) {
366 return &(table[tableLen]);
373 static const char *lookupInDefaultTable(const char *name, struct defaultEntry *table,
377 if (!strcmp(name, table[tableLen].name)) {
378 return table[tableLen].defName;
385 int rpmReadConfigFiles(const char * file, const char * target)
388 /* Preset target macros */
389 rpmRebuildTargetVars(&target, NULL);
392 if (rpmReadRC(file)) return -1;
394 /* Reset target macros */
395 rpmRebuildTargetVars(&target, NULL);
397 /* Finally set target platform */
398 { const char *cpu = rpmExpand("%{_target_cpu}", NULL);
399 const char *os = rpmExpand("%{_target_os}", NULL);
400 rpmSetMachine(cpu, os);
408 static void setVarDefault(int var, const char *macroname, const char *val, const char *body)
410 if (var >= 0) { /* XXX Dying ... */
411 if (rpmGetVar(var)) return;
416 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
419 static void setPathDefault(int var, const char *macroname, const char *subdir)
422 if (var >= 0) { /* XXX Dying ... */
426 if (rpmGetVar(var)) return;
428 topdir = rpmGetPath("%{_topdir}", NULL);
430 fn = alloca(strlen(topdir) + strlen(subdir) + 2);
432 if (fn[strlen(topdir) - 1] != '/')
437 if (topdir) xfree(topdir);
440 if (macroname != NULL) {
441 #define _TOPDIRMACRO "%{_topdir}/"
442 char *body = alloca(sizeof(_TOPDIRMACRO) + strlen(subdir));
443 strcpy(body, _TOPDIRMACRO);
444 strcat(body, subdir);
445 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
450 static const char *prescriptenviron = "\n\
451 RPM_SOURCE_DIR=\"%{_sourcedir}\"\n\
452 RPM_BUILD_DIR=\"%{_builddir}\"\n\
453 RPM_OPT_FLAGS=\"%{optflags}\"\n\
454 RPM_ARCH=\"%{_arch}\"\n\
456 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS\n\
457 RPM_DOC_DIR=\"%{_docdir}\"\n\
458 export RPM_DOC_DIR\n\
459 RPM_PACKAGE_NAME=\"%{name}\"\n\
460 RPM_PACKAGE_VERSION=\"%{version}\"\n\
461 RPM_PACKAGE_RELEASE=\"%{release}\"\n\
462 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\n\
463 %{?buildroot:RPM_BUILD_ROOT=\"%{buildroot}\"\n\
464 export RPM_BUILD_ROOT\n}\
467 static void setDefaults(void) {
469 addMacro(NULL, "_usr", NULL, "/usr", RMIL_DEFAULT);
470 addMacro(NULL, "_var", NULL, "/var", RMIL_DEFAULT);
472 addMacro(NULL, "_preScriptEnvironment",NULL, prescriptenviron,RMIL_DEFAULT);
474 setVarDefault(-1, "_topdir",
475 "/usr/src/redhat", "%{_usr}/src/redhat");
476 setVarDefault(-1, "_tmppath",
477 "/var/tmp", "%{_var}/tmp");
478 setVarDefault(-1, "_dbpath",
479 "/var/lib/rpm", "%{_var}/lib/rpm");
480 setVarDefault(-1, "_defaultdocdir",
481 "/usr/doc", "%{_usr}/doc");
483 setVarDefault(-1, "_rpmfilename",
484 "%%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",NULL);
486 setVarDefault(RPMVAR_OPTFLAGS, "optflags",
488 setVarDefault(-1, "sigtype",
490 setVarDefault(-1, "_buildshell",
493 setPathDefault(-1, "_builddir", "BUILD");
494 setPathDefault(-1, "_rpmdir", "RPMS");
495 setPathDefault(-1, "_srcrpmdir", "SRPMS");
496 setPathDefault(-1, "_sourcedir", "SOURCES");
497 setPathDefault(-1, "_specdir", "SPECS");
501 int rpmReadRC(const char * rcfiles)
503 char *myrcfiles, *r, *re;
505 static int first = 1;
513 rcfiles = defrcfiles;
515 /* Read each file in rcfiles. */
517 for (r = myrcfiles = xstrdup(rcfiles); *r != '\0'; r = re) {
521 /* Get pointer to rest of files */
522 if ((re = strchr(r, ':')) != NULL)
527 /* Expand ~/ to $HOME/ */
529 if (r[0] == '~' && r[1] == '/') {
530 char *home = getenv("HOME");
532 rpmError(RPMERR_RPMRC, _("Cannot expand %s"), r);
541 /* Read another rcfile */
542 fd = fdOpen(fn, O_RDONLY, 0);
544 /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */
545 if (rcfiles == defrcfiles && myrcfiles != r)
548 rpmError(RPMERR_RPMRC, _("Unable to open %s for reading: %s."),
549 fn, strerror(errno));
553 rc = doReadRC(fd, fn);
557 if (myrcfiles) free(myrcfiles);
561 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
563 { const char *macrofiles;
564 if ((macrofiles = rpmGetVar(RPMVAR_MACROFILES)) != NULL) {
565 macrofiles = strdup(macrofiles);
566 initMacros(NULL, macrofiles);
574 static int doReadRC(FD_t fd, const char * filename)
579 struct rpmOption searchOption, * option;
583 fstat(Fileno(fd), &sb);
584 next = alloca(sb.st_size + 2);
585 if (Fread(next, sb.st_size, 1, fd) != sb.st_size) {
586 rpmError(RPMERR_RPMRC, _("Failed to read %s: %s."), filename,
590 next[sb.st_size] = '\n';
591 next[sb.st_size + 1] = '\0';
598 while (*se && *se != '\n') se++;
599 if (*se) *se++ = '\0';
602 while (*s && isspace(*s)) s++;
604 /* we used to allow comments to begin anywhere, but not anymore */
605 if (*s == '#' || *s == '\0') continue;
608 while (*se && !isspace(*se) && *se != ':') se++;
612 while (*se && isspace(*se) && *se != ':') se++;
616 rpmError(RPMERR_RPMRC, _("missing ':' at %s:%d"),
621 while (*se && isspace(*se)) se++;
623 /* Find keyword in table */
624 searchOption.name = s;
625 option = bsearch(&searchOption, optionTable, optionTableSize,
626 sizeof(struct rpmOption), optionCompare);
628 if (option) { /* For configuration variables ... */
629 const char *arch, *val, *fn;
631 arch = val = fn = NULL;
633 rpmError(RPMERR_RPMRC, _("missing argument for %s at %s:%d"),
634 option->name, filename, linenum);
638 switch (option->var) {
643 while (*se && !isspace(*se)) se++;
644 if (*se) *se++ = '\0';
646 rpmRebuildTargetVars(NULL, NULL);
648 fn = rpmGetPath(s, NULL);
649 if (fn == NULL || *fn == '\0') {
650 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\""),
651 option->name, filename, linenum, s);
657 fdinc = fdOpen(fn, O_RDONLY, 0);
660 rpmError(RPMERR_RPMRC, _("cannot open %s at %s:%d"),
661 fn, filename, linenum);
664 rc = doReadRC(fdinc, fn);
669 continue; /* XXX don't save include value as var/macro */
670 } /*@notreached@*/ break;
671 case RPMVAR_MACROFILES:
672 fn = rpmGetPath(se, NULL);
673 if (fn == NULL || *fn == '\0') {
674 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\""),
675 option->name, filename, linenum, fn);
681 case RPMVAR_PROVIDES:
683 s = rpmGetVar(RPMVAR_PROVIDES);
684 if (s == NULL) s = "";
685 fn = t = xmalloc(strlen(s) + strlen(se) + 2);
686 while (*s) *t++ = *s++;
688 while (*se) *t++ = *se++;
696 if (option->archSpecific) {
698 while (*se && !isspace(*se)) se++;
700 rpmError(RPMERR_RPMRC,
701 _("missing architecture for %s at %s:%d"),
702 option->name, filename, linenum);
706 while (*se && isspace(*se)) se++;
708 rpmError(RPMERR_RPMRC,
709 _("missing argument for %s at %s:%d"),
710 option->name, filename, linenum);
717 /* Only add macros if appropriate for this arch */
718 if (option->macroize &&
719 (arch == NULL || !strcmp(arch, current[ARCH]))) {
721 n = name = xmalloc(strlen(option->name)+2);
722 if (option->localize)
724 strcpy(n, option->name);
725 addMacro(NULL, name, NULL, val, RMIL_RPMRC);
728 rpmSetVarArch(option->var, val, arch);
731 } else { /* For arch/os compatibilty tables ... */
737 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
738 if (!strncmp(tables[i].key, s, strlen(tables[i].key)))
742 if (i < RPM_MACHTABLE_COUNT) {
743 const char *rest = s + strlen(tables[i].key);
744 if (*rest == '_') rest++;
746 if (!strcmp(rest, "compat")) {
747 if (machCompatCacheAdd(se, filename, linenum,
751 } else if (tables[i].hasTranslate &&
752 !strcmp(rest, "translate")) {
753 if (addDefault(&tables[i].defaults,
754 &tables[i].defaultsLength,
755 se, filename, linenum))
758 } else if (tables[i].hasCanon &&
759 !strcmp(rest, "canon")) {
760 if (addCanon(&tables[i].canons, &tables[i].canonsLength,
761 se, filename, linenum))
768 rpmError(RPMERR_RPMRC, _("bad option '%s' at %s:%d"),
769 s, filename, linenum);
777 static void defaultMachine(const char ** arch, const char ** os) {
778 static struct utsname un;
779 static int gotDefaults = 0;
781 struct canonEntry * canon;
786 #if !defined(__linux__)
788 /* USUALLY un.sysname on sinix does start with the word "SINIX"
789 * let's be absolutely sure
791 sprintf(un.sysname,"SINIX");
793 if (!strcmp(un.sysname, "AIX")) {
794 strcpy(un.machine, __power_pc() ? "ppc" : "rs6000");
795 sprintf(un.sysname,"aix%s.%s",un.version,un.release);
797 else if (!strcmp(un.sysname, "SunOS")) {
798 if (!strncmp(un.release,"4", 1)) /* SunOS 4.x */ {
800 for (fd=0;(un.release[fd] != 0 && (fd < sizeof(un.release)));fd++)
801 if (!isdigit(un.release[fd]) && (un.release[fd] != '.')) {
805 sprintf(un.sysname,"sunos%s",un.release);
808 else /* Solaris 2.x: n.x.x becomes n-3.x.x */
809 sprintf(un.sysname,"solaris%1d%s",atoi(un.release)-3,un.release+1+(atoi(un.release)/10));
811 else if (!strcmp(un.sysname, "HP-UX"))
812 /*make un.sysname look like hpux9.05 for example*/
813 sprintf(un.sysname,"hpux%s",strpbrk(un.release,"123456789"));
814 else if (!strcmp(un.sysname, "OSF1"))
815 /*make un.sysname look like osf3.2 for example*/
816 sprintf(un.sysname,"osf%s",strpbrk(un.release,"123456789"));
817 else if (!strncmp(un.sysname, "IP", 2))
818 un.sysname[2] = '\0';
819 else if (!strncmp(un.sysname, "SINIX", 5)) {
820 sprintf(un.sysname, "sinix%s",un.release);
821 if (!strncmp(un.machine, "RM", 2))
822 sprintf(un.machine, "mips");
824 else if ((!strncmp(un.machine, "34", 2) || \
825 !strncmp(un.machine, "33", 2)) && \
826 !strncmp(un.release, "4.0", 3)) {
827 /* we are on ncr-sysv4 */
829 FD_t fd = fdOpen("/etc/.relid", O_RDONLY, 0700);
831 chptr = (char *) xcalloc(1, 256);
833 int irelid = Fread(chptr, 256, 1, fd);
835 /* example: "112393 RELEASE 020200 Version 01 OS" */
837 if ((prelid=strstr(chptr, "RELEASE "))){
838 prelid += strlen("RELEASE ")+1;
839 sprintf(un.sysname,"ncr-sysv4.%.*s",1,prelid);
845 if (prelid == NULL) /* parsing /etc/.relid file failed? */
846 strcpy(un.sysname,"ncr-sysv4");
847 /* wrong, just for now, find out how to look for i586 later*/
848 strcpy(un.machine,"i486");
850 #endif /* __linux__ */
852 /* get rid of the hyphens in the sysname */
853 for (chptr = un.machine; *chptr; chptr++)
854 if (*chptr == '/') *chptr = '-';
856 # if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
858 strcpy(un.machine, "mipsel");
859 # elif defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB)
861 strcpy(un.machine, "mipseb");
864 # if defined(__hpux) && defined(_SC_CPU_VERSION)
866 # if !defined(CPU_PA_RISC1_2)
867 # define CPU_PA_RISC1_2 0x211 /* HP PA-RISC1.2 */
869 # if !defined(CPU_PA_RISC2_0)
870 # define CPU_PA_RISC2_0 0x214 /* HP PA-RISC2.0 */
872 int cpu_version = sysconf(_SC_CPU_VERSION);
874 # if defined(CPU_HP_MC68020)
875 if (cpu_version == CPU_HP_MC68020)
876 strcpy(un.machine, "m68k");
878 # if defined(CPU_HP_MC68030)
879 if (cpu_version == CPU_HP_MC68030)
880 strcpy(un.machine, "m68k");
882 # if defined(CPU_HP_MC68040)
883 if (cpu_version == CPU_HP_MC68040)
884 strcpy(un.machine, "m68k");
887 # if defined(CPU_PA_RISC1_0)
888 if (cpu_version == CPU_PA_RISC1_0)
889 strcpy(un.machine, "hppa1.0");
891 # if defined(CPU_PA_RISC1_1)
892 if (cpu_version == CPU_PA_RISC1_1)
893 strcpy(un.machine, "hppa1.1");
895 # if defined(CPU_PA_RISC1_2)
896 if (cpu_version == CPU_PA_RISC1_2)
897 strcpy(un.machine, "hppa1.2");
899 # if defined(CPU_PA_RISC2_0)
900 if (cpu_version == CPU_PA_RISC2_0)
901 strcpy(un.machine, "hppa2.0");
906 # if defined(__linux__) && defined(__sparc__)
907 if (!strcmp(un.machine, "sparc")) {
908 #define PERS_LINUX 0x00000000
909 #define PERS_LINUX_32BIT 0x00800000
910 #define PERS_LINUX32 0x00000008
912 extern int personality(unsigned long);
915 oldpers = personality(PERS_LINUX_32BIT);
917 if (personality(PERS_LINUX) != -1) {
919 if (! strcmp(un.machine, "sparc64")) {
920 strcpy(un.machine, "sparcv9");
921 oldpers = PERS_LINUX32;
924 personality(oldpers);
927 # endif /* sparc*-linux */
929 /* the uname() result goes through the arch_canon table */
930 canon = lookupInCanonTable(un.machine,
931 tables[RPM_MACHTABLE_INSTARCH].canons,
932 tables[RPM_MACHTABLE_INSTARCH].canonsLength);
934 strcpy(un.machine, canon->short_name);
936 canon = lookupInCanonTable(un.sysname,
937 tables[RPM_MACHTABLE_INSTOS].canons,
938 tables[RPM_MACHTABLE_INSTOS].canonsLength);
940 strcpy(un.sysname, canon->short_name);
944 if (arch) *arch = un.machine;
945 if (os) *os = un.sysname;
948 static const char * rpmGetVarArch(int var, char * arch) {
949 struct rpmvarValue * next;
951 if (!arch) arch = current[ARCH];
956 if (next->arch && !strcmp(next->arch, arch)) return next->value;
962 while (next && next->arch) next = next->next;
964 return next ? next->value : NULL;
967 const char *rpmGetVar(int var)
969 return rpmGetVarArch(var, NULL);
972 /* this doesn't free the passed pointer! */
973 static void freeRpmVar(struct rpmvarValue * orig) {
974 struct rpmvarValue * next, * var = orig;
987 if (var != orig) free(var);
992 void rpmSetVar(int var, const char *val) {
993 freeRpmVar(&values[var]);
994 values[var].value = (val ? xstrdup(val) : NULL);
997 static void rpmSetVarArch(int var, const char * val, const char * arch) {
998 struct rpmvarValue * next = values + var;
1002 while (next->next) {
1003 if (next->arch && !strcmp(next->arch, arch)) break;
1007 while (next->next) {
1008 if (!next->arch) break;
1013 if (next->arch && arch && !strcmp(next->arch, arch)) {
1014 if (next->value) xfree(next->value);
1015 if (next->arch) xfree(next->arch);
1016 } else if (next->arch || arch) {
1017 next->next = xmalloc(sizeof(*next->next));
1025 next->value = xstrdup(val); /* XXX memory leak, hard to plug */
1026 next->arch = (arch ? xstrdup(arch) : NULL);
1029 void rpmSetTables(int archTable, int osTable) {
1030 const char * arch, * os;
1032 defaultMachine(&arch, &os);
1034 if (currTables[ARCH] != archTable) {
1035 currTables[ARCH] = archTable;
1036 rebuildCompatTables(ARCH, arch);
1039 if (currTables[OS] != osTable) {
1040 currTables[OS] = osTable;
1041 rebuildCompatTables(OS, os);
1045 int rpmMachineScore(int type, const char * name) {
1046 struct machEquivInfo * info = machEquivSearch(&tables[type].equiv, name);
1047 return (info != NULL ? info->score : 0);
1050 void rpmGetMachine(const char **arch, const char **os)
1053 *arch = current[ARCH];
1059 void rpmSetMachine(const char * arch, const char * os) {
1060 const char * host_cpu, * host_os;
1062 defaultMachine(&host_cpu, &host_os);
1066 if (tables[currTables[ARCH]].hasTranslate)
1067 arch = lookupInDefaultTable(arch,
1068 tables[currTables[ARCH]].defaults,
1069 tables[currTables[ARCH]].defaultsLength);
1074 if (tables[currTables[OS]].hasTranslate)
1075 os = lookupInDefaultTable(os,
1076 tables[currTables[OS]].defaults,
1077 tables[currTables[OS]].defaultsLength);
1080 if (!current[ARCH] || strcmp(arch, current[ARCH])) {
1081 if (current[ARCH]) free(current[ARCH]);
1082 current[ARCH] = xstrdup(arch);
1083 rebuildCompatTables(ARCH, host_cpu);
1086 if (!current[OS] || strcmp(os, current[OS])) {
1087 if (current[OS]) free(current[OS]);
1088 current[OS] = xstrdup(os);
1090 * XXX Capitalizing the 'L' is needed to insure that old
1091 * XXX os-from-uname (e.g. "Linux") is compatible with the new
1092 * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
1093 * XXX A copy of this string is embedded in headers and is
1094 * XXX used by rpmInstallPackage->{os,arch}Okay->rpmMachineScore->
1095 * XXX to verify correct arch/os from headers.
1097 if (!strcmp(current[OS], "linux"))
1099 rebuildCompatTables(OS, host_os);
1103 static void rebuildCompatTables(int type, const char * name) {
1104 machFindEquivs(&tables[currTables[type]].cache,
1105 &tables[currTables[type]].equiv,
1109 static void getMachineInfo(int type, /*@only@*/ /*@out@*/ const char ** name,
1112 struct canonEntry * canon;
1113 int which = currTables[type];
1115 /* use the normal canon tables, even if we're looking up build stuff */
1116 if (which >= 2) which -= 2;
1118 canon = lookupInCanonTable(current[type],
1119 tables[which].canons,
1120 tables[which].canonsLength);
1123 if (num) *num = canon->num;
1124 if (name) *name = canon->short_name;
1126 if (num) *num = 255;
1127 if (name) *name = current[type];
1129 if (tables[currTables[type]].hasCanon) {
1130 rpmMessage(RPMMESS_WARNING, _("Unknown system: %s\n"), current[type]);
1131 rpmMessage(RPMMESS_WARNING, _("Please contact rpm-list@redhat.com\n"));
1136 void rpmGetArchInfo(const char ** name, int * num) {
1137 getMachineInfo(ARCH, name, num);
1140 void rpmGetOsInfo(const char ** name, int * num) {
1141 getMachineInfo(OS, name, num);
1144 void rpmRebuildTargetVars(const char **buildtarget, const char ** canontarget)
1147 char *ca = NULL, *co = NULL, *ct = NULL;
1150 /* Rebuild the compat table to recalculate the current target arch. */
1152 rpmSetMachine(NULL, NULL);
1153 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1154 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
1156 if (buildtarget && *buildtarget) {
1158 /* Set arch and os from specified build target */
1159 ca = xstrdup(*buildtarget);
1160 if ((c = strchr(ca, '-')) != NULL) {
1163 if ((co = strrchr(c, '-')) == NULL) {
1166 if (!strcasecmp(co, "-gnu"))
1168 if ((co = strrchr(c, '-')) == NULL)
1173 if (co != NULL) co = xstrdup(co);
1177 /* Set build target from rpm arch and os */
1178 rpmGetArchInfo(&a,NULL);
1179 ca = (a) ? xstrdup(a) : NULL;
1180 rpmGetOsInfo(&o,NULL);
1181 co = (o) ? xstrdup(o) : NULL;
1184 /* If still not set, Set target arch/os from default uname(2) values */
1187 defaultMachine(&a, NULL);
1188 ca = (a) ? xstrdup(a) : NULL;
1190 for (x = 0; ca[x]; x++)
1191 ca[x] = tolower(ca[x]);
1195 defaultMachine(NULL, &o);
1196 co = (o) ? xstrdup(o) : NULL;
1198 for (x = 0; co[x]; x++)
1199 co[x] = tolower(co[x]);
1201 /* XXX For now, set canonical target to arch-os */
1203 ct = xmalloc(strlen(ca) + sizeof("-") + strlen(co));
1204 sprintf(ct, "%s-%s", ca, co);
1208 * XXX All this macro pokery/jiggery could be achieved by doing a delayed
1209 * initMacros(NULL, PER-PLATFORM-MACRO-FILE-NAMES);
1211 delMacro(NULL, "_target");
1212 addMacro(NULL, "_target", NULL, ct, RMIL_RPMRC);
1213 delMacro(NULL, "_target_cpu");
1214 addMacro(NULL, "_target_cpu", NULL, ca, RMIL_RPMRC);
1215 delMacro(NULL, "_target_os");
1216 addMacro(NULL, "_target_os", NULL, co, RMIL_RPMRC);
1218 * XXX Make sure that per-arch optflags is initialized correctly.
1220 { const char *optflags = rpmGetVarArch(RPMVAR_OPTFLAGS, ca);
1221 if (optflags != NULL) {
1222 delMacro(NULL, "optflags");
1223 addMacro(NULL, "optflags", NULL, optflags, RMIL_RPMRC);
1235 void rpmFreeRpmrc(void)
1239 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
1240 struct tableType *t;
1242 if (t->equiv.list) {
1243 for (j = 0; j < t->equiv.count; j++) {
1244 if (t->equiv.list[j].name) xfree(t->equiv.list[j].name);
1246 xfree(t->equiv.list);
1248 if (t->cache.cache) {
1249 for (j = 0; j < t->cache.size; j++) {
1250 struct machCacheEntry *e;
1251 e = t->cache.cache + j;
1252 if (e == NULL) continue;
1253 if (e->name) xfree(e->name);
1255 for (k = 0; k < e->count; k++) {
1256 if (e->equivs[k]) xfree(e->equivs[k]);
1261 xfree(t->cache.cache);
1264 for (j = 0; j < t->defaultsLength; j++) {
1265 if (t->defaults[j].name) xfree(t->defaults[j].name);
1266 if (t->defaults[j].defName) xfree(t->defaults[j].defName);
1271 for (j = 0; j < t->canonsLength; j++) {
1272 if (t->canons[j].name) xfree(t->canons[j].name);
1273 if (t->canons[j].short_name) xfree(t->canons[j].short_name);
1279 for (i = 0; i < RPMVAR_NUM; i++) {
1280 struct rpmvarValue *this;
1281 while ((this = values[i].next) != NULL) {
1282 values[i].next = this->next;
1283 if (this->value) xfree(this->value);
1284 if (this->arch) xfree(this->arch);
1287 if (values[i].value) xfree(values[i].value);
1288 if (values[i].arch) xfree(values[i].arch);
1290 if (current[OS]) xfree(current[OS]);
1291 if (current[ARCH]) xfree(current[ARCH]);
1295 int rpmShowRC(FILE *f)
1297 struct rpmOption *opt;
1299 struct machEquivTable * equivTable;
1301 /* the caller may set the build arch which should be printed here */
1302 fprintf(f, "ARCHITECTURE AND OS:\n");
1303 fprintf(f, "build arch : %s\n", current[ARCH]);
1305 fprintf(f, "compatible build archs:");
1306 equivTable = &tables[RPM_MACHTABLE_BUILDARCH].equiv;
1307 for (i = 0; i < equivTable->count; i++)
1308 fprintf(f," %s", equivTable->list[i].name);
1311 fprintf(f, "build os : %s\n", current[OS]);
1313 fprintf(f, "compatible build os's :");
1314 equivTable = &tables[RPM_MACHTABLE_BUILDOS].equiv;
1315 for (i = 0; i < equivTable->count; i++)
1316 fprintf(f," %s", equivTable->list[i].name);
1319 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1320 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
1322 fprintf(f, "install arch : %s\n", current[ARCH]);
1323 fprintf(f, "install os : %s\n", current[OS]);
1325 fprintf(f, "compatible archs :");
1326 equivTable = &tables[RPM_MACHTABLE_INSTARCH].equiv;
1327 for (i = 0; i < equivTable->count; i++)
1328 fprintf(f," %s", equivTable->list[i].name);
1331 fprintf(f, "compatible os's :");
1332 equivTable = &tables[RPM_MACHTABLE_INSTOS].equiv;
1333 for (i = 0; i < equivTable->count; i++)
1334 fprintf(f," %s", equivTable->list[i].name);
1337 fprintf(f, "\nRPMRC VALUES:\n");
1338 for (i = 0, opt = optionTable; i < optionTableSize; i++, opt++) {
1339 const char *s = rpmGetVar(opt->var);
1340 if (s != NULL || rpmGetVerbosity() < RPMMESS_NORMAL)
1341 fprintf(f, "%-21s : %s\n", opt->name, s ? s : "(not set)");
1344 dumpMacroTable(NULL, f);