xref: /csrg-svn/old/awk/main.c (revision 48236)
1*48236Sbostic /*-
2*48236Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*48236Sbostic  * All rights reserved.
4*48236Sbostic  *
5*48236Sbostic  * %sccs.include.proprietary.c%
6*48236Sbostic  */
7*48236Sbostic 
814473Ssam #ifndef lint
9*48236Sbostic char copyright[] =
10*48236Sbostic "@(#) Copyright (c) 1991 The Regents of the University of California.\n\
11*48236Sbostic  All rights reserved.\n";
12*48236Sbostic #endif /* not lint */
136672Smckusick 
14*48236Sbostic #ifndef lint
15*48236Sbostic static char sccsid[] = "@(#)main.c	4.6 (Berkeley) 04/17/91";
16*48236Sbostic #endif /* not lint */
17*48236Sbostic 
186672Smckusick #include "stdio.h"
196672Smckusick #include "ctype.h"
206672Smckusick #include "awk.def"
216672Smckusick #include "awk.h"
226672Smckusick #define TOLOWER(c)	(isupper(c) ? tolower(c) : c) /* ugh!!! */
236672Smckusick 
246672Smckusick int	dbg	= 0;
2517490Ssam int	ldbg	= 0;
266672Smckusick int	svflg	= 0;
276672Smckusick int	rstflg	= 0;
286672Smckusick int	svargc;
296672Smckusick char	**svargv, **xargv;
306672Smckusick extern FILE	*yyin;	/* lex input file */
316672Smckusick char	*lexprog;	/* points to program argument if it exists */
326672Smckusick extern	errorflag;	/* non-zero if any syntax errors; set by yyerror */
336672Smckusick 
346672Smckusick int filefd, symnum, ansfd;
356672Smckusick char *filelist;
366672Smckusick extern int maxsym, errno;
main(argc,argv)376672Smckusick main(argc, argv) int argc; char *argv[]; {
386672Smckusick 	if (argc == 1)
396672Smckusick 		error(FATAL, "Usage: awk [-f source | 'cmds'] [files]");
406672Smckusick 	syminit();
416672Smckusick 	while (argc > 1) {
426672Smckusick 		argc--;
436672Smckusick 		argv++;
446672Smckusick 		/* this nonsense is because gcos argument handling */
456672Smckusick 		/* folds -F into -f.  accordingly, one checks the next
466672Smckusick 		/* character after f to see if it's -f file or -Fx.
476672Smckusick 		*/
486672Smckusick 		if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f' && argv[0][2] == '\0') {
4932353Sbostic 			if (argv[1][0] == '-' && argv[1][1] == '\0')
5032353Sbostic 				yyin = stdin;
5132353Sbostic 			else {
5232353Sbostic 				yyin = fopen(argv[1], "r");
5332353Sbostic 				if (yyin == NULL)
5432353Sbostic 					error(FATAL, "can't open %s", argv[1]);
5532353Sbostic 			}
566672Smckusick 			argc--;
576672Smckusick 			argv++;
586672Smckusick 			break;
596672Smckusick 		} else if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f') {	/* set field sep */
606672Smckusick 			if (argv[0][2] == 't')	/* special case for tab */
616672Smckusick 				**FS = '\t';
626672Smckusick 			else
636672Smckusick 				**FS = argv[0][2];
646672Smckusick 			continue;
656672Smckusick 		} else if (argv[0][0] != '-') {
666672Smckusick 			dprintf("cmds=|%s|\n", argv[0], NULL, NULL);
676672Smckusick 			yyin = NULL;
686672Smckusick 			lexprog = argv[0];
696672Smckusick 			argv[0] = argv[-1];	/* need this space */
706672Smckusick 			break;
716672Smckusick 		} else if (strcmp("-d", argv[0])==0) {
726672Smckusick 			dbg = 1;
736672Smckusick 		}
7417490Ssam 		else if (strcmp("-l", argv[0])==0) {
7517490Ssam 			ldbg = 1;
7617490Ssam 		}
776672Smckusick 		else if(strcmp("-S", argv[0]) == 0) {
786672Smckusick 			svflg = 1;
796672Smckusick 		}
806672Smckusick 		else if(strncmp("-R", argv[0], 2) == 0) {
816672Smckusick 			if(thaw(argv[0] + 2) == 0)
826672Smckusick 				rstflg = 1;
836672Smckusick 			else {
846672Smckusick 				fprintf(stderr, "not restored\n");
856672Smckusick 				exit(1);
866672Smckusick 			}
876672Smckusick 		}
886672Smckusick 	}
896672Smckusick 	if (argc <= 1) {
906672Smckusick 		argv[0][0] = '-';
916672Smckusick 		argv[0][1] = '\0';
926672Smckusick 		argc++;
936672Smckusick 		argv--;
946672Smckusick 	}
956672Smckusick 	svargc = --argc;
966672Smckusick 	svargv = ++argv;
976672Smckusick 	dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0], NULL);
986672Smckusick 	*FILENAME = *svargv;	/* initial file name */
996672Smckusick 	if(rstflg == 0)
1006672Smckusick 		yyparse();
1016672Smckusick 	dprintf("errorflag=%d\n", errorflag, NULL, NULL);
1026672Smckusick 	if (errorflag)
1036672Smckusick 		exit(errorflag);
1046672Smckusick 	if(svflg) {
1056672Smckusick 		svflg = 0;
1066672Smckusick 		if(freeze("awk.out") != 0)
1076672Smckusick 			fprintf(stderr, "not saved\n");
1086672Smckusick 		exit(0);
1096672Smckusick 	}
1106672Smckusick 	run();
1116672Smckusick 	exit(errorflag);
1126672Smckusick }
1136672Smckusick 
yywrap()1146672Smckusick yywrap()
1156672Smckusick {
1166672Smckusick 	return(1);
1176672Smckusick }
118