main.c revision 12772
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1980, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes * Redistribution and use in source and binary forms, with or without
61553Srgrimes * modification, are permitted provided that the following conditions
71553Srgrimes * are met:
81553Srgrimes * 1. Redistributions of source code must retain the above copyright
91553Srgrimes *    notice, this list of conditions and the following disclaimer.
101553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111553Srgrimes *    notice, this list of conditions and the following disclaimer in the
121553Srgrimes *    documentation and/or other materials provided with the distribution.
131553Srgrimes * 3. All advertising materials mentioning features or use of this software
141553Srgrimes *    must display the following acknowledgement:
151553Srgrimes *	This product includes software developed by the University of
161553Srgrimes *	California, Berkeley and its contributors.
171553Srgrimes * 4. Neither the name of the University nor the names of its contributors
181553Srgrimes *    may be used to endorse or promote products derived from this software
191553Srgrimes *    without specific prior written permission.
201553Srgrimes *
211553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311553Srgrimes * SUCH DAMAGE.
321553Srgrimes */
331553Srgrimes
341553Srgrimes#ifndef lint
351553Srgrimesstatic char copyright[] =
361553Srgrimes"@(#) Copyright (c) 1980, 1993\n\
371553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381553Srgrimes#endif /* not lint */
391553Srgrimes
401553Srgrimes#ifndef lint
411553Srgrimesstatic char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
421553Srgrimes#endif /* not lint */
431553Srgrimes
441553Srgrimes#include <sys/types.h>
451553Srgrimes#include <sys/stat.h>
461553Srgrimes#include <sys/file.h>
471553Srgrimes#include <stdio.h>
481553Srgrimes#include <ctype.h>
491553Srgrimes#include "y.tab.h"
501553Srgrimes#include "config.h"
511553Srgrimes
526631Sjkh#ifndef TRUE
536631Sjkh#define TRUE	(1)
546631Sjkh#endif
556631Sjkh
566631Sjkh#ifndef FALSE
576631Sjkh#define FALSE	(0)
586631Sjkh#endif
596631Sjkh
601553Srgrimesstatic char *PREFIX;
616631Sjkhstatic int no_config_clobber = FALSE;
6212772Speterint old_config_present;
631553Srgrimes
641553Srgrimes/*
651553Srgrimes * Config builds a set of files for building a UNIX
661553Srgrimes * system given a description of the desired system.
671553Srgrimes */
681553Srgrimesmain(argc, argv)
691553Srgrimes	int argc;
701553Srgrimes	char **argv;
711553Srgrimes{
721553Srgrimes
731553Srgrimes	extern char *optarg;
741553Srgrimes	extern int optind;
751553Srgrimes	struct stat buf;
761553Srgrimes	int ch;
771553Srgrimes	char *p;
781553Srgrimes
796631Sjkh	while ((ch = getopt(argc, argv, "gpn")) != EOF)
801553Srgrimes		switch (ch) {
811553Srgrimes		case 'g':
821553Srgrimes			debugging++;
831553Srgrimes			break;
841553Srgrimes		case 'p':
851553Srgrimes			profiling++;
861553Srgrimes			break;
876631Sjkh		case 'n':
886631Sjkh			no_config_clobber = TRUE;
896631Sjkh			break;
901553Srgrimes		case '?':
911553Srgrimes		default:
921553Srgrimes			goto usage;
931553Srgrimes		}
941553Srgrimes	argc -= optind;
951553Srgrimes	argv += optind;
961553Srgrimes
971553Srgrimes	if (argc != 1) {
986631Sjkhusage:		fputs("usage: config [-gpn] sysname\n", stderr);
991553Srgrimes		exit(1);
1001553Srgrimes	}
1011553Srgrimes
1021553Srgrimes	if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
1031553Srgrimes		perror(PREFIX);
1041553Srgrimes		exit(2);
1051553Srgrimes	}
1066631Sjkh	if (getenv("NO_CONFIG_CLOBBER"))
1076631Sjkh		no_config_clobber = TRUE;
1086631Sjkh
1092483Sjkh	p = path((char *)NULL);
1102483Sjkh	if (stat(p, &buf)) {
1111553Srgrimes		if (mkdir(p, 0777)) {
1121553Srgrimes			perror(p);
1131553Srgrimes			exit(2);
1141553Srgrimes		}
1151553Srgrimes	}
1161553Srgrimes	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
1171553Srgrimes		fprintf(stderr, "config: %s isn't a directory.\n", p);
1181553Srgrimes		exit(2);
1191553Srgrimes	}
12012508Swollman#ifndef NO_CLOBBER_EVER
1216631Sjkh	else if (!no_config_clobber) {
1222483Sjkh		char tmp[strlen(p) + 8];
1231553Srgrimes
1242483Sjkh		fprintf(stderr, "Removing old directory %s:  ", p);
1252483Sjkh		fflush(stderr);
1262483Sjkh		sprintf(tmp, "rm -rf %s", p);
1272483Sjkh		if (system(tmp)) {
1282483Sjkh			fprintf(stderr, "Failed!\n");
1292483Sjkh			perror(tmp);
1302483Sjkh			exit(2);
1312483Sjkh		}
1322483Sjkh		fprintf(stderr, "Done.\n");
1332483Sjkh		if (mkdir(p, 0777)) {
1342483Sjkh			perror(p);
1352483Sjkh			exit(2);
1362483Sjkh		}
1372483Sjkh	}
13812508Swollman#endif
13912772Speter	else
14012772Speter		old_config_present++;
14112772Speter
1421566Srgrimes	loadaddress = -1;
1431553Srgrimes	dtab = NULL;
1441553Srgrimes	confp = &conf_list;
1451553Srgrimes	compp = &comp_list;
1461553Srgrimes	if (yyparse())
1471553Srgrimes		exit(3);
1481553Srgrimes	switch (machine) {
1491553Srgrimes
1501553Srgrimes	case MACHINE_VAX:
1511553Srgrimes		vax_ioconf();		/* Print ioconf.c */
1521553Srgrimes		ubglue();		/* Create ubglue.s */
1531553Srgrimes		break;
1541553Srgrimes
1551553Srgrimes	case MACHINE_TAHOE:
1561553Srgrimes		tahoe_ioconf();
1571553Srgrimes		vbglue();
1581553Srgrimes		break;
1591553Srgrimes
1601553Srgrimes	case MACHINE_HP300:
1611553Srgrimes	case MACHINE_LUNA68K:
1621553Srgrimes		hp300_ioconf();
1631553Srgrimes		hpglue();
1641553Srgrimes		break;
1651553Srgrimes
1661553Srgrimes	case MACHINE_I386:
1671553Srgrimes		i386_ioconf();		/* Print ioconf.c */
1681553Srgrimes		vector();		/* Create vector.s */
1691553Srgrimes		break;
1701553Srgrimes
1711553Srgrimes	case MACHINE_MIPS:
1721553Srgrimes	case MACHINE_PMAX:
1731553Srgrimes		pmax_ioconf();
1741553Srgrimes		break;
1751553Srgrimes
1761553Srgrimes	case MACHINE_NEWS3400:
1771553Srgrimes		news_ioconf();
1781553Srgrimes		break;
1791553Srgrimes
1801553Srgrimes	default:
1811553Srgrimes		printf("Specify machine type, e.g. ``machine vax''\n");
1821553Srgrimes		exit(1);
1831553Srgrimes	}
1841553Srgrimes	/*
1851553Srgrimes	 * make symbolic links in compilation directory
1861553Srgrimes	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
1871553Srgrimes	 * and similarly for "machine".
1881553Srgrimes	 */
1891553Srgrimes	{
1901553Srgrimes	char xxx[80];
1911553Srgrimes
1921553Srgrimes	(void) sprintf(xxx, "../../%s/include", machinename);
1931553Srgrimes	(void) symlink(xxx, path("machine"));
1941553Srgrimes	}
19512772Speter	options();			/* make options .h files */
1961553Srgrimes	makefile();			/* build Makefile */
1971553Srgrimes	headers();			/* make a lot of .h files */
1981553Srgrimes	swapconf();			/* swap config files */
1998248Sjkh	printf("Kernel build directory is %s\n", p);
2001553Srgrimes	exit(0);
2011553Srgrimes}
2021553Srgrimes
2031553Srgrimes/*
2041553Srgrimes * get_word
2051553Srgrimes *	returns EOF on end of file
2061553Srgrimes *	NULL on end of line
2071553Srgrimes *	pointer to the word otherwise
2081553Srgrimes */
2091553Srgrimeschar *
2101553Srgrimesget_word(fp)
2111553Srgrimes	register FILE *fp;
2121553Srgrimes{
2131553Srgrimes	static char line[80];
2141553Srgrimes	register int ch;
2151553Srgrimes	register char *cp;
2164571Sgibbs	int escaped_nl = 0;
2171553Srgrimes
2184571Sgibbsbegin:
2191553Srgrimes	while ((ch = getc(fp)) != EOF)
2201553Srgrimes		if (ch != ' ' && ch != '\t')
2211553Srgrimes			break;
2221553Srgrimes	if (ch == EOF)
2231553Srgrimes		return ((char *)EOF);
2244571Sgibbs	if (ch == '\\'){
2254571Sgibbs		escaped_nl = 1;
2264571Sgibbs		goto begin;
2274571Sgibbs	}
2281553Srgrimes	if (ch == '\n')
2294571Sgibbs		if (escaped_nl){
2304571Sgibbs			escaped_nl = 0;
2314571Sgibbs			goto begin;
2324571Sgibbs		}
2334571Sgibbs		else
2344571Sgibbs			return (NULL);
2351553Srgrimes	cp = line;
2361553Srgrimes	*cp++ = ch;
2371553Srgrimes	while ((ch = getc(fp)) != EOF) {
2381553Srgrimes		if (isspace(ch))
2391553Srgrimes			break;
2401553Srgrimes		*cp++ = ch;
2411553Srgrimes	}
2421553Srgrimes	*cp = 0;
2431553Srgrimes	if (ch == EOF)
2441553Srgrimes		return ((char *)EOF);
2451553Srgrimes	(void) ungetc(ch, fp);
2461553Srgrimes	return (line);
2471553Srgrimes}
2481553Srgrimes
2491553Srgrimes/*
2501553Srgrimes * get_quoted_word
2511553Srgrimes *	like get_word but will accept something in double or single quotes
2521553Srgrimes *	(to allow embedded spaces).
2531553Srgrimes */
2541553Srgrimeschar *
2551553Srgrimesget_quoted_word(fp)
2561553Srgrimes	register FILE *fp;
2571553Srgrimes{
2581553Srgrimes	static char line[256];
2591553Srgrimes	register int ch;
2601553Srgrimes	register char *cp;
2614571Sgibbs	int escaped_nl = 0;
2621553Srgrimes
2634571Sgibbsbegin:
2641553Srgrimes	while ((ch = getc(fp)) != EOF)
2651553Srgrimes		if (ch != ' ' && ch != '\t')
2661553Srgrimes			break;
2671553Srgrimes	if (ch == EOF)
2681553Srgrimes		return ((char *)EOF);
2694571Sgibbs	if (ch == '\\'){
2704571Sgibbs		escaped_nl = 1;
2714571Sgibbs		goto begin;
2724571Sgibbs	}
2731553Srgrimes	if (ch == '\n')
2744571Sgibbs		if (escaped_nl){
2754571Sgibbs			escaped_nl = 0;
2764571Sgibbs			goto begin;
2774571Sgibbs		}
2784571Sgibbs		else
2794571Sgibbs			return (NULL);
2801553Srgrimes	cp = line;
2811553Srgrimes	if (ch == '"' || ch == '\'') {
2821553Srgrimes		register int quote = ch;
2831553Srgrimes
2841553Srgrimes		while ((ch = getc(fp)) != EOF) {
2851553Srgrimes			if (ch == quote)
2861553Srgrimes				break;
2871553Srgrimes			if (ch == '\n') {
2881553Srgrimes				*cp = 0;
2891553Srgrimes				printf("config: missing quote reading `%s'\n",
2901553Srgrimes					line);
2911553Srgrimes				exit(2);
2921553Srgrimes			}
2931553Srgrimes			*cp++ = ch;
2941553Srgrimes		}
2951553Srgrimes	} else {
2961553Srgrimes		*cp++ = ch;
2971553Srgrimes		while ((ch = getc(fp)) != EOF) {
2981553Srgrimes			if (isspace(ch))
2991553Srgrimes				break;
3001553Srgrimes			*cp++ = ch;
3011553Srgrimes		}
3021553Srgrimes		if (ch != EOF)
3031553Srgrimes			(void) ungetc(ch, fp);
3041553Srgrimes	}
3051553Srgrimes	*cp = 0;
3061553Srgrimes	if (ch == EOF)
3071553Srgrimes		return ((char *)EOF);
3081553Srgrimes	return (line);
3091553Srgrimes}
3101553Srgrimes
3111553Srgrimes/*
3121553Srgrimes * prepend the path to a filename
3131553Srgrimes */
3141553Srgrimeschar *
3151553Srgrimespath(file)
3161553Srgrimes	char *file;
3171553Srgrimes{
3181553Srgrimes	register char *cp;
3191553Srgrimes
3201553Srgrimes#define	CDIR	"../../compile/"
3211553Srgrimes	cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) +
3221553Srgrimes	    (file ? strlen(file) : 0) + 2));
3231553Srgrimes	(void) strcpy(cp, CDIR);
3241553Srgrimes	(void) strcat(cp, PREFIX);
3251553Srgrimes	if (file) {
3261553Srgrimes		(void) strcat(cp, "/");
3271553Srgrimes		(void) strcat(cp, file);
3281553Srgrimes	}
3291553Srgrimes	return (cp);
3301553Srgrimes}
331