aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/plugins
diff options
context:
space:
mode:
authorAndreas Steffen <andreas.steffen@strongswan.org>2014-06-12 22:57:15 +0200
committerAndreas Steffen <andreas.steffen@strongswan.org>2014-06-14 15:40:22 +0200
commitdacb75f5c04b19e142762c81d5b13d3005c57c71 (patch)
tree12c863a85455b2354854c01880b756f31d1c9b11 /src/libcharon/plugins
parent12d618e280c4e8c54d94ecddf23ef937a3cbae39 (diff)
downloadstrongswan-dacb75f5c04b19e142762c81d5b13d3005c57c71.tar.bz2
strongswan-dacb75f5c04b19e142762c81d5b13d3005c57c71.tar.xz
Split swanctl --raw mode into single-line and --pretty mode
Diffstat (limited to 'src/libcharon/plugins')
-rw-r--r--src/libcharon/plugins/vici/libvici.c4
-rw-r--r--src/libcharon/plugins/vici/libvici.h5
-rw-r--r--src/libcharon/plugins/vici/vici_message.c66
-rw-r--r--src/libcharon/plugins/vici/vici_message.h26
4 files changed, 70 insertions, 31 deletions
diff --git a/src/libcharon/plugins/vici/libvici.c b/src/libcharon/plugins/vici/libvici.c
index d1dadddcb..a2cbb3082 100644
--- a/src/libcharon/plugins/vici/libvici.c
+++ b/src/libcharon/plugins/vici/libvici.c
@@ -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;
}
diff --git a/src/libcharon/plugins/vici/libvici.h b/src/libcharon/plugins/vici/libvici.h
index 6957a00b6..a2ad95891 100644
--- a/src/libcharon/plugins/vici/libvici.h
+++ b/src/libcharon/plugins/vici/libvici.h
@@ -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.
diff --git a/src/libcharon/plugins/vici/vici_message.c b/src/libcharon/plugins/vici/vici_message.c
index d3c1b23e2..dcc175f67 100644
--- a/src/libcharon/plugins/vici/vici_message.c
+++ b/src/libcharon/plugins/vici/vici_message.c
@@ -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;
diff --git a/src/libcharon/plugins/vici/vici_message.h b/src/libcharon/plugins/vici/vici_message.h
index 8817959c1..c5e25b350 100644
--- a/src/libcharon/plugins/vici/vici_message.h
+++ b/src/libcharon/plugins/vici/vici_message.h
@@ -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.