#include "wstool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../server/config/serverconf.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 = load_server_config(configfile);
UcxList *list = ucx_map_sstr_get(serverconf->objects, sstrn(
"Runtime",
7));
if(!list) {
fprintf(stderr,
"Error: No Runtime element in %s\n", configfile);
return -
1;
}
if(ucx_list_size(list) !=
1) {
fprintf(stderr,
"Error: Multiple Runtime elements in %s\n", configfile);
return -
1;
}
ServerConfigObject *runtime = list->data;
sstr_t tmp = cfg_directivelist_get_str(runtime->directives, sstr(
"Temp"));
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;
}