Blob


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