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