Magellan Linux

Contents of /trunk/mkinitrd-magellan/klibc/usr/dash/arith.y

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File size: 4168 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 %{
2 /*-
3 * Copyright (c) 1993
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1997-2005
6 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Kenneth Almquist.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <stdlib.h>
37 #include "expand.h"
38 #include "shell.h"
39 #include "error.h"
40 #include "output.h"
41 #include "memalloc.h"
42
43 const char *arith_buf, *arith_startbuf;
44
45 #ifndef YYBISON
46 int yyparse(void);
47 #endif
48 void yyerror(const char *);
49 #ifdef TESTARITH
50 int main(int , char *[]);
51 int sh_error(char *);
52 #endif
53
54 %}
55 %token ARITH_NUM ARITH_LPAREN ARITH_RPAREN
56
57 %left ARITH_OR
58 %left ARITH_AND
59 %left ARITH_BOR
60 %left ARITH_BXOR
61 %left ARITH_BAND
62 %left ARITH_EQ ARITH_NE
63 %left ARITH_LT ARITH_GT ARITH_GE ARITH_LE
64 %left ARITH_LSHIFT ARITH_RSHIFT
65 %left ARITH_ADD ARITH_SUB
66 %left ARITH_MUL ARITH_DIV ARITH_REM
67 %left ARITH_UNARYMINUS ARITH_UNARYPLUS ARITH_NOT ARITH_BNOT
68 %%
69
70 exp: expr {
71 return ($1);
72 }
73 ;
74
75
76 expr: ARITH_LPAREN expr ARITH_RPAREN { $$ = $2; }
77 | expr ARITH_OR expr { $$ = $1 || $3; }
78 | expr ARITH_AND expr { $$ = $1 && $3; }
79 | expr ARITH_BOR expr { $$ = $1 | $3; }
80 | expr ARITH_BXOR expr { $$ = $1 ^ $3; }
81 | expr ARITH_BAND expr { $$ = $1 & $3; }
82 | expr ARITH_EQ expr { $$ = $1 == $3; }
83 | expr ARITH_GT expr { $$ = $1 > $3; }
84 | expr ARITH_GE expr { $$ = $1 >= $3; }
85 | expr ARITH_LT expr { $$ = $1 < $3; }
86 | expr ARITH_LE expr { $$ = $1 <= $3; }
87 | expr ARITH_NE expr { $$ = $1 != $3; }
88 | expr ARITH_LSHIFT expr { $$ = $1 << $3; }
89 | expr ARITH_RSHIFT expr { $$ = $1 >> $3; }
90 | expr ARITH_ADD expr { $$ = $1 + $3; }
91 | expr ARITH_SUB expr { $$ = $1 - $3; }
92 | expr ARITH_MUL expr { $$ = $1 * $3; }
93 | expr ARITH_DIV expr {
94 if ($3 == 0)
95 yyerror("division by zero");
96 $$ = $1 / $3;
97 }
98 | expr ARITH_REM expr {
99 if ($3 == 0)
100 yyerror("division by zero");
101 $$ = $1 % $3;
102 }
103 | ARITH_NOT expr { $$ = !($2); }
104 | ARITH_BNOT expr { $$ = ~($2); }
105 | ARITH_SUB expr %prec ARITH_UNARYMINUS { $$ = -($2); }
106 | ARITH_ADD expr %prec ARITH_UNARYPLUS { $$ = $2; }
107 | ARITH_NUM
108 ;
109 %%
110 int
111 arith(s)
112 const char *s;
113 {
114 long result;
115
116 arith_buf = arith_startbuf = s;
117
118 INTOFF;
119 result = yyparse();
120 arith_lex_reset(); /* reprime lex */
121 INTON;
122
123 return (result);
124 }
125
126
127 /*************************/
128 #ifdef TEST_ARITH
129 #include <stdio.h>
130 main(argc, argv)
131 char *argv[];
132 {
133 printf("%d\n", exp(argv[1]));
134 }
135 sh_error(s)
136 char *s;
137 {
138 fprintf(stderr, "exp: %s\n", s);
139 exit(1);
140 }
141 #endif
142
143 void
144 yyerror(s)
145 const char *s;
146 {
147
148 #ifndef YYBISON
149 yyerrok;
150 #endif
151 yyclearin;
152 arith_lex_reset(); /* reprime lex */
153 sh_error("arithmetic expression: %s: \"%s\"", s, arith_startbuf);
154 /* NOTREACHED */
155 }