xref: /csrg-svn/usr.sbin/config/main.c (revision 50348)
119987Sdist /*
219987Sdist  * Copyright (c) 1980 Regents of the University of California.
334131Sbostic  * All rights reserved.
434131Sbostic  *
542795Sbostic  * %sccs.include.redist.c%
619987Sdist  */
719987Sdist 
814557Ssam #ifndef lint
919987Sdist char copyright[] =
1019987Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1119987Sdist  All rights reserved.\n";
1234131Sbostic #endif /* not lint */
132653Stoy 
1419987Sdist #ifndef lint
15*50348Skarels static char sccsid[] = "@(#)main.c	5.17 (Berkeley) 07/01/91";
1634131Sbostic #endif /* not lint */
1719987Sdist 
1834109Sbostic #include <sys/types.h>
1934109Sbostic #include <sys/stat.h>
2034109Sbostic #include <sys/file.h>
212653Stoy #include <stdio.h>
222653Stoy #include <ctype.h>
232653Stoy #include "y.tab.h"
242653Stoy #include "config.h"
252653Stoy 
2634109Sbostic static char *PREFIX;
2734109Sbostic 
288892Sroot /*
298892Sroot  * Config builds a set of files for building a UNIX
308892Sroot  * system given a description of the desired system.
318892Sroot  */
322653Stoy main(argc, argv)
338892Sroot 	int argc;
348892Sroot 	char **argv;
352653Stoy {
368892Sroot 
3734109Sbostic 	extern char *optarg;
3834109Sbostic 	extern int optind;
3934109Sbostic 	struct stat buf;
4034109Sbostic 	int ch;
4134109Sbostic 	char *p;
4234109Sbostic 
4342110Smckusick 	while ((ch = getopt(argc, argv, "gp")) != EOF)
44*50348Skarels 		switch (ch) {
4542110Smckusick 		case 'g':
4642110Smckusick 			debugging++;
4742110Smckusick 			break;
4834109Sbostic 		case 'p':
4934109Sbostic 			profiling++;
5034109Sbostic 			break;
5134109Sbostic 		case '?':
5234109Sbostic 		default:
5334109Sbostic 			goto usage;
5434109Sbostic 		}
5534109Sbostic 	argc -= optind;
5634109Sbostic 	argv += optind;
5734109Sbostic 
5834109Sbostic 	if (argc != 1) {
5942110Smckusick usage:		fputs("usage: config [-gp] sysname\n", stderr);
608892Sroot 		exit(1);
618892Sroot 	}
6234109Sbostic 
6334109Sbostic 	if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
6434109Sbostic 		perror(PREFIX);
658892Sroot 		exit(2);
668892Sroot 	}
6734109Sbostic 	if (stat(p = path((char *)NULL), &buf)) {
6842110Smckusick 		if (mkdir(p, 0777)) {
6934109Sbostic 			perror(p);
7034109Sbostic 			exit(2);
7134109Sbostic 		}
7234109Sbostic 	}
7334109Sbostic 	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
7434109Sbostic 		fprintf(stderr, "config: %s isn't a directory.\n", p);
7534109Sbostic 		exit(2);
7634109Sbostic 	}
7734109Sbostic 
788892Sroot 	dtab = NULL;
7912487Ssam 	confp = &conf_list;
808892Sroot 	if (yyparse())
818892Sroot 		exit(3);
828892Sroot 	switch (machine) {
838892Sroot 
848892Sroot 	case MACHINE_VAX:
858892Sroot 		vax_ioconf();		/* Print ioconf.c */
868892Sroot 		ubglue();		/* Create ubglue.s */
878892Sroot 		break;
888892Sroot 
8929841Ssam 	case MACHINE_TAHOE:
9029841Ssam 		tahoe_ioconf();
9129841Ssam 		vbglue();
928892Sroot 		break;
938892Sroot 
9442110Smckusick 	case MACHINE_HP300:
9542110Smckusick 		hp300_ioconf();
9642110Smckusick 		hpglue();
9742110Smckusick 		break;
9842110Smckusick 
9945952Swilliam 	case MACHINE_I386:
10045952Swilliam 		i386_ioconf();		/* Print ioconf.c */
10145952Swilliam 		vector();		/* Create vector.s */
10245952Swilliam 		break;
10345952Swilliam 
1048892Sroot 	default:
1058892Sroot 		printf("Specify machine type, e.g. ``machine vax''\n");
1068892Sroot 		exit(1);
1078892Sroot 	}
10830379Skarels 	/*
10930379Skarels 	 * make symbolic links in compilation directory
11030379Skarels 	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
11130379Skarels 	 * and similarly for "machine".
11230379Skarels 	 */
11330379Skarels 	{
11430379Skarels 	char xxx[80];
11530379Skarels 
11645888Sbostic 	(void) sprintf(xxx, "../../%s/include", machinename);
11730379Skarels 	(void) symlink(xxx, path("machine"));
11830379Skarels 	}
1192653Stoy 	makefile();			/* build Makefile */
1202653Stoy 	headers();			/* make a lot of .h files */
12112487Ssam 	swapconf();			/* swap config files */
1223156Stoy 	printf("Don't forget to run \"make depend\"\n");
12332605Sbostic 	exit(0);
1242653Stoy }
1252653Stoy 
1262653Stoy /*
1272653Stoy  * get_word
1282653Stoy  *	returns EOF on end of file
1292653Stoy  *	NULL on end of line
1302653Stoy  *	pointer to the word otherwise
1312653Stoy  */
1328892Sroot char *
1338892Sroot get_word(fp)
1348892Sroot 	register FILE *fp;
1352653Stoy {
1368892Sroot 	static char line[80];
1378892Sroot 	register int ch;
1388892Sroot 	register char *cp;
1392653Stoy 
1408892Sroot 	while ((ch = getc(fp)) != EOF)
1418892Sroot 		if (ch != ' ' && ch != '\t')
1428892Sroot 			break;
1438892Sroot 	if (ch == EOF)
1448900Sroot 		return ((char *)EOF);
1458892Sroot 	if (ch == '\n')
1468892Sroot 		return (NULL);
1478892Sroot 	cp = line;
1482653Stoy 	*cp++ = ch;
1498892Sroot 	while ((ch = getc(fp)) != EOF) {
1508892Sroot 		if (isspace(ch))
1518892Sroot 			break;
1528892Sroot 		*cp++ = ch;
1538892Sroot 	}
1548892Sroot 	*cp = 0;
1558892Sroot 	if (ch == EOF)
1568900Sroot 		return ((char *)EOF);
1578900Sroot 	(void) ungetc(ch, fp);
1588892Sroot 	return (line);
1592653Stoy }
1602682Stoy 
1612682Stoy /*
162*50348Skarels  * get_quoted_word
163*50348Skarels  *	like get_word but will accept something in double or single quotes
164*50348Skarels  *	(to allow embedded spaces).
165*50348Skarels  */
166*50348Skarels char *
167*50348Skarels get_quoted_word(fp)
168*50348Skarels 	register FILE *fp;
169*50348Skarels {
170*50348Skarels 	static char line[256];
171*50348Skarels 	register int ch;
172*50348Skarels 	register char *cp;
173*50348Skarels 
174*50348Skarels 	while ((ch = getc(fp)) != EOF)
175*50348Skarels 		if (ch != ' ' && ch != '\t')
176*50348Skarels 			break;
177*50348Skarels 	if (ch == EOF)
178*50348Skarels 		return ((char *)EOF);
179*50348Skarels 	if (ch == '\n')
180*50348Skarels 		return (NULL);
181*50348Skarels 	cp = line;
182*50348Skarels 	if (ch == '"' || ch == '\'') {
183*50348Skarels 		register int quote = ch;
184*50348Skarels 
185*50348Skarels 		while ((ch = getc(fp)) != EOF) {
186*50348Skarels 			if (ch == quote)
187*50348Skarels 				break;
188*50348Skarels 			if (ch == '\n') {
189*50348Skarels 				*cp = 0;
190*50348Skarels 				printf("config: missing quote reading `%s'\n",
191*50348Skarels 					line);
192*50348Skarels 				exit(2);
193*50348Skarels 			}
194*50348Skarels 			*cp++ = ch;
195*50348Skarels 		}
196*50348Skarels 	} else {
197*50348Skarels 		*cp++ = ch;
198*50348Skarels 		while ((ch = getc(fp)) != EOF) {
199*50348Skarels 			if (isspace(ch))
200*50348Skarels 				break;
201*50348Skarels 			*cp++ = ch;
202*50348Skarels 		}
203*50348Skarels 		if (ch != EOF)
204*50348Skarels 			(void) ungetc(ch, fp);
205*50348Skarels 	}
206*50348Skarels 	*cp = 0;
207*50348Skarels 	if (ch == EOF)
208*50348Skarels 		return ((char *)EOF);
209*50348Skarels 	return (line);
210*50348Skarels }
211*50348Skarels 
212*50348Skarels /*
2138892Sroot  * prepend the path to a filename
2142682Stoy  */
2158900Sroot char *
2162682Stoy path(file)
2178892Sroot 	char *file;
2182682Stoy {
2198892Sroot 	register char *cp;
2202682Stoy 
22145888Sbostic #define	CDIR	"../../compile/"
22246297Sbostic 	cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) +
22347744Sbostic 	    (file ? strlen(file) : 0) + 2));
22445888Sbostic 	(void) strcpy(cp, CDIR);
2258900Sroot 	(void) strcat(cp, PREFIX);
22634109Sbostic 	if (file) {
22734109Sbostic 		(void) strcat(cp, "/");
22834109Sbostic 		(void) strcat(cp, file);
22934109Sbostic 	}
2308892Sroot 	return (cp);
2312682Stoy }
232