aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/dtc/srcpos.c
diff options
context:
space:
mode:
authorStephen Warren <swarren@nvidia.com>2012-09-28 17:25:59 -0400
committerRob Herring <rob.herring@calxeda.com>2012-10-01 12:11:35 -0400
commitcd296721a9645f9f28800a072490fa15458d1fb7 (patch)
tree492b9a268a48af07844fbbd39519f95676ee73fe /scripts/dtc/srcpos.c
parentacc2097934b5403b97f95763fe99fc115b818061 (diff)
dtc: import latest upstream dtc
This updates scripts/dtc to commit 317a5d9 "dtc: zero out new label objects" from git://git.jdl.com/software/dtc.git. This adds features such as: * /bits/ syntax for cell data. * Math expressions within cell data. * The ability to delete properties or nodes. * Support for #line directives in the input file, which allows the use of cpp on *.dts. * -i command-line option (/include/ path) * -W/-E command-line options for error/warning control. * Removal of spew to STDOUT containing the filename being compiled. * Many additions to the libfdt API. Signed-off-by: Stephen Warren <swarren@nvidia.com> Acked-by: Jon Loeliger <jdl@jdl.com> Signed-off-by: Rob Herring <rob.herring@calxeda.com>
Diffstat (limited to 'scripts/dtc/srcpos.c')
-rw-r--r--scripts/dtc/srcpos.c98
1 files changed, 91 insertions, 7 deletions
diff --git a/scripts/dtc/srcpos.c b/scripts/dtc/srcpos.c
index 36a38e9f1a2c..246ab4bc0d9d 100644
--- a/scripts/dtc/srcpos.c
+++ b/scripts/dtc/srcpos.c
@@ -24,6 +24,15 @@
24#include "dtc.h" 24#include "dtc.h"
25#include "srcpos.h" 25#include "srcpos.h"
26 26
27/* A node in our list of directories to search for source/include files */
28struct search_path {
29 struct search_path *next; /* next node in list, NULL for end */
30 const char *dirname; /* name of directory to search */
31};
32
33/* This is the list of directories that we search for source files */
34static struct search_path *search_path_head, **search_path_tail;
35
27 36
28static char *dirname(const char *path) 37static char *dirname(const char *path)
29{ 38{
@@ -47,6 +56,64 @@ struct srcfile_state *current_srcfile; /* = NULL */
47#define MAX_SRCFILE_DEPTH (100) 56#define MAX_SRCFILE_DEPTH (100)
48static int srcfile_depth; /* = 0 */ 57static int srcfile_depth; /* = 0 */
49 58
59
60/**
61 * Try to open a file in a given directory.
62 *
63 * If the filename is an absolute path, then dirname is ignored. If it is a
64 * relative path, then we look in that directory for the file.
65 *
66 * @param dirname Directory to look in, or NULL for none
67 * @param fname Filename to look for
68 * @param fp Set to NULL if file did not open
69 * @return allocated filename on success (caller must free), NULL on failure
70 */
71static char *try_open(const char *dirname, const char *fname, FILE **fp)
72{
73 char *fullname;
74
75 if (!dirname || fname[0] == '/')
76 fullname = xstrdup(fname);
77 else
78 fullname = join_path(dirname, fname);
79
80 *fp = fopen(fullname, "r");
81 if (!*fp) {
82 free(fullname);
83 fullname = NULL;
84 }
85
86 return fullname;
87}
88
89/**
90 * Open a file for read access
91 *
92 * If it is a relative filename, we search the full search path for it.
93 *
94 * @param fname Filename to open
95 * @param fp Returns pointer to opened FILE, or NULL on failure
96 * @return pointer to allocated filename, which caller must free
97 */
98static char *fopen_any_on_path(const char *fname, FILE **fp)
99{
100 const char *cur_dir = NULL;
101 struct search_path *node;
102 char *fullname;
103
104 /* Try current directory first */
105 assert(fp);
106 if (current_srcfile)
107 cur_dir = current_srcfile->dir;
108 fullname = try_open(cur_dir, fname, fp);
109
110 /* Failing that, try each search path in turn */
111 for (node = search_path_head; !*fp && node; node = node->next)
112 fullname = try_open(node->dirname, fname, fp);
113
114 return fullname;
115}
116
50FILE *srcfile_relative_open(const char *fname, char **fullnamep) 117FILE *srcfile_relative_open(const char *fname, char **fullnamep)
51{ 118{
52 FILE *f; 119 FILE *f;
@@ -56,13 +123,7 @@ FILE *srcfile_relative_open(const char *fname, char **fullnamep)
56 f = stdin; 123 f = stdin;
57 fullname = xstrdup("<stdin>"); 124 fullname = xstrdup("<stdin>");
58 } else { 125 } else {
59 if (!current_srcfile || !current_srcfile->dir 126 fullname = fopen_any_on_path(fname, &f);
60 || (fname[0] == '/'))
61 fullname = xstrdup(fname);
62 else
63 fullname = join_path(current_srcfile->dir, fname);
64
65 f = fopen(fullname, "r");
66 if (!f) 127 if (!f)
67 die("Couldn't open \"%s\": %s\n", fname, 128 die("Couldn't open \"%s\": %s\n", fname,
68 strerror(errno)); 129 strerror(errno));
@@ -119,6 +180,23 @@ int srcfile_pop(void)
119 return current_srcfile ? 1 : 0; 180 return current_srcfile ? 1 : 0;
120} 181}
121 182
183void srcfile_add_search_path(const char *dirname)
184{
185 struct search_path *node;
186
187 /* Create the node */
188 node = xmalloc(sizeof(*node));
189 node->next = NULL;
190 node->dirname = xstrdup(dirname);
191
192 /* Add to the end of our list */
193 if (search_path_tail)
194 *search_path_tail = node;
195 else
196 search_path_head = node;
197 search_path_tail = &node->next;
198}
199
122/* 200/*
123 * The empty source position. 201 * The empty source position.
124 */ 202 */
@@ -250,3 +328,9 @@ srcpos_warn(struct srcpos *pos, char const *fmt, ...)
250 328
251 va_end(va); 329 va_end(va);
252} 330}
331
332void srcpos_set_line(char *f, int l)
333{
334 current_srcfile->name = f;
335 current_srcfile->lineno = l;
336}