- using ERROR_COMMAND_SYNTAX_ERROR to print syntax in a couple of places
[fw/openocd] / src / helper / configuration.c
1 /***************************************************************************\r
2  *   Copyright (C) 2004, 2005 by Dominic Rath                              *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\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
9  *                                                                         *\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
14  *                                                                         *\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
21 #include "config.h"\r
22 #endif\r
23 \r
24 #include "types.h"\r
25 #include "command.h"\r
26 #include "configuration.h"\r
27 #include "log.h"\r
28 \r
29 #include <stdio.h>\r
30 #include <stdlib.h>\r
31 #include <string.h>\r
32 \r
33 static size_t num_config_files;\r
34 static char** config_file_names;\r
35 \r
36 static size_t num_script_dirs;\r
37 static char** script_search_dirs;\r
38 \r
39 \r
40 void add_script_search_dir (const char *dir)\r
41 {\r
42         num_script_dirs++;\r
43         script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));\r
44 \r
45         script_search_dirs[num_script_dirs-1] = strdup(dir);\r
46         script_search_dirs[num_script_dirs] = NULL;\r
47 }\r
48 \r
49 void add_config_file_name (const char *cfg)\r
50 {\r
51         num_config_files++;\r
52         config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));\r
53 \r
54         config_file_names[num_config_files-1] = strdup(cfg);\r
55         config_file_names[num_config_files] = NULL;\r
56 }\r
57 \r
58 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)\r
59 {\r
60         FILE *fp = NULL;\r
61         char **search_dirs = script_search_dirs;\r
62         char *dir;\r
63         char full_path[1024];\r
64 \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
69 \r
70         while (!fp)\r
71         {\r
72                 dir = *search_dirs++;\r
73 \r
74                 if (!dir)\r
75                         break;\r
76 \r
77                 snprintf(full_path, 1024, "%s/%s", dir, file);\r
78                 fp = fopen(full_path, mode);\r
79         }\r
80 \r
81         if (fp)\r
82                 command_print(cmd_ctx, "opened %s", full_path);\r
83 \r
84         return fp;\r
85 }\r
86 \r
87 int parse_config_file(struct command_context_s *cmd_ctx)\r
88 {\r
89         char **cfg;\r
90         FILE *config_file;\r
91 \r
92         if (!config_file_names)\r
93                 add_config_file_name ("script openocd.cfg");\r
94 \r
95         cfg = config_file_names;\r
96 \r
97         while (*cfg)\r
98         {\r
99                 command_run_line(cmd_ctx, *cfg);\r
100                 cfg++;\r
101         }\r
102 \r
103         return ERROR_OK;\r
104 }\r