plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

plumb.c (2236B)


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