31a618c665efa5cc8c8a4462fd381e8bec7febec
[openocd.git] / src / helper / configuration.c
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, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "configuration.h"
28 #include "log.h"
29
30 static size_t num_config_files;
31 static char **config_file_names;
32
33 static size_t num_script_dirs;
34 static char **script_search_dirs;
35
36 void add_script_search_dir(const char *dir)
37 {
38 num_script_dirs++;
39 script_search_dirs =
40 (char **)realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
41
42 script_search_dirs[num_script_dirs-1] = strdup(dir);
43 script_search_dirs[num_script_dirs] = NULL;
44
45 LOG_DEBUG("adding %s", dir);
46 }
47
48 void add_config_command(const char *cfg)
49 {
50 num_config_files++;
51 config_file_names =
52 (char **)realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
53
54 config_file_names[num_config_files-1] = strdup(cfg);
55 config_file_names[num_config_files] = NULL;
56 }
57
58 /* return full path or NULL according to search rules */
59 char *find_file(const char *file)
60 {
61 FILE *fp = NULL;
62 char **search_dirs = script_search_dirs;
63 char *dir;
64 char const *mode = "r";
65 char *full_path;
66
67 /* Check absolute and relative to current working dir first.
68 * This keeps full_path reporting belowing working. */
69 full_path = alloc_printf("%s", file);
70 fp = fopen(full_path, mode);
71
72 while (!fp) {
73 free(full_path);
74 full_path = NULL;
75 dir = *search_dirs++;
76
77 if (!dir)
78 break;
79
80 full_path = alloc_printf("%s/%s", dir, file);
81 fp = fopen(full_path, mode);
82 }
83
84 if (fp) {
85 fclose(fp);
86 LOG_DEBUG("found %s", full_path);
87 return full_path;
88 }
89
90 free(full_path);
91
92 return NULL;
93 }
94
95 FILE *open_file_from_path(const char *file, const char *mode)
96 {
97 if (mode[0] != 'r')
98 return fopen(file, mode);
99 else {
100 char *full_path = find_file(file);
101 if (full_path == NULL)
102 return NULL;
103 FILE *fp = NULL;
104 fp = fopen(full_path, mode);
105 free(full_path);
106 return fp;
107 }
108 }
109
110 int parse_config_file(struct command_context *cmd_ctx)
111 {
112 int retval;
113 char **cfg;
114
115 if (!config_file_names) {
116 command_run_line(cmd_ctx, "script openocd.cfg");
117 return ERROR_OK;
118 }
119
120 cfg = config_file_names;
121
122 while (*cfg) {
123 retval = command_run_line(cmd_ctx, *cfg);
124 if (retval != ERROR_OK)
125 return retval;
126 cfg++;
127 }
128
129 return ERROR_OK;
130 }
131
132 #ifndef _WIN32
133 #include <pwd.h>
134 #endif
135
136 char *get_home_dir(const char *append_path)
137 {
138 char *home = getenv("HOME");
139
140 if (home == NULL) {
141
142 #ifdef _WIN32
143 home = getenv("USERPROFILE");
144
145 if (home == NULL) {
146
147 char homepath[MAX_PATH];
148 char *drive = getenv("HOMEDRIVE");
149 char *path = getenv("HOMEPATH");
150 if (drive && path) {
151 snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
152 home = homepath;
153 }
154 }
155 #else
156 struct passwd *pwd = getpwuid(getuid());
157 if (pwd)
158 home = pwd->pw_dir;
159
160 #endif
161 }
162
163 if (home == NULL)
164 return home;
165
166 char *home_path;
167
168 if (append_path)
169 home_path = alloc_printf("%s/%s", home, append_path);
170 else
171 home_path = alloc_printf("%s", home);
172
173 return home_path;
174 }