Skip to content

Commit

Permalink
operations: implement "c" cmd
Browse files Browse the repository at this point in the history
Does not handle correctly two addrs (should technically only "activate"
at the very end of the range), this is due to the translator having no
knowledge of what comes after two addresses and also not keeping track
of any scopes. Handling this correctly might prove quite tricky with the
current design.
  • Loading branch information
lhoursquentin committed Apr 12, 2020
1 parent e402c94 commit 41c1df5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,17 @@ Files generated.c and - are identical

# Notes

- Some commands are missing (currently c, l, w, r, y), and some features are
missing as well (empty label jumps, "#n" marker).
Supporting those is planned.
- Missing commands (supporting those is planned):
- l
- r
- w
- y

- Missing/incomplete features (supporting those is planned):
- empty label jumps
- #n marker
- c command correct behavior with 2 addresses
- s command nth and w options

- The translator does not handle invalid sed scripts, it will just generate
invalid C code which will probably fail to compile, make sure you can run your
Expand All @@ -164,7 +172,7 @@ Files generated.c and - are identical
option. Also check out the [POSIX specification](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sed.html).

- It is assumed no options are passed to `sed` (`-n` is a common option for
instance), supporting those is not planned.
instance), supporting those is not planned for the moment.

- There are some bugs, the C code is very rough around the edges (by that I mean
dirty and unsafe, for instance allocating everything on the stack without
Expand Down
6 changes: 6 additions & 0 deletions operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ void a(Status *const status, const char *const output) {
status->pending_output[status->pending_output_counter++] = output;
}

void c(Status *const status, const char *const output) {
char *const pattern_space = status->pattern_space;
pattern_space[0] = '\0';
puts(output);
}

void d(Status *const status) {
status->pattern_space[0] = '\0';
}
Expand Down
1 change: 1 addition & 0 deletions operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define S_OPT_P 0x02

void a(Status *const status, const char *const output);
void c(Status *const status, const char *const output);
void d(Status *const status);
operation_ret D(Status *const status);
void equal(const Status *const status);
Expand Down
7 changes: 4 additions & 3 deletions par.sed
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ s/^[Nn]/if (&(\&status) == BREAK) break;\
/
t single_char_cmd

s/^\([ai]\)[[:blank:]]*\\$/\1/; t ai_cmds
s/^\([aci]\)[[:blank:]]*\\$/\1/; t aci_cmds

# TODO missing cmds
# aci
Expand Down Expand Up @@ -380,10 +380,10 @@ t start
s/^/label cmds cleanup: /
b fail

: ai_cmds
: aci_cmds
N
s/\\$//
t ai_cmds
t aci_cmds
# remove first newline
s/\n//
# "\n" -> '\n'
Expand All @@ -396,6 +396,7 @@ s/[\"]/\\&/g
s/\n/\\n/g
s/^i\(.*\)/i("\1");/
s/^a\(.*\)/a(\&status, "\1");/
s/^c\(.*\)/{ c(\&status, "\1"); continue; }/
n
b start

Expand Down
8 changes: 8 additions & 0 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,12 @@ bar
baz
funk'

verify 'c \
punk\
dunk
s/^/x/' 'foo
bar
baz
funk'

end_tests

0 comments on commit 41c1df5

Please sign in to comment.