2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
9 /******************************************************************************
17 VALUE1/A,OP,VALUE2/M,TO/K,LFORMAT/K
25 Evaluate an integer expression and print the result. The result is
26 written to standard output if not the TO switch are used which instead
27 prints the result to a file. Using the switch LFORMAT, it is
28 possible to direct how to write the result. Numbers prefixed by
29 0x or #x are interpreted as hexadecimal and those prefixed by # or 0
30 are interpreted as octals. Alphabetical characters are indicated
31 by a leading single quotation mark ('), and are evaluated as their
38 VALUE2 -- The expression to evaluate. The following operators
42 ----------------------------------
54 exclusive or xor, X, x
55 bitwise equivalence eqv, E, e
57 TO -- File to write the result to
58 LFORMAT -- printf-like specification of what to write.
59 The possible swiches are:
61 %xd -- hexadecimal output, width digit d
62 %od -- octal output, width digit d
64 %c -- character output (the ANSI-character
65 corresponding to the result value)
67 By specifying *n in the LFORMAT string, a newline
84 04.05.2011 polluks width digit was missing
85 01.01.2001 SDuvan implemented (although a bit tired... happy new year!)
87 ******************************************************************************/
90 #include <aros/debug.h>
92 #include "evalParser.tab.c"
94 #include <exec/types.h>
95 #include <exec/memory.h>
97 #include <dos/dosextens.h>
98 #include <dos/rdargs.h>
99 #include <proto/dos.h>
100 #include <proto/exec.h>
103 const TEXT version
[] = "$VER: Eval 41.2 (4.5.2011)\n";
105 #define ARG_TEMPLATE "VALUE1/A,OP,VALUE2/M,TO/K,LFORMAT/K"
117 void printLformat(STRPTR format
, int value
);
118 STRPTR
fixExpression(STRPTR val1
, STRPTR op
, STRPTR
*vals
);
125 int retval
= RETURN_OK
;
127 IPTR args
[] = { (IPTR
)NULL
,
135 rda
= ReadArgs(ARG_TEMPLATE
, args
, NULL
);
139 PrintFault(IoErr(), "Eval");
143 STRPTR toFile
= (STRPTR
)args
[ARG_TO
];
144 STRPTR lFormat
= (STRPTR
)args
[ARG_LFORMAT
];
145 STRPTR value1
= (STRPTR
)args
[ARG_VALUE1
];
146 STRPTR op
= (STRPTR
)args
[ARG_OP
];
147 STRPTR
*value2
= (STRPTR
*)args
[ARG_VALUE2
];
151 BPTR oldout
= BNULL
; /* Former output stream if using TO */
152 BPTR file
= BNULL
; /* Output redirection stream */
154 /* The developer to use fixExpression had no high regard
155 of the Amiga Eval command, and at the same time considered
156 ReadArgs() not to be really suitable for these arguments.
157 To be compatible to the Amiga Eval command, the developer
158 felt it was necessary to patch the arguments back together. */
159 argString
= fixExpression(value1
, op
, value2
);
161 text
= argString
; /* Note: text is a global variable that is
162 modified, so we cannot free 'text' below */
164 D(bug("Put together text: %s\n", text
));
166 if (text
== NULL
|| yyparse() == 1)
168 retval
= RETURN_ERROR
;
173 file
= Open(toFile
, MODE_NEWFILE
);
177 printf("Cannot open output file %s\n", toFile
);
183 oldout
= SelectOutput(file
);
187 if (toFile
== NULL
|| file
!= BNULL
) {
190 printLformat(lFormat
, g_result
);
194 printf("%i\n", g_result
);
197 /* Reinstall output stream if we changed it */
200 SelectOutput(oldout
);
202 /* Close the TO/K file */
217 extern int g_result
; /* The result variable is global for now */
219 void printLformat(STRPTR format
, int value
)
221 ULONG i
; /* Loop variable */
223 /* If it weren't for *n we could use VfWriteF() */
225 for (i
= 0; format
[i
] != 0; i
++)
230 if (format
[i
] == 'n')
244 switch (tolower(format
[i
]))
246 /* Hexadecimal display */
248 printf("%0*x", format
[++i
] - '0', value
);
253 printf("%0*o", format
[++i
] - '0', value
);
256 /* Integer display */
261 /* Character display */
270 /* Stupid user writes "...%" */
276 printf("%%%c", format
[i
]);
279 } /* switch(%-command) */
284 printf("%c", format
[i
]);
286 } /* switch format character */
291 STRPTR
fixExpression(STRPTR val1
, STRPTR op
, STRPTR
*vals
)
293 /* val1 must be != 0 as it's an /A argument */
295 int i
; /* Loop variable */
298 len
= strlen(val1
) + 1 + 1; /* One extra for the 0 byte to end
303 len
+= strlen(op
) + 1;
306 if (vals
) for (i
= 0; vals
[i
] != NULL
; i
++)
308 len
+= strlen(vals
[i
]) + 1;
311 arg
= AllocVec(len
, MEMF_ANY
);
327 if (vals
) for (i
= 0; vals
[i
] != NULL
; i
++)
329 strcat(arg
, vals
[i
]);