2022-06-26 22:58:56 +00:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
|
|
|
|
2008-02-25 17:48:04 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2004, 2005 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
* *
|
2009-07-17 19:54:25 +00:00
|
|
|
* Copyright (C) 2007,2008 Øyvind Harboe *
|
2008-07-25 06:54:17 +00:00
|
|
|
* oyvind.harboe@zylin.com *
|
2008-02-25 17:48:04 +00:00
|
|
|
***************************************************************************/
|
2021-04-19 20:04:30 +00:00
|
|
|
|
2008-02-25 17:48:04 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "log.h"
|
2021-04-19 20:04:30 +00:00
|
|
|
#include "replacements.h"
|
2008-02-25 17:48:04 +00:00
|
|
|
|
|
|
|
static size_t num_config_files;
|
2012-01-30 14:31:21 +00:00
|
|
|
static char **config_file_names;
|
2008-02-25 17:48:04 +00:00
|
|
|
|
|
|
|
static size_t num_script_dirs;
|
2012-01-30 14:31:21 +00:00
|
|
|
static char **script_search_dirs;
|
2008-02-25 17:48:04 +00:00
|
|
|
|
2012-01-30 14:31:21 +00:00
|
|
|
void add_script_search_dir(const char *dir)
|
2008-02-25 17:48:04 +00:00
|
|
|
{
|
|
|
|
num_script_dirs++;
|
2013-09-28 19:43:37 +00:00
|
|
|
script_search_dirs = realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
|
2008-02-25 17:48:04 +00:00
|
|
|
|
|
|
|
script_search_dirs[num_script_dirs-1] = strdup(dir);
|
|
|
|
script_search_dirs[num_script_dirs] = NULL;
|
2009-11-21 17:30:09 +00:00
|
|
|
|
|
|
|
LOG_DEBUG("adding %s", dir);
|
2008-02-25 17:48:04 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 14:31:21 +00:00
|
|
|
void add_config_command(const char *cfg)
|
2008-02-25 17:48:04 +00:00
|
|
|
{
|
|
|
|
num_config_files++;
|
2013-09-28 19:43:37 +00:00
|
|
|
config_file_names = realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
|
2008-02-25 17:48:04 +00:00
|
|
|
|
|
|
|
config_file_names[num_config_files-1] = strdup(cfg);
|
|
|
|
config_file_names[num_config_files] = NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-14 21:38:13 +00:00
|
|
|
void free_config(void)
|
|
|
|
{
|
|
|
|
while (num_config_files)
|
|
|
|
free(config_file_names[--num_config_files]);
|
|
|
|
|
|
|
|
free(config_file_names);
|
|
|
|
config_file_names = NULL;
|
|
|
|
|
|
|
|
while (num_script_dirs)
|
|
|
|
free(script_search_dirs[--num_script_dirs]);
|
|
|
|
|
|
|
|
free(script_search_dirs);
|
|
|
|
script_search_dirs = NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-27 06:20:41 +00:00
|
|
|
/* return full path or NULL according to search rules */
|
2008-07-14 06:34:23 +00:00
|
|
|
char *find_file(const char *file)
|
2008-02-25 17:48:04 +00:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char **search_dirs = script_search_dirs;
|
|
|
|
char *dir;
|
2012-01-30 14:31:21 +00:00
|
|
|
char const *mode = "r";
|
2009-11-21 22:29:58 +00:00
|
|
|
char *full_path;
|
2008-02-25 17:48:04 +00:00
|
|
|
|
|
|
|
/* Check absolute and relative to current working dir first.
|
|
|
|
* This keeps full_path reporting belowing working. */
|
2009-11-21 22:29:58 +00:00
|
|
|
full_path = alloc_printf("%s", file);
|
2008-02-25 17:48:04 +00:00
|
|
|
fp = fopen(full_path, mode);
|
|
|
|
|
2012-01-30 14:31:21 +00:00
|
|
|
while (!fp) {
|
2009-11-21 22:29:58 +00:00
|
|
|
free(full_path);
|
|
|
|
full_path = NULL;
|
2008-02-25 17:48:04 +00:00
|
|
|
dir = *search_dirs++;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
break;
|
|
|
|
|
2009-11-21 22:29:58 +00:00
|
|
|
full_path = alloc_printf("%s/%s", dir, file);
|
2008-02-25 17:48:04 +00:00
|
|
|
fp = fopen(full_path, mode);
|
|
|
|
}
|
2008-10-08 11:06:44 +00:00
|
|
|
|
2012-01-30 14:31:21 +00:00
|
|
|
if (fp) {
|
2008-06-27 06:20:41 +00:00
|
|
|
fclose(fp);
|
|
|
|
LOG_DEBUG("found %s", full_path);
|
2009-11-21 22:29:58 +00:00
|
|
|
return full_path;
|
2008-06-27 06:20:41 +00:00
|
|
|
}
|
2009-11-21 22:29:58 +00:00
|
|
|
|
|
|
|
free(full_path);
|
|
|
|
|
2008-06-27 06:20:41 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-25 17:48:04 +00:00
|
|
|
|
2009-11-13 22:21:56 +00:00
|
|
|
FILE *open_file_from_path(const char *file, const char *mode)
|
2008-06-27 06:20:41 +00:00
|
|
|
{
|
2012-01-30 14:31:21 +00:00
|
|
|
if (mode[0] != 'r')
|
2008-06-27 06:20:41 +00:00
|
|
|
return fopen(file, mode);
|
2012-01-30 14:31:21 +00:00
|
|
|
else {
|
2009-06-23 22:42:54 +00:00
|
|
|
char *full_path = find_file(file);
|
2021-07-03 16:51:20 +00:00
|
|
|
if (!full_path)
|
2008-10-08 11:06:44 +00:00
|
|
|
return NULL;
|
2008-06-27 06:20:41 +00:00
|
|
|
FILE *fp = NULL;
|
|
|
|
fp = fopen(full_path, mode);
|
|
|
|
free(full_path);
|
|
|
|
return fp;
|
2008-02-29 11:31:49 +00:00
|
|
|
}
|
2008-02-25 17:48:04 +00:00
|
|
|
}
|
|
|
|
|
2009-11-13 21:25:47 +00:00
|
|
|
int parse_config_file(struct command_context *cmd_ctx)
|
2008-02-25 17:48:04 +00:00
|
|
|
{
|
2008-07-11 15:07:58 +00:00
|
|
|
int retval;
|
2008-02-25 17:48:04 +00:00
|
|
|
char **cfg;
|
|
|
|
|
2010-01-05 21:32:39 +00:00
|
|
|
if (!config_file_names) {
|
|
|
|
command_run_line(cmd_ctx, "script openocd.cfg");
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2008-02-25 17:48:04 +00:00
|
|
|
|
|
|
|
cfg = config_file_names;
|
|
|
|
|
2012-01-30 14:31:21 +00:00
|
|
|
while (*cfg) {
|
2009-06-23 22:42:54 +00:00
|
|
|
retval = command_run_line(cmd_ctx, *cfg);
|
2009-06-23 22:38:12 +00:00
|
|
|
if (retval != ERROR_OK)
|
2008-07-11 15:07:58 +00:00
|
|
|
return retval;
|
2008-02-25 17:48:04 +00:00
|
|
|
cfg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2013-03-27 16:28:31 +00:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *get_home_dir(const char *append_path)
|
|
|
|
{
|
2022-06-19 20:10:47 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
char homepath[MAX_PATH];
|
|
|
|
#endif
|
|
|
|
|
2013-03-27 16:28:31 +00:00
|
|
|
char *home = getenv("HOME");
|
|
|
|
|
2021-07-03 16:51:20 +00:00
|
|
|
if (!home) {
|
2013-03-27 16:28:31 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
home = getenv("USERPROFILE");
|
|
|
|
|
2021-07-03 16:51:20 +00:00
|
|
|
if (!home) {
|
2013-03-27 16:28:31 +00:00
|
|
|
char *drive = getenv("HOMEDRIVE");
|
|
|
|
char *path = getenv("HOMEPATH");
|
|
|
|
if (drive && path) {
|
|
|
|
snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
|
|
|
|
home = homepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct passwd *pwd = getpwuid(getuid());
|
|
|
|
if (pwd)
|
|
|
|
home = pwd->pw_dir;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-03 16:51:20 +00:00
|
|
|
if (!home)
|
2013-03-27 16:28:31 +00:00
|
|
|
return home;
|
|
|
|
|
|
|
|
char *home_path;
|
|
|
|
|
|
|
|
if (append_path)
|
|
|
|
home_path = alloc_printf("%s/%s", home, append_path);
|
|
|
|
else
|
|
|
|
home_path = alloc_printf("%s", home);
|
|
|
|
|
|
|
|
return home_path;
|
|
|
|
}
|