]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: parser: Rename pakfire_parser_add_declaration() to set
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2019 15:32:02 +0000 (16:32 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 22 May 2019 15:32:02 +0000 (16:32 +0100)
This is what the function really does and it now also
updates values correctly.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/include/pakfire/parser.h
src/libpakfire/parser.c
src/libpakfire/parser/grammar.y

index 2d48124c5f1d8c9cc0985a67ad4b73044066be08..504e95c799a0ae1f5504eab159b19581de605eac 100644 (file)
@@ -27,7 +27,7 @@
 
 PakfireParser pakfire_parser_create(Pakfire pakfire);
 PakfireParser pakfire_parser_unref(PakfireParser parser);
-int pakfire_parser_add_declaration(PakfireParser parser,
+int pakfire_parser_set_declaration(PakfireParser parser,
                const char* name, const char* value);
 int pakfire_parser_append_declaration(PakfireParser parser,
        const char* name, const char* value);
index 95f800ee9fa19ca80b7ae400f16cad48fc66fee4..16185252759863d07a8f1fec3f144484db8e13ec 100644 (file)
@@ -108,18 +108,31 @@ static struct pakfire_parser_declaration* pakfire_parser_get_declaration(
        return NULL;
 }
 
-PAKFIRE_EXPORT int pakfire_parser_add_declaration(PakfireParser parser,
+PAKFIRE_EXPORT int pakfire_parser_set_declaration(PakfireParser parser,
                const char* name, const char* value) {
+       // Handle when name already exists
+       struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, name);
+       if (d) {
+               // Replace value
+               if (d->value)
+                       pakfire_free(d->value);
+               d->value = pakfire_strdup(value);
+
+               DEBUG(parser->pakfire, "Updated declaration: %s = %s\n",
+                       d->name, d->value);
+
+               // All done
+               return 0;
+       }
+
        // Check if we have any space left
        if (parser->next_declaration >= parser->num_declarations) {
                ERROR(parser->pakfire, "No free declarations left\n");
                return -1;
        }
 
-       // XXX handle when name already exists
-
        // Allocate a new declaration
-       struct pakfire_parser_declaration* d = pakfire_calloc(1, sizeof(*d));
+       d = pakfire_calloc(1, sizeof(*d));
        if (!d)
                return -1;
 
@@ -141,7 +154,7 @@ PAKFIRE_EXPORT int pakfire_parser_append_declaration(PakfireParser parser,
 
        // Add the declaration if we could not find it
        if (!d)
-               return pakfire_parser_add_declaration(parser, name, value);
+               return pakfire_parser_set_declaration(parser, name, value);
 
        char* buffer = NULL;
 
index be1e58011933e2984dadd4ccac33c4e6171d7b46..679803d00b293e66f88829aa396742b34a1cf9b9 100644 (file)
@@ -174,7 +174,7 @@ assignment                                  : variable T_ASSIGN value T_EOL
                                                                if (!name)
                                                                        ABORT;
 
-                                                               int r = pakfire_parser_add_declaration(parser, name, $3);
+                                                               int r = pakfire_parser_set_declaration(parser, name, $3);
                                                                pakfire_free(name);
 
                                                                if (r < 0)
@@ -198,7 +198,7 @@ assignment                                  : variable T_ASSIGN value T_EOL
                                                                if (!name)
                                                                        ABORT;
 
-                                                               int r = pakfire_parser_add_declaration(parser, name, $2);
+                                                               int r = pakfire_parser_set_declaration(parser, name, $2);
                                                                pakfire_free(name);
 
                                                                if (r < 0)