DTC: Add support for a C-like #include "file" mechanism.
PATCH 1/1 DTC: Add support for a C-like #include "file" mechanism.
Keeps track of open files in a stack, and assigns a filenum to source positions for each lexical token. Modified error reporting to show source file as well. No policy on file directory basis has been decided. Still handles stdin.
Tested on all arch/powerpc/boot/dts DTS files
--- Makefile | 3 +- dtc-lexer.l | 39 ++++++++++++++++- dtc-parser.y | 9 +++- dtc.c | 19 +------- dtc.h | 2 +- srcpos.c | 105 +++++++++++++++++++++++++++++++++++++++++++ srcpos.h | 71 +++++++++++++++++++++++++++++ srcposstack.h | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ treesource.c | 6 ++- 9 files changed, 369 insertions(+), 23 deletions(-) create mode 100644 srcpos.c create mode 100644 srcpos.h create mode 100644 srcposstack.h
Makefile +2 -1
@@ -3,7 +3,8 @@ CFLAGS = -Wall -g BISON = bison -DTC_OBJS = dtc.o livetree.o flattree.o data.o treesource.o fstree.o \ +DTC_OBJS = dtc.o flattree.o fstree.o data.o livetree.o \ + srcpos.o treesource.o \ dtc-parser.tab.o lex.yy.o DEPFILES = $(DTC_OBJS:.o=.d)
dtc-lexer.l +38 -1
@@ -20,6 +20,7 @@ %option noyywrap nounput yylineno +%x INCLUDE %x CELLDATA %x BYTESTRING %x MEMRESERVE
@@ -32,8 +33,9 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) %{ #include "dtc.h" - #include "dtc-parser.tab.h" +#include "srcposstack.h" + /*#define LEXDEBUG 1*/
@@ -49,7 +51,27 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) %% +#[ \t]*include BEGIN(INCLUDE); + +<INCLUDE>[ \t]* /* whitespace before file name */ +<INCLUDE>\"[^"\n]*\" { + yytext[strlen(yytext) - 1] = 0; + if (!push_input_file(yytext + 1)) { + /* Some unrecoverable error.*/ + exit(1); + } + BEGIN(INITIAL); + } + + +<<EOF>> { + if (!pop_input_file()) { + yyterminate(); + } + } + \"[^"]*\" { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("String: %s\n", yytext); yylval.data = data_copy_escape_string(yytext+1,
@@ -59,6 +81,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } "/memreserve/" { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("Keyword: /memreserve/\n"); BEGIN(MEMRESERVE);
@@ -66,6 +89,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <MEMRESERVE>[0-9a-fA-F]+ { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; if (yyleng > 2*sizeof(yylval.addr)) { fprintf(stderr, "Address value %s too large\n",
@@ -78,12 +102,14 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <MEMRESERVE>";" { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("/MEMRESERVE\n"); BEGIN(INITIAL); return ';'; } <CELLDATA>[bodh]# { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; if (*yytext == 'b') yylval.cbase = 2;
@@ -98,6 +124,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <CELLDATA>[0-9a-fA-F]+ { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; yylval.str = strdup(yytext); DPRINT("Cell: '%s'\n", yylval.str);
@@ -105,6 +132,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <CELLDATA>">" { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("/CELLDATA\n"); BEGIN(INITIAL);
@@ -112,6 +140,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <CELLDATA>\&{REFCHAR}* { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("Ref: %s\n", yytext+1); yylval.str = strdup(yytext+1);
@@ -119,6 +148,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <BYTESTRING>[0-9a-fA-F]{2} { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; yylval.byte = strtol(yytext, NULL, 16); DPRINT("Byte: %02x\n", (int)yylval.byte);
@@ -126,6 +156,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } <BYTESTRING>"]" { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("/BYTESTRING\n"); BEGIN(INITIAL);
@@ -135,6 +166,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) , { /* Technically this is a valid property name, but we'd rather use it as punctuation, so detect it here in preference */ + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("Char (propname like): %c (\\x%02x)\n", yytext[0], (unsigned)yytext[0]);
@@ -142,6 +174,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } {PROPCHAR}+ { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("PropName: %s\n", yytext); yylval.str = strdup(yytext);
@@ -149,6 +182,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) } {PROPCHAR}+(@{UNITCHAR}+)? { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("NodeName: %s\n", yytext); yylval.str = strdup(yytext);
@@ -157,6 +191,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) [a-zA-Z_][a-zA-Z0-9_]*: { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("Label: %s\n", yytext); yylval.str = strdup(yytext);
@@ -167,6 +202,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) <*>{WS}+ /* eat whitespace */ <*>"/*"([^*]|\*+[^*/])*\*+"/" { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; DPRINT("Comment: %s\n", yytext); /* eat comments */
@@ -175,6 +211,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) <*>"//".*\n /* eat line comments */ <*>. { + yylloc.filenum = srcpos_filenum; yylloc.first_line = yylineno; switch (yytext[0]) { case '<':
dtc-parser.y +8 -1
@@ -23,6 +23,7 @@ %{ #include "dtc.h" +#include "srcpos.h" int yylex(void); void yyerror(char const *);
@@ -178,7 +179,13 @@ label: DT_LABEL { $$ = $1; } void yyerror (char const *s) { - fprintf (stderr, "%s at line %d\n", s, yylloc.first_line); + const char *fname = srcpos_filename_for_num(yylloc.filenum); + + if (strcmp(fname, "-") == 0) + fname = "stdin"; + + fprintf(stderr, "%s:%d %s\n", + fname, yylloc.first_line, s); }
dtc.c +2 -17
@@ -19,6 +19,7 @@*/ #include "dtc.h" +#include "srcpos.h" char *join_path (char *path,char *name) {
@@ -61,21 +62,6 @@ void fill_fullpaths(struct node *tree, char *prefix)fill_fullpaths (child, tree->fullpath); } -static FILE *dtc_open_file (char *fname) -{ -FILE *f; - -if (streq (fname,"-" )) - f = stdin; -else - f =fopen (fname,"r" ); - -if (! f) -die ("Couldn't open \"%s\": %s\n" , fname,strerror (errno)); - -return f; -} -static void usage (void ) {fprintf (stderr,"Usage:\n" );
@@ -166,8 +152,7 @@ int main(int argc, char *argv[]) inform, outform, arg);if (streq (inform,"dts" )) { - inf =dtc_open_file (arg); - bi =dt_from_source (inf); + bi =dt_from_source (arg); }else if (streq (inform,"fs" )) { bi =dt_from_fs (arg); }else if (streq (inform,"dtb" )) {
dtc.h +1 -1
@@ -223,7 +223,7 @@ struct boot_info *dt_from_blob(FILE *f);/* Tree source */ void dt_to_source (FILE *f,struct boot_info *bi); -struct boot_info *dt_from_source (FILE *f); +struct boot_info *dt_from_source (const char *f);/* FS trees */
srcpos.c new file +105 -0
105 lines not on this page · raw message
srcpos.h new file +71 -0
71 lines not on this page · raw message
srcposstack.h new file +138 -0
138 lines not on this page · raw message
treesource.c +4 -2
@@ -19,6 +19,7 @@*/ #include "dtc.h" +#include "srcpos.h" extern FILE *yyin;extern int yyparse (void );
@@ -26,11 +27,12 @@ extern void yyerror(char const *);struct boot_info *the_boot_info; -struct boot_info *dt_from_source (FILE *f) +struct boot_info *dt_from_source (const char *fname) { the_boot_info =NULL ; - yyin = f; +push_input_file (fname); +if (yyparse () !=0 )return NULL ;
Signature · 1 line
1.5.0.3
Jon Loeliger
AppliedSo, like, the other day Jon Loeliger mumbled:
Quoted 10 lines
Keeps track of open files in a stack, and assigns a filenum to source positions for each lexical token. Modified error reporting to show source file as well. No policy on file directory basis has been decided. Still handles stdin.
Tested on all
arch/powerpc/boot/dtsDTS filesSigned-off-by Jon Loeliger <[email protected]>
Applied.
Er, thanks,
jdl
Quoted 8 lines · Jon Loeliger
So, like, the other day Jon Loeliger mumbled:
Quoted 7 lines
Keeps track of open files in a stack, and assigns a filenum to source positions for each lexical token. Modified error reporting to show source file as well. No policy on file directory basis has been decided. Still handles stdin.
Quoted 5 lines
Quoted 3 lines
Tested on all
arch/powerpc/boot/dtsDTS filesSigned-off-by Jon Loeliger <[email protected]>Applied.
Erg. I was going to comment on this, but I've been sick this week. I like the idea, but I'm not real happy with some of the details.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 6 lines · Jon Loeliger
Keeps track of open files in a stack, and assigns a filenum to source positions for each lexical token. Modified error reporting to show source file as well. No policy on file directory basis has been decided. Still handles stdin.
I have some issues with the details of this approach.
[snip]
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 54
Quoted 5 lines
dtc-lexer.l +1
-0@@ -49,7 +51,27 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) %% +#[ \t]*include BEGIN(INCLUDE);
I don't think using this CPP like syntax is a good idea, for several reasons. Firstly, if people see '#include' in the file, they're likely to assume that the file is actually processed by CPP and so #define and other CPP directives will work. Second, the circumstances under which this token will be recognized, and what's valid after it aren't *quite* the same as CPP which is also potentially confusing. Thirdly, '#include' is technically a valid property name, they can be disambiguated lexically in various ways, but they're all pretty ugly.
If we need new keywords in the dts, which need to be lexically distinct from property and node names, I suggest we do what I did for memreserve and use /s (so '/include/' in this case). Because of its use in paths, / is one of the few characters we can be pretty confident won't appear in node or property names.
However, in any case. It's not clear to me that an include facility built into dtc itself it particularly useful without some sort of reasonable macro system as well, which would be substantially more complex to implement.
So, instead of doing this in dtc itself, I really suggest we instead use m4 to preprocess dts files, which gives us both include and macro facilities. If that turns out to be inadequate or too inconvenient for people's needs, then we can revisit putting these facilities into dtc itself - and out experience with using m4 is likely to give us a much better understanding of how the built-in system need to work.
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 56
Quoted 1 line
dtc-lexer.l +1
-0+<INCLUDE>[ \t]* /* whitespace before file name */
You don't need this rule - the lex rule which ignores whitespace is recognized in all starting conditions, including <INCLUDE>.
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 60
Quoted 4 lines
dtc-lexer.l +4
-0+<INCLUDE>\"[^"\n]*\" { + yytext[strlen(yytext) - 1] = 0; + if (!push_input_file(yytext + 1)) { + /* Some unrecoverable error.*/
Should be some kind of error message here.
[snip]
Quoted 52 lines
srcposstack.h new file +46
-0@@ -0,0 +1,138 @@ +/* +* Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of the +* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +* USA +*/ + +#include "srcpos.h" + + +/* +* This code should only be included into the lexical analysis. +* It references global context symbols that are only present +* in the generated lex.yy,c file. +*/ + +#ifdef FLEX_SCANNER + + +/* +* Stack of nested include file contexts. +*/ + +struct incl_file { +int filenum; +FILE *file; +YY_BUFFER_STATE yy_prev_buf; +int yy_prev_lineno; +struct incl_file *prev; +}; + +struct incl_file *incl_file_stack; + +
srcposstack.h line 56
Quoted 10 lines
srcposstack.h new file +10
-0+/* +* Detect infinite include recursion. +*/ +#define MAX_INCLUDE_DEPTH (100) + +static int incl_depth =0 ; + + + +int push_input_file (const char *filename)
Functions of this sort of size shouldn't go in a .h (particularly when not marked static). Either put then in srcpos.c, or directly into dtc-lexer.l in the (currently empty) third section.
[snip]
Quoted 16 lines
treesource.c +1
-0@@ -19,6 +19,7 @@*/ #include "dtc.h" +#include "srcpos.h" extern FILE *yyin;extern int yyparse (void );@@ -26,11 +27,12 @@ extern void yyerror(char const *);struct boot_info *the_boot_info;
treesource.c extern void yyerror(char const *); · line 37
Quoted 10 lines
treesource.c +3
-2-struct boot_info *dt_from_source (FILE *f) +struct boot_info *dt_from_source (const char *fname) { the_boot_info =NULL ; - yyin = f; +push_input_file (fname); +if (yyparse () !=0 )return NULL ;
It's not at all clear to me how these changes still handle input from stdin.
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 5 lines · David Gibson
[snip]
Quoted 4 lines
(quoted diff) +0
-0@@ -49,7 +51,27 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) %%
Quoted 10 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 54
Quoted 1 line
dtc-lexer.l +1
-0+#[ \t]*include BEGIN(INCLUDE);I don't think using this CPP like syntax is a good idea, for several reasons. Firstly, if people see '#include' in the file, they're likely to assume that the file is actually processed by CPP and so #define and other CPP directives will work. Second, the circumstances under which this token will be recognized, and what's valid after it aren't *quite* the same as CPP which is also potentially confusing. Thirdly, '#include' is technically a valid property name, they can be disambiguated lexically in various ways, but they're all pretty ugly.
I can happily change the "3include" syntax. I'm not tied to it at all, but was rather just tacitly defaulting to a C-like syntax.
Quoted 5 lines
If we need new keywords in the dts, which need to be lexically distinct from property and node names, I suggest we do what I did for memreserve and use /s (so '/include/' in this case). Because of its use in paths, / is one of the few characters we can be pretty confident won't appear in node or property names.
Yeah. I'll change it to somethig like:
/include/ "filename"
Does that work for you?
Quoted 4 lines
However, in any case. It's not clear to me that an include facility built into dtc itself it particularly useful without some sort of reasonable macro system as well, which would be substantially more complex to implement.
Totally agree. We're doing incremental development here.... :-)
Quoted 3 lines
So, instead of doing this in dtc itself, I really suggest we instead use m4 to preprocess dts files, which gives us both include and macro facilities.
To be honest, I really don't think m4 is the right solution for us. Sure, it will likely work at the start, but I think over time it will be lacking and not quite the right functionality. For example, I tend to agree with Kumar that we might need some form of "overlay" kind of concepts, both "merge" and "replace" forms, along with some interpolative substitution mechanisms.
Quoted 5 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 56
Quoted 2 lines
dtc-lexer.l +1
-0+<INCLUDE>[ \t]* /* whitespace before file name */You don't need this rule - the lex rule which ignores whitespace is recognized in all starting conditions, including <INCLUDE>.
It was "by the book" anyway. But we can remove it.
Quoted 6 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 60
Quoted 4 lines
dtc-lexer.l +4
-0+<INCLUDE>\"[^"\n]*\" { + yytext[strlen(yytext) - 1] = 0; + if (!push_input_file(yytext + 1)) { + /* Some unrecoverable error.*/Should be some kind of error message here.
Hrm. Yeah.
Quoted 5 lines
srcposstack.h line 56
Quoted 1 line
srcposstack.h new file +1
-0+int push_input_file (const char *filename)Functions of this sort of size shouldn't go in a .h (particularly when not marked static). Either put then in
srcpos.c, or directly into dtc-lexer.l in the (currently empty) third section.
Well, it has to come early so that the type is seen prior to the builtin version. But it could be reorganized a bit, I suppose. I was just angling on not cluttering up the lexer proper.
Quoted 6 lines
Quoted 6 lines
treesource.c +1
-1-struct boot_info *dt_from_source (FILE *f) +struct boot_info *dt_from_source (const char *fname) { the_boot_info =NULL ;
Quoted 8 lines
treesource.c extern void yyerror(char const *); · line 37
Quoted 5 lines
treesource.c +2
-1- yyin = f; +push_input_file (fname); +if (yyparse () !=0 )return NULL ;It's not at all clear to me how these changes still handle input from stdin.
It is handled here:
FILE *dtc_open_file(const char *fname)
+{
+ FILE *f;
+
+ if (lookup_file_name(fname, 1) < 0)
+ die("Too many files opened\n");
+
+ if (streq(fname, "-"))
+ f = stdin;
+ else
+ f = fopen(fname, "r");
+
+ if (! f)
+ die("Couldn't open \"%s\": %s\n", fname,
strerror(errno));
+
+ return f;
+}
Note some people are seeing some compilation problems around the struct field "filenum" that I introduced. I don't see that problem, but a coworker here does, so I am now able to reproduce the issue and am working on a fix. Sorry.
Thanks,
jdl
Quoted 28 lines · Jon Loeliger
Quoted 5 lines · David Gibson
[snip]
Quoted 4 lines
(quoted diff) +0
-0@@ -49,7 +51,27 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) %%Quoted 10 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 54
Quoted 1 line
dtc-lexer.l +1
-0+#[ \t]*include BEGIN(INCLUDE);I don't think using this CPP like syntax is a good idea, for several reasons. Firstly, if people see '#include' in the file, they're likely to assume that the file is actually processed by CPP and so #define and other CPP directives will work. Second, the circumstances under which this token will be recognized, and what's valid after it aren't *quite* the same as CPP which is also potentially confusing. Thirdly, '#include' is technically a valid property name, they can be disambiguated lexically in various ways, but they're all pretty ugly.
I can happily change the "3include" syntax. I'm not tied to it at all, but was rather just tacitly defaulting to a C-like syntax.
Quoted 5 lines
If we need new keywords in the dts, which need to be lexically distinct from property and node names, I suggest we do what I did for memreserve and use /s (so '/include/' in this case). Because of its use in paths, / is one of the few characters we can be pretty confident won't appear in node or property names.
Quoted 5 lines
Yeah. I'll change it to somethig like:
/include/ "filename"
Does that work for you?
I think it should have a ; after it too, to match other things. On second though, I'm a bit uncertain about this syntax - this construct is recognized at the lexical, not the grammar level, which I think means it should look different from other constructs. Maybe
/include "filename"/
which has the same flavour and lexical recognition properties as the former. Perhaps simpler, even, since we could recognize it as a single token. But it gives a bit more of an impression that it's a single, magic, atom, rather than a "statement" similar to the rest of the file.
Or else maybe we should change where it's recognized, so it is only valid between "statements". I don't think there's a strong need to allow such constructs as:
reg = /include/ "foo";;
or whatever.
Quoted 6 lines
Quoted 4 lines
However, in any case. It's not clear to me that an include facility built into dtc itself it particularly useful without some sort of reasonable macro system as well, which would be substantially more complex to implement.
Totally agree. We're doing incremental development here.... :-)
Hrm, yeah, that's kind of what worries me. For a full macro facilitiy, we're pretty much heading into the realm of a real (mini-) language, not just an alternate representation of the device tree structure itself.
Languages developed incrementally tend to be atrocious. So, I really don't like the idea of adding macro facilities to dtc itself without a coherent and at least complete in outline idea of what the macro language will look like as a whole.
Quoted 11 lines
Quoted 3 lines
So, instead of doing this in dtc itself, I really suggest we instead use m4 to preprocess dts files, which gives us both include and macro facilities.
To be honest, I really don't think m4 is the right solution for us. Sure, it will likely work at the start, but I think over time it will be lacking and not quite the right functionality. For example, I tend to agree with Kumar that we might need some form of "overlay" kind of concepts, both "merge" and "replace" forms, along with some interpolative substitution mechanisms.
Oh we'll certainly need some sort of overlay thing. But I think that can be orthogonal to the actual include and macro mechanism. Here's what I vaguely had in mind.
Simplest variant:
The same node can be defined any number of times, each copy is merged. Properties within the node can be defined any number of times, last definition wins.
Extensions:
A prefix on the property names can mark the definion as weak or strong (the default). Weak properties can be freely redefined, redefining a strong one is an error. Final .dts files would usually use strong definitions, includes and macros of library components would use weak ones, at least for the properties it makes sense to override.
To avoid having to reconstruct the entire path to a node to redefine a few properties in something deep within the tree, a shorthand could allow redfinition of a labelled node. So something like:
foo {
bar {
label: baz {
property1 = ...;
}
}
}
&label {
property2 = ...;
}Would result in /foo/bar/baz having two properties.
Quoted 18 lines
Quoted 4 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 56
Quoted 1 line
dtc-lexer.l +1
-0+<INCLUDE>[ \t]* /* whitespace before file name */You don't need this rule - the lex rule which ignores whitespace is recognized in all starting conditions, including <INCLUDE>.
It was "by the book" anyway. But we can remove it.
Quoted 6 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 60
Quoted 4 lines
dtc-lexer.l +4
-0+<INCLUDE>\"[^"\n]*\" { + yytext[strlen(yytext) - 1] = 0; + if (!push_input_file(yytext + 1)) { + /* Some unrecoverable error.*/Should be some kind of error message here.
Hrm. Yeah.
Quoted 2 lines
Quoted 1 line
srcposstack.h new file +1
-0+int push_input_file (const char *filename)
Quoted 8 lines
Quoted 3 lines
Functions of this sort of size shouldn't go in a .h (particularly when not marked static). Either put then in
srcpos.c, or directly into dtc-lexer.l in the (currently empty) third section.Well, it has to come early so that the type is seen prior to the builtin version. But it could be reorganized a bit, I suppose. I was just angling on not cluttering up the lexer proper.
Err... builtin version of what?
Quoted 36 lines
Quoted 5 lines
Quoted 5 lines
treesource.c +1
-1-struct boot_info *dt_from_source (FILE *f) +struct boot_info *dt_from_source (const char *fname) { the_boot_info =NULL ;Quoted 8 lines
treesource.c extern void yyerror(char const *); · line 37
Quoted 5 lines
treesource.c +2
-1- yyin = f; +push_input_file (fname); +if (yyparse () !=0 )return NULL ;It's not at all clear to me how these changes still handle input from stdin.
It is handled here:
FILE *dtc_open_file(const char *fname)
srcpos.c new file +13
-0+{ +FILE *f; + +if (lookup_file_name (fname,1 ) <0 ) +die ("Too many files opened\n" ); + +if (streq (fname,"-" )) + f = stdin; +else + f =fopen (fname,"r" ); + +if (! f) + die("Couldn't open \"%s\": %s\n", fname,strerror(errno));
srcpos.c new file +3
-0+ +return f; +}
Quoted 5 lines
Note some people are seeing some compilation problems around the struct field "filenum" that I introduced. I don't see that problem, but a coworker here does, so I am now able to reproduce the issue and am working on a fix. Sorry.
Hrm. Which struct did you add the field to?
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Quoted 8 lines · David Gibson
Quoted 8 lines
Quoted 6 lines
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 60
Quoted 4 lines
+<INCLUDE>\"[^"\n]*\" {
dtc-lexer.l +3
-0+ yytext[strlen(yytext) - 1] = 0; + if (!push_input_file(yytext + 1)) { + /* Some unrecoverable error.*/Should be some kind of error message here.
Hrm. Yeah.
So I looked, and all the return 0 cases issue specific errors before returning, so that happens already.
Quoted 6 lines
Quoted 4 lines
Well, it has to come early so that the type is seen prior to the builtin version. But it could be reorganized a bit, I suppose. I was just angling on not cluttering up the lexer proper.
Err... builtin version of what?
I forgot I already separated out the YYLTYPE definition to handle this issue. Never mind; it's kosher already.
Quoted 7 lines
Quoted 5 lines
Note some people are seeing some compilation problems around the struct field "filenum" that I introduced. I don't see that problem, but a coworker here does, so I am now able to reproduce the issue and am working on a fix. Sorry.
Hrm. Which struct did you add the field to?
And the patch I applied yesterday fixed this YYLTYPE up too.
jdl
Hi Jon,
I got the following error while compiling new dtc.
cc -Wall -g -c -o lex.yy.o lex.yy.c
In file included from dtc-lexer.l:37:srcposstack.h: In function `push_input_file':
srcposstack.h:62: warning: implicit declaration of function yyerror' dtc-lexer.l: In function yylex':
dtc-lexer.l:74: structure has no member named filenum' dtc-lexer.l:84: structure has no member named filenum' dtc-lexer.l:92: structure has no member named filenum' dtc-lexer.l:105: structure has no member named filenum' dtc-lexer.l:112: structure has no member named filenum' dtc-lexer.l:127: structure has no member named filenum' dtc-lexer.l:135: structure has no member named filenum' dtc-lexer.l:143: structure has no member named filenum' dtc-lexer.l:151: structure has no member named filenum' dtc-lexer.l:159: structure has no member named filenum' dtc-lexer.l:169: structure has no member named filenum' dtc-lexer.l:177: structure has no member named filenum' dtc-lexer.l:185: structure has no member named filenum' dtc-lexer.l:194: structure has no member named filenum' dtc-lexer.l:205: structure has no member named filenum' dtc-lexer.l:214: structure has no member named filenum' lex.yy.c:734: warning: label `find_rule' defined but not used dtc-lexer.l: At top level:
lex.yy.c:1870: warning: `yy_flex_realloc' defined but not used
make: *** [lex.yy.o] Error 1
- Leo
Quoted 3 lines
-----Original Message-----
From: [email protected] [mailto:[email protected]] On =
Behalf
Of Jon
Quoted 4 lines
Loeliger
Sent: Saturday, March 24, 2007 4:19 AM
To: [email protected]
Subject: [PATCH] DTC: Add support for a C-like #include "file"
mechanism.
Quoted 11 lines
=20
=20
Keeps track of open files in a stack, and assigns a filenum to source positions for each lexical token. Modified error reporting to show source file as well. No policy on file directory basis has been decided. Still handles stdin.
=20
Tested on allarch/powerpc/boot/dtsDTS files =20Signed-off-by Jon Loeliger <[email protected]>
Quoted 9 lines
--- Makefile | 3 +- dtc-lexer.l | 39 ++++++++++++++++- dtc-parser.y | 9 +++- dtc.c | 19 +------- dtc.h | 2 +- srcpos.c | 105 +++++++++++++++++++++++++++++++++++++++++++ srcpos.h | 71 +++++++++++++++++++++++++++++ srcposstack.h | 138
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Quoted 11 lines
treesource.c | 6 ++- 9 files changed, 369 insertions(+), 23 deletions(-) create mode 100644 srcpos.c create mode 100644 srcpos.h create mode 100644 srcposstack.h=20
Makefile +0
-0@@ -3,7 +3,8 @@ CFLAGS =3D -Wall -g
Quoted 4 lines
=20
BISON =3D bison
=20
-DTC_OBJS =3D dtc.o livetree.o flattree.o data.o treesource.o fstree.o =
\
Quoted 68 lines
Makefile +2
-0+DTC_OBJS =3D dtc.o flattree.o fstree.o data.o livetree.o \ + srcpos.o treesource.o \ dtc-parser.tab.o lex.yy.o=20
DEPFILES =3D $(DTC_OBJS:.o=3D.d)dtc-lexer.l +0
-0@@ -20,6 +20,7 @@=20
%option noyywrap nounput yylineno
=20
+%x INCLUDE
%x CELLDATA
%x BYTESTRING
%x MEMRESERVEdtc-lexer.l +0
-0@@ -32,8 +33,9 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])=20
%{
#include "dtc.h"dtc-lexer.l +2
-1- #include "dtc-parser.tab.h" +#include "srcposstack.h" +=20
/*#define LEXDEBUG 1*/
=20dtc-lexer.l +0
-0@@ -49,7 +51,27 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])=20
%%
=20dtc-lexer.l +20
-0+#[ \t]*include BEGIN(INCLUDE); + +<INCLUDE>[ \t]* /* whitespace before file name */ +<INCLUDE>\"[^"\n]*\" { + yytext[strlen(yytext) - 1] =3D 0; + if (!push_input_file(yytext + 1)) { + /* Some unrecoverable error.*/ + exit(1); + } + BEGIN(INITIAL); + } + + +<<EOF>> { + if (!pop_input_file()) { + yyterminate(); + } + } + \"[^"]*\" { + yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("String: %s\n", yytext); yylval.data =3D data_copy_escape_string(yytext+1,@@ -59,6 +81,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
"/memreserve/" {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("Keyword: /memreserve/\n"); BEGIN(MEMRESERVE);@@ -66,6 +89,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<MEMRESERVE>[0-9a-fA-F]+ {
dtc-lexer.l
Quoted 4 lines
dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; if (yyleng > 2*sizeof(yylval.addr)) { fprintf(stderr, "Address value %s too
large\n",
Quoted 56 lines
dtc-lexer.l +0
-0@@ -78,12 +102,14 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<MEMRESERVE>";" {dtc-lexer.l +2
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("/MEMRESERVE\n"); BEGIN(INITIAL); return ';'; } <CELLDATA>[bodh]# { + yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; if (*yytext =3D=3D 'b') yylval.cbase =3D 2;@@ -98,6 +124,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<CELLDATA>[0-9a-fA-F]+ {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; yylval.str =3D strdup(yytext); DPRINT("Cell: '%s'\n", yylval.str);@@ -105,6 +132,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<CELLDATA>">" {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("/CELLDATA\n"); BEGIN(INITIAL);@@ -112,6 +140,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<CELLDATA>\&{REFCHAR}* {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("Ref: %s\n", yytext+1); yylval.str =3D strdup(yytext+1);@@ -119,6 +148,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<BYTESTRING>[0-9a-fA-F]{2} {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; yylval.byte =3D strtol(yytext, NULL, 16); DPRINT("Byte: %02x\n", (int)yylval.byte);@@ -126,6 +156,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
<BYTESTRING>"]" {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("/BYTESTRING\n"); BEGIN(INITIAL);
dtc-lexer.l REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) · line 171
Quoted 6 lines
dtc-lexer.l +1
-0@@ -135,6 +166,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) , { /* Technically this is a valid property name, but we'd rather use it as punctuation, so detect it here in preference */ + yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("Char (propname like): %c (\\x%02x)\n",
yytext[0],
dtc.c void fill_fullpaths(struct node *tree, char
Quoted 81 lines
dtc-lexer.l +0
-0(unsigned)yytext[0]);@@ -142,6 +174,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
{PROPCHAR}+ {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("PropName: %s\n", yytext); yylval.str =3D strdup(yytext);@@ -149,6 +182,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) }=20
{PROPCHAR}+(@{UNITCHAR}+)? {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("NodeName: %s\n", yytext); yylval.str =3D strdup(yytext);@@ -157,6 +191,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@])=20
=20
[a-zA-Z_][a-zA-Z0-9_]*: {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("Label: %s\n", yytext); yylval.str =3D strdup(yytext);@@ -167,6 +202,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) <*>{WS}+ /* eat whitespace */=20
<*>"/*"([^*]|\*+[^*/])*\*+"/" {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; DPRINT("Comment: %s\n", yytext); /* eat comments */@@ -175,6 +211,7 @@ REFCHAR ({PROPCHAR}|{UNITCHAR}|[/@]) <*>"//".*\n /* eat line comments */=20
<*>. {dtc-lexer.l +1
-0+ yylloc.filenum =3D srcpos_filenum; yylloc.first_line =3D yylineno; switch (yytext[0]) { case '<':dtc-parser.y +0
-0@@ -23,6 +23,7 @@=20
%{
#include "dtc.h"
+#include "srcpos.h"
=20
int yylex(void);
voidyyerror(char const *);dtc-parser.y +0
-0@@ -178,7 +179,13 @@ label: DT_LABEL { $$ =3D $1; }=20
void yyerror (char const *s)
{dtc-parser.y +7
-1- fprintf (stderr, "%s at line %d\n", s, yylloc.first_line); + const char *fname =3D srcpos_filename_for_num(yylloc.filenum); + + if (strcmp(fname, "-") =3D=3D 0) + fname =3D "stdin"; + + fprintf(stderr, "%s:%d %s\n", + fname, yylloc.first_line, s); }=20
=20dtc.c +0
-0@@ -19,6 +19,7 @@*/ =20
#include "dtc.h"
+#include "srcpos.h"
=20
char *join_path(char *path, char *name)
{dtc.c +0
-0@@ -61,21 +62,6 @@ void fill_fullpaths(struct node *tree, char
*prefix)
Quoted 4 lines
dtc.c +0
-0fill_fullpaths (child, tree->fullpath); }=20
dtc.c +0
-1-static FILE *dtc_open_file (char *fname)
dtc.c void fill_fullpaths(struct node *tree, char *prefix)
Quoted 10 lines
dtc.c +0
-10-{ -FILE *f; - -if (streq (fname,"-" )) - f =3D stdin; -else - f =3D fopen(fname, "r"); - -if (! f) - die("Couldn't open \"%s\": %s\n", fname,
strerror(errno));
Quoted 42 lines
dtc.c +0
-4- -return f; -} -static void usage (void ) {fprintf (stderr,"Usage:\n" );@@ -166,8 +152,7 @@ int main(int argc, char *argv[]) inform, outform, arg);=20
dtc.c +1
-2if (streq (inform,"dts" )) { - inf =3D dtc_open_file(arg); - bi =3D dt_from_source(inf); + bi =3D dt_from_source(arg); }else if (streq (inform,"fs" )) { bi =3D dt_from_fs(arg); }else if (streq (inform,"dtb" )) {dtc.h +0
-0@@ -223,7 +223,7 @@ struct boot_info *dt_from_blob(FILE *f);/* Tree source */ =20
voiddt_to_source(FILE *f, struct boot_info *bi);dtc.h +1
-1-struct boot_info *dt_from_source (FILE *f); +struct boot_info *dt_from_source (const char *f);=20
/* FS trees */
=20srcpos.c new file +6
-0@@ -0,0 +1,105 @@ +/* +* Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of the
srcpos.c line 16
Quoted 10 lines
srcpos.c new file +10
-0+* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307
srcpos.c line 17
Quoted 1 line
srcpos.c new file +1
-0+*
USA
Quoted 27 lines
srcpos.c new file +27
-0+*/ + +#include "dtc.h" +#include "srcpos.h" + + +/* +* Record the complete unique set of opened file names. +* Primarily used to cache source position file names. +*/ +#define MAX_N_FILE_NAMES (100) + +const char *file_names[MAX_N_FILE_NAMES ]; +static int n_file_names =3D 0; + +/* +* Like yylineno, this is the current open file pos. +*/ + +int srcpos_filenum =3D -1 ; + + + +FILE *dtc_open_file (const char *fname) +{ +FILE *f; +
srcpos.c
Quoted 10 lines
srcpos.c new file +10
-0+if (lookup_file_name (fname,1 ) <0 ) +die ("Too many files opened\n" ); + +if (streq (fname,"-" )) + f =3D stdin; +else + f =3D fopen(fname, "r"); + +if (! f) + die("Couldn't open \"%s\": %s\n", fname,
strerror(errno));
srcpos.c
Quoted 8 lines
srcpos.c new file +8
-0+ +return f; +} + + + +/* +* Locate and optionally add filename fname in the file_names[]
array.
Quoted 55 lines
srcpos.c new file +43
-0+* +* If the filename is currently not in the array and the boolean +* add_it is non-zero, an attempt to add the filename will be made. +* +* Returns; +* Index [0..MAX_N_FILE_NAMES) where the filename is kept +* -1 if the name can not be recorded +*/ + +int lookup_file_name (const char *fname,int add_it) +{ +int i; + + for (i =3D 0; i < n_file_names; i++) { + if (strcmp(file_names[i], fname) =3D=3D 0) +return i; + } + +if (add_it) { +if (n_file_names <MAX_N_FILE_NAMES ) { + file_names[n_file_names] =3D strdup(fname); +return n_file_names++; + } + } + +return -1 ; +} + + +const char *srcpos_filename_for_num (int filenum) +{ + if (0 <=3D filenum && filenum < n_file_names) { +return file_names[filenum]; + } + +return 0 ; +} + + +const char *srcpos_get_filename (void ) +{ +return srcpos_filename_for_num (srcpos_filenum); +}srcpos.h new file +6
-0@@ -0,0 +1,71 @@ +/* +* Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of the
srcpos.h line 16
Quoted 10 lines
srcpos.h new file +10
-0+* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307
srcpos.h line 17
Quoted 1 line
srcpos.h new file +1
-0+*
USA
Quoted 12 lines
srcpos.h new file +12
-0+*/ + +/* +* Augment the standard YYLTYPE with a filenum index into an +* array of all opened filenames. +*/ + +#if ! defined YYLTYPE && ! defined YYLTYPE_IS_DECLARED +typedef struct YYLTYPE { + int first_line; + int first_column; + int last_line;
srcpos.h
Quoted 10 lines
srcpos.h new file +10
-0+int last_column; + int filenum; +}YYLTYPE ; + +#define YYLTYPE_IS_DECLARED 1 +#define YYLTYPE_IS_TRIVIAL 1 +#endif + + +#define YYLLOC_DEFAULT (Current, Rhs,N )
\
srcpos.h line 40
Quoted 1 line
srcpos.h new file +1
-0+ do
\
srcpos.h line 41
Quoted 1 line
srcpos.h new file +1
-0+if (YYID (N ))
\
srcpos.h line 42
Quoted 1 line
srcpos.h new file +1
-0+ {
\
srcpos.h line 43
Quoted 1 line
srcpos.h new file +1
-0+ (Current).first_line =3D YYRHSLOC (Rhs, 1).first_line;
\
srcpos.h line 44
Quoted 1 line
srcpos.h new file +1
-0+ (Current).first_column =3D YYRHSLOC (Rhs, 1).first_column;
\
srcpos.h line 45
Quoted 1 line
srcpos.h new file +1
-0+ (Current).last_line =3D YYRHSLOC (Rhs, N).last_line;
\
srcpos.h line 46
Quoted 1 line
srcpos.h new file +1
-0+ (Current).last_column =3D YYRHSLOC (Rhs, N).last_column;
\
srcpos.h line 47
Quoted 1 line
srcpos.h new file +1
-0+ (Current).filenum =3D YYRHSLOC (Rhs, N).filenum;
\
srcpos.h line 48
Quoted 1 line
srcpos.h new file +1
-0+ }
\
srcpos.h line 49
Quoted 1 line
srcpos.h new file +1
-0+else
\
srcpos.h line 50
Quoted 1 line
srcpos.h new file +1
-0+ {
\
srcpos.h line 51
Quoted 1 line
srcpos.h new file +1
-0+ (Current).first_line =3D (Current).last_line =3D
\
srcpos.h line 52
Quoted 1 line
srcpos.h new file +1
-0+YYRHSLOC (Rhs,0 ).last_line;
\
srcpos.h line 53
Quoted 1 line
srcpos.h new file +1
-0+ (Current).first_column =3D (Current).last_column =3D
\
srcpos.h line 54
Quoted 1 line
srcpos.h new file +1
-0+YYRHSLOC (Rhs,0 ).last_column;
\
srcpos.h line 55
Quoted 1 line
srcpos.h new file +1
-0+ (Current).filenum =3D YYRHSLOC (Rhs, 0).filenum;
\
srcpos.h line 56
Quoted 1 line
srcpos.h new file +1
-0+ }
\
Quoted 27 lines
srcpos.h new file +15
-0+while (YYID (0 )) + + + + +extern int srcpos_filenum; + +extern int push_input_file (const char *filename); +extern int pop_input_file (void ); + +extern FILE *dtc_open_file (const char *fname); +extern int lookup_file_name (const char *fname,int add_it); +extern const char *srcpos_filename_for_num (int filenum); +const char *srcpos_get_filename (void ); +srcposstack.h new file +6
-0@@ -0,0 +1,138 @@ +/* +* Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of the
srcposstack.h line 16
Quoted 10 lines
srcposstack.h new file +10
-0+* License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307
srcposstack.h line 17
Quoted 1 line
srcposstack.h new file +1
-0+*
USA
Quoted 157 lines
srcposstack.h new file +121
-0+*/ + +#include "srcpos.h" + + +/* +* This code should only be included into the lexical analysis. +* It references global context symbols that are only present +* in the generated lex.yy,c file. +*/ + +#ifdef FLEX_SCANNER + + +/* +* Stack of nested include file contexts. +*/ + +struct incl_file { +int filenum; +FILE *file; +YY_BUFFER_STATE yy_prev_buf; +int yy_prev_lineno; +struct incl_file *prev; +}; + +struct incl_file *incl_file_stack; + + +/* +* Detect infinite include recursion. +*/ +#define MAX_INCLUDE_DEPTH (100) + +static int incl_depth =3D 0; + + + +int push_input_file (const char *filename) +{ +FILE *f; +struct incl_file *incl_file; + +if (!filename) { +yyerror ("No include file name given." ); +return 0 ; + } + + if (incl_depth++ >=3D MAX_INCLUDE_DEPTH) { +yyerror ("Includes nested too deeply" ); +return 0 ; + } + + f =3D dtc_open_file(filename); + + incl_file =3D malloc(sizeof(struct incl_file)); +if (!incl_file) { +yyerror ("Can not allocate include file space." ); +return 0 ; + } + +/* +* Save current context. +*/ + incl_file->yy_prev_buf =3D YY_CURRENT_BUFFER; + incl_file->yy_prev_lineno =3D yylineno; + incl_file->filenum =3D srcpos_filenum; + incl_file->file =3D yyin; + incl_file->prev =3D incl_file_stack; + + incl_file_stack =3D incl_file; + +/* +* Establish new context. +*/ + srcpos_filenum =3D lookup_file_name(filename, 0); + yylineno =3D 1; + yyin =3D f; +yy_switch_to_buffer (yy_create_buffer (yyin,YY_BUF_SIZE )); + +return 1 ; +} + + +int pop_input_file (void ) +{ +struct incl_file *incl_file; + + if (incl_file_stack =3D=3D 0) +return 0 ; + +fclose (yyin); + +/* +* Pop. +*/ + --incl_depth; + incl_file =3D incl_file_stack; + incl_file_stack =3D incl_file->prev; + +/* +* Recover old context. +*/ +yy_delete_buffer (YY_CURRENT_BUFFER ); +yy_switch_to_buffer (incl_file->yy_prev_buf); + yylineno =3D incl_file->yy_prev_lineno; + srcpos_filenum =3D incl_file->filenum; + yyin =3D incl_file->file; + +/* +* Free old state. +*/ +free (incl_file); + + if (YY_CURRENT_BUFFER =3D=3D 0) +return 0 ; + +return 1 ; +} + +#endif /* FLEX_SCANNER */ treesource.c +0
-0@@ -19,6 +19,7 @@*/ =20
#include "dtc.h"
+#include "srcpos.h"
=20
extern FILE *yyin;
extern int yyparse(void);treesource.c +0
-0@@ -26,11 +27,12 @@ extern void yyerror(char const *);=20
structboot_info*the_boot_info;
=20treesource.c +1
-1-struct boot_info *dt_from_source (FILE *f) +struct boot_info *dt_from_source (const char *fname) { the_boot_info =3D NULL;=20
treesource.c +2
-1- yyin =3D f; +push_input_file (fname); + if (yyparse() !=3D 0)return NULL ;=20
--
1.5.0.3
=20
=20
=20
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://ozlabs.org/mailman/listinfo/linuxppc-dev