target/arm_adi_v5: fix sync CSW cache on apreg write
[openocd.git] / src / helper / configuration.c
blob114ad2c6c90e6f47ee591d61becdbd92a15304d8
1 /***************************************************************************
2 * Copyright (C) 2004, 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
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. *
12 * *
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. *
17 * *
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 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include "configuration.h"
26 #include "log.h"
28 static size_t num_config_files;
29 static char **config_file_names;
31 static size_t num_script_dirs;
32 static char **script_search_dirs;
34 void add_script_search_dir(const char *dir)
36 num_script_dirs++;
37 script_search_dirs = realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
39 script_search_dirs[num_script_dirs-1] = strdup(dir);
40 script_search_dirs[num_script_dirs] = NULL;
42 LOG_DEBUG("adding %s", dir);
45 void add_config_command(const char *cfg)
47 num_config_files++;
48 config_file_names = realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
50 config_file_names[num_config_files-1] = strdup(cfg);
51 config_file_names[num_config_files] = NULL;
54 void free_config(void)
56 while (num_config_files)
57 free(config_file_names[--num_config_files]);
59 free(config_file_names);
60 config_file_names = NULL;
62 while (num_script_dirs)
63 free(script_search_dirs[--num_script_dirs]);
65 free(script_search_dirs);
66 script_search_dirs = NULL;
69 /* return full path or NULL according to search rules */
70 char *find_file(const char *file)
72 FILE *fp = NULL;
73 char **search_dirs = script_search_dirs;
74 char *dir;
75 char const *mode = "r";
76 char *full_path;
78 /* Check absolute and relative to current working dir first.
79 * This keeps full_path reporting belowing working. */
80 full_path = alloc_printf("%s", file);
81 fp = fopen(full_path, mode);
83 while (!fp) {
84 free(full_path);
85 full_path = NULL;
86 dir = *search_dirs++;
88 if (!dir)
89 break;
91 full_path = alloc_printf("%s/%s", dir, file);
92 fp = fopen(full_path, mode);
95 if (fp) {
96 fclose(fp);
97 LOG_DEBUG("found %s", full_path);
98 return full_path;
101 free(full_path);
103 return NULL;
106 FILE *open_file_from_path(const char *file, const char *mode)
108 if (mode[0] != 'r')
109 return fopen(file, mode);
110 else {
111 char *full_path = find_file(file);
112 if (full_path == NULL)
113 return NULL;
114 FILE *fp = NULL;
115 fp = fopen(full_path, mode);
116 free(full_path);
117 return fp;
121 int parse_config_file(struct command_context *cmd_ctx)
123 int retval;
124 char **cfg;
126 if (!config_file_names) {
127 command_run_line(cmd_ctx, "script openocd.cfg");
128 return ERROR_OK;
131 cfg = config_file_names;
133 while (*cfg) {
134 retval = command_run_line(cmd_ctx, *cfg);
135 if (retval != ERROR_OK)
136 return retval;
137 cfg++;
140 return ERROR_OK;
143 #ifndef _WIN32
144 #include <pwd.h>
145 #endif
147 char *get_home_dir(const char *append_path)
149 char *home = getenv("HOME");
151 if (home == NULL) {
153 #ifdef _WIN32
154 home = getenv("USERPROFILE");
156 if (home == NULL) {
158 char homepath[MAX_PATH];
159 char *drive = getenv("HOMEDRIVE");
160 char *path = getenv("HOMEPATH");
161 if (drive && path) {
162 snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
163 home = homepath;
166 #else
167 struct passwd *pwd = getpwuid(getuid());
168 if (pwd)
169 home = pwd->pw_dir;
171 #endif
174 if (home == NULL)
175 return home;
177 char *home_path;
179 if (append_path)
180 home_path = alloc_printf("%s/%s", home, append_path);
181 else
182 home_path = alloc_printf("%s", home);
184 return home_path;