main.c revision 6631
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;
621553Srgrimes
631553Srgrimes/*
641553Srgrimes * Config builds a set of files for building a UNIX
651553Srgrimes * system given a description of the desired system.
661553Srgrimes */
671553Srgrimesmain(argc, argv)
681553Srgrimes	int argc;
691553Srgrimes	char **argv;
701553Srgrimes{
711553Srgrimes
721553Srgrimes	extern char *optarg;
731553Srgrimes	extern int optind;
741553Srgrimes	struct stat buf;
751553Srgrimes	int ch;
761553Srgrimes	char *p;
771553Srgrimes
786631Sjkh	while ((ch = getopt(argc, argv, "gpn")) != EOF)
791553Srgrimes		switch (ch) {
801553Srgrimes		case 'g':
811553Srgrimes			debugging++;
821553Srgrimes			break;
831553Srgrimes		case 'p':
841553Srgrimes			profiling++;
851553Srgrimes			break;
866631Sjkh		case 'n':
876631Sjkh			no_config_clobber = TRUE;
886631Sjkh			break;
891553Srgrimes		case '?':
901553Srgrimes		default:
911553Srgrimes			goto usage;
921553Srgrimes		}
931553Srgrimes	argc -= optind;
941553Srgrimes	argv += optind;
951553Srgrimes
961553Srgrimes	if (argc != 1) {
976631Sjkhusage:		fputs("usage: config [-gpn] sysname\n", stderr);
981553Srgrimes		exit(1);
991553Srgrimes	}
1001553Srgrimes
1011553Srgrimes	if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
1021553Srgrimes		perror(PREFIX);
1031553Srgrimes		exit(2);
1041553Srgrimes	}
1056631Sjkh	if (getenv("NO_CONFIG_CLOBBER"))
1066631Sjkh		no_config_clobber = TRUE;
1076631Sjkh
1082483Sjkh	p = path((char *)NULL);
1092483Sjkh	if (stat(p, &buf)) {
1101553Srgrimes		if (mkdir(p, 0777)) {
1111553Srgrimes			perror(p);
1121553Srgrimes			exit(2);
1131553Srgrimes		}
1141553Srgrimes	}
1151553Srgrimes	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
1161553Srgrimes		fprintf(stderr, "config: %s isn't a directory.\n", p);
1171553Srgrimes		exit(2);
1181553Srgrimes	}
1196631Sjkh	else if (!no_config_clobber) {
1202483Sjkh		char tmp[strlen(p) + 8];
1211553Srgrimes
1222483Sjkh		fprintf(stderr, "Removing old directory %s:  ", p);
1232483Sjkh		fflush(stderr);
1242483Sjkh		sprintf(tmp, "rm -rf %s", p);
1252483Sjkh		if (system(tmp)) {
1262483Sjkh			fprintf(stderr, "Failed!\n");
1272483Sjkh			perror(tmp);
1282483Sjkh			exit(2);
1292483Sjkh		}
1302483Sjkh		fprintf(stderr, "Done.\n");
1312483Sjkh		if (mkdir(p, 0777)) {
1322483Sjkh			perror(p);
1332483Sjkh			exit(2);
1342483Sjkh		}
1352483Sjkh	}
1361566Srgrimes	loadaddress = -1;
1371553Srgrimes	dtab = NULL;
1381553Srgrimes	confp = &conf_list;
1391553Srgrimes	compp = &comp_list;
1401553Srgrimes	if (yyparse())
1411553Srgrimes		exit(3);
1421553Srgrimes	switch (machine) {
1431553Srgrimes
1441553Srgrimes	case MACHINE_VAX:
1451553Srgrimes		vax_ioconf();		/* Print ioconf.c */
1461553Srgrimes		ubglue();		/* Create ubglue.s */
1471553Srgrimes		break;
1481553Srgrimes
1491553Srgrimes	case MACHINE_TAHOE:
1501553Srgrimes		tahoe_ioconf();
1511553Srgrimes		vbglue();
1521553Srgrimes		break;
1531553Srgrimes
1541553Srgrimes	case MACHINE_HP300:
1551553Srgrimes	case MACHINE_LUNA68K:
1561553Srgrimes		hp300_ioconf();
1571553Srgrimes		hpglue();
1581553Srgrimes		break;
1591553Srgrimes
1601553Srgrimes	case MACHINE_I386:
1611553Srgrimes		i386_ioconf();		/* Print ioconf.c */
1621553Srgrimes		vector();		/* Create vector.s */
1631553Srgrimes		break;
1641553Srgrimes
1651553Srgrimes	case MACHINE_MIPS:
1661553Srgrimes	case MACHINE_PMAX:
1671553Srgrimes		pmax_ioconf();
1681553Srgrimes		break;
1691553Srgrimes
1701553Srgrimes	case MACHINE_NEWS3400:
1711553Srgrimes		news_ioconf();
1721553Srgrimes		break;
1731553Srgrimes
1741553Srgrimes	default:
1751553Srgrimes		printf("Specify machine type, e.g. ``machine vax''\n");
1761553Srgrimes		exit(1);
1771553Srgrimes	}
1781553Srgrimes	/*
1791553Srgrimes	 * make symbolic links in compilation directory
1801553Srgrimes	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
1811553Srgrimes	 * and similarly for "machine".
1821553Srgrimes	 */
1831553Srgrimes	{
1841553Srgrimes	char xxx[80];
1851553Srgrimes
1861553Srgrimes	(void) sprintf(xxx, "../../%s/include", machinename);
1871553Srgrimes	(void) symlink(xxx, path("machine"));
1881553Srgrimes	}
1891553Srgrimes	makefile();			/* build Makefile */
1901553Srgrimes	headers();			/* make a lot of .h files */
1911553Srgrimes	swapconf();			/* swap config files */
1921553Srgrimes	printf("Don't forget to run \"make depend\"\n");
1931553Srgrimes	exit(0);
1941553Srgrimes}
1951553Srgrimes
1961553Srgrimes/*
1971553Srgrimes * get_word
1981553Srgrimes *	returns EOF on end of file
1991553Srgrimes *	NULL on end of line
2001553Srgrimes *	pointer to the word otherwise
2011553Srgrimes */
2021553Srgrimeschar *
2031553Srgrimesget_word(fp)
2041553Srgrimes	register FILE *fp;
2051553Srgrimes{
2061553Srgrimes	static char line[80];
2071553Srgrimes	register int ch;
2081553Srgrimes	register char *cp;
2094571Sgibbs	int escaped_nl = 0;
2101553Srgrimes
2114571Sgibbsbegin:
2121553Srgrimes	while ((ch = getc(fp)) != EOF)
2131553Srgrimes		if (ch != ' ' && ch != '\t')
2141553Srgrimes			break;
2151553Srgrimes	if (ch == EOF)
2161553Srgrimes		return ((char *)EOF);
2174571Sgibbs	if (ch == '\\'){
2184571Sgibbs		escaped_nl = 1;
2194571Sgibbs		goto begin;
2204571Sgibbs	}
2211553Srgrimes	if (ch == '\n')
2224571Sgibbs		if (escaped_nl){
2234571Sgibbs			escaped_nl = 0;
2244571Sgibbs			goto begin;
2254571Sgibbs		}
2264571Sgibbs		else
2274571Sgibbs			return (NULL);
2281553Srgrimes	cp = line;
2291553Srgrimes	*cp++ = ch;
2301553Srgrimes	while ((ch = getc(fp)) != EOF) {
2311553Srgrimes		if (isspace(ch))
2321553Srgrimes			break;
2331553Srgrimes		*cp++ = ch;
2341553Srgrimes	}
2351553Srgrimes	*cp = 0;
2361553Srgrimes	if (ch == EOF)
2371553Srgrimes		return ((char *)EOF);
2381553Srgrimes	(void) ungetc(ch, fp);
2391553Srgrimes	return (line);
2401553Srgrimes}
2411553Srgrimes
2421553Srgrimes/*
2431553Srgrimes * get_quoted_word
2441553Srgrimes *	like get_word but will accept something in double or single quotes
2451553Srgrimes *	(to allow embedded spaces).
2461553Srgrimes */
2471553Srgrimeschar *
2481553Srgrimesget_quoted_word(fp)
2491553Srgrimes	register FILE *fp;
2501553Srgrimes{
2511553Srgrimes	static char line[256];
2521553Srgrimes	register int ch;
2531553Srgrimes	register char *cp;
2544571Sgibbs	int escaped_nl = 0;
2551553Srgrimes
2564571Sgibbsbegin:
2571553Srgrimes	while ((ch = getc(fp)) != EOF)
2581553Srgrimes		if (ch != ' ' && ch != '\t')
2591553Srgrimes			break;
2601553Srgrimes	if (ch == EOF)
2611553Srgrimes		return ((char *)EOF);
2624571Sgibbs	if (ch == '\\'){
2634571Sgibbs		escaped_nl = 1;
2644571Sgibbs		goto begin;
2654571Sgibbs	}
2661553Srgrimes	if (ch == '\n')
2674571Sgibbs		if (escaped_nl){
2684571Sgibbs			escaped_nl = 0;
2694571Sgibbs			goto begin;
2704571Sgibbs		}
2714571Sgibbs		else
2724571Sgibbs			return (NULL);
2731553Srgrimes	cp = line;
2741553Srgrimes	if (ch == '"' || ch == '\'') {
2751553Srgrimes		register int quote = ch;
2761553Srgrimes
2771553Srgrimes		while ((ch = getc(fp)) != EOF) {
2781553Srgrimes			if (ch == quote)
2791553Srgrimes				break;
2801553Srgrimes			if (ch == '\n') {
2811553Srgrimes				*cp = 0;
2821553Srgrimes				printf("config: missing quote reading `%s'\n",
2831553Srgrimes					line);
2841553Srgrimes				exit(2);
2851553Srgrimes			}
2861553Srgrimes			*cp++ = ch;
2871553Srgrimes		}
2881553Srgrimes	} else {
2891553Srgrimes		*cp++ = ch;
2901553Srgrimes		while ((ch = getc(fp)) != EOF) {
2911553Srgrimes			if (isspace(ch))
2921553Srgrimes				break;
2931553Srgrimes			*cp++ = ch;
2941553Srgrimes		}
2951553Srgrimes		if (ch != EOF)
2961553Srgrimes			(void) ungetc(ch, fp);
2971553Srgrimes	}
2981553Srgrimes	*cp = 0;
2991553Srgrimes	if (ch == EOF)
3001553Srgrimes		return ((char *)EOF);
3011553Srgrimes	return (line);
3021553Srgrimes}
3031553Srgrimes
3041553Srgrimes/*
3051553Srgrimes * prepend the path to a filename
3061553Srgrimes */
3071553Srgrimeschar *
3081553Srgrimespath(file)
3091553Srgrimes	char *file;
3101553Srgrimes{
3111553Srgrimes	register char *cp;
3121553Srgrimes
3131553Srgrimes#define	CDIR	"../../compile/"
3141553Srgrimes	cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) +
3151553Srgrimes	    (file ? strlen(file) : 0) + 2));
3161553Srgrimes	(void) strcpy(cp, CDIR);
3171553Srgrimes	(void) strcat(cp, PREFIX);
3181553Srgrimes	if (file) {
3191553Srgrimes		(void) strcat(cp, "/");
3201553Srgrimes		(void) strcat(cp, file);
3211553Srgrimes	}
3221553Srgrimes	return (cp);
3231553Srgrimes}
324