- added -c option that will execute an openocd command
[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 <getopt.h>\r
32 #include <string.h>\r
33 \r
34 static size_t num_config_files;\r
35 static char** config_file_names;\r
36 \r
37 static size_t num_script_dirs;\r
38 static char** script_search_dirs;\r
39 \r
40 static int help_flag;\r
41 \r
42 static struct option long_options[] =\r
43 {\r
44         {"help",                        no_argument,    &help_flag, 1},\r
45 \r
46         {"debug",                       optional_argument,      0, 'd'},\r
47         {"file",                        required_argument,      0, 'f'},\r
48         {"search",                      required_argument,      0, 's'},\r
49         {"log_output",          required_argument,      0, 'l'},\r
50         {"command",                     required_argument,      0, 'c'},\r
51         \r
52         {0, 0, 0, 0}\r
53 };\r
54 \r
55 int configuration_output_handler(struct command_context_s *context, char* line)\r
56 {\r
57         INFO(line);\r
58         \r
59         return ERROR_OK;\r
60 }\r
61 \r
62 void add_script_search_dir (const char *dir)\r
63 {\r
64         num_script_dirs++;\r
65         script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));\r
66 \r
67         script_search_dirs[num_script_dirs-1] = strdup(dir);\r
68         script_search_dirs[num_script_dirs] = NULL;\r
69 }\r
70 \r
71 void add_config_file_name (const char *cfg)\r
72 {\r
73         num_config_files++;\r
74         config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));\r
75 \r
76         config_file_names[num_config_files-1] = strdup(cfg);\r
77         config_file_names[num_config_files] = NULL;\r
78 }\r
79 \r
80 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])\r
81 {\r
82         int c;\r
83         char command_buffer[128];\r
84 \r
85         while (1)\r
86         {       \r
87                 /* getopt_long stores the option index here. */\r
88                 int option_index = 0;\r
89                 \r
90                 c = getopt_long(argc, argv, "hd::l:f:s:c:", long_options, &option_index);\r
91                 \r
92                 /* Detect the end of the options. */\r
93                 if (c == -1)\r
94                         break;\r
95                 \r
96                 switch (c)\r
97                 {\r
98                         case 0:\r
99                                 break;\r
100                         case 'h':       /* --help | -h */\r
101                                 help_flag = 1;\r
102                                 break;\r
103                         case 'f':       /* --file | -f */\r
104                                 snprintf(command_buffer, 128, "script %s", optarg);\r
105                                 add_config_file_name(command_buffer);\r
106                                 break;\r
107                         case 's':       /* --search | -s */\r
108                                 add_script_search_dir(optarg);\r
109                                 break;\r
110                         case 'd':       /* --debug | -d */\r
111                                 if (optarg)\r
112                                         snprintf(command_buffer, 128, "debug_level %s", optarg);\r
113                                 else\r
114                                         snprintf(command_buffer, 128, "debug_level 3");\r
115                                 command_run_line(cmd_ctx, command_buffer);\r
116                                 break;\r
117                         case 'l':       /* --log_output | -l */\r
118                                 if (optarg)\r
119                                 {\r
120                                         snprintf(command_buffer, 128, "log_output %s", optarg);\r
121                                         command_run_line(cmd_ctx, command_buffer);\r
122                                 }       \r
123                                 break;\r
124                         case 'c':       /* --command | -c */\r
125                                 if (optarg)\r
126                                 {\r
127                                         add_config_file_name(optarg);\r
128                                 }       \r
129                                 break;\r
130                                 \r
131                 }\r
132         }\r
133 \r
134         if (help_flag)\r
135         {\r
136                 printf("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");\r
137                 printf("--help       | -h\tdisplay this help\n");\r
138                 printf("--file       | -f\tuse configuration file <name>\n");\r
139                 printf("--search     | -s\tdir to search for config files and scripts.\n");\r
140                 printf("--debug      | -d\tset debug level <0-3>\n");\r
141                 printf("--log_output | -l\tredirect log output to file <name>\n");\r
142                 printf("--command    | -c\trun <command>\n");\r
143                 exit(-1);\r
144         }       \r
145 \r
146         /* Add dir for openocd supplied scripts last so that user can over\r
147            ride those scripts if desired. */\r
148         add_script_search_dir(PKGDATADIR);\r
149         add_script_search_dir(PKGLIBDIR);\r
150 \r
151         return ERROR_OK;\r
152 }\r
153 \r
154 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)\r
155 {\r
156         FILE *fp = NULL;\r
157         char **search_dirs = script_search_dirs;\r
158         char *dir;\r
159         char full_path[1024];\r
160 \r
161         /* Check absolute and relative to current working dir first.\r
162          * This keeps full_path reporting belowing working. */\r
163         snprintf(full_path, 1024, "%s", file);\r
164         fp = fopen(full_path, mode);\r
165 \r
166         while (!fp)\r
167         {\r
168                 dir = *search_dirs++;\r
169 \r
170                 if (!dir)\r
171                         break;\r
172 \r
173                 snprintf(full_path, 1024, "%s/%s", dir, file);\r
174                 fp = fopen(full_path, mode);\r
175         }\r
176 \r
177         if (fp)\r
178                 command_print(cmd_ctx, "opened %s", full_path);\r
179 \r
180         return fp;\r
181 }\r
182 \r
183 int parse_config_file(struct command_context_s *cmd_ctx)\r
184 {\r
185         char **cfg;\r
186         FILE *config_file;\r
187 \r
188         if (!config_file_names)\r
189                 add_config_file_name ("script openocd.cfg");\r
190 \r
191         cfg = config_file_names;\r
192 \r
193         while (*cfg)\r
194         {\r
195                 command_run_line(cmd_ctx, *cfg);\r
196                 cfg++;\r
197         }\r
198 \r
199         return ERROR_OK;\r
200 }\r