Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ed: newline append versus 'r' command #346

Merged
merged 2 commits into from
Nov 24, 2023
Merged

Conversation

mknos
Copy link
Contributor

@mknos mknos commented Nov 22, 2023

  • ed requires that all lines in its buffer are terminated with "\n"
  • ed attempts to append a "\n" if it is missing on the last line of input
  • The existing append logic in edEdit() was observed to only work for 'E' and 'e' commands, but not 'r'
  • With E/e the entire buffer is destroyed and replaced with the content from the new file
  • The append logic would then check the final line in buffer after the buffer was replaced (this worked ok)
  • The 'r' command inserts the file content to any position within the existing buffer (e.g. 0r to insert to beginning of buffer)
  • Checking for missing newline at end of buffer doesn't work if the data is inserted at a different location in buffer
  • More general fix: check final line of tmp_lines list before it is applied to the buffer
  • To test, construct a file 'in' with a missing newline at end, then read it into the beginning of buffer (initiated from file 'in2')
%hd in
00000000  68 65 6c 6c 6f 0a 67 6f  6f 64 62 79 65           |hello.goodbye|
0000000d
%perl ed in2
4595
P
*1,5n
1	#!/usr/bin/perl
2	
3	=begin metadata
4	
5	Name: awk
*0r in
Newline appended
13
*1,5n
1	hello
2	goodbye
3	#!/usr/bin/perl
4	
5	=begin metadata
*Q

* ed requires that all lines in its buffer are terminated with "\n"
* ed attempts to append a "\n" if it is missing on the last line of input
* The existing append logic in edEdit() was observed to only work for 'E' and 'e' commands, but not 'r'
* With E/e the entire buffer is destroyed and replaced with the content from the new file
* The append logic would then check the final line in buffer *after* the buffer was replaced (this worked ok)
* The 'r' command inserts the file content to any position within the existing buffer (e.g. 0r to insert to beginning of buffer)
* Checking for missing newline at end of buffer doesn't work if the data is inserted at a different location in buffer
* More general fix: check final line of tmp_lines list before it is applied to the buffer
* To test, construct a file 'in' with a missing newline at end, then read it into the beginning of buffer (initiated from file 'in2')

%hd in
00000000  68 65 6c 6c 6f 0a 67 6f  6f 64 62 79 65           |hello.goodbye|
0000000d
%perl ed in2
4595
P
*1,5n
1	#!/usr/bin/perl
2	
3	=begin metadata
4	
5	Name: awk
*0r in
Newline appended
13
*1,5n
1	hello
2	goodbye
3	#!/usr/bin/perl
4	
5	=begin metadata
*Q
@github-actions github-actions bot added Type: enhancement improve a feature that already exists Priority: low get to this whenever Program: ed The ed program labels Nov 22, 2023
@briandfoy briandfoy self-assigned this Nov 24, 2023
@briandfoy briandfoy merged commit 5eced3c into briandfoy:master Nov 24, 2023
1 check passed
@briandfoy briandfoy added Status: accepted The fix is accepted Status: released there is a new release with this fix and removed Priority: low get to this whenever Status: accepted The fix is accepted labels Nov 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Program: ed The ed program Status: released there is a new release with this fix Type: enhancement improve a feature that already exists
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants