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 (default) */
9 #define BUFSIZE 70
10
11 void stringit(char *);
12 int isprint(Rune);
13
14 static int minspan = MINSPAN;
15
16 static void
usage(void)17 usage(void)
18 {
19 fprint(2, "usage: %s [-m min] [file...]\n", argv0);
20 exits("usage");
21 }
22
23 void
main(int argc,char ** argv)24 main(int argc, char **argv)
25 {
26 int i;
27
28 ARGBEGIN{
29 case 'm':
30 minspan = atoi(EARGF(usage()));
31 break;
32 default:
33 usage();
34 break;
35 }ARGEND
36 Binit(&fout, 1, OWRITE);
37 if(argc < 1) {
38 stringit("/fd/0");
39 exits(0);
40 }
41
42 for(i = 0; i < argc; i++) {
43 if(argc > 2)
44 print("%s:\n", argv[i]);
45
46 stringit(argv[i]);
47 }
48
49 exits(0);
50 }
51
52 void
stringit(char * str)53 stringit(char *str)
54 {
55 long posn, start;
56 int cnt = 0;
57 long c;
58
59 Rune buf[BUFSIZE];
60
61 if ((fin = Bopen(str, OREAD)) == 0) {
62 perror("open");
63 return;
64 }
65
66 start = 0;
67 posn = Boffset(fin);
68 while((c = Bgetrune(fin)) >= 0) {
69 if(isprint(c)) {
70 if(start == 0)
71 start = posn;
72 buf[cnt++] = c;
73 if(cnt == BUFSIZE-1) {
74 buf[cnt] = 0;
75 Bprint(&fout, "%8ld: %S ...\n", start, buf);
76 start = 0;
77 cnt = 0;
78 }
79 } else {
80 if(cnt >= minspan) {
81 buf[cnt] = 0;
82 Bprint(&fout, "%8ld: %S\n", start, buf);
83 }
84 start = 0;
85 cnt = 0;
86 }
87 posn = Boffset(fin);
88 }
89
90 if(cnt >= minspan){
91 buf[cnt] = 0;
92 Bprint(&fout, "%8ld: %S\n", start, buf);
93 }
94 Bterm(fin);
95 }
96
97 int
isprint(Rune r)98 isprint(Rune r)
99 {
100 if (r != Runeerror)
101 if ((r >= ' ' && r < 0x7F) || r > 0xA0)
102 return 1;
103 return 0;
104 }
105