occtl: corrected bug in json checks

This commit is contained in:
Nikos Mavrogiannopoulos
2015-07-01 11:37:54 +02:00
parent 800021234c
commit 62050bec87
4 changed files with 24 additions and 21 deletions

View File

@@ -102,7 +102,7 @@ void print_iface_stats(const char *iface, time_t since, FILE * out, cmd_params_s
bytes2human(rx, buf1, sizeof(buf1), NULL);
bytes2human(tx, buf2, sizeof(buf2), NULL);
if (params->json) {
if (HAVE_JSON(params)) {
fprintf(out, " \"RX\": \"%"PRIu64"\",\n \"TX\": \"%"PRIu64"\",\n", rx, tx);
fprintf(out, " \"_RX\": \"%s\",\n \"_TX\": \"%s\",\n", buf1, buf2);
} else
@@ -110,7 +110,7 @@ void print_iface_stats(const char *iface, time_t since, FILE * out, cmd_params_s
value2speed(rx, diff, buf1, sizeof(buf1));
value2speed(tx, diff, buf2, sizeof(buf2));
if (params->json)
if (HAVE_JSON(params))
fprintf(out, " \"Average RX\": \"%s\",\n \"Average TX\": \"%s\"%s\n", buf1, buf2, have_more?",":"");
else
fprintf(out, "\tAverage bandwidth RX: %s TX: %s\n", buf1, buf2);

View File

@@ -41,7 +41,7 @@ int print_list_entries(FILE* out, cmd_params_st *params, const char* name, char
const char * tmp;
unsigned int i = 0;
if (params->json) {
if (HAVE_JSON(params)) {
fprintf(out, " \"%s\":\t[", name);
for (i=0;i<vsize;i++) {
tmp = val[i];
@@ -70,31 +70,31 @@ int print_list_entries(FILE* out, cmd_params_st *params, const char* name, char
void print_start_block(FILE *out, cmd_params_st *params)
{
if (params->json)
if (HAVE_JSON(params))
fprintf(out, " {\n");
}
void print_end_block(FILE *out, cmd_params_st *params, unsigned have_more)
{
if (params->json)
if (HAVE_JSON(params))
fprintf(out, " }%s\n", have_more?",":"");
}
void print_array_block(FILE *out, cmd_params_st *params)
{
if (params->json)
if (HAVE_JSON(params))
fprintf(out, "[\n");
}
void print_end_array_block(FILE *out, cmd_params_st *params)
{
if (params->json)
if (HAVE_JSON(params))
fprintf(out, "]\n");
}
void print_separator(FILE *out, cmd_params_st *params)
{
if (!params->json)
if (NO_JSON(params))
fprintf(out, "\n");
}
@@ -104,7 +104,7 @@ void print_single_value(FILE *out, cmd_params_st *params, const char *name, cons
if (value[0] == 0)
return;
if (params->json)
if (HAVE_JSON(params))
fprintf(out, " \"%s\": \"%s\"%s\n", name, escape_val(tmp, sizeof(tmp), value), have_more?",":"");
else
fprintf(out, "\t%s: %s\n", name, value);
@@ -112,7 +112,7 @@ void print_single_value(FILE *out, cmd_params_st *params, const char *name, cons
void print_single_value_int(FILE *out, cmd_params_st *params, const char *name, long i, unsigned have_more)
{
if (params->json)
if (HAVE_JSON(params))
fprintf(out, " \"%s\": \%lu%s\n", name, i, have_more?",":"");
else
fprintf(out, "\t%s: %lu\n", name, i);
@@ -124,7 +124,7 @@ void print_single_value_ex(FILE *out, cmd_params_st *params, const char *name, c
if (value[0] == 0)
return;
if (params->json) {
if (HAVE_JSON(params)) {
fprintf(out, " \"%s\": \"%s\",\n", name, escape_val(tmp, sizeof(tmp), value));
fprintf(out, " \"_%s\": \"%s\"%s\n", name, escape_val(tmp, sizeof(tmp), ex), have_more?",":"");
} else
@@ -134,7 +134,7 @@ void print_single_value_ex(FILE *out, cmd_params_st *params, const char *name, c
void print_pair_value(FILE *out, cmd_params_st *params, const char *name1, const char *value1, const char *name2, const char *value2, unsigned have_more)
{
char tmp[MAX_STR_SIZE];
if (params->json) {
if (HAVE_JSON(params)) {
if (value1 && value1[0] != 0)
fprintf(out, " \"%s\": \"%s\"%s\n", name1, escape_val(tmp, sizeof(tmp), value1), have_more?",":"");
if (value2 && value2[0] != 0)

View File

@@ -227,7 +227,7 @@ int handle_status_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st *para
init_reply(&raw);
print_start_block(stdout, params);
if (!params->json)
if (NO_JSON(params))
printf("OpenConnect SSL VPN server\n");
ret = send_cmd(ctx, CTL_CMD_STATUS, NULL, NULL, NULL, &raw);
@@ -553,7 +553,7 @@ int handle_list_users_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st *
if (rep == NULL)
goto error;
if (params->json) {
if (HAVE_JSON(params)) {
common_info_cmd(rep, out, params);
} else for (i=0;i<rep->n_user;i++) {
username = rep->user[i]->username;
@@ -661,7 +661,7 @@ int handle_list_banned_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st
continue;
}
if (i == 0 && !params->json) {
if (i == 0 && NO_JSON(params)) {
fprintf(out, "%14s %14s %30s\n",
"IP", "score", "expires");
}
@@ -669,7 +669,7 @@ int handle_list_banned_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st
print_time_ival7(tmpbuf, t, time(0));
if (params->json) {
if (HAVE_JSON(params)) {
print_single_value(out, params, "IP", rep->info[i]->ip, 1);
print_single_value_ex(out, params, "Since", str_since, tmpbuf, 1);
print_single_value_int(out, params, "Score", rep->info[i]->score, 0);
@@ -678,13 +678,13 @@ int handle_list_banned_cmd(struct unix_ctx *ctx, const char *arg, cmd_params_st
rep->info[i]->ip, (unsigned)rep->info[i]->score, str_since, tmpbuf);
}
} else {
if (i == 0 && !params->json) {
if (i == 0 && NO_JSON(params)) {
fprintf(out, "%14s %14s\n",
"IP", "score");
}
print_start_block(out, params);
if (params->json) {
if (HAVE_JSON(params)) {
print_single_value(out, params, "IP", rep->info[i]->ip, 1);
print_single_value_int(out, params, "Score", rep->info[i]->score, 0);
} else {
@@ -754,7 +754,7 @@ int common_info_cmd(UserListRep * args, FILE *out, cmd_params_st *params)
init_pager = 1;
}
if (params->json)
if (HAVE_JSON(params))
fprintf(out, "[\n");
for (i=0;i<args->n_user;i++) {
@@ -852,7 +852,7 @@ int common_info_cmd(UserListRep * args, FILE *out, cmd_params_st *params)
at_least_one = 1;
}
if (params->json)
if (HAVE_JSON(params))
fprintf(out, "]\n");
ret = 0;
@@ -863,7 +863,7 @@ int common_info_cmd(UserListRep * args, FILE *out, cmd_params_st *params)
goto cleanup;
cleanup:
if (at_least_one == 0) {
if (!params->json)
if (NO_JSON(params))
fprintf(out, "user or ID not found\n");
ret = 2;
}

View File

@@ -15,6 +15,9 @@
#define DATE_TIME_FMT "%Y-%m-%d %H:%M"
#define MAX_TMPSTR_SIZE 64
#define NO_JSON(params) (!params || !params->json)
#define HAVE_JSON(params) (params && params->json)
typedef struct cmd_params_st {
unsigned json;
} cmd_params_st;