diff options
Diffstat (limited to 'appl/cmd/strings.b')
| -rw-r--r-- | appl/cmd/strings.b | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/appl/cmd/strings.b b/appl/cmd/strings.b new file mode 100644 index 00000000..9f806fa5 --- /dev/null +++ b/appl/cmd/strings.b @@ -0,0 +1,87 @@ +# +# initially generated by c2l +# + +implement Strings; + +include "draw.m"; + +Strings: module +{ + init: fn(nil: ref Draw->Context, argl: list of string); +}; + +include "sys.m"; + sys: Sys; +include "bufio.m"; + bufio: Bufio; + Iobuf: import bufio; + +MINSPAN: con 6; +BUFSIZE: con 70; + +init(nil: ref Draw->Context, argl: list of string) +{ + sys = load Sys Sys->PATH; + bufio = load Bufio Bufio->PATH; + argc := len argl; + if(argc < 2){ + stringit(""); + exit; + } + argl = tl argl; + for(i := 1; i < argc; i++){ + if(argc > 2) + sys->print("%s:\n", hd argl); + stringit(hd argl); + argl = tl argl; + } +} + +stringit(str: string) +{ + cnt := 0; + c: int; + buf := string array[BUFSIZE] of { * => byte 'z' }; + + if(str == nil) + fin := bufio->fopen(sys->fildes(0), Bufio->OREAD); + else + fin = bufio->open(str, Bufio->OREAD); + if(fin == nil){ + sys->fprint(sys->fildes(2), "cannot open %s\n", str); + return; + } + start := big -1; + posn := fin.offset(); + while((c = fin.getc()) >= 0){ + if(isprint(c)){ + if(start == big -1) + start = posn; + buf[cnt++] = c; + if(cnt == BUFSIZE){ + sys->print("%8bd: %s ...\n", start, buf[0: cnt]); + start = big -1; + cnt = 0; + } + } + else{ + if(cnt >= MINSPAN) + sys->print("%8bd: %s\n", start, buf[0: cnt]); + start = big -1; + cnt = 0; + } + posn = fin.offset(); + } + if(cnt >= MINSPAN) + sys->print("%8bd: %s\n", start, buf[0: cnt]); + fin = nil; +} + +isprint(r: int): int +{ + if(r >= ' ' && r < 16r7f || r > 16ra0) + return 1; + else + return 0; +} |
