2008-02-25 11:48:04 -06:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2004, 2005 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
* *
|
2009-07-17 14:54:25 -05:00
|
|
|
* Copyright (C) 2007,2008 Øyvind Harboe *
|
2008-07-25 01:54:17 -05:00
|
|
|
* oyvind.harboe@zylin.com *
|
|
|
|
* *
|
2008-02-25 11:48:04 -06:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
2013-06-02 14:32:36 -05:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
|
2008-02-25 11:48:04 -06:00
|
|
|
***************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "configuration.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static size_t num_config_files;
|
2012-01-30 08:31:21 -06:00
|
|
|
static char **config_file_names;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
static size_t num_script_dirs;
|
2012-01-30 08:31:21 -06:00
|
|
|
static char **script_search_dirs;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
void add_script_search_dir(const char *dir)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
|
|
|
num_script_dirs++;
|
2013-09-28 14:43:37 -05:00
|
|
|
script_search_dirs = realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
script_search_dirs[num_script_dirs-1] = strdup(dir);
|
|
|
|
script_search_dirs[num_script_dirs] = NULL;
|
2009-11-21 11:30:09 -06:00
|
|
|
|
|
|
|
LOG_DEBUG("adding %s", dir);
|
2008-02-25 11:48:04 -06:00
|
|
|
}
|
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
void add_config_command(const char *cfg)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
|
|
|
num_config_files++;
|
2013-09-28 14:43:37 -05:00
|
|
|
config_file_names = realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
config_file_names[num_config_files-1] = strdup(cfg);
|
|
|
|
config_file_names[num_config_files] = NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-27 01:20:41 -05:00
|
|
|
/* return full path or NULL according to search rules */
|
2008-07-14 01:34:23 -05:00
|
|
|
char *find_file(const char *file)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
|
|
|
FILE *fp = NULL;
|
|
|
|
char **search_dirs = script_search_dirs;
|
|
|
|
char *dir;
|
2012-01-30 08:31:21 -06:00
|
|
|
char const *mode = "r";
|
2009-11-21 16:29:58 -06:00
|
|
|
char *full_path;
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
/* Check absolute and relative to current working dir first.
|
|
|
|
* This keeps full_path reporting belowing working. */
|
2009-11-21 16:29:58 -06:00
|
|
|
full_path = alloc_printf("%s", file);
|
2008-02-25 11:48:04 -06:00
|
|
|
fp = fopen(full_path, mode);
|
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
while (!fp) {
|
2009-11-21 16:29:58 -06:00
|
|
|
free(full_path);
|
|
|
|
full_path = NULL;
|
2008-02-25 11:48:04 -06:00
|
|
|
dir = *search_dirs++;
|
|
|
|
|
|
|
|
if (!dir)
|
|
|
|
break;
|
|
|
|
|
2009-11-21 16:29:58 -06:00
|
|
|
full_path = alloc_printf("%s/%s", dir, file);
|
2008-02-25 11:48:04 -06:00
|
|
|
fp = fopen(full_path, mode);
|
|
|
|
}
|
2008-10-08 06:06:44 -05:00
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
if (fp) {
|
2008-06-27 01:20:41 -05:00
|
|
|
fclose(fp);
|
|
|
|
LOG_DEBUG("found %s", full_path);
|
2009-11-21 16:29:58 -06:00
|
|
|
return full_path;
|
2008-06-27 01:20:41 -05:00
|
|
|
}
|
2009-11-21 16:29:58 -06:00
|
|
|
|
|
|
|
free(full_path);
|
|
|
|
|
2008-06-27 01:20:41 -05:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-02-25 11:48:04 -06:00
|
|
|
|
2009-11-13 16:21:56 -06:00
|
|
|
FILE *open_file_from_path(const char *file, const char *mode)
|
2008-06-27 01:20:41 -05:00
|
|
|
{
|
2012-01-30 08:31:21 -06:00
|
|
|
if (mode[0] != 'r')
|
2008-06-27 01:20:41 -05:00
|
|
|
return fopen(file, mode);
|
2012-01-30 08:31:21 -06:00
|
|
|
else {
|
2009-06-23 17:42:54 -05:00
|
|
|
char *full_path = find_file(file);
|
2009-06-23 17:42:03 -05:00
|
|
|
if (full_path == NULL)
|
2008-10-08 06:06:44 -05:00
|
|
|
return NULL;
|
2008-06-27 01:20:41 -05:00
|
|
|
FILE *fp = NULL;
|
|
|
|
fp = fopen(full_path, mode);
|
|
|
|
free(full_path);
|
|
|
|
return fp;
|
2008-02-29 05:31:49 -06:00
|
|
|
}
|
2008-02-25 11:48:04 -06:00
|
|
|
}
|
|
|
|
|
2009-11-13 15:25:47 -06:00
|
|
|
int parse_config_file(struct command_context *cmd_ctx)
|
2008-02-25 11:48:04 -06:00
|
|
|
{
|
2008-07-11 10:07:58 -05:00
|
|
|
int retval;
|
2008-02-25 11:48:04 -06:00
|
|
|
char **cfg;
|
|
|
|
|
2010-01-05 15:32:39 -06:00
|
|
|
if (!config_file_names) {
|
|
|
|
command_run_line(cmd_ctx, "script openocd.cfg");
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2008-02-25 11:48:04 -06:00
|
|
|
|
|
|
|
cfg = config_file_names;
|
|
|
|
|
2012-01-30 08:31:21 -06:00
|
|
|
while (*cfg) {
|
2009-06-23 17:42:54 -05:00
|
|
|
retval = command_run_line(cmd_ctx, *cfg);
|
2009-06-23 17:38:12 -05:00
|
|
|
if (retval != ERROR_OK)
|
2008-07-11 10:07:58 -05:00
|
|
|
return retval;
|
2008-02-25 11:48:04 -06:00
|
|
|
cfg++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2013-03-27 11:28:31 -05:00
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char *get_home_dir(const char *append_path)
|
|
|
|
{
|
|
|
|
char *home = getenv("HOME");
|
|
|
|
|
|
|
|
if (home == NULL) {
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
home = getenv("USERPROFILE");
|
|
|
|
|
|
|
|
if (home == NULL) {
|
|
|
|
|
|
|
|
char homepath[MAX_PATH];
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if (home == NULL)
|
|
|
|
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;
|
|
|
|
}
|