- convert all files to unix line-ending
[fw/openocd] / src / helper / options.c
1 /***************************************************************************
2  *   Copyright (C) 2004, 2005 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "types.h"
25 #include "command.h"
26 #include "configuration.h"
27 #include "log.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <string.h>
33
34
35 static int help_flag;
36
37 static struct option long_options[] =
38 {
39         {"help",                        no_argument,    &help_flag, 1},
40
41         {"debug",                       optional_argument,      0, 'd'},
42         {"file",                        required_argument,      0, 'f'},
43         {"search",                      required_argument,      0, 's'},
44         {"log_output",          required_argument,      0, 'l'},
45         {"command",                     required_argument,      0, 'c'},
46         
47         {0, 0, 0, 0}
48 };
49
50
51
52 int configuration_output_handler(struct command_context_s *context, char* line)
53 {
54         INFO(line);
55
56         return ERROR_OK;
57 }
58
59
60 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
61 {
62         int c;
63         char command_buffer[128];
64
65         while (1)
66         {       
67                 /* getopt_long stores the option index here. */
68                 int option_index = 0;
69                 
70                 c = getopt_long(argc, argv, "hd::l:f:s:c:", long_options, &option_index);
71                 
72                 /* Detect the end of the options. */
73                 if (c == -1)
74                         break;
75                 
76                 switch (c)
77                 {
78                         case 0:
79                                 break;
80                         case 'h':       /* --help | -h */
81                                 help_flag = 1;
82                                 break;
83                         case 'f':       /* --file | -f */
84                                 snprintf(command_buffer, 128, "script %s", optarg);
85                                 add_config_file_name(command_buffer);
86                                 break;
87                         case 's':       /* --search | -s */
88                                 add_script_search_dir(optarg);
89                                 break;
90                         case 'd':       /* --debug | -d */
91                                 if (optarg)
92                                         snprintf(command_buffer, 128, "debug_level %s", optarg);
93                                 else
94                                         snprintf(command_buffer, 128, "debug_level 3");
95                                 command_run_line(cmd_ctx, command_buffer);
96                                 break;
97                         case 'l':       /* --log_output | -l */
98                                 if (optarg)
99                                 {
100                                         snprintf(command_buffer, 128, "log_output %s", optarg);
101                                         command_run_line(cmd_ctx, command_buffer);
102                                 }       
103                                 break;
104                         case 'c':       /* --command | -c */
105                                 if (optarg)
106                                 {
107                                         add_config_file_name(optarg);
108                                 }       
109                                 break;
110                                 
111                 }
112         }
113
114         if (help_flag)
115         {
116                 OUTPUT("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");
117                 OUTPUT("--help       | -h\tdisplay this help\n");
118                 OUTPUT("--file       | -f\tuse configuration file <name>\n");
119                 OUTPUT("--search     | -s\tdir to search for config files and scripts.\n");
120                 OUTPUT("--debug      | -d\tset debug level <0-3>\n");
121                 OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
122                 OUTPUT("--command    | -c\trun <command>\n");
123                 exit(-1);
124         }       
125
126 #ifdef _WIN32
127         /* Add the parent of the directory where openocd.exe resides to the
128          * config script search path.
129          * Directory layout: 
130          * bin\openocd.exe
131          * lib\openocd
132          * event\at91eb40a_reset.cfg
133          * target\at91eb40a.cfg
134          */
135         {
136                 char strExePath [MAX_PATH];
137                 GetModuleFileName (NULL, strExePath, MAX_PATH);
138                 /* Either this code will *always* work or it will SEGFAULT giving
139                  * excellent information on the culprit. 
140                  */
141                 *strrchr(strExePath, '\\')=0;
142                 strcat(strExePath, "\\..");
143                 add_script_search_dir(strExePath);
144         }
145 #else
146         /* Add dir for openocd supplied scripts last so that user can over
147            ride those scripts if desired. */
148         add_script_search_dir(PKGDATADIR);
149         add_script_search_dir(PKGLIBDIR);
150 #endif
151
152         return ERROR_OK;
153 }