Skip to content

Commit

Permalink
Upgrade the One True Awk
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Apr 7, 2024
1 parent b9d6e6e commit 49a3213
Show file tree
Hide file tree
Showing 15 changed files with 5,178 additions and 4,220 deletions.
3 changes: 1 addition & 2 deletions third_party/awk/BUILD.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ THIRD_PARTY_AWK_A_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_AWK_A_DIRECTDEPS),$($(x))))

THIRD_PARTY_AWK_CHECKS = \
$(THIRD_PARTY_AWK_A).pkg \
$(THIRD_PARTY_AWK_HDRS:%=o/$(MODE)/%.ok)
$(THIRD_PARTY_AWK_A).pkg

$(THIRD_PARTY_AWK_A): \
third_party/awk/ \
Expand Down
5 changes: 2 additions & 3 deletions third_party/awk/README.cosmo
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ SOURCE

https://github.com/onetrueawk/awk

commit b92d8cecd132ce8e02a373e28dd42e6be34d3d59
Author: ozan yigit <ozan.yigit@gmail.com>
Date: Mon May 30 11:34:52 2022 -0400
commit 2bab10b60b3f4d3bbefdcb60410655f317d1a452
date Thu Mar 14 14:25:30 2024 +0200

added YACC definition for byacc.
246 changes: 54 additions & 192 deletions third_party/awk/awk.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_AWK_AWK_H_
#define COSMOPOLITAN_THIRD_PARTY_AWK_AWK_H_
#include "libc/assert.h"
#include "libc/limits.h"
#include "libc/literal.h"
#include "libc/stdio/stdio.h"
COSMOPOLITAN_C_START_
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name Lucent Technologies or any of
its entities not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission.
LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#if __STDC_VERSION__ <= 199901L
#define noreturn
#else
#include <stdnoreturn.h>
#endif

typedef double Awkfloat;

/* unsigned char is more trouble than it's worth */

typedef unsigned char uschar;

#define xfree(a) { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
#define xfree(a) { free((void *)(intptr_t)(a)); (a) = NULL; }
/*
* We sometimes cheat writing read-only pointers to NUL-terminate them
* and then put back the original value
Expand All @@ -39,6 +64,8 @@ extern bool safe; /* false => unsafe, true => safe */
#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
extern int recsize; /* size of current record, orig RECSIZE */

extern size_t awk_mb_cur_max; /* max size of a multi-byte character */

extern char EMPTY[]; /* this avoid -Wwritable-strings issues */
extern char **FS;
extern char **RS;
Expand All @@ -53,6 +80,8 @@ extern char **SUBSEP;
extern Awkfloat *RSTART;
extern Awkfloat *RLENGTH;

extern bool CSV; /* true for csv input */

extern char *record; /* points to $0 */
extern int lineno; /* line number in awk program */
extern int errorflag; /* 1 if error has occurred */
Expand Down Expand Up @@ -200,7 +229,8 @@ extern int pairstack[], paircnt;

/* structures used by regular expression matching machinery, mostly b.c: */

#define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
#define NCHARS (1256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
/* BUG: some overflows (caught) if we use 256 */
/* watch out in match(), etc. */
#define HAT (NCHARS+2) /* matches ^ in regular expr */
#define NSTATES 32
Expand All @@ -211,12 +241,24 @@ typedef struct rrow {
int i;
Node *np;
uschar *up;
int *rp; /* rune representation of char class */
} lval; /* because Al stores a pointer in it! */
int *lfollow;
} rrow;

typedef struct gtte { /* gototab entry */
unsigned int ch;
unsigned int state;
} gtte;

typedef struct gtt { /* gototab */
size_t allocated;
size_t inuse;
gtte *entries;
} gtt;

typedef struct fa {
unsigned int **gototab;
gtt *gototab;
uschar *out;
uschar *restr;
int **posns;
Expand All @@ -229,185 +271,5 @@ typedef struct fa {
struct rrow re[1]; /* variable: actual size set by calling malloc */
} fa;

extern int yywrap(void);
extern void setfname(Cell *);
extern int constnode(Node *);
extern char *strnode(Node *);
extern Node *notnull(Node *);
extern int yyparse(void);

extern int yylex(void);
extern void startreg(void);
extern int input(void);
extern void unput(int);
extern void unputstr(const char *);
extern int yylook(void);
extern int yyback(int *, int);
extern int yyinput(void);

extern fa *makedfa(const char *, bool);
extern fa *mkdfa(const char *, bool);
extern int makeinit(fa *, bool);
extern void penter(Node *);
extern void freetr(Node *);
extern int hexstr(const uschar **);
extern int quoted(const uschar **);
extern char *cclenter(const char *);
extern wontreturn void overflo(const char *);
extern void cfoll(fa *, Node *);
extern int first(Node *);
extern void follow(Node *);
extern int member(int, const char *);
extern int match(fa *, const char *);
extern int pmatch(fa *, const char *);
extern int nematch(fa *, const char *);
extern bool fnematch(fa *, FILE *, char **, int *, int);
extern Node *reparse(const char *);
extern Node *regexp(void);
extern Node *primary(void);
extern Node *concat(Node *);
extern Node *alt(Node *);
extern Node *unary(Node *);
extern int relex(void);
extern int cgoto(fa *, int, int);
extern void freefa(fa *);

extern int pgetc(void);
extern char *cursource(void);

extern Node *nodealloc(int);
extern Node *exptostat(Node *);
extern Node *node1(int, Node *);
extern Node *node2(int, Node *, Node *);
extern Node *node3(int, Node *, Node *, Node *);
extern Node *node4(int, Node *, Node *, Node *, Node *);
extern Node *stat3(int, Node *, Node *, Node *);
extern Node *op2(int, Node *, Node *);
extern Node *op1(int, Node *);
extern Node *stat1(int, Node *);
extern Node *op3(int, Node *, Node *, Node *);
extern Node *op4(int, Node *, Node *, Node *, Node *);
extern Node *stat2(int, Node *, Node *);
extern Node *stat4(int, Node *, Node *, Node *, Node *);
extern Node *celltonode(Cell *, int);
extern Node *rectonode(void);
extern Node *makearr(Node *);
extern Node *pa2stat(Node *, Node *, Node *);
extern Node *linkum(Node *, Node *);
extern void defn(Cell *, Node *, Node *);
extern int isarg(const char *);
extern const char *tokname(int);
extern Cell *(*proctab[])(Node **, int);
extern int ptoi(void *);
extern Node *itonp(int);

extern void syminit(void);
extern void arginit(int, char **);
extern void envinit(char **);
extern Array *makesymtab(int);
extern void freesymtab(Cell *);
extern void freeelem(Cell *, const char *);
extern Cell *setsymtab(const char *, const char *, double, unsigned int, Array *);
extern int hash(const char *, int);
extern void rehash(Array *);
extern Cell *lookup(const char *, Array *);
extern double setfval(Cell *, double);
extern void funnyvar(Cell *, const char *);
extern char *setsval(Cell *, const char *);
extern double getfval(Cell *);
extern char *getsval(Cell *);
extern char *getpssval(Cell *); /* for print */
extern char *tostring(const char *);
extern char *tostringN(const char *, size_t);
extern char *qstring(const char *, int);
extern Cell *catstr(Cell *, Cell *);

extern void recinit(unsigned int);
extern void initgetrec(void);
extern void makefields(int, int);
extern void growfldtab(int n);
extern void savefs(void);
extern int getrec(char **, int *, bool);
extern void nextfile(void);
extern int readrec(char **buf, int *bufsize, FILE *inf, bool isnew);
extern char *getargv(int);
extern void setclvar(char *);
extern void fldbld(void);
extern void cleanfld(int, int);
extern void newfld(int);
extern void setlastfld(int);
extern int refldbld(const char *, const char *);
extern void recbld(void);
extern Cell *fieldadr(int);
extern void yyerror(const char *);
extern void bracecheck(void);
extern void bcheck2(int, int, int);
extern void SYNTAX(const char *, ...)
__attribute__((__format__(__printf__, 1, 2)));
extern wontreturn void FATAL(const char *, ...)
__attribute__((__format__(__printf__, 1, 2)));
extern void WARNING(const char *, ...)
__attribute__((__format__(__printf__, 1, 2)));
extern void error(void);
extern void eprint(void);
extern void bclass(int);
extern double errcheck(double, const char *);
extern int isclvar(const char *);
extern bool is_valid_number(const char *s, bool trailing_stuff_ok,
bool *no_trailing, double *result);
#define is_number(s, val) is_valid_number(s, false, NULL, val)

extern int adjbuf(char **pb, int *sz, int min, int q, char **pbp, const char *what);
extern void run(Node *);
extern Cell *execute(Node *);
extern Cell *program(Node **, int);
extern Cell *call(Node **, int);
extern Cell *copycell(Cell *);
extern Cell *arg(Node **, int);
extern Cell *jump(Node **, int);
extern Cell *awkgetline(Node **, int);
extern Cell *getnf(Node **, int);
extern Cell *array(Node **, int);
extern Cell *awkdelete(Node **, int);
extern Cell *intest(Node **, int);
extern Cell *matchop(Node **, int);
extern Cell *boolop(Node **, int);
extern Cell *relop(Node **, int);
extern void tfree(Cell *);
extern Cell *gettemp(void);
extern Cell *field(Node **, int);
extern Cell *indirect(Node **, int);
extern Cell *substr(Node **, int);
extern Cell *sindex(Node **, int);
extern int format(char **, int *, const char *, Node *);
extern Cell *awksprintf(Node **, int);
extern Cell *awkprintf(Node **, int);
extern Cell *arith(Node **, int);
extern double ipow(double, int);
extern Cell *incrdecr(Node **, int);
extern Cell *assign(Node **, int);
extern Cell *cat(Node **, int);
extern Cell *pastat(Node **, int);
extern Cell *dopa2(Node **, int);
extern Cell *split(Node **, int);
extern Cell *condexpr(Node **, int);
extern Cell *ifstat(Node **, int);
extern Cell *whilestat(Node **, int);
extern Cell *dostat(Node **, int);
extern Cell *forstat(Node **, int);
extern Cell *instat(Node **, int);
extern Cell *bltin(Node **, int);
extern Cell *printstat(Node **, int);
extern Cell *nullproc(Node **, int);
extern FILE *redirect(int, Node *);
extern FILE *openfile(int, const char *, bool *);
extern const char *filename(FILE *);
extern Cell *closefile(Node **, int);
extern void closeall(void);
extern Cell *sub(Node **, int);
extern Cell *gsub(Node **, int);

extern const char *flags2str(int flags);

COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_THIRD_PARTY_AWK_AWK_H_ */

#include "proto.h"
Loading

0 comments on commit 49a3213

Please sign in to comment.