plumb.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <plumb.h>
  4. char *plumbfile = nil;
  5. Plumbmsg m;
  6. void
  7. usage(void)
  8. {
  9. fprint(2, "usage: plumb [-p plumbfile] [-a 'attr=value ...'] [-s src] [-d dst] [-t type] [-w wdir] -i | data1\n");
  10. exits("usage");
  11. }
  12. void
  13. gather(void)
  14. {
  15. char buf[8192];
  16. int n;
  17. m.ndata = 0;
  18. m.data = nil;
  19. while((n = read(0, buf, sizeof buf)) > 0){
  20. m.data = realloc(m.data, m.ndata+n);
  21. if(m.data == nil){
  22. fprint(2, "plumb: alloc failed: %r\n");
  23. exits("alloc");
  24. }
  25. memmove(m.data+m.ndata, buf, n);
  26. m.ndata += n;
  27. }
  28. if(n < 0){
  29. fprint(2, "plumb: i/o error on input: %r\n");
  30. exits("read");
  31. }
  32. }
  33. void
  34. main(int argc, char *argv[])
  35. {
  36. char buf[1024], *p;
  37. int fd, i, input;
  38. input = 0;
  39. m.src = "plumb";
  40. m.dst = nil;
  41. m.wdir = getwd(buf, sizeof buf);
  42. m.type = "text";
  43. m.attr = nil;
  44. ARGBEGIN{
  45. case 'a':
  46. p = ARGF();
  47. if(p == nil)
  48. usage();
  49. m.attr = plumbaddattr(m.attr, plumbunpackattr(p));
  50. break;
  51. case 'd':
  52. m.dst = ARGF();
  53. if(m.dst == nil)
  54. usage();
  55. break;
  56. case 'i':
  57. input++;
  58. break;
  59. case 't':
  60. case 'k': /* for backwards compatibility */
  61. m.type = ARGF();
  62. if(m.type == nil)
  63. usage();
  64. break;
  65. case 'p':
  66. plumbfile = ARGF();
  67. if(plumbfile == nil)
  68. usage();
  69. break;
  70. case 's':
  71. m.src = ARGF();
  72. if(m.src == nil)
  73. usage();
  74. break;
  75. case 'w':
  76. m.wdir = ARGF();
  77. if(m.wdir == nil)
  78. usage();
  79. break;
  80. }ARGEND
  81. if((input && argc>0) || (!input && argc<1))
  82. usage();
  83. if(plumbfile != nil)
  84. fd = open(plumbfile, OWRITE);
  85. else
  86. fd = plumbopen("send", OWRITE);
  87. if(fd < 0){
  88. fprint(2, "plumb: can't open plumb file: %r\n");
  89. exits("open");
  90. }
  91. if(input){
  92. gather();
  93. if(plumblookup(m.attr, "action") == nil)
  94. m.attr = plumbaddattr(m.attr, plumbunpackattr("action=showdata"));
  95. if(plumbsend(fd, &m) < 0){
  96. fprint(2, "plumb: can't send message: %r\n");
  97. exits("error");
  98. }
  99. exits(nil);
  100. }
  101. for(i=0; i<argc; i++){
  102. if(input == 0){
  103. m.data = argv[i];
  104. m.ndata = -1;
  105. }
  106. if(plumbsend(fd, &m) < 0){
  107. fprint(2, "plumb: can't send message: %r\n");
  108. exits("error");
  109. }
  110. }
  111. exits(nil);
  112. }