2 * Copyright (C) 2007 - Renzo Davoli, Luca Bigliardi
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version 2
6 * of the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/socket.h>
32 #include <vdecommon.h>
34 void usage(char *progname
){
35 /* TODO: write it better */
36 printf("Usage: %s OPTIONS command\n", progname
);
37 printf("\t-s sockname management socket path (default is %s/%s)\n", VDE_SOCK_DIR
, basename(progname
));
38 printf("\t-f rcfile configuration path (default is %s/%s)\n", VDE_RC_DIR
, basename(progname
));
39 printf("\t-v run parse machine in debug mode\n");
42 int main(int argc
,char *argv
[])
44 struct sockaddr_un sun
;
51 struct utm_out
*outbuf
;
52 struct utm_buf parsebuf
;
55 static struct option long_options
[] = {
56 {"rcfile", 1, 0, 'f'},
58 {"verbose", 0, 0, 'v'},
61 while ((c
=getopt_long (argc
, argv
, "f:s:v",
62 long_options
, &option_index
)) >= 0)
65 case 'f': rcfile
=strdup(optarg
); break;
66 case 's': sockname
=strdup(optarg
); break;
67 case 'v': debug
=1; break;
71 if(argc
-optind
== 0){ usage(argv
[0]); return -1; }
73 if (!rcfile
) asprintf(&rcfile
,"%s/%s",VDE_RC_DIR
,basename(argv
[0]));
74 if( (utm
=utm_alloc(rcfile
)) == NULL
) { perror("alloc parse machine"); usage(argv
[0]); return -1;}
76 if (!sockname
) asprintf(&sockname
,"%s/%s",VDE_SOCK_DIR
,basename(argv
[0]));
77 sun
.sun_family
=PF_UNIX
;
78 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s",sockname
);
79 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
80 if(fcntl(fd
, F_SETFL
, O_NONBLOCK
) < 0){ perror("nonblock"); return -1; }
81 if( connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) ){ perror("connect"); return -1; }
83 memset(&parsebuf
, 0, sizeof(struct utm_buf
));
84 outbuf
=utmout_alloc();
86 rv
=utm_run(utm
,&parsebuf
,fd
,argc
-optind
,argv
+optind
,outbuf
,debug
);
87 if(outbuf
->sz
) write(1, outbuf
->buf
, outbuf
->sz
);