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