[ovs-dev,v2,11/15] ovn-nbctl: Extract helper for printing oneline output.

Message ID 20180712134016.14336-12-jkbs@redhat.com
State New
Headers show
Series
  • Daemon mode for ovn-nbctl
Related show

Commit Message

Jakub Sitnicki July 12, 2018, 1:40 p.m.
This will allow us to direct oneline-formatted output to other sinks
than stdout if needed. Preparatory work for daemon mode.

Signed-off-by: Jakub Sitnicki <jkbs@redhat.com>
---
 ovn/utilities/ovn-nbctl.c | 53 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 19 deletions(-)

Patch

diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c
index 7f83abc40..3dd24d193 100644
--- a/ovn/utilities/ovn-nbctl.c
+++ b/ovn/utilities/ovn-nbctl.c
@@ -4162,6 +4162,39 @@  run_prerequisites(struct ctl_command *commands, size_t n_commands,
     return NULL;
 }
 
+static void
+oneline_format(struct ds *lines, struct ds *s)
+{
+    size_t j;
+
+    ds_chomp(lines, '\n');
+    for (j = 0; j < lines->length; j++) {
+        int ch = lines->string[j];
+        switch (ch) {
+        case '\n':
+            ds_put_cstr(s, "\\n");
+            break;
+
+        case '\\':
+            ds_put_cstr(s, "\\\\");
+            break;
+
+        default:
+            ds_put_char(s, ch);
+        }
+    }
+    ds_put_char(s, '\n');
+}
+
+static void
+oneline_print(struct ds *lines)
+{
+    struct ds s = DS_EMPTY_INITIALIZER;
+    oneline_format(lines, &s);
+    fputs(ds_cstr(&s), stdout);
+    ds_destroy(&s);
+}
+
 static char *
 do_nbctl(const char *args, struct ctl_command *commands, size_t n_commands,
          struct ovsdb_idl *idl, const struct timer *wait_timeout, bool *retry)
@@ -4297,25 +4330,7 @@  do_nbctl(const char *args, struct ctl_command *commands, size_t n_commands,
         if (c->table) {
             table_print(c->table, &table_style);
         } else if (oneline) {
-            size_t j;
-
-            ds_chomp(ds, '\n');
-            for (j = 0; j < ds->length; j++) {
-                int ch = ds->string[j];
-                switch (ch) {
-                case '\n':
-                    fputs("\\n", stdout);
-                    break;
-
-                case '\\':
-                    fputs("\\\\", stdout);
-                    break;
-
-                default:
-                    putchar(ch);
-                }
-            }
-            putchar('\n');
+            oneline_print(ds);
         } else {
             fputs(ds_cstr(ds), stdout);
         }