xref: /plan9/sys/src/cmd/venti/readlist.c (revision 368c31ab13393dea083228fdd1c3445076f83a4b)
1 #include <u.h>
2 #include <libc.h>
3 #include <thread.h>
4 #include <venti.h>
5 #include <bio.h>
6 
7 char *host;
8 Biobuf b;
9 VtConn *z;
10 uchar *buf;
11 void run(Biobuf*);
12 int nn;
13 
14 void
usage(void)15 usage(void)
16 {
17 	fprint(2, "usage: readlist [-h host] list\n");
18 	threadexitsall("usage");
19 }
20 
21 int
parsescore(uchar * score,char * buf,int n)22 parsescore(uchar *score, char *buf, int n)
23 {
24 	int i, c;
25 
26 	memset(score, 0, VtScoreSize);
27 
28 	if(n != VtScoreSize*2){
29 		werrstr("score wrong length %d", n);
30 		return -1;
31 	}
32 	for(i=0; i<VtScoreSize*2; i++) {
33 		if(buf[i] >= '0' && buf[i] <= '9')
34 			c = buf[i] - '0';
35 		else if(buf[i] >= 'a' && buf[i] <= 'f')
36 			c = buf[i] - 'a' + 10;
37 		else if(buf[i] >= 'A' && buf[i] <= 'F')
38 			c = buf[i] - 'A' + 10;
39 		else {
40 			c = buf[i];
41 			werrstr("bad score char %d '%c'", c, c);
42 			return -1;
43 		}
44 
45 		if((i & 1) == 0)
46 			c <<= 4;
47 
48 		score[i>>1] |= c;
49 	}
50 	return 0;
51 }
52 
53 void
threadmain(int argc,char * argv[])54 threadmain(int argc, char *argv[])
55 {
56 	int fd, i;
57 
58 	ARGBEGIN{
59 	case 'h':
60 		host = EARGF(usage());
61 		break;
62 	default:
63 		usage();
64 		break;
65 	}ARGEND
66 
67 	fmtinstall('V', vtscorefmt);
68 	buf = vtmallocz(VtMaxLumpSize);
69 	z = vtdial(host);
70 	if(z == nil)
71 		sysfatal("could not connect to server: %r");
72 	if(vtconnect(z) < 0)
73 		sysfatal("vtconnect: %r");
74 
75 	if(argc == 0){
76 		Binit(&b, 0, OREAD);
77 		run(&b);
78 	}else{
79 		for(i=0; i<argc; i++){
80 			if((fd = open(argv[i], OREAD)) < 0)
81 				sysfatal("open %s: %r", argv[i]);
82 			Binit(&b, fd, OREAD);
83 			run(&b);
84 		}
85 	}
86 	threadexitsall(nil);
87 }
88 
89 void
run(Biobuf * b)90 run(Biobuf *b)
91 {
92 	char *p, *f[10];
93 	int nf;
94 	uchar score[20];
95 	int type, n;
96 
97 	while((p = Brdline(b, '\n')) != nil){
98 		p[Blinelen(b)-1] = 0;
99 		nf = tokenize(p, f, nelem(f));
100 		if(nf != 2)
101 			sysfatal("syntax error in work list");
102 		if(parsescore(score, f[0], strlen(f[0])) < 0)
103 			sysfatal("bad score %s in work list", f[0]);
104 		type = atoi(f[1]);
105 		n = vtread(z, score, type, buf, VtMaxLumpSize);
106 		if(n < 0)
107 			sysfatal("could not read %s %s: %r", f[0], f[1]);
108 		/* write(1, buf, n); */
109 		if(++nn%1000 == 0)
110 			print("%d...", nn);
111 	}
112 }
113