xref: /inferno-os/appl/cmd/strings.b (revision 37da2899f40661e3e9631e497da8dc59b971cbd0)
1#
2#	initially generated by c2l
3#
4
5implement Strings;
6
7include "draw.m";
8
9Strings: module
10{
11	init: fn(nil: ref Draw->Context, argl: list of string);
12};
13
14include "sys.m";
15	sys: Sys;
16include "bufio.m";
17	bufio: Bufio;
18	Iobuf: import bufio;
19
20MINSPAN: con 6;
21BUFSIZE: con 70;
22
23init(nil: ref Draw->Context, argl: list of string)
24{
25	sys = load Sys Sys->PATH;
26	bufio = load Bufio Bufio->PATH;
27	argc := len argl;
28	if(argc < 2){
29		stringit("");
30		exit;
31	}
32	argl = tl argl;
33	for(i := 1; i < argc; i++){
34		if(argc > 2)
35			sys->print("%s:\n", hd argl);
36		stringit(hd argl);
37		argl = tl argl;
38	}
39}
40
41stringit(str: string)
42{
43	cnt := 0;
44	c: int;
45	buf := string array[BUFSIZE] of { * => byte 'z' };
46
47	if(str == nil)
48		fin := bufio->fopen(sys->fildes(0), Bufio->OREAD);
49	else
50		fin = bufio->open(str, Bufio->OREAD);
51	if(fin == nil){
52		sys->fprint(sys->fildes(2), "cannot open %s\n", str);
53		return;
54	}
55	start := big -1;
56	posn := fin.offset();
57	while((c = fin.getc()) >= 0){
58		if(isprint(c)){
59			if(start == big -1)
60				start = posn;
61			buf[cnt++] = c;
62			if(cnt == BUFSIZE){
63				sys->print("%8bd: %s ...\n", start, buf[0: cnt]);
64				start = big -1;
65				cnt = 0;
66			}
67		}
68		else{
69			if(cnt >= MINSPAN)
70				sys->print("%8bd: %s\n", start, buf[0: cnt]);
71			start = big -1;
72			cnt = 0;
73		}
74		posn = fin.offset();
75	}
76	if(cnt >= MINSPAN)
77		sys->print("%8bd: %s\n", start, buf[0: cnt]);
78	fin = nil;
79}
80
81isprint(r: int): int
82{
83	if(r >= ' ' && r < 16r7f || r > 16ra0)
84		return 1;
85	else
86		return 0;
87}
88