Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/dist/nawk add GNU awk compatible functions systime(), strfti...



details:   https://anonhg.NetBSD.org/src/rev/10d58f461b5f
branches:  trunk
changeset: 503567:10d58f461b5f
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Thu Feb 08 20:42:39 2001 +0000

description:
add GNU awk compatible functions systime(), strftime() and gensub()
XXX gensub() doesn't handle backreferences (\0 .... \9) yet

diffstat:

 dist/nawk/awk.h     |    2 +
 dist/nawk/awkgram.y |   18 +++++-
 dist/nawk/lex.c     |    3 +
 dist/nawk/maketab.c |    1 +
 dist/nawk/parse.c   |   23 +++++++
 dist/nawk/proctab.c |   30 +++++----
 dist/nawk/proto.h   |    5 +-
 dist/nawk/run.c     |  159 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 8 files changed, 220 insertions(+), 21 deletions(-)

diffs (truncated from 416 to 300 lines):

diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/awk.h
--- a/dist/nawk/awk.h   Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/awk.h   Thu Feb 08 20:42:39 2001 +0000
@@ -124,6 +124,8 @@
 #define        FTOUPPER 12
 #define        FTOLOWER 13
 #define        FFLUSH  14
+#define FSYSTIME       15
+#define FSTRFTIME      16
 
 /* Node:  parse tree is made of nodes, with Cell's at bottom */
 
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/awkgram.y
--- a/dist/nawk/awkgram.y       Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/awkgram.y       Thu Feb 08 20:42:39 2001 +0000
@@ -80,7 +80,7 @@
 %left  GETLINE
 %nonassoc APPEND EQ GE GT LE LT NE MATCHOP IN '|'
 %left  ARG BLTIN BREAK CALL CLOSE CONTINUE DELETE DO EXIT FOR FUNC 
-%left  GSUB IF INDEX LSUBSTR MATCHFCN NEXT NUMBER
+%left  GENSUB GSUB IF INDEX LSUBSTR MATCHFCN NEXT NUMBER
 %left  PRINT PRINTF RETURN SPLIT SPRINTF STRING SUB SUBSTR
 %left  REGEXPR VAR VARNF IVAR WHILE '('
 %left  CAT
@@ -369,6 +369,22 @@
        | INCR var                      { $$ = op1(PREINCR, $2); }
        | var DECR                      { $$ = op1(POSTDECR, $1); }
        | var INCR                      { $$ = op1(POSTINCR, $1); }
+       | GENSUB '(' reg_expr comma pattern comma pattern ')'
+               { $$ = op5(GENSUB, NIL, (Node*)makedfa($3, 1), $5, $7, rectonode()); }
+       | GENSUB '(' pattern comma pattern comma pattern ')'
+               { if (constnode($3))
+                       $$ = op5(GENSUB, NIL, (Node *)makedfa(strnode($3), 1), $5, $7, rectonode());
+                 else
+                       $$ = op5(GENSUB, (Node *)1, $3, $5, $7, rectonode());
+               }
+       | GENSUB '(' reg_expr comma pattern comma pattern comma pattern ')'
+               { $$ = op5(GENSUB, NIL, (Node*)makedfa($3, 1), $5, $7, $9); }
+       | GENSUB '(' pattern comma pattern comma pattern comma pattern ')'
+               { if (constnode($3))
+                       $$ = op5(GENSUB, NIL, (Node *)makedfa(strnode($3),1), $5,$7,$9);
+                 else
+                       $$ = op5(GENSUB, (Node *)1, $3, $5, $7, $9);
+               }
        | GETLINE var LT term           { $$ = op3(GETLINE, $2, itonp($3), $4); }
        | GETLINE LT term               { $$ = op3(GETLINE, NIL, itonp($2), $3); }
        | GETLINE var                   { $$ = op3(GETLINE, $2, NIL, NIL); }
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/lex.c
--- a/dist/nawk/lex.c   Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/lex.c   Thu Feb 08 20:42:39 2001 +0000
@@ -65,6 +65,7 @@
        { "for",        FOR,            FOR },
        { "func",       FUNC,           FUNC },
        { "function",   FUNC,           FUNC },
+       { "gensub",     GENSUB,         GENSUB },
        { "getline",    GETLINE,        GETLINE },
        { "gsub",       GSUB,           GSUB },
        { "if",         IF,             IF },
@@ -85,9 +86,11 @@
        { "sprintf",    SPRINTF,        SPRINTF },
        { "sqrt",       FSQRT,          BLTIN },
        { "srand",      FSRAND,         BLTIN },
+       { "strftime",   FSTRFTIME,      BLTIN },
        { "sub",        SUB,            SUB },
        { "substr",     SUBSTR,         SUBSTR },
        { "system",     FSYSTEM,        BLTIN },
+       { "systime",    FSYSTIME,       BLTIN },
        { "tolower",    FTOLOWER,       BLTIN },
        { "toupper",    FTOUPPER,       BLTIN },
        { "while",      WHILE,          WHILE },
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/maketab.c
--- a/dist/nawk/maketab.c       Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/maketab.c       Thu Feb 08 20:42:39 2001 +0000
@@ -103,6 +103,7 @@
        { ARG, "arg", "arg" },
        { VARNF, "getnf", "NF" },
        { GETLINE, "getline", "getline" },
+       { GENSUB, "gensub", "gensub" },
        { 0, "", "" },
 };
 
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/parse.c
--- a/dist/nawk/parse.c Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/parse.c Thu Feb 08 20:42:39 2001 +0000
@@ -93,6 +93,20 @@
        return(x);
 }
 
+Node *node5(int a, Node *b, Node *c, Node *d, Node *e, Node *f)
+{
+       Node *x;
+
+       x = nodealloc(5);
+       x->nobj = a;
+       x->narg[0] = b;
+       x->narg[1] = c;
+       x->narg[2] = d;
+       x->narg[3] = e;
+       x->narg[4] = f;
+       return(x);
+}
+
 Node *stat1(int a, Node *b)
 {
        Node *x;
@@ -165,6 +179,15 @@
        return(x);
 }
 
+Node *op5(int a, Node *b, Node *c, Node *d, Node *e, Node *f)
+{
+       Node *x;
+
+       x = node5(a,b,c,d,e, f);
+       x->ntype = NEXPR;
+       return(x);
+}
+
 Node *celltonode(Cell *a, int b)
 {
        Node *x;
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/proctab.c
--- a/dist/nawk/proctab.c       Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/proctab.c       Thu Feb 08 20:42:39 2001 +0000
@@ -2,7 +2,7 @@
 #include "awk.h"
 #include "awkgram.h"
 
-static const char * const printname[92] = {
+static const char * const printname[93] = {
        "FIRSTTOKEN",   /* 257 */
        "PROGRAM",      /* 258 */
        "PASTAT",       /* 259 */
@@ -83,22 +83,23 @@
        "STRING",       /* 334 */
        "REGEXPR",      /* 335 */
        "GETLINE",      /* 336 */
-       "RETURN",       /* 337 */
-       "SPLIT",        /* 338 */
-       "SUBSTR",       /* 339 */
-       "WHILE",        /* 340 */
-       "CAT",  /* 341 */
-       "NOT",  /* 342 */
-       "UMINUS",       /* 343 */
-       "POWER",        /* 344 */
-       "DECR", /* 345 */
-       "INCR", /* 346 */
-       "INDIRECT",     /* 347 */
-       "LASTTOKEN",    /* 348 */
+       "GENSUB",       /* 337 */
+       "RETURN",       /* 338 */
+       "SPLIT",        /* 339 */
+       "SUBSTR",       /* 340 */
+       "WHILE",        /* 341 */
+       "CAT",  /* 342 */
+       "NOT",  /* 343 */
+       "UMINUS",       /* 344 */
+       "POWER",        /* 345 */
+       "DECR", /* 346 */
+       "INCR", /* 347 */
+       "INDIRECT",     /* 348 */
+       "LASTTOKEN",    /* 349 */
 };
 
 
-Cell *(*proctab[92])(Node **, int) = {
+Cell *(*proctab[93])(Node **, int) = {
        nullproc,       /* FIRSTTOKEN */
        program,        /* PROGRAM */
        pastat, /* PASTAT */
@@ -179,6 +180,7 @@
        nullproc,       /* STRING */
        nullproc,       /* REGEXPR */
        getline,        /* GETLINE */
+       gensub, /* GENSUB */
        jump,   /* RETURN */
        split,  /* SPLIT */
        substr, /* SUBSTR */
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/proto.h
--- a/dist/nawk/proto.h Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/proto.h Thu Feb 08 20:42:39 2001 +0000
@@ -73,12 +73,14 @@
 extern Node    *node2(int, Node *, Node *);
 extern Node    *node3(int, Node *, Node *, Node *);
 extern Node    *node4(int, Node *, Node *, Node *, Node *);
+extern Node    *node5(int, Node *, 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    *op5(int, Node *, Node *, Node *, Node *, Node *);
 extern Node    *stat2(int, Node *, Node *);
 extern Node    *stat4(int, Node *, Node *, Node *, Node *);
 extern Node    *celltonode(Cell *, int);
@@ -184,11 +186,12 @@
 extern Cell    *nullproc(Node **, int);
 extern FILE    *redirect(int, Node *);
 extern FILE    *openfile(int, char *);
-extern char    *filename(FILE *);
+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 Cell    *gensub(Node **, int);
 
 extern FILE    *popen(const char *, const char *);
 extern int     pclose(FILE *);
diff -r b6ab67567859 -r 10d58f461b5f dist/nawk/run.c
--- a/dist/nawk/run.c   Thu Feb 08 20:27:24 2001 +0000
+++ b/dist/nawk/run.c   Thu Feb 08 20:42:39 2001 +0000
@@ -217,7 +217,7 @@
 
 Cell *call(Node **a, int n)    /* function call.  very kludgy and fragile */
 {
-       static Cell newcopycell = { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE };
+       static const Cell newcopycell = { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE };
        int i, ncall, ndef;
        Node *x;
        Cell *args[NARGS], *oargs[NARGS];       /* BUG: fixed size arrays */
@@ -1444,10 +1444,12 @@
 {
        Cell *x, *y;
        Awkfloat u;
-       int t;
-       char *p, *buf;
+       int t, sz;
+       char *p, *buf, *fmt;
        Node *nextarg;
        FILE *fp;
+       time_t tv;
+       struct tm *tm;
 
        t = ptoi(a[0]);
        x = execute(a[1]);
@@ -1516,6 +1518,35 @@
                else
                        u = fflush(fp);
                break;
+       case FSYSTIME:
+               u = time((time_t *) 0); break;
+       case FSTRFTIME:
+               /* strftime([format [,timestamp]]) */
+               if (nextarg) {
+                       y = execute(nextarg), nextarg = nextarg->nnext;
+                       tv = (time_t) getfval(y);
+                       tempfree(y);
+               } else
+                       tv = time((time_t *) 0);
+               tm = localtime(&tv);
+
+               if (isrec(x)) {
+                       /* format argument not provided, use default */
+                       fmt = "%a %b %d %H:%M:%S %Z %Y";
+               } else
+                       fmt = tostring(getsval(x));
+
+               sz = 32, buf = NULL;
+               do {
+                       if ((buf = realloc(buf, (sz *= 2))) == NULL)
+                               FATAL("out of memory in strftime");
+               } while(strftime(buf, sz, fmt, tm) == 0);
+
+               y = gettemp();
+               setsval(y, buf);
+               free(buf);
+
+               return y;
        default:        /* can't happen */
                FATAL("illegal function type %d", t);
                break;
@@ -1582,7 +1613,7 @@
 
 struct files {
        FILE    *fp;
-       char    *fname;
+       const char      *fname;
        int     mode;   /* '|', 'a', 'w' => LE/LT, GT */
 } files[FOPEN_MAX] ={
        { NULL,  "/dev/stdin",  LT },   /* watch out: don't free this! */
@@ -1643,7 +1674,7 @@
        return fp;
 }
 
-char *filename(FILE *fp)
+const char *filename(FILE *fp)
 {
        int i;
 
@@ -1864,6 +1895,124 @@
        return(x);
 }
 
+Cell *gensub(Node **a, int nnn)        /* global selective substitute */
+       /* XXX incomplete - doesn't support backreferences \0 ... \9 */
+{
+       Cell *x, *y, *res, *h;
+       char *rptr, *sptr, *t, *pb, *q;



Home | Main Index | Thread Index | Old Index