-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
145 lines (140 loc) · 2.95 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <errno.h>
#include "awk.def"
#include "awk.h"
int dbg = 0;
int svargc;
char **svargv, **xargv;
extern FILE *yyin; /* lex input file */
char *lexprog; /* points to program argument if it exists */
extern errorflg; /* non-zero if any syntax errors; set by yyerror */
char buf[2];
int filefd, iflag, symnum, ansfd;
char *filelist;
extern int maxsym, errno;
main(argc, argv) int argc; char *argv[]; {
pflinit(); /* CoCo support */
pffinit(); /* CoCo support */
yyin = stdin;
if (argc == 1)
error(FATAL, "Usage: awk [-f source | 'cmds'] [files]");
if (strcmp(argv[0], "a.out"))
logit(argc, argv);
syminit();
while (argc > 1) {
argc--;
argv++;
if (argv[0][0] == '-' && argv[0][1] == 'f') {
yyin = fopen(argv[1], "r");
if (yyin == NULL)
error(FATAL, "can't open %s", argv[1]);
argc--;
argv++;
break;
} else if (argv[0][0] == '-' && argv[0][1] == 'F') { /* set field sep */
if (argv[0][2] == 't') /* special case for tab */
**FS = '\t';
else
**FS = argv[0][2];
continue;
} else if (argv[0][0] != '-') {
dprintf("cmds=|%s|\n", argv[0], NULL, NULL);
yyin = NULL;
lexprog = argv[0];
argv[0] = argv[-1]; /* need this space */
break;
} else if (strcmp("-d", argv[0])==0)
dbg = 1;
else if (argv[0][0]=='-' && argv[0][1]=='i') {
iflag=1;
sscanf(argv[0], "-i%d, %d", &filefd, &ansfd);
}
}
if (argc <= 1 && !iflag) {
argv[0][0] = '-';
argv[0][1] = '\0';
argc++;
argv--;
}
if (!iflag) {
svargc = --argc;
svargv = ++argv;
dprintf("svargc=%d svargv[0]=%s\n", svargc, svargv[0], NULL);
}
*FILENAME = *svargv; /* initial file name */
iloop:
if (iflag)
msgfiles();
yyparse();
dprintf("errorflg=%d\n", errorflg, NULL, NULL);
if (errorflg)
exit(0);
run();
if (iflag)
write(ansfd, &errorflg, sizeof(errorflg));
else exit(errorflg);
/*sym cleanup should go here , followed by another syminit*/
goto iloop;
}
logit(n, s) char *s[];
{ int i, tvec[2];
FILE *f, *g;
char buf[512];
if ((f=fopen("/usr/pjw/awk/awkhist", "a"))==NULL)
return;
time(tvec);
fprintf(f, "%-8s %s", getlogin(), ctime(tvec));
for (i=0; i<n; i++)
fprintf(f, "'%s'", s[i]);
putc('\n', f);
if (strcmp(s[1], "-f")) {
fclose(f);
return;
}
if ((g=fopen(s[2], "r"))==NULL) {
fclose(f);
return;
}
while ((i=fread(buf, 1, 512, g))>0)
fwrite(buf, 1, i, f);
fclose(f);
fclose(g);
}
yywrap()
{
return(1);
}
msgfiles()
{ char buf[512], *p, *q, **s;
int n;
n=read(filefd, buf, 512);
if (n<=0) /*no one at other end?*/ {
perror("no files");
exit(errno);
}
xfree(filelist);
q=filelist=malloc(n);
for (p=buf; *p==' ' || *p=='\t' || *p=='\n'; p++);
for (n=0; *p!=';'; )
{
if (*p==' ' || *p=='\t' || *p=='\n') {
n++;
*q++=0;
while (*p==' ' || *p=='\t' || *p=='\n')
p++;
}
else *q++ = *p++;
}
if (q!=filelist && *(q-1)!=0) {
n++;
*q++ = 0;
}
svargc=n;
xfree(xargv);
xargv=s=svargv=malloc(n*sizeof(char *));
for (p=filelist; n>0; n--)
{
*s++=p;
while (*p++ != 0);
}
}