fixup! Add automatic installer and upgrader.
This commit is contained in:
parent
4824083707
commit
5bde899212
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2017 Jonas 'Sortie' Termansen.
|
* Copyright (c) 2017, 2023 Jonas 'Sortie' Termansen.
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -28,15 +28,50 @@
|
||||||
|
|
||||||
#include "autoconf.h"
|
#include "autoconf.h"
|
||||||
|
|
||||||
// TODO: This file is very unfinished draft stuff.
|
static char** keyvalues = NULL;
|
||||||
|
static size_t keyvalues_used = 0;
|
||||||
|
static size_t keyvalues_length = 0;
|
||||||
|
|
||||||
bool has_autoconf = false;
|
bool has_autoconf = false;
|
||||||
|
|
||||||
const char* autoconf_get(const char* name)
|
const char* autoconf_get(const char* name)
|
||||||
{
|
{
|
||||||
if ( !name || !has_autoconf )
|
if ( !name )
|
||||||
return NULL;
|
return NULL;
|
||||||
return getenv(name);
|
size_t len = strlen(name);
|
||||||
|
for ( size_t i = 0; i < keyvalues_used; i++ )
|
||||||
|
if ( !strncmp(keyvalues[i], name, len) && keyvalues[i][len] == '=' )
|
||||||
|
return keyvalues[i] + len + 1;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool autoconf_set(const char* name, const char* value)
|
||||||
|
{
|
||||||
|
char* keyvalue;
|
||||||
|
if ( asprintf(&keyvalue, "%s=%s", name, value) < 0 )
|
||||||
|
return false;
|
||||||
|
size_t len = strlen(name);
|
||||||
|
for ( size_t i = 0; i < keyvalues_used; i++ )
|
||||||
|
{
|
||||||
|
if ( !strncmp(keyvalues[i], name, len) && keyvalues[i][len] == '=' )
|
||||||
|
{
|
||||||
|
free(keyvalues[i]);
|
||||||
|
keyvalues[i] = keyvalue;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ( keyvalues_used == keyvalues_length )
|
||||||
|
{
|
||||||
|
size_t new_length = keyvalues_length ? 2 * keyvalues_length : 4;
|
||||||
|
char** new_keyvalues =
|
||||||
|
reallocarray(keyvalues, new_length, sizeof(char*));
|
||||||
|
if ( !new_keyvalues )
|
||||||
|
return free(keyvalue), false;
|
||||||
|
keyvalues = new_keyvalues;
|
||||||
|
keyvalues_length = new_length;
|
||||||
|
}
|
||||||
|
keyvalues[keyvalues_used++] = keyvalue;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void autoconf_load(const char* path)
|
void autoconf_load(const char* path)
|
||||||
|
@ -75,40 +110,43 @@ void autoconf_load(const char* path)
|
||||||
{
|
{
|
||||||
name[name_length + 0] = '\0';
|
name[name_length + 0] = '\0';
|
||||||
char* value = name + name_length + 3;
|
char* value = name + name_length + 3;
|
||||||
const char* existing = getenv(name);
|
const char* existing = autoconf_get(name);
|
||||||
if ( existing )
|
if ( existing )
|
||||||
{
|
{
|
||||||
char* full;
|
char* full;
|
||||||
if ( asprintf(&full, "%s\n%s", existing, value) < 0 )
|
if ( asprintf(&full, "%s\n%s", existing, value) < 0 )
|
||||||
err(2, "%s: malloc", path);
|
err(2, "%s: malloc", path);
|
||||||
setenv(name, full, 1);
|
if ( !autoconf_set(name, full) )
|
||||||
|
err(2, "%s: malloc", path);
|
||||||
free(full);
|
free(full);
|
||||||
}
|
}
|
||||||
else
|
else if ( !autoconf_set(name, value) )
|
||||||
setenv(name, value, 1);
|
err(2, "%s: malloc", path);
|
||||||
}
|
}
|
||||||
else if ( name[name_length + 0] == '+' &&
|
else if ( name[name_length + 0] == '+' &&
|
||||||
name[name_length + 1] == '=' )
|
name[name_length + 1] == '=' )
|
||||||
{
|
{
|
||||||
name[name_length + 0] = '\0';
|
name[name_length + 0] = '\0';
|
||||||
char* value = name + name_length + 2;
|
char* value = name + name_length + 2;
|
||||||
const char* existing = getenv(name);
|
const char* existing = autoconf_get(name);
|
||||||
if ( existing )
|
if ( existing )
|
||||||
{
|
{
|
||||||
char* full;
|
char* full;
|
||||||
if ( asprintf(&full, "%s %s", existing, value) < 0 )
|
if ( asprintf(&full, "%s %s", existing, value) < 0 )
|
||||||
err(2, "%s: malloc", path);
|
err(2, "%s: malloc", path);
|
||||||
setenv(name, full, 1);
|
if ( !autoconf_set(name, full) )
|
||||||
|
err(2, "%s: malloc", path);
|
||||||
free(full);
|
free(full);
|
||||||
}
|
}
|
||||||
else
|
else if ( !autoconf_set(name, value) )
|
||||||
setenv(name, value, 1);
|
err(2, "%s: malloc", path);
|
||||||
}
|
}
|
||||||
else if ( name[name_length + 0] == '=' )
|
else if ( name[name_length + 0] == '=' )
|
||||||
{
|
{
|
||||||
name[name_length + 0] = '\0';
|
name[name_length + 0] = '\0';
|
||||||
char* value = name + name_length + 1;
|
char* value = name + name_length + 1;
|
||||||
setenv(name, value, 1);
|
if ( !autoconf_set(name, value) )
|
||||||
|
err(2, "%s: malloc", path);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
errx(2, "%s:%ji: Invalid line: %s",
|
errx(2, "%s:%ji: Invalid line: %s",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2017 Jonas 'Sortie' Termansen.
|
* Copyright (c) 2017, 2023 Jonas 'Sortie' Termansen.
|
||||||
*
|
*
|
||||||
* Permission to use, copy, modify, and distribute this software for any
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
@ -23,6 +23,7 @@
|
||||||
extern bool has_autoconf;
|
extern bool has_autoconf;
|
||||||
|
|
||||||
const char* autoconf_get(const char* name);
|
const char* autoconf_get(const char* name);
|
||||||
|
bool autoconf_set(const char* name, const char* value);
|
||||||
void autoconf_load(const char* path);
|
void autoconf_load(const char* path);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue