Review at  https://gerrit.osmocom.org/3882

VTY: allow comments in the same line as vty commands

Note: this breaks users' VTY config when a command's argument starts with a '!'
or '#'.

Allow comments following VTY commands, on the same line. Start a comment with
'!' or '#' following a space, which allows using the comment characters in
command arguments -- just not at the start of an argument.

This patch allows to do:

   remote-ip 10.23.24.1    # where to reach the STP

When they are not preceded by a space, the comment characters still count as
VTY command tokens, i.e. still allowing:

   short-name instance#23

This would parse "instance#23" as name.

(We usually use '!' as the VTY comment delimiter, but the code also allowed and
still allows using '#', hence add both '#' and '!' as same-line comment
delimiter.)

Change-Id: Iccd9cc8604494379910c534b35ce7e74e329d863
---
M src/vty/command.c
M tests/Makefile.am
A tests/vty/ok_comments.cfg
M tests/vty/vty_test.c
M tests/vty/vty_test.ok
5 files changed, 11 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/82/3882/1

diff --git a/src/vty/command.c b/src/vty/command.c
index 9161bf5..f981692 100644
--- a/src/vty/command.c
+++ b/src/vty/command.c
@@ -242,6 +242,9 @@
 
        /* Copy each command piece and set into vector. */
        while (1) {
+               if (*cp == '!' || *cp == '#')
+                       break;
+
                start = cp;
                while (!(isspace((int)*cp) || *cp == '\r' || *cp == '\n') &&
                       *cp != '\0')
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8526847..5a124c1 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -218,7 +218,8 @@
              fr/fr_test.ok loggingrb/logging_test.ok                   \
              loggingrb/logging_test.err        strrb/strrb_test.ok             
\
             vty/vty_test.ok vty/fail_not_de-indented.cfg               \
-            vty/fail_too_much_indent.cfg vty/ok.cfg                    \
+            vty/fail_too_much_indent.cfg vty/ok.cfg                    \
+            vty/ok_comments.cfg                                        \
             comp128/comp128_test.ok                                    \
             utils/utils_test.ok stats/stats_test.ok                    \
             bitvec/bitvec_test.ok msgb/msgb_test.ok bits/bitcomp_test.ok \
diff --git a/tests/vty/ok_comments.cfg b/tests/vty/ok_comments.cfg
new file mode 100644
index 0000000..c040892
--- /dev/null
+++ b/tests/vty/ok_comments.cfg
@@ -0,0 +1,3 @@
+line vty ! comments
+ no login # after
+log stderr #commands
diff --git a/tests/vty/vty_test.c b/tests/vty/vty_test.c
index 63b0b03..a269764 100644
--- a/tests/vty/vty_test.c
+++ b/tests/vty/vty_test.c
@@ -335,6 +335,7 @@
        test_exit_by_indent("ok.cfg", 0);
        test_exit_by_indent("fail_not_de-indented.cfg", -EINVAL);
        test_exit_by_indent("fail_too_much_indent.cfg", -EINVAL);
+       test_exit_by_indent("ok_comments.cfg", 0);
 
        /* Leak check */
        OSMO_ASSERT(talloc_total_blocks(stats_ctx) == 1);
diff --git a/tests/vty/vty_test.ok b/tests/vty/vty_test.ok
index 0da0c2f..85ee337 100644
--- a/tests/vty/vty_test.ok
+++ b/tests/vty/vty_test.ok
@@ -114,4 +114,6 @@
 got rc=-22
 reading file fail_too_much_indent.cfg, expecting rc=-22
 got rc=-22
+reading file ok_comments.cfg, expecting rc=0
+got rc=0
 All tests passed

-- 
To view, visit https://gerrit.osmocom.org/3882
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iccd9cc8604494379910c534b35ce7e74e329d863
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <[email protected]>

Reply via email to