csquery.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <bio.h>
  12. char *server;
  13. char *status;
  14. int statusonly;
  15. void
  16. usage(void)
  17. {
  18. fprint(2, "usage: ndb/csquery [/net/cs [addr...]]\n");
  19. exits("usage");
  20. }
  21. void
  22. query(char *addr)
  23. {
  24. char buf[128];
  25. int fd, n;
  26. fd = open(server, ORDWR);
  27. if(fd < 0)
  28. sysfatal("cannot open %s: %r", server);
  29. if(write(fd, addr, strlen(addr)) != strlen(addr)){
  30. if(!statusonly)
  31. fprint(2, "translating %s: %r\n", addr);
  32. status = "errors";
  33. close(fd);
  34. return;
  35. }
  36. if(!statusonly){
  37. seek(fd, 0, 0);
  38. while((n = read(fd, buf, sizeof(buf)-1)) > 0){
  39. buf[n] = 0;
  40. print("%s\n", buf);
  41. }
  42. }
  43. close(fd);
  44. }
  45. void
  46. main(int argc, char **argv)
  47. {
  48. char *p;
  49. int i;
  50. Biobuf in;
  51. ARGBEGIN{
  52. case 's':
  53. statusonly = 1;
  54. break;
  55. default:
  56. usage();
  57. }ARGEND
  58. if(argc > 0)
  59. server = argv[0];
  60. else
  61. server = "/net/cs";
  62. if(argc > 1){
  63. for(i=1; i<argc; i++)
  64. query(argv[i]);
  65. exits(status);
  66. }
  67. Binit(&in, 0, OREAD);
  68. for(;;){
  69. print("> ");
  70. p = Brdline(&in, '\n');
  71. if(p == 0)
  72. break;
  73. p[Blinelen(&in)-1] = 0;
  74. query(p);
  75. }
  76. exits(nil);
  77. }