2ac9143e6de1991aab1a8cf5e188c8f59d75ce65
[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(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 #else
84         /* Add dir for openocd supplied scripts last so that user can over
85            ride those scripts if desired. */
86         add_script_search_dir(PKGDATADIR);
87         add_script_search_dir(PKGLIBDIR);
88 #endif
89         return ERROR_OK;
90 }
91
92 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
93 {
94         int c;
95         char command_buffer[128];
96
97         while (1)
98         {       
99                 /* getopt_long stores the option index here. */
100                 int option_index = 0;
101                 
102                 c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
103                 
104                 /* Detect the end of the options. */
105                 if (c == -1)
106                         break;
107                 
108                 switch (c)
109                 {
110                         case 0:
111                                 break;
112                         case 'h':       /* --help | -h */
113                                 help_flag = 1;
114                                 break;
115                         case 'v':       /* --version | -v */
116                                 version_flag = 1;
117                                 break;
118                         case 'f':       /* --file | -f */
119                         {
120                                 snprintf(command_buffer, 128, "script {%s}", optarg);
121                                 add_config_command(command_buffer);
122                                 break;
123                         }
124                         case 's':       /* --search | -s */
125                                 add_script_search_dir(optarg);
126                                 break;
127                         case 'd':       /* --debug | -d */
128                                 if (optarg)
129                                         snprintf(command_buffer, 128, "debug_level %s", optarg);
130                                 else
131                                         snprintf(command_buffer, 128, "debug_level 3");
132                                 command_run_line(cmd_ctx, command_buffer);
133                                 break;
134                         case 'l':       /* --log_output | -l */
135                                 if (optarg)
136                                 {
137                                         snprintf(command_buffer, 128, "log_output %s", optarg);
138                                         command_run_line(cmd_ctx, command_buffer);
139                                 }       
140                                 break;
141                         case 'c':       /* --command | -c */
142                                 if (optarg)
143                                 {
144                                         add_config_command(optarg);
145                                 }       
146                                 break;
147                         case 'p':       /* --pipe | -p */
148 #if BUILD_ECOSBOARD == 1
149                                 /* pipes unsupported on hosted platforms */
150                                 LOG_WARNING("pipes not supported on this platform");
151 #else
152 #ifdef IS_MINGW
153                                 /* pipes currently unsupported on win32 */
154                                 LOG_WARNING("pipes currently unsupported on win32");
155                                 exit(1);
156 #else
157                                 server_use_pipes = 1;
158 #endif
159 #endif
160                                 break;
161                 }
162         }
163
164         if (help_flag)
165         {
166                 LOG_OUTPUT("Open On-Chip Debugger\n(c) 2005-2008 by Dominic Rath\n\n");
167                 LOG_OUTPUT("--help       | -h\tdisplay this help\n");
168                 LOG_OUTPUT("--version    | -v\tdisplay OpenOCD version\n");
169                 LOG_OUTPUT("--file       | -f\tuse configuration file <name>\n");
170                 LOG_OUTPUT("--search     | -s\tdir to search for config files and scripts\n");
171                 LOG_OUTPUT("--debug      | -d\tset debug level <0-3>\n");
172                 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
173                 LOG_OUTPUT("--command    | -c\trun <command>\n");
174                 LOG_OUTPUT("--pipe       | -p\tuse pipes for gdb communication\n");
175                 exit(-1);
176         }       
177
178         if (version_flag)
179         {
180                 /* Nothing to do, version gets printed automatically. */
181                 exit(-1);
182         }
183         
184         return ERROR_OK;
185 }