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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/* $OpenBSD: OpenBSD-PATCHES,v 1.4 1999/04/20 17:31:28 millert Exp $ */
I merged the April 16, 1999 version of the "one true awk" from Brian Kernighan's
web page http://cm.bell-labs.com/who/bwk/
Mods to the distribution sources:
1) OpenBSD tags in all files.
2) setlocale(3) call added to main().
3) Use __progname instead of argv[0] to determine name.
4) left out win95 bits, distribution makefile, backup copies of yacc generated
files.
Start of historical section:
--------------------------------------------------------------
These are the changes made from Research Awk to allow it to
build on OpenBSD with flex. Below is the inof that came
with the original set of diffs (from 4.4BSD). ytab.* was
changed to awkgram.* for make's benefit.
- todd
From: Vern Paxson <vern@daffy.ee.lbl.gov>
I've ported Research Awk to flex and tested it moderately. Note that I
didn't have time to support the nice error-message context stuff (where
it points out exactly where on a line it thinks an error occurred), as
the original code made a lot of assumptions regarding the internal
buffering of a lex scanner that are no longer valid with flex. Also, the
sources had a function called "isnumber" which conflicted with a macro by
the same name in <ctype.h>, so I changed its name to is_a_number.
Let me know if you find more problems.
Vern
--- /home/millert/tmp/awk/awk.1 Sun Jan 19 18:06:25 1997
+++ awk.1 Sun Jan 19 17:51:39 1997
@@ -1,3 +1,4 @@
+.\" $OpenBSD: OpenBSD-PATCHES,v 1.4 1999/04/20 17:31:28 millert Exp $
.de EX
.nf
.ft CW
@@ -13,7 +14,7 @@
.SH NAME
awk \- pattern-directed scanning and processing language
.SH SYNOPSIS
-.B awk
+.B awk|nawk
[
.BI \-F
.I fs
--- /home/millert/tmp/awk/awklex.l Sun Jan 19 18:06:24 1997
+++ awklex.l Sun Jan 19 18:00:52 1997
@@ -29,13 +29,15 @@
may not be preserved in other implementations of lex.
*/
+#ifndef FLEX_SCANNER
#undef input /* defeat lex */
#undef unput
+#endif /* !FLEX_SCANNER */
#include <stdlib.h>
#include <string.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
extern YYSTYPE yylval;
extern int infunc;
@@ -60,6 +62,20 @@
char *s;
Gstring *gs = 0; /* initialized in main() */
int cflag;
+
+#ifdef FLEX_SCANNER
+static int my_input( YY_CHAR *buf, int max_size );
+
+#undef YY_INPUT
+#define YY_INPUT(buf,result,max_size) result = my_input(buf, max_size);
+
+#undef YY_USER_INIT
+#define YY_USER_INIT init_input_source();
+
+#define FIRST ((yy_start - 1) / 2)
+#else /* FLEX_SCANNER */
+#define FIRST (yybgin - yysvec - 1)
+#endif /* FLEX_SCANNER */
%}
A [a-zA-Z_]
@@ -70,7 +86,7 @@
WS [ \t]
%%
- switch (yybgin-yysvec-1) { /* witchcraft */
+ switch (FIRST) { /* witchcraft */
case 0:
BEGIN A;
break;
@@ -116,14 +132,20 @@
<A>"$"{D}+ { yylval.cp = fieldadr(atoi(yytext+1)); RET(FIELD); }
<A>"$NF" { unputstr("(NF)"); return(INDIRECT); }
-<A>"$"{A}{B}* { int c, n;
- c = input(); unput(c);
- if (c == '(' || c == '[' || (infunc && (n=isarg(yytext+1)) >= 0)) {
- unputstr(yytext+1);
- return(INDIRECT);
+<A>"$"{A}{B}* {
+ int c;
+ char *yytext_copy = strdup(yytext);
+ c = input(); unput(c); /* look for '(' or '[' */
+ if (c == '(' || c == '[' ||
+ infunc && isarg(yytext_copy+1) >= 0) {
+ unputstr(yytext_copy+1);
+ free(yytext_copy);
+ return(INDIRECT);
} else {
- yylval.cp = setsymtab(yytext+1, "", 0.0, STR|NUM, symtab);
- RET(IVAR);
+ yylval.cp =
+ setsymtab(yytext_copy+1,"",0.0,STR|NUM,symtab);
+ free(yytext_copy);
+ RET(IVAR);
}
}
<A>"$" { RET(INDIRECT); }
@@ -173,12 +195,15 @@
<A>fflush { yylval.i = FFLUSH; RET(BLTIN); }
<A>{A}{B}* { int n, c;
+ char *yytext_copy = strdup(yytext);
c = input(); unput(c); /* look for '(' */
- if (c != '(' && infunc && (n=isarg(yytext)) >= 0) {
+ if (c != '(' && infunc && (n=isarg(yytext_copy)) >= 0) {
yylval.i = n;
+ free(yytext_copy);
RET(ARG);
} else {
- yylval.cp = setsymtab(yytext, "", 0.0, STR|NUM, symtab);
+ yylval.cp = setsymtab(yytext_copy, "", 0.0, STR|NUM, symtab);
+ free(yytext_copy);
if (c == '(') {
RET(CALL);
} else {
@@ -237,6 +262,32 @@
caddreset(gs);
}
+#ifdef FLEX_SCANNER
+static int my_input( YY_CHAR *buf, int max_size )
+{
+ extern uschar *lexprog;
+
+ if ( lexprog ) { /* awk '...' */
+ int num_chars = strlen( lexprog );
+ if ( num_chars > max_size )
+ {
+ num_chars = max_size;
+ strncpy( buf, lexprog, num_chars );
+ }
+ else
+ strcpy( buf, lexprog );
+ lexprog += num_chars;
+ return num_chars;
+
+ } else { /* awk -f ... */
+ int c = pgetc();
+ if (c == EOF)
+ return 0;
+ buf[0] = c;
+ return 1;
+ }
+}
+#else /* FLEX_SCANNER */
/* input() and unput() are transcriptions of the standard lex
macros for input and output with additions for error message
printing. God help us all if someone changes how lex works.
@@ -275,7 +326,7 @@
if (--ep < ebuf)
ep = ebuf + sizeof(ebuf) - 1;
}
-
+#endif /* FLEX_SCANNER */
void unputstr(char *s) /* put a string back on input */
{
@@ -285,6 +336,11 @@
unput(s[i]);
}
+int lex_input()
+{
+ return input();
+}
+
/* growing-string code */
const int CBUFLEN = 400;
@@ -330,3 +386,20 @@
free((void *) gs->cbuf);
free((void *) gs);
}
+
+#ifdef FLEX_SCANNER
+void init_input_source(void)
+{
+ extern int curpfile;
+ extern char *pfile[];
+
+ if (yyin == NULL) {
+ if (pfile[curpfile] == 0)
+ return;
+ if (strcmp((char *) pfile[curpfile], "-") == 0)
+ yyin = stdin;
+ else if ((yyin = fopen((char *) pfile[curpfile], "r")) == NULL)
+ ERROR "can't open file %s", pfile[curpfile] FATAL;
+ }
+}
+#endif
--- /home/millert/tmp/awk/b.c Sun Jan 19 18:06:24 1997
+++ b.c Sun Jan 19 18:00:55 1997
@@ -31,7 +31,7 @@
#include <string.h>
#include <stdlib.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
#define HAT (NCHARS-1) /* matches ^ in regular expr */
/* NCHARS is 2**n */
--- /home/millert/tmp/awk/lib.c Sun Jan 19 18:06:24 1997
+++ lib.c Sun Jan 19 18:01:45 1997
@@ -29,7 +29,7 @@
#include <errno.h>
#include <stdlib.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
FILE *infile = NULL;
char *file = "";
@@ -431,7 +431,7 @@
if (beenhere++)
return;
- while ((c = input()) != EOF && c != '\0')
+ while ((c = lex_input()) != EOF && c != '\0')
bclass(c);
bcheck2(bracecnt, '{', '}');
bcheck2(brackcnt, '[', ']');
@@ -479,6 +479,7 @@
void eprint(void) /* try to print context around error */
{
+#if 0
char *p, *q;
int c;
static int been_here = 0;
@@ -511,6 +512,7 @@
}
putc('\n', stderr);
ep = ebuf;
+#endif
}
void bclass(int c)
--- /home/millert/tmp/awk/main.c Sun Jan 19 18:06:24 1997
+++ main.c Sun Jan 19 18:00:57 1997
@@ -27,11 +27,12 @@
#define DEBUG
#include <stdio.h>
#include <ctype.h>
+#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
extern char **environ;
extern int nfields;
@@ -53,7 +54,12 @@
char *fs = NULL, *marg;
int temp;
- cmdname = argv[0];
+ setlocale(LC_ALL, "");
+
+ if ((cmdname = strrchr(argv[0], '/')) != NULL)
+ cmdname++;
+ else
+ cmdname = argv[0];
if (argc == 1) {
fprintf(stderr, "Usage: %s [-f programfile | 'program'] [-Ffieldsep] [-v var=value] [-mf n] [-mr n] [files]\n", cmdname);
exit(1);
--- /home/millert/tmp/awk/maketab.c Sun Jan 19 18:06:24 1997
+++ maketab.c Sun Jan 19 18:01:08 1997
@@ -25,14 +25,14 @@
/*
* this program makes the table to link function names
* and type indices that is used by execute() in run.c.
- * it finds the indices in ytab.h, produced by yacc.
+ * it finds the indices in awkgram.h, produced by yacc.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
struct xx
{ int token;
@@ -120,12 +120,12 @@
printf("#include <stdio.h>\n");
printf("#include \"awk.h\"\n");
- printf("#include \"ytab.h\"\n\n");
+ printf("#include \"awkgram.h\"\n\n");
for (i = SIZE; --i >= 0; )
names[i] = "";
- if ((fp = fopen("ytab.h", "r")) == NULL) {
- fprintf(stderr, "maketab can't open ytab.h!\n");
+ if ((fp = fopen("awkgram.h", "r")) == NULL) {
+ fprintf(stderr, "maketab can't open awkgram.h!\n");
exit(1);
}
printf("static char *printname[%d] = {\n", SIZE);
--- /home/millert/tmp/awk/parse.c Sun Jan 19 18:06:24 1997
+++ parse.c Sun Jan 19 18:01:11 1997
@@ -27,7 +27,7 @@
#include <string.h>
#include <stdlib.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
Node *nodealloc(int n)
{
--- /home/millert/tmp/awk/proto.h Sun Jan 19 18:06:24 1997
+++ proto.h Sun Jan 19 17:51:39 1997
@@ -22,7 +22,6 @@
USE OR PERFORMANCE OF THIS SOFTWARE.
****************************************************************/
-extern int yywrap(void);
extern void setfname(Cell *);
extern int constnode(Node *);
extern char *strnode(Node *);
@@ -31,12 +30,8 @@
extern int yylex(void);
extern void startreg(void);
-extern int input(void);
-extern void unput(int);
+extern int lex_input(void);
extern void unputstr(char *);
-extern int yylook(void);
-extern int yyback(int *, int);
-extern int yyinput(void);
extern fa *makedfa(char *, int);
extern fa *mkdfa(char *, int);
@@ -65,6 +60,7 @@
extern void freefa(fa *);
extern int pgetc(void);
+extern void init_input_source(void);
extern Node *nodealloc(int);
extern Node *exptostat(Node *);
--- /home/millert/tmp/awk/run.c Sun Jan 19 18:06:24 1997
+++ run.c Sun Jan 19 18:01:15 1997
@@ -31,7 +31,7 @@
#include <stdlib.h>
#include <time.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
#define tempfree(x) if (istemp(x)) tfree(x); else
--- /home/millert/tmp/awk/tran.c Sun Jan 19 18:06:24 1997
+++ tran.c Sun Jan 19 18:01:18 1997
@@ -29,7 +29,7 @@
#include <string.h>
#include <stdlib.h>
#include "awk.h"
-#include "ytab.h"
+#include "awkgram.h"
#define FULLTAB 2 /* rehash when table gets this x full */
#define GROWTAB 4 /* grow table by this factor */
|