main.c revision 4571
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
521553Srgrimesstatic char *PREFIX;
531553Srgrimes
541553Srgrimes/*
551553Srgrimes * Config builds a set of files for building a UNIX
561553Srgrimes * system given a description of the desired system.
571553Srgrimes */
581553Srgrimesmain(argc, argv)
591553Srgrimes	int argc;
601553Srgrimes	char **argv;
611553Srgrimes{
621553Srgrimes
631553Srgrimes	extern char *optarg;
641553Srgrimes	extern int optind;
651553Srgrimes	struct stat buf;
661553Srgrimes	int ch;
671553Srgrimes	char *p;
681553Srgrimes
691553Srgrimes	while ((ch = getopt(argc, argv, "gp")) != EOF)
701553Srgrimes		switch (ch) {
711553Srgrimes		case 'g':
721553Srgrimes			debugging++;
731553Srgrimes			break;
741553Srgrimes		case 'p':
751553Srgrimes			profiling++;
761553Srgrimes			break;
771553Srgrimes		case '?':
781553Srgrimes		default:
791553Srgrimes			goto usage;
801553Srgrimes		}
811553Srgrimes	argc -= optind;
821553Srgrimes	argv += optind;
831553Srgrimes
841553Srgrimes	if (argc != 1) {
851553Srgrimesusage:		fputs("usage: config [-gp] sysname\n", stderr);
861553Srgrimes		exit(1);
871553Srgrimes	}
881553Srgrimes
891553Srgrimes	if (freopen(PREFIX = *argv, "r", stdin) == NULL) {
901553Srgrimes		perror(PREFIX);
911553Srgrimes		exit(2);
921553Srgrimes	}
932567Sjkh#ifdef CONFIG_DONT_CLOBBER
942525Swollman	if (stat(p = path((char *)NULL), &buf)) {
952525Swollman#else /* CONFIG_DONT_CLOBBER */
962483Sjkh	p = path((char *)NULL);
972483Sjkh	if (stat(p, &buf)) {
982525Swollman#endif /* CONFIG_DONT_CLOBBER */
991553Srgrimes		if (mkdir(p, 0777)) {
1001553Srgrimes			perror(p);
1011553Srgrimes			exit(2);
1021553Srgrimes		}
1031553Srgrimes	}
1041553Srgrimes	else if ((buf.st_mode & S_IFMT) != S_IFDIR) {
1051553Srgrimes		fprintf(stderr, "config: %s isn't a directory.\n", p);
1061553Srgrimes		exit(2);
1072567Sjkh#ifndef CONFIG_DONT_CLOBBER
1081553Srgrimes	}
1092483Sjkh	else {
1102483Sjkh		char tmp[strlen(p) + 8];
1111553Srgrimes
1122483Sjkh		fprintf(stderr, "Removing old directory %s:  ", p);
1132483Sjkh		fflush(stderr);
1142483Sjkh		sprintf(tmp, "rm -rf %s", p);
1152483Sjkh		if (system(tmp)) {
1162483Sjkh			fprintf(stderr, "Failed!\n");
1172483Sjkh			perror(tmp);
1182483Sjkh			exit(2);
1192483Sjkh		}
1202483Sjkh		fprintf(stderr, "Done.\n");
1212483Sjkh		if (mkdir(p, 0777)) {
1222483Sjkh			perror(p);
1232483Sjkh			exit(2);
1242483Sjkh		}
1252525Swollman#endif /* CONFIG_DONT_CLOBBER */
1262483Sjkh	}
1272483Sjkh
1281566Srgrimes	loadaddress = -1;
1291553Srgrimes	dtab = NULL;
1301553Srgrimes	confp = &conf_list;
1311553Srgrimes	compp = &comp_list;
1321553Srgrimes	if (yyparse())
1331553Srgrimes		exit(3);
1341553Srgrimes	switch (machine) {
1351553Srgrimes
1361553Srgrimes	case MACHINE_VAX:
1371553Srgrimes		vax_ioconf();		/* Print ioconf.c */
1381553Srgrimes		ubglue();		/* Create ubglue.s */
1391553Srgrimes		break;
1401553Srgrimes
1411553Srgrimes	case MACHINE_TAHOE:
1421553Srgrimes		tahoe_ioconf();
1431553Srgrimes		vbglue();
1441553Srgrimes		break;
1451553Srgrimes
1461553Srgrimes	case MACHINE_HP300:
1471553Srgrimes	case MACHINE_LUNA68K:
1481553Srgrimes		hp300_ioconf();
1491553Srgrimes		hpglue();
1501553Srgrimes		break;
1511553Srgrimes
1521553Srgrimes	case MACHINE_I386:
1531553Srgrimes		i386_ioconf();		/* Print ioconf.c */
1541553Srgrimes		vector();		/* Create vector.s */
1551553Srgrimes		break;
1561553Srgrimes
1571553Srgrimes	case MACHINE_MIPS:
1581553Srgrimes	case MACHINE_PMAX:
1591553Srgrimes		pmax_ioconf();
1601553Srgrimes		break;
1611553Srgrimes
1621553Srgrimes	case MACHINE_NEWS3400:
1631553Srgrimes		news_ioconf();
1641553Srgrimes		break;
1651553Srgrimes
1661553Srgrimes	default:
1671553Srgrimes		printf("Specify machine type, e.g. ``machine vax''\n");
1681553Srgrimes		exit(1);
1691553Srgrimes	}
1701553Srgrimes	/*
1711553Srgrimes	 * make symbolic links in compilation directory
1721553Srgrimes	 * for "sys" (to make genassym.c work along with #include <sys/xxx>)
1731553Srgrimes	 * and similarly for "machine".
1741553Srgrimes	 */
1751553Srgrimes	{
1761553Srgrimes	char xxx[80];
1771553Srgrimes
1781553Srgrimes	(void) sprintf(xxx, "../../%s/include", machinename);
1791553Srgrimes	(void) symlink(xxx, path("machine"));
1801553Srgrimes	}
1811553Srgrimes	makefile();			/* build Makefile */
1821553Srgrimes	headers();			/* make a lot of .h files */
1831553Srgrimes	swapconf();			/* swap config files */
1841553Srgrimes	printf("Don't forget to run \"make depend\"\n");
1851553Srgrimes	exit(0);
1861553Srgrimes}
1871553Srgrimes
1881553Srgrimes/*
1891553Srgrimes * get_word
1901553Srgrimes *	returns EOF on end of file
1911553Srgrimes *	NULL on end of line
1921553Srgrimes *	pointer to the word otherwise
1931553Srgrimes */
1941553Srgrimeschar *
1951553Srgrimesget_word(fp)
1961553Srgrimes	register FILE *fp;
1971553Srgrimes{
1981553Srgrimes	static char line[80];
1991553Srgrimes	register int ch;
2001553Srgrimes	register char *cp;
2014571Sgibbs	int escaped_nl = 0;
2021553Srgrimes
2034571Sgibbsbegin:
2041553Srgrimes	while ((ch = getc(fp)) != EOF)
2051553Srgrimes		if (ch != ' ' && ch != '\t')
2061553Srgrimes			break;
2071553Srgrimes	if (ch == EOF)
2081553Srgrimes		return ((char *)EOF);
2094571Sgibbs	if (ch == '\\'){
2104571Sgibbs		escaped_nl = 1;
2114571Sgibbs		goto begin;
2124571Sgibbs	}
2131553Srgrimes	if (ch == '\n')
2144571Sgibbs		if (escaped_nl){
2154571Sgibbs			escaped_nl = 0;
2164571Sgibbs			goto begin;
2174571Sgibbs		}
2184571Sgibbs		else
2194571Sgibbs			return (NULL);
2201553Srgrimes	cp = line;
2211553Srgrimes	*cp++ = ch;
2221553Srgrimes	while ((ch = getc(fp)) != EOF) {
2231553Srgrimes		if (isspace(ch))
2241553Srgrimes			break;
2251553Srgrimes		*cp++ = ch;
2261553Srgrimes	}
2271553Srgrimes	*cp = 0;
2281553Srgrimes	if (ch == EOF)
2291553Srgrimes		return ((char *)EOF);
2301553Srgrimes	(void) ungetc(ch, fp);
2311553Srgrimes	return (line);
2321553Srgrimes}
2331553Srgrimes
2341553Srgrimes/*
2351553Srgrimes * get_quoted_word
2361553Srgrimes *	like get_word but will accept something in double or single quotes
2371553Srgrimes *	(to allow embedded spaces).
2381553Srgrimes */
2391553Srgrimeschar *
2401553Srgrimesget_quoted_word(fp)
2411553Srgrimes	register FILE *fp;
2421553Srgrimes{
2431553Srgrimes	static char line[256];
2441553Srgrimes	register int ch;
2451553Srgrimes	register char *cp;
2464571Sgibbs	int escaped_nl = 0;
2471553Srgrimes
2484571Sgibbsbegin:
2491553Srgrimes	while ((ch = getc(fp)) != EOF)
2501553Srgrimes		if (ch != ' ' && ch != '\t')
2511553Srgrimes			break;
2521553Srgrimes	if (ch == EOF)
2531553Srgrimes		return ((char *)EOF);
2544571Sgibbs	if (ch == '\\'){
2554571Sgibbs		escaped_nl = 1;
2564571Sgibbs		goto begin;
2574571Sgibbs	}
2581553Srgrimes	if (ch == '\n')
2594571Sgibbs		if (escaped_nl){
2604571Sgibbs			escaped_nl = 0;
2614571Sgibbs			goto begin;
2624571Sgibbs		}
2634571Sgibbs		else
2644571Sgibbs			return (NULL);
2651553Srgrimes	cp = line;
2661553Srgrimes	if (ch == '"' || ch == '\'') {
2671553Srgrimes		register int quote = ch;
2681553Srgrimes
2691553Srgrimes		while ((ch = getc(fp)) != EOF) {
2701553Srgrimes			if (ch == quote)
2711553Srgrimes				break;
2721553Srgrimes			if (ch == '\n') {
2731553Srgrimes				*cp = 0;
2741553Srgrimes				printf("config: missing quote reading `%s'\n",
2751553Srgrimes					line);
2761553Srgrimes				exit(2);
2771553Srgrimes			}
2781553Srgrimes			*cp++ = ch;
2791553Srgrimes		}
2801553Srgrimes	} else {
2811553Srgrimes		*cp++ = ch;
2821553Srgrimes		while ((ch = getc(fp)) != EOF) {
2831553Srgrimes			if (isspace(ch))
2841553Srgrimes				break;
2851553Srgrimes			*cp++ = ch;
2861553Srgrimes		}
2871553Srgrimes		if (ch != EOF)
2881553Srgrimes			(void) ungetc(ch, fp);
2891553Srgrimes	}
2901553Srgrimes	*cp = 0;
2911553Srgrimes	if (ch == EOF)
2921553Srgrimes		return ((char *)EOF);
2931553Srgrimes	return (line);
2941553Srgrimes}
2951553Srgrimes
2961553Srgrimes/*
2971553Srgrimes * prepend the path to a filename
2981553Srgrimes */
2991553Srgrimeschar *
3001553Srgrimespath(file)
3011553Srgrimes	char *file;
3021553Srgrimes{
3031553Srgrimes	register char *cp;
3041553Srgrimes
3051553Srgrimes#define	CDIR	"../../compile/"
3061553Srgrimes	cp = malloc((unsigned int)(sizeof(CDIR) + strlen(PREFIX) +
3071553Srgrimes	    (file ? strlen(file) : 0) + 2));
3081553Srgrimes	(void) strcpy(cp, CDIR);
3091553Srgrimes	(void) strcat(cp, PREFIX);
3101553Srgrimes	if (file) {
3111553Srgrimes		(void) strcat(cp, "/");
3121553Srgrimes		(void) strcat(cp, file);
3131553Srgrimes	}
3141553Srgrimes	return (cp);
3151553Srgrimes}
316