[network-manager-fortisslvpn] shared: reimport shared files



commit 1a89b36029f4a7297234fde4c0744aaa101c9ce5
Author: Thomas Haller <thaller redhat com>
Date:   Tue Jan 24 15:11:34 2017 +0100

    shared: reimport shared files
    
    Also, change the compiler option to use gnu99 C.

 configure.ac                         |    2 +-
 m4/compiler_options.m4               |    4 +-
 shared/nm-utils/nm-macros-internal.h |  351 ++++++++++++++++++++++++----------
 shared/nm-utils/nm-shared-utils.c    |   83 ++++++++-
 shared/nm-utils/nm-shared-utils.h    |   27 +++-
 5 files changed, 360 insertions(+), 107 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 327c1df..996bad4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -124,7 +124,7 @@ LIBNM_CFLAGS="$LIBNM_CFLAGS -DNM_VERSION_MAX_ALLOWED=NM_VERSION_1_2"
 NM_VPN_SERVICE_DIR=`$PKG_CONFIG --define-variable prefix='\${prefix}' --variable vpnservicedir libnm`
 AC_SUBST(NM_VPN_SERVICE_DIR)
 
-NM_COMPILER_WARNINGS
+NM_COMPILER_WARNINGS([yes])
 
 NM_PLUGIN_DIR="$libdir/NetworkManager"
 AC_SUBST(NM_PLUGIN_DIR)
diff --git a/m4/compiler_options.m4 b/m4/compiler_options.m4
index fa11c52..0023c36 100644
--- a/m4/compiler_options.m4
+++ b/m4/compiler_options.m4
@@ -37,7 +37,7 @@ AC_DEFUN([NM_COMPILER_WARNING], [
 AC_DEFUN([NM_COMPILER_WARNINGS],
 [AC_ARG_ENABLE(more-warnings,
        AS_HELP_STRING([--enable-more-warnings], [Possible values: no/yes/error]),
-       set_more_warnings="$enableval",set_more_warnings=yes)
+       set_more_warnings="$enableval",set_more_warnings=$1)
 AC_MSG_CHECKING(for more warnings)
 if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
        AC_MSG_RESULT(yes)
@@ -55,7 +55,7 @@ if test "$GCC" = "yes" -a "$set_more_warnings" != "no"; then
        dnl attach it to the CFLAGS.
        NM_COMPILER_WARNING([unknown-warning-option], [])
 
-       CFLAGS_MORE_WARNINGS="-Wall -std=gnu89"
+       CFLAGS_MORE_WARNINGS="-Wall -std=gnu99"
 
        if test "x$set_more_warnings" = xerror; then
                CFLAGS_MORE_WARNINGS="$CFLAGS_MORE_WARNINGS -Werror"
diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h
index 91970c3..70476b7 100644
--- a/shared/nm-utils/nm-macros-internal.h
+++ b/shared/nm-utils/nm-macros-internal.h
@@ -22,11 +22,13 @@
 #ifndef __NM_MACROS_INTERNAL_H__
 #define __NM_MACROS_INTERNAL_H__
 
+#include <stdio.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #include "nm-glib.h"
 
-/********************************************************/
+/*****************************************************************************/
 
 #define _nm_packed __attribute__ ((packed))
 #define _nm_unused __attribute__ ((unused))
@@ -34,6 +36,8 @@
 #define _nm_const  __attribute__ ((const))
 #define _nm_printf(a,b) __attribute__ ((__format__ (__printf__, a, b)))
 
+#define nm_offsetofend(t,m) (G_STRUCT_OFFSET (t,m) + sizeof (((t *) NULL)->m))
+
 #define nm_auto(fcn) __attribute__ ((cleanup(fcn)))
 
 /**
@@ -59,7 +63,38 @@ _nm_auto_free_gstring_impl (GString **str)
 }
 #define nm_auto_free_gstring nm_auto(_nm_auto_free_gstring_impl)
 
-/********************************************************/
+static inline void
+_nm_auto_close_impl (int *pfd)
+{
+       if (*pfd >= 0) {
+               int errsv = errno;
+
+               (void) close (*pfd);
+               errno = errsv;
+       }
+}
+#define nm_auto_close nm_auto(_nm_auto_close_impl)
+
+static inline void
+_nm_auto_fclose_impl (FILE **pfd)
+{
+       if (*pfd) {
+               int errsv = errno;
+
+               (void) fclose (*pfd);
+               errno = errsv;
+       }
+}
+#define nm_auto_fclose nm_auto(_nm_auto_fclose_impl)
+
+static inline void
+_nm_auto_protect_errno (int *p_saved_errno)
+{
+       errno = *p_saved_errno;
+}
+#define NM_AUTO_PROTECT_ERRNO(errsv_saved) nm_auto(_nm_auto_protect_errno) _nm_unused const int errsv_saved 
= (errno)
+
+/*****************************************************************************/
 
 /* http://stackoverflow.com/a/11172679 */
 #define  _NM_UTILS_MACRO_FIRST(...)                           __NM_UTILS_MACRO_FIRST_HELPER(__VA_ARGS__, 
throwaway)
@@ -78,7 +113,7 @@ _nm_auto_free_gstring_impl (GString **str)
                 TWOORMORE, TWOORMORE, TWOORMORE, ONE, throwaway)
 #define __NM_UTILS_MACRO_REST_SELECT_20TH(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, 
a16, a17, a18, a19, a20, ...) a20
 
-/********************************************************/
+/*****************************************************************************/
 
 /* http://stackoverflow.com/a/2124385/354393 */
 
@@ -103,7 +138,7 @@ _nm_auto_free_gstring_impl (GString **str)
          19,18,17,16,15,14,13,12,11,10, \
          9,8,7,6,5,4,3,2,1,0
 
-/********************************************************/
+/*****************************************************************************/
 
 #if defined (__GNUC__)
 #define _NM_PRAGMA_WARNING_DO(warning)       G_STRINGIFY(GCC diagnostic ignored warning)
@@ -139,7 +174,7 @@ _nm_auto_free_gstring_impl (GString **str)
 #define NM_PRAGMA_WARNING_REENABLE
 #endif
 
-/********************************************************/
+/*****************************************************************************/
 
 /**
  * NM_G_ERROR_MSG:
@@ -159,11 +194,16 @@ NM_G_ERROR_MSG (GError *error)
        return error ? (error->message ? : "(null)") : "(no-error)"; \
 }
 
-/********************************************************/
+/*****************************************************************************/
 
 /* macro to return strlen() of a compile time string. */
 #define NM_STRLEN(str)     ( sizeof ("" str) - 1 )
 
+/* Note: @value is only evaluated when *out_val is present.
+ * Thus,
+ *    NM_SET_OUT (out_str, g_strdup ("hallo"));
+ * does the right thing.
+ */
 #define NM_SET_OUT(out_val, value) \
        G_STMT_START { \
                typeof(*(out_val)) *_out_val = (out_val); \
@@ -173,47 +213,26 @@ NM_G_ERROR_MSG (GError *error)
                } \
        } G_STMT_END
 
-/********************************************************/
-
-#define _NM_IN_SET_EVAL_1(op, _x, y1)                               \
-    (_x == (y1))
-
-#define _NM_IN_SET_EVAL_2(op, _x, y1, y2)                           \
-    (   (_x == (y1))                                                \
-     op (_x == (y2))                                                \
-    )
-
-#define _NM_IN_SET_EVAL_3(op, _x, y1, y2, y3)                       \
-    (   (_x == (y1))                                                \
-     op (_x == (y2))                                                \
-     op (_x == (y3))                                                \
-    )
-
-#define _NM_IN_SET_EVAL_4(op, _x, y1, y2, y3, y4)                   \
-    (   (_x == (y1))                                                \
-     op (_x == (y2))                                                \
-     op (_x == (y3))                                                \
-     op (_x == (y4))                                                \
-    )
-
-#define _NM_IN_SET_EVAL_5(op, _x, y1, y2, y3, y4, y5)               \
-    (   (_x == (y1))                                                \
-     op (_x == (y2))                                                \
-     op (_x == (y3))                                                \
-     op (_x == (y4))                                                \
-     op (_x == (y5))                                                \
-    )
-
-#define _NM_IN_SET_EVAL_6(op, _x, y1, y2, y3, y4, y5, y6)           \
-    (   (_x == (y1))                                                \
-     op (_x == (y2))                                                \
-     op (_x == (y3))                                                \
-     op (_x == (y4))                                                \
-     op (_x == (y5))                                                \
-     op (_x == (y6))                                                \
-    )
-
-#define _NM_IN_SET_EVAL_N2(op, _x, n, ...)        _NM_IN_SET_EVAL_##n(op, _x, __VA_ARGS__)
+/*****************************************************************************/
+
+#define _NM_IN_SET_EVAL_1( op, _x, y)           (_x == (y))
+#define _NM_IN_SET_EVAL_2( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_1  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_3( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_2  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_4( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_3  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_5( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_4  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_6( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_5  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_7( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_6  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_8( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_7  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_9( op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_8  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_10(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_9  (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_11(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_10 (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_12(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_11 (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_13(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_12 (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_14(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_13 (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_15(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_14 (op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_16(op, _x, y, ...)      (_x == (y)) op _NM_IN_SET_EVAL_15 (op, _x, __VA_ARGS__)
+
+#define _NM_IN_SET_EVAL_N2(op, _x, n, ...)      (_NM_IN_SET_EVAL_##n(op, _x, __VA_ARGS__))
 #define _NM_IN_SET_EVAL_N(op, x, n, ...)                            \
     ({                                                              \
         typeof(x) _x = (x);                                         \
@@ -230,7 +249,7 @@ NM_G_ERROR_MSG (GError *error)
  * side-effects. */
 #define NM_IN_SET_SE(x, ...)            _NM_IN_SET_EVAL_N(|, x, NM_NARG (__VA_ARGS__), __VA_ARGS__)
 
-/********************************************************/
+/*****************************************************************************/
 
 static inline gboolean
 _NM_IN_STRSET_streq (const char *x, const char *s)
@@ -238,45 +257,24 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
        return s && strcmp (x, s) == 0;
 }
 
-#define _NM_IN_STRSET_EVAL_1(op, _x, y1)                            \
-    _NM_IN_STRSET_streq (_x, y1)
-
-#define _NM_IN_STRSET_EVAL_2(op, _x, y1, y2)                        \
-    (   _NM_IN_STRSET_streq (_x, y1)                                \
-     op _NM_IN_STRSET_streq (_x, y2)                                \
-    )
-
-#define _NM_IN_STRSET_EVAL_3(op, _x, y1, y2, y3)                    \
-    (   _NM_IN_STRSET_streq (_x, y1)                                \
-     op _NM_IN_STRSET_streq (_x, y2)                                \
-     op _NM_IN_STRSET_streq (_x, y3)                                \
-    )
-
-#define _NM_IN_STRSET_EVAL_4(op, _x, y1, y2, y3, y4)                \
-    (   _NM_IN_STRSET_streq (_x, y1)                                \
-     op _NM_IN_STRSET_streq (_x, y2)                                \
-     op _NM_IN_STRSET_streq (_x, y3)                                \
-     op _NM_IN_STRSET_streq (_x, y4)                                \
-    )
-
-#define _NM_IN_STRSET_EVAL_5(op, _x, y1, y2, y3, y4, y5)            \
-    (   _NM_IN_STRSET_streq (_x, y1)                                \
-     op _NM_IN_STRSET_streq (_x, y2)                                \
-     op _NM_IN_STRSET_streq (_x, y3)                                \
-     op _NM_IN_STRSET_streq (_x, y4)                                \
-     op _NM_IN_STRSET_streq (_x, y5)                                \
-    )
-
-#define _NM_IN_STRSET_EVAL_6(op, _x, y1, y2, y3, y4, y5, y6)        \
-    (   _NM_IN_STRSET_streq (_x, y1)                                \
-     op _NM_IN_STRSET_streq (_x, y2)                                \
-     op _NM_IN_STRSET_streq (_x, y3)                                \
-     op _NM_IN_STRSET_streq (_x, y4)                                \
-     op _NM_IN_STRSET_streq (_x, y5)                                \
-     op _NM_IN_STRSET_streq (_x, y6)                                \
-    )
-
-#define _NM_IN_STRSET_EVAL_N2(op, _x, n, ...) _NM_IN_STRSET_EVAL_##n(op, _x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_1( op, _x, y)        _NM_IN_STRSET_streq (_x, y)
+#define _NM_IN_STRSET_EVAL_2( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_1  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_3( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_2  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_4( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_3  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_5( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_4  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_6( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_5  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_7( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_6  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_8( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_7  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_9( op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_8  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_10(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_9  (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_11(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_10 (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_12(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_11 (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_13(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_12 (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_14(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_13 (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_15(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_14 (op, 
_x, __VA_ARGS__)
+#define _NM_IN_STRSET_EVAL_16(op, _x, y, ...)   _NM_IN_STRSET_streq (_x, y) op _NM_IN_STRSET_EVAL_15 (op, 
_x, __VA_ARGS__)
+
+#define _NM_IN_STRSET_EVAL_N2(op, _x, n, ...)   (_NM_IN_STRSET_EVAL_##n(op, _x, __VA_ARGS__))
 #define _NM_IN_STRSET_EVAL_N(op, x, n, ...)                       \
     ({                                                            \
         const char *_x = (x);                                     \
@@ -295,6 +293,52 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
  * side-effects. */
 #define NM_IN_STRSET_SE(x, ...)            _NM_IN_STRSET_EVAL_N(|, x, NM_NARG (__VA_ARGS__), __VA_ARGS__)
 
+#define NM_STRCHAR_ALL(str, ch_iter, predicate) \
+       ({ \
+               gboolean _val = TRUE; \
+               const char *_str = (str); \
+               \
+               if (_str) { \
+                       for (;;) { \
+                               const char ch_iter = _str[0]; \
+                               \
+                               if (ch_iter != '\0') { \
+                                       if (predicate) {\
+                                               _str++; \
+                                               continue; \
+                                       } \
+                                       _val = FALSE; \
+                               } \
+                               break; \
+                       } \
+               } \
+               _val; \
+       })
+
+#define NM_STRCHAR_ANY(str, ch_iter, predicate) \
+       ({ \
+               gboolean _val = FALSE; \
+               const char *_str = (str); \
+               \
+               if (_str) { \
+                       for (;;) { \
+                               const char ch_iter = _str[0]; \
+                               \
+                               if (ch_iter != '\0') { \
+                                       if (predicate) { \
+                                               ; \
+                                       } else { \
+                                               _str++; \
+                                               continue; \
+                                       } \
+                                       _val = TRUE; \
+                               } \
+                               break; \
+                       } \
+               } \
+               _val; \
+       })
+
 /*****************************************************************************/
 
 #define nm_streq(s1, s2)  (strcmp (s1, s2) == 0)
@@ -302,13 +346,11 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
 
 /*****************************************************************************/
 
-#define nm_str_not_empty(str) \
-       ({ \
-               /* implemented as macro to preserve constness */ \
-               typeof (str) __str = (str); \
-               _nm_unused const char *__str_type_check = __str; \
-               ((__str && __str[0]) ? __str : ((char *) NULL)); \
-       })
+static inline const char *
+nm_str_not_empty (const char *str)
+{
+       return str && str[0] ? str : NULL;
+}
 
 static inline char *
 nm_strdup_not_empty (const char *str)
@@ -316,6 +358,25 @@ nm_strdup_not_empty (const char *str)
        return str && str[0] ? g_strdup (str) : NULL;
 }
 
+static inline char *
+nm_str_realloc (char *str)
+{
+       gs_free char *s = str;
+
+       /* Returns a new clone of @str and frees @str. The point is that @str
+        * possibly points to a larger chunck of memory. We want to freshly allocate
+        * a buffer.
+        *
+        * We could use realloc(), but that might not do anything or leave
+        * @str in its memory pool for chunks of a different size (bad for
+        * fragmentation).
+        *
+        * This is only useful when we want to keep the buffer around for a long
+        * time and want to re-allocate a more optimal buffer. */
+
+       return g_strdup (s);
+}
+
 /*****************************************************************************/
 
 #define NM_PRINT_FMT_QUOTED(cond, prefix, str, suffix, str_else) \
@@ -344,9 +405,11 @@ nm_strdup_not_empty (const char *str)
 
 #if NM_MORE_ASSERTS
 #define nm_assert(cond) G_STMT_START { g_assert (cond); } G_STMT_END
+#define nm_assert_se(cond) G_STMT_START { if (G_LIKELY (cond)) { ; } else { g_assert (FALSE && (cond)); } } 
G_STMT_END
 #define nm_assert_not_reached() G_STMT_START { g_assert_not_reached (); } G_STMT_END
 #else
 #define nm_assert(cond) G_STMT_START { if (FALSE) { if (cond) { } } } G_STMT_END
+#define nm_assert_se(cond) G_STMT_START { if (G_LIKELY (cond)) { ; } } G_STMT_END
 #define nm_assert_not_reached() G_STMT_START { ; } G_STMT_END
 #endif
 
@@ -410,6 +473,27 @@ nm_g_object_unref (gpointer obj)
                g_object_unref (obj);
 }
 
+/* basically, replaces
+ *   g_clear_pointer (&location, g_free)
+ * with
+ *   nm_clear_g_free (&location)
+ *
+ * Another advantage is that by using a macro and typeof(), it is more
+ * typesafe and gives you for example a compiler warning when pp is a const
+ * pointer or points to a const-pointer.
+ */
+#define nm_clear_g_free(pp) \
+       ({  \
+               typeof (*(pp)) *_pp = (pp); \
+               typeof (**_pp) *_p = *_pp; \
+               \
+               if (_p) { \
+                       *_pp = NULL; \
+                       g_free (_p); \
+               } \
+               !!_p; \
+       })
+
 static inline gboolean
 nm_clear_g_source (guint *id)
 {
@@ -521,6 +605,18 @@ _NM_BACKPORT_SYMBOL_IMPL(VERSION, RETURN_TYPE, FUNC, _##FUNC##_##VERSION, ARGS_T
 
 /*****************************************************************************/
 
+#define nm_str_skip_leading_spaces(str) \
+       ({ \
+               typeof (*(str)) *_str = (str); \
+               _nm_unused const char *_str_type_check = _str; \
+               \
+               if (_str) { \
+                       while (g_ascii_isspace (_str[0])) \
+                               _str++; \
+               } \
+               _str; \
+       })
+
 static inline char *
 nm_strstrip (char *str)
 {
@@ -558,6 +654,19 @@ nm_strcmp_p_with_data (gconstpointer a, gconstpointer b, gpointer user_data)
        return strcmp (s1, s2);
 }
 
+static inline int
+nm_cmp_uint32_p_with_data (gconstpointer p_a, gconstpointer p_b, gpointer user_data)
+{
+       const guint32 a = *((const guint32 *) p_a);
+       const guint32 b = *((const guint32 *) p_b);
+
+       if (a < b)
+               return -1;
+       if (a > b)
+               return 1;
+       return 0;
+}
+
 /*****************************************************************************/
 
 /* Taken from systemd's UNIQ_T and UNIQ macros. */
@@ -616,6 +725,21 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
        *minor = (version & 0x0000FF00u) >>  8;
        *micro = (version & 0x000000FFu);
 }
+
+/*****************************************************************************/
+
+/* taken from systemd's DECIMAL_STR_MAX()
+ *
+ * Returns the number of chars needed to format variables of the
+ * specified type as a decimal string. Adds in extra space for a
+ * negative '-' prefix (hence works correctly on signed
+ * types). Includes space for the trailing NUL. */
+#define NM_DECIMAL_STR_MAX(type) \
+    (2+(sizeof(type) <= 1 ? 3 : \
+        sizeof(type) <= 2 ? 5 : \
+        sizeof(type) <= 4 ? 10 : \
+        sizeof(type) <= 8 ? 20 : sizeof(int[-2*(sizeof(type) > 8)])))
+
 /*****************************************************************************/
 
 /* if @str is NULL, return "(null)". Otherwise, allocate a buffer using
@@ -649,23 +773,27 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
 
 #define nm_sprintf_buf(buf, format, ...) ({ \
                char * _buf = (buf); \
+               int _buf_len; \
                \
                /* some static assert trying to ensure that the buffer is statically allocated.
                 * It disallows a buffer size of sizeof(gpointer) to catch that. */ \
                G_STATIC_ASSERT (G_N_ELEMENTS (buf) == sizeof (buf) && sizeof (buf) != sizeof (char *)); \
-               g_snprintf (_buf, sizeof (buf), \
-                           ""format"", ##__VA_ARGS__); \
+               _buf_len = g_snprintf (_buf, sizeof (buf), \
+                                      ""format"", ##__VA_ARGS__); \
+               nm_assert (_buf_len < sizeof (buf)); \
                _buf; \
        })
 
 #define nm_sprintf_bufa(n_elements, format, ...) \
        ({ \
                char *_buf; \
+               int _buf_len; \
                \
                G_STATIC_ASSERT (sizeof (char[MAX ((n_elements), 1)]) == (n_elements)); \
                _buf = g_alloca (n_elements); \
-               g_snprintf (_buf, n_elements, \
-                           ""format"", ##__VA_ARGS__); \
+               _buf_len = g_snprintf (_buf, (n_elements), \
+                                      ""format"", ##__VA_ARGS__); \
+               nm_assert (_buf_len < (n_elements)); \
                _buf; \
        })
 
@@ -716,6 +844,33 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
 #define false   0
 #endif
 
+
+#ifdef _G_BOOLEAN_EXPR
+/* g_assert() uses G_LIKELY(), which in turn uses _G_BOOLEAN_EXPR().
+ * As glib's implementation uses a local variable _g_boolean_var_,
+ * we cannot do
+ *   g_assert (some_macro ());
+ * where some_macro() itself expands to ({g_assert(); ...}).
+ * In other words, you cannot have a g_assert() inside a g_assert()
+ * without getting a -Werror=shadow failure.
+ *
+ * Workaround that by re-defining _G_BOOLEAN_EXPR()
+ **/
+#undef  _G_BOOLEAN_EXPR
+#define __NM_G_BOOLEAN_EXPR_IMPL(v, expr) \
+       ({ \
+               int NM_UNIQ_T(V, v); \
+               \
+               if (expr) \
+                       NM_UNIQ_T(V, v) = 1; \
+               else \
+                       NM_UNIQ_T(V, v) = 0; \
+               NM_UNIQ_T(V, v); \
+       })
+#define _G_BOOLEAN_EXPR(expr) __NM_G_BOOLEAN_EXPR_IMPL (NM_UNIQ, expr)
+#endif
+
+
 /*****************************************************************************/
 
 #endif /* __NM_MACROS_INTERNAL_H__ */
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index 38f6529..38bb818 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -27,6 +27,85 @@
 
 /*****************************************************************************/
 
+void
+nm_utils_strbuf_append_c (char **buf, gsize *len, char c)
+{
+       switch (*len) {
+       case 0:
+               return;
+       case 1:
+               (*buf)[0] = '\0';
+               *len = 0;
+               (*buf)++;
+               return;
+       default:
+               (*buf)[0] = c;
+               (*buf)[1] = '\0';
+               (*len)--;
+               (*buf)++;
+               return;
+       }
+}
+
+void
+nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str)
+{
+       gsize src_len;
+
+       switch (*len) {
+       case 0:
+               return;
+       case 1:
+               if (!str || !*str) {
+                       (*buf)[0] = '\0';
+                       return;
+               }
+               (*buf)[0] = '\0';
+               *len = 0;
+               (*buf)++;
+               return;
+       default:
+               if (!str || !*str) {
+                       (*buf)[0] = '\0';
+                       return;
+               }
+               src_len = g_strlcpy (*buf, str, *len);
+               if (src_len >= *len) {
+                       *buf = &(*buf)[*len];
+                       *len = 0;
+               } else {
+                       *buf = &(*buf)[src_len];
+                       *len -= src_len;
+               }
+               return;
+       }
+}
+
+void
+nm_utils_strbuf_append (char **buf, gsize *len, const char *format, ...)
+{
+       char *p = *buf;
+       va_list args;
+       gint retval;
+
+       if (*len == 0)
+               return;
+
+       va_start (args, format);
+       retval = g_vsnprintf (p, *len, format, args);
+       va_end (args);
+
+       if (retval >= *len) {
+               *buf = &p[*len];
+               *len = 0;
+       } else {
+               *buf = &p[retval];
+               *len -= retval;
+       }
+}
+
+/*****************************************************************************/
+
 /* _nm_utils_ascii_str_to_int64:
  *
  * A wrapper for g_ascii_strtoll, that checks whether the whole string
@@ -45,7 +124,7 @@ gint64
 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback)
 {
        gint64 v;
-       char *s = NULL;
+       const char *s = NULL;
 
        if (str) {
                while (g_ascii_isspace (str[0]))
@@ -57,7 +136,7 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
        }
 
        errno = 0;
-       v = g_ascii_strtoll (str, &s, base);
+       v = g_ascii_strtoll (str, (char **) &s, base);
 
        if (errno != 0)
                return fallback;
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index cfa8f99..5d8a3a8 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -22,14 +22,33 @@
 #ifndef __NM_SHARED_UTILS_H__
 #define __NM_SHARED_UTILS_H__
 
-/******************************************************************************/
+/*****************************************************************************/
+
+static inline void
+_nm_utils_strbuf_init (char *buf, gsize len, char **p_buf_ptr, gsize *p_buf_len)
+{
+       NM_SET_OUT (p_buf_len, len);
+       NM_SET_OUT (p_buf_ptr, buf);
+       buf[0] = '\0';
+}
+
+#define nm_utils_strbuf_init(buf, p_buf_ptr, p_buf_len) \
+       G_STMT_START { \
+               G_STATIC_ASSERT (G_N_ELEMENTS (buf) == sizeof (buf) && sizeof (buf) > sizeof (char *)); \
+               _nm_utils_strbuf_init ((buf), sizeof (buf), (p_buf_ptr), (p_buf_len)); \
+       } G_STMT_END
+void nm_utils_strbuf_append (char **buf, gsize *len, const char *format, ...) _nm_printf (3, 4);
+void nm_utils_strbuf_append_c (char **buf, gsize *len, char c);
+void nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str);
+
+/*****************************************************************************/
 
 gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
 
 gint _nm_utils_ascii_str_to_bool (const char *str,
                                   gint default_value);
 
-/******************************************************************************/
+/*****************************************************************************/
 
 /**
  * NMUtilsError:
@@ -54,13 +73,13 @@ void nm_utils_error_set_cancelled (GError **error,
 gboolean nm_utils_error_is_cancelled (GError *error,
                                       gboolean consider_is_disposing);
 
-/******************************************************************************/
+/*****************************************************************************/
 
 gboolean nm_g_object_set_property (GObject *object,
                                    const gchar  *property_name,
                                    const GValue *value,
                                    GError **error);
 
-/******************************************************************************/
+/*****************************************************************************/
 
 #endif /* __NM_SHARED_UTILS_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]