xref: /xnu-10002.61.3/SETUP/config/main.c (revision 0f4c859e951fba394238ab619495c4e1d54d0f34)
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
52 char 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
58 static 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  */
70 int
main(int argc,char * argv[])71 main(int argc, char *argv[])
72 {
73 	source_directory = "..";        /* default */
74 	object_directory = "..";
75 	config_directory = (char *) 0;
76 	while ((argc > 1) && (argv[1][0] == '-')) {
77 		char            *c;
78 
79 		argv++; argc--;
80 		for (c = &argv[0][1]; *c; c++) {
81 			switch (*c) {
82 			case 'b':
83 				build_directory = argv[1];
84 				goto check_arg;
85 
86 			case 'd':
87 				source_directory = argv[1];
88 				goto check_arg;
89 
90 			case 'o':
91 				object_directory = argv[1];
92 				goto check_arg;
93 
94 			case 'c':
95 				config_directory = argv[1];
96 
97 check_arg:
98 				if (argv[1] == (char *) 0) {
99 					goto usage_error;
100 				}
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 	}
125 	if (freopen(argv[1], "r", stdin) == NULL) {
126 		perror(argv[1]);
127 		exit(2);
128 	}
129 	dtab = NULL;
130 	confp = &conf_list;
131 	opt = 0;
132 	if (yyparse()) {
133 		exit(3);
134 	}
135 
136 	mkioconf();                     /* ioconf.c */
137 	makefile();                     /* build Makefile */
138 	headers();                      /* make a lot of .h files */
139 
140 	return 0;
141 }
142 
143 /*
144  * get_word
145  *	returns EOF on end of file
146  *	NULL on end of line
147  *	pointer to the word otherwise
148  */
149 const char *
get_word(FILE * fp)150 get_word(FILE *fp)
151 {
152 	static char line[80];
153 	int ch;
154 	char *cp;
155 
156 	while ((ch = getc(fp)) != EOF) {
157 		if (ch != ' ' && ch != '\t') {
158 			break;
159 		}
160 	}
161 	if (ch == EOF) {
162 		return (char *)EOF;
163 	}
164 	if (ch == '\n') {
165 		return NULL;
166 	}
167 	if (ch == '|') {
168 		return "|";
169 	}
170 	cp = line;
171 	*cp++ = ch;
172 	while ((ch = getc(fp)) != EOF) {
173 		if (isspace(ch)) {
174 			break;
175 		}
176 		*cp++ = ch;
177 	}
178 	*cp = 0;
179 	if (ch == EOF) {
180 		return (char *)EOF;
181 	}
182 	(void) ungetc(ch, fp);
183 	return line;
184 }
185 
186 /*
187  * get_rest
188  *	returns EOF on end of file
189  *	NULL on end of line
190  *	pointer to the word otherwise
191  */
192 char *
get_rest(FILE * fp)193 get_rest(FILE *fp)
194 {
195 	static char line[80];
196 	int ch;
197 	char *cp;
198 
199 	cp = line;
200 	while ((ch = getc(fp)) != EOF) {
201 		if (ch == '\n') {
202 			break;
203 		}
204 		*cp++ = ch;
205 	}
206 	*cp = 0;
207 	if (ch == EOF) {
208 		return (char *)EOF;
209 	}
210 	return line;
211 }
212 
213 /*
214  * prepend the path to a filename
215  */
216 char *
path(const char * file)217 path(const char *file)
218 {
219 	char *cp;
220 
221 	cp = malloc((unsigned)(strlen(build_directory) +
222 	    strlen(file) +
223 	    strlen(object_directory) +
224 	    3));
225 	(void) sprintf(cp, "%s/%s/%s", object_directory, build_directory, file);
226 	return cp;
227 }
228