aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-12-20 23:28:30 -0600
committerRob Landley <rob@landley.net>2014-12-20 23:28:30 -0600
commit32cd2b770fe3ac8c419a29156162f3a037cf47a3 (patch)
treec5647659fa79f133734ed02473b8613c71b5c192
parenta136fa5704a789cf9ef93fb7987a0dc342b81898 (diff)
downloadtoybox-32cd2b770fe3ac8c419a29156162f3a037cf47a3.tar.gz
sed: implement 'l'
-rw-r--r--toys/pending/sed.c34
1 files changed, 29 insertions, 5 deletions
diff --git a/toys/pending/sed.c b/toys/pending/sed.c
index 070022c4..a5c24454 100644
--- a/toys/pending/sed.c
+++ b/toys/pending/sed.c
@@ -83,9 +83,9 @@ config SED
H Remember this line (appending to remembered line, if any)
- l Print this line, escaping \abfrtv (but leaving \n as a newline),
- using octal escapes for other nonprintable characters, and
- wrapping lines to terminal width with a backslash and newline
+ l Print line, escaping \abfrtv (but not newline), octal escaping other
+ nonprintable characters, wrapping lines to terminal width with a
+ backslash, and appending $ to actual end of line.
n Print default output and read next line, replacing current line
(If no next line available, quit processing script)
@@ -175,6 +175,7 @@ GLOBALS(
void *restart, *lastregex;
long nextlen, rememberlen, count;
int fdout, noeol;
+ unsigned xx;
)
struct step {
@@ -425,8 +426,31 @@ static void walk_pattern(char **pline, long plen)
} else if (c=='i') {
str = logrus->arg1+(char *)logrus;
emit(str, strlen(str), 1);
-// } else if (c=='l') {
-// error_exit("todo: l");
+ } else if (c=='l') {
+ int i, x, off;
+
+ if (!TT.xx) {
+ terminal_size(&TT.xx, 0);
+ if (!TT.xx) TT.xx = 80;
+ if (TT.xx > sizeof(toybuf)-10) TT.xx = sizeof(toybuf)-10;
+ if (TT.xx > 4) TT.xx -= 4;
+ }
+
+ for (i = off = 0; i<len; i++) {
+ if (off >= TT.xx) {
+ toybuf[off++] = '\\';
+ emit(toybuf, off, 1);
+ off = 0;
+ }
+ x = stridx("\\\a\b\f\r\t\v", line[i]);
+ if (x != -1) {
+ toybuf[off++] = '\\';
+ toybuf[off++] = "\\abfrtv"[x];
+ } else if (line[i] >= ' ') toybuf[off++] = line[i];
+ else off += sprintf(toybuf+off, "\\%03o", line[i]);
+ }
+ toybuf[off++] = '$';
+ emit(toybuf, off, 1);
} else if (c=='n') {
TT.restart = logrus->next;