#include "wstool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../server/config/serverconfig.h"
#include "srvctrlsocket.h"
static void print_info(
char *cmd) {
fprintf(stderr,
"usage:\n");
fprintf(stderr,
"%s -t <srvconfigfile>\n", cmd);
fprintf(stderr,
"%s -s <socketpath> <command>\n", cmd);
fprintf(stderr,
"Commands: reconfig, shutdown, stat, log\n");
}
int main(
int argc,
char **argv) {
if(argc >
2) {
if(!strcmp(argv[
1],
"-t")) {
return tool_get_tmpdir(argv[
2]);
}
else if(!strcmp(argv[
1],
"-s")) {
if(argc !=
4) {
print_info(argv[
0]);
return -
2;
}
return tool_srvctrl(argv[
2], argv[
3]);
}
}
print_info(argv[
0]);
return -
2;
}
int tool_get_tmpdir(
char *configfile) {
ServerConfig *serverconf = serverconfig_load(configfile);
CxList *list = serverconfig_get_node_list(serverconf->root,
CONFIG_NODE_OBJECT, cx_str(
"Runtime"));
if(!list) {
fprintf(stderr,
"Error: No Runtime element in %s\n", configfile);
return -
1;
}
if(list->size !=
1) {
fprintf(stderr,
"Error: Multiple Runtime elements in %s\n", configfile);
return -
1;
}
ConfigNode *runtime = cxListAt(list,
0);
cxstring tmp = serverconfig_object_directive_value(runtime, cx_str(
"Temp"));
cxListDestroy(list);
if(!tmp.ptr) {
fprintf(stderr,
"Error: No Temp directive in Runtime Object\n");
return -
1;
}
printf(
"%.*s\n", (
int)tmp.length, tmp.ptr);
return 0;
}
int tool_srvctrl(
char *socketfile,
char *cmd) {
SrvConnection *srv = srvctrl_connet(socketfile);
if(!srv) {
return -
1;
}
fprintf(srv->stream,
"%s\n", cmd);
fflush(srv->stream);
SrvMsg msg;
while(!srvctrl_readmsg(srv, &msg)) {
if(msg.type ==
0) {
fprintf(stdout,
"%.*s", (
int)msg.length, msg.message);
fflush(stdout);
}
else if(msg.type ==
1) {
fprintf(stderr,
"%.*s", (
int)msg.length, msg.message);
fflush(stderr);
}
}
srvctrl_close(srv);
return 0;
}
int log_ereport(
int degree,
const char *format, ...) {
va_list args;
va_start(args, format);
int ret = log_ereport_v(degree, format, args);
va_end(args);
return ret;
}
int log_ereport_v(
int degree,
const char *format, va_list args) {
return 0;
}