[Toybox] [PATCH] expr

Rob Landley rob at landley.net
Tue Jun 4 23:06:54 PDT 2013


On 06/04/2013 03:09:52 AM, Daniel Verkamp wrote:
> This half-baked implementation of expr has been sitting on my disk for
> a little while now, but I find myself struggling to find time to
> finish it, so I'm posting it in the hopes that someone will pick it
> up.

I know the feeling. :)

That someone is usually me, but finding the time...

> The expression is evaluated using a recursive descent parser; the core
> of the evaluator is parse_op(), which uses a table to determine
> operator behavior and precedence.
> 
> All numeric values are represented as 'long', which is only 32 bits on
> 32-bit hosts; it may be interesting to use 'long long' instead.

Back when my timeconst perl removal patch was a shell script, it relied  
on 64 bit expr support and I got a busybox fix for that. Since then,  
I've rewritten it in C (and HPA rewrote it in bc), but it would  
probably be best to have it as 64 bit math anyway.

> What's left to do:
> - Flesh out the help text.

Ironically, the expr implementation I have lying around here was  
nothing _but_ help text (that's as far as I got), but I'm not finding  
it at the moment...

Ah, no, I was thinking toys/pending/test.c.

> - Write more comprehensive test cases.
> - Decide what to do about integer overflow (the current code can
> execute undefined signed overflow behavior with large inputs; GNU
> coreutils expr detects this and prints an appropriate error).

What does Posix require?

> - Implement the : operator - yes, in fine 'cat -v' tradition, POSIX
> expr can match regular expressions.

Oh joy.

I note that part of the reason I've held off with this one is that  
(like test.c) it's basically shell behavior. Test is [ ] and expr is  
$(( )), and they should eventually share code. I haven't yet studied  
_how_ similar they are and how much (if any0 they diverge...

And then there's bc, which thanks to Peter Anvin I now have to  
implement to build unmodified linux source. (The Linux From Scratch  
guys had to add it to chapter 5. Sigh.) Dunno what overlap's there,  
haven't studied it yet...

> I have done some minimal testing while writing the command, but it
> could use some real-world testing (I think the Linux kernel build
> contains a few expr invocations, but I have not tried it with this
> implementation).
> 
> Feel free to fold, spindle, or mutilate as desired.

I checked it in and put it on the todo list. Unfortunately, my todo  
list has roving packs of archeologists circling it at this point,  
looking for an unguarded moment to attack with brush and trowel and  
plaster cast...

Thanks,

Rob
 1370412414.0


More information about the Toybox mailing list