plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

readlist.c (1993B)


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