|
|
1.1 root 1: #include "stdio.h"
2: #include "awk.def"
3: #include "awk.h"
4: #include "ctype.h"
5:
6: FILE *infile = NULL;
7: char *file;
8: #define RECSIZE (5 * 512)
9: char record[RECSIZE];
10: char fields[RECSIZE];
11:
12: #define MAXFLD 100
13: int donefld; /* 1 = implies rec broken into fields */
14: int donerec; /* 1 = record is valid (no flds have changed) */
15: int mustfld; /* 1 = NF seen, so always break*/
16:
17: #define FINIT {0, NULL, 0.0, FLD|STR}
18: cell fldtab[MAXFLD] = { /*room for fields */
19: { "$record", record, 0.0, STR|FLD},
20: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
21: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
22: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
23: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
24: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
25: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT,
26: FINIT, FINIT, FINIT, FINIT, FINIT, FINIT, FINIT
27: };
28: int maxfld = 0; /* last used field */
29:
30:
31: getrec()
32: {
33: register char *rr;
34: extern int svargc;
35: extern char **svargv;
36: register c, sep;
37:
38: dprintf("**RS=%o, **FS=%o\n", **RS, **FS, NULL);
39: donefld = 0;
40: donerec = 1;
41: record[0] = 0;
42: while (svargc > 0) {
43: dprintf("svargc=%d, *svargv=%s\n", svargc, *svargv, NULL);
44: if (infile == NULL) { /* have to open a new file */
45: if (member('=', *svargv)) { /* it's a var=value argument */
46: setclvar(*svargv);
47: svargv++;
48: svargc--;
49: continue;
50: }
51: *FILENAME = file = *svargv;
52: dprintf("opening file %s\n", file, NULL, NULL);
53: if (*file == '-')
54: infile = stdin;
55: else if ((infile = fopen(file, "r")) == NULL)
56: error(FATAL, "can't open %s", file);
57: }
58: if ((sep = **RS) == 0)
59: sep = '\n';
60: for (rr = record; ; ) {
61: for (; (c=getc(infile)) != sep && c != EOF; *rr++ = c)
62: ;
63: if (**RS == sep || c == EOF)
64: break;
65: if ((c = getc(infile)) == '\n' || c == EOF) /* 2 in a row */
66: break;
67: *rr++ = '\n';
68: *rr++ = c;
69: }
70: if (rr > record+RECSIZE)
71: error(FATAL, "record `%.20s...' too long", record);
72: *rr = 0;
73: if (mustfld)
74: fldbld();
75: if (c != EOF || rr > record) { /* normal record */
76: recloc->tval &= ~NUM;
77: recloc->tval |= STR;
78: ++nrloc->fval;
79: nrloc->tval &= ~STR;
80: nrloc->tval |= NUM;
81: return(1);
82: }
83: /* EOF arrived on this file; set up next */
84: if (infile != stdin)
85: fclose(infile);
86: infile = NULL;
87: svargc--;
88: svargv++;
89: }
90: return(0); /* true end of file */
91: }
92:
93: setclvar(s) /* set var=value from s */
94: char *s;
95: {
96: char *p;
97: cell *q;
98:
99: for (p=s; *p != '='; p++)
100: ;
101: *p++ = 0;
102: q = setsymtab(s, tostring(p), 0.0, STR, symtab);
103: setsval(q, p);
104: dprintf("command line set %s to |%s|\n", s, p, NULL);
105: }
106:
107: fldbld()
108: {
109: register char *r, *fr, sep;
110: int i, j;
111:
112: r = record;
113: fr = fields;
114: i = 0; /* number of fields accumulated here */
115: if ((sep = **FS) == ' ')
116: for (i = 0; ; ) {
117: while (*r == ' ' || *r == '\t' || *r == '\n')
118: r++;
119: if (*r == 0)
120: break;
121: i++;
122: if (i >= MAXFLD)
123: error(FATAL, "record `%.20s...' has too many fields", record);
124: if (!(fldtab[i].tval&FLD))
125: xfree(fldtab[i].sval);
126: fldtab[i].sval = fr;
127: fldtab[i].tval = FLD | STR;
128: do
129: *fr++ = *r++;
130: while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0');
131: *fr++ = 0;
132: }
133: else if (*r != 0) /* if 0, it's a null field */
134: for (;;) {
135: i++;
136: if (i >= MAXFLD)
137: error(FATAL, "record `%.20s...' has too many fields", record);
138: if (!(fldtab[i].tval&FLD))
139: xfree(fldtab[i].sval);
140: fldtab[i].sval = fr;
141: fldtab[i].tval = FLD | STR;
142: while (*r != sep && *r != '\n' && *r != '\0') /* \n always a separator */
143: *fr++ = *r++;
144: *fr++ = 0;
145: if (*r++ == 0)
146: break;
147: }
148: *fr = 0;
149: for (j=MAXFLD-1; j>i; j--) { /* clean out junk from previous record */
150: if (!(fldtab[j].tval&FLD))
151: xfree(fldtab[j].sval);
152: fldtab[j].tval = STR | FLD;
153: fldtab[j].sval = NULL;
154: }
155: maxfld = i;
156: donefld = 1;
157: for(i=1; i<=maxfld; i++)
158: if(isnumber(fldtab[i].sval)) {
159: fldtab[i].fval = atof(fldtab[i].sval);
160: fldtab[i].tval |= NUM;
161: }
162: setfval(lookup("NF", symtab, 0), (awkfloat) maxfld);
163: if (dbg)
164: for (i = 0; i <= maxfld; i++)
165: printf("field %d: |%s|\n", i, fldtab[i].sval);
166: }
167:
168: recbld()
169: {
170: int i;
171: register char *r, *p;
172:
173: if (donefld == 0 || donerec == 1)
174: return;
175: r = record;
176: for (i = 1; i <= *NF; i++) {
177: p = getsval(&fldtab[i]);
178: while (*r++ = *p++)
179: ;
180: *(r-1) = **OFS;
181: }
182: *(r-1) = '\0';
183: dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
184: recloc->tval = STR | FLD;
185: dprintf("in recbld FS=%o, recloc=%o\n", **FS, recloc, NULL);
186: if (r > record+RECSIZE)
187: error(FATAL, "built giant record `%.20s...'", record);
188: dprintf("recbld = |%s|\n", record, NULL, NULL);
189: }
190:
191: cell *fieldadr(n)
192: {
193: if (n >= MAXFLD)
194: error(FATAL, "trying to access field %d", n);
195: return(&fldtab[n]);
196: }
197:
198: int errorflag = 0;
199:
200: yyerror(s) char *s; {
201: fprintf(stderr, "awk: %s near line %d\n", s, lineno);
202: errorflag = 2;
203: }
204:
205: error(f, s, a1, a2, a3, a4, a5, a6, a7) {
206: fprintf(stderr, "awk: ");
207: fprintf(stderr, s, a1, a2, a3, a4, a5, a6, a7);
208: fprintf(stderr, "\n");
209: if (*NR > 0)
210: fprintf(stderr, " record number %g\n", *NR);
211: if (f)
212: exit(2);
213: }
214:
215: PUTS(s) char *s; {
216: dprintf("%s\n", s, NULL, NULL);
217: }
218:
219: #define MAXEXPON 38 /* maximum exponenet for fp number */
220:
221: isnumber(s)
222: register char *s;
223: {
224: register d1, d2;
225: int point;
226: char *es;
227:
228: d1 = d2 = point = 0;
229: while (*s == ' ' || *s == '\t' || *s == '\n')
230: s++;
231: if (*s == '\0')
232: return(0); /* empty stuff isn't number */
233: if (*s == '+' || *s == '-')
234: s++;
235: if (!isdigit(*s) && *s != '.')
236: return(0);
237: if (isdigit(*s)) {
238: do {
239: d1++;
240: s++;
241: } while (isdigit(*s));
242: }
243: if(d1 >= MAXEXPON)
244: return(0); /* too many digits to convert */
245: if (*s == '.') {
246: point++;
247: s++;
248: }
249: if (isdigit(*s)) {
250: d2++;
251: do {
252: s++;
253: } while (isdigit(*s));
254: }
255: if (!(d1 || point && d2))
256: return(0);
257: if (*s == 'e' || *s == 'E') {
258: s++;
259: if (*s == '+' || *s == '-')
260: s++;
261: if (!isdigit(*s))
262: return(0);
263: es = s;
264: do {
265: s++;
266: } while (isdigit(*s));
267: if (s - es > 2)
268: return(0);
269: else if (s - es == 2 && 10 * (*es-'0') + *(es+1)-'0' >= MAXEXPON)
270: return(0);
271: }
272: while (*s == ' ' || *s == '\t' || *s == '\n')
273: s++;
274: if (*s == '\0')
275: return(1);
276: else
277: return(0);
278: }
279: /*
280: isnumber(s) char *s; {return(0);}
281: */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.