BSD 4_4 release
[unix-history] / usr / src / old / awk / main.c
CommitLineData
2791ff57
KB
1/*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
ad787160
C
5 * This module is believed to contain source code proprietary to AT&T.
6 * Use and redistribution is subject to the Berkeley Software License
7 * Agreement and your Software Agreement with AT&T (Western Electric).
2791ff57
KB
8 */
9
10#ifndef lint
11char copyright[] =
12"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
13 All rights reserved.\n";
14#endif /* not lint */
15
b803c326 16#ifndef lint
ad787160 17static char sccsid[] = "@(#)main.c 4.6 (Berkeley) 4/17/91";
2791ff57 18#endif /* not lint */
90a429fa
KM
19
20#include "stdio.h"
21#include "ctype.h"
22#include "awk.def"
23#include "awk.h"
24#define TOLOWER(c) (isupper(c) ? tolower(c) : c) /* ugh!!! */
25
26int dbg = 0;
569c4905 27int ldbg = 0;
90a429fa
KM
28int svflg = 0;
29int rstflg = 0;
30int svargc;
31char **svargv, **xargv;
32extern FILE *yyin; /* lex input file */
33char *lexprog; /* points to program argument if it exists */
34extern errorflag; /* non-zero if any syntax errors; set by yyerror */
35
36int filefd, symnum, ansfd;
37char *filelist;
38extern int maxsym, errno;
39main(argc, argv) int argc; char *argv[]; {
40 if (argc == 1)
41 error(FATAL, "Usage: awk [-f source | 'cmds'] [files]");
90a429fa
KM
42 syminit();
43 while (argc > 1) {
44 argc--;
45 argv++;
46 /* this nonsense is because gcos argument handling */
47 /* folds -F into -f. accordingly, one checks the next
48 /* character after f to see if it's -f file or -Fx.
49 */
50 if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f' && argv[0][2] == '\0') {
26758489
KB
51 if (argv[1][0] == '-' && argv[1][1] == '\0')
52 yyin = stdin;
53 else {
54 yyin = fopen(argv[1], "r");
55 if (yyin == NULL)
56 error(FATAL, "can't open %s", argv[1]);
57 }
90a429fa
KM
58 argc--;
59 argv++;
60 break;
61 } else if (argv[0][0] == '-' && TOLOWER(argv[0][1]) == 'f') { /* set field sep */
62 if (argv[0][2] == 't') /* special case for tab */
63 **FS = '\t';
64 else
65 **FS = argv[0][2];
66 continue;
67 } else if (argv[0][0] != '-') {
68 dprintf("cmds=|%s|\n", argv[0], NULL, NULL);
69 yyin = NULL;
70 lexprog = argv[0];
71 argv[0] = argv[-1]; /* need this space */
72 break;
73 } else if (strcmp("-d", argv[0])==0) {
74 dbg = 1;
75 }
569c4905
SL
76 else if (strcmp("-l", argv[0])==0) {
77 ldbg = 1;
78 }
90a429fa
KM
79 else if(strcmp("-S", argv[0]) == 0) {
80 svflg = 1;
81 }
82 else if(strncmp("-R", argv[0], 2) == 0) {
83 if(thaw(argv[0] + 2) == 0)
84 rstflg = 1;
85 else {
86 fprintf(stderr, "not restored\n");
87 exit(1);
88 }
89 }
90 }
91 if (argc <= 1) {
92 argv[0][0] = '-';
93 argv[0][1] = '\0';
94 argc++;
95 argv--;
96 }
97 svargc = --argc;
98 svargv = ++argv;
99 dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0], NULL);
100 *FILENAME = *svargv; /* initial file name */
101 if(rstflg == 0)
102 yyparse();
103 dprintf("errorflag=%d\n", errorflag, NULL, NULL);
104 if (errorflag)
105 exit(errorflag);
106 if(svflg) {
107 svflg = 0;
108 if(freeze("awk.out") != 0)
109 fprintf(stderr, "not saved\n");
110 exit(0);
111 }
112 run();
113 exit(errorflag);
114}
115
90a429fa
KM
116yywrap()
117{
118 return(1);
119}