File parse_compat_flags.patch of Package gpg2.42184

commit ce63eaa4f8f3f41aafcaddd8d658dacd522334a8
Author: Werner Koch <wk@gnupg.org>
Date:   Mon Jun 13 17:35:05 2022 +0200

    common: New function parse_compatibility_flags.
    
    * common/miscellaneous.c (parse_compatibility_flags): New.
    * common/util.h (struct compatibility_flags_s): New.
    --
    
    This is similar to parse_debug_flags but does not support specifying a
    value.  This way we can more easily change the internal values or
    re-use them for other purposes.

diff --git a/common/miscellaneous.c b/common/miscellaneous.c
index 5ede00128..df6b68784 100644
--- a/common/miscellaneous.c
+++ b/common/miscellaneous.c
@@ -712,3 +712,83 @@ parse_debug_flag (const char *string, unsigned int *debugvar,
   *debugvar |= result;
   return 0;
 }
+
+
+
+/* Parse an --comaptibility_flags style argument consisting of comma
+ * separated strings.
+ *
+ * Returns: 0 on success or -1 and ERRNO set on error.  On success the
+ *          supplied variable is updated by the parsed flags.
+ *
+ * If STRING is NULL the enabled flags are printed.
+ */
+int
+parse_compatibility_flags (const char *string, unsigned int *flagvar,
+                           const struct compatibility_flags_s *flags)
+
+{
+  unsigned long result = 0;
+  int i, j;
+
+  if (!string)
+    {
+      if (flagvar)
+        {
+          log_info ("enabled compatibility flags:");
+          for (i=0; flags[i].name; i++)
+            if ((*flagvar & flags[i].flag))
+              log_printf (" %s", flags[i].name);
+          log_printf ("\n");
+        }
+      return 0;
+    }
+
+  while (spacep (string))
+    string++;
+
+  if (!strcmp (string, "?") || !strcmp (string, "help"))
+    {
+      log_info ("available compatibility flags:\n");
+      for (i=0; flags[i].name; i++)
+        log_info (" %s\n", flags[i].name);
+      if (flags[i].flag != 77)
+        exit (0);
+    }
+  else
+    {
+      char **words;
+      words = strtokenize (string, ",");
+      if (!words)
+        return -1;
+      for (i=0; words[i]; i++)
+        {
+          if (*words[i])
+            {
+              for (j=0; flags[j].name; j++)
+                if (!strcmp (words[i], flags[j].name))
+                  {
+                    result |= flags[j].flag;
+                    break;
+                  }
+              if (!flags[j].name)
+                {
+                  if (!strcmp (words[i], "none"))
+                    {
+                      *flagvar = 0;
+                      result = 0;
+                    }
+                  else if (!strcmp (words[i], "all"))
+                    result = ~0;
+                  else
+                    log_info ("unknown compatibility flag '%s' ignored\n",
+                              words[i]);
+                }
+            }
+        }
+      xfree (words);
+    }
+
+  *flagvar |= result;
+  return 0;
+}
diff --git a/common/util.h b/common/util.h
index 4327153bc..62e5af51d 100644
--- a/common/util.h
+++ b/common/util.h
@@ -372,6 +372,15 @@ struct debug_flags_s
 int parse_debug_flag (const char *string, unsigned int *debugvar,
                       const struct debug_flags_s *flags);
 
+struct compatibility_flags_s
+{
+  unsigned int flag;
+  const char *name;
+  const char *desc;
+};
+int parse_compatibility_flags (const char *string, unsigned int *flagvar,
+                               const struct compatibility_flags_s *flags);
+
 
 /*-- Simple replacement functions. */
 
openSUSE Build Service is sponsored by