readlist.c 1.9 KB

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