add support for 'length' keyword, for compatibility with GNU expr

This commit is contained in:
jdolecek 2004-04-20 19:44:51 +00:00
parent 5da9234d88
commit c92704dc53
2 changed files with 30 additions and 5 deletions

View File

@ -1,4 +1,4 @@
.\" $NetBSD: expr.1,v 1.25 2003/12/21 11:18:25 wiz Exp $
.\" $NetBSD: expr.1,v 1.26 2004/04/20 19:44:51 jdolecek Exp $
.\"
.\" Copyright (c) 2000,2003 The NetBSD Foundation, Inc.
.\" All rights reserved.
@ -101,15 +101,23 @@ otherwise the matching operator returns the number of characters matched.
If the match fails and the pattern contains a regular expression subexpression
the null string is returned;
otherwise 0.
.It Ar "( " expr Li " )"
.It "( " Ar expr Li " )"
Parentheses are used for grouping in the usual manner.
.El
.Pp
Additionally, following keywords are recognized:
.Bl -tag -width indent
.It length Ar expr
Returns length of specified string in bytes.
.El
.Pp
Operator precedence (from highest to lowest):
.Bl -enum -compact -offset indent
.It
parentheses
.It
length
.It
.Dq \&:
.It
.Dq "*" ,
@ -163,6 +171,10 @@ The
.Nm
utility conforms to
.St -p1003.2 .
The
.Ar length
keyword is an extension for compatibility with GNU
.Nm .
.Sh AUTHORS
Original implementation was written by
.An J.T. Conklin

View File

@ -1,4 +1,4 @@
/* $NetBSD: expr.y,v 1.30 2004/03/20 08:45:05 jdolecek Exp $ */
/* $NetBSD: expr.y,v 1.31 2004/04/20 19:44:51 jdolecek Exp $ */
/*_
* Copyright (c) 2000 The NetBSD Foundation, Inc.
@ -38,7 +38,7 @@
%{
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: expr.y,v 1.30 2004/03/20 08:45:05 jdolecek Exp $");
__RCSID("$NetBSD: expr.y,v 1.31 2004/04/20 19:44:51 jdolecek Exp $");
#endif /* not lint */
#include <sys/types.h>
@ -73,6 +73,7 @@ int main(int, const char * const *);
%left ADD_SUB_OPERATOR
%left MUL_DIV_MOD_OPERATOR
%left SPEC_REG
%left LENGTH
%left LEFT_PARENT RIGHT_PARENT
%%
@ -219,6 +220,15 @@ expr: item { $$ = $1; }
}
| LEFT_PARENT expr RIGHT_PARENT { $$ = $2; }
| LENGTH expr {
/*
* Return length of 'expr' in bytes.
*/
char *ln;
asprintf(&ln, "%ld", (long) strlen($2));
$$ = ln;
}
;
item: STRING
@ -228,6 +238,7 @@ item: STRING
| SPEC_OR
| SPEC_AND
| SPEC_REG
| LENGTH
;
%%
@ -399,7 +410,9 @@ yylex(void)
/* "--" is to be ignored */
p = yylval;
}
} else
} else if (strcmp(p, "length") == 0)
retval = LENGTH;
else
retval = STRING;
handle_ddash = 0;