move options handling to separate file to better support embedded implementations...
[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 int configuration_output_handler(struct command_context_s *context, char* line)\r
40 {\r
41         INFO(line);\r
42         \r
43         return ERROR_OK;\r
44 }\r
45 \r
46 void add_script_search_dir (const char *dir)\r
47 {\r
48         num_script_dirs++;\r
49         script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));\r
50 \r
51         script_search_dirs[num_script_dirs-1] = strdup(dir);\r
52         script_search_dirs[num_script_dirs] = NULL;\r
53 }\r
54 \r
55 void add_config_file_name (const char *cfg)\r
56 {\r
57         num_config_files++;\r
58         config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));\r
59 \r
60         config_file_names[num_config_files-1] = strdup(cfg);\r
61         config_file_names[num_config_files] = NULL;\r
62 }\r
63 \r
64 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)\r
65 {\r
66         FILE *fp = NULL;\r
67         char **search_dirs = script_search_dirs;\r
68         char *dir;\r
69         char full_path[1024];\r
70 \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
75 \r
76         while (!fp)\r
77         {\r
78                 dir = *search_dirs++;\r
79 \r
80                 if (!dir)\r
81                         break;\r
82 \r
83                 snprintf(full_path, 1024, "%s/%s", dir, file);\r
84                 fp = fopen(full_path, mode);\r
85         }\r
86 \r
87         if (fp)\r
88                 command_print(cmd_ctx, "opened %s", full_path);\r
89 \r
90         return fp;\r
91 }\r
92 \r
93 int parse_config_file(struct command_context_s *cmd_ctx)\r
94 {\r
95         char **cfg;\r
96         FILE *config_file;\r
97 \r
98         if (!config_file_names)\r
99                 add_config_file_name ("script openocd.cfg");\r
100 \r
101         cfg = config_file_names;\r
102 \r
103         while (*cfg)\r
104         {\r
105                 command_run_line(cmd_ctx, *cfg);\r
106                 cfg++;\r
107         }\r
108 \r
109         return ERROR_OK;\r
110 }\r