Added new kernel source file 'config.c'. This file is generated by config(8),
[unix-history] / usr.sbin / config / main.c
CommitLineData
15637ed4
RG
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, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35char copyright[] =
36"@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)main.c 5.17 (Berkeley) 7/1/91";
42#endif /* not lint */
43
44#include <sys/types.h>
45#include <sys/stat.h>
46#include <sys/file.h>
47#include <stdio.h>
48#include <ctype.h>
49#include "y.tab.h"
50#include "config.h"
51
52static char *PREFIX;
53
54/*
55 * Config builds a set of files for building a UNIX
56 * system given a description of the desired system.
57 */
58main(argc, argv)
59 int argc;
60 char **argv;
61{
62
63 extern char *optarg;
64 extern int optind;
65 struct stat buf;
66 int ch;
67 char *p;
68
69 while ((ch = getopt(argc, argv, "gp")) != EOF)
70 switch (ch) {
71 case 'g':
72 debugging++;
73 break;
74 case 'p':
75 profiling++;
76 break;
77 case '?':
78 default:
79 goto usage;
80 }
81 argc -= optind;
82 argv += optind;
83
84 if (argc != 1) {
85usage: fputs("usage: config [-gp] sysname\n", stderr);
86 exit(1);
87 }
88
89 if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
90 perror(PREFIX);
91 exit(2);
92 }
93 if (stat(p = path((char *)NULL), &buf)) {
94 if (mkdir(p, 0777)) {
95 perror(p);
96 exit(2);
97 }
98 }
99 else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
100 fprintf(stderr, "config: %s isn't a directory.\n", p);
101 exit(2);
102 }
103
1419e6d4 104 loadaddress = -1;
15637ed4
RG
105 dtab = NULL;
106 confp = &conf_list;
107 if (yyparse())
108 exit(3);
109 switch (machine) {
110
111 case MACHINE_VAX:
112 vax_ioconf(); /* Print ioconf.c */
113 ubglue(); /* Create ubglue.s */
114 break;
115
116 case MACHINE_TAHOE:
117 tahoe_ioconf();
118 vbglue();
119 break;
120
121 case MACHINE_HP300:
122 hp300_ioconf();
123 hpglue();
124 break;
125
126 case MACHINE_I386:
127 i386_ioconf(); /* Print ioconf.c */
128 vector(); /* Create vector.s */
129 break;
130
131 default:
132 printf("Specify machine type, e.g. ``machine vax''\n");
133 exit(1);
134 }
135 /*
136 * make symbolic links in compilation directory
137 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
138 * and similarly for "machine".
139 */
140 {
141 char xxx[80];
142
143 (void) sprintf(xxx, "../../%s/include", machinename);
144 (void) symlink(xxx, path("machine"));
145 }
146 makefile(); /* build Makefile */
147 headers(); /* make a lot of .h files */
148 swapconf(); /* swap config files */
9f5e3574 149 configfile(); /* add config file into kernel */
15637ed4
RG
150 printf("Don't forget to run \"make depend\"\n");
151 exit(0);
152}
153
154/*
155 * get_word
156 * returns EOF on end of file
157 * NULL on end of line
158 * pointer to the word otherwise
159 */
160char *
161get_word(fp)
162 register FILE *fp;
163{
164 static char line[80];
165 register int ch;
166 register char *cp;
167
168 while ((ch = getc(fp)) != EOF)
169 if (ch != ' ' && ch != '\t')
170 break;
171 if (ch == EOF)
172 return ((char *)EOF);
173 if (ch == '\n')
174 return (NULL);
175 cp = line;
176 *cp++ = ch;
177 while ((ch = getc(fp)) != EOF) {
178 if (isspace(ch))
179 break;
180 *cp++ = ch;
181 }
182 *cp = 0;
183 if (ch == EOF)
184 return ((char *)EOF);
185 (void) ungetc(ch, fp);
186 return (line);
187}
188
189/*
190 * get_quoted_word
191 * like get_word but will accept something in double or single quotes
192 * (to allow embedded spaces).
193 */
194char *
195get_quoted_word(fp)
196 register FILE *fp;
197{
198 static char line[256];
199 register int ch;
200 register char *cp;
201
202 while ((ch = getc(fp)) != EOF)
203 if (ch != ' ' && ch != '\t')
204 break;
205 if (ch == EOF)
206 return ((char *)EOF);
207 if (ch == '\n')
208 return (NULL);
209 cp = line;
210 if (ch == '"' || ch == '\'') {
211 register int quote = ch;
212
213 while ((ch = getc(fp)) != EOF) {
214 if (ch == quote)
215 break;
216 if (ch == '\n') {
217 *cp = 0;
218 printf("config: missing quote reading `%s'\n",
219 line);
220 exit(2);
221 }
222 *cp++ = ch;
223 }
224 } else {
225 *cp++ = ch;
226 while ((ch = getc(fp)) != EOF) {
227 if (isspace(ch))
228 break;
229 *cp++ = ch;
230 }
231 if (ch != EOF)
232 (void) ungetc(ch, fp);
233 }
234 *cp = 0;
235 if (ch == EOF)
236 return ((char *)EOF);
237 return (line);
238}
239
240/*
241 * prepend the path to a filename
242 */
243char *
244path(file)
245 char *file;
246{
247 register char *cp;
248
249#define CDIR "../../compile/"
250 cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) +
251 (file ? strlen(file) : 0) + 2));
252 (void) strcpy(cp, CDIR);
253 (void) strcat(cp, PREFIX);
254 if (file) {
255 (void) strcat(cp, "/");
256 (void) strcat(cp, file);
257 }
258 return (cp);
259}
9f5e3574
PHK
260
261
262configfile()
263{
264 FILE *fi, *fo;
265 char *p;
266 int i;
267
268 fi = fopen(PREFIX,"r");
269 if(!fi) {
270 perror(PREFIX);
271 exit(2);
272 }
273 fo = fopen(p=path("config.c"),"w");
274 if(!fo) {
275 perror(p);
276 exit(2);
277 }
278 fprintf(fo,"static char *config = \"\n");
279 fprintf(fo,"START CONFIG FILE %s\n___",PREFIX);
280 while (EOF != (i=getc(fi))) {
281 if(i == '\n') {
282 fprintf(fo,"\n___");
283 } else if(i == '\"') {
284 fprintf(fo,"\\\"");
285 } else if(i == '\\') {
286 fprintf(fo,"\\\\");
287 } else {
288 putc(i,fo);
289 }
290 }
291 fprintf(fo,"\nEND CONFIG FILE %s\n",PREFIX);
292 fprintf(fo,"\";\n");
293 fclose(fi);
294 fclose(fo);
295}