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