Skip to content

Commit

Permalink
Rearrange config code to allow multiple config files. HTCONDOR-521
Browse files Browse the repository at this point in the history
Move one-time config initialization from config_read_cmd() to
config_read(). This will allow config_read_cmd() to be called multiple
times with different config files.
  • Loading branch information
JaimeFrey committed Jul 26, 2021
1 parent 5e18504 commit 45a8ad2
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 108 deletions.
217 changes: 111 additions & 106 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ config_setenv(const char *ipath)
{
const char *printenv_command_before = "printenv";
const char *printenv_command_after = ". %s;printenv";
config_handle *envs_before;
config_handle *envs_after;
config_handle *envs_before = config_new(ipath);
config_handle *envs_after = config_new(ipath);
config_entry *cur;
int n_added = 0;

envs_before = config_read_cmd(ipath, printenv_command_before);
envs_after = config_read_cmd(ipath, printenv_command_after);
envs_before = config_read_cmd(ipath, printenv_command_before, envs_before);
envs_after = config_read_cmd(ipath, printenv_command_after, envs_after);


/* Set in the local environment all env variables that were exported in */
Expand All @@ -115,30 +115,11 @@ config_handle *
config_read(const char *ipath)
{
const char *set_command_format = ". %s; set";
return config_read_cmd(ipath, set_command_format);
}

config_handle *
config_read_cmd(const char *ipath, const char *set_command_format)
{
char *path;
char *install_location=NULL;
char *line=NULL,*new_line=NULL;
char *cur;
char *key_start, *key_end, *val_start, *val_end;
int key_len, val_len;
char *eq_pos;
config_handle *rha;
config_entry *c_tail = NULL;
config_entry *found,*new_entry=NULL;
char *set_command=NULL;
int set_command_size;
int line_len = 0;
int line_alloc = 0;
int c;
const int line_alloc_chunk = 128;
FILE *test,*cf;
config_entry *bp;
FILE *test;

if ((install_location = getenv("BLAHPD_LOCATION")) == NULL)
{
Expand Down Expand Up @@ -173,54 +154,112 @@ config_read_cmd(const char *ipath, const char *set_command_format)
if (path == NULL) return NULL;
}

set_command_size = snprintf(NULL,0,set_command_format,path)+1;
set_command = (char *)malloc(set_command_size);
if (set_command == NULL)
rha = config_new(path);
free(path);

if ( ! config_read_cmd(rha->config_path, set_command_format, rha) )
{
free(path);
config_free(rha);
return NULL;
}
snprintf(set_command, set_command_size, set_command_format, path);

cf = popen(set_command, "r");
if (cf == NULL)
if ((bp = config_get("blah_bin_directory", rha)) != NULL)
{
free(set_command);
free(path);
rha->bin_path = strdup(bp->value);
}
else
{
rha->bin_path = (char *)malloc(strlen(install_location)+5);
if (rha->bin_path != NULL) sprintf(rha->bin_path,"%s/bin",install_location);
}
if (rha->bin_path == NULL)
{
/* Out of memory */
config_free(rha);
return NULL;
}

free(set_command);

rha = (config_handle *)malloc(sizeof(config_handle));
if (rha == NULL)
if ((bp = config_get("blah_sbin_directory", rha)) != NULL)
{
fclose(cf);
free(path);
rha->sbin_path = strdup(bp->value);
}
else
{
rha->sbin_path = (char *)malloc(strlen(install_location)+6);
if (rha->sbin_path != NULL) sprintf(rha->sbin_path,"%s/sbin",install_location);
}
if (rha->sbin_path == NULL)
{
/* Out of memory */
config_free(rha);
return NULL;
}

rha->config_path = path;
rha->list = NULL;
if (install_location != NULL) rha->install_path = strdup(install_location);
if (rha->install_path == NULL)
if ((bp = config_get("blah_libexec_directory", rha)) != NULL)
{
rha->libexec_path = strdup(bp->value);
}
else
{
rha->libexec_path = (char *)malloc(strlen(install_location)+9);
if (rha->libexec_path != NULL) sprintf(rha->libexec_path,"%s/libexec",install_location);
}
if (rha->libexec_path == NULL)
{
/* Out of memory */
fclose(cf);
config_free(rha);
return NULL;
}
rha->bin_path = NULL; /* These may be filled out of config file contents. */
rha->sbin_path = NULL;
rha->libexec_path = NULL;

return rha;
}

int
config_read_cmd(const char *path, const char *set_command_format, config_handle *rha)
{
char *line=NULL,*new_line=NULL;
char *cur;
char *key_start, *key_end, *val_start, *val_end;
int key_len, val_len;
char *eq_pos;
config_entry *c_tail = NULL;
config_entry *found,*new_entry=NULL;
char *set_command=NULL;
int set_command_size;
int line_len = 0;
int line_alloc = 0;
int c;
const int line_alloc_chunk = 128;
FILE *cf;

if (path == NULL)
{
return FALSE;
}

set_command_size = snprintf(NULL,0,set_command_format,path)+1;
set_command = (char *)malloc(set_command_size);
if (set_command == NULL)
{
return FALSE;
}
snprintf(set_command, set_command_size, set_command_format, path);

cf = popen(set_command, "r");
if (cf == NULL)
{
free(set_command);
return FALSE;
}

free(set_command);

line_alloc = line_alloc_chunk;
line = (char *)malloc(line_alloc);
if (line == NULL)
{
fclose(cf);
config_free(rha);
return NULL;
return FALSE;
}
line[0]='\000';

Expand Down Expand Up @@ -269,8 +308,7 @@ config_read_cmd(const char *ipath, const char *set_command_format)
{
/* Out of memory */
free(line);
config_free(rha);
return NULL;
return FALSE;
}
memcpy(found->value, val_start, val_len);
found->value[val_len] = '\000';
Expand All @@ -283,8 +321,7 @@ config_read_cmd(const char *ipath, const char *set_command_format)
{
/* Out of memory */
free(line);
config_free(rha);
return NULL;
return FALSE;
}

new_entry->key = new_entry->value = NULL;
Expand All @@ -299,8 +336,7 @@ config_read_cmd(const char *ipath, const char *set_command_format)
{
/* Out of memory */
free(line);
config_free(rha);
return NULL;
return FALSE;
}
memcpy(new_entry->key, key_start, key_len);
new_entry->key[key_len] = '\000';
Expand Down Expand Up @@ -336,55 +372,7 @@ config_read_cmd(const char *ipath, const char *set_command_format)
fclose(cf);
free(line);

if ((bp = config_get("blah_bin_directory", rha)) != NULL)
{
rha->bin_path = strdup(bp->value);
}
else
{
rha->bin_path = (char *)malloc(strlen(install_location)+5);
if (rha->bin_path != NULL) sprintf(rha->bin_path,"%s/bin",install_location);
}
if (rha->bin_path == NULL)
{
/* Out of memory */
config_free(rha);
return NULL;
}

if ((bp = config_get("blah_sbin_directory", rha)) != NULL)
{
rha->sbin_path = strdup(bp->value);
}
else
{
rha->sbin_path = (char *)malloc(strlen(install_location)+6);
if (rha->sbin_path != NULL) sprintf(rha->sbin_path,"%s/sbin",install_location);
}
if (rha->sbin_path == NULL)
{
/* Out of memory */
config_free(rha);
return NULL;
}

if ((bp = config_get("blah_libexec_directory", rha)) != NULL)
{
rha->libexec_path = strdup(bp->value);
}
else
{
rha->libexec_path = (char *)malloc(strlen(install_location)+9);
if (rha->libexec_path != NULL) sprintf(rha->libexec_path,"%s/libexec",install_location);
}
if (rha->libexec_path == NULL)
{
/* Out of memory */
config_free(rha);
return NULL;
}

return rha;
return TRUE;
}

config_entry *
Expand Down Expand Up @@ -427,6 +415,24 @@ config_test_boolean(const config_entry *entry)
return FALSE;
}

config_handle *
config_new(const char *path)
{
config_handle *rha = (config_handle *)malloc(sizeof(config_handle));
if (rha == NULL)
{
return NULL;
}

rha->config_path = strdup(path);
rha->list = NULL;
rha->bin_path = NULL; /* These may be filled out of config file contents. */
rha->sbin_path = NULL;
rha->libexec_path = NULL;

return rha;
}

void
config_free(config_handle *handle)
{
Expand Down Expand Up @@ -456,7 +462,6 @@ config_free(config_handle *handle)
}

if ((handle->config_path) != NULL) free(handle->config_path);
if ((handle->install_path) != NULL) free(handle->install_path);
if ((handle->bin_path) != NULL) free(handle->bin_path);
if ((handle->sbin_path) != NULL) free(handle->sbin_path);
if ((handle->libexec_path) != NULL) free(handle->libexec_path);
Expand Down Expand Up @@ -531,7 +536,7 @@ main(int argc, char *argv[])

setenv("BLAHPD_CONFIG_LOCATION",path,1);
cha = config_read(NULL);
n_env = config_setenv(NULL);
n_env = config_setenv(path);
unlink(path);
if (cha == NULL)
{
Expand Down
4 changes: 2 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ typedef struct config_entry_s

typedef struct config_handle_s
{
char *install_path;
char *bin_path;
char *sbin_path;
char *libexec_path;
Expand All @@ -57,10 +56,11 @@ typedef struct config_handle_s
} config_handle;

config_handle *config_read(const char *path);
config_handle *config_read_cmd(const char *path, const char *cmd);
int config_read_cmd(const char *path, const char *cmd, config_handle *handle);
int config_setenv(const char *ipath);
config_entry *config_get(const char *key, config_handle *handle);
int config_test_boolean(const config_entry *entry);
config_handle *config_new(const char *path);
void config_free(config_handle *handle);

#define CONFIG_FILE_BASE "blah.config"
Expand Down

0 comments on commit 45a8ad2

Please sign in to comment.