]> git.sur5r.net Git - openocd/blob - src/helper/options.c
4232cb443979510540464ef81f5565066ab33d71
[openocd] / src / helper / options.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 \r
35 static int help_flag;\r
36 \r
37 static struct option long_options[] =\r
38 {\r
39         {"help",                        no_argument,    &help_flag, 1},\r
40 \r
41         {"debug",                       optional_argument,      0, 'd'},\r
42         {"file",                        required_argument,      0, 'f'},\r
43         {"search",                      required_argument,      0, 's'},\r
44         {"log_output",          required_argument,      0, 'l'},\r
45         {"command",                     required_argument,      0, 'c'},\r
46         \r
47         {0, 0, 0, 0}\r
48 };\r
49 \r
50 \r
51 \r
52 int configuration_output_handler(struct command_context_s *context, char* line)\r
53 {\r
54         INFO(line);\r
55 \r
56         return ERROR_OK;\r
57 }\r
58 \r
59 \r
60 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])\r
61 {\r
62         int c;\r
63         char command_buffer[128];\r
64 \r
65         while (1)\r
66         {       \r
67                 /* getopt_long stores the option index here. */\r
68                 int option_index = 0;\r
69                 \r
70                 c = getopt_long(argc, argv, "hd::l:f:s:c:", long_options, &option_index);\r
71                 \r
72                 /* Detect the end of the options. */\r
73                 if (c == -1)\r
74                         break;\r
75                 \r
76                 switch (c)\r
77                 {\r
78                         case 0:\r
79                                 break;\r
80                         case 'h':       /* --help | -h */\r
81                                 help_flag = 1;\r
82                                 break;\r
83                         case 'f':       /* --file | -f */\r
84                                 snprintf(command_buffer, 128, "script %s", optarg);\r
85                                 add_config_file_name(command_buffer);\r
86                                 break;\r
87                         case 's':       /* --search | -s */\r
88                                 add_script_search_dir(optarg);\r
89                                 break;\r
90                         case 'd':       /* --debug | -d */\r
91                                 if (optarg)\r
92                                         snprintf(command_buffer, 128, "debug_level %s", optarg);\r
93                                 else\r
94                                         snprintf(command_buffer, 128, "debug_level 3");\r
95                                 command_run_line(cmd_ctx, command_buffer);\r
96                                 break;\r
97                         case 'l':       /* --log_output | -l */\r
98                                 if (optarg)\r
99                                 {\r
100                                         snprintf(command_buffer, 128, "log_output %s", optarg);\r
101                                         command_run_line(cmd_ctx, command_buffer);\r
102                                 }       \r
103                                 break;\r
104                         case 'c':       /* --command | -c */\r
105                                 if (optarg)\r
106                                 {\r
107                                         add_config_file_name(optarg);\r
108                                 }       \r
109                                 break;\r
110                                 \r
111                 }\r
112         }\r
113 \r
114         if (help_flag)\r
115         {\r
116                 OUTPUT("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");\r
117                 OUTPUT("--help       | -h\tdisplay this help\n");\r
118                 OUTPUT("--file       | -f\tuse configuration file <name>\n");\r
119                 OUTPUT("--search     | -s\tdir to search for config files and scripts.\n");\r
120                 OUTPUT("--debug      | -d\tset debug level <0-3>\n");\r
121                 OUTPUT("--log_output | -l\tredirect log output to file <name>\n");\r
122                 OUTPUT("--command    | -c\trun <command>\n");\r
123                 exit(-1);\r
124         }       \r
125 \r
126 #ifdef _WIN32\r
127         /* Add the parent of the directory where openocd.exe resides to the\r
128          * config script search path.\r
129          * Directory layout: \r
130          * bin\openocd.exe\r
131          * lib\openocd\r
132          * event\at91eb40a_reset.cfg\r
133          * target\at91eb40a.cfg\r
134          */\r
135         {\r
136                 char strExePath [MAX_PATH];\r
137                 GetModuleFileName (NULL, strExePath, MAX_PATH);\r
138                 /* Either this code will *always* work or it will SEGFAULT giving\r
139                  * excellent information on the culprit. \r
140                  */\r
141                 *strrchr(strExePath, '\\')=0;\r
142                 strcat(strExePath, "\\..");\r
143                 add_script_search_dir(strExePath);\r
144         }\r
145 #else\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 #endif\r
151 \r
152         return ERROR_OK;\r
153 }\r