43 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
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
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
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
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
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
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
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
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
fbc4b42524 tests: Fix 0007-ed.sh 2025-12-08 18:20:07 +01:00
Roberto E. Vargas Caballero
13db2e48d4 tests: Minor improvements to 0006-ed.sh 2025-12-08 18:19:29 +01:00
Roberto E. Vargas Caballero
96b9a71acf ed: Accept no address in k command 2025-12-08 18:19:25 +01:00
Roberto E. Vargas Caballero
284e154a27 ed: Restore newlines in match()
Match was removing the trailing newline to fix some problems
in regex, but this modified the content of the line making
that the output file had embedded NUL characters (and obviously
no newlines).
2025-12-08 18:19:04 +01:00
Roberto E. Vargas Caballero
a45474cfbb ed: Accept no address in k command 2025-12-08 18:03:44 +01:00
Roberto E. Vargas Caballero
f6a1fbb9b1 ed: Restore newlines in match()
Match was removing the trailing newline to fix some problems
in regex, but this modified the content of the line making
that the output file had embedded NUL characters (and obviously
no newlines).
2025-12-08 17:54:10 +01:00
Roberto E. Vargas Caballero
beb6a2fa2a ed: Detect correctly end of file in gettxt()
The function gettxt() is basically a repeat loop where it loops
until it arrives to the end of the file when it doesn't find  a
newline, but the condition to detect the end of file was wrong
and it looped forever in a file without newline.
2025-12-08 17:47:35 +01:00
Roberto E. Vargas Caballero
c63dd6d499 tests: Remove cleanup functions 2025-12-08 17:30:50 +01:00
Roberto E. Vargas Caballero
ae50e31d53 ed: Allow z commands with no address
When a single z commit is executed then nlines == 0, and
we don't have a value for the line where scroll and this
is why we have to call deflines() to setup the default
value.
2025-12-08 17:13:25 +01:00
Santtu Lakkala
0190f5e7bf ed: Split tests
Use multiple separate test scripts for ed to make it more obvious which
test failed.
2025-12-02 09:23:47 +01:00
Roberto E. Vargas Caballero
eedc752297 tests: Use heredoc for diff
This simplifies the logic because it avoids the use of an additional
temporary file. The cleanup function is added only to EXIT because
in the way that it is written it works also in the case of signals.
2025-11-20 16:47:11 +01:00
Santtu Lakkala
b3c479298c ed: Add tests
Add tests for some cases fixed by previous changes, and also generic
usage.
2025-11-20 15:49:35 +01:00
Roberto E. Vargas Caballero
c15aa18ff3 tests: Use tmp. prefix in 0002-printf
The clear rule of the Makefile only knows about files
that begin with tmp, and having files with the prefix
exp can produce dirty workcopies even after running
make clean.
2025-11-20 15:01:30 +01:00
Santtu Lakkala
d77328ca4d printf: Fix multiple flags and read overflow
Support having multiple flags for a single conversion; at least '+'/'#' and
'0' are not exclusive.

Further use strspn() instead of inlined version for correct handling of
string ending with allowed characters.
2025-11-12 17:03:54 +01:00
Roberto E. Vargas Caballero
4593bfb359 tests: Add initial support for tests 2025-11-10 09:12:00 +01:00