← Back to context

Comment by koito17

4 days ago

The README gets straight to the point and I really like that.

Additionally, the .env file parser is quite clean.

https://github.com/ashtonjamesd/lavandula/blob/main/src/dote...

However, it doesn't seem that the parser supports comments. I guess a "good first issue" for anyone interested in contributing would be extending the `skipWhitespace` function to detect `#` tokens and skip the rest of the line when present.

Would also need to handle edge cases like env vars having values containing `#` tokens inside (but these will be quoted, so it's probably not too tricky to handle.)

> Additionally, the .env file parser is quite clean.

I didn't find it clean; it's so over-engineered that you won't easily be able to spot bugs in it.

What you want is (assuming you have a string-trimming function):

    while ((fgets (name, sizeof name, inputf)) {
        if (!(value = strchr (name, '='))) { continue; }
        *value++ = 0;
        strtrim(name);
        strtrim(value);
        if (!*name) { continue; }
        // Now store `name` and `value`
    }

> I guess a "good first issue" for anyone interested in contributing would be extending the `skipWhitespace` function to detect `#` tokens and skip the rest of the line when present.

Or do it before processing:

    // First statement of while loop
    char *comment = strchr (name, '#');
    if (comment) *comment = 0;
    // Continue with processing `name`

The way it's done in the linked code raises a ton of red flags.

So what happens when the env value happens to actually have a # in it?

So you then need to implement escaping which can go from a very simple implementation to an actual lookahead parser

EDIT:

Actually I agree, this parser is already very overbuilt and should be able to handle comments. Generally an env parser is a few lines a best… you need to read a line, look for the first instance of the separator, generally no reason to build a full parser for that, env is an absurdly simple format if you don’t want features like comments.

Hell even an ini format parser is simple to implement in the same style.