texinfo-commits
[Top][All Lists]
Advanced

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

[no subject]


From: Patrice Dumas
Date: Wed, 3 Jan 2024 18:10:55 -0500 (EST)

branch: master
commit 2c8b0420e5cfd8c1788d1464f982a9feed204b66
Author: Patrice Dumas <pertusus@free.fr>
AuthorDate: Wed Jan 3 22:30:19 2024 +0100

    * tp/Texinfo/XS/convert/converter.c (copy_option, new_option_value)
    (command_init, set_global_document_commands): simplify command_init
    by simply copying the option.  Properly initialize all the fields in
    new_option_value.  Add copy_option to copy an option in another and
    use it in set_global_document_commands.  Only copy option if set field
    is not on.
---
 ChangeLog                         |  9 +++++
 tp/Texinfo/XS/convert/converter.c | 71 ++++++++++++++++++---------------------
 2 files changed, 41 insertions(+), 39 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 234311de77..d3b4f2cb58 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2024-01-03  Patrice Dumas  <pertusus@free.fr>
+
+       * tp/Texinfo/XS/convert/converter.c (copy_option, new_option_value)
+       (command_init, set_global_document_commands): simplify command_init
+       by simply copying the option.  Properly initialize all the fields in
+       new_option_value.  Add copy_option to copy an option in another and
+       use it in set_global_document_commands.  Only copy option if set field
+       is not on.
+
 2024-01-03  Patrice Dumas  <pertusus@free.fr>
 
        * tp/Texinfo/XS/main/utils.h: remove COMMAND_OPTION_REF and
diff --git a/tp/Texinfo/XS/convert/converter.c 
b/tp/Texinfo/XS/convert/converter.c
index afa5718a94..b6c66a9948 100644
--- a/tp/Texinfo/XS/convert/converter.c
+++ b/tp/Texinfo/XS/convert/converter.c
@@ -105,12 +105,33 @@ unregister_converter_descriptor (int converter_descriptor)
     }
 }
 
+static void
+copy_option (OPTION *destination, OPTION *source)
+{
+  switch (source->type)
+   {
+     case GO_integer:
+       destination->integer = source->integer;
+       break;
+
+     case GO_char:
+     case GO_bytes:
+       free (destination->string);
+       destination->string = strdup (source->string);
+       break;
+
+     default:
+       fprintf (stderr, "BUG: copy_option type not handled: %d\n",
+                source->type);
+   }
+}
+
 /* freed by caller */
 static OPTION *
 new_option_value (enum global_option_type type, int int_value, char 
*char_value)
 {
-  OPTION *result
-    = (OPTION *) malloc (sizeof (OPTION));
+  OPTION *result = (OPTION *) malloc (sizeof (OPTION));
+  memset (result, 0, sizeof (OPTION));
   result->type = type;
   if (type == GO_integer)
     result->integer = int_value;
@@ -119,7 +140,8 @@ new_option_value (enum global_option_type type, int 
int_value, char *char_value)
   return result;
 }
 
-/* freed by caller */
+/* freed by caller.  Information in structure refers to other data, so
+   should not be freed */
 static OPTION *
 command_init (enum command_id cmd, OPTIONS *init_conf)
 {
@@ -131,26 +153,9 @@ command_init (enum command_id cmd, OPTIONS *init_conf)
       init_conf_ref = get_command_option (init_conf, cmd);
       if (init_conf_ref)
         {
-          if (init_conf_ref->type == GO_integer)
-            {
-              if (init_conf_ref->integer >= 0)
-                {
-                  option_value
-                    = new_option_value (GO_integer, init_conf_ref->integer, 0);
-                  return option_value;
-                }
-            }
-          else
-            {
-              if (init_conf_ref->type == GO_char
-                  || init_conf_ref->type == GO_bytes)
-                {
-                  option_value
-                    = new_option_value (init_conf_ref->type, -1,
-                                        init_conf_ref->string);
-                  return option_value;
-                }
-            }
+          option_value = (OPTION *) malloc (sizeof (OPTION));
+          memcpy (option_value, init_conf_ref, sizeof (OPTION));
+          return option_value;
         }
     }
   option_default = &command_option_default_table[cmd];
@@ -184,13 +189,8 @@ set_global_document_commands (CONVERTER *converter,
             {
               OPTION *option_ref
                = get_command_option (converter->conf, cmd);
-              if (option_value->type == GO_integer)
-                option_ref->integer = option_value->integer;
-              else
-                {
-                  free (option_ref->string);
-                  option_ref->string = strdup (option_value->string);
-                }
+              if (option_ref->set <= 0)
+                copy_option (option_ref, option_value);
               free (option_value);
             }
         }
@@ -220,15 +220,8 @@ set_global_document_commands (CONVERTER *converter,
                 {
                   OPTION *option_ref
                     = get_command_option (converter->conf, cmd);
-                  if (option_value->type == GO_integer)
-                    option_ref->integer = option_value->integer;
-                  else
-                    {
-                      free (option_ref->string);
-                      option_ref->string
-                        = strdup (option_value->string);
-                    }
-
+                  if (option_ref->set <= 0)
+                    copy_option (option_ref, option_value);
                   free (option_value);
                 }
             }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]