- prepare OpenOCD for branching, created ./trunk/
[fw/openocd] / src / helper / configuration.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
33 char* config_file_name;
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         {"log_output",          required_argument,      0, 'l'},
44         
45         {0, 0, 0, 0}
46 };
47
48 int configuration_output_handler(struct command_context_s *context, char* line)
49 {
50         INFO(line);
51         
52         return ERROR_OK;
53 }
54
55 int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
56 {
57         int c;
58         char command_buffer[128];
59                         
60         while (1)
61         {       
62                 /* getopt_long stores the option index here. */
63                 int option_index = 0;
64                 
65                 c = getopt_long(argc, argv, "hd::l:f:", long_options, &option_index);
66                 
67                 /* Detect the end of the options. */
68                 if (c == -1)
69                         break;
70                 
71                 switch (c)
72                 {
73                         case 0:
74                                 break;
75                         case 'h':       /* --help | -h */
76                                 help_flag = 1;
77                                 break;
78                         case 'f':       /* --file | -f */
79                                 config_file_name = optarg;
80                                 break;
81                         case 'd':       /* --debug | -d */
82                                 if (optarg)
83                                         snprintf(command_buffer, 128, "debug_level %s", optarg);
84                                 else
85                                         snprintf(command_buffer, 128, "debug_level 3");
86                                 command_run_line(cmd_ctx, command_buffer);
87                                 break;
88                         case 'l':       /* --log_output | -l */
89                                 if (optarg)
90                                 {
91                                         snprintf(command_buffer, 128, "log_output %s", optarg);
92                                         command_run_line(cmd_ctx, command_buffer);
93                                 }       
94                                 break;
95                 }
96         }
97
98         if (help_flag)
99         {
100                 printf("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");
101                 printf("--help       | -h\tdisplay this help\n");
102                 printf("--file       | -f\tuse configuration file <name>\n");
103                 printf("--debug      | -d\tset debug level <0-3>\n");
104                 printf("--log_output | -l\tredirect log output to file <name>\n");
105                 exit(-1);
106         }       
107
108         return ERROR_OK;
109 }
110
111 int parse_config_file(struct command_context_s *cmd_ctx)
112 {
113         FILE *config_file;
114
115         if (!config_file_name)
116                 config_file_name = "openocd.cfg";
117
118         config_file = fopen(config_file_name, "r");
119         if (!config_file)
120         {
121                 ERROR("couldn't open config file");
122                 return ERROR_NO_CONFIG_FILE;
123         }
124
125         command_run_file(cmd_ctx, config_file, COMMAND_CONFIG);
126
127         fclose(config_file);
128
129         return ERROR_OK;
130 }
131