OpenOCD
configuration.c
Go to the documentation of this file.
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 {
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);
32 
33  LOG_DEBUG("adding %s", dir);
34 }
35 
36 void add_config_command(const char *cfg)
37 {
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);
43 }
44 
45 void free_config(void)
46 {
47  while (num_config_files)
49 
50  free(config_file_names);
52 
53  while (num_script_dirs)
55 
56  free(script_search_dirs);
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 
75  while (!fp) {
76  free(full_path);
77  full_path = NULL;
78  dir = *search_dirs++;
79 
80  if (!dir)
81  break;
82 
83  full_path = alloc_printf("%s/%s", dir, file);
84  fp = fopen(full_path, mode);
85  }
86 
87  if (fp) {
88  fclose(fp);
89  LOG_DEBUG("found %s", full_path);
90  return full_path;
91  }
92 
93  free(full_path);
94 
95  return NULL;
96 }
97 
98 FILE *open_file_from_path(const char *file, const char *mode)
99 {
100  if (mode[0] != 'r')
101  return fopen(file, mode);
102  else {
103  char *full_path = find_file(file);
104  if (!full_path)
105  return NULL;
106  FILE *fp = NULL;
107  fp = fopen(full_path, mode);
108  free(full_path);
109  return fp;
110  }
111 }
112 
113 int parse_config_file(struct command_context *cmd_ctx)
114 {
115  int retval;
116  char **cfg;
117 
118  if (!config_file_names) {
119  command_run_line(cmd_ctx, "script openocd.cfg");
120  return ERROR_OK;
121  }
122 
123  cfg = config_file_names;
124 
125  while (*cfg) {
126  retval = command_run_line(cmd_ctx, *cfg);
127  if (retval != ERROR_OK)
128  return retval;
129  cfg++;
130  }
131 
132  return ERROR_OK;
133 }
134 
135 #ifndef _WIN32
136 #include <pwd.h>
137 #endif
138 
139 char *get_home_dir(const char *append_path)
140 {
141 #ifdef _WIN32
142  char homepath[MAX_PATH];
143 #endif
144 
145  char *home = getenv("HOME");
146 
147  if (!home) {
148 
149 #ifdef _WIN32
150  home = getenv("USERPROFILE");
151 
152  if (!home) {
153  char *drive = getenv("HOMEDRIVE");
154  char *path = getenv("HOMEPATH");
155  if (drive && path) {
156  snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
157  home = homepath;
158  }
159  }
160 #else
161  struct passwd *pwd = getpwuid(getuid());
162  if (pwd)
163  home = pwd->pw_dir;
164 
165 #endif
166  }
167 
168  if (!home)
169  return home;
170 
171  char *home_path;
172 
173  if (append_path)
174  home_path = alloc_printf("%s/%s", home, append_path);
175  else
176  home_path = alloc_printf("%s", home);
177 
178  return home_path;
179 }
enum arm_mode mode
Definition: armv4_5.c:281
int command_run_line(struct command_context *context, char *line)
Definition: command.c:476
static char ** config_file_names
Definition: configuration.c:20
void add_config_command(const char *cfg)
Definition: configuration.c:36
char * find_file(const char *file)
Definition: configuration.c:61
static char ** script_search_dirs
Definition: configuration.c:23
FILE * open_file_from_path(const char *file, const char *mode)
Definition: configuration.c:98
int parse_config_file(struct command_context *cmd_ctx)
static size_t num_config_files
Definition: configuration.c:19
char * get_home_dir(const char *append_path)
void add_script_search_dir(const char *dir)
Definition: configuration.c:25
static size_t num_script_dirs
Definition: configuration.c:22
void free_config(void)
Definition: configuration.c:45
char * alloc_printf(const char *format,...)
Definition: log.c:375
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
#define fp
Definition: mips32.c:223
#define NULL
Definition: usb.h:16