vtread.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "stdinc.h"
  2. #include <bio.h>
  3. typedef struct Source Source;
  4. struct Source
  5. {
  6. ulong gen;
  7. int psize;
  8. int dsize;
  9. int dir;
  10. int active;
  11. int depth;
  12. uvlong size;
  13. uchar score[VtScoreSize];
  14. int reserved;
  15. };
  16. int bsize;
  17. Biobuf *bout;
  18. int ver;
  19. int cmp;
  20. int all;
  21. int find;
  22. uchar fscore[VtScoreSize];
  23. int dirSize;
  24. void (*parse)(Source*, uchar*);
  25. VtSession *z;
  26. int vtGetUint16(uchar *p);
  27. ulong vtGetUint32(uchar *p);
  28. uvlong vtGetUint48(uchar *p);
  29. void usage(void);
  30. int parseScore(uchar *score, char *buf, int n);
  31. void parse1(Source*, uchar*);
  32. void parse2(Source*, uchar*);
  33. int dumpDir(Source*, int indent);
  34. void
  35. main(int argc, char *argv[])
  36. {
  37. char *host = nil;
  38. uchar score[VtScoreSize];
  39. uchar buf[VtMaxLumpSize];
  40. int type;
  41. int n;
  42. type = VtDataType;
  43. ARGBEGIN{
  44. case 't':
  45. type = atoi(ARGF());
  46. break;
  47. default:
  48. usage();
  49. break;
  50. }ARGEND
  51. vtAttach();
  52. bout = vtMemAllocZ(sizeof(Biobuf));
  53. Binit(bout, 1, OWRITE);
  54. if(argc != 1)
  55. usage();
  56. vtAttach();
  57. fmtinstall('V', vtScoreFmt);
  58. fmtinstall('R', vtErrFmt);
  59. z = vtDial(host, 0);
  60. if(z == nil)
  61. vtFatal("could not connect to server: %s", vtGetError());
  62. if(!vtConnect(z, 0))
  63. sysfatal("vtConnect: %r");
  64. if(!parseScore(score, argv[0], strlen(argv[0])))
  65. vtFatal("could not parse score: %s", vtGetError());
  66. n = vtRead(z, score, type, buf, VtMaxLumpSize);
  67. if(n < 0)
  68. vtFatal("could not read block: %s", vtGetError());
  69. Bwrite(bout, buf, n);
  70. Bterm(bout);
  71. vtClose(z);
  72. vtDetach();
  73. exits(0);
  74. }
  75. void
  76. usage(void)
  77. {
  78. fprint(2, "%s: [-t type] score\n", argv0);
  79. exits("usage");
  80. }
  81. int
  82. parseScore(uchar *score, char *buf, int n)
  83. {
  84. int i, c;
  85. memset(score, 0, VtScoreSize);
  86. if(n < VtScoreSize*2)
  87. return 0;
  88. for(i=0; i<VtScoreSize*2; i++) {
  89. if(buf[i] >= '0' && buf[i] <= '9')
  90. c = buf[i] - '0';
  91. else if(buf[i] >= 'a' && buf[i] <= 'f')
  92. c = buf[i] - 'a' + 10;
  93. else if(buf[i] >= 'A' && buf[i] <= 'F')
  94. c = buf[i] - 'A' + 10;
  95. else {
  96. return 0;
  97. }
  98. if((i & 1) == 0)
  99. c <<= 4;
  100. score[i>>1] |= c;
  101. }
  102. return 1;
  103. }