Print this page
3731 Update nawk to version 20121220

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/awk/main.c
          +++ new/usr/src/cmd/awk/main.c
↓ open down ↓ 16 lines elided ↑ open up ↑
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
  25   25   */
  26   26  
  27      -/*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  28      -/*        All Rights Reserved   */
       27 +/*
       28 + * Copyright (C) Lucent Technologies 1997
       29 + * All Rights Reserved
       30 + *
       31 + * Permission to use, copy, modify, and distribute this software and
       32 + * its documentation for any purpose and without fee is hereby
       33 + * granted, provided that the above copyright notice appear in all
       34 + * copies and that both that the copyright notice and this
       35 + * permission notice and warranty disclaimer appear in supporting
       36 + * documentation, and that the name Lucent Technologies or any of
       37 + * its entities not be used in advertising or publicity pertaining
       38 + * to distribution of the software without specific, written prior
       39 + * permission.
       40 + *
       41 + * LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
       42 + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
       43 + * IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
       44 + * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
       45 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
       46 + * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
       47 + * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
       48 + * THIS SOFTWARE.
       49 + */
  29   50  
  30      -#include <stdio.h>
  31      -#include <ctype.h>
  32   51  #include <signal.h>
  33   52  #include <locale.h>
  34      -#include <libintl.h>
  35   53  #include <stdarg.h>
  36   54  #include <errno.h>
  37   55  #include <values.h>
  38   56  #include <langinfo.h>
  39   57  #include "awk.h"
  40   58  #include "y.tab.h"
  41   59  
  42      -char    *version = "version Oct 11, 1989";
       60 +char    *version = "version 20121220";
  43   61  
  44   62  int     dbg     = 0;
       63 +Awkfloat        srand_seed = 1;
  45   64  uchar   *cmdname;       /* gets argv[0] for error messages */
  46   65  uchar   *lexprog;       /* points to program argument if it exists */
  47   66  int     compile_time = 2;       /* for error printing: */
  48   67                                  /* 2 = cmdline, 1 = compile, 0 = running */
  49   68  char    radixpoint = '.';
  50   69  
  51      -static uchar    **pfile = NULL; /* program filenames from -f's */
       70 +#define MAX_PFILE       20      /* max number of -f's */
       71 +
       72 +static uchar    *pfile[MAX_PFILE];      /* program filenames from -f's */
  52   73  static int      npfile = 0;     /* number of filenames */
  53   74  static int      curpfile = 0;   /* current filename */
  54   75  
       76 +int     safe = 0;       /* 1 => "safe" mode */
       77 +
  55   78  int
  56   79  main(int argc, char *argv[], char *envp[])
  57   80  {
  58      -        uchar *fs = NULL;
       81 +        const uchar *fs = NULL;
  59   82          char    *nl_radix;
  60   83          /*
  61   84           * At this point, numbers are still scanned as in
  62   85           * the POSIX locale.
  63   86           * (POSIX.2, volume 2, P867, L4742-4757)
  64   87           */
  65   88          (void) setlocale(LC_ALL, "");
  66   89          (void) setlocale(LC_NUMERIC, "C");
  67   90  #if !defined(TEXT_DOMAIN)       /* Should be defined by cc -D */
  68   91  #define TEXT_DOMAIN     "SYS_TEST"      /* Use this only if it weren't */
  69   92  #endif
  70   93          (void) textdomain(TEXT_DOMAIN);
  71   94          cmdname = (uchar *)argv[0];
  72   95          if (argc == 1) {
  73   96                  (void) fprintf(stderr, gettext(
  74   97                      "Usage: %s [-f programfile | 'program'] [-Ffieldsep] "
  75   98                      "[-v var=value] [files]\n"), cmdname);
  76   99                  exit(1);
  77  100          }
  78  101          (void) signal(SIGFPE, fpecatch);
      102 +
      103 +        srand_seed = 1;
      104 +        srand((unsigned int)srand_seed);
      105 +
  79  106          yyin = NULL;
  80      -        syminit();
      107 +        symtab = makesymtab(NSYMTAB/NSYMTAB);
  81  108          while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') {
  82      -                if (strcmp(argv[1], "--") == 0) {
      109 +                if (strcmp(argv[1], "-version") == 0 ||
      110 +                    strcmp(argv[1], "--version") == 0) {
      111 +                        (void) printf("awk %s\n", version);
      112 +                        exit(0);
      113 +                        break;
      114 +                }
      115 +                if (strncmp(argv[1], "--", 2) == 0) {
  83  116                          /* explicit end of args */
  84  117                          argc--;
  85  118                          argv++;
  86  119                          break;
  87  120                  }
  88  121                  switch (argv[1][1]) {
      122 +                case 's':
      123 +                        if (strcmp(argv[1], "-safe") == 0)
      124 +                                safe = 1;
      125 +                        break;
  89  126                  case 'f':       /* next argument is program filename */
  90      -                        argc--;
  91      -                        argv++;
  92      -                        if (argc <= 1)
  93      -                                ERROR "no program filename" FATAL;
  94      -                        pfile = realloc(pfile, sizeof (uchar *) * (npfile + 1));
  95      -                        if (pfile == NULL)
  96      -                                ERROR "out of space in main" FATAL;
  97      -                        pfile[npfile++] = (uchar *)argv[1];
      127 +                        if (argv[1][2] != 0) {  /* arg is -fsomething */
      128 +                                if (npfile >= MAX_PFILE - 1)
      129 +                                        FATAL("too many -f options");
      130 +                                pfile[npfile++] = (uchar *)&argv[1][2];
      131 +                        } else {                /* arg is -f something */
      132 +                                argc--; argv++;
      133 +                                if (argc <= 1)
      134 +                                        FATAL("no program filename");
      135 +                                if (npfile >= MAX_PFILE - 1)
      136 +                                        FATAL("too many -f options");
      137 +                                pfile[npfile++] = (uchar *)argv[1];
      138 +                        }
  98  139                          break;
  99  140                  case 'F':       /* set field separator */
 100  141                          if (argv[1][2] != 0) {  /* arg is -Fsomething */
 101  142                                  /* wart: t=>\t */
 102  143                                  if (argv[1][2] == 't' && argv[1][3] == 0)
 103  144                                          fs = (uchar *) "\t";
 104  145                                  else if (argv[1][2] != 0)
 105  146                                          fs = (uchar *)&argv[1][2];
 106  147                          } else {                /* arg is -F something */
 107  148                                  argc--; argv++;
 108      -                                if (argc > 1) {
 109      -                                        /* wart: t=>\t */
 110      -                                        if (argv[1][0] == 't' &&
 111      -                                            argv[1][1] == 0)
 112      -                                                fs = (uchar *) "\t";
 113      -                                        else if (argv[1][0] != 0)
 114      -                                                fs = (uchar *)&argv[1][0];
 115      -                                }
      149 +                                /* wart: t=>\t */
      150 +                                if (argc > 1 && argv[1][0] == 't' &&
      151 +                                    argv[1][1] == 0)
      152 +                                        fs = (uchar *) "\t";
      153 +                                else if (argc > 1 && argv[1][0] != 0)
      154 +                                        fs = (uchar *)&argv[1][0];
 116  155                          }
 117  156                          if (fs == NULL || *fs == '\0')
 118      -                                ERROR "field separator FS is empty" WARNING;
      157 +                                WARNING("field separator FS is empty");
 119  158                          break;
 120  159                  case 'v':       /* -v a=1 to be done NOW.  one -v for each */
 121      -                        if (argv[1][2] == '\0' && --argc > 1 &&
 122      -                            isclvar((uchar *)(++argv)[1]))
 123      -                                setclvar((uchar *)argv[1]);
      160 +                        if (argv[1][2] != 0) {  /* arg is -vsomething */
      161 +                                if (isclvar((uchar *)&argv[1][2]))
      162 +                                        setclvar((uchar *)&argv[1][2]);
      163 +                                else
      164 +                                        FATAL(
      165 +                                "invalid -v option argument: %s", &argv[1][2]);
      166 +                        } else {        /* arg is -v something */
      167 +                                argc--; argv++;
      168 +                                if (argc <= 1)
      169 +                                        FATAL("no variable name");
      170 +                                if (isclvar((uchar *)argv[1]))
      171 +                                        setclvar((uchar *)argv[1]);
      172 +                                else
      173 +                                        FATAL(
      174 +                                "invalid -v option argument: %s", argv[1]);
      175 +                        }
 124  176                          break;
 125  177                  case 'd':
 126  178                          dbg = atoi(&argv[1][2]);
 127  179                          if (dbg == 0)
 128  180                                  dbg = 1;
 129  181                          (void) printf("awk %s\n", version);
 130  182                          break;
 131  183                  default:
 132      -                        ERROR "unknown option %s ignored", argv[1] WARNING;
      184 +                        WARNING("unknown option %s ignored", argv[1]);
 133  185                          break;
 134  186                  }
 135  187                  argc--;
 136  188                  argv++;
 137  189          }
 138  190          /* argv[1] is now the first argument */
 139  191          if (npfile == 0) {      /* no -f; first argument is program */
 140  192                  if (argc <= 1) {
 141  193                          if (dbg)
 142  194                                  exit(0);
 143      -                        ERROR "no program given" FATAL;
      195 +                        FATAL("no program given");
 144  196                  }
 145  197                  dprintf(("program = |%s|\n", argv[1]));
 146  198                  lexprog = (uchar *)argv[1];
 147  199                  argc--;
 148  200                  argv++;
 149  201          }
      202 +        recinit(record_size);
      203 +        syminit();
 150  204          compile_time = 1;
 151  205          argv[0] = (char *)cmdname;      /* put prog name at front of arglist */
 152  206          dprintf(("argc=%d, argv[0]=%s\n", argc, argv[0]));
 153  207          arginit(argc, (uchar **)argv);
 154      -        envinit((uchar **)envp);
      208 +        if (!safe)
      209 +                envinit((uchar **)envp);
 155  210          (void) yyparse();
 156  211          if (fs)
 157  212                  *FS = qstring(fs, '\0');
 158  213          dprintf(("errorflag=%d\n", errorflag));
 159  214          /*
 160  215           * done parsing, so now activate the LC_NUMERIC
 161  216           */
 162  217          (void) setlocale(LC_ALL, "");
 163  218          nl_radix = nl_langinfo(RADIXCHAR);
 164  219          if (nl_radix)
↓ open down ↓ 1 lines elided ↑ open up ↑
 166  221  
 167  222          if (errorflag == 0) {
 168  223                  compile_time = 0;
 169  224                  run(winner);
 170  225          } else
 171  226                  bracecheck();
 172  227          return (errorflag);
 173  228  }
 174  229  
 175  230  int
 176      -pgetc(void)             /* get program character */
      231 +pgetc(void)             /* get 1 character from awk program */
 177  232  {
 178  233          int c;
 179  234  
 180  235          for (;;) {
 181  236                  if (yyin == NULL) {
 182  237                          if (curpfile >= npfile)
 183  238                                  return (EOF);
 184      -                        yyin = (strcmp((char *)pfile[curpfile], "-") == 0) ?
 185      -                            stdin : fopen((char *)pfile[curpfile], "r");
 186      -                        if (yyin == NULL) {
 187      -                                ERROR "can't open file %s",
 188      -                                    pfile[curpfile] FATAL;
 189      -                        }
      239 +                        if (strcmp((char *)pfile[curpfile], "-") == 0)
      240 +                                yyin = stdin;
      241 +                        else if ((yyin = fopen(
      242 +                            (char *)pfile[curpfile], "r")) == NULL)
      243 +                                FATAL("can't open file %s", pfile[curpfile]);
      244 +                        lineno = 1;
 190  245                  }
 191  246                  if ((c = getc(yyin)) != EOF)
 192  247                          return (c);
 193      -                (void) fclose(yyin);
      248 +                if (yyin != stdin)
      249 +                        (void) fclose(yyin);
 194  250                  yyin = NULL;
 195  251                  curpfile++;
 196  252          }
 197  253  }
      254 +
      255 +uchar *
      256 +cursource(void)         /* current source file name */
      257 +{
      258 +        if (npfile > 0)
      259 +                return (pfile[curpfile]);
      260 +        else
      261 +                return (NULL);
      262 +}
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX