readlist.c 2.3 KB

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