xref: /plan9-contrib/sys/src/cmd/strings.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 #include	<u.h>
2 #include 	<libc.h>
3 #include	<bio.h>
4 
5 Biobuf	*fin;
6 Biobuf	fout;
7 
8 #define	MINSPAN		6		/* Min characters in string */
9 
10 #define BUFSIZE		70
11 
12 void stringit(char *);
13 int isprint(Rune);
14 
15 void
16 main(int argc, char **argv)
17 {
18 	int i;
19 
20 	Binit(&fout, 1, OWRITE);
21 	if(argc < 2) {
22 		stringit("/fd/0");
23 		exits(0);
24 	}
25 
26 	for(i = 1; i < argc; i++) {
27 		if(argc > 2)
28 			print("%s:\n", argv[i]);
29 
30 		stringit(argv[i]);
31 	}
32 
33 	exits(0);
34 }
35 
36 void
37 stringit(char *str)
38 {
39 	long posn, start;
40 	int cnt = 0;
41 	long c;
42 
43 	Rune buf[BUFSIZE];
44 
45 	if ((fin = Bopen(str, OREAD)) == 0) {
46 		perror("open");
47 		return;
48 	}
49 
50 	start = 0;
51 	posn = BOFFSET(fin);
52 	while((c = Bgetrune(fin)) >= 0) {
53 		if(isprint(c)) {
54 			if(start == 0)
55 				start = posn;
56 			buf[cnt++] = c;
57 			if(cnt == BUFSIZE-1) {
58 				buf[cnt] = 0;
59 				Bprint(&fout, "%8d: %S ...\n", start, buf);
60 				start = 0;
61 				cnt = 0;
62 			}
63 		} else {
64 			 if(cnt >= MINSPAN) {
65 				buf[cnt] = 0;
66 				Bprint(&fout, "%8d: %S\n", start, buf);
67 			}
68 			start = 0;
69 			cnt = 0;
70 		}
71 		posn = BOFFSET(fin);
72 	}
73 
74 	if(cnt >= MINSPAN){
75 		buf[cnt] = 0;
76 		Bprint(&fout, "%8d: %S\n", start, buf);
77 	}
78 	Bterm(fin);
79 }
80 
81 int
82 isprint(Rune r)
83 {
84 	if ((r >= ' ' && r <0x7f) || r > 0xA0)
85 		return 1;
86 	else
87 		return 0;
88 }
89