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 * urlfn);
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);
274 * We have a general graph built using strings instead of pointers.
275 * Yuck. We have to start at a point at traverse it, remembering how
276 * far away everything is.
278 machAddEquiv(table, key, 1);
279 machCacheEntryVisit(cache, table, key, 2);
282 static int addCanon(struct canonEntry **table, int *tableLen, char *line,
283 const char *fn, int lineNum)
285 struct canonEntry *t;
290 *table = xmalloc(2 * sizeof(struct canonEntry));
293 *table = xrealloc(*table, sizeof(struct canonEntry) * (*tableLen));
295 t = & ((*table)[*tableLen - 2]);
297 t->name = strtok(line, ": \t");
298 t->short_name = strtok(NULL, " \t");
299 s = strtok(NULL, " \t");
300 if (! (t->name && t->short_name && s)) {
301 rpmError(RPMERR_RPMRC, _("Incomplete data line at %s:%d"), fn, lineNum);
304 if (strtok(NULL, " \t")) {
305 rpmError(RPMERR_RPMRC, _("Too many args in data line at %s:%d"),
310 t->num = strtoul(s, &s1, 10);
311 if ((*s1) || (s1 == s) || (t->num == ULONG_MAX)) {
312 rpmError(RPMERR_RPMRC, _("Bad arch/os number: %s (%s:%d)"), s,
314 return(RPMERR_RPMRC);
317 t->name = xstrdup(t->name);
318 t->short_name = xstrdup(t->short_name);
320 /* From A B C entry */
321 /* Add B B C entry */
322 t[1].name = xstrdup(t->short_name);
323 t[1].short_name = xstrdup(t->short_name);
329 static int addDefault(struct defaultEntry **table, int *tableLen, char *line,
330 const char *fn, int lineNum)
332 struct defaultEntry *t;
336 *table = xmalloc(sizeof(struct defaultEntry));
339 *table = xrealloc(*table, sizeof(struct defaultEntry) * (*tableLen));
341 t = & ((*table)[*tableLen - 1]);
343 t->name = strtok(line, ": \t");
344 t->defName = strtok(NULL, " \t");
345 if (! (t->name && t->defName)) {
346 rpmError(RPMERR_RPMRC, _("Incomplete default line at %s:%d"),
350 if (strtok(NULL, " \t")) {
351 rpmError(RPMERR_RPMRC, _("Too many args in default line at %s:%d"),
356 t->name = xstrdup(t->name);
357 t->defName = xstrdup(t->defName);
362 static struct canonEntry *lookupInCanonTable(char *name,
363 struct canonEntry *table,
367 if (!strcmp(name, table[tableLen].name)) {
368 return &(table[tableLen]);
375 static const char *lookupInDefaultTable(const char *name, struct defaultEntry *table,
379 if (!strcmp(name, table[tableLen].name)) {
380 return table[tableLen].defName;
387 int rpmReadConfigFiles(const char * file, const char * target)
390 /* Preset target macros */
391 rpmRebuildTargetVars(&target, NULL);
394 if (rpmReadRC(file)) return -1;
396 /* Reset target macros */
397 rpmRebuildTargetVars(&target, NULL);
399 /* Finally set target platform */
400 { const char *cpu = rpmExpand("%{_target_cpu}", NULL);
401 const char *os = rpmExpand("%{_target_os}", NULL);
402 rpmSetMachine(cpu, os);
410 static void setVarDefault(int var, const char *macroname, const char *val, const char *body)
412 if (var >= 0) { /* XXX Dying ... */
413 if (rpmGetVar(var)) return;
418 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
421 static void setPathDefault(int var, const char *macroname, const char *subdir)
424 if (var >= 0) { /* XXX Dying ... */
428 if (rpmGetVar(var)) return;
430 topdir = rpmGetPath("%{_topdir}", NULL);
432 fn = alloca(strlen(topdir) + strlen(subdir) + 2);
434 if (fn[strlen(topdir) - 1] != '/')
439 if (topdir) xfree(topdir);
442 if (macroname != NULL) {
443 #define _TOPDIRMACRO "%{_topdir}/"
444 char *body = alloca(sizeof(_TOPDIRMACRO) + strlen(subdir));
445 strcpy(body, _TOPDIRMACRO);
446 strcat(body, subdir);
447 addMacro(NULL, macroname, NULL, body, RMIL_DEFAULT);
452 static const char *prescriptenviron = "\n\
453 RPM_SOURCE_DIR=\"%{_sourcedir}\"\n\
454 RPM_BUILD_DIR=\"%{_builddir}\"\n\
455 RPM_OPT_FLAGS=\"%{optflags}\"\n\
456 RPM_ARCH=\"%{_arch}\"\n\
458 export RPM_SOURCE_DIR RPM_BUILD_DIR RPM_OPT_FLAGS RPM_ARCH RPM_OS\n\
459 RPM_DOC_DIR=\"%{_docdir}\"\n\
460 export RPM_DOC_DIR\n\
461 RPM_PACKAGE_NAME=\"%{name}\"\n\
462 RPM_PACKAGE_VERSION=\"%{version}\"\n\
463 RPM_PACKAGE_RELEASE=\"%{release}\"\n\
464 export RPM_PACKAGE_NAME RPM_PACKAGE_VERSION RPM_PACKAGE_RELEASE\n\
465 %{?buildroot:RPM_BUILD_ROOT=\"%{buildroot}\"\n\
466 export RPM_BUILD_ROOT\n}\
469 static void setDefaults(void) {
471 addMacro(NULL, "_usr", NULL, "/usr", RMIL_DEFAULT);
472 addMacro(NULL, "_var", NULL, "/var", RMIL_DEFAULT);
474 addMacro(NULL, "_preScriptEnvironment",NULL, prescriptenviron,RMIL_DEFAULT);
476 setVarDefault(-1, "_topdir",
477 "/usr/src/redhat", "%{_usr}/src/redhat");
478 setVarDefault(-1, "_tmppath",
479 "/var/tmp", "%{_var}/tmp");
480 setVarDefault(-1, "_dbpath",
481 "/var/lib/rpm", "%{_var}/lib/rpm");
482 setVarDefault(-1, "_defaultdocdir",
483 "/usr/doc", "%{_usr}/doc");
485 setVarDefault(-1, "_rpmfilename",
486 "%%{ARCH}/%%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm",NULL);
488 setVarDefault(RPMVAR_OPTFLAGS, "optflags",
490 setVarDefault(-1, "sigtype",
492 setVarDefault(-1, "_buildshell",
495 setPathDefault(-1, "_builddir", "BUILD");
496 setPathDefault(-1, "_rpmdir", "RPMS");
497 setPathDefault(-1, "_srcrpmdir", "SRPMS");
498 setPathDefault(-1, "_sourcedir", "SOURCES");
499 setPathDefault(-1, "_specdir", "SPECS");
503 int rpmReadRC(const char * rcfiles)
505 char *myrcfiles, *r, *re;
507 static int first = 1;
515 rcfiles = defrcfiles;
517 /* Read each file in rcfiles. */
519 for (r = myrcfiles = xstrdup(rcfiles); *r != '\0'; r = re) {
523 /* Get pointer to rest of files */
524 for (re = r; (re = strchr(re, ':')) != NULL; re++) {
525 if (!(re[1] == '/' && re[2] == '/'))
528 if (re && *re == ':')
533 /* Expand ~/ to $HOME/ */
535 if (r[0] == '~' && r[1] == '/') {
536 char *home = getenv("HOME");
538 /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */
539 if (rcfiles == defrcfiles && myrcfiles != r)
541 rpmError(RPMERR_RPMRC, _("Cannot expand %s"), r);
550 /* Read another rcfile */
551 fd = Fopen(fn, "r.fpio");
552 if (fd == NULL || Ferror(fd)) {
553 /* XXX Only /usr/lib/rpm/rpmrc must exist in default rcfiles list */
554 if (rcfiles == defrcfiles && myrcfiles != r)
556 rpmError(RPMERR_RPMRC, _("Unable to open %s for reading: %s."),
561 rc = doReadRC(fd, fn);
565 if (myrcfiles) free(myrcfiles);
569 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
571 { const char *macrofiles;
572 if ((macrofiles = rpmGetVar(RPMVAR_MACROFILES)) != NULL) {
573 macrofiles = strdup(macrofiles);
574 rpmInitMacros(NULL, macrofiles);
582 static int doReadRC( /*@killref@*/ FD_t fd, const char * urlfn)
587 struct rpmOption searchOption, * option;
590 /* XXX really need rc = Slurp(fd, const char * filename, char ** buf) */
591 { off_t size = fdSize(fd);
592 size_t nb = (size >= 0 ? size : (8*BUFSIZ - 2));
597 next = alloca(nb + 2);
599 rc = Fread(next, sizeof(*next), nb, fd);
600 if (Ferror(fd) || (size > 0 && rc != nb)) { /* XXX Feof(fd) */
601 rpmError(RPMERR_RPMRC, _("Failed to read %s: %s."), urlfn,
617 /* Find end-of-line. */
618 while (*se && *se != '\n') se++;
619 if (*se) *se++ = '\0';
622 /* Trim leading spaces */
623 while (*s && isspace(*s)) s++;
625 /* We used to allow comments to begin anywhere, but not anymore. */
626 if (*s == '#' || *s == '\0') continue;
628 /* Find end-of-keyword. */
630 while (*se && !isspace(*se) && *se != ':') se++;
634 while (*se && isspace(*se) && *se != ':') se++;
638 rpmError(RPMERR_RPMRC, _("missing ':' (found 0x%02x) at %s:%d"),
639 (0xff & *se), urlfn, linenum);
642 *se++ = '\0'; /* terminate keyword or option, point to value */
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, urlfn, 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, urlfn, linenum, s);
679 fdinc = Fopen(fn, "r.fpio");
680 if (fdinc == NULL || Ferror(fdinc)) {
681 rpmError(RPMERR_RPMRC, _("cannot open %s at %s:%d: %s"),
682 fn, urlfn, linenum, Fstrerror(fdinc));
685 rc = doReadRC(fdinc, fn);
689 continue; /* XXX don't save include value as var/macro */
690 } /*@notreached@*/ break;
691 case RPMVAR_MACROFILES:
692 fn = rpmGetPath(se, NULL);
693 if (fn == NULL || *fn == '\0') {
694 rpmError(RPMERR_RPMRC, _("%s expansion failed at %s:%d \"%s\""),
695 option->name, urlfn, linenum, fn);
701 case RPMVAR_PROVIDES:
703 s = rpmGetVar(RPMVAR_PROVIDES);
704 if (s == NULL) s = "";
705 fn = t = xmalloc(strlen(s) + strlen(se) + 2);
706 while (*s) *t++ = *s++;
708 while (*se) *t++ = *se++;
716 if (option->archSpecific) {
718 while (*se && !isspace(*se)) se++;
720 rpmError(RPMERR_RPMRC,
721 _("missing architecture for %s at %s:%d"),
722 option->name, urlfn, linenum);
726 while (*se && isspace(*se)) se++;
728 rpmError(RPMERR_RPMRC,
729 _("missing argument for %s at %s:%d"),
730 option->name, urlfn, linenum);
737 /* Only add macros if appropriate for this arch */
738 if (option->macroize &&
739 (arch == NULL || !strcmp(arch, current[ARCH]))) {
741 n = name = xmalloc(strlen(option->name)+2);
742 if (option->localize)
744 strcpy(n, option->name);
745 addMacro(NULL, name, NULL, val, RMIL_RPMRC);
748 rpmSetVarArch(option->var, val, arch);
751 } else { /* For arch/os compatibilty tables ... */
757 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
758 if (!strncmp(tables[i].key, s, strlen(tables[i].key)))
762 if (i < RPM_MACHTABLE_COUNT) {
763 const char *rest = s + strlen(tables[i].key);
764 if (*rest == '_') rest++;
766 if (!strcmp(rest, "compat")) {
767 if (machCompatCacheAdd(se, urlfn, linenum,
771 } else if (tables[i].hasTranslate &&
772 !strcmp(rest, "translate")) {
773 if (addDefault(&tables[i].defaults,
774 &tables[i].defaultsLength,
778 } else if (tables[i].hasCanon &&
779 !strcmp(rest, "canon")) {
780 if (addCanon(&tables[i].canons, &tables[i].canonsLength,
788 rpmError(RPMERR_RPMRC, _("bad option '%s' at %s:%d"),
797 static void defaultMachine(const char ** arch, const char ** os) {
798 static struct utsname un;
799 static int gotDefaults = 0;
801 struct canonEntry * canon;
806 #if !defined(__linux__)
808 /* USUALLY un.sysname on sinix does start with the word "SINIX"
809 * let's be absolutely sure
811 sprintf(un.sysname,"SINIX");
813 if (!strcmp(un.sysname, "AIX")) {
814 strcpy(un.machine, __power_pc() ? "ppc" : "rs6000");
815 sprintf(un.sysname,"aix%s.%s",un.version,un.release);
817 else if (!strcmp(un.sysname, "SunOS")) {
818 if (!strncmp(un.release,"4", 1)) /* SunOS 4.x */ {
821 (un.release[fd] != 0 && (fd < sizeof(un.release)));
823 if (!isdigit(un.release[fd]) && (un.release[fd] != '.')) {
828 sprintf(un.sysname,"sunos%s",un.release);
831 else /* Solaris 2.x: n.x.x becomes n-3.x.x */
832 sprintf(un.sysname, "solaris%1d%s", atoi(un.release)-3,
833 un.release+1+(atoi(un.release)/10));
835 else if (!strcmp(un.sysname, "HP-UX"))
836 /*make un.sysname look like hpux9.05 for example*/
837 sprintf(un.sysname, "hpux%s", strpbrk(un.release,"123456789"));
838 else if (!strcmp(un.sysname, "OSF1"))
839 /*make un.sysname look like osf3.2 for example*/
840 sprintf(un.sysname,"osf%s",strpbrk(un.release,"123456789"));
841 else if (!strncmp(un.sysname, "IP", 2))
842 un.sysname[2] = '\0';
843 else if (!strncmp(un.sysname, "SINIX", 5)) {
844 sprintf(un.sysname, "sinix%s",un.release);
845 if (!strncmp(un.machine, "RM", 2))
846 sprintf(un.machine, "mips");
848 else if ((!strncmp(un.machine, "34", 2) ||
849 !strncmp(un.machine, "33", 2)) && \
850 !strncmp(un.release, "4.0", 3))
852 /* we are on ncr-sysv4 */
855 fd = Fopen("/etc/.relid", "r.fdio");
857 chptr = (char *) xcalloc(1, 256);
859 int irelid = Fread(chptr, sizeof(*chptr), 256, fd);
861 /* example: "112393 RELEASE 020200 Version 01 OS" */
863 if ((prelid=strstr(chptr, "RELEASE "))){
864 prelid += strlen("RELEASE ")+1;
865 sprintf(un.sysname,"ncr-sysv4.%.*s",1,prelid);
871 if (prelid == NULL) /* parsing /etc/.relid file failed? */
872 strcpy(un.sysname,"ncr-sysv4");
873 /* wrong, just for now, find out how to look for i586 later*/
874 strcpy(un.machine,"i486");
876 #endif /* __linux__ */
878 /* get rid of the hyphens in the sysname */
879 for (chptr = un.machine; *chptr; chptr++)
880 if (*chptr == '/') *chptr = '-';
882 # if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL)
884 strcpy(un.machine, "mipsel");
885 # elif defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB)
887 strcpy(un.machine, "mipseb");
890 # if defined(__hpux) && defined(_SC_CPU_VERSION)
892 # if !defined(CPU_PA_RISC1_2)
893 # define CPU_PA_RISC1_2 0x211 /* HP PA-RISC1.2 */
895 # if !defined(CPU_PA_RISC2_0)
896 # define CPU_PA_RISC2_0 0x214 /* HP PA-RISC2.0 */
898 int cpu_version = sysconf(_SC_CPU_VERSION);
900 # if defined(CPU_HP_MC68020)
901 if (cpu_version == CPU_HP_MC68020)
902 strcpy(un.machine, "m68k");
904 # if defined(CPU_HP_MC68030)
905 if (cpu_version == CPU_HP_MC68030)
906 strcpy(un.machine, "m68k");
908 # if defined(CPU_HP_MC68040)
909 if (cpu_version == CPU_HP_MC68040)
910 strcpy(un.machine, "m68k");
913 # if defined(CPU_PA_RISC1_0)
914 if (cpu_version == CPU_PA_RISC1_0)
915 strcpy(un.machine, "hppa1.0");
917 # if defined(CPU_PA_RISC1_1)
918 if (cpu_version == CPU_PA_RISC1_1)
919 strcpy(un.machine, "hppa1.1");
921 # if defined(CPU_PA_RISC1_2)
922 if (cpu_version == CPU_PA_RISC1_2)
923 strcpy(un.machine, "hppa1.2");
925 # if defined(CPU_PA_RISC2_0)
926 if (cpu_version == CPU_PA_RISC2_0)
927 strcpy(un.machine, "hppa2.0");
932 # if defined(__linux__) && defined(__sparc__)
933 if (!strcmp(un.machine, "sparc")) {
934 #define PERS_LINUX 0x00000000
935 #define PERS_LINUX_32BIT 0x00800000
936 #define PERS_LINUX32 0x00000008
938 extern int personality(unsigned long);
941 oldpers = personality(PERS_LINUX_32BIT);
943 if (personality(PERS_LINUX) != -1) {
945 if (! strcmp(un.machine, "sparc64")) {
946 strcpy(un.machine, "sparcv9");
947 oldpers = PERS_LINUX32;
950 personality(oldpers);
953 # endif /* sparc*-linux */
955 /* the uname() result goes through the arch_canon table */
956 canon = lookupInCanonTable(un.machine,
957 tables[RPM_MACHTABLE_INSTARCH].canons,
958 tables[RPM_MACHTABLE_INSTARCH].canonsLength);
960 strcpy(un.machine, canon->short_name);
962 canon = lookupInCanonTable(un.sysname,
963 tables[RPM_MACHTABLE_INSTOS].canons,
964 tables[RPM_MACHTABLE_INSTOS].canonsLength);
966 strcpy(un.sysname, canon->short_name);
970 if (arch) *arch = un.machine;
971 if (os) *os = un.sysname;
974 static const char * rpmGetVarArch(int var, char * arch) {
975 struct rpmvarValue * next;
977 if (!arch) arch = current[ARCH];
982 if (next->arch && !strcmp(next->arch, arch)) return next->value;
988 while (next && next->arch) next = next->next;
990 return next ? next->value : NULL;
993 const char *rpmGetVar(int var)
995 return rpmGetVarArch(var, NULL);
998 /* this doesn't free the passed pointer! */
999 static void freeRpmVar(struct rpmvarValue * orig) {
1000 struct rpmvarValue * next, * var = orig;
1013 if (var != orig) free(var);
1018 void rpmSetVar(int var, const char *val) {
1019 freeRpmVar(&values[var]);
1020 values[var].value = (val ? xstrdup(val) : NULL);
1023 static void rpmSetVarArch(int var, const char * val, const char * arch) {
1024 struct rpmvarValue * next = values + var;
1028 while (next->next) {
1029 if (next->arch && !strcmp(next->arch, arch)) break;
1033 while (next->next) {
1034 if (!next->arch) break;
1039 if (next->arch && arch && !strcmp(next->arch, arch)) {
1040 if (next->value) xfree(next->value);
1041 if (next->arch) xfree(next->arch);
1042 } else if (next->arch || arch) {
1043 next->next = xmalloc(sizeof(*next->next));
1051 next->value = xstrdup(val); /* XXX memory leak, hard to plug */
1052 next->arch = (arch ? xstrdup(arch) : NULL);
1055 void rpmSetTables(int archTable, int osTable) {
1056 const char * arch, * os;
1058 defaultMachine(&arch, &os);
1060 if (currTables[ARCH] != archTable) {
1061 currTables[ARCH] = archTable;
1062 rebuildCompatTables(ARCH, arch);
1065 if (currTables[OS] != osTable) {
1066 currTables[OS] = osTable;
1067 rebuildCompatTables(OS, os);
1071 int rpmMachineScore(int type, const char * name) {
1072 struct machEquivInfo * info = machEquivSearch(&tables[type].equiv, name);
1073 return (info != NULL ? info->score : 0);
1076 void rpmGetMachine(const char **arch, const char **os)
1079 *arch = current[ARCH];
1085 void rpmSetMachine(const char * arch, const char * os) {
1086 const char * host_cpu, * host_os;
1088 defaultMachine(&host_cpu, &host_os);
1092 if (tables[currTables[ARCH]].hasTranslate)
1093 arch = lookupInDefaultTable(arch,
1094 tables[currTables[ARCH]].defaults,
1095 tables[currTables[ARCH]].defaultsLength);
1100 if (tables[currTables[OS]].hasTranslate)
1101 os = lookupInDefaultTable(os,
1102 tables[currTables[OS]].defaults,
1103 tables[currTables[OS]].defaultsLength);
1106 if (!current[ARCH] || strcmp(arch, current[ARCH])) {
1107 if (current[ARCH]) free(current[ARCH]);
1108 current[ARCH] = xstrdup(arch);
1109 rebuildCompatTables(ARCH, host_cpu);
1112 if (!current[OS] || strcmp(os, current[OS])) {
1113 if (current[OS]) free(current[OS]);
1114 current[OS] = xstrdup(os);
1116 * XXX Capitalizing the 'L' is needed to insure that old
1117 * XXX os-from-uname (e.g. "Linux") is compatible with the new
1118 * XXX os-from-platform (e.g "linux" from "sparc-*-linux").
1119 * XXX A copy of this string is embedded in headers and is
1120 * XXX used by rpmInstallPackage->{os,arch}Okay->rpmMachineScore->
1121 * XXX to verify correct arch/os from headers.
1123 if (!strcmp(current[OS], "linux"))
1125 rebuildCompatTables(OS, host_os);
1129 static void rebuildCompatTables(int type, const char * name) {
1130 machFindEquivs(&tables[currTables[type]].cache,
1131 &tables[currTables[type]].equiv,
1135 static void getMachineInfo(int type, /*@only@*/ /*@out@*/ const char ** name,
1138 struct canonEntry * canon;
1139 int which = currTables[type];
1141 /* use the normal canon tables, even if we're looking up build stuff */
1142 if (which >= 2) which -= 2;
1144 canon = lookupInCanonTable(current[type],
1145 tables[which].canons,
1146 tables[which].canonsLength);
1149 if (num) *num = canon->num;
1150 if (name) *name = canon->short_name;
1152 if (num) *num = 255;
1153 if (name) *name = current[type];
1155 if (tables[currTables[type]].hasCanon) {
1156 rpmMessage(RPMMESS_WARNING, _("Unknown system: %s\n"), current[type]);
1157 rpmMessage(RPMMESS_WARNING, _("Please contact rpm-list@redhat.com\n"));
1162 void rpmGetArchInfo(const char ** name, int * num) {
1163 getMachineInfo(ARCH, name, num);
1166 void rpmGetOsInfo(const char ** name, int * num) {
1167 getMachineInfo(OS, name, num);
1170 void rpmRebuildTargetVars(const char **buildtarget, const char ** canontarget)
1173 char *ca = NULL, *co = NULL, *ct = NULL;
1176 /* Rebuild the compat table to recalculate the current target arch. */
1178 rpmSetMachine(NULL, NULL);
1179 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1180 rpmSetTables(RPM_MACHTABLE_BUILDARCH, RPM_MACHTABLE_BUILDOS);
1182 if (buildtarget && *buildtarget) {
1184 /* Set arch and os from specified build target */
1185 ca = xstrdup(*buildtarget);
1186 if ((c = strchr(ca, '-')) != NULL) {
1189 if ((co = strrchr(c, '-')) == NULL) {
1192 if (!strcasecmp(co, "-gnu"))
1194 if ((co = strrchr(c, '-')) == NULL)
1199 if (co != NULL) co = xstrdup(co);
1203 /* Set build target from rpm arch and os */
1204 rpmGetArchInfo(&a,NULL);
1205 ca = (a) ? xstrdup(a) : NULL;
1206 rpmGetOsInfo(&o,NULL);
1207 co = (o) ? xstrdup(o) : NULL;
1210 /* If still not set, Set target arch/os from default uname(2) values */
1213 defaultMachine(&a, NULL);
1214 ca = (a) ? xstrdup(a) : NULL;
1216 for (x = 0; ca[x]; x++)
1217 ca[x] = tolower(ca[x]);
1221 defaultMachine(NULL, &o);
1222 co = (o) ? xstrdup(o) : NULL;
1224 for (x = 0; co[x]; x++)
1225 co[x] = tolower(co[x]);
1227 /* XXX For now, set canonical target to arch-os */
1229 ct = xmalloc(strlen(ca) + sizeof("-") + strlen(co));
1230 sprintf(ct, "%s-%s", ca, co);
1234 * XXX All this macro pokery/jiggery could be achieved by doing a delayed
1235 * rpmInitMacros(NULL, PER-PLATFORM-MACRO-FILE-NAMES);
1237 delMacro(NULL, "_target");
1238 addMacro(NULL, "_target", NULL, ct, RMIL_RPMRC);
1239 delMacro(NULL, "_target_cpu");
1240 addMacro(NULL, "_target_cpu", NULL, ca, RMIL_RPMRC);
1241 delMacro(NULL, "_target_os");
1242 addMacro(NULL, "_target_os", NULL, co, RMIL_RPMRC);
1244 * XXX Make sure that per-arch optflags is initialized correctly.
1246 { const char *optflags = rpmGetVarArch(RPMVAR_OPTFLAGS, ca);
1247 if (optflags != NULL) {
1248 delMacro(NULL, "optflags");
1249 addMacro(NULL, "optflags", NULL, optflags, RMIL_RPMRC);
1261 void rpmFreeRpmrc(void)
1265 for (i = 0; i < RPM_MACHTABLE_COUNT; i++) {
1266 struct tableType *t;
1268 if (t->equiv.list) {
1269 for (j = 0; j < t->equiv.count; j++) {
1270 if (t->equiv.list[j].name) xfree(t->equiv.list[j].name);
1272 xfree(t->equiv.list);
1274 if (t->cache.cache) {
1275 for (j = 0; j < t->cache.size; j++) {
1276 struct machCacheEntry *e;
1277 e = t->cache.cache + j;
1278 if (e == NULL) continue;
1279 if (e->name) xfree(e->name);
1281 for (k = 0; k < e->count; k++) {
1282 if (e->equivs[k]) xfree(e->equivs[k]);
1287 xfree(t->cache.cache);
1290 for (j = 0; j < t->defaultsLength; j++) {
1291 if (t->defaults[j].name) xfree(t->defaults[j].name);
1292 if (t->defaults[j].defName) xfree(t->defaults[j].defName);
1297 for (j = 0; j < t->canonsLength; j++) {
1298 if (t->canons[j].name) xfree(t->canons[j].name);
1299 if (t->canons[j].short_name) xfree(t->canons[j].short_name);
1305 for (i = 0; i < RPMVAR_NUM; i++) {
1306 struct rpmvarValue *this;
1307 while ((this = values[i].next) != NULL) {
1308 values[i].next = this->next;
1309 if (this->value) xfree(this->value);
1310 if (this->arch) xfree(this->arch);
1313 if (values[i].value) xfree(values[i].value);
1314 if (values[i].arch) xfree(values[i].arch);
1316 if (current[OS]) xfree(current[OS]);
1317 if (current[ARCH]) xfree(current[ARCH]);
1321 int rpmShowRC(FILE *f)
1323 struct rpmOption *opt;
1325 struct machEquivTable * equivTable;
1327 /* the caller may set the build arch which should be printed here */
1328 fprintf(f, "ARCHITECTURE AND OS:\n");
1329 fprintf(f, "build arch : %s\n", current[ARCH]);
1331 fprintf(f, "compatible build archs:");
1332 equivTable = &tables[RPM_MACHTABLE_BUILDARCH].equiv;
1333 for (i = 0; i < equivTable->count; i++)
1334 fprintf(f," %s", equivTable->list[i].name);
1337 fprintf(f, "build os : %s\n", current[OS]);
1339 fprintf(f, "compatible build os's :");
1340 equivTable = &tables[RPM_MACHTABLE_BUILDOS].equiv;
1341 for (i = 0; i < equivTable->count; i++)
1342 fprintf(f," %s", equivTable->list[i].name);
1345 rpmSetTables(RPM_MACHTABLE_INSTARCH, RPM_MACHTABLE_INSTOS);
1346 rpmSetMachine(NULL, NULL); /* XXX WTFO? Why bother? */
1348 fprintf(f, "install arch : %s\n", current[ARCH]);
1349 fprintf(f, "install os : %s\n", current[OS]);
1351 fprintf(f, "compatible archs :");
1352 equivTable = &tables[RPM_MACHTABLE_INSTARCH].equiv;
1353 for (i = 0; i < equivTable->count; i++)
1354 fprintf(f," %s", equivTable->list[i].name);
1357 fprintf(f, "compatible os's :");
1358 equivTable = &tables[RPM_MACHTABLE_INSTOS].equiv;
1359 for (i = 0; i < equivTable->count; i++)
1360 fprintf(f," %s", equivTable->list[i].name);
1363 fprintf(f, "\nRPMRC VALUES:\n");
1364 for (i = 0, opt = optionTable; i < optionTableSize; i++, opt++) {
1365 const char *s = rpmGetVar(opt->var);
1366 if (s != NULL || rpmGetVerbosity() < RPMMESS_NORMAL)
1367 fprintf(f, "%-21s : %s\n", opt->name, s ? s : "(not set)");
1370 rpmDumpMacroTable(NULL, f);