awk: allow string as exit status
This commit is contained in:
parent
3f9d5e4a4f
commit
9cf3dc9a25
7 changed files with 18 additions and 16 deletions
|
@ -45,7 +45,7 @@ extern Awkfloat *RLENGTH;
|
||||||
|
|
||||||
extern char *record; /* points to $0 */
|
extern char *record; /* points to $0 */
|
||||||
extern int lineno; /* line number in awk program */
|
extern int lineno; /* line number in awk program */
|
||||||
extern int errorflag; /* 1 if error has occurred */
|
extern char *exitstatus; /* exit status string */
|
||||||
extern int donefld; /* 1 if record broken into fields */
|
extern int donefld; /* 1 if record broken into fields */
|
||||||
extern int donerec; /* 1 if record is valid (no fld has changed */
|
extern int donerec; /* 1 if record is valid (no fld has changed */
|
||||||
extern char inputFS[]; /* FS at time of input, for field splitting */
|
extern char inputFS[]; /* FS at time of input, for field splitting */
|
||||||
|
|
|
@ -98,7 +98,7 @@ Node *arglist = 0; /* list of args for current function */
|
||||||
%%
|
%%
|
||||||
|
|
||||||
program:
|
program:
|
||||||
pas { if (errorflag==0)
|
pas { if (exitstatus==nil)
|
||||||
winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); }
|
winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); }
|
||||||
| error { yyclearin; bracecheck(); SYNTAX("bailing out"); }
|
| error { yyclearin; bracecheck(); SYNTAX("bailing out"); }
|
||||||
;
|
;
|
||||||
|
|
|
@ -355,12 +355,15 @@ int string(void)
|
||||||
if (buf == 0 && (buf = (char *) malloc(bufsz)) == nil)
|
if (buf == 0 && (buf = (char *) malloc(bufsz)) == nil)
|
||||||
FATAL("out of space for strings");
|
FATAL("out of space for strings");
|
||||||
for (bp = buf; (c = input()) != '"'; ) {
|
for (bp = buf; (c = input()) != '"'; ) {
|
||||||
if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, 0))
|
if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, 0)){
|
||||||
|
*bp = 0;
|
||||||
FATAL("out of space for string %.10s...", buf);
|
FATAL("out of space for string %.10s...", buf);
|
||||||
|
}
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '\n':
|
case '\n':
|
||||||
case '\r':
|
case '\r':
|
||||||
case 0:
|
case 0:
|
||||||
|
*bp = 0;
|
||||||
SYNTAX( "non-terminated string %.10s...", buf );
|
SYNTAX( "non-terminated string %.10s...", buf );
|
||||||
lineno++;
|
lineno++;
|
||||||
RET(0);
|
RET(0);
|
||||||
|
|
|
@ -466,7 +466,7 @@ void recbld(void) /* create $0 from $1..$NF if necessary */
|
||||||
donerec = 1;
|
donerec = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int errorflag = 0;
|
char *exitstatus = nil;
|
||||||
|
|
||||||
void yyerror(char *s)
|
void yyerror(char *s)
|
||||||
{
|
{
|
||||||
|
@ -492,7 +492,7 @@ void SYNTAX(char *fmt, ...)
|
||||||
if (curfname != nil)
|
if (curfname != nil)
|
||||||
Bprint(&stderr, " in function %s", curfname);
|
Bprint(&stderr, " in function %s", curfname);
|
||||||
Bprint(&stderr, "\n");
|
Bprint(&stderr, "\n");
|
||||||
errorflag = 2;
|
exitstatus = "syntax error";
|
||||||
eprint();
|
eprint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,6 @@ int dbg = 0;
|
||||||
char *cmdname; /* gets argv[0] for error messages */
|
char *cmdname; /* gets argv[0] for error messages */
|
||||||
extern Biobuf *yyin; /* lex input file */
|
extern Biobuf *yyin; /* lex input file */
|
||||||
char *lexprog; /* points to program argument if it exists */
|
char *lexprog; /* points to program argument if it exists */
|
||||||
extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
|
|
||||||
int compile_time = 2; /* for error printing: */
|
int compile_time = 2; /* for error printing: */
|
||||||
/* 2 = cmdline, 1 = compile, 0 = running */
|
/* 2 = cmdline, 1 = compile, 0 = running */
|
||||||
|
|
||||||
|
@ -161,15 +160,13 @@ void main(int argc, char *argv[])
|
||||||
yyparse();
|
yyparse();
|
||||||
if (fs)
|
if (fs)
|
||||||
*FS = qstring(fs, '\0');
|
*FS = qstring(fs, '\0');
|
||||||
dprint( ("errorflag=%d\n", errorflag) );
|
dprint( ("exitstatus=%s\n", exitstatus) );
|
||||||
if (errorflag == 0) {
|
if (exitstatus == nil) {
|
||||||
compile_time = 0;
|
compile_time = 0;
|
||||||
run(winner);
|
run(winner);
|
||||||
} else
|
} else
|
||||||
bracecheck();
|
bracecheck();
|
||||||
if(errorflag)
|
exits(exitstatus);
|
||||||
exits("error");
|
|
||||||
exits(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int pgetc(void) /* get 1 character from awk program */
|
int pgetc(void) /* get 1 character from awk program */
|
||||||
|
|
|
@ -217,7 +217,7 @@ Node *linkum(Node *a, Node *b)
|
||||||
{
|
{
|
||||||
Node *c;
|
Node *c;
|
||||||
|
|
||||||
if (errorflag) /* don't link things that are wrong */
|
if (exitstatus != nil) /* don't link things that are wrong */
|
||||||
return a;
|
return a;
|
||||||
if (a == nil)
|
if (a == nil)
|
||||||
return(b);
|
return(b);
|
||||||
|
|
|
@ -353,9 +353,11 @@ Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */
|
||||||
case EXIT:
|
case EXIT:
|
||||||
if (a[0] != nil) {
|
if (a[0] != nil) {
|
||||||
y = execute(a[0]);
|
y = execute(a[0]);
|
||||||
errorflag = (int) getfval(y);
|
if((y->tval & (NUM|STR)) == STR) {
|
||||||
if (istemp(y))
|
exitstatus = getsval(y);
|
||||||
tfree(y);
|
} else if((int) getfval(y) != 0) {
|
||||||
|
exitstatus = "error";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
longjmp(env, 1);
|
longjmp(env, 1);
|
||||||
case RETURN:
|
case RETURN:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue