Split swanctl --raw mode into single-line and --pretty mode

This commit is contained in:
Andreas Steffen
2014-06-14 15:40:22 +02:00
parent 12d618e280
commit dacb75f5c0
18 changed files with 310 additions and 185 deletions
+2 -2
View File
@@ -438,9 +438,9 @@ void vici_free_req(vici_req_t *req)
free(req);
}
int vici_dump(vici_res_t *res, char *label, FILE *out)
int vici_dump(vici_res_t *res, char *label, bool pretty, FILE *out)
{
if (res->message->dump(res->message, label, out))
if (res->message->dump(res->message, label, pretty, out))
{
return 0;
}
+4 -1
View File
@@ -75,6 +75,8 @@
#include <stdio.h>
#include <utils/utils.h>
/**
* Opaque vici connection contex.
*/
@@ -278,10 +280,11 @@ void vici_free_req(vici_req_t *req);
*
* @param res response message to dump
* @param label a label to print for this message
* @param pretty use pretty print with indentation
* @param out FILE to dump to
* @return 0 if dumped complete message, 1 on error
*/
int vici_dump(vici_res_t *res, char *label, FILE *out);
int vici_dump(vici_res_t *res, char *label, bool pretty, FILE *out);
/**
* Parse next element from a vici response message.
+49 -17
View File
@@ -49,7 +49,8 @@ struct private_vici_message_t {
linked_list_t *strings;
};
ENUM(vici_type_names, VICI_SECTION_START, VICI_END,
ENUM(vici_type_names, VICI_START, VICI_END,
"start",
"section-start",
"section-end",
"key-value",
@@ -449,6 +450,9 @@ METHOD(vici_message_t, parse, bool,
{
switch (type)
{
case VICI_START:
/* should never occur */
continue;
case VICI_KEY_VALUE:
if (ctx->level == base && kv)
{
@@ -507,15 +511,31 @@ METHOD(vici_message_t, parse, bool,
}
METHOD(vici_message_t, dump, bool,
private_vici_message_t *this, char *label, FILE *out)
private_vici_message_t *this, char *label, bool pretty, FILE *out)
{
enumerator_t *enumerator;
int ident = 0, delta = 2;
vici_type_t type;
char *name;
int ident = 0, delta;
vici_type_t type, last_type = VICI_START;
char *name, *term, *sep, *separ, *assign;
chunk_t value;
fprintf(out, "%s {\n", label);
/* pretty print uses indentation on multiple lines */
if (pretty)
{
delta = 2;
term = "\n";
separ = "";
assign = " = ";
}
else
{
delta = 0;
term = "";
separ = " ";
assign = "=";
}
fprintf(out, "%s {%s", label, term);
ident += delta;
enumerator = create_enumerator(this);
@@ -523,43 +543,54 @@ METHOD(vici_message_t, dump, bool,
{
switch (type)
{
case VICI_START:
/* should never occur */
break;
case VICI_SECTION_START:
fprintf(out, "%*s%s {\n", ident, "", name);
sep = (last_type != VICI_SECTION_START &&
last_type != VICI_START) ? separ : "";
fprintf(out, "%*s%s%s {%s", ident, "", sep, name, term);
ident += delta;
break;
case VICI_SECTION_END:
ident -= delta;
fprintf(out, "%*s}\n", ident, "");
fprintf(out, "%*s}%s", ident, "", term);
break;
case VICI_KEY_VALUE:
sep = (last_type != VICI_SECTION_START &&
last_type != VICI_START) ? separ : "";
if (chunk_printable(value, NULL, ' '))
{
fprintf(out, "%*s%s = %.*s\n",
ident, "", name, (int)value.len, value.ptr);
fprintf(out, "%*s%s%s%s%.*s%s", ident, "", sep, name,
assign, (int)value.len, value.ptr, term);
}
else
{
fprintf(out, "%*s%s = 0x%+#B\n",
ident, "", name, &value);
fprintf(out, "%*s%s%s%s0x%+#B%s", ident, "", sep, name,
assign, &value, term);
}
break;
case VICI_LIST_START:
fprintf(out, "%*s%s = [\n", ident, "", name);
sep = (last_type != VICI_SECTION_START &&
last_type != VICI_START) ? separ : "";
fprintf(out, "%*s%s%s%s[%s", ident, "", sep, name, assign, term);
ident += delta;
break;
case VICI_LIST_END:
ident -= delta;
fprintf(out, "%*s]\n", ident, "");
fprintf(out, "%*s]%s", ident, "", term);
break;
case VICI_LIST_ITEM:
sep = (last_type != VICI_LIST_START) ? separ : "";
if (chunk_printable(value, NULL, ' '))
{
fprintf(out, "%*s%.*s\n",
ident, "", (int)value.len, value.ptr);
fprintf(out, "%*s%s%.*s%s", ident, "", sep,
(int)value.len, value.ptr, term);
}
else
{
fprintf(out, "%*s 0x%+#B\n", ident, "", &value);
fprintf(out, "%*s%s0x%+#B%s", ident, "", sep,
&value, term);
}
break;
case VICI_END:
@@ -567,6 +598,7 @@ METHOD(vici_message_t, dump, bool,
enumerator->destroy(enumerator);
return TRUE;
}
last_type = type;
}
enumerator->destroy(enumerator);
return FALSE;
+15 -11
View File
@@ -31,21 +31,24 @@ typedef enum vici_type_t vici_type_t;
* Vici message encoding types
*/
enum vici_type_t {
/** never used in an argument list, needed by dump as initial value */
VICI_START = 0,
/** begin of new section, argument is section name as char* */
VICI_SECTION_START = 0,
VICI_SECTION_START = 1,
/** end of current section, no arguments */
VICI_SECTION_END,
VICI_SECTION_END = 2,
/** key/value, arguments are key as char*, value as chunk_t */
VICI_KEY_VALUE,
VICI_KEY_VALUE = 3,
/** list start, argument is list name as char* */
VICI_LIST_START,
VICI_LIST_START = 4,
/** list item, argument is item value as chunk_t */
VICI_LIST_ITEM,
VICI_LIST_ITEM = 5,
/** end of list, no arguments */
VICI_LIST_END,
VICI_LIST_END = 6,
/** end of argument list, no arguments (never encoded) */
VICI_END
VICI_END = 7
};
/**
@@ -184,11 +187,12 @@ struct vici_message_t {
/**
* Dump a message text representation to a FILE stream.
*
* @param label label to print for message
* @param out FILE stream to dump to
* @return TRUE if message valid
* @param label label to print for message
* @param pretty use pretty print with indentation
* @param out FILE stream to dump to
* @return TRUE if message valid
*/
bool (*dump)(vici_message_t *this, char *label, FILE *out);
bool (*dump)(vici_message_t *this, char *label, bool pretty, FILE *out);
/**
* Destroy a vici_message_t.