[Toybox] [PATCH] A "dc" implementation

Oliver Webb aquahobbyist at proton.me
Tue Sep 19 23:41:46 PDT 2023


I have made a implementation of the 'dc' command in toybox

Since dc isn't specified in POSIX or LSB, the things I have
compared it to and tested against are the busybox implementation 
of the command, and the GNU implementation.

My implementation includes most things from the busybox implementation missing a few things, 
and a lot of the GNU stuff that was easy to include.

The main things that my implementation can't do yet are arbitrary
precision math, input and output radixes, and stings, macros or conditionals.

I know that bc.c has a arbitrary precision library in it somewhere, 
But I haven't looked through the ~5700 lines of code
for the command to see how they got it to work or how it could be shared with dc yet
Everything that dc works with is just a long double as of now

Input and output radixes are much more trivial to get to work but much less important (Have less real use cases),
I know that strtoll and friends have a input radix argument but I haven't looked into it that much yet.

As for strings macros and conditionals, I was originally planning to put these in because 
it would turn what is essentially expr or $((shell math)) but more confusing, into a Turing complete language
with branching, loops, recursion, etc. The main problem with that is that it turns the simple 2 functions you
need to manipulate the stack in my implementation into at least 6 with separate error checking for every command
because a value on the stack can either store a string or number so _every_ command needs to make sure it operates
on the right one.

There are some bugs with it as well, mainly memory leaks I haven't diagnosed yet.
They only leak the commands given to it so the cases where it's actually apparent we should be freeing memory
is stuff like `yes p | dc` 

The other main bug I have found is that the part of the program 
that gets statements to process _only_ breaks statements with whitespace
so `2d+p` will not print 4 like it should and only adds 2 onto the stack.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 60fda44c.patch
Type: text/x-patch
Size: 5297 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20230920/a91e8bbd/attachment.bin>


More information about the Toybox mailing list