xref: /original-bsd/usr.sbin/config/main.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1980, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/file.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include "y.tab.h"
24 #include "config.h"
25 
26 static char *PREFIX;
27 
28 /*
29  * Config builds a set of files for building a UNIX
30  * system given a description of the desired system.
31  */
32 main(argc, argv)
33 	int argc;
34 	char **argv;
35 {
36 
37 	extern char *optarg;
38 	extern int optind;
39 	struct stat buf;
40 	int ch;
41 	char *p;
42 
43 	while ((ch = getopt(argc, argv, "gp")) != EOF)
44 		switch (ch) {
45 		case 'g':
46 			debugging++;
47 			break;
48 		case 'p':
49 			profiling++;
50 			break;
51 		case '?':
52 		default:
53 			goto usage;
54 		}
55 	argc -= optind;
56 	argv += optind;
57 
58 	if (argc != 1) {
59 usage:		fputs("usage: config [-gp] sysname\n", stderr);
60 		exit(1);
61 	}
62 
63 	if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
64 		perror(PREFIX);
65 		exit(2);
66 	}
67 	if (stat(p = path((char *)NULL), &buf)) {
68 		if (mkdir(p, 0777)) {
69 			perror(p);
70 			exit(2);
71 		}
72 	}
73 	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
74 		fprintf(stderr, "config: %s isn't a directory.\n", p);
75 		exit(2);
76 	}
77 
78 	dtab = NULL;
79 	confp = &conf_list;
80 	compp = &comp_list;
81 	if (yyparse())
82 		exit(3);
83 	switch (machine) {
84 
85 	case MACHINE_VAX:
86 		vax_ioconf();		/* Print ioconf.c */
87 		ubglue();		/* Create ubglue.s */
88 		break;
89 
90 	case MACHINE_TAHOE:
91 		tahoe_ioconf();
92 		vbglue();
93 		break;
94 
95 	case MACHINE_HP300:
96 	case MACHINE_LUNA68K:
97 		hp300_ioconf();
98 		hpglue();
99 		break;
100 
101 	case MACHINE_I386:
102 		i386_ioconf();		/* Print ioconf.c */
103 		vector();		/* Create vector.s */
104 		break;
105 
106 	case MACHINE_MIPS:
107 	case MACHINE_PMAX:
108 		pmax_ioconf();
109 		break;
110 
111 	case MACHINE_NEWS3400:
112 		news_ioconf();
113 		break;
114 
115 	default:
116 		printf("Specify machine type, e.g. ``machine vax''\n");
117 		exit(1);
118 	}
119 	/*
120 	 * make symbolic links in compilation directory
121 	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
122 	 * and similarly for "machine".
123 	 */
124 	{
125 	char xxx[80];
126 
127 	(void) sprintf(xxx, "../../%s/include", machinename);
128 	(void) symlink(xxx, path("machine"));
129 	}
130 	makefile();			/* build Makefile */
131 	headers();			/* make a lot of .h files */
132 	swapconf();			/* swap config files */
133 	printf("Don't forget to run \"make depend\"\n");
134 	exit(0);
135 }
136 
137 /*
138  * get_word
139  *	returns EOF on end of file
140  *	NULL on end of line
141  *	pointer to the word otherwise
142  */
143 char *
144 get_word(fp)
145 	register FILE *fp;
146 {
147 	static char line[80];
148 	register int ch;
149 	register char *cp;
150 
151 	while ((ch = getc(fp)) != EOF)
152 		if (ch != ' ' && ch != '\t')
153 			break;
154 	if (ch == EOF)
155 		return ((char *)EOF);
156 	if (ch == '\n')
157 		return (NULL);
158 	cp = line;
159 	*cp++ = ch;
160 	while ((ch = getc(fp)) != EOF) {
161 		if (isspace(ch))
162 			break;
163 		*cp++ = ch;
164 	}
165 	*cp = 0;
166 	if (ch == EOF)
167 		return ((char *)EOF);
168 	(void) ungetc(ch, fp);
169 	return (line);
170 }
171 
172 /*
173  * get_quoted_word
174  *	like get_word but will accept something in double or single quotes
175  *	(to allow embedded spaces).
176  */
177 char *
178 get_quoted_word(fp)
179 	register FILE *fp;
180 {
181 	static char line[256];
182 	register int ch;
183 	register char *cp;
184 
185 	while ((ch = getc(fp)) != EOF)
186 		if (ch != ' ' && ch != '\t')
187 			break;
188 	if (ch == EOF)
189 		return ((char *)EOF);
190 	if (ch == '\n')
191 		return (NULL);
192 	cp = line;
193 	if (ch == '"' || ch == '\'') {
194 		register int quote = ch;
195 
196 		while ((ch = getc(fp)) != EOF) {
197 			if (ch == quote)
198 				break;
199 			if (ch == '\n') {
200 				*cp = 0;
201 				printf("config: missing quote reading `%s'\n",
202 					line);
203 				exit(2);
204 			}
205 			*cp++ = ch;
206 		}
207 	} else {
208 		*cp++ = ch;
209 		while ((ch = getc(fp)) != EOF) {
210 			if (isspace(ch))
211 				break;
212 			*cp++ = ch;
213 		}
214 		if (ch != EOF)
215 			(void) ungetc(ch, fp);
216 	}
217 	*cp = 0;
218 	if (ch == EOF)
219 		return ((char *)EOF);
220 	return (line);
221 }
222 
223 /*
224  * prepend the path to a filename
225  */
226 char *
227 path(file)
228 	char *file;
229 {
230 	register char *cp;
231 
232 #define	CDIR	"../../compile/"
233 	cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) +
234 	    (file ? strlen(file) : 0) + 2));
235 	(void) strcpy(cp, CDIR);
236 	(void) strcat(cp, PREFIX);
237 	if (file) {
238 		(void) strcat(cp, "/");
239 		(void) strcat(cp, file);
240 	}
241 	return (cp);
242 }
243