Skip to content

Commit

Permalink
Adding awk-23
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Holzschuch committed Aug 19, 2023
1 parent 6092a42 commit 72d5514
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions awk-23.30.1/src/awkgram.y
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Node *arglist = 0; /* list of args for current function */
%token <i> MATCH NOTMATCH MATCHOP
%token <i> FINAL DOT ALL CCL NCCL CHAR OR STAR QUEST PLUS EMPTYRE IGNORE_PRIOR_ATOM
%token <i> AND BOR APPEND EQ GE GT LE LT NE IN
%token <i> ARG BLTIN BREAK CLOSE CONTINUE DELETE DO EXIT FOR FUNC
%token <i> ARG BLTIN BREAK CLOSE CONTINUE DELETE DO EXIT FOR FUNC
%token <i> SUB GSUB IF INDEX LSUBSTR MATCHFCN NEXT NEXTFILE
%token <i> ADD MINUS MULT DIVIDE MOD
%token <i> ASSIGN ASGNOP ADDEQ SUBEQ MULTEQ DIVEQ MODEQ POWEQ
Expand All @@ -79,7 +79,7 @@ Node *arglist = 0; /* list of args for current function */
%left AND
%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 ARG BLTIN BREAK CALL CLOSE CONTINUE DELETE DO EXIT FOR FUNC
%left GSUB IF INDEX LSUBSTR MATCHFCN NEXT NUMBER
%left PRINT PRINTF RETURN SPLIT SPRINTF STRING SUB SUBSTR
%left REGEXPR VAR VARNF IVAR WHILE '('
Expand Down
2 changes: 0 additions & 2 deletions awk-23.30.1/src/b.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,12 @@ void freetr(Node *p) /* free parse tree */
break;
UNARY
freetr(left(p));
// fprintf(stderr, "Freeing unary %x\n", p); fflush(stderr);
xfree(p);
break;
case CAT:
case OR:
freetr(left(p));
freetr(right(p));
// fprintf(stderr, "Freeing tree %x\n", p); fflush(stderr);
xfree(p);
break;
case 0:
Expand Down
2 changes: 1 addition & 1 deletion awk-23.30.1/src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ int refldbld(const char *rec, const char *fs) /* build fields from reg expr in F
break;
}
}
return i;
return i;
}

void recbld(void) /* create $0 from $1..$NF if necessary */
Expand Down
4 changes: 2 additions & 2 deletions awk-23.30.1/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ int awk_main(int argc, char *argv[])
setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */
cmdname = argv[0];
if (argc == 1) {
fprintf(thread_stderr,
"usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
fprintf(thread_stderr,
"usage: %s [-F fs] [-v var=value] [-f progfile | 'prog'] [file ...]\n",
cmdname);
exit(1);
}
Expand Down
32 changes: 25 additions & 7 deletions awk-23.30.1/src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ void freeTree(Node *u, int eraseSelf) /* scan the entire tree, and frees the
if ((a->nobj == ARG) && (i == 0)) continue;
if ((a->nobj == VARNF) && (i == 0)) continue;
if ((a->nobj == GETLINE) && (i == 1)) continue;
if ((a->nobj == PRINTF) && (i == 1)) continue;
if ((a->nobj == PRINT) && (i == 1)) continue;
if ((a->nobj == PASTAT2) && (i == 3)) continue;
if ((a->nobj == SPLIT) && (i == 3)) continue;
freeTree(a->narg[i], 0); // never free narg, it was allocated as part of the node
a->narg[i] = NULL;
}
Expand Down Expand Up @@ -1308,7 +1312,8 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
int n, tempstat, arg3type;

y = execute(a[0]); /* source string */
s = getsval(y);
s = strdup(getsval(y));
dprintf( (thread_stdout, "split: s=|%s|\n", s) ); // OK so far
arg3type = ptoi(a[3]);
if (a[2] == 0) /* fs string */
fs = *FS;
Expand All @@ -1319,9 +1324,12 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
else
FATAL("illegal type of split");
dprintf( (thread_stdout, "split: s=|%s|\n", s) ); // That one is OK too
sep = *fs;
ap = execute(a[1]); /* array name */
freesymtab(ap);
dprintf( (thread_stdout, "split: s=|%s| %x\n", s, s) ); // This one is OK
freesymtab(ap); // ?????? This is causing s to disappear
dprintf( (thread_stdout, "split: s=|%s|\n", s) ); // This one is not
dprintf( (thread_stdout, "split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs) );
ap->tval &= ~STR;
ap->tval |= ARR;
Expand Down Expand Up @@ -1428,6 +1436,7 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
x = gettemp();
x->tval = NUM;
x->fval = n;
free(s);
return(x);
}

Expand Down Expand Up @@ -1599,7 +1608,13 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis
break;
case FSYSTEM:
fflush(thread_stdout); /* in case something is buffered already */
u = (Awkfloat) ios_system(getsval(x)) / 256; /* 256 is unix-dep */
// iOS
int pid = ios_fork();
int returnValue;
ios_system(getsval(x));
waitpid(pid, &returnValue, 0);
u = (Awkfloat) returnValue / 256; /* 256 is unix-dep */
// u = (Awkfloat) system(getsval(x)) / 256; /* 256 is unix-dep */
break;
case FRAND:
/* in principle, rand() returns something in 0..RAND_MAX */
Expand Down Expand Up @@ -1705,7 +1720,6 @@ FILE *redirect(int a, Node *b) /* set up all i/o redirections */
return fp;
}


FILE *openfile(int a, const char *us)
{
const char *s = us;
Expand Down Expand Up @@ -1797,14 +1811,18 @@ void closeall(void)
if (files[i].fp == thread_stderr) continue;
if (ferror(files[i].fp))
WARNING( "i/o error occurred on %s", files[i].fname );
if (files[i].mode == '|' || files[i].mode == LE)
stat = pclose(files[i].fp);
else {
if (files[i].mode == '|' || files[i].mode == LE) {
stat = pclose(files[i].fp);
files[i].fp = 0;
} else {
stat = fclose(files[i].fp);
files[i].fp = 0;
}
if (stat == EOF)
WARNING( "i/o error occurred while closing %s", files[i].fname );
// iOS: cleanup
free(files[i].fname);
files[i].fname = NULL;
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions awk-23.30.1/src/tran.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ void freesymtab(Cell *ap) /* free a symbol table */
for (i = 0; i < tp->size; i++) {
for (cp = tp->tab[i]; cp != NULL; cp = temp) {
xfree(cp->nval);
if (freeable(cp))
xfree(cp->sval);
if (freeable(cp))
xfree(cp->sval);
temp = cp->cnext; /* avoids freeing then using */
free(cp);
free(cp);
tp->nelem--;
}
tp->tab[i] = 0;
Expand All @@ -189,7 +189,7 @@ void freeelem(Cell *ap, const char *s) /* free elem s from ap (i.e., ap["s"] */
Array *tp;
Cell *p, *prev = NULL;
int h;

tp = (Array *) ap->sval;
h = hash(s, tp->size);
for (p = tp->tab[h]; p != NULL; prev = p, p = p->cnext)
Expand Down

0 comments on commit 72d5514

Please sign in to comment.