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