query.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /*
  10. * search the network database for matches
  11. */
  12. #include <u.h>
  13. #include <libc.h>
  14. #include <bio.h>
  15. #include <ndb.h>
  16. static int all, multiple;
  17. static Biobuf bout;
  18. void
  19. usage(void)
  20. {
  21. fprint(2, "usage: query [-am] [-f ndbfile] attr value "
  22. "[returned-attr [reps]]\n");
  23. exits("usage");
  24. }
  25. /* print values of nt's attributes matching rattr */
  26. static void
  27. prmatch(Ndbtuple *nt, char *rattr)
  28. {
  29. for(; nt; nt = nt->entry)
  30. if (strcmp(nt->attr, rattr) == 0)
  31. Bprint(&bout, "%s\n", nt->val);
  32. }
  33. void
  34. search(Ndb *db, char *attr, char *val, char *rattr)
  35. {
  36. char *p;
  37. Ndbs s;
  38. Ndbtuple *t, *nt;
  39. /* first entry with a matching rattr */
  40. if(rattr && !all){
  41. p = ndbgetvalue(db, &s, attr, val, rattr, &t);
  42. if (multiple)
  43. prmatch(t, rattr);
  44. else if(p)
  45. Bprint(&bout, "%s\n", p);
  46. ndbfree(t);
  47. free(p);
  48. return;
  49. }
  50. /* all entries with matching rattrs */
  51. if(rattr) {
  52. for(t = ndbsearch(db, &s, attr, val); t != nil;
  53. t = ndbsnext(&s, attr, val)){
  54. prmatch(t, rattr);
  55. ndbfree(t);
  56. }
  57. return;
  58. }
  59. /* all entries */
  60. for(t = ndbsearch(db, &s, attr, val); t; t = ndbsnext(&s, attr, val)){
  61. for(nt = t; nt; nt = nt->entry)
  62. Bprint(&bout, "%s=%s ", nt->attr, nt->val);
  63. Bprint(&bout, "\n");
  64. ndbfree(t);
  65. }
  66. }
  67. void
  68. main(int argc, char **argv)
  69. {
  70. int reps = 1;
  71. char *rattr = nil, *dbfile = nil;
  72. Ndb *db;
  73. ARGBEGIN{
  74. case 'a':
  75. all++;
  76. break;
  77. case 'm':
  78. multiple++;
  79. break;
  80. case 'f':
  81. dbfile = EARGF(usage());
  82. break;
  83. default:
  84. usage();
  85. }ARGEND;
  86. switch(argc){
  87. case 4:
  88. reps = atoi(argv[3]); /* wtf use is this? */
  89. /* fall through */
  90. case 3:
  91. rattr = argv[2];
  92. break;
  93. case 2:
  94. rattr = nil;
  95. break;
  96. default:
  97. usage();
  98. }
  99. if(Binit(&bout, 1, OWRITE) == -1)
  100. sysfatal("Binit: %r");
  101. db = ndbopen(dbfile);
  102. if(db == nil){
  103. fprint(2, "%s: no db files\n", argv0);
  104. exits("no db");
  105. }
  106. while(reps--)
  107. search(db, argv[0], argv[1], rattr);
  108. ndbclose(db);
  109. exits(0);
  110. }