changeset 1618:01d44ee47503 draft

sed: implement 'l'
author Rob Landley <rob@landley.net>
date Sat, 20 Dec 2014 23:28:30 -0600
parents 942e1cacb5b3
children a7e4dc024bbb
files toys/pending/sed.c
diffstat 1 files changed, 29 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/toys/pending/sed.c	Sat Dec 20 14:58:03 2014 -0600
+++ b/toys/pending/sed.c	Sat Dec 20 23:28:30 2014 -0600
@@ -83,9 +83,9 @@
 
       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 @@
   void *restart, *lastregex;
   long nextlen, rememberlen, count;
   int fdout, noeol;
+  unsigned xx;
 )
 
 struct step {
@@ -425,8 +426,31 @@
     } 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;