added support for Tcl config scripts on the command line(use .tcl extension).
[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 <string.h>
32
33 static size_t num_config_files;
34 static char** config_file_names;
35
36 static size_t num_script_dirs;
37 static char** script_search_dirs;
38
39 void add_script_search_dir (const char *dir)
40 {
41         num_script_dirs++;
42         script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
43
44         script_search_dirs[num_script_dirs-1] = strdup(dir);
45         script_search_dirs[num_script_dirs] = NULL;
46 }
47
48 void add_config_command (const char *cfg)
49 {
50         num_config_files++;
51         config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
52
53         config_file_names[num_config_files-1] = strdup(cfg);
54         config_file_names[num_config_files] = NULL;
55 }
56
57 /* return full path or NULL according to search rules */
58 char *find_file(char *file)
59 {
60         FILE *fp = NULL;
61         char **search_dirs = script_search_dirs;
62         char *dir;
63         char const *mode="r";
64         char full_path[1024];
65
66         /* Check absolute and relative to current working dir first.
67          * This keeps full_path reporting belowing working. */
68         snprintf(full_path, 1024, "%s", file);
69         fp = fopen(full_path, mode);
70
71         while (!fp)
72         {
73                 dir = *search_dirs++;
74
75                 if (!dir)
76                         break;
77
78                 snprintf(full_path, 1024, "%s/%s", dir, file);
79                 fp = fopen(full_path, mode);
80         }
81         
82         if (fp)
83         {
84                 fclose(fp);
85                 LOG_DEBUG("found %s", full_path);
86                 return strdup(full_path);
87         }
88         return NULL;
89 }
90
91
92 FILE *open_file_from_path (char *file, char *mode)
93 {
94         if (mode[0]!='r')
95         {
96                 return fopen(file, mode);
97         } else
98         {
99                 char *full_path=find_file(file);
100                 FILE *fp = NULL;
101                 fp = fopen(full_path, mode);
102                 free(full_path);
103                 return fp;
104         }
105 }
106
107 int parse_config_file(struct command_context_s *cmd_ctx)
108 {
109         char **cfg;
110
111         if (!config_file_names)
112                 add_config_command ("script openocd.cfg");
113
114         cfg = config_file_names;
115
116         while (*cfg)
117         {
118                 command_run_line(cmd_ctx, *cfg);
119                 cfg++;
120         }
121
122         return ERROR_OK;
123 }