]> git.gag.com Git - fw/openocd/blob - src/helper/configuration.c
checkpatch: adapt shell script to the tool's new version
[fw/openocd] / src / helper / configuration.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Copyright (C) 2004, 2005 by Dominic Rath                              *
5  *   Dominic.Rath@gmx.de                                                   *
6  *                                                                         *
7  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
8  *   oyvind.harboe@zylin.com                                               *
9  ***************************************************************************/
10
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include "configuration.h"
16 #include "log.h"
17 #include "replacements.h"
18
19 static size_t num_config_files;
20 static char **config_file_names;
21
22 static size_t num_script_dirs;
23 static char **script_search_dirs;
24
25 void add_script_search_dir(const char *dir)
26 {
27         num_script_dirs++;
28         script_search_dirs = realloc(script_search_dirs, (num_script_dirs + 1) * sizeof(char *));
29
30         script_search_dirs[num_script_dirs-1] = strdup(dir);
31         script_search_dirs[num_script_dirs] = NULL;
32
33         LOG_DEBUG("adding %s", dir);
34 }
35
36 void add_config_command(const char *cfg)
37 {
38         num_config_files++;
39         config_file_names = realloc(config_file_names, (num_config_files + 1) * sizeof(char *));
40
41         config_file_names[num_config_files-1] = strdup(cfg);
42         config_file_names[num_config_files] = NULL;
43 }
44
45 void free_config(void)
46 {
47         while (num_config_files)
48                 free(config_file_names[--num_config_files]);
49
50         free(config_file_names);
51         config_file_names = NULL;
52
53         while (num_script_dirs)
54                 free(script_search_dirs[--num_script_dirs]);
55
56         free(script_search_dirs);
57         script_search_dirs = NULL;
58 }
59
60 /* return full path or NULL according to search rules */
61 char *find_file(const char *file)
62 {
63         FILE *fp = NULL;
64         char **search_dirs = script_search_dirs;
65         char *dir;
66         char const *mode = "r";
67         char *full_path;
68
69         /* Check absolute and relative to current working dir first.
70          * This keeps full_path reporting belowing working. */
71         full_path = alloc_printf("%s", file);
72         fp = fopen(full_path, mode);
73
74         while (!fp) {
75                 free(full_path);
76                 full_path = NULL;
77                 dir = *search_dirs++;
78
79                 if (!dir)
80                         break;
81
82                 full_path = alloc_printf("%s/%s", dir, file);
83                 fp = fopen(full_path, mode);
84         }
85
86         if (fp) {
87                 fclose(fp);
88                 LOG_DEBUG("found %s", full_path);
89                 return full_path;
90         }
91
92         free(full_path);
93
94         return NULL;
95 }
96
97 FILE *open_file_from_path(const char *file, const char *mode)
98 {
99         if (mode[0] != 'r')
100                 return fopen(file, mode);
101         else {
102                 char *full_path = find_file(file);
103                 if (!full_path)
104                         return NULL;
105                 FILE *fp = NULL;
106                 fp = fopen(full_path, mode);
107                 free(full_path);
108                 return fp;
109         }
110 }
111
112 int parse_config_file(struct command_context *cmd_ctx)
113 {
114         int retval;
115         char **cfg;
116
117         if (!config_file_names) {
118                 command_run_line(cmd_ctx, "script openocd.cfg");
119                 return ERROR_OK;
120         }
121
122         cfg = config_file_names;
123
124         while (*cfg) {
125                 retval = command_run_line(cmd_ctx, *cfg);
126                 if (retval != ERROR_OK)
127                         return retval;
128                 cfg++;
129         }
130
131         return ERROR_OK;
132 }
133
134 #ifndef _WIN32
135 #include <pwd.h>
136 #endif
137
138 char *get_home_dir(const char *append_path)
139 {
140 #ifdef _WIN32
141         char homepath[MAX_PATH];
142 #endif
143
144         char *home = getenv("HOME");
145
146         if (!home) {
147
148 #ifdef _WIN32
149                 home = getenv("USERPROFILE");
150
151                 if (!home) {
152                         char *drive = getenv("HOMEDRIVE");
153                         char *path = getenv("HOMEPATH");
154                         if (drive && path) {
155                                 snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
156                                 home = homepath;
157                         }
158                 }
159 #else
160                 struct passwd *pwd = getpwuid(getuid());
161                 if (pwd)
162                         home = pwd->pw_dir;
163
164 #endif
165         }
166
167         if (!home)
168                 return home;
169
170         char *home_path;
171
172         if (append_path)
173                 home_path = alloc_printf("%s/%s", home, append_path);
174         else
175                 home_path = alloc_printf("%s", home);
176
177         return home_path;
178 }