This just compares 2 version strings.
Signed-off-by: David Lamparter <equinox@diac24.net>
--- /dev/null
+/*
+ * FRR switchable defaults.
+ * Copyright (c) 2017-2019 David Lamparter, for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <zebra.h>
+
+#include "defaults.h"
+#include "libfrr.h"
+#include "version.h"
+
+static int version_value(int ch)
+{
+ /* non-ASCII shouldn't happen */
+ if (ch < 0 || ch >= 128)
+ return 2;
+
+ /* ~foo sorts older than nothing */
+ if (ch == '~')
+ return 0;
+ if (ch == '\0')
+ return 1;
+ if (isalpha(ch))
+ return 0x100 + tolower(ch);
+
+ /* punctuation and digits (and everything else) */
+ return 0x200 + ch;
+}
+
+int frr_version_cmp(const char *aa, const char *bb)
+{
+ const char *apos = aa, *bpos = bb;
+
+ /* || is correct, we won't scan past the end of a string since that
+ * doesn't compare equal to anything else */
+ while (apos[0] || bpos[0]) {
+ if (isdigit((unsigned char)apos[0]) &&
+ isdigit((unsigned char)bpos[0])) {
+ unsigned long av, bv;
+ char *aend = NULL, *bend = NULL;
+
+ av = strtoul(apos, &aend, 10);
+ bv = strtoul(bpos, &bend, 10);
+ if (av < bv)
+ return -1;
+ if (av > bv)
+ return 1;
+
+ apos = aend;
+ bpos = bend;
+ continue;
+ }
+
+ int a = version_value(*apos++);
+ int b = version_value(*bpos++);
+
+ if (a < b)
+ return -1;
+ if (a > b)
+ return 1;
+ }
+ return 0;
+}
--- /dev/null
+/*
+ * FRR switchable defaults.
+ * Copyright (C) 2017-2019 David Lamparter for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef _FRR_DEFAULTS_H
+#define _FRR_DEFAULTS_H
+
+/* like strcmp(), but with version ordering */
+extern int frr_version_cmp(const char *aa, const char *bb);
+
+#endif /* _FRR_DEFAULTS_H */
" -g, --group Group to run as\n",
lo_user};
-
bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len,
const char *path)
{
lib/command_parse.y \
lib/csv.c \
lib/debug.c \
+ lib/defaults.c \
lib/distribute.c \
lib/ferr.c \
lib/filter.c \
lib/csv.h \
lib/db.h \
lib/debug.h \
+ lib/defaults.h \
lib/distribute.h \
lib/ferr.h \
lib/filter.h \
/lib/test_timer_performance
/lib/test_ttable
/lib/test_typelist
+/lib/test_versioncmp
/lib/test_zlog
/lib/test_zmq
/ospf6d/test_lsdb
--- /dev/null
+/*
+ * frr_version_cmp() tests
+ * Copyright (C) 2018 David Lamparter for NetDEF, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <zebra.h>
+#include <defaults.h>
+
+static const char *rel(int x)
+{
+ if (x < 0)
+ return "<";
+ if (x > 0)
+ return ">";
+ return "==";
+}
+
+static int fail;
+
+static void compare(const char *a, const char *b, int expect)
+{
+ int result = frr_version_cmp(a, b);
+
+ if (expect == result)
+ printf("\"%s\" %s \"%s\"\n", a, rel(result), b);
+ else {
+ printf("\"%s\" %s \"%s\", expected %s!\n", a, rel(result), b,
+ rel(expect));
+ fail = 1;
+ }
+}
+
+int main(int argc, char **argv)
+{
+ compare("", "", 0);
+ compare("1", "1", 0);
+ compare("1.0", "1.00", 0);
+ compare("10.0", "1", 1);
+ compare("10.0", "2", 1);
+ compare("2.1", "10.0", -1);
+ compare("1.1.1", "1.1.0", 1);
+ compare("1.0a", "1.0", 1);
+ compare("1.0a", "1.0b", -1);
+ compare("1.0a10", "1.0a2", 1);
+ compare("1.00a2", "1.0a2", 0);
+ compare("1.00a2", "1.0a3", -1);
+ compare("1.0-dev", "1.0", 1);
+ compare("1.0~foo", "1.0", -1);
+ compare("1.0~1", "1.0~0", 1);
+ compare("1.00~1", "1.0~0", 1);
+ printf("final tally: %s\n", fail ? "FAILED" : "ok");
+ return fail;
+}
--- /dev/null
+import frrtest
+
+class TestVersionCmp(frrtest.TestMultiOut):
+ program = './test_versioncmp'
+
+TestVersionCmp.exit_cleanly()
tests/lib/test_timer_performance \
tests/lib/test_ttable \
tests/lib/test_typelist \
+ tests/lib/test_versioncmp \
tests/lib/test_zlog \
tests/lib/test_graph \
tests/lib/cli/test_cli \
tests_lib_test_typelist_CPPFLAGS = $(TESTS_CPPFLAGS)
tests_lib_test_typelist_LDADD = $(ALL_TESTS_LDADD)
tests_lib_test_typelist_SOURCES = tests/lib/test_typelist.c tests/helpers/c/prng.c
+tests_lib_test_versioncmp_CFLAGS = $(TESTS_CFLAGS)
+tests_lib_test_versioncmp_CPPFLAGS = $(TESTS_CPPFLAGS)
+tests_lib_test_versioncmp_LDADD = $(ALL_TESTS_LDADD)
+tests_lib_test_versioncmp_SOURCES = tests/lib/test_versioncmp.c
tests_lib_test_zlog_CFLAGS = $(TESTS_CFLAGS)
tests_lib_test_zlog_CPPFLAGS = $(TESTS_CPPFLAGS)
tests_lib_test_zlog_LDADD = $(ALL_TESTS_LDADD)
tests/lib/test_ttable.py \
tests/lib/test_ttable.refout \
tests/lib/test_typelist.py \
+ tests/lib/test_versioncmp.py \
tests/lib/test_zlog.py \
tests/lib/test_graph.py \
tests/lib/test_graph.refout \