2166 Commits

Author SHA1 Message Date
Roberto E. Vargas Caballero
1bb5a34dfe dc: Use scale for number of digits in multiplication
The scale factor has to be considered in the multiplication because
many algorithms will depend of the precision configured in the scale,
that, until now, was considered only for division. BSD and Plan9
dc differ about how to handle this, and plan9 always use only the
scale factor, while BSD uses the maximun of the scale factor or the
bigger scale of the operands. The second seems more sensible and
produces output that user would expect, for example 0k 0.5 0.5*p would
produce 0.2 with the BSD criteria, but it would generate 0 with the
plan9 criteria.
2026-01-22 12:40:34 +01:00
Roberto E. Vargas Caballero
74096de403 bc: Fix typo 2026-01-22 11:26:18 +01:00
Roberto E. Vargas Caballero
02db7b173a tests: Be deterministic about stderr
Different libcsuse different criteria about buffering stderr
and it created problems in tests merging stdout and stderr.
To avoid the problem just redirect stderr to a different file
and merge them later using cat in a subshell.
2026-01-22 11:10:19 +01:00
Roberto E. Vargas Caballero
c5bfe949dc bc: Add support for long names
As dc supports extended identifiers and we already had
the option -s in bc that modifies the standard behaviour
we can extend bc in the -s mode to support longer names.
The lower case restriction is maintained just for
simplicity but it is possible to extend bc to support
full utf8 identifers.
2026-01-20 16:31:13 +01:00
Roberto E. Vargas Caballero
759ef4f504 tests/bc: Fix values in 0010-bc.sh
Several typos and different criteria.
2026-01-20 16:13:41 +01:00
Roberto E. Vargas Caballero
b7e30e5970 dc: Don't use negative numbers in divscale()
Divscale uses the function muln() that is not prepared to handle
negative numbers. The solution is to convert them to positive
numbers and handle the sign in divnum() and modnum(). We don't
need a copy of the parameters because they are used directly
with values from the stack, or from the expnum() function that
discards the input parameters when it calls divnum().
2026-01-20 16:00:33 +01:00
Roberto E. Vargas Caballero
a2940adeba dc: Don't trash val next pointer
When a value was assigned to a register using the 's'
command we were assigning the full value from execution
stack, overwriting the next pointer of the register
stack with the next pointer of the execution stack.
2026-01-19 18:49:42 +01:00
Roberto E. Vargas Caballero
40a4999b60 tests: Minor modifications and fixes 2026-01-19 16:25:04 +01:00
Roberto E. Vargas Caballero
6edd9510c2 tests/bc: Improve output in 0010-bc.sh 2026-01-19 13:42:40 +01:00
Roberto E. Vargas Caballero
1fbc996c41 tests/ed: Fix test comparation
The string equality operator in test is = not ==.
2026-01-19 11:51:24 +01:00
Roberto E. Vargas Caballero
dc8a781c16 bc: Remove leftover from c00921a
The commit c00921a fixed a segfault happening when no parameters
or auto variables happened, moving the initialization of the
unwind string to the moment when the macro is initialized, but
a dummy initialization remained for every declaration making that
variables were not poped (and of course a memory leak).
2026-01-19 09:42:23 +01:00
Roberto E. Vargas Caballero
9b4e1dd6d4 bc: Fix minor typo using EARGF 2026-01-19 09:35:41 +01:00
Roberto E. Vargas Caballero
c00921a3a3 bc: Force a unwind string in every function
The code in funcode() assumes that the string unwind has a
value but it didn't happen in the case of a function without
parameters or local variables.
2026-01-18 17:52:37 +01:00
Elie Le Vaillant
1c79098178 dc: prevent segfaults by cleaning up data
Without these changes, dc can undergo segfaults in a variety of
situations (list of cases that segfault, one for changed line):
1. echo 'e(5)' | bc -lc | dc
2. echo 'la sa sa sa' | dc
3. echo '5p' > a.dc; dc a.dc
2026-01-17 21:16:35 +01:00
Elie Le Vaillant
46c00d58d5 bc: small fixes regarding bc.library 2026-01-17 21:16:35 +01:00
Roberto E. Vargas Caballero
41883950c9 bc: Add -p to change the dc executable
This option is mainly added to make easier testing and enabling
using bc with custom dc versions.
2026-01-17 19:10:28 +01:00
Roberto E. Vargas Caballero
2544b70216 dc: Relax tail call optimization
Classical dc implementations only apply tail recursion optimization
but we were applying tail call recursion, removing one frame even
when no recursion was involved. This creates problems with bc that
does not track this optimization and it generates values for the Q
command without caring about this optimization.
2026-01-17 10:57:43 +01:00
Roberto E. Vargas Caballero
9439e85c04 Ignore dc 2026-01-17 10:07:18 +01:00
Santtu Lakkala
53e147613a tr: Allow combining classes and sets
Allow combining chracter classes and mixing with ranges and sets.
2026-01-15 20:26:35 +01:00
Roberto E. Vargas Caballero
860737f897 dc: Don't use truncate()
Truncate() is a function since POSIX 2008, and even it should
be exposed only if unistd.h is included, OpenBSD exposes it
even when we are using only c90 headers. Renamed to numtrunc()
to avoid the name collision.
2026-01-14 21:30:02 +01:00
Roberto E. Vargas Caballero
9f27b727a2 dc: Remove lower case hexa digits
The support for lower case hexa digits was introduced to be compatible
with the plan9 dc as described in the man page, but this was not
actually implemented because it creates many problems with almost
everything (and specially with bc) and the man page was not
updated.
2026-01-14 18:26:11 +01:00
Roberto E. Vargas Caballero
608f88f08f dc: Add initial version
This is the initial version of dc already tested in deep and
with a considerable good set of tests.
2026-01-14 12:49:01 +01:00
Roberto E. Vargas Caballero
a1bf3adbc5 tests: Merge stderr and stdout in 0002-printf.sh
It simplifies the test shell script.
2026-01-13 15:31:46 +01:00
Roberto E. Vargas Caballero
c02bf1a70d tests: USE $EXEC and fix ed path
Using an EXEC variable for every test make easy to run tools to
debug issues like for example using EXEC=valgrind ./0025-ed.sh.
Some tests for ed had a wrong path to the ed binary and they
were not testing the actual ed but the system one.
2026-01-13 12:11:16 +01:00
Roberto E. Vargas Caballero
59f7e45270 tests: Use exit in signal traps
It avoids duplicating the cleaning actions and keep the correct exit
status.
2026-01-05 22:29:57 +01:00
Roberto E. Vargas Caballero
ead2c4616a build: Remove all .o in clean
Sometimes the OBJ variable gets out of sync with the object
files in the file system, and using a wildcard does a better
job in this regard.
2025-12-31 16:15:53 +01:00
Roberto E. Vargas Caballero
f7f69125cf make: Synchronize with scc
Scc fixed a race condition hapenning while forking and execing the command
and receiving a signal that could keep make waiting forever. Signals
are correctly masked now to avoid this problems.
2025-12-31 15:31:21 +01:00
Roberto E. Vargas Caballero
f8d39b2329 bc: Improve man page 2025-12-31 15:25:57 +01:00
Roberto E. Vargas Caballero
79bc44c184 ed: Correct behaviour of shell escape in r and w
Shell escapes in these commands do not print a ! after its execution
but it prints the number of bytes read/write.
2025-12-31 13:19:34 +01:00
Roberto E. Vargas Caballero
dae48911d2 ed: Update man page and TODO 2025-12-31 13:08:52 +01:00
Roberto E. Vargas Caballero
4bc4a1d030 ed: Use the variable LINES for z
Some shells keep a variable LINES with the number of lines of the terminal
updated in every SIGWINCH. Using that variable makes easier to get a
full listing.
2025-12-31 12:38:39 +01:00
Santtu Lakkala
60d9f7a5a9 ed: Fix multiline commands
Fix command line parsing escape handling. Further process all
commands on the command line.
2025-12-30 10:21:33 +01:00
Roberto E. Vargas Caballero
c11a21f426 build: Install bc library 2025-12-13 16:06:45 +01:00
Roberto E. Vargas Caballero
85721b5a33 bc: Add man page 2025-12-13 16:03:58 +01:00
Roberto E. Vargas Caballero
42ebd643f6 bc: Fix path of the bc library 2025-12-13 16:03:34 +01:00
Roberto E. Vargas Caballero
8ceddc568d bc: Increment the number of nestting to 32
The number of 10 nested control flow data structures was too
low, and a value of 32 would work for almost any bc program.
2025-12-13 15:37:08 +01:00
Roberto E. Vargas Caballero
54f1f935c0 ed: Update TODO error list 2025-12-13 13:29:17 +01:00
Roberto E. Vargas Caballero
77f8d7e593 ed: Add the x and X commands
These are extensions to the POSIX standard but very useful
in situations like writing a commit message where the exit
status can discard the full edit session.
2025-12-13 12:57:33 +01:00
Roberto E. Vargas Caballero
5765a6e6b4 ed: Implement non truncate writes
The trunc parameter of dowrite() was ignored, making no difference
between the w and W commands.
2025-12-13 12:57:29 +01:00
Roberto E. Vargas Caballero
08b8c73dda ed: Don't clear modflag in shell escapes
The function dowrite() is used for normal writes and for shell
escapes and in the second case we don't have the actual file
saved to disk.
2025-12-13 11:52:32 +01:00
Roberto E. Vargas Caballero
5364863516 ed: Don't clear modflag until is safe
Setting modflag unconditionally in the Q command could
produce that after an error happening dealing with the Q
command then unsaved files were silently ignored.
2025-12-13 11:48:06 +01:00
Roberto E. Vargas Caballero
daec4db8a3 ed: Improve man page
Add missing z command, give more bibliography and clarify
some of the commands.
2025-12-13 11:14:15 +01:00
Roberto E. Vargas Caballero
8227fbab2e ed: Don't clear modflag until is safe
Setting modflag unconditionally in the E command could
produce that after an error happening dealing with the E
command then unsaved files were silently ignored.
2025-12-13 10:42:51 +01:00
Roberto E. Vargas Caballero
9a0d04fcf9 ed: Accept shell escapes in r, e and E commands 2025-12-13 10:29:17 +01:00
Roberto E. Vargas Caballero
8adf85e686 tests/ed: Fix 0008-ed.sh and 0009-ed.sh 2025-12-12 20:36:03 +01:00
Roberto E. Vargas Caballero
325953bd2c tests/bc: Add test cases for the bc library
These tests cover a big portion of the code because they
execute complex functions suing many of the features of
bc.
2025-12-12 20:34:56 +01:00
Roberto E. Vargas Caballero
1106076d53 bc: Simplify loading internal library
Testing can be done using directly the path to the bc.library
file, and get rid of the ugly function that we had to build
dynamically the path to the library.
2025-12-12 12:14:21 +01:00
Roberto E. Vargas Caballero
d898b61ed2 bc: Don't pop value from body in while
Statements already deal with the cleaning of the stack, but the
init and increment part of for loops are expressions without the
cleaning, and for that reason we have to care about the stack.
2025-12-11 21:55:06 +01:00
Roberto E. Vargas Caballero
9febd6d57c bc: Add a space after relationals
Some dc implementations fail if the macro name is contiguous
to the ] that finalizes the string.
2025-12-11 12:45:35 +01:00
Roberto E. Vargas Caballero
c6cbb33ef0 bc: Unify local() and param()
These functions only differentiate in a single letter, so it
was a good opportunity to merge them. Also, it was a good idea
to add the values in reverse order to make the output more
similar to other bc implementations.
2025-12-11 12:43:42 +01:00