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.
24 #include <sys/socket.h>
32 #include <utils/cmdparse.h>
34 #define STD_RC_DIR INSTALLPATH"/etc"
35 #define STD_SOCK_DIR "/var/run"
37 void usage(char *progname
){
38 /* TODO: write it better */
39 printf("Usage: %s OPTIONS command\n", progname
);
40 printf("\t-s sockname : management socket path\n");
41 printf("\t-f rcfile : configuration path (default is %s/%s)\n", STD_RC_DIR
, progname
);
42 printf("\t-v : run parse machine in debug mode\n");
45 int main(int argc
,char *argv
[])
47 struct sockaddr_un sun
;
54 struct utm_out
*outbuf
;
55 struct utm_buf parsebuf
;
58 static struct option long_options
[] = {
59 {"rcfile", 1, 0, 'f'},
61 {"verbose", 0, 0, 'v'},
64 while ((c
=getopt_long (argc
, argv
, "f:s:v",
65 long_options
, &option_index
)) >= 0)
68 case 'f': rcfile
=strdup(optarg
); break;
69 case 's': sockname
=strdup(optarg
); break;
70 case 'v': debug
=1; break;
74 if(argc
-optind
== 0){ usage(argv
[0]); return -1; }
76 if (!rcfile
) asprintf(&rcfile
,"%s/%s",STD_RC_DIR
,basename(argv
[0]));
77 if( (utm
=utm_alloc(rcfile
)) == NULL
) { perror("alloc parse machine"); usage(argv
[0]); return -1;}
79 if (!sockname
) asprintf(&sockname
,"%s/%s",STD_SOCK_DIR
,basename(argv
[0]));
80 sun
.sun_family
=PF_UNIX
;
81 snprintf(sun
.sun_path
,sizeof(sun
.sun_path
),"%s",sockname
);
82 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
83 if(fcntl(fd
, F_SETFL
, O_NONBLOCK
) < 0){ perror("nonblock"); return -1; }
84 if( connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) ){ perror("connect"); return -1; }
86 memset(&parsebuf
, 0, sizeof(struct utm_buf
));
87 outbuf
=utmout_alloc();
89 rv
=utm_run(utm
,&parsebuf
,fd
,argc
-optind
,argv
+optind
,outbuf
,debug
);
90 if(outbuf
->sz
) write(1, outbuf
->buf
, outbuf
->sz
);