Zach Welch <zw@superlucidity.net> fix -Wformat-security warnings (1 of 4)
[fw/openocd] / src / helper / options.c
1 /***************************************************************************
2  *   Copyright (C) 2004, 2005 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "replacements.h"
28
29 #include "types.h"
30 #include "command.h"
31 #include "configuration.h"
32 #include "log.h"
33 #include "server.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <getopt.h>
38 #include <string.h>
39
40 static int help_flag, version_flag;
41
42 static struct option long_options[] =
43 {
44         {"help",        no_argument,            &help_flag,     1},
45         {"version",     no_argument,            &version_flag,  1},
46         {"debug",       optional_argument,      0,              'd'},
47         {"file",        required_argument,      0,              'f'},
48         {"search",      required_argument,      0,              's'},
49         {"log_output",  required_argument,      0,      'l'},
50         {"command",     required_argument,      0,              'c'},
51         {"pipe",        no_argument,            0,              'p'},
52         {0, 0, 0, 0}
53 };
54
55 int configuration_output_handler(struct command_context_s *context, const char* line)
56 {
57         LOG_USER_N("%s", line);
58
59         return ERROR_OK;
60 }
61
62 int add_default_dirs(void)
63 {
64 #ifdef _WIN32
65         /* Add the parent of the directory where openocd.exe resides to the
66          * config script search path.
67          * Directory layout: 
68          * bin\openocd.exe
69          * lib\openocd
70          * event\at91eb40a_reset.cfg
71          * target\at91eb40a.cfg
72          */
73         {
74                 char strExePath [MAX_PATH];
75                 GetModuleFileName (NULL, strExePath, MAX_PATH);
76                 /* Either this code will *always* work or it will SEGFAULT giving
77                  * excellent information on the culprit. 
78                  */
79                 *strrchr(strExePath, '\\')=0;
80                 strcat(strExePath, "\\..");
81                 add_script_search_dir(strExePath);
82         }
83         /*
84          * Add support for the default (as of 20080121) layout when
85          * using autotools and cygwin to build native MinGW binary.
86          * Path separator is converted to UNIX style so that MinGW is
87          * pleased.
88          *
89          * bin/openocd.exe
90          * lib/openocd/event/at91eb40a_reset.cfg
91          * lib/openocd/target/at91eb40a.cfg
92          */
93         {
94                 char strExePath [MAX_PATH];
95                 char *p;
96                 GetModuleFileName (NULL, strExePath, MAX_PATH);
97                 *strrchr(strExePath, '\\')=0;
98                 strcat(strExePath, "/../lib/"PACKAGE);
99                 for(p=strExePath; *p; p++) {
100                         if(*p == '\\')
101                                 *p = '/';
102                 }
103                 add_script_search_dir(strExePath);
104         }
105 #else
106         /* Add dir for openocd supplied scripts last so that user can over
107            ride those scripts if desired. */
108         add_script_search_dir(PKGDATADIR);
109         add_script_search_dir(PKGLIBDIR);
110 #endif
111         return ERROR_OK;
112 }
113
114 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
115 {
116         int c;
117         char command_buffer[128];
118
119         while (1)
120         {       
121                 /* getopt_long stores the option index here. */
122                 int option_index = 0;
123                 
124                 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
125                 
126                 /* Detect the end of the options. */
127                 if (c == -1)
128                         break;
129                 
130                 switch (c)
131                 {
132                         case 0:
133                                 break;
134                         case 'h':       /* --help | -h */
135                                 help_flag = 1;
136                                 break;
137                         case 'v':       /* --version | -v */
138                                 version_flag = 1;
139                                 break;
140                         case 'f':       /* --file | -f */
141                         {
142                                 snprintf(command_buffer, 128, "script {%s}", optarg);
143                                 add_config_command(command_buffer);
144                                 break;
145                         }
146                         case 's':       /* --search | -s */
147                                 add_script_search_dir(optarg);
148                                 break;
149                         case 'd':       /* --debug | -d */
150                                 if (optarg)
151                                         snprintf(command_buffer, 128, "debug_level %s", optarg);
152                                 else
153                                         snprintf(command_buffer, 128, "debug_level 3");
154                                 command_run_line(cmd_ctx, command_buffer);
155                                 break;
156                         case 'l':       /* --log_output | -l */
157                                 if (optarg)
158                                 {
159                                         snprintf(command_buffer, 128, "log_output %s", optarg);
160                                         command_run_line(cmd_ctx, command_buffer);
161                                 }       
162                                 break;
163                         case 'c':       /* --command | -c */
164                                 if (optarg)
165                                 {
166                                         add_config_command(optarg);
167                                 }       
168                                 break;
169                         case 'p':       /* --pipe | -p */
170 #if BUILD_ECOSBOARD == 1
171                                 /* pipes unsupported on hosted platforms */
172                                 LOG_WARNING("pipes not supported on this platform");
173 #else
174                                 server_use_pipes = 1;
175 #endif
176                                 break;
177                 }
178         }
179
180         if (help_flag)
181         {
182                 LOG_OUTPUT("Open On-Chip Debugger\n(c) 2005-2008 by Dominic Rath\n\n");
183                 LOG_OUTPUT("--help       | -h\tdisplay this help\n");
184                 LOG_OUTPUT("--version    | -v\tdisplay OpenOCD version\n");
185                 LOG_OUTPUT("--file       | -f\tuse configuration file <name>\n");
186                 LOG_OUTPUT("--search     | -s\tdir to search for config files and scripts\n");
187                 LOG_OUTPUT("--debug      | -d\tset debug level <0-3>\n");
188                 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
189                 LOG_OUTPUT("--command    | -c\trun <command>\n");
190                 LOG_OUTPUT("--pipe       | -p\tuse pipes for gdb communication\n");
191                 exit(-1);
192         }       
193
194         if (version_flag)
195         {
196                 /* Nothing to do, version gets printed automatically. */
197                 // It is not an error to request the VERSION number.
198                 exit(0);
199         }
200         
201         return ERROR_OK;
202 }