helper/configuration: free script_search_dirs and config_file_names
[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, 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 void free_config(void)
55 {
56 while (num_config_files)
57 free(config_file_names[--num_config_files]);
58
59 free(config_file_names);
60 config_file_names = NULL;
61
62 while (num_script_dirs)
63 free(script_search_dirs[--num_script_dirs]);
64
65 free(script_search_dirs);
66 script_search_dirs = NULL;
67 }
68
69 /* return full path or NULL according to search rules */
70 char *find_file(const char *file)
71 {
72 FILE *fp = NULL;
73 char **search_dirs = script_search_dirs;
74 char *dir;
75 char const *mode = "r";
76 char *full_path;
77
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);
82
83 while (!fp) {
84 free(full_path);
85 full_path = NULL;
86 dir = *search_dirs++;
87
88 if (!dir)
89 break;
90
91 full_path = alloc_printf("%s/%s", dir, file);
92 fp = fopen(full_path, mode);
93 }
94
95 if (fp) {
96 fclose(fp);
97 LOG_DEBUG("found %s", full_path);
98 return full_path;
99 }
100
101 free(full_path);
102
103 return NULL;
104 }
105
106 FILE *open_file_from_path(const char *file, const char *mode)
107 {
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;
118 }
119 }
120
121 int parse_config_file(struct command_context *cmd_ctx)
122 {
123 int retval;
124 char **cfg;
125
126 if (!config_file_names) {
127 command_run_line(cmd_ctx, "script openocd.cfg");
128 return ERROR_OK;
129 }
130
131 cfg = config_file_names;
132
133 while (*cfg) {
134 retval = command_run_line(cmd_ctx, *cfg);
135 if (retval != ERROR_OK)
136 return retval;
137 cfg++;
138 }
139
140 return ERROR_OK;
141 }
142
143 #ifndef _WIN32
144 #include <pwd.h>
145 #endif
146
147 char *get_home_dir(const char *append_path)
148 {
149 char *home = getenv("HOME");
150
151 if (home == NULL) {
152
153 #ifdef _WIN32
154 home = getenv("USERPROFILE");
155
156 if (home == NULL) {
157
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;
164 }
165 }
166 #else
167 struct passwd *pwd = getpwuid(getuid());
168 if (pwd)
169 home = pwd->pw_dir;
170
171 #endif
172 }
173
174 if (home == NULL)
175 return home;
176
177 char *home_path;
178
179 if (append_path)
180 home_path = alloc_printf("%s/%s", home, append_path);
181 else
182 home_path = alloc_printf("%s", home);
183
184 return home_path;
185 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)