Style cleanup, mostly

Requested by:	bde
This commit is contained in:
Jens Schweikhardt 2003-09-04 18:28:42 +00:00
parent 43bf41ebf5
commit de7112e1ba
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=119746
4 changed files with 233 additions and 194 deletions

View File

@ -34,6 +34,6 @@
* $FreeBSD$
*/
int arith_assign(char *, arith_t);
int arith(char *);
int expcmd(int , char **);
int arith_assign(char *, arith_t);
int expcmd(int, char **);

View File

@ -35,11 +35,12 @@
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
#ifndef lint
static char sccsid[] = "@(#)arith.y 8.3 (Berkeley) 5/4/95";
#endif
#endif /* not lint */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
@ -73,152 +74,187 @@ __FBSDID("$FreeBSD$");
%left ARITH_UNARYMINUS ARITH_UNARYPLUS ARITH_NOT ARITH_BNOT
%%
exp: expr = {
return ($1);
}
exp:
expr
{ return ($1); }
;
expr: ARITH_LPAREN expr ARITH_RPAREN = { $$ = $2; }
| expr ARITH_OR expr = { $$ = $1 ? $1 : $3 ? $3 : 0; }
| expr ARITH_AND expr = { $$ = $1 ? ( $3 ? $3 : 0 ) : 0; }
| expr ARITH_BOR expr = { $$ = $1 | $3; }
| expr ARITH_BXOR expr = { $$ = $1 ^ $3; }
| expr ARITH_BAND expr = { $$ = $1 & $3; }
| expr ARITH_EQ expr = { $$ = $1 == $3; }
| expr ARITH_GT expr = { $$ = $1 > $3; }
| expr ARITH_GE expr = { $$ = $1 >= $3; }
| expr ARITH_LT expr = { $$ = $1 < $3; }
| expr ARITH_LE expr = { $$ = $1 <= $3; }
| expr ARITH_NE expr = { $$ = $1 != $3; }
| expr ARITH_LSHIFT expr = { $$ = $1 << $3; }
| expr ARITH_RSHIFT expr = { $$ = $1 >> $3; }
| expr ARITH_ADD expr = { $$ = $1 + $3; }
| expr ARITH_SUB expr = { $$ = $1 - $3; }
| expr ARITH_MUL expr = { $$ = $1 * $3; }
| expr ARITH_DIV expr = {
if ($3 == 0)
yyerror("division by zero");
$$ = $1 / $3;
}
| expr ARITH_REM expr = {
if ($3 == 0)
yyerror("division by zero");
$$ = $1 % $3;
}
| ARITH_NOT expr = { $$ = !($2); }
| ARITH_BNOT expr = { $$ = ~($2); }
| ARITH_SUB expr %prec ARITH_UNARYMINUS = { $$ = -($2); }
| ARITH_ADD expr %prec ARITH_UNARYPLUS = { $$ = $2; }
| ARITH_NUM
| ARITH_VAR {
char *p;
arith_t arith_val;
char *str_val;
expr:
ARITH_LPAREN expr ARITH_RPAREN
{ $$ = $2; } |
expr ARITH_OR expr
{ $$ = $1 ? $1 : $3 ? $3 : 0; } |
expr ARITH_AND expr
{ $$ = $1 ? ( $3 ? $3 : 0 ) : 0; } |
expr ARITH_BOR expr
{ $$ = $1 | $3; } |
expr ARITH_BXOR expr
{ $$ = $1 ^ $3; } |
expr ARITH_BAND expr
{ $$ = $1 & $3; } |
expr ARITH_EQ expr
{ $$ = $1 == $3; } |
expr ARITH_GT expr
{ $$ = $1 > $3; } |
expr ARITH_GE expr
{ $$ = $1 >= $3; } |
expr ARITH_LT expr
{ $$ = $1 < $3; } |
expr ARITH_LE expr
{ $$ = $1 <= $3; } |
expr ARITH_NE expr
{ $$ = $1 != $3; } |
expr ARITH_LSHIFT expr
{ $$ = $1 << $3; } |
expr ARITH_RSHIFT expr
{ $$ = $1 >> $3; } |
expr ARITH_ADD expr
{ $$ = $1 + $3; } |
expr ARITH_SUB expr
{ $$ = $1 - $3; } |
expr ARITH_MUL expr
{ $$ = $1 * $3; } |
expr ARITH_DIV expr
{
if ($3 == 0)
yyerror("division by zero");
$$ = $1 / $3;
} |
expr ARITH_REM expr
{
if ($3 == 0)
yyerror("division by zero");
$$ = $1 % $3;
} |
ARITH_NOT expr
{ $$ = !($2); } |
ARITH_BNOT expr
{ $$ = ~($2); } |
ARITH_SUB expr %prec ARITH_UNARYMINUS
{ $$ = -($2); } |
ARITH_ADD expr %prec ARITH_UNARYPLUS
{ $$ = $2; } |
ARITH_NUM |
ARITH_VAR
{
char *p;
arith_t arith_val;
char *str_val;
if (lookupvar($1) == NULL)
setvarsafe($1, "0", 0);
str_val = lookupvar($1);
arith_val = strtoarith_t(str_val, &p, 0);
/*
* Conversion is successful only in case
* we've converted _all_ characters.
*/
if (*p != '\0')
yyerror("variable conversion error");
$$ = arith_val;
}
| ARITH_VAR ARITH_ASSIGN expr {
if (arith_assign($1, $3) != 1)
yyerror("variable assignment error");
$$ = $3;
}
| ARITH_VAR ARITH_ADDASSIGN expr {
arith_t value;
if (lookupvar($1) == NULL)
setvarsafe($1, "0", 0);
str_val = lookupvar($1);
arith_val = strtoarith_t(str_val, &p, 0);
/*
* Conversion is successful only in case
* we've converted _all_ characters.
*/
if (*p != '\0')
yyerror("variable conversion error");
$$ = arith_val;
} |
ARITH_VAR ARITH_ASSIGN expr
{
if (arith_assign($1, $3) != 1)
yyerror("variable assignment error");
$$ = $3;
} |
ARITH_VAR ARITH_ADDASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) + $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_SUBASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) + $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_SUBASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) - $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_MULASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) - $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_MULASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) * $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_DIVASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) * $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_DIVASSIGN expr
{
arith_t value;
if ($3 == 0)
yyerror("division by zero");
if ($3 == 0)
yyerror("division by zero");
value = atoarith_t(lookupvar($1)) / $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_REMASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) / $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_REMASSIGN expr
{
arith_t value;
if ($3 == 0)
yyerror("division by zero");
if ($3 == 0)
yyerror("division by zero");
value = atoarith_t(lookupvar($1)) % $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_RSHASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) % $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_RSHASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) >> $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_LSHASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) >> $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_LSHASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) << $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_BANDASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) << $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_BANDASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) & $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_BXORASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) & $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_BXORASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) ^ $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
| ARITH_VAR ARITH_BORASSIGN expr {
arith_t value;
value = atoarith_t(lookupvar($1)) ^ $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} |
ARITH_VAR ARITH_BORASSIGN expr
{
arith_t value;
value = atoarith_t(lookupvar($1)) | $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
}
;
value = atoarith_t(lookupvar($1)) | $3;
if (arith_assign($1, value) != 0)
yyerror("variable assignment error");
$$ = value;
} ;
%%
#include "error.h"
#include "output.h"
@ -233,7 +269,8 @@ int yylex(void);
int yyparse(void);
int
arith_assign(char *name, arith_t value) {
arith_assign(char *name, arith_t value)
{
char *str;
int ret;
@ -253,10 +290,10 @@ arith(char *s)
INTOFF;
result = yyparse();
arith_lex_reset(); /* reprime lex */
arith_lex_reset(); /* Reprime lex. */
INTON;
return (result);
return result;
}
void
@ -265,7 +302,7 @@ yyerror(char *s)
yyerrok;
yyclearin;
arith_lex_reset(); /* reprime lex */
arith_lex_reset(); /* Reprime lex. */
error("arithmetic expression: %s: \"%s\"", s, arith_startbuf);
}
@ -284,7 +321,7 @@ expcmd(int argc, char **argv)
p = argv[1];
if (argc > 2) {
/*
* concatenate arguments
* Concatenate arguments.
*/
STARTSTACKSTR(concat);
ap = argv + 2;
@ -304,7 +341,7 @@ expcmd(int argc, char **argv)
i = arith(p);
out1fmt("%ld\n", i);
return (! i);
return !i;
}
/*************************/

View File

@ -35,19 +35,20 @@
* SUCH DAMAGE.
*/
#ifndef lint
#if 0
#ifndef lint
static char sccsid[] = "@(#)arith_lex.l 8.3 (Berkeley) 5/4/95";
#endif
#endif /* not lint */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "shell.h"
#include "y.tab.h"
#include "error.h"
#include "var.h"
#include "memalloc.h"
#include "var.h"
extern char *arith_buf, *arith_startbuf;
#undef YY_INPUT
@ -60,24 +61,23 @@ extern char *arith_buf, *arith_startbuf;
[ \t\n] { ; }
0x[a-fA-F0-9]+ {
yylval.l_value = strtoarith_t(yytext, NULL, 16);
return(ARITH_NUM);
yylval.l_value = strtoarith_t(yytext, NULL, 16);
return ARITH_NUM;
}
0[0-7]+ {
yylval.l_value = strtoarith_t(yytext, NULL, 8);
return(ARITH_NUM);
0[0-7]+ {
yylval.l_value = strtoarith_t(yytext, NULL, 8);
return ARITH_NUM;
}
[0-9]+ {
yylval.l_value = strtoarith_t(yytext, NULL, 10);
return(ARITH_NUM);
yylval.l_value = strtoarith_t(yytext, NULL, 10);
return ARITH_NUM;
}
[A-Za-z][A-Za-z0-9_]* {
/*
* If variable doesn't exist, we should initialize
* If variable doesn't exist, we should initialize
* it to zero.
*/
char *temp;
@ -86,46 +86,49 @@ extern char *arith_buf, *arith_startbuf;
temp = (char *)ckmalloc(strlen(yytext) + 1);
yylval.s_value = strcpy(temp, yytext);
return(ARITH_VAR);
return ARITH_VAR;
}
"(" { return ARITH_LPAREN; }
")" { return ARITH_RPAREN; }
"||" { return ARITH_OR; }
"&&" { return ARITH_AND; }
"|" { return ARITH_BOR; }
"^" { return ARITH_BXOR; }
"&" { return ARITH_BAND; }
"==" { return ARITH_EQ; }
"!=" { return ARITH_NE; }
">" { return ARITH_GT; }
">=" { return ARITH_GE; }
"<" { return ARITH_LT; }
"<=" { return ARITH_LE; }
"<<" { return ARITH_LSHIFT; }
">>" { return ARITH_RSHIFT; }
"*" { return ARITH_MUL; }
"/" { return ARITH_DIV; }
"%" { return ARITH_REM; }
"+" { return ARITH_ADD; }
"-" { return ARITH_SUB; }
"~" { return ARITH_BNOT; }
"!" { return ARITH_NOT; }
"=" { return ARITH_ASSIGN; }
"+=" { return ARITH_ADDASSIGN; }
"-=" { return ARITH_SUBASSIGN; }
"*=" { return ARITH_MULASSIGN; }
"/=" { return ARITH_DIVASSIGN; }
"%=" { return ARITH_REMASSIGN; }
">>=" { return ARITH_RSHASSIGN; }
"<<=" { return ARITH_LSHASSIGN; }
"&=" { return ARITH_BANDASSIGN; }
"^=" { return ARITH_BXORASSIGN; }
"|=" { return ARITH_BORASSIGN; }
. {
error("arith: syntax error: \"%s\"\n", arith_startbuf);
}
"(" { return(ARITH_LPAREN); }
")" { return(ARITH_RPAREN); }
"||" { return(ARITH_OR); }
"&&" { return(ARITH_AND); }
"|" { return(ARITH_BOR); }
"^" { return(ARITH_BXOR); }
"&" { return(ARITH_BAND); }
"==" { return(ARITH_EQ); }
"!=" { return(ARITH_NE); }
">" { return(ARITH_GT); }
">=" { return(ARITH_GE); }
"<" { return(ARITH_LT); }
"<=" { return(ARITH_LE); }
"<<" { return(ARITH_LSHIFT); }
">>" { return(ARITH_RSHIFT); }
"*" { return(ARITH_MUL); }
"/" { return(ARITH_DIV); }
"%" { return(ARITH_REM); }
"+" { return(ARITH_ADD); }
"-" { return(ARITH_SUB); }
"~" { return(ARITH_BNOT); }
"!" { return(ARITH_NOT); }
"=" { return(ARITH_ASSIGN); }
"+=" { return(ARITH_ADDASSIGN); }
"-=" { return(ARITH_SUBASSIGN); }
"*=" { return(ARITH_MULASSIGN); }
"/=" { return(ARITH_DIVASSIGN); }
"%=" { return(ARITH_REMASSIGN); }
">>=" { return(ARITH_RSHASSIGN); }
"<<=" { return(ARITH_LSHASSIGN); }
"&=" { return(ARITH_BANDASSIGN); }
"^=" { return(ARITH_BXORASSIGN); }
"|=" { return(ARITH_BORASSIGN); }
. { error("arith: syntax error: \"%s\"\n", arith_startbuf); }
%%
void
arith_lex_reset()
arith_lex_reset(void)
{
YY_NEW_FILE;
}

View File

@ -48,28 +48,27 @@
*/
#define JOBS 1
#define JOBS 1
/* #define DEBUG 1 */
/*
* Type of used arithmetics. SUSv3 requires us to have at least signed long.
*/
typedef long arith_t;
#define strtoarith_t(nptr, endptr, base) strtol(nptr, endptr, base)
#define atoarith_t(arg) strtol(arg, NULL, 0)
#define ARITH_FORMAT_STR "%ld"
#define ARITH_FORMAT_STR "%ld"
#define atoarith_t(arg) strtol(arg, NULL, 0)
#define strtoarith_t(nptr, endptr, base) strtol(nptr, endptr, base)
typedef void *pointer;
#define STATIC static
#define MKINIT /* empty */
#define MKINIT /* empty */
#include <sys/cdefs.h>
extern char nullstr[1]; /* null string */
#ifdef DEBUG
#define TRACE(param) sh_trace param
#define TRACE(param) sh_trace param
#else
#define TRACE(param)
#endif