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  if (!file || strlen(file) == 0)
70  return NULL;
71 
72  /* Check absolute and relative to current working dir first.
73  * This keeps full_path reporting belowing working. */
74  full_path = alloc_printf("%s", file);
75  fp = fopen(full_path, mode);
76 
78  while (!fp) {
79  free(full_path);
80  full_path = NULL;
81  dir = *search_dirs++;
82 
83  if (!dir)
84  break;
85 
86  full_path = alloc_printf("%s/%s", dir, file);
87  fp = fopen(full_path, mode);
88  }
89 
90  if (fp) {
91  fclose(fp);
92  LOG_DEBUG("found %s", full_path);
93  return full_path;
94  }
95 
96  free(full_path);
97 
98  return NULL;
99 }
100 
101 FILE *open_file_from_path(const char *file, const char *mode)
102 {
103  if (mode[0] != 'r')
104  return fopen(file, mode);
105  else {
106  char *full_path = find_file(file);
107  if (!full_path)
108  return NULL;
109  FILE *fp = NULL;
110  fp = fopen(full_path, mode);
111  free(full_path);
112  return fp;
113  }
114 }
115 
116 int parse_config_file(struct command_context *cmd_ctx)
117 {
118  int retval;
119  char **cfg;
120 
121  if (!config_file_names) {
122  command_run_line(cmd_ctx, "script openocd.cfg");
123  return ERROR_OK;
124  }
125 
126  cfg = config_file_names;
127 
128  while (*cfg) {
129  retval = command_run_line(cmd_ctx, *cfg);
130  if (retval != ERROR_OK)
131  return retval;
132  cfg++;
133  }
134 
135  return ERROR_OK;
136 }
137 
138 #ifndef _WIN32
139 #include <pwd.h>
140 #endif
141 
142 char *get_home_dir(const char *append_path)
143 {
144 #ifdef _WIN32
145  char homepath[MAX_PATH];
146 #endif
147 
148  char *home = getenv("HOME");
149 
150  if (!home) {
151 
152 #ifdef _WIN32
153  home = getenv("USERPROFILE");
154 
155  if (!home) {
156  char *drive = getenv("HOMEDRIVE");
157  char *path = getenv("HOMEPATH");
158  if (drive && path) {
159  snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
160  home = homepath;
161  }
162  }
163 #else
164  struct passwd *pwd = getpwuid(getuid());
165  if (pwd)
166  home = pwd->pw_dir;
167 
168 #endif
169  }
170 
171  if (!home)
172  return home;
173 
174  char *home_path;
175 
176  if (append_path)
177  home_path = alloc_printf("%s/%s", home, append_path);
178  else
179  home_path = alloc_printf("%s", home);
180 
181  return home_path;
182 }
enum arm_mode mode
Definition: armv4_5.c:280
int command_run_line(struct command_context *context, char *line)
Definition: command.c:497
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)
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:386
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define fp
Definition: mips32.c:223
#define NULL
Definition: usb.h:16