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 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 donerec; /* 1 if record is valid (no fld has changed */
|
||||
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:
|
||||
pas { if (errorflag==0)
|
||||
pas { if (exitstatus==nil)
|
||||
winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); }
|
||||
| error { yyclearin; bracecheck(); SYNTAX("bailing out"); }
|
||||
;
|
||||
|
|
|
@ -355,12 +355,15 @@ int string(void)
|
|||
if (buf == 0 && (buf = (char *) malloc(bufsz)) == nil)
|
||||
FATAL("out of space for strings");
|
||||
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);
|
||||
}
|
||||
switch (c) {
|
||||
case '\n':
|
||||
case '\r':
|
||||
case 0:
|
||||
*bp = 0;
|
||||
SYNTAX( "non-terminated string %.10s...", buf );
|
||||
lineno++;
|
||||
RET(0);
|
||||
|
|
|
@ -466,7 +466,7 @@ void recbld(void) /* create $0 from $1..$NF if necessary */
|
|||
donerec = 1;
|
||||
}
|
||||
|
||||
int errorflag = 0;
|
||||
char *exitstatus = nil;
|
||||
|
||||
void yyerror(char *s)
|
||||
{
|
||||
|
@ -492,7 +492,7 @@ void SYNTAX(char *fmt, ...)
|
|||
if (curfname != nil)
|
||||
Bprint(&stderr, " in function %s", curfname);
|
||||
Bprint(&stderr, "\n");
|
||||
errorflag = 2;
|
||||
exitstatus = "syntax error";
|
||||
eprint();
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,6 @@ int dbg = 0;
|
|||
char *cmdname; /* gets argv[0] for error messages */
|
||||
extern Biobuf *yyin; /* lex input file */
|
||||
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: */
|
||||
/* 2 = cmdline, 1 = compile, 0 = running */
|
||||
|
||||
|
@ -161,15 +160,13 @@ void main(int argc, char *argv[])
|
|||
yyparse();
|
||||
if (fs)
|
||||
*FS = qstring(fs, '\0');
|
||||
dprint( ("errorflag=%d\n", errorflag) );
|
||||
if (errorflag == 0) {
|
||||
dprint( ("exitstatus=%s\n", exitstatus) );
|
||||
if (exitstatus == nil) {
|
||||
compile_time = 0;
|
||||
run(winner);
|
||||
} else
|
||||
bracecheck();
|
||||
if(errorflag)
|
||||
exits("error");
|
||||
exits(0);
|
||||
exits(exitstatus);
|
||||
}
|
||||
|
||||
int pgetc(void) /* get 1 character from awk program */
|
||||
|
|
|
@ -217,7 +217,7 @@ Node *linkum(Node *a, Node *b)
|
|||
{
|
||||
Node *c;
|
||||
|
||||
if (errorflag) /* don't link things that are wrong */
|
||||
if (exitstatus != nil) /* don't link things that are wrong */
|
||||
return a;
|
||||
if (a == nil)
|
||||
return(b);
|
||||
|
|
|
@ -353,9 +353,11 @@ Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */
|
|||
case EXIT:
|
||||
if (a[0] != nil) {
|
||||
y = execute(a[0]);
|
||||
errorflag = (int) getfval(y);
|
||||
if (istemp(y))
|
||||
tfree(y);
|
||||
if((y->tval & (NUM|STR)) == STR) {
|
||||
exitstatus = getsval(y);
|
||||
} else if((int) getfval(y) != 0) {
|
||||
exitstatus = "error";
|
||||
}
|
||||
}
|
||||
longjmp(env, 1);
|
||||
case RETURN:
|
||||
|
|
Loading…
Reference in a new issue