]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/strings.c
cc: use 7 octal digits for 21 bit runes
[plan9front.git] / sys / src / cmd / strings.c
index 67cf464ff6964d7b4fdd19c7ff159fdb22d40f15..1cba97e5a487c655857d501e74cedc7709bca11b 100644 (file)
@@ -2,16 +2,14 @@
 #include       <libc.h>
 #include       <bio.h>
 
-Biobuf *fin;
+Biobuf fin;
 Biobuf fout;
 
-#define        MINSPAN         6               /* Min characters in string (default) */
-#define BUFSIZE                70
-
-void stringit(char *);
+void stringit(int);
 int isprint(Rune);
 
-static int minspan = MINSPAN;
+int minspan = 6;       /* Min characters in string (default) */
+Rune *span;
 
 static void
 usage(void)
@@ -23,75 +21,74 @@ usage(void)
 void
 main(int argc, char **argv)
 {
-       int i;
+       int i, fd;
 
        ARGBEGIN{
        case 'm':
                minspan = atoi(EARGF(usage()));
+               if(minspan <= 0)
+                       usage();
                break;
        default:
                usage();
                break;
        }ARGEND
+
+       span = malloc(sizeof(Rune)*(minspan+1));
+       if(span == nil)
+               sysfatal("out of memory");
+
        Binit(&fout, 1, OWRITE);
        if(argc < 1) {
-               stringit("/fd/0");
+               stringit(0);
                exits(0);
        }
 
        for(i = 0; i < argc; i++) {
-               if(argc > 2)
-                       print("%s:\n", argv[i]);
-
-               stringit(argv[i]);
+               if(argc > 1){
+                       Bprint(&fout, "%s:\n", argv[i]);
+                       Bflush(&fout);
+               }
+               if((fd = open(argv[i], OREAD)) < 0){
+                       perror("open");
+                       continue;
+               }
+               stringit(fd);
+               close(fd);
        }
 
        exits(0);
 }
 
 void
-stringit(char *str)
+stringit(int fd)
 {
-       long posn, start;
-       int cnt = 0;
+       Rune *sp;
        long c;
 
-       Rune buf[BUFSIZE];
-
-       if ((fin = Bopen(str, OREAD)) == 0) {
-               perror("open");
-               return;
-       }
-
-       start = 0;
-       posn = Boffset(fin);
-       while((c = Bgetrune(fin)) >= 0) {
+       Binit(&fin, fd, OREAD);
+       sp = span;
+       while((c = Bgetrune(&fin)) >= 0) {
                if(isprint(c)) {
-                       if(start == 0)
-                               start = posn;
-                       buf[cnt++] = c;
-                       if(cnt == BUFSIZE-1) {
-                               buf[cnt] = 0;
-                               Bprint(&fout, "%8ld: %S ...\n", start, buf);
-                               start = 0;
-                               cnt = 0;
+                       if(sp == nil){
+                               Bputrune(&fout, c);
+                               continue;
                        }
+                       *sp++ = c;
+                       if((sp-span) < minspan)
+                               continue;
+                       *sp = 0;
+                       Bprint(&fout, "%8lld: %S", Boffset(&fin)-minspan, span);
+                       sp = nil;
                } else {
-                        if(cnt >= minspan) {
-                               buf[cnt] = 0;
-                               Bprint(&fout, "%8ld: %S\n", start, buf);
-                       }
-                       start = 0;
-                       cnt = 0;
-               }       
-               posn = Boffset(fin);
-       }
-
-       if(cnt >= minspan){
-               buf[cnt] = 0;
-               Bprint(&fout, "%8ld: %S\n", start, buf);
+                       if(sp == nil)
+                               Bputrune(&fout, '\n');
+                       sp = span;
+               }
        }
-       Bterm(fin);
+       if(sp == nil)
+               Bputrune(&fout, '\n');
+       Bterm(&fin);
 }
 
 int