- added myself to copyright on files i remember adding large contributions for over...
[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 "types.h"
28 #include "command.h"
29 #include "configuration.h"
30 #include "log.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <getopt.h>
35 #include <string.h>
36
37 static int help_flag, version_flag;
38
39 static struct option long_options[] =
40 {
41         {"help",        no_argument,            &help_flag,     1},
42         {"version",     no_argument,            &version_flag,  1},
43         {"debug",       optional_argument,      0,              'd'},
44         {"file",        required_argument,      0,              'f'},
45         {"search",      required_argument,      0,              's'},
46         {"log_output",  required_argument,      0,              'l'},
47         {"command",     required_argument,      0,              'c'},
48         {0, 0, 0, 0}
49 };
50
51 int configuration_output_handler(struct command_context_s *context, const char* line)
52 {
53         LOG_USER_N(line);
54
55         return ERROR_OK;
56 }
57
58 int add_default_dirs(void)
59 {
60 #ifdef _WIN32
61         /* Add the parent of the directory where openocd.exe resides to the
62          * config script search path.
63          * Directory layout: 
64          * bin\openocd.exe
65          * lib\openocd
66          * event\at91eb40a_reset.cfg
67          * target\at91eb40a.cfg
68          */
69         {
70                 char strExePath [MAX_PATH];
71                 GetModuleFileName (NULL, strExePath, MAX_PATH);
72                 /* Either this code will *always* work or it will SEGFAULT giving
73                  * excellent information on the culprit. 
74                  */
75                 *strrchr(strExePath, '\\')=0;
76                 strcat(strExePath, "\\..");
77                 add_script_search_dir(strExePath);
78         }
79 #else
80         /* Add dir for openocd supplied scripts last so that user can over
81            ride those scripts if desired. */
82         add_script_search_dir(PKGDATADIR);
83         add_script_search_dir(PKGLIBDIR);
84 #endif
85         return ERROR_OK;
86 }
87
88 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
89 {
90         int c;
91         char command_buffer[128];
92
93         while (1)
94         {       
95                 /* getopt_long stores the option index here. */
96                 int option_index = 0;
97                 
98                 c = getopt_long(argc, argv, "hvd::l:f:s:c:", long_options, &option_index);
99                 
100                 /* Detect the end of the options. */
101                 if (c == -1)
102                         break;
103                 
104                 switch (c)
105                 {
106                         case 0:
107                                 break;
108                         case 'h':       /* --help | -h */
109                                 help_flag = 1;
110                                 break;
111                         case 'v':       /* --version | -v */
112                                 version_flag = 1;
113                                 break;
114                         case 'f':       /* --file | -f */
115                         {
116                                 snprintf(command_buffer, 128, "script {%s}", optarg);
117                                 add_config_command(command_buffer);
118                                 break;
119                         }
120                         case 's':       /* --search | -s */
121                                 add_script_search_dir(optarg);
122                                 break;
123                         case 'd':       /* --debug | -d */
124                                 if (optarg)
125                                         snprintf(command_buffer, 128, "debug_level %s", optarg);
126                                 else
127                                         snprintf(command_buffer, 128, "debug_level 3");
128                                 command_run_line(cmd_ctx, command_buffer);
129                                 break;
130                         case 'l':       /* --log_output | -l */
131                                 if (optarg)
132                                 {
133                                         snprintf(command_buffer, 128, "log_output %s", optarg);
134                                         command_run_line(cmd_ctx, command_buffer);
135                                 }       
136                                 break;
137                         case 'c':       /* --command | -c */
138                                 if (optarg)
139                                 {
140                                         add_config_command(optarg);
141                                 }       
142                                 break;
143                                 
144                 }
145         }
146
147         if (help_flag)
148         {
149                 LOG_OUTPUT("Open On-Chip Debugger\n(c) 2005-2008 by Dominic Rath\n\n");
150                 LOG_OUTPUT("--help       | -h\tdisplay this help\n");
151                 LOG_OUTPUT("--version    | -v\tdisplay OpenOCD version\n");
152                 LOG_OUTPUT("--file       | -f\tuse configuration file <name>\n");
153                 LOG_OUTPUT("--search     | -s\tdir to search for config files and scripts\n");
154                 LOG_OUTPUT("--debug      | -d\tset debug level <0-3>\n");
155                 LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
156                 LOG_OUTPUT("--command    | -c\trun <command>\n");
157                 exit(-1);
158         }       
159
160         if (version_flag)
161         {
162                 /* Nothing to do, version gets printed automatically. */
163                 exit(-1);
164         }       
165
166
167         return ERROR_OK;
168 }