I have a string with the following content (UTF-8):
__$FOO ${FOO} ${FOO:def} ${FOO2:-тест}
And environment variable FOO with a value test. My C application should work like a GNU envsubs - replace all $FOO or ${FOO} entries with a test - nothing complicated. Expected result:
__test test test тест
But... How can I do this using C only? I can't use something like exec or external (dynamic) libraries (my app is statically linked for using in docker scratch).
I know about envsubst from gettext, but it does not support the default values, as a minimum.
I found libraries with all required features in Go - stephenc/envsub and Rust - stephenc/envsub, but maybe anyone knows how I can do the same in C? I don't want to invent something that has probably already been invented.
static char *envsubst(char *str) {
// magic
}
Since I could not find an answer, I decided to write my own parser. It has less functionality than I was looking for, but that was enough for my case:
And the tests: