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
34 static size_t num_config_files;
\r
35 static char** config_file_names;
\r
37 static size_t num_script_dirs;
\r
38 static char** script_search_dirs;
\r
40 static int help_flag;
\r
42 static struct option long_options[] =
\r
44 {"help", no_argument, &help_flag, 1},
\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
55 int configuration_output_handler(struct command_context_s *context, char* line)
\r
62 void add_script_search_dir (const char *dir)
\r
65 script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
\r
67 script_search_dirs[num_script_dirs-1] = strdup(dir);
\r
68 script_search_dirs[num_script_dirs] = NULL;
\r
71 void add_config_file_name (const char *cfg)
\r
74 config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
\r
76 config_file_names[num_config_files-1] = strdup(cfg);
\r
77 config_file_names[num_config_files] = NULL;
\r
80 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
\r
83 char command_buffer[128];
\r
87 /* getopt_long stores the option index here. */
\r
88 int option_index = 0;
\r
90 c = getopt_long(argc, argv, "hd::l:f:s:c:", long_options, &option_index);
\r
92 /* Detect the end of the options. */
\r
100 case 'h': /* --help | -h */
\r
103 case 'f': /* --file | -f */
\r
104 snprintf(command_buffer, 128, "script %s", optarg);
\r
105 add_config_file_name(command_buffer);
\r
107 case 's': /* --search | -s */
\r
108 add_script_search_dir(optarg);
\r
110 case 'd': /* --debug | -d */
\r
112 snprintf(command_buffer, 128, "debug_level %s", optarg);
\r
114 snprintf(command_buffer, 128, "debug_level 3");
\r
115 command_run_line(cmd_ctx, command_buffer);
\r
117 case 'l': /* --log_output | -l */
\r
120 snprintf(command_buffer, 128, "log_output %s", optarg);
\r
121 command_run_line(cmd_ctx, command_buffer);
\r
124 case 'c': /* --command | -c */
\r
127 add_config_file_name(optarg);
\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
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
154 FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)
\r
157 char **search_dirs = script_search_dirs;
\r
159 char full_path[1024];
\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
168 dir = *search_dirs++;
\r
173 snprintf(full_path, 1024, "%s/%s", dir, file);
\r
174 fp = fopen(full_path, mode);
\r
178 command_print(cmd_ctx, "opened %s", full_path);
\r
183 int parse_config_file(struct command_context_s *cmd_ctx)
\r
188 if (!config_file_names)
\r
189 add_config_file_name ("script openocd.cfg");
\r
191 cfg = config_file_names;
\r
195 command_run_line(cmd_ctx, *cfg);
\r