/* $OpenBSD: OpenBSD-PATCHES,v 1.2 1997/08/25 16:17:08 kstailey Exp $ */ I merged the August 1997 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) setlocate(3) call added to main(). 3) slashes in argv[0] stripped from program name in main(). 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 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 , 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.2 1997/08/25 16:17:08 kstailey 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 #include #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 @@ "$"{D}+ { yylval.cp = fieldadr(atoi(yytext+1)); RET(FIELD); } "$NF" { unputstr("(NF)"); return(INDIRECT); } -"$"{A}{B}* { int c, n; - c = input(); unput(c); - if (c == '(' || c == '[' || (infunc && (n=isarg(yytext+1)) >= 0)) { - unputstr(yytext+1); - return(INDIRECT); +"$"{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); } } "$" { RET(INDIRECT); } @@ -173,12 +195,15 @@ fflush { yylval.i = FFLUSH; RET(BLTIN); } {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 #include #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 #include #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 #include +#include #include #include #include #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 #include #include #include "awk.h" -#include "ytab.h" +#include "awkgram.h" struct xx { int token; @@ -120,12 +120,12 @@ printf("#include \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 #include #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 #include #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 #include #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 */