Skip to content

Commit

Permalink
operations: implement "a" cmd
Browse files Browse the repository at this point in the history
Also give i cmd a body instead of directly generating a puts from the
translator.
  • Loading branch information
lhoursquentin committed Mar 29, 2020
1 parent 0b7981a commit 2d37479
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ Files generated.c and - are identical

# Notes

- Some commands are missing (currently a, c, w, r, y), and some features are
missing as well (empty label jumps, 0 and $ line addresses).
- Some commands are missing (currently c, l, w, r, y), and some features are
missing as well (empty label jumps, "$" line address, "#n" marker).
Supporting those is planned.

- The translator does not handle invalid sed scripts, it will just generate
Expand Down
8 changes: 8 additions & 0 deletions operations.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ void x(Status *const status) {
status->hold_space = pattern_space;
}

void a(Status *const status, const char *const output) {
status->pending_output[status->pending_output_counter++] = output;
}

void d(Status *const status) {
status->pattern_space[0] = '\0';
}
Expand Down Expand Up @@ -289,6 +293,10 @@ void H(Status *status) {
hold_space[hold_space_len] = '\n';
}

void i(const char *const output) {
puts(output);
}

operation_ret n(Status *const status) {
puts(status->pattern_space);
if (!read_pattern(status, status->pattern_space, PATTERN_SIZE)) {
Expand Down
2 changes: 2 additions & 0 deletions operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
#define S_OPT_G 0x01
#define S_OPT_P 0x02

void a(Status *const status, const char *const output);
void d(Status *const status);
operation_ret D(Status *const status);
void equal(const Status *const status);
void g(Status *const status);
void G(Status *const status);
void h(Status *const status);
void H(Status *const status);
void i(const char *const output);
operation_ret n(Status *const status);
operation_ret N(Status *const status);
void p(const Status *const status);
Expand Down
11 changes: 6 additions & 5 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/^i[[:blank:]]*\\$//; t i_cmd
s/^\([ai]\)[[:blank:]]*\\$/\1/; t ai_cmds

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

: i_cmd
: ai_cmds
N
s/\\$//
t i_cmd
t ai_cmds
# remove first newline
s/.//
s/\n//
# "\n" -> '\n'
s/\\n/\n/g
# \<any char> -> <any char>
Expand All @@ -412,7 +412,8 @@ s/\\\(.\)/\1/g
s/[\"]/\\&/g
# '\n' -> "\n" for the C
s/\n/\\n/g
s/.*/puts("&");/
s/^i\(.*\)/i("\1");/
s/^a\(.*\)/a(\&status, "\1");/
n
b start

Expand Down
5 changes: 5 additions & 0 deletions read.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include "status.h"

bool read_pattern(Status *const status, char *const buf, const int size) {
for (int i = 0; i < status->pending_output_counter; ++i) {
puts(status->pending_output[i]);
}
status->pending_output_counter = 0;

if (!fgets(buf, size, stdin)) {
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions sed-bin.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ int main(int argc, char **argv) {
.last_pattern = NULL,
.range_ids = (int [MAX_ACTIVE_RANGES]){},
.suppressed_range_ids = (int [MAX_ACTIVE_RANGES]){},
.pending_output = (const char *[MAX_PENDING_OUTPUT]){},
.pending_output_counter = 0,
};

while (true) {
Expand Down
3 changes: 3 additions & 0 deletions status.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define PATTERN_SIZE 1024
#define MAX_MATCHES 9
#define MAX_ACTIVE_RANGES 100
#define MAX_PENDING_OUTPUT 100

typedef enum {
CONTINUE,
Expand All @@ -21,6 +22,8 @@ typedef struct {
const char *last_pattern;
int *const range_ids;
int *const suppressed_range_ids;
const char **const pending_output;
int pending_output_counter;
} Status;

#endif /* STATUS_H */
29 changes: 29 additions & 0 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,21 @@ bar
funk
fax'

verify '\,foo,,/funk/s,^,x,' 'foo
bar
funk
fax'

verify '/foo/,\,funk,s,^,x,' 'foo
bar
funk
fax'

verify '\,foo,,\,funk,s,^,x,' 'foo
bar
funk
fax'

verify '/foo/,/funk/s/^/x/; 1n' 'foo
bar
funk
Expand Down Expand Up @@ -325,4 +340,18 @@ bar
funk
fax'

verify 'a \
funk\
punk
s/^/x/
a \
yo\
yeah
s/^/y/
n
s/^/z/' 'foo
bar
baz
funk'

end_tests

0 comments on commit 2d37479

Please sign in to comment.