summaryrefslogtreecommitdiff
path: root/controller
diff options
context:
space:
mode:
Diffstat (limited to 'controller')
-rw-r--r--controller/main.c76
1 files changed, 66 insertions, 10 deletions
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 <logfile> <ip addr> [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 <logfile> <ip addr> [port=57575]\n",argv[0]);
+ fprintf(stderr," %s read <zzfile> <logfile>\n",argv[0]);
+ fprintf(stderr,"<logfile> will probably contain JSON.\n");
+ fprintf(stderr,"<zzfile> 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;
+ }
+}