Skip to content

Commit

Permalink
operations: implement "l" cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
lhoursquentin committed Apr 15, 2020
1 parent 097f14c commit 68411ab
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ Files generated.c and - are identical

# Notes

- Missing commands (supporting those is planned):
- l
- Missing command (supporting it is planned):
- w

- Missing/incomplete features (supporting those is planned):
Expand Down
55 changes: 55 additions & 0 deletions operations.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <assert.h>
#include <ctype.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -257,6 +258,60 @@ operation_ret n(Status *const status) {
return 0;
}

void l(const Status *const status) {
const char *const pattern_space = status->pattern_space;
for (int i = 0, fold_counter = 0; pattern_space[i]; ++i, ++fold_counter) {
const char c = pattern_space[i];
if (fold_counter > 80) {
puts("\\");
fold_counter = 0;
}
if (isprint(c)) {
if (c == '\\') { // needs to be doubled
putchar('\\');
fold_counter++;
}
putchar(c);
} else {
fold_counter++;
switch (c) {
case '\n':
// POSIX states:
// > [...] '\t', '\v' ) shall be written as the corresponding escape
// > sequence; the '\n' in that table is not applicable
//
// toybox and gnu sed still print newlines as "\n", I'll choose to
// stick to my understanding of POSIX there.
puts("$");
fold_counter = 0;
case '\a':
printf("\\a");
break;
case '\b':
printf("\\b");
break;
case '\f':
printf("\\f");
break;
case '\r':
printf("\\r");
break;
case '\t':
printf("\\t");
break;
case '\v':
printf("\\v");
break;
default:
fold_counter += 2; // 3 counting the beginning of the else branch
printf("\\%03hho", c);
break;
}
}
}
puts("$");
}

operation_ret N(Status *const status) {
char *const pattern_space = status->pattern_space;
const int pattern_space_len = strlen(pattern_space);
Expand Down
1 change: 1 addition & 0 deletions operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void G(Status *const status);
void h(Status *const status);
void H(Status *const status);
void i(const char *const output);
void l(const Status *const status);
operation_ret n(Status *const status);
operation_ret N(Status *const status);
void p(const Status *const status);
Expand Down
4 changes: 2 additions & 2 deletions par.sed
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ t single_char_cmd

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

# TODO missing cmds
# w, l
# TODO missing cmd
# w

: address_check
s|^/|&|; t addr_regex
Expand Down
2 changes: 2 additions & 0 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ verify 'N; y/a\nb/dxe/' 'Hello
World'
verify 'y///' 'foo'

verify 'l' 'foo \\ \ '"$(printf '\a\b\f\r\t\v')"' é '"$(printf '\001\020\300')"' bar'

verify 'r non-existing-file
s/^/x/' 'bar
baz'
Expand Down

0 comments on commit 68411ab

Please sign in to comment.