Diff for /rpl/rplawk/run.c between versions 1.1 and 1.2

version 1.1, 2010/09/07 12:53:06 version 1.2, 2013/06/12 09:47:52
Line 66  void tempfree(Cell *p) { Line 66  void tempfree(Cell *p) {
   
 jmp_buf env;  jmp_buf env;
 extern  int pairstack[];  extern  int pairstack[];
   extern  Awkfloat    srand_seed;
   
 Node    *winner = NULL; /* root of parse tree */  Node    *winner = NULL; /* root of parse tree */
 Cell    *tmps;      /* free temporary cells for execution */  Cell    *tmps;      /* free temporary cells for execution */
Line 1209  Cell *dopa2(Node **a, int n) /* a[0], a[ Line 1210  Cell *dopa2(Node **a, int n) /* a[0], a[
 Cell *split(Node **a, int nnn)  /* split(a[0], a[1], a[2]); a[3] is type */  Cell *split(Node **a, int nnn)  /* split(a[0], a[1], a[2]); a[3] is type */
 {  {
     Cell *x = 0, *y, *ap;      Cell *x = 0, *y, *ap;
     char *s;      char *s, *origs;
     int sep;      int sep;
     char *t, temp, num[50], *fs = 0;      char *t, temp, num[50], *fs = 0;
     int n, tempstat, arg3type;      int n, tempstat, arg3type;
   
     y = execute(a[0]);  /* source string */      y = execute(a[0]);  /* source string */
     s = getsval(y);      origs = s = strdup(getsval(y));
     arg3type = ptoi(a[3]);      arg3type = ptoi(a[3]);
     if (a[2] == 0)      /* fs string */      if (a[2] == 0)      /* fs string */
         fs = *FS;          fs = *FS;
Line 1235  Cell *split(Node **a, int nnn) /* split( Line 1236  Cell *split(Node **a, int nnn) /* split(
     ap->sval = (char *) makesymtab(NSYMTAB);      ap->sval = (char *) makesymtab(NSYMTAB);
   
     n = 0;      n = 0;
           if (arg3type == REGEXPR && strlen((char*)((fa*)a[2])->restr) == 0) {
           /* split(s, a, //); have to arrange that it looks like empty sep */
           arg3type = 0;
           fs = "";
           sep = 0;
       }
     if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) {    /* reg expr */      if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) {    /* reg expr */
         fa *pfa;          fa *pfa;
         if (arg3type == REGEXPR) {  /* it's ready already */          if (arg3type == REGEXPR) {  /* it's ready already */
Line 1329  Cell *split(Node **a, int nnn) /* split( Line 1336  Cell *split(Node **a, int nnn) /* split(
     }      }
     tempfree(ap);      tempfree(ap);
     tempfree(y);      tempfree(y);
       free(origs);
     if (a[2] != 0 && arg3type == STRING) {      if (a[2] != 0 && arg3type == STRING) {
         tempfree(x);          tempfree(x);
     }      }
Line 1466  Cell *bltin(Node **a, int n) /* builtin Line 1474  Cell *bltin(Node **a, int n) /* builtin
     Cell *x, *y;      Cell *x, *y;
     Awkfloat u;      Awkfloat u;
     int t;      int t;
       Awkfloat tmp;
     char *p, *buf;      char *p, *buf;
     Node *nextarg;      Node *nextarg;
     FILE *fp;      FILE *fp;
Line 1517  Cell *bltin(Node **a, int n) /* builtin Line 1526  Cell *bltin(Node **a, int n) /* builtin
             u = time((time_t *)0);              u = time((time_t *)0);
         else          else
             u = getfval(x);              u = getfval(x);
           tmp = u;
         srand((unsigned int) u);          srand((unsigned int) u);
           u = srand_seed;
           srand_seed = tmp;
         break;          break;
     case FTOUPPER:      case FTOUPPER:
     case FTOLOWER:      case FTOLOWER:
Line 1613  struct files { Line 1625  struct files {
     FILE    *fp;      FILE    *fp;
     const char  *fname;      const char  *fname;
     int mode;   /* '|', 'a', 'w' => LE/LT, GT */      int mode;   /* '|', 'a', 'w' => LE/LT, GT */
 } files[FOPEN_MAX] ={  } *files;
     { NULL,  "/dev/stdin",  LT },   /* watch out: don't free this! */  
     { NULL, "/dev/stdout", GT },  int nfiles;
     { NULL, "/dev/stderr", GT }  
 };  
   
 void stdinit(void)  /* in case stdin, etc., are not constants */  void stdinit(void)  /* in case stdin, etc., are not constants */
 {  {
     files[0].fp = stdin;      nfiles = FOPEN_MAX;
     files[1].fp = stdout;      files = calloc(nfiles, sizeof(*files));
     files[2].fp = stderr;      if (files == NULL)
           FATAL("can't allocate file memory for %u files", nfiles);
           files[0].fp = stdin;
       files[0].fname = "/dev/stdin";
       files[0].mode = LT;
           files[1].fp = stdout;
       files[1].fname = "/dev/stdout";
       files[1].mode = GT;
           files[2].fp = stderr;
       files[2].fname = "/dev/stderr";
       files[2].mode = GT;
 }  }
   
 FILE *openfile(int a, const char *us)  FILE *openfile(int a, const char *us)
Line 1634  FILE *openfile(int a, const char *us) Line 1654  FILE *openfile(int a, const char *us)
   
     if (*s == '\0')      if (*s == '\0')
         FATAL("null file name in print or getline");          FATAL("null file name in print or getline");
     for (i=0; i < FOPEN_MAX; i++)      for (i=0; i < nfiles; i++)
         if (files[i].fname && strcmp(s, files[i].fname) == 0) {          if (files[i].fname && strcmp(s, files[i].fname) == 0) {
             if (a == files[i].mode || (a==APPEND && files[i].mode==GT))              if (a == files[i].mode || (a==APPEND && files[i].mode==GT))
                 return files[i].fp;                  return files[i].fp;
Line 1644  FILE *openfile(int a, const char *us) Line 1664  FILE *openfile(int a, const char *us)
     if (a == FFLUSH)    /* didn't find it, so don't create it! */      if (a == FFLUSH)    /* didn't find it, so don't create it! */
         return NULL;          return NULL;
   
     for (i=0; i < FOPEN_MAX; i++)      for (i=0; i < nfiles; i++)
         if (files[i].fp == 0)          if (files[i].fp == 0)
             break;              break;
     if (i >= FOPEN_MAX)      if (i >= nfiles) {
         FATAL("%s makes too many open files", s);          struct files *nf;
           int nnf = nfiles + FOPEN_MAX;
           nf = realloc(files, nnf * sizeof(*nf));
           if (nf == NULL)
               FATAL("cannot grow files for %s and %d files", s, nnf);
           memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf));
           nfiles = nnf;
           files = nf;
       }
     fflush(stdout); /* force a semblance of order */      fflush(stdout); /* force a semblance of order */
     m = a;      m = a;
     if (a == GT) {      if (a == GT) {
Line 1676  const char *filename(FILE *fp) Line 1704  const char *filename(FILE *fp)
 {  {
     int i;      int i;
   
     for (i = 0; i < FOPEN_MAX; i++)      for (i = 0; i < nfiles; i++)
         if (fp == files[i].fp)          if (fp == files[i].fp)
             return files[i].fname;              return files[i].fname;
     return "???";      return "???";
Line 1691  Cell *closefile(Node **a, int n) Line 1719  Cell *closefile(Node **a, int n)
     x = execute(a[0]);      x = execute(a[0]);
     getsval(x);      getsval(x);
     stat = -1;      stat = -1;
     for (i = 0; i < FOPEN_MAX; i++) {      for (i = 0; i < nfiles; i++) {
         if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) {          if (files[i].fname && strcmp(x->sval, files[i].fname) == 0) {
             if (ferror(files[i].fp))              if (ferror(files[i].fp))
                 WARNING( "i/o error occurred on %s", files[i].fname );                  WARNING( "i/o error occurred on %s", files[i].fname );
Line 1735  void flush_all(void) Line 1763  void flush_all(void)
 {  {
     int i;      int i;
   
     for (i = 0; i < FOPEN_MAX; i++)      for (i = 0; i < nfiles; i++)
         if (files[i].fp)          if (files[i].fp)
             fflush(files[i].fp);              fflush(files[i].fp);
 }  }

Removed from v.1.1  
changed lines
  Added in v.1.2


CVSweb interface <joel.bertrand@systella.fr>