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
40 void add_script_search_dir (const char *dir)
\r
43 script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
\r
45 script_search_dirs[num_script_dirs-1] = strdup(dir);
\r
46 script_search_dirs[num_script_dirs] = NULL;
\r
49 void add_config_file_name (const char *cfg)
\r
52 config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
\r
54 config_file_names[num_config_files-1] = strdup(cfg);
\r
55 config_file_names[num_config_files] = NULL;
\r
58 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)
\r
61 char **search_dirs = script_search_dirs;
\r
63 char full_path[1024];
\r
65 /* Check absolute and relative to current working dir first.
\r
66 * This keeps full_path reporting belowing working. */
\r
67 snprintf(full_path, 1024, "%s", file);
\r
68 fp = fopen(full_path, mode);
\r
72 dir = *search_dirs++;
\r
77 snprintf(full_path, 1024, "%s/%s", dir, file);
\r
78 fp = fopen(full_path, mode);
\r
82 command_print(cmd_ctx, "opened %s", full_path);
\r
87 int parse_config_file(struct command_context_s *cmd_ctx)
\r
92 if (!config_file_names)
\r
93 add_config_file_name ("script openocd.cfg");
\r
95 cfg = config_file_names;
\r
99 command_run_line(cmd_ctx, *cfg);
\r