:: Re: [DNG] An Anti-Initsystem
Top Page
Delete this message
Reply to this message
Author: aitor
Date:  
To: dng
Subject: Re: [DNG] An Anti-Initsystem
Hi,

On 4/11/26 18:44, aitor wrote:
> Hi Rainer,
>
> On 3/29/26 21:44, Rainer Weikusat via Dng wrote:
>> The GitHub URL for this is
>>
>> https://github.com/rweikusat/process-tools
>
> I am writing to ask for your permission to include part of your
> *uni-json* source code:
>
> https://github.com/rweikusat/uni-json
>
> directly as a "built-in" third-party component (statically linked)
> within the vdev repository.
> Of course, keeping all your original copyright headers and the MIT
> license file intact. Please,
> let me know if you have any concerns or specific requirements
> regarding this.


I have a working example:

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include "uni_json_parser.h"
#include "uni_json_p_binding.h"
#include "uni_json_types.h"

/* union */
struct uni_json_value {
    unsigned type;
    union {
        struct {
            struct uni_json_object_entry *first;
        } obj;
        struct {
            uint8_t *s;
            size_t len;
        } s;
    } u;
};

struct uni_json_object_entry {
    uint8_t *key;
    struct uni_json_value *value;
    struct uni_json_object_entry *next;
};

/* Finalize returns the flat string (char *) */
static void *finalize_str(void *str, uint8_t *data, size_t len)
{
    (void)str;
    char *s = malloc(len + 1);

    if (s) {
        memcpy(s, data, len);
        s[len] = '\0';
    }

    return s;
}

/* wrap the 'value', but use the 'key' as it is */
static int add_2_obj(void *key, void *val, void *obj)
{
    struct uni_json_value *vobj = obj;
    struct uni_json_object_entry *entry = calloc(1, sizeof(*entry));

    if (!entry)
        return -1;

    /* Wrap the value in the struct that main() function expects */
    struct uni_json_value *vval = calloc(1, sizeof(*vval));
    vval->type = UJ_T_STR;
    vval->u.s.s = (uint8_t *)val;
    vval->u.s.len = strlen(val);

    /* key is the char * given by finalize_str() */
    entry->key = (uint8_t *)key;
    entry->value = vval;
    entry->next = vobj->u.obj.first;
    vobj->u.obj.first = entry;

    return 0;
}

static void *make_obj(void)
{
    struct uni_json_value *v = calloc(1, sizeof(*v));

    if (v)
        v->type = UJ_T_OBJ;

    return v;
}

struct uni_json_p_binding my_vdev_bindings = {
    .make_object = make_obj,
    .add_2_object = add_2_obj,
    .finalize_string = finalize_str
};

int main()
{
    struct uni_json_object_entry *e;

    char *config = "{\"phase\":\"early\", \"type\":\"one-shot\"}";

    struct uni_json_value *ev = (struct uni_json_value *)uni_json_parse(
        (uint8_t *)config, strlen(config), 10, &my_vdev_bindings, NULL);

    if (!ev)
        return 1;

    for (e = ev->u.obj.first; e; e = e->next)
        printf("%s=%s\n", (char *)e->key, (char *)e->value->u.s.s);

    /* Cleanup e->key, e->value->u.s.s, e->value, e and ev ?????? */

    return 0;
}

Cheers,

Aitor.