Commit Graph

89 Commits

Author SHA1 Message Date
Roberto E. Vargas Caballero 2732217a40 ed: Handle correctly lines in substitutions
The s command can apply a replace pattern with embedded newlines
which modifies the line/index assignament. Using a range in  the
address fail because afther the call to subline() the next  line
has to be searched based in the index because the replace  could
insert newlines.
2023-12-29 20:27:23 +01:00
Roberto E. Vargas Caballero 6a557314b9 ed: Don't undo commands in sigint
If newcmd is 0 then error() undo all the modifications
that happened since the last command, but this is  not
what POSIX mandates:

SIGINT	The ed utility shall interrupt its current activity, write the
	string "?\n" to standard output, and return to command mode
	(see the EXTENDED DESCRIPTION section).
2023-12-28 17:26:50 +01:00
Roberto E. Vargas Caballero bbdd7bedc4 ed: Simplify sighup dealing
As we already have the dump() function we can move the
modification check inside the new dump() function.
2023-12-28 17:19:47 +01:00
Roberto E. Vargas Caballero 137f0917e4 ed: Print only last line in empty command 2023-12-28 17:19:17 +01:00
Roberto E. Vargas Caballero 2d4d7dc6d4 ed: Fix G and V commands 2023-12-28 17:16:34 +01:00
Roberto E. Vargas Caballero 516e7fec92 ed: Remove nothing comments
Several bugs happened in the past due to this kind of comments
and it is better to get rid of them.
2023-12-28 17:16:24 +01:00
Roberto E. Vargas Caballero 54a0fc3ecc ed: Fix makeline
Strings without newlines created problems in the function
and the global field was not updated, making that new lines
added were marked as global being processed in the current
global command.
2023-12-28 17:14:04 +01:00
Roberto E. Vargas Caballero 890f6c2c05 ed: Improve execsh 2023-12-28 17:02:44 +01:00
Roberto E. Vargas Caballero aacfa18b79 ed: Avoid dangling pointer in getrhs()
If the string r.str is freed but error() is called then
next call will see a pointer that maybe it will try to free
because the call to error unwind the frame stack.
2023-12-28 17:02:19 +01:00
Roberto E. Vargas Caballero b089261c3a ed: Read from input in append()
This enables using a and i commands in a global command
because the input is not anymore taken from stdin.
2023-12-28 17:01:58 +01:00
Roberto E. Vargas Caballero 1e10bf6069 ed: Add getinput() and setinput()
These functions allow to read from stdin the full next
line or seting as input a character array. These functions
avoid all the complexity about repeat commands that is very
fragile and depends on having multiple global variables with
weak relation between them.
2023-12-28 16:49:34 +01:00
Roberto E. Vargas Caballero b710ee81fc ed: Add copystring()
This makes possible to use the function to initialize the string from
an existing char array.
2023-12-28 14:01:30 +01:00
Roberto E. Vargas Caballero d3780956a9 ed: Deal signals in a reliable way
The signal handlers were calling longjmp() but as the code was calling
non signal safe functions the behaviour was very unpredictable generating
segmentation faults and dead lock. This commit changes the signal handlers
to only set a variable that is checked in safe places where long loops
happen.
2023-09-26 11:33:45 +02:00
Rene Kita 95b9603975 Ensure commands are followed by a blank
POSIX.1-2017 demands in Shell & Utilities under 'Commands in ed':
The e, E, f, r, and w commands shall take an optional file parameter,
separated from the command letter by one or more <blank> characters.

Ensure at least one <blank> character (as defined for the POSIX locale)
is present or error out.

Signed-off-by: Rene Kita <mail@rkta.de>
2023-09-26 10:50:15 +02:00
Roberto E. Vargas Caballero 67a00c86f9 ed: Open output file for writing
Fopen() and Popen() were open as read streams, but we were writing
in both cases. In the same way, the FILE pointer returned by popen()
was close with fclose() that can lead to file descriptor leaks and
zombie processes.
2023-09-22 20:45:38 +02:00
Roberto E. Vargas Caballero feeb6e3279 ed: Don't discard full lines
Discard() was reading stdin until a new line was found, but in
case of having an empty line in the input buffer then it didn't
make sense because we were just discarding the full next line.
2023-09-22 20:44:17 +02:00
Heiko Berges 0b4c2ceb2f ed: Add support for ! in w command
When the file name begins with ! then the addressed buffer content is
piped to the name of the file that is considered a command that is
executed in a shell.
2023-09-21 15:14:16 +02:00
Andrea Calligaris 93f34c1840 ed: Fix 'w' command not respecting '-s' option 2023-04-12 21:01:27 -07:00
Cág 3eb89c44aa ed: Fix double-free
After join() is called for the first time, s.str is left pointing
to a string that was just freed. Upon the second call to join(),
it is freed again at the start of the function.

Since the string is reset on every function call, there is no reason
for it to be static, so just replace the initial free with assignment
to NULL.
2020-10-19 14:50:47 -07:00
Michael Forney a139474486 ed: Use fwrite to write lines instead of fputs
We already have the length of the line, so there is no point in
recalculating it before printing.
2020-09-18 19:58:22 -07:00
Tait Hoyem 714601b29d ed: Add bytecount print to 'w' command 2020-09-18 19:58:22 -07:00
Michael Forney 6902aad435 Add a bugs section to TODO 2020-04-05 20:02:15 -07:00
Michael Forney a8dc42e6b5 ed: Use reallocarray 2019-12-31 13:41:38 -08:00
Firas Khalil Khana a1627b9419 ed: Changed delimiter to fix unterminated TODO comment 2018-09-23 11:02:31 +02:00
Roberto E. Vargas Caballero 6d3f18c821 ed: Add more bugs to TODO 2018-09-20 10:54:21 +02:00
Roberto E. Vargas Caballero f45156d5b4 Simplify expression in makeline()
This expression was wrong, but it was causing a false positive
in some compilers that couldn't see that error() cannot return.
The actual problem of the line is that it was too complex and it is better
to split it in simplex expressions.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 18f6c5e014 Add TODO 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero ece6569297 Fix undo
Avoid incorrect values in the number of undo elements and restore lastln
value after an undo operation.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero cd9b149c8f Use nextln() in scroll() 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero ebf740f962 Improve doglobal()
Don't use directly the line numbers and call to getlst()
when a line is matched.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 9781959581 Fix copy()
Copy was using directly the line numbers and incrementing them
without calling nextln(). It also didn't worry about how
line numbers are modified when we insert new lines.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 504dde1373 Allow overlapping transfer 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero d849e6d4aa Rename addchar_() to addchar()
All the ocurrences of addchar() were moved to addchar_(),
so we can rename addchar_() and remove the old definition.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 3ce26f33c6 Move subline() to use String type 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 77fe371fe4 Fix type of c in getrhs()
C is compared against EOF, so it cannot be char.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 0732529fef Fix type of c in execsh()
C is compared against EOF, so it cannot be char.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero a5d268c409 Move execsh() to use String type 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 8d59405ae0 Move join() to use String type 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 7c2eec7bd1 Move lastre to String type 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 35cf3c79f2 Move text to String type 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero f5cb020f06 Move cmdline to String type 2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 20794b570e Define new String type
Current handling of strings is a bit messy. This type is copied
from the sed implementation. Addchar_ is added to be able to live
with String and old style chars based in 3 different variables.
2018-07-03 09:31:46 +01:00
Roberto E. Vargas Caballero 651e2b0ee2 Set restore point before setting signal handlers 2018-07-03 09:31:46 +01:00
Michael Forney 1ff2a54e8f ed: Write "?\n" and explanatory messages to stdout
This is the behavior specified by POSIX.
2018-02-24 00:09:40 -08:00
Samadi van Koten 44ccadcde3 Fix line buffering issue with ed prompt
This fixes an issue whereby ed would not output the prompt (when
enabled) until after exitting due to stdout being line-buffered.
2018-02-23 23:58:29 -08:00
Roberto E. Vargas Caballero 9ab1478f1e ed: fix commit 2ccc1e8
The patch was wrong because the prototype of
strcpy is different to the prototype of strlcpy
2017-01-10 11:30:34 +01:00
Roberto E. Vargas Caballero b95c8ed79e ed: Don't use strlcpy()
All the buffers related to files have FILENAME_MAX size, so it is impossible
to have any buffer overrun.
2017-01-10 08:49:17 +01:00
Roberto E. Vargas Caballero 78bfd8978e Revert "ed: remove double free in join()"
This reverts commit 30da327fbd.
The double free is needed to avoid memory leaks when signals are caught
2017-01-10 08:41:35 +01:00
Thomas Mannay 89f5f84316 ed: Treat addresses of 0 as 1 for insert
From 6665eaa1d2c25a95b44a4f4fb3d24a3bd5c1180f Mon Sep 17 00:00:00 2001
From: Thomas Mannay <audiobarrier@openmailbox.org>
Date: Thu, 3 Nov 2016 15:16:32 +0000
Subject: [PATCH] Treat addresses of 0 as 1 for insert
2017-01-02 20:57:00 +01:00
Laslo Hunhold e2f886c7e2 ed: Use strlcpy() instead of strcpy() and other minor things
Based on contribution by Ali H. Fardan, thanks!
2016-12-27 13:07:02 +01:00