xref: /csrg-svn/usr.bin/f77/f77.tahoe/f77.c (revision 47930)
1*47930Sbostic /*-
2*47930Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*47930Sbostic  * All rights reserved.
4*47930Sbostic  *
5*47930Sbostic  * %sccs.include.proprietary.c%
6*47930Sbostic  */
7*47930Sbostic 
8*47930Sbostic #ifndef lint
9*47930Sbostic char copyright[] =
10*47930Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
11*47930Sbostic  All rights reserved.\n";
12*47930Sbostic #endif /* not lint */
13*47930Sbostic 
14*47930Sbostic #ifndef lint
15*47930Sbostic static char sccsid[] = "@(#)f77.c	5.5 (Berkeley) 04/12/91";
16*47930Sbostic #endif /* not lint */
17*47930Sbostic 
1837414Sbostic /*
1937414Sbostic  * f77.c
2037414Sbostic  *
2137414Sbostic  * Driver program for the 4.2 BSD f77 compiler.
2237414Sbostic  *
2337414Sbostic  * University of Utah CS Dept modification history:
2437414Sbostic  *
2537414Sbostic  * $Log:	f77.c,v $
2637414Sbostic  * Revision 1.14  85/03/01  00:07:57  donn
2737414Sbostic  * Portability fix from Ralph Campbell.
2837414Sbostic  *
2937414Sbostic  * Revision 1.13  85/02/12  19:31:47  donn
3037414Sbostic  * Use CATNAME to get the name of a concatenation command instead of
3137414Sbostic  * explicitly running 'cat' -- you can get the wrong 'cat' the old way!
3237414Sbostic  *
3337414Sbostic  * Revision 1.12  85/01/14  06:42:30  donn
3437414Sbostic  * Changed to call the peephole optimizer with the '-f' flag, so that
3537414Sbostic  * floating point moves are translated to integer moves.
3637414Sbostic  *
3737414Sbostic  * Revision 1.11  85/01/14  04:38:59  donn
3837414Sbostic  * Jerry's change to pass -O to f1 so it knows whether the peephole optimizer
3937414Sbostic  * will be run.  This is necessary in order to handle movf/movl translation.
4037414Sbostic  *
4137414Sbostic  * Revision 1.10  85/01/14  03:59:12  donn
4237414Sbostic  * Added Jerry Berkman's fix for the '-q' flag.
4337414Sbostic  *
4437414Sbostic  * Revision 1.9  84/11/09  01:51:26  donn
4537414Sbostic  * Cosmetic change to stupid() suggested by John McCarthy at Memorial
4637414Sbostic  * University, St. Johns.
4737414Sbostic  *
4837414Sbostic  * Revision 1.8  84/09/14  16:02:34  donn
4937414Sbostic  * Added changes to notice when people do 'f77 -c foo.f -o bar.o' and tell
5037414Sbostic  * them why it doesn't do what they think it does.
5137414Sbostic  *
5237414Sbostic  * Revision 1.7  84/08/24  21:08:31  donn
5337414Sbostic  * Added call to setrlimit() to prevent core dumps when not debugging.
5437414Sbostic  * Reorganized the include file arrangment somewhat.
5537414Sbostic  *
5637414Sbostic  * Revision 1.6  84/08/24  20:20:24  donn
5737414Sbostic  * Changed stupidity check on Jerry Berkman's suggestion -- now it balks if
5837414Sbostic  * the load file exists and has a sensitive suffix.
5937414Sbostic  *
6037414Sbostic  * Revision 1.5  84/08/15  18:56:44  donn
6137414Sbostic  * Added test for -O combined with -g, suggested by Raleigh Romine.  To keep
6237414Sbostic  * things simple, if both are specified then the second in the list is thrown
6337414Sbostic  * out and the user is warned.
6437414Sbostic  *
6537414Sbostic  * Revision 1.4  84/08/05  21:33:15  donn
6637414Sbostic  * Added stupidity check -- f77 won't load on a file that it's asked to
6737414Sbostic  * compile as well.
6837414Sbostic  *
6937414Sbostic  * Revision 1.3  84/08/04  22:58:24  donn
7037414Sbostic  * Improved error reporting -- we now explain why we died and what we did.
7137414Sbostic  * Only works on 4.2.  Added at the instigation of Jerry Berkman.
7237414Sbostic  *
7337414Sbostic  * Revision 1.2  84/07/28  13:11:24  donn
7437414Sbostic  * Added Ralph Campbell's changes to reduce offsets to data.
7537414Sbostic  *
7637414Sbostic  */
7737414Sbostic 
7837414Sbostic char *xxxvers[] = "\n@(#) F77 DRIVER, VERSION 4.2,   1984 JULY 28\n";
7937414Sbostic #include <stdio.h>
8037414Sbostic #include <sys/types.h>
8137414Sbostic #include <sys/stat.h>
8237414Sbostic #include <ctype.h>
8337414Sbostic #include <signal.h>
8437414Sbostic 
8537414Sbostic #ifdef	SIGPROF
8637414Sbostic /*
8737414Sbostic  * Some 4.2 BSD capabilities.
8837414Sbostic  */
8937414Sbostic #include <sys/time.h>
9037414Sbostic #include <sys/resource.h>
9137414Sbostic #define	NOCORE		1
9237414Sbostic #include <sys/wait.h>
9337414Sbostic #define PSIGNAL		1
9437414Sbostic #define INLINE		1
9537414Sbostic #endif
9637414Sbostic 
9737414Sbostic #include "defines.h"
9837414Sbostic #include "machdefs.h"
9937792Sbostic #include "pathnames.h"
10037414Sbostic #include "version.h"
10137414Sbostic 
10237414Sbostic static FILEP diagfile	= {stderr} ;
10337414Sbostic static int pid;
10437414Sbostic static int sigivalue	= 0;
10537414Sbostic static int sigqvalue	= 0;
10637414Sbostic static int sighvalue	= 0;
10737414Sbostic static int sigtvalue	= 0;
10837414Sbostic 
10937414Sbostic static char *pass1name	= PASS1NAME ;
11037414Sbostic static char *pass2name	= PASS2NAME ;
11137414Sbostic static char *pass2opt	= PASS2OPT ;
11237414Sbostic static char *asmname	= ASMNAME ;
11337414Sbostic static char *ldname	= LDNAME ;
11437414Sbostic static char *footname	= FOOTNAME;
11537414Sbostic static char *proffoot	= PROFFOOT;
11637414Sbostic static char *macroname	= "m4";
11737792Sbostic static char *shellname	= _PATH_BSHELL;
11837792Sbostic static char *cppname	= _PATH_CPP;
11937414Sbostic static char *aoutname	= "a.out" ;
12037414Sbostic static char *temppref	= TEMPPREF;
12137414Sbostic 
12237414Sbostic static char *infname;
12337414Sbostic static char textfname[44];
12437414Sbostic static char asmfname[44];
12537414Sbostic static char asmpass2[44];
12637414Sbostic static char initfname[44];
12737414Sbostic static char sortfname[44];
12837414Sbostic static char prepfname[44];
12937414Sbostic static char objfdefault[44];
13037414Sbostic static char optzfname[44];
13137414Sbostic static char setfname[44];
13237414Sbostic 
13337414Sbostic static char fflags[50]	= "-";
13437414Sbostic static char f2flags[50];
13537414Sbostic static char cflags[50]	= "-c";
13637414Sbostic #if TARGET == GCOS
13737414Sbostic 	static char eflags[30]	= "system=gcos ";
13837414Sbostic #else
13937414Sbostic 	static char eflags[30]	= "system=unix ";
14037414Sbostic #endif
14137414Sbostic static char rflags[30]	= "";
14237414Sbostic static char lflag[3]	= "-x";
14337414Sbostic static char *fflagp	= fflags+1;
14437414Sbostic static char *f2flagp	= f2flags;
14537414Sbostic static char *cflagp	= cflags+2;
14637414Sbostic static char *eflagp	= eflags+12;
14737414Sbostic static char *rflagp	= rflags;
14837414Sbostic static char *cppflags	= "";
14937414Sbostic static char **cppargs;
15037414Sbostic static char **loadargs;
15137414Sbostic static char **loadp;
15237414Sbostic 
15337414Sbostic static flag erred	= NO;
15437414Sbostic static flag loadflag	= YES;
15537414Sbostic static flag saveasmflag	= NO;
15637414Sbostic static flag profileflag	= NO;
15737414Sbostic static flag optimflag	= NO;
15837414Sbostic static flag debugflag	= NO;
15937414Sbostic static flag verbose	= NO;
16037414Sbostic static flag nofloating	= NO;
16137414Sbostic static flag fortonly	= NO;
16237414Sbostic static flag macroflag	= NO;
16337414Sbostic static flag sdbflag	= NO;
16437414Sbostic static flag namesflag	= YES;
16537414Sbostic 
16637414Sbostic static int ncpp;
16737414Sbostic 
16837414Sbostic 
main(argc,argv)16937414Sbostic main(argc, argv)
17037414Sbostic int argc;
17137414Sbostic char **argv;
17237414Sbostic {
17337414Sbostic int i, c, status;
17437414Sbostic char *setdoto(), *lastchar(), *lastfield(), *copys(), *argvtos();
17537414Sbostic ptr ckalloc();
17637414Sbostic register char *s;
17737414Sbostic char fortfile[20], *t;
17837414Sbostic char buff[100];
17937414Sbostic int intrupt();
18037414Sbostic int new_aoutname = NO;
18137414Sbostic 
18237414Sbostic sigivalue = signal(SIGINT, SIG_IGN) == SIG_IGN;
18337414Sbostic sigqvalue = signal(SIGQUIT,SIG_IGN) == SIG_IGN;
18437414Sbostic sighvalue = signal(SIGHUP, SIG_IGN) == SIG_IGN;
18537414Sbostic sigtvalue = signal(SIGTERM,SIG_IGN) == SIG_IGN;
18637414Sbostic enbint(intrupt);
18737414Sbostic 
18837414Sbostic pid = getpid();
18937414Sbostic crfnames();
19037414Sbostic 
19137414Sbostic cppargs  = (char **) ckalloc( argc * sizeof(*cppargs) );
19237414Sbostic loadargs = (char **) ckalloc( (argc+20) * sizeof(*loadargs) );
19337414Sbostic loadargs[1] = "-X";
19437414Sbostic loadargs[2] = "-u";
19537414Sbostic #if HERE==PDP11 || HERE==VAX || HERE==TAHOE
19637414Sbostic 	loadargs[3] = "_MAIN_";
19737414Sbostic #endif
19837414Sbostic #if HERE == INTERDATA
19937414Sbostic 	loadargs[3] = "main";
20037414Sbostic #endif
20137414Sbostic loadp = loadargs + 4;
20237414Sbostic 
20337414Sbostic --argc;
20437414Sbostic ++argv;
20537414Sbostic 
20637414Sbostic while(argc>0 && argv[0][0]=='-' && argv[0][1]!='\0')
20737414Sbostic 	{
20837414Sbostic 	for(s = argv[0]+1 ; *s ; ++s) switch(*s)
20937414Sbostic 		{
21037414Sbostic 		case 'T':  /* use special passes */
21137414Sbostic 			switch(*++s)
21237414Sbostic 				{
21337414Sbostic 				case '1':
21437414Sbostic 					pass1name = s+1; goto endfor;
21537414Sbostic 				case '2':
21637414Sbostic 					pass2name = s+1; goto endfor;
21737414Sbostic 				case 'p':
21837414Sbostic 					pass2opt = s+1; goto endfor;
21937414Sbostic 				case 'a':
22037414Sbostic 					asmname = s+1; goto endfor;
22137414Sbostic 				case 'l':
22237414Sbostic 					ldname = s+1; goto endfor;
22337414Sbostic 				case 'F':
22437414Sbostic 					footname = s+1; goto endfor;
22537414Sbostic 				case 'm':
22637414Sbostic 					macroname = s+1; goto endfor;
22737414Sbostic 				case 't':
22837414Sbostic 					temppref = s+1; goto endfor;
22937414Sbostic 				default:
23037414Sbostic 					fatali("bad option -T%c", *s);
23137414Sbostic 				}
23237414Sbostic 			break;
23337414Sbostic 
23437414Sbostic 		case '6':
23537414Sbostic 			if(s[1]=='6')
23637414Sbostic 				{
23737414Sbostic 				*fflagp++ = *s++;
23837414Sbostic 				goto copyfflag;
23937414Sbostic 				}
24037414Sbostic 			else	{
24137414Sbostic 				fprintf(diagfile, "invalid flag 6%c\n", s[1]);
24237414Sbostic 				done(1);
24337414Sbostic 				}
24437414Sbostic 
24537414Sbostic 		case 'w':
24637414Sbostic 			if(s[1]=='6' && s[2]=='6')
24737414Sbostic 				{
24837414Sbostic 				*fflagp++ = *s++;
24937414Sbostic 				*fflagp++ = *s++;
25037414Sbostic 				}
25137414Sbostic 
25237414Sbostic 		copyfflag:
25337414Sbostic 		case 'u':
25437414Sbostic 		case 'U':
25537414Sbostic 		case '1':
25637414Sbostic 		case 'C':
25737414Sbostic 			*fflagp++ = *s;
25837414Sbostic 			break;
25937414Sbostic 
26037414Sbostic 		case 'O':
26137414Sbostic 			if(sdbflag)
26237414Sbostic 				{
26337414Sbostic 				fprintf(diagfile, "-O and -g are incompatible; -O ignored\n");
26437414Sbostic 				break;
26537414Sbostic 				}
26637414Sbostic 			optimflag = YES;
26737414Sbostic #if TARGET == INTERDATA
26837414Sbostic 				*loadp++ = "-r";
26937414Sbostic 				*loadp++ = "-d";
27037414Sbostic #endif
27137414Sbostic 			*fflagp++ = 'O';
27237414Sbostic 			break;
27337414Sbostic 
27437414Sbostic 		case 'N':
27537414Sbostic 			*fflagp++ = 'N';
27637414Sbostic 			if( oneof(*++s, "qxscn") )
27737414Sbostic 				*fflagp++ = *s++;
27837414Sbostic 			else	{
27937414Sbostic 				fprintf(diagfile, "invalid flag -N%c\n", *s);
28037414Sbostic 				done(1);
28137414Sbostic 				}
28237414Sbostic 			while( isdigit(*s) )
28337414Sbostic 				*fflagp++ = *s++;
28437414Sbostic 			*fflagp++ = 'X';
28537414Sbostic 			goto endfor;
28637414Sbostic 
28737414Sbostic 		case 'm':
28837414Sbostic 			if(s[1] == '4')
28937414Sbostic 				++s;
29037414Sbostic 			macroflag = YES;
29137414Sbostic 			break;
29237414Sbostic 
29337414Sbostic 		case 'S':
29437414Sbostic 			strcat(cflags, " -S");
29537414Sbostic 			saveasmflag = YES;
29637414Sbostic 
29737414Sbostic 		case 'c':
29837414Sbostic 			if( new_aoutname == YES ){
29937414Sbostic 				fprintf(diagfile, "-c prevents loading, -o %s ignored\n", aoutname);
30037414Sbostic 				new_aoutname = NO;
30137414Sbostic 				}
30237414Sbostic 			loadflag = NO;
30337414Sbostic 			break;
30437414Sbostic 
30537414Sbostic 		case 'v':
30637414Sbostic 			verbose = YES;
30737414Sbostic 			fprintf(diagfile,"\nBerkeley F77, version %s\n",
30837414Sbostic 				VERSIONNUMBER);
30937414Sbostic 			break;
31037414Sbostic 
31137414Sbostic 		case 'd':
31237414Sbostic 			debugflag = YES;
31337414Sbostic 			*fflagp++ = 'd';
31437414Sbostic 			s++;
31537414Sbostic 			while( isdigit(*s) || *s == ',' )
31637414Sbostic 				*fflagp++ = *s++;
31737414Sbostic 			*fflagp++ = 'X';
31837414Sbostic 			goto endfor;
31937414Sbostic 
32037414Sbostic 		case 'M':
32137414Sbostic 			*loadp++ = "-M";
32237414Sbostic 			break;
32337414Sbostic 
32437414Sbostic 		case 'g':
32537414Sbostic 			if(optimflag)
32637414Sbostic 				{
32737414Sbostic 				fprintf(diagfile, "-g and -O are incompatible; -g ignored\n");
32837414Sbostic 				break;
32937414Sbostic 				}
33037414Sbostic 			strcat(cflags," -g");
33137414Sbostic 			sdbflag = YES;
33237414Sbostic 			goto copyfflag;
33337414Sbostic 
33437414Sbostic 		case 'p':
33537414Sbostic 			profileflag = YES;
33637414Sbostic 			strcat(cflags," -p");
33737414Sbostic 			*fflagp++ = 'p';
33837414Sbostic 			if(s[1] == 'g')
33937414Sbostic 				{
34037414Sbostic 				proffoot = GPRFFOOT;
34137414Sbostic 				s++;
34237414Sbostic 				}
34337414Sbostic 			break;
34437414Sbostic 
34537414Sbostic 		case 'q':
34637414Sbostic 			namesflag = NO;
34737414Sbostic 			*fflagp++ = *s;
34837414Sbostic 			break;
34937414Sbostic 
35037414Sbostic 		case 'o':
35137414Sbostic 			if( ! strcmp(s, "onetrip") )
35237414Sbostic 				{
35337414Sbostic 				*fflagp++ = '1';
35437414Sbostic 				goto endfor;
35537414Sbostic 				}
35637414Sbostic 			new_aoutname = YES;
35737414Sbostic 			aoutname = *++argv;
35837414Sbostic 			--argc;
35937414Sbostic 			if( loadflag == NO ){
36037414Sbostic 				fprintf(diagfile, "-c prevents loading, -o %s ignored\n", aoutname);
36137414Sbostic 				new_aoutname = NO;
36237414Sbostic 				}
36337414Sbostic 			break;
36437414Sbostic 
36537414Sbostic #if TARGET == PDP11
36637414Sbostic 		case 'f':
36737414Sbostic 			nofloating = YES;
36837414Sbostic 			pass2name = NOFLPASS2;
36937414Sbostic 		break;
37037414Sbostic #endif
37137414Sbostic 
37237414Sbostic 		case 'F':
37337414Sbostic 			fortonly = YES;
37437414Sbostic 			loadflag = NO;
37537414Sbostic 			break;
37637414Sbostic 		case 'D':
37737414Sbostic 		case 'I':
37837414Sbostic 			cppargs[ncpp++] = *argv;
37937414Sbostic 			goto endfor;
38037414Sbostic 
38137414Sbostic 		case 'i':
38237414Sbostic 			if((s[1]=='2' || s[1]=='4') && s[2] == '\0')
38337414Sbostic 				{
38437414Sbostic 				*fflagp++ = *s++;
38537414Sbostic 				goto copyfflag;
38637414Sbostic 				}
38737414Sbostic #ifdef INLINE
38837414Sbostic 			*f2flagp++ = '-';
38937414Sbostic 			while(*f2flagp++ = *s++)
39037414Sbostic 				;
39137414Sbostic 			*f2flagp = ' ';
39237414Sbostic 			if(strcmp(pass2name, PASS2NAME) == 0)
39337414Sbostic 				pass2name = PASS2INAME;
39437414Sbostic 			goto endfor;
39537414Sbostic #else
39637414Sbostic 			fprintf(diagfile, "invalid flag -i%c\n", s[1]);
39737414Sbostic 			done(1);
39837414Sbostic #endif
39937414Sbostic 
40037414Sbostic 		case 'l':	/* letter ell--library */
40137414Sbostic 			s[-1] = '-';
40237414Sbostic 			*loadp++ = s-1;
40337414Sbostic 			goto endfor;
40437414Sbostic 
40537414Sbostic 		case 'E':	/* EFL flag argument */
40637414Sbostic 			while( *eflagp++ = *++s)
40737414Sbostic 				;
40837414Sbostic 			*eflagp++ = ' ';
40937414Sbostic 			goto endfor;
41037414Sbostic 		case 'R':
41137414Sbostic 			while( *rflagp++ = *++s )
41237414Sbostic 				;
41337414Sbostic 			*rflagp++ = ' ';
41437414Sbostic 			goto endfor;
41537414Sbostic 		default:
41637414Sbostic 			lflag[1] = *s;
41737414Sbostic 			*loadp++ = copys(lflag);
41837414Sbostic 			break;
41937414Sbostic 		}
42037414Sbostic endfor:
42137414Sbostic 	--argc;
42237414Sbostic 	++argv;
42337414Sbostic 	}
42437414Sbostic 
42537414Sbostic #ifdef	NOCORE
42637414Sbostic if(!debugflag)
42737414Sbostic 	{
42837414Sbostic 	struct rlimit r;
42937414Sbostic 
43037414Sbostic 	r.rlim_cur = r.rlim_max = 0;
43137414Sbostic 	setrlimit(RLIMIT_CORE, &r);
43237414Sbostic 	}
43337414Sbostic #endif	NOCORE
43437414Sbostic 
43537414Sbostic *fflagp = '\0';
43637414Sbostic 
43737414Sbostic if (ncpp > 0)
43837414Sbostic 	cppflags = argvtos (ncpp,cppargs);
43937414Sbostic 
44037414Sbostic loadargs[0] = ldname;
44137414Sbostic #if TARGET == PDP11
44237414Sbostic 	if(nofloating)
44337414Sbostic 		*loadp++ = (profileflag ? NOFLPROF : NOFLFOOT);
44437414Sbostic 	else
44537414Sbostic #endif
44637414Sbostic *loadp++ = (profileflag ? proffoot : footname);
44737414Sbostic 
44837414Sbostic for(i = 0 ; i<argc ; ++i)
44937414Sbostic 	switch(c =  dotchar(infname = argv[i]) )
45037414Sbostic 		{
45137414Sbostic 		case 'r':	/* Ratfor file */
45237414Sbostic 		case 'e':	/* EFL file */
45337414Sbostic 			if( unreadable(argv[i]) )
45437414Sbostic 				{
45537414Sbostic 				erred = YES;
45637414Sbostic 				break;
45737414Sbostic 				}
45837414Sbostic 			s = fortfile;
45937414Sbostic 			t = lastfield(argv[i]);
46037414Sbostic 			while( *s++ = *t++)
46137414Sbostic 				;
46237414Sbostic 			s[-2] = 'f';
46337414Sbostic 
46437414Sbostic 			if(macroflag)
46537414Sbostic 				{
46637414Sbostic 				sprintf(buff, "%s %s >%s", macroname, infname, prepfname);
46737414Sbostic 				if( sys(buff) )
46837414Sbostic 					{
46937414Sbostic 					rmf(prepfname);
47037414Sbostic 					erred = YES;
47137414Sbostic 					break;
47237414Sbostic 					}
47337414Sbostic 				infname = prepfname;
47437414Sbostic 				}
47537414Sbostic 
47637414Sbostic 			if(c == 'e')
47737414Sbostic 				sprintf(buff, "efl %s %s >%s", eflags, infname, fortfile);
47837414Sbostic 			else
47937414Sbostic 				sprintf(buff, "ratfor %s %s >%s", rflags, infname, fortfile);
48037414Sbostic 			status = sys(buff);
48137414Sbostic 			if(macroflag)
48237414Sbostic 				rmf(infname);
48337414Sbostic 			if(status)
48437414Sbostic 				{
48537414Sbostic 				erred = YES;
48637414Sbostic 				rmf(fortfile);
48737414Sbostic 				break;
48837414Sbostic 				}
48937414Sbostic 
49037414Sbostic 			if( ! fortonly )
49137414Sbostic 				{
49237414Sbostic 				infname = argv[i] = lastfield(argv[i]);
49337414Sbostic 				*lastchar(infname) = 'f';
49437414Sbostic 
49537414Sbostic 				if( dofort(argv[i]) )
49637414Sbostic 					erred = YES;
49737414Sbostic 				else	{
49837414Sbostic 					if( nodup(t = setdoto(argv[i])) )
49937414Sbostic 						*loadp++ = t;
50037414Sbostic 					rmf(fortfile);
50137414Sbostic 					}
50237414Sbostic 				}
50337414Sbostic 			break;
50437414Sbostic 
50537414Sbostic 		case 'F':	/* C preprocessor -> Fortran file */
50637414Sbostic 			if( unreadable(argv[i]) )
50737414Sbostic 				{
50837414Sbostic 				erred = YES;
50937414Sbostic 				break;
51037414Sbostic 				}
51137414Sbostic 			s = fortfile;
51237414Sbostic 			t = lastfield(argv[i]);
51337414Sbostic 			while( *s++ = *t++)
51437414Sbostic 				;
51537414Sbostic 			s[-2] = 'f';
51637414Sbostic 			sprintf(buff,"%s %s %s >%s", cppname, cppflags, infname, fortfile);
51737414Sbostic 			status = sys(buff);
51837414Sbostic 			if(status)
51937414Sbostic 				{
52037414Sbostic 				erred = YES;
52137414Sbostic 				rmf(fortfile);
52237414Sbostic 				break;
52337414Sbostic 				}
52437414Sbostic 
52537414Sbostic 			if( ! fortonly )
52637414Sbostic 				{
52737414Sbostic 				infname = argv[i] = lastfield(argv[i]);
52837414Sbostic 				*lastchar(infname) = 'f';
52937414Sbostic 
53037414Sbostic 				if ( dofort(argv[i]) )
53137414Sbostic 					erred = YES;
53237414Sbostic 				else	{
53337414Sbostic 					if (nodup(t = setdoto(argv[i])) )
53437414Sbostic 						*loadp++ = t;
53537414Sbostic 						rmf(fortfile);
53637414Sbostic 						}
53737414Sbostic 				}
53837414Sbostic 			break;
53937414Sbostic 
54037414Sbostic 		case 'f':	/* Fortran file */
54137414Sbostic 			if( unreadable(argv[i]) )
54237414Sbostic 				erred = YES;
54337414Sbostic 			else if( dofort(argv[i]) )
54437414Sbostic 				erred = YES;
54537414Sbostic 			else if( nodup(t=setdoto(argv[i])) )
54637414Sbostic 				*loadp++ = t;
54737414Sbostic 			break;
54837414Sbostic 
54937414Sbostic 		case 'c':	/* C file */
55037414Sbostic 		case 's':	/* Assembler file */
55137414Sbostic 			if( unreadable(argv[i]) )
55237414Sbostic 				{
55337414Sbostic 				erred = YES;
55437414Sbostic 				break;
55537414Sbostic 				}
55637414Sbostic #if HERE==PDP11 || HERE==VAX || HERE==TAHOE
55737414Sbostic 			if( namesflag == YES )
55837414Sbostic 				fprintf(diagfile, "%s:\n", argv[i]);
55937414Sbostic #endif
56037414Sbostic 			sprintf(buff, "cc %s %s", cflags, argv[i] );
56137414Sbostic 			if( sys(buff) )
56237414Sbostic 				erred = YES;
56337414Sbostic 			else
56437414Sbostic 				if( nodup(t = setdoto(argv[i])) )
56537414Sbostic 					*loadp++ = t;
56637414Sbostic 			break;
56737414Sbostic 
56837414Sbostic 		case 'o':
56937414Sbostic 			if( nodup(argv[i]) )
57037414Sbostic 				*loadp++ = argv[i];
57137414Sbostic 			break;
57237414Sbostic 
57337414Sbostic 		default:
57437414Sbostic 			if( ! strcmp(argv[i], "-o") ) {
57537414Sbostic 				aoutname = argv[++i];
57637414Sbostic 				new_aoutname = YES;
57737414Sbostic 				if( loadflag == NO ){
57837414Sbostic 					fprintf(diagfile, "-c prevents loading, -o %s ignored\n", aoutname);
57937414Sbostic 					new_aoutname = NO;
58037414Sbostic 					}
58137414Sbostic 			} else
58237414Sbostic 				*loadp++ = argv[i];
58337414Sbostic 			break;
58437414Sbostic 		}
58537414Sbostic 
58637414Sbostic if( loadflag && stupid(aoutname) )
58737414Sbostic 	erred = YES;
58837414Sbostic if(loadflag && !erred)
58937414Sbostic 	doload(loadargs, loadp);
59037414Sbostic done(erred);
59137414Sbostic }
59237414Sbostic 
59337414Sbostic 
59437414Sbostic 
59537414Sbostic /*
59637414Sbostic  * argvtos() copies a list of arguments contained in an array of character
59737414Sbostic  * strings to a single dynamically allocated string. Each argument is
59837414Sbostic  * separated by one blank space. Returns a pointer to the string or null
59937414Sbostic  * if out of memory.
60037414Sbostic  */
60137414Sbostic #define SBUFINCR	1024
60237414Sbostic #define SBUFMAX		10240
60337414Sbostic 
60437414Sbostic char *
argvtos(argc,argv)60537414Sbostic argvtos(argc, argv)
60637414Sbostic 	char **argv;
60737414Sbostic 	int  argc;
60837414Sbostic {
60937414Sbostic 	register char *s;		/* string pointer */
61037414Sbostic 	register int  i;		/* string buffer pointer */
61137414Sbostic 	char *malloc();			/* memory allocator */
61237414Sbostic 	char *realloc();		/* increase size of storage */
61337414Sbostic 	char *sbuf;			/* string buffer */
61437414Sbostic 	int nbytes;			/* bytes of memory required */
61537414Sbostic 	int nu;				/* no. of SBUFINCR units required */
61637414Sbostic 	int sbufsize;			/* current size of sbuf */
61737414Sbostic 	int strlen();			/* string length */
61837414Sbostic 
61937414Sbostic 	sbufsize = SBUFINCR;
62037414Sbostic 	if ((sbuf = malloc((unsigned)sbufsize)) == NULL)
62137414Sbostic 		{
62237414Sbostic 		fatal("out of memory (argvtos)");
62337414Sbostic 		/* NOTREACHED */
62437414Sbostic 		}
62537414Sbostic 
62637414Sbostic 	for (i = 0; argc-- > 0; ++argv)
62737414Sbostic 		{
62837414Sbostic 		if ((nbytes = (i+strlen(*argv)+1-sbufsize)) > 0)
62937414Sbostic 			{
63037414Sbostic 			nu = (nbytes+SBUFINCR-1)/SBUFINCR;
63137414Sbostic 			sbufsize += nu * SBUFINCR;
63237414Sbostic 			if (sbufsize > SBUFMAX)
63337414Sbostic 				{
63437414Sbostic 				fatal("argument length exceeded (argvtos)");
63537414Sbostic 				/* NOTREACHED */
63637414Sbostic 				}
63737414Sbostic 			if ((sbuf = realloc(sbuf, (unsigned)sbufsize)) == NULL)
63837414Sbostic 				{
63937414Sbostic 				fatal("out of memory (argvtos)");
64037414Sbostic 				/* NOTREACHED */
64137414Sbostic 				}
64237414Sbostic 			}
64337414Sbostic 		for (s = *argv; *s != '\0'; i++, s++)
64437414Sbostic 			sbuf[i] = *s;
64537414Sbostic 		sbuf[i++] = ' ';
64637414Sbostic 		}
64737414Sbostic 	sbuf[--i] = '\0';
64837414Sbostic 	return(sbuf);
64937414Sbostic }
65037414Sbostic 
dofort(s)65137414Sbostic dofort(s)
65237414Sbostic char *s;
65337414Sbostic {
65437414Sbostic int retcode;
65537414Sbostic char buff[200];
65637414Sbostic 
65737414Sbostic infname = s;
65837414Sbostic sprintf(buff, "%s %s %s %s %s %s",
65937414Sbostic 	pass1name, fflags, s, asmfname, initfname, textfname);
66037414Sbostic switch( sys(buff) )
66137414Sbostic 	{
66237414Sbostic 	case 1:
66337414Sbostic 		goto error;
66437414Sbostic 	case 0:
66537414Sbostic 		break;
66637414Sbostic 	default:
66737414Sbostic 		goto comperror;
66837414Sbostic 	}
66937414Sbostic 
67037414Sbostic if( dopass2() )
67137414Sbostic 	goto comperror;
67237414Sbostic doasm(s);
67337414Sbostic retcode = 0;
67437414Sbostic 
67537414Sbostic ret:
67637414Sbostic 	rmf(asmfname);
67737414Sbostic 	rmf(initfname);
67837414Sbostic 	rmf(textfname);
67937414Sbostic 	return(retcode);
68037414Sbostic 
68137414Sbostic error:
68237414Sbostic 	fprintf(diagfile, "\nError.  No assembly.\n");
68337414Sbostic 	retcode = 1;
68437414Sbostic 	goto ret;
68537414Sbostic 
68637414Sbostic comperror:
68737414Sbostic 	fprintf(diagfile, "\ncompiler error.\n");
68837414Sbostic 	retcode = 2;
68937414Sbostic 	goto ret;
69037414Sbostic }
69137414Sbostic 
69237414Sbostic 
69337414Sbostic 
69437414Sbostic 
dopass2()69537414Sbostic dopass2()
69637414Sbostic {
69737414Sbostic char buff[100];
69837414Sbostic 
69937414Sbostic if(verbose)
70037414Sbostic 	fprintf(diagfile, "PASS2.");
70137414Sbostic 
70237414Sbostic #if FAMILY==DMR
70337414Sbostic 	sprintf(buff, "%s %s - %s", pass2name, textfname, asmpass2);
70437414Sbostic 	return( sys(buff) );
70537414Sbostic #endif
70637414Sbostic 
70737414Sbostic 
70837414Sbostic #if FAMILY == PCC
70937414Sbostic #	if TARGET==INTERDATA
71037414Sbostic 	sprintf(buff, "%s -A%s <%s >%s", pass2name, setfname, textfname, asmpass2);
71137414Sbostic #	else
71237414Sbostic 	sprintf(buff, "%s %s %s >%s",
71337414Sbostic 		pass2name, f2flags, textfname, asmpass2);
71437414Sbostic #	endif
71537414Sbostic 	return( sys(buff) );
71637414Sbostic #endif
71737414Sbostic }
71837414Sbostic 
71937414Sbostic 
72037414Sbostic 
72137414Sbostic 
doasm(s)72237414Sbostic doasm(s)
72337414Sbostic char *s;
72437414Sbostic {
72537414Sbostic register char *lastc;
72637414Sbostic char *obj;
72737414Sbostic char buff[200];
72837414Sbostic char *lastchar(), *setdoto();
72937414Sbostic 
73037414Sbostic if(*s == '\0')
73137414Sbostic 	s = objfdefault;
73237414Sbostic lastc = lastchar(s);
73337414Sbostic obj = setdoto(s);
73437414Sbostic 
73537414Sbostic #if TARGET==PDP11 || TARGET==VAX || TARGET==TAHOE
73637414Sbostic #	ifdef PASS2OPT
73737414Sbostic 	if(optimflag)
73837414Sbostic 		{
73937414Sbostic #if TARGET==TAHOE
74037414Sbostic 		sprintf(buff, "%s -f %s %s",
74137414Sbostic #else
74237414Sbostic 		sprintf(buff, "%s %s %s",
74337414Sbostic #endif
74437414Sbostic 		 pass2opt, asmpass2, optzfname);
74537414Sbostic 		if( sys(buff) )
74637414Sbostic 			rmf(optzfname);
74737414Sbostic 		else
74839250Sbostic 			(void)rename(optzfname, asmpass2);
74937414Sbostic 		}
75037414Sbostic #	endif
75137414Sbostic #endif
75237414Sbostic 
75337414Sbostic if(saveasmflag)
75437414Sbostic 	{
75537414Sbostic 	*lastc = 's';
75637414Sbostic #if TARGET == INTERDATA
75737414Sbostic 	sprintf(buff, "%s %s %s %s %s >%s", CATNAME, asmfname, initfname,
75837414Sbostic 		setfname, asmpass2, obj);
75937414Sbostic #else
76037414Sbostic #if TARGET == VAX || TARGET == TAHOE
76137414Sbostic 		sprintf(buff, "%s %s %s %s >%s",
76237414Sbostic 		CATNAME, asmfname, asmpass2, initfname, obj);
76337414Sbostic #else
76437414Sbostic 	sprintf(buff, "%s %s %s %s >%s",
76537414Sbostic 		CATNAME, asmfname, initfname, asmpass2, obj);
76637414Sbostic #endif
76737414Sbostic #endif
76837414Sbostic 	sys(buff);
76937414Sbostic 	*lastc = 'o';
77037414Sbostic 	}
77137414Sbostic else
77237414Sbostic 	{
77337414Sbostic 	if(verbose)
77437414Sbostic 		fprintf(diagfile, "  ASM.");
77537414Sbostic #if TARGET == INTERDATA
77637414Sbostic 	sprintf(buff, "%s -o %s %s %s %s %s", asmname, obj, asmfname,
77737414Sbostic 		initfname, setfname, asmpass2);
77837414Sbostic #endif
77937414Sbostic 
78037414Sbostic #if TARGET == VAX || TARGET == TAHOE
78137414Sbostic 	/* vax assembler currently accepts only one input file */
78237414Sbostic 
78337414Sbostic 	sprintf(buff, "%s %s %s >>%s",
78437414Sbostic 	CATNAME, asmpass2, initfname, asmfname);
78537414Sbostic 	sys(buff);
78637414Sbostic #ifdef UCBVAXASM
78737414Sbostic 	sprintf(buff, "%s -J -o %s %s", asmname, obj, asmfname);
78837414Sbostic #else
78937414Sbostic 	sprintf(buff, "%s -o %s %s", asmname, obj, asmfname);
79037414Sbostic #endif
79137414Sbostic #endif
79237414Sbostic 
79337414Sbostic #if TARGET == PDP11
79437414Sbostic 	sprintf(buff, "%s -u -o %s %s %s", asmname, obj, asmfname, asmpass2);
79537414Sbostic #endif
79637414Sbostic 
79737414Sbostic #if TARGET!=INTERDATA && TARGET!=PDP11 && TARGET!=VAX && TARGET!=TAHOE
79837414Sbostic 	sprintf(buff, "%s -o %s %s %s", asmname, obj, asmfname, asmpass2);
79937414Sbostic #endif
80037414Sbostic 
80137414Sbostic 	if( sys(buff) )
80237414Sbostic 		fatal("assembler error");
80337414Sbostic 	if(verbose)
80437414Sbostic 		fprintf(diagfile, "\n");
80537414Sbostic #if HERE==PDP11 && TARGET!=PDP11
80637414Sbostic 	rmf(obj);
80737414Sbostic #endif
80837414Sbostic 	}
80937414Sbostic 
81037414Sbostic rmf(asmpass2);
81137414Sbostic }
81237414Sbostic 
81337414Sbostic 
81437414Sbostic 
doload(v0,v)81537414Sbostic doload(v0, v)
81637414Sbostic register char *v0[], *v[];
81737414Sbostic {
81837414Sbostic char **p;
81937414Sbostic int waitpid;
82037414Sbostic 
82137414Sbostic if (profileflag)
82237414Sbostic 	{
82337414Sbostic 	for(p = p_liblist ; *p ; *v++ = *p++)
82437414Sbostic 		;
82537414Sbostic 	}
82637414Sbostic else	{
82737414Sbostic 	for(p = liblist ; *p ; *v++ = *p++)
82837414Sbostic 		;
82937414Sbostic 	}
83037414Sbostic 
83137414Sbostic *v++ = "-o";
83237414Sbostic *v++ = aoutname;
83337414Sbostic *v = NULL;
83437414Sbostic 
83537414Sbostic if(verbose)
83637414Sbostic 	fprintf(diagfile, "LOAD.");
83737414Sbostic if(debugflag)
83837414Sbostic 	{
83937414Sbostic 	for(p = v0 ; p<v ; ++p)
84037414Sbostic 		fprintf(diagfile, "%s ", *p);
84137414Sbostic 	fprintf(diagfile, "\n");
84237414Sbostic 	}
84337414Sbostic 
84437414Sbostic #if HERE==PDP11 || HERE==INTERDATA || HERE==VAX || HERE==TAHOE
84537414Sbostic 	if( (waitpid = fork()) == 0)
84637414Sbostic 		{
84737414Sbostic 		enbint(SIG_DFL);
84837414Sbostic 		execv(ldname, v0);
84937414Sbostic 		fatalstr("couldn't load %s", ldname);
85037414Sbostic 		}
85137414Sbostic 	await(waitpid);
85237414Sbostic #endif
85337414Sbostic 
85437414Sbostic #if HERE==INTERDATA
85537414Sbostic 	if(optimflag)
85637414Sbostic 		{
85737414Sbostic 		char buff1[100], buff2[100];
85837414Sbostic 		sprintf(buff1, "nopt %s -o junk.%d", aoutname, pid);
85937414Sbostic 		sprintf(buff2, "mv junk.%d %s", pid, aoutname);
86037414Sbostic 		if( sys(buff1) || sys(buff2) )
86137414Sbostic 			err("bad optimization");
86237414Sbostic 		}
86337414Sbostic #endif
86437414Sbostic 
86537414Sbostic if(verbose)
86637414Sbostic 	fprintf(diagfile, "\n");
86737414Sbostic }
86837414Sbostic 
86937414Sbostic /* Process control and Shell-simulating routines */
87037414Sbostic 
sys(str)87137414Sbostic sys(str)
87237414Sbostic char *str;
87337414Sbostic {
87437414Sbostic register char *s, *t;
87537792Sbostic char *argv[100];
87637414Sbostic char *inname, *outname;
87737414Sbostic int append;
87837414Sbostic int waitpid;
87937414Sbostic int argc;
88037414Sbostic 
88137414Sbostic 
88237414Sbostic if(debugflag)
88337414Sbostic 	fprintf(diagfile, "%s\n", str);
88437414Sbostic inname  = NULL;
88537414Sbostic outname = NULL;
88637414Sbostic argv[0] = shellname;
88737414Sbostic argc = 1;
88837414Sbostic 
88937414Sbostic t = str;
89037414Sbostic while( isspace(*t) )
89137414Sbostic 	++t;
89237414Sbostic while(*t)
89337414Sbostic 	{
89437414Sbostic 	if(*t == '<')
89537414Sbostic 		inname = t+1;
89637414Sbostic 	else if(*t == '>')
89737414Sbostic 		{
89837414Sbostic 		if(t[1] == '>')
89937414Sbostic 			{
90037414Sbostic 			append = YES;
90137414Sbostic 			outname = t+2;
90237414Sbostic 			}
90337414Sbostic 		else	{
90437414Sbostic 			append = NO;
90537414Sbostic 			outname = t+1;
90637414Sbostic 			}
90737414Sbostic 		}
90837414Sbostic 	else
90937414Sbostic 		argv[argc++] = t;
91037414Sbostic 	while( !isspace(*t) && *t!='\0' )
91137414Sbostic 		++t;
91237414Sbostic 	if(*t)
91337414Sbostic 		{
91437414Sbostic 		*t++ = '\0';
91537414Sbostic 		while( isspace(*t) )
91637414Sbostic 			++t;
91737414Sbostic 		}
91837414Sbostic 	}
91937414Sbostic 
92037414Sbostic if(argc == 1)   /* no command */
92137414Sbostic 	return(-1);
92237414Sbostic argv[argc] = 0;
92337414Sbostic 
92437414Sbostic if((waitpid = fork()) == 0)
92537414Sbostic 	{
92637414Sbostic 	if(inname)
92737414Sbostic 		freopen(inname, "r", stdin);
92837414Sbostic 	if(outname)
92937414Sbostic 		freopen(outname, (append ? "a" : "w"), stdout);
93037414Sbostic 	enbint(SIG_DFL);
93137414Sbostic 
93237792Sbostic 	texec(argv[1]  , argv);
93337414Sbostic 
93437792Sbostic 	fatalstr("Cannot load %s", argv[1]);
93537414Sbostic 	}
93637414Sbostic 
93737414Sbostic return( await(waitpid) );
93837414Sbostic }
93937414Sbostic 
94037414Sbostic 
94137414Sbostic 
94237414Sbostic 
94337414Sbostic 
94437414Sbostic #include "errno.h"
94537414Sbostic 
94637414Sbostic /* modified version from the Shell */
texec(f,av)94737414Sbostic texec(f, av)
94837414Sbostic char *f;
94937414Sbostic char **av;
95037414Sbostic {
95137414Sbostic extern int errno;
95237414Sbostic 
95337414Sbostic execv(f, av+1);
95437414Sbostic 
95537414Sbostic if (errno==ENOEXEC)
95637414Sbostic 	{
95737414Sbostic 	av[1] = f;
95837414Sbostic 	execv(shellname, av);
95937414Sbostic 	fatal("No shell!");
96037414Sbostic 	}
96137414Sbostic if (errno==ENOMEM)
96237414Sbostic 	fatalstr("%s: too large", f);
96337414Sbostic }
96437414Sbostic 
96537414Sbostic 
96637414Sbostic 
96737414Sbostic 
96837414Sbostic 
96937414Sbostic 
done(k)97037414Sbostic done(k)
97137414Sbostic int k;
97237414Sbostic {
97337414Sbostic static int recurs	= NO;
97437414Sbostic 
97537414Sbostic if(recurs == NO)
97637414Sbostic 	{
97737414Sbostic 	recurs = YES;
97837414Sbostic 	rmfiles();
97937414Sbostic 	}
98037414Sbostic exit(k);
98137414Sbostic }
98237414Sbostic 
98337414Sbostic 
98437414Sbostic 
98537414Sbostic 
98637414Sbostic 
98737414Sbostic 
98837414Sbostic enbint(k)
98937414Sbostic int (*k)();
99037414Sbostic {
99137414Sbostic if(sigivalue == 0)
99237414Sbostic 	signal(SIGINT,k);
99337414Sbostic if(sigqvalue == 0)
99437414Sbostic 	signal(SIGQUIT,k);
99537414Sbostic if(sighvalue == 0)
99637414Sbostic 	signal(SIGHUP,k);
99737414Sbostic if(sigtvalue == 0)
99837414Sbostic 	signal(SIGTERM,k);
99937414Sbostic }
100037414Sbostic 
100137414Sbostic 
100237414Sbostic 
100337414Sbostic 
intrupt()100437414Sbostic intrupt()
100537414Sbostic {
100637414Sbostic done(2);
100737414Sbostic }
100837414Sbostic 
100937414Sbostic 
101037414Sbostic #ifdef PSIGNAL
101137414Sbostic /*
101237414Sbostic  * Fancy 4.2 BSD signal printing stuff.
101337414Sbostic  */
101437414Sbostic char harmless[NSIG] = { 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
101537414Sbostic #endif
101637414Sbostic 
101737414Sbostic 
await(waitpid)101837414Sbostic await(waitpid)
101937414Sbostic int waitpid;
102037414Sbostic {
102137414Sbostic 
102237414Sbostic #ifdef PSIGNAL
102337414Sbostic extern char *sys_siglist[];
102437414Sbostic union wait status;
102537414Sbostic #else PSIGNAL
102637414Sbostic int status;
102737414Sbostic #endif PSIGNAL
102837414Sbostic 
102937414Sbostic int w;
103037414Sbostic 
103137414Sbostic enbint(SIG_IGN);
103237414Sbostic while ( (w = wait(&status)) != waitpid)
103337414Sbostic 	if(w == -1)
103437414Sbostic 		fatal("bad wait code");
103537414Sbostic enbint(intrupt);
103637414Sbostic 
103737414Sbostic #ifdef PSIGNAL
103837414Sbostic if(status.w_termsig)
103937414Sbostic 	{
104037414Sbostic 	debugflag = 0;	/* Prevent us from dumping core ourselves */
104137414Sbostic 	if(status.w_termsig != SIGINT && status.w_termsig < NSIG)
104237414Sbostic 		fprintf(diagfile, "%s%s\n", sys_siglist[status.w_termsig],
104337414Sbostic 			status.w_coredump ? " -- core dumped" : "");
104437414Sbostic 	if(status.w_termsig < NSIG && ! harmless[status.w_termsig])
104537414Sbostic 		fatal("see a system manager");
104637414Sbostic 	else
104737414Sbostic 		done(3);
104837414Sbostic 	}
104937414Sbostic return(status.w_retcode);
105037414Sbostic #else PSIGNAL
105137414Sbostic if(status & 0377)
105237414Sbostic 	{
105337414Sbostic 	if(status != SIGINT)
105437414Sbostic 		fprintf(diagfile, "Termination code %d\n", status);
105537414Sbostic 	done(3);
105637414Sbostic 	}
105737414Sbostic return(status>>8);
105837414Sbostic #endif PSIGNAL
105937414Sbostic }
106037414Sbostic 
106137414Sbostic /* File Name and File Manipulation Routines */
106237414Sbostic 
unreadable(s)106337414Sbostic unreadable(s)
106437414Sbostic register char *s;
106537414Sbostic {
106637414Sbostic register FILE *fp;
106737414Sbostic 
106837414Sbostic if(fp = fopen(s, "r"))
106937414Sbostic 	{
107037414Sbostic 	fclose(fp);
107137414Sbostic 	return(NO);
107237414Sbostic 	}
107337414Sbostic 
107437414Sbostic else
107537414Sbostic 	{
107637414Sbostic 	fprintf(diagfile, "Error: Cannot read file %s\n", s);
107737414Sbostic 	return(YES);
107837414Sbostic 	}
107937414Sbostic }
108037414Sbostic 
108137414Sbostic 
108237414Sbostic 
stupid(s)108337414Sbostic stupid(s)
108437414Sbostic char *s;
108537414Sbostic {
108637414Sbostic char c;
108737414Sbostic 
108837414Sbostic if( (c = dotchar(s))
108937414Sbostic   && index("focsreF", c)
109037414Sbostic   && access(s, 0) == 0 )
109137414Sbostic 	{
109237414Sbostic 	fprintf(diagfile, "Loading on %s would destroy it\n", s);
109337414Sbostic 	return(YES);
109437414Sbostic 	}
109537414Sbostic return(NO);
109637414Sbostic }
109737414Sbostic 
109837414Sbostic 
109937414Sbostic 
clf(p)110037414Sbostic clf(p)
110137414Sbostic FILEP *p;
110237414Sbostic {
110337414Sbostic if(p!=NULL && *p!=NULL && *p!=stdout)
110437414Sbostic 	{
110537414Sbostic 	if(ferror(*p))
110637414Sbostic 		fatal("writing error");
110737414Sbostic 	fclose(*p);
110837414Sbostic 	}
110937414Sbostic *p = NULL;
111037414Sbostic }
111137414Sbostic 
rmfiles()111237414Sbostic rmfiles()
111337414Sbostic {
111437414Sbostic rmf(textfname);
111537414Sbostic rmf(asmfname);
111637414Sbostic rmf(initfname);
111737414Sbostic rmf(asmpass2);
111837414Sbostic #if TARGET == INTERDATA
111937414Sbostic 	rmf(setfname);
112037414Sbostic #endif
112137414Sbostic }
112237414Sbostic 
112337414Sbostic 
112437414Sbostic 
112537414Sbostic 
112637414Sbostic 
112737414Sbostic 
112837414Sbostic 
112937414Sbostic 
113037414Sbostic /* return -1 if file does not exist, 0 if it is of zero length
113137414Sbostic    and 1 if of positive length
113237414Sbostic */
content(filename)113337414Sbostic content(filename)
113437414Sbostic char *filename;
113537414Sbostic {
113637414Sbostic #ifdef VERSION6
113737414Sbostic 	struct stat
113837414Sbostic 		{
113937414Sbostic 		char cjunk[9];
114037414Sbostic 		char size0;
114137414Sbostic 		int size1;
114237414Sbostic 		int ijunk[12];
114337414Sbostic 		} buf;
114437414Sbostic #else
114537414Sbostic 	struct stat buf;
114637414Sbostic #endif
114737414Sbostic 
114837414Sbostic if(stat(filename,&buf) < 0)
114937414Sbostic 	return(-1);
115037414Sbostic #ifdef VERSION6
115137414Sbostic 	return(buf.size0 || buf.size1);
115237414Sbostic #else
115337414Sbostic 	return( buf.st_size > 0 );
115437414Sbostic #endif
115537414Sbostic }
115637414Sbostic 
115737414Sbostic 
115837414Sbostic 
115937414Sbostic 
crfnames()116037414Sbostic crfnames()
116137414Sbostic {
116237414Sbostic fname(textfname, "x");
116337414Sbostic fname(asmfname, "s");
116437414Sbostic fname(asmpass2, "a");
116537414Sbostic fname(initfname, "d");
116637414Sbostic fname(sortfname, "S");
116737414Sbostic fname(objfdefault, "o");
116837414Sbostic fname(prepfname, "p");
116937414Sbostic fname(optzfname, "z");
117037414Sbostic fname(setfname, "A");
117137414Sbostic }
117237414Sbostic 
117337414Sbostic 
117437414Sbostic 
117537414Sbostic 
rmf(fn)117637414Sbostic rmf(fn)
117737414Sbostic register char *fn;
117837414Sbostic {
117937414Sbostic /* if(!debugflag && fn!=NULL && *fn!='\0') */
118037414Sbostic 
118137414Sbostic if(fn!=NULL && *fn!='\0')
118237414Sbostic 	unlink(fn);
118337414Sbostic }
118437414Sbostic 
118537414Sbostic 
118637414Sbostic 
118737414Sbostic 
118837414Sbostic 
fname(name,suff)118937414Sbostic LOCAL fname(name, suff)
119037414Sbostic char *name, *suff;
119137414Sbostic {
119237792Sbostic sprintf(name, "%s/%s%d.%s", _PATH_TMP, temppref, pid, suff);
119337414Sbostic }
119437414Sbostic 
119537414Sbostic 
119637414Sbostic 
119737414Sbostic 
dotchar(s)119837414Sbostic dotchar(s)
119937414Sbostic register char *s;
120037414Sbostic {
120137414Sbostic for( ; *s ; ++s)
120237414Sbostic 	if(s[0]=='.' && s[1]!='\0' && s[2]=='\0')
120337414Sbostic 		return( s[1] );
120437414Sbostic return(NO);
120537414Sbostic }
120637414Sbostic 
120737414Sbostic 
120837414Sbostic 
lastfield(s)120937414Sbostic char *lastfield(s)
121037414Sbostic register char *s;
121137414Sbostic {
121237414Sbostic register char *t;
121337414Sbostic for(t = s; *s ; ++s)
121437414Sbostic 	if(*s == '/')
121537414Sbostic 		t = s+1;
121637414Sbostic return(t);
121737414Sbostic }
121837414Sbostic 
121937414Sbostic 
122037414Sbostic 
lastchar(s)122137414Sbostic char *lastchar(s)
122237414Sbostic register char *s;
122337414Sbostic {
122437414Sbostic while(*s)
122537414Sbostic 	++s;
122637414Sbostic return(s-1);
122737414Sbostic }
122837414Sbostic 
setdoto(s)122937414Sbostic char *setdoto(s)
123037414Sbostic register char *s;
123137414Sbostic {
123237414Sbostic *lastchar(s) = 'o';
123337414Sbostic return( lastfield(s) );
123437414Sbostic }
123537414Sbostic 
123637414Sbostic 
123737414Sbostic 
badfile(s)123837414Sbostic badfile(s)
123937414Sbostic char *s;
124037414Sbostic {
124137414Sbostic fatalstr("cannot open intermediate file %s", s);
124237414Sbostic }
124337414Sbostic 
124437414Sbostic 
124537414Sbostic 
ckalloc(n)124637414Sbostic ptr ckalloc(n)
124737414Sbostic int n;
124837414Sbostic {
124937414Sbostic ptr p, calloc();
125037414Sbostic 
125137414Sbostic if( p = calloc(1, (unsigned) n) )
125237414Sbostic 	return(p);
125337414Sbostic 
125437414Sbostic fatal("out of memory");
125537414Sbostic /* NOTREACHED */
125637414Sbostic }
125737414Sbostic 
125837414Sbostic 
125937414Sbostic 
126037414Sbostic 
126137414Sbostic 
copyn(n,s)126237414Sbostic char *copyn(n, s)
126337414Sbostic register int n;
126437414Sbostic register char *s;
126537414Sbostic {
126637414Sbostic register char *p, *q;
126737414Sbostic 
126837414Sbostic p = q = (char *) ckalloc(n);
126937414Sbostic while(n-- > 0)
127037414Sbostic 	*q++ = *s++;
127137414Sbostic return(p);
127237414Sbostic }
127337414Sbostic 
127437414Sbostic 
127537414Sbostic 
copys(s)127637414Sbostic char *copys(s)
127737414Sbostic char *s;
127837414Sbostic {
127937414Sbostic return( copyn( strlen(s)+1 , s) );
128037414Sbostic }
128137414Sbostic 
128237414Sbostic 
128337414Sbostic 
128437414Sbostic 
128537414Sbostic 
oneof(c,s)128637414Sbostic oneof(c,s)
128737414Sbostic register c;
128837414Sbostic register char *s;
128937414Sbostic {
129037414Sbostic while( *s )
129137414Sbostic 	if(*s++ == c)
129237414Sbostic 		return(YES);
129337414Sbostic return(NO);
129437414Sbostic }
129537414Sbostic 
129637414Sbostic 
129737414Sbostic 
nodup(s)129837414Sbostic nodup(s)
129937414Sbostic char *s;
130037414Sbostic {
130137414Sbostic register char **p;
130237414Sbostic 
130337414Sbostic for(p = loadargs ; p < loadp ; ++p)
130437414Sbostic 	if( !strcmp(*p, s) )
130537414Sbostic 		return(NO);
130637414Sbostic 
130737414Sbostic return(YES);
130837414Sbostic }
130937414Sbostic 
131037414Sbostic 
131137414Sbostic 
fatal(t)131237414Sbostic static fatal(t)
131337414Sbostic char *t;
131437414Sbostic {
131537414Sbostic fprintf(diagfile, "Compiler error in file %s: %s\n", infname, t);
131637414Sbostic if(debugflag)
131737414Sbostic 	abort();
131837414Sbostic done(1);
131937414Sbostic exit(1);
132037414Sbostic }
132137414Sbostic 
132237414Sbostic 
132337414Sbostic 
132437414Sbostic 
fatali(t,d)132537414Sbostic static fatali(t,d)
132637414Sbostic char *t;
132737414Sbostic int d;
132837414Sbostic {
132937414Sbostic char buff[100];
133037414Sbostic sprintf(buff, t, d);
133137414Sbostic fatal(buff);
133237414Sbostic }
133337414Sbostic 
133437414Sbostic 
133537414Sbostic 
133637414Sbostic 
fatalstr(t,s)133737414Sbostic static fatalstr(t, s)
133837414Sbostic char *t, *s;
133937414Sbostic {
134037414Sbostic char buff[100];
134137414Sbostic sprintf(buff, t, s);
134237414Sbostic fatal(buff);
134337414Sbostic }
err(s)134437414Sbostic err(s)
134537414Sbostic char *s;
134637414Sbostic {
134737414Sbostic fprintf(diagfile, "Error in file %s: %s\n", infname, s);
134837414Sbostic }
134937414Sbostic 
1350