#include #include #include #include static const char *host = NULL; static const char *port = NULL; static int drctl_do (const char *cmd) { glocal int ret = -1; glocal const char *cmd = cmd; (host,port,5); // Nothing to do // printf ("%s\n",line); if (strncmp(line,"drsnap ",7)==0){ extern const char *version; if (strcmp(line+7,version)!=0){ tlmp_error ("Incompatible drsnapd server, expected version %s, got %s\n" ,version,line+7); end = true; } }else if (strncmp(line,"help: ",6)==0){ sendf ("%s\n",glocal.cmd); }else if (strncmp(line,"Ok: ",4)==0){ printf ("%s\n",line+4); glocal.ret = 0; send ("quit\n"); }else if (strncmp(line,"*** ",4)==0){ printf ("ERROR: %s\n",line+4); glocal.ret = -1; send ("quit\n"); }else if (strncmp(line,"# ",2)==0){ printf ("%s\n",line+2); }else{ tlmp_error ("Unknown message from drsnap server: %s",line); end = true; } return glocal.ret; } int main (int argc, char *argv[]) { glocal int ret = -1; glocal.ret = (argc,argv); extern const char *version; setproginfo ("drctl",version ,"Send commands to a remote drsnapd server\n" "\n" "\tbranch branch-name\n" "\t/dev/null on|off\n" "\tsnap\n" "\tsplit new-branch-name\n" "\tsplitrev revision new-branch-name\n" "\tstats\n"); setarg ('h',"host","Host running the nbd-server",host,true); setarg ('p',"port","TCP port to connect to",port,true); fprintf (stderr,"%s\n",msg); int ret = -1; const char *arg0 = argv[0]; if (argc == 1 && strcmp(arg0,"snap")==0){ ret = drctl_do ("snap"); }else if (argc == 2 && strcmp(arg0,"branch")==0){ SSTRING tmp; tmp.setfromf ("%s %s",arg0,argv[1]); ret = drctl_do (tmp.c_str()); }else if (argc == 2 && strcmp(arg0,"split")==0){ SSTRING tmp; tmp.setfromf ("%s %s",arg0,argv[1]); ret = drctl_do (tmp.c_str()); }else if (argc == 3 && strcmp(arg0,"splitrev")==0){ SSTRING tmp; tmp.setfromf ("%s %s %s",arg0,argv[1],argv[2]); ret = drctl_do (tmp.c_str()); }else if (argc == 1 && strcmp(arg0,"stats")==0){ ret = drctl_do (arg0); }else if (argc == 2 && strcmp(arg0,"/dev/null")==0 && (strcmp(argv[1],"on")==0 || strcmp(argv[1],"off")==0)){ SSTRING tmp; tmp.setfromf ("%s %s",arg0,argv[1]); ret = drctl_do (tmp.c_str()); }else{ usage(); } return ret; return glocal.ret; }