From 54d371e4e897fa58c316fcc3b6dc7bde5af6868c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 11 Oct 2017 22:28:00 +0200 Subject: Third --- controller/main.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 66 insertions(+), 10 deletions(-) (limited to 'controller') diff --git a/controller/main.c b/controller/main.c index d289ce9..be13773 100644 --- a/controller/main.c +++ b/controller/main.c @@ -172,16 +172,7 @@ static void frame_data_sink(u8 *newdata,i64 newdatalen,void *payload){ bufferlen-=cursor; } -int main(int argc,char **argv){ - if(argc<3||argc>4){ - fprintf(stderr,"Usage: %s [port=57575]\n",argv[0]); - return 1; - } - - const char *logfname=argv[1]; - const char *ipaddr=argv[2]; - const char *port=argc==4?argv[4]:"57575"; - +static int connect_server(const char *logfname,const char *ipaddr,const char *port){ logfile=fopen(logfname,"w"); if(logfile==NULL){ fprintf(stderr,"Cannot open log file '%s'\n",logfname); @@ -284,3 +275,68 @@ cleanup: if(haderror)return 1; return 0; } + +static int read_zz_file(const char *zzfname,const char *logfname){ + logfile=fopen(logfname,"w"); + if(logfile==NULL){ + fprintf(stderr,"Cannot open log file '%s'\n",logfname); + return 1; + } + + FILE *f=fopen(zzfname,"r"); + if(f==NULL){ + fprintf(stderr,"Cannot open zz file '%s'\n",zzfname); + return 1; + } + + struct unzbuffer *z=unzbuffer_init(frame_data_sink,NULL); + u8 buf[4096]; + while(true){ + i64 nr=fread(buf,1,sizeof buf,f); + if(nr<=0){ + if(ferror(f)){ + fprintf(stderr,"An error occurred reading the zz file '%s'\n",zzfname); + return 1; + } + break; + } + if(!unzbuffer_write(z,buf,nr)){ + return 1; + } + } + if(!unzbuffer_finish_destroy(z))return 1; + return 0; +} + +int main(int argc,char **argv){ + if(argc<=1){ + fprintf(stderr,"Usage:\n"); + fprintf(stderr," %s connect [port=57575]\n",argv[0]); + fprintf(stderr," %s read \n",argv[0]); + fprintf(stderr," will probably contain JSON.\n"); + fprintf(stderr," should contain zlib-compressed protocol data.\n"); + return 1; + } + + if(strcmp(argv[1],"connect")==0){ + if(argc<4||argc>5){ + fprintf(stderr,"Invalid number of arguments, run without arguments for usage\n"); + return 1; + } + const char *logfname=argv[2]; + const char *ipaddr=argv[3]; + const char *port=argc==5?argv[4]:"57575"; + return connect_server(logfname,ipaddr,port); + } else if(strcmp(argv[1],"read")==0){ + if(argc!=4){ + fprintf(stderr,"Invalid number of arguments, run without arguments for usage\n"); + return 1; + } + const char *zzfname=argv[2]; + const char *logfname=argv[3]; + return read_zz_file(zzfname,logfname); + } else { + fprintf(stderr,"Invalid command '%s'; run without arguments for usage\n",argv[1]); + return 1; + } +} -- cgit v1.2.3-70-g09d2