1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
20 ***************************************************************************/
26 #include "configuration.h"
28 #include "replacements.h"
30 static size_t num_config_files;
31 static char **config_file_names;
33 static size_t num_script_dirs;
34 static char **script_search_dirs;
36 void add_script_search_dir(const char *dir)
39 script_search_dirs = realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
41 script_search_dirs[num_script_dirs-1] = strdup(dir);
42 script_search_dirs[num_script_dirs] = NULL;
44 LOG_DEBUG("adding %s", dir);
47 void add_config_command(const char *cfg)
50 config_file_names = realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
52 config_file_names[num_config_files-1] = strdup(cfg);
53 config_file_names[num_config_files] = NULL;
56 void free_config(void)
58 while (num_config_files)
59 free(config_file_names[--num_config_files]);
61 free(config_file_names);
62 config_file_names = NULL;
64 while (num_script_dirs)
65 free(script_search_dirs[--num_script_dirs]);
67 free(script_search_dirs);
68 script_search_dirs = NULL;
71 /* return full path or NULL according to search rules */
72 char *find_file(const char *file)
75 char **search_dirs = script_search_dirs;
77 char const *mode = "r";
80 /* Check absolute and relative to current working dir first.
81 * This keeps full_path reporting belowing working. */
82 full_path = alloc_printf("%s", file);
83 fp = fopen(full_path, mode);
93 full_path = alloc_printf("%s/%s", dir, file);
94 fp = fopen(full_path, mode);
99 LOG_DEBUG("found %s", full_path);
108 FILE *open_file_from_path(const char *file, const char *mode)
111 return fopen(file, mode);
113 char *full_path = find_file(file);
117 fp = fopen(full_path, mode);
123 int parse_config_file(struct command_context *cmd_ctx)
128 if (!config_file_names) {
129 command_run_line(cmd_ctx, "script openocd.cfg");
133 cfg = config_file_names;
136 retval = command_run_line(cmd_ctx, *cfg);
137 if (retval != ERROR_OK)
149 char *get_home_dir(const char *append_path)
151 char *home = getenv("HOME");
156 home = getenv("USERPROFILE");
160 char homepath[MAX_PATH];
161 char *drive = getenv("HOMEDRIVE");
162 char *path = getenv("HOMEPATH");
164 snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
169 struct passwd *pwd = getpwuid(getuid());
182 home_path = alloc_printf("%s/%s", home, append_path);
184 home_path = alloc_printf("%s", home);