xref: /original-bsd/usr.sbin/config/main.c (revision 7a9bd2f5)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)main.c	5.11 (Berkeley) 05/16/90";
26 #endif /* not lint */
27 
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/file.h>
31 #include <stdio.h>
32 #include <ctype.h>
33 #include "y.tab.h"
34 #include "config.h"
35 
36 static char *PREFIX;
37 
38 /*
39  * Config builds a set of files for building a UNIX
40  * system given a description of the desired system.
41  */
42 main(argc, argv)
43 	int argc;
44 	char **argv;
45 {
46 
47 	extern char *optarg;
48 	extern int optind;
49 	struct stat buf;
50 	int ch;
51 	char *p;
52 
53 	while ((ch = getopt(argc, argv, "gp")) != EOF)
54 		switch((char)ch) {
55 		case 'g':
56 			debugging++;
57 			break;
58 		case 'p':
59 			profiling++;
60 			break;
61 		case '?':
62 		default:
63 			goto usage;
64 		}
65 	argc -= optind;
66 	argv += optind;
67 
68 	if (argc != 1) {
69 usage:		fputs("usage: config [-gp] sysname\n", stderr);
70 		exit(1);
71 	}
72 
73 	if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
74 		perror(PREFIX);
75 		exit(2);
76 	}
77 	if (stat(p = path((char *)NULL), &buf)) {
78 		if (mkdir(p, 0777)) {
79 			perror(p);
80 			exit(2);
81 		}
82 	}
83 	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
84 		fprintf(stderr, "config: %s isn't a directory.\n", p);
85 		exit(2);
86 	}
87 
88 	dtab = NULL;
89 	confp = &conf_list;
90 	if (yyparse())
91 		exit(3);
92 	switch (machine) {
93 
94 	case MACHINE_VAX:
95 		vax_ioconf();		/* Print ioconf.c */
96 		ubglue();		/* Create ubglue.s */
97 		break;
98 
99 	case MACHINE_TAHOE:
100 		tahoe_ioconf();
101 		vbglue();
102 		break;
103 
104 	case MACHINE_HP300:
105 		hp300_ioconf();
106 		hpglue();
107 		break;
108 
109 	default:
110 		printf("Specify machine type, e.g. ``machine vax''\n");
111 		exit(1);
112 	}
113 	/*
114 	 * make symbolic links in compilation directory
115 	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
116 	 * and similarly for "machine".
117 	 */
118 	{
119 	char xxx[80];
120 
121 	(void) symlink("../sys", path("sys"));
122 	(void) sprintf(xxx, "../%s", machinename);
123 	(void) symlink(xxx, path("machine"));
124 	}
125 	makefile();			/* build Makefile */
126 	headers();			/* make a lot of .h files */
127 	swapconf();			/* swap config files */
128 	printf("Don't forget to run \"make depend\"\n");
129 	exit(0);
130 }
131 
132 /*
133  * get_word
134  *	returns EOF on end of file
135  *	NULL on end of line
136  *	pointer to the word otherwise
137  */
138 char *
139 get_word(fp)
140 	register FILE *fp;
141 {
142 	static char line[80];
143 	register int ch;
144 	register char *cp;
145 
146 	while ((ch = getc(fp)) != EOF)
147 		if (ch != ' ' && ch != '\t')
148 			break;
149 	if (ch == EOF)
150 		return ((char *)EOF);
151 	if (ch == '\n')
152 		return (NULL);
153 	cp = line;
154 	*cp++ = ch;
155 	while ((ch = getc(fp)) != EOF) {
156 		if (isspace(ch))
157 			break;
158 		*cp++ = ch;
159 	}
160 	*cp = 0;
161 	if (ch == EOF)
162 		return ((char *)EOF);
163 	(void) ungetc(ch, fp);
164 	return (line);
165 }
166 
167 /*
168  * prepend the path to a filename
169  */
170 char *
171 path(file)
172 	char *file;
173 {
174 	register char *cp;
175 
176 	cp = malloc((unsigned)(strlen(PREFIX)+strlen(file)+5));
177 	(void) strcpy(cp, "../");
178 	(void) strcat(cp, PREFIX);
179 	if (file) {
180 		(void) strcat(cp, "/");
181 		(void) strcat(cp, file);
182 	}
183 	return (cp);
184 }
185