1/*
2 * Copyright (c) 1999-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7 * Reserved.  This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License').  You may not use this file
10 * except in compliance with the License.  Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Mach Operating System
26 * Copyright (c) 1990 Carnegie-Mellon University
27 * Copyright (c) 1989 Carnegie-Mellon University
28 * Copyright (c) 1988 Carnegie-Mellon University
29 * Copyright (c) 1987 Carnegie-Mellon University
30 * All rights reserved.  The CMU software License Agreement specifies
31 * the terms and conditions for use and redistribution.
32 */
33
34/*
35 * Copyright (c) 1980 Regents of the University of California.
36 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms are permitted
39 * provided that the above copyright notice and this paragraph are
40 * duplicated in all such forms and that any documentation,
41 * advertising materials, and other materials related to such
42 * distribution and use acknowledge that the software was developed
43 * by the University of California, Berkeley.  The name of the
44 * University may not be used to endorse or promote products derived
45 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
48 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
49 */
50
51#ifndef lint
52char copyright[] =
53"@(#) Copyright (c) 1980 Regents of the University of California.\n\
54 All rights reserved.\n";
55#endif /* not lint */
56
57#ifndef lint
58static char sccsid[] __attribute__((used)) = "@(#)main.c	5.9 (Berkeley) 6/18/88";
59#endif /* not lint */
60
61#include <stdio.h>
62#include <ctype.h>
63#include "parser.h"
64#include "config.h"
65
66/*
67 * Config builds a set of files for building a UNIX
68 * system given a description of the desired system.
69 */
70int
71main(int argc, char *argv[])
72{
73
74	source_directory = "..";	/* default */
75	object_directory = "..";
76	config_directory = (char *) 0;
77	while ((argc > 1) && (argv[1][0] == '-')) {
78		char		*c;
79
80		argv++; argc--;
81		for (c = &argv[0][1]; *c ; c++) {
82			switch (*c) {
83				case 'b':
84					build_directory = argv[1];
85					goto check_arg;
86
87				case 'd':
88					source_directory = argv[1];
89					goto check_arg;
90
91				case 'o':
92					object_directory = argv[1];
93					goto check_arg;
94
95				case 'c':
96					config_directory = argv[1];
97
98				 check_arg:
99				 	if (argv[1] == (char *) 0)
100						goto usage_error;
101					argv++; argc--;
102					break;
103
104				case 'p':
105					profiling++;
106					break;
107				default:
108					goto usage_error;
109			}
110		}
111	}
112	if (config_directory == (char *) 0) {
113		config_directory =
114			malloc((unsigned) strlen(source_directory) + 6);
115		(void) sprintf(config_directory, "%s/conf", source_directory);
116	}
117	if (argc != 2) {
118		usage_error: ;
119		fprintf(stderr, "usage: config [ -bcdo dir ] [ -p ] sysname\n");
120		exit(1);
121	}
122	if (!build_directory)
123		build_directory = argv[1];
124	if (freopen(argv[1], "r", stdin) == NULL) {
125		perror(argv[1]);
126		exit(2);
127	}
128	dtab = NULL;
129	confp = &conf_list;
130	opt = 0;
131	if (yyparse())
132		exit(3);
133	switch (machine) {
134
135	case MACHINE_VAX:
136		vax_ioconf();		/* Print ioconf.c */
137		ubglue();		/* Create ubglue.s */
138		break;
139
140	case MACHINE_SUN:
141		sun_ioconf();
142		break;
143
144	case MACHINE_SUN2:
145	case MACHINE_SUN3:
146	case MACHINE_SUN4:
147		sun_ioconf();           /* Print ioconf.c */
148		mbglue();               /* Create mbglue.s */
149		break;
150
151	case MACHINE_ROMP:
152		romp_ioconf();
153		break;
154
155	case MACHINE_MMAX:
156		mmax_ioconf();
157		break;
158
159	case MACHINE_SQT:
160		sqt_ioconf();
161		break;
162
163	case MACHINE_I386:
164	case MACHINE_IX:
165		i386_ioconf();
166		break;
167
168	case MACHINE_MIPSY:
169	case MACHINE_MIPS:
170		mips_ioconf();
171		break;
172
173	case MACHINE_I860:
174		/* i860_ioconf(); */
175		break;
176
177	case MACHINE_M68K:
178		m68k_ioconf();
179  		break;
180
181	case MACHINE_M88K:
182		m88k_ioconf();
183  		break;
184
185	case MACHINE_M98K:
186		m98k_ioconf();
187  		break;
188
189	case MACHINE_HPPA:
190		hppa_ioconf();
191		break;
192
193	case MACHINE_SPARC:
194		sparc_ioconf();
195		break;
196
197	case MACHINE_PPC:
198		ppc_ioconf();
199		break;
200
201	case MACHINE_ARM:
202	case MACHINE_ARM64:
203		arm_ioconf();
204		break;
205
206	case MACHINE_X86_64:
207		x86_64_ioconf();
208		break;
209
210	default:
211		printf("Specify machine type, e.g. ``machine vax''\n");
212		exit(1);
213	}
214
215	makefile();			/* build Makefile */
216	headers();			/* make a lot of .h files */
217	swapconf();			/* swap config files */
218
219	return 0;
220}
221
222/*
223 * get_word
224 *	returns EOF on end of file
225 *	NULL on end of line
226 *	pointer to the word otherwise
227 */
228const char *
229get_word(FILE *fp)
230{
231	static char line[80];
232	register int ch;
233	register char *cp;
234
235	while ((ch = getc(fp)) != EOF)
236		if (ch != ' ' && ch != '\t')
237			break;
238	if (ch == EOF)
239		return ((char *)EOF);
240	if (ch == '\n')
241		return (NULL);
242	if (ch == '|')
243		return( "|");
244	cp = line;
245	*cp++ = ch;
246	while ((ch = getc(fp)) != EOF) {
247		if (isspace(ch))
248			break;
249		*cp++ = ch;
250	}
251	*cp = 0;
252	if (ch == EOF)
253		return ((char *)EOF);
254	(void) ungetc(ch, fp);
255	return (line);
256}
257
258/*
259 * get_rest
260 *	returns EOF on end of file
261 *	NULL on end of line
262 *	pointer to the word otherwise
263 */
264char *
265get_rest(FILE *fp)
266{
267	static char line[80];
268	register int ch;
269	register char *cp;
270
271	cp = line;
272	while ((ch = getc(fp)) != EOF) {
273		if (ch == '\n')
274			break;
275		*cp++ = ch;
276	}
277	*cp = 0;
278	if (ch == EOF)
279		return ((char *)EOF);
280	return (line);
281}
282
283/*
284 * prepend the path to a filename
285 */
286char *
287path(const char *file)
288{
289	register char *cp;
290
291	cp = malloc((unsigned)(strlen(build_directory)+
292			       strlen(file)+
293			       strlen(object_directory)+
294			       3));
295	(void) sprintf(cp, "%s/%s/%s", object_directory, build_directory, file);
296	return (cp);
297}
298