- added myself to copyright on files i remember adding large contributions for over...
[fw/openocd] / src / helper / configuration.c
1 /***************************************************************************
2  *   Copyright (C) 2004, 2005 by Dominic Rath                              *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "types.h"
28 #include "command.h"
29 #include "configuration.h"
30 #include "log.h"
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 static size_t num_config_files;
37 static char** config_file_names;
38
39 static size_t num_script_dirs;
40 static char** script_search_dirs;
41
42 void add_script_search_dir (const char *dir)
43 {
44         num_script_dirs++;
45         script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
46
47         script_search_dirs[num_script_dirs-1] = strdup(dir);
48         script_search_dirs[num_script_dirs] = NULL;
49 }
50
51 void add_config_command (const char *cfg)
52 {
53         num_config_files++;
54         config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
55
56         config_file_names[num_config_files-1] = strdup(cfg);
57         config_file_names[num_config_files] = NULL;
58 }
59
60 /* return full path or NULL according to search rules */
61 char *find_file(const char *file)
62 {
63         FILE *fp = NULL;
64         char **search_dirs = script_search_dirs;
65         char *dir;
66         char const *mode="r";
67         char full_path[1024];
68
69         /* Check absolute and relative to current working dir first.
70          * This keeps full_path reporting belowing working. */
71         snprintf(full_path, 1024, "%s", file);
72         fp = fopen(full_path, mode);
73
74         while (!fp)
75         {
76                 dir = *search_dirs++;
77
78                 if (!dir)
79                         break;
80
81                 snprintf(full_path, 1024, "%s/%s", dir, file);
82                 fp = fopen(full_path, mode);
83         }
84         
85         if (fp)
86         {
87                 fclose(fp);
88                 LOG_DEBUG("found %s", full_path);
89                 return strdup(full_path);
90         }
91         return NULL;
92 }
93
94
95 FILE *open_file_from_path (char *file, char *mode)
96 {
97         if (mode[0]!='r')
98         {
99                 return fopen(file, mode);
100         } else
101         {
102                 char *full_path=find_file(file);
103                 FILE *fp = NULL;
104                 fp = fopen(full_path, mode);
105                 free(full_path);
106                 return fp;
107         }
108 }
109
110 int parse_config_file(struct command_context_s *cmd_ctx)
111 {
112         int retval;
113         char **cfg;
114
115         if (!config_file_names)
116                 add_config_command ("script openocd.cfg");
117
118         cfg = config_file_names;
119
120         while (*cfg)
121         {
122                 retval=command_run_line(cmd_ctx, *cfg);
123                 if (retval!=ERROR_OK)
124                         return retval;
125                 cfg++;
126         }
127
128         return ERROR_OK;
129 }