Print this page
3731 Update nawk to version 20121220

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/awk/parse.c
          +++ new/usr/src/cmd/awk/parse.c
↓ open down ↓ 17 lines elided ↑ open up ↑
  18   18   * information: Portions Copyright [yyyy] [name of copyright owner]
  19   19   *
  20   20   * CDDL HEADER END
  21   21   */
  22   22  
  23   23  /*
  24   24   * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  25   25   * Use is subject to license terms.
  26   26   */
  27   27  
  28      -/*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  29      -/*        All Rights Reserved   */
  30      -
  31      -#pragma ident   "%Z%%M% %I%     %E% SMI"
       28 +/*
       29 + * Copyright (C) Lucent Technologies 1997
       30 + * All Rights Reserved
       31 + *
       32 + * Permission to use, copy, modify, and distribute this software and
       33 + * its documentation for any purpose and without fee is hereby
       34 + * granted, provided that the above copyright notice appear in all
       35 + * copies and that both that the copyright notice and this
       36 + * permission notice and warranty disclaimer appear in supporting
       37 + * documentation, and that the name Lucent Technologies or any of
       38 + * its entities not be used in advertising or publicity pertaining
       39 + * to distribution of the software without specific, written prior
       40 + * permission.
       41 + *
       42 + * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
       43 + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
       44 + * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
       45 + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
       46 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
       47 + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
       48 + * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
       49 + * THIS SOFTWARE.
       50 + */
  32   51  
  33   52  #define DEBUG
  34   53  #include "awk.h"
  35   54  #include "y.tab.h"
  36   55  
  37   56  Node *
  38   57  nodealloc(int n)
  39   58  {
  40      -        register Node *x;
       59 +        Node *x;
  41   60  
  42   61          x = (Node *)malloc(sizeof (Node) + (n - 1) * sizeof (Node *));
  43   62          if (x == NULL)
  44      -                ERROR "out of space in nodealloc" FATAL;
       63 +                FATAL("out of space in nodealloc");
  45   64          x->nnext = NULL;
  46   65          x->lineno = lineno;
  47   66          return (x);
  48   67  }
  49   68  
  50   69  Node *
  51   70  exptostat(Node *a)
  52   71  {
  53   72          a->ntype = NSTAT;
  54   73          return (a);
  55   74  }
  56   75  
  57   76  Node *
  58   77  node1(int a, Node *b)
  59   78  {
  60      -        register Node *x;
       79 +        Node *x;
  61   80  
  62   81          x = nodealloc(1);
  63   82          x->nobj = a;
  64   83          x->narg[0] = b;
  65   84          return (x);
  66   85  }
  67   86  
  68   87  Node *
  69   88  node2(int a, Node *b, Node *c)
  70   89  {
  71      -        register Node *x;
       90 +        Node *x;
  72   91  
  73   92          x = nodealloc(2);
  74   93          x->nobj = a;
  75   94          x->narg[0] = b;
  76   95          x->narg[1] = c;
  77   96          return (x);
  78   97  }
  79   98  
  80   99  Node *
  81  100  node3(int a, Node *b, Node *c, Node *d)
  82  101  {
  83      -        register Node *x;
      102 +        Node *x;
  84  103  
  85  104          x = nodealloc(3);
  86  105          x->nobj = a;
  87  106          x->narg[0] = b;
  88  107          x->narg[1] = c;
  89  108          x->narg[2] = d;
  90  109          return (x);
  91  110  }
  92  111  
  93  112  Node *
  94  113  node4(int a, Node *b, Node *c, Node *d, Node *e)
  95  114  {
  96      -        register Node *x;
      115 +        Node *x;
  97  116          x = nodealloc(4);
  98  117          x->nobj = a;
  99  118          x->narg[0] = b;
 100  119          x->narg[1] = c;
 101  120          x->narg[2] = d;
 102  121          x->narg[3] = e;
 103  122          return (x);
 104  123  }
 105  124  
 106  125  Node *
 107      -stat3(int a, Node *b, Node *c, Node *d)
      126 +stat1(int a, Node *b)
 108  127  {
 109      -        register Node *x;
      128 +        Node *x;
 110  129  
 111      -        x = node3(a, b, c, d);
      130 +        x = node1(a, b);
 112  131          x->ntype = NSTAT;
 113  132          return (x);
 114  133  }
 115  134  
 116  135  Node *
 117      -op2(int a, Node *b, Node *c)
      136 +stat2(int a, Node *b, Node *c)
 118  137  {
 119      -        register Node *x;
      138 +        Node *x;
 120  139  
 121  140          x = node2(a, b, c);
 122      -        x->ntype = NEXPR;
      141 +        x->ntype = NSTAT;
 123  142          return (x);
 124  143  }
 125  144  
 126  145  Node *
 127      -op1(int a, Node *b)
      146 +stat3(int a, Node *b, Node *c, Node *d)
 128  147  {
 129      -        register Node *x;
      148 +        Node *x;
 130  149  
 131      -        x = node1(a, b);
 132      -        x->ntype = NEXPR;
      150 +        x = node3(a, b, c, d);
      151 +        x->ntype = NSTAT;
 133  152          return (x);
 134  153  }
 135  154  
 136  155  Node *
 137      -stat1(int a, Node *b)
      156 +stat4(int a, Node *b, Node *c, Node *d, Node *e)
 138  157  {
 139      -        register Node *x;
      158 +        Node *x;
 140  159  
 141      -        x = node1(a, b);
      160 +        x = node4(a, b, c, d, e);
 142  161          x->ntype = NSTAT;
 143  162          return (x);
 144  163  }
 145  164  
 146  165  Node *
 147      -op3(int a, Node *b, Node *c, Node *d)
      166 +op1(int a, Node *b)
 148  167  {
 149      -        register Node *x;
      168 +        Node *x;
 150  169  
 151      -        x = node3(a, b, c, d);
      170 +        x = node1(a, b);
 152  171          x->ntype = NEXPR;
 153  172          return (x);
 154  173  }
 155  174  
 156  175  Node *
 157      -op4(int a, Node *b, Node *c, Node *d, Node *e)
      176 +op2(int a, Node *b, Node *c)
 158  177  {
 159      -        register Node *x;
      178 +        Node *x;
 160  179  
 161      -        x = node4(a, b, c, d, e);
      180 +        x = node2(a, b, c);
 162  181          x->ntype = NEXPR;
 163  182          return (x);
 164  183  }
 165  184  
 166  185  Node *
 167      -stat2(int a, Node *b, Node *c)
      186 +op3(int a, Node *b, Node *c, Node *d)
 168  187  {
 169      -        register Node *x;
      188 +        Node *x;
 170  189  
 171      -        x = node2(a, b, c);
 172      -        x->ntype = NSTAT;
      190 +        x = node3(a, b, c, d);
      191 +        x->ntype = NEXPR;
 173  192          return (x);
 174  193  }
 175  194  
 176  195  Node *
 177      -stat4(int a, Node *b, Node *c, Node *d, Node *e)
      196 +op4(int a, Node *b, Node *c, Node *d, Node *e)
 178  197  {
 179      -        register Node *x;
      198 +        Node *x;
 180  199  
 181  200          x = node4(a, b, c, d, e);
 182      -        x->ntype = NSTAT;
      201 +        x->ntype = NEXPR;
 183  202          return (x);
 184  203  }
 185  204  
 186      -Node *
 187      -valtonode(Cell *a, int b)
      205 +Node
      206 +*celltonode(Cell *a, int b)
 188  207  {
 189      -        register Node *x;
      208 +        Node *x;
 190  209  
 191  210          a->ctype = OCELL;
 192  211          a->csub = b;
 193  212          x = node1(0, (Node *)a);
 194  213          x->ntype = NVALUE;
 195  214          return (x);
 196  215  }
 197  216  
 198      -Node *
 199      -rectonode(void)
      217 +Node
      218 +*rectonode(void)        /* make $0 into a Node */
 200  219  {
 201      -        /* return valtonode(lookup("$0", symtab), CFLD); */
 202      -        return (valtonode(recloc, CFLD));
      220 +        extern Cell *literal0;
      221 +        return (op1(INDIRECT, celltonode(literal0, CUNK)));
 203  222  }
 204  223  
 205  224  Node *
 206  225  makearr(Node *p)
 207  226  {
 208  227          Cell *cp;
 209  228  
 210  229          if (isvalue(p)) {
 211  230                  cp = (Cell *)(p->narg[0]);
 212      -                if (isfunc(cp))
 213      -                        ERROR "%s is a function, not an array", cp->nval SYNTAX;
      231 +                if (isfcn(cp))
      232 +                        SYNTAX("%s is a function, not an array", cp->nval);
 214  233                  else if (!isarr(cp)) {
 215  234                          xfree(cp->sval);
 216  235                          cp->sval = (uchar *)makesymtab(NSYMTAB);
 217  236                          cp->tval = ARR;
 218  237                  }
 219  238          }
 220  239          return (p);
 221  240  }
 222  241  
      242 +/*
      243 + * The One True Awk sets PA2NUM to 50, which is not enough for webrev(1).
      244 + */
      245 +#define PA2NUM  250     /* max number of pat,pat patterns allowed */
      246 +int     paircnt;                /* number of them in use */
      247 +int     pairstack[PA2NUM];      /* state of each pat,pat */
      248 +
 223  249  Node *
 224      -pa2stat(Node *a, Node *b, Node *c)
      250 +pa2stat(Node *a, Node *b, Node *c)      /* pat, pat {...} */
 225  251  {
 226      -        register Node *x;
      252 +        Node *x;
 227  253  
 228      -        x = node4(PASTAT2, a, b, c, (Node *)paircnt);
 229      -        paircnt++;
      254 +        x = node4(PASTAT2, a, b, c, itonp(paircnt));
      255 +        if (paircnt++ >= PA2NUM)
      256 +                SYNTAX("limited to %d pat,pat statements", PA2NUM);
 230  257          x->ntype = NSTAT;
 231  258          return (x);
 232  259  }
 233  260  
 234  261  Node *
 235  262  linkum(Node *a, Node *b)
 236  263  {
 237      -        register Node *c;
      264 +        Node *c;
 238  265  
 239  266          if (errorflag)  /* don't link things that are wrong */
 240  267                  return (a);
 241  268          if (a == NULL)
 242  269                  return (b);
 243  270          else if (b == NULL)
 244  271                  return (a);
 245  272          for (c = a; c->nnext != NULL; c = c->nnext)
 246  273                  ;
 247  274          c->nnext = b;
 248  275          return (a);
 249  276  }
 250  277  
 251  278  void
 252  279  defn(Cell *v, Node *vl, Node *st)       /* turn on FCN bit in definition */
 253      -{
      280 +{                                       /*   body of function, arglist */
 254  281          Node *p;
 255  282          int n;
 256  283  
 257  284          if (isarr(v)) {
 258      -                ERROR "`%s' is an array name and a function name",
 259      -                    v->nval SYNTAX;
      285 +                SYNTAX("`%s' is an array name and a function name",
      286 +                    v->nval);
      287 +                return;
      288 +        }
      289 +        if (isarg(v->nval) != -1) {
      290 +                SYNTAX("`%s' is both function name and argument name",
      291 +                    v->nval);
 260  292                  return;
 261  293          }
      294 +
 262  295          v->tval = FCN;
 263  296          v->sval = (uchar *)st;
 264  297          n = 0;  /* count arguments */
 265  298          for (p = vl; p; p = p->nnext)
 266  299                  n++;
 267  300          v->fval = n;
 268  301          dprintf(("defining func %s (%d args)\n", v->nval, n));
 269  302  }
 270  303  
 271  304  int
 272      -isarg(uchar *s) /* is s in argument list for current function? */
 273      -{
      305 +isarg(const uchar *s)   /* is s in argument list for current function? */
      306 +{               /* return -1 if not, otherwise arg # */
 274  307          extern Node *arglist;
 275  308          Node *p = arglist;
 276  309          int n;
 277  310  
 278  311          for (n = 0; p != 0; p = p->nnext, n++) {
 279  312                  if (strcmp((char *)((Cell *)(p->narg[0]))->nval,
 280  313                      (char *)s) == 0) {
 281  314                          return (n);
 282  315                  }
 283  316          }
 284  317          return (-1);
 285  318  }
      319 +
      320 +int
      321 +ptoi(void *p)           /* convert pointer to integer */
      322 +{
      323 +        return ((int)(long)p);  /* swearing that p fits, of course */
      324 +}
      325 +
      326 +Node
      327 +*itonp(int i)           /* and vice versa */
      328 +{
      329 +        return ((Node *)(long)i);
      330 +}
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX