1 /***************************************************************************
\r
2 * Copyright (C) 2004, 2005 by Dominic Rath *
\r
3 * Dominic.Rath@gmx.de *
\r
5 * This program is free software; you can redistribute it and/or modify *
\r
6 * it under the terms of the GNU General Public License as published by *
\r
7 * the Free Software Foundation; either version 2 of the License, or *
\r
8 * (at your option) any later version. *
\r
10 * This program is distributed in the hope that it will be useful, *
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
\r
13 * GNU General Public License for more details. *
\r
15 * You should have received a copy of the GNU General Public License *
\r
16 * along with this program; if not, write to the *
\r
17 * Free Software Foundation, Inc., *
\r
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
\r
19 ***************************************************************************/
\r
20 #ifdef HAVE_CONFIG_H
\r
25 #include "command.h"
\r
26 #include "configuration.h"
\r
33 static size_t num_config_files;
\r
34 static char** config_file_names;
\r
36 static size_t num_script_dirs;
\r
37 static char** script_search_dirs;
\r
39 int configuration_output_handler(struct command_context_s *context, char* line)
\r
46 void add_script_search_dir (const char *dir)
\r
49 script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
\r
51 script_search_dirs[num_script_dirs-1] = strdup(dir);
\r
52 script_search_dirs[num_script_dirs] = NULL;
\r
55 void add_config_file_name (const char *cfg)
\r
58 config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
\r
60 config_file_names[num_config_files-1] = strdup(cfg);
\r
61 config_file_names[num_config_files] = NULL;
\r
64 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)
\r
67 char **search_dirs = script_search_dirs;
\r
69 char full_path[1024];
\r
71 /* Check absolute and relative to current working dir first.
\r
72 * This keeps full_path reporting belowing working. */
\r
73 snprintf(full_path, 1024, "%s", file);
\r
74 fp = fopen(full_path, mode);
\r
78 dir = *search_dirs++;
\r
83 snprintf(full_path, 1024, "%s/%s", dir, file);
\r
84 fp = fopen(full_path, mode);
\r
88 command_print(cmd_ctx, "opened %s", full_path);
\r
93 int parse_config_file(struct command_context_s *cmd_ctx)
\r
98 if (!config_file_names)
\r
99 add_config_file_name ("script openocd.cfg");
\r
101 cfg = config_file_names;
\r
105 command_run_line(cmd_ctx, *cfg);
\r