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