xref: /openbmc/linux/scripts/dtc/srcpos.h (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*1a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
29fffb55fSDavid Gibson /*
39fffb55fSDavid Gibson  * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
49fffb55fSDavid Gibson  */
59fffb55fSDavid Gibson 
69130ba88SRob Herring #ifndef SRCPOS_H
79130ba88SRob Herring #define SRCPOS_H
89fffb55fSDavid Gibson 
99fffb55fSDavid Gibson #include <stdio.h>
1047605971SRob Herring #include <stdbool.h>
1189d12310SRob Herring #include "util.h"
129fffb55fSDavid Gibson 
13658f29a5SJohn Bonesio struct srcfile_state {
14658f29a5SJohn Bonesio 	FILE *f;
15658f29a5SJohn Bonesio 	char *name;
169fffb55fSDavid Gibson 	char *dir;
17658f29a5SJohn Bonesio 	int lineno, colno;
18658f29a5SJohn Bonesio 	struct srcfile_state *prev;
199fffb55fSDavid Gibson };
209fffb55fSDavid Gibson 
21136ec204SStephen Warren extern FILE *depfile; /* = NULL */
22658f29a5SJohn Bonesio extern struct srcfile_state *current_srcfile; /* = NULL */
23658f29a5SJohn Bonesio 
24cd296721SStephen Warren /**
25cd296721SStephen Warren  * Open a source file.
26cd296721SStephen Warren  *
27cd296721SStephen Warren  * If the source file is a relative pathname, then it is searched for in the
28cd296721SStephen Warren  * current directory (the directory of the last source file read) and after
29cd296721SStephen Warren  * that in the search path.
30cd296721SStephen Warren  *
31cd296721SStephen Warren  * We work through the search path in order from the first path specified to
32cd296721SStephen Warren  * the last.
33cd296721SStephen Warren  *
34cd296721SStephen Warren  * If the file is not found, then this function does not return, but calls
35cd296721SStephen Warren  * die().
36cd296721SStephen Warren  *
37cd296721SStephen Warren  * @param fname		Filename to search
38cd296721SStephen Warren  * @param fullnamep	If non-NULL, it is set to the allocated filename of the
39cd296721SStephen Warren  *			file that was opened. The caller is then responsible
40cd296721SStephen Warren  *			for freeing the pointer.
41cd296721SStephen Warren  * @return pointer to opened FILE
42cd296721SStephen Warren  */
43658f29a5SJohn Bonesio FILE *srcfile_relative_open(const char *fname, char **fullnamep);
44cd296721SStephen Warren 
45658f29a5SJohn Bonesio void srcfile_push(const char *fname);
4647605971SRob Herring bool srcfile_pop(void);
47658f29a5SJohn Bonesio 
48cd296721SStephen Warren /**
49cd296721SStephen Warren  * Add a new directory to the search path for input files
50cd296721SStephen Warren  *
51cd296721SStephen Warren  * The new path is added at the end of the list.
52cd296721SStephen Warren  *
53cd296721SStephen Warren  * @param dirname	Directory to add
54cd296721SStephen Warren  */
55cd296721SStephen Warren void srcfile_add_search_path(const char *dirname);
56cd296721SStephen Warren 
57658f29a5SJohn Bonesio struct srcpos {
589fffb55fSDavid Gibson     int first_line;
599fffb55fSDavid Gibson     int first_column;
609fffb55fSDavid Gibson     int last_line;
619fffb55fSDavid Gibson     int last_column;
62658f29a5SJohn Bonesio     struct srcfile_state *file;
63c2e7075cSRob Herring     struct srcpos *next;
64658f29a5SJohn Bonesio };
659fffb55fSDavid Gibson 
66658f29a5SJohn Bonesio #define YYLTYPE struct srcpos
679fffb55fSDavid Gibson 
689fffb55fSDavid Gibson #define YYLLOC_DEFAULT(Current, Rhs, N)						\
69658f29a5SJohn Bonesio 	do {									\
70658f29a5SJohn Bonesio 		if (N) {							\
719fffb55fSDavid Gibson 			(Current).first_line = YYRHSLOC(Rhs, 1).first_line;	\
729fffb55fSDavid Gibson 			(Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
739fffb55fSDavid Gibson 			(Current).last_line = YYRHSLOC(Rhs, N).last_line;	\
749fffb55fSDavid Gibson 			(Current).last_column  = YYRHSLOC (Rhs, N).last_column;	\
759fffb55fSDavid Gibson 			(Current).file = YYRHSLOC(Rhs, N).file;			\
76658f29a5SJohn Bonesio 		} else {							\
779fffb55fSDavid Gibson 			(Current).first_line = (Current).last_line =		\
789fffb55fSDavid Gibson 				YYRHSLOC(Rhs, 0).last_line;			\
799fffb55fSDavid Gibson 			(Current).first_column = (Current).last_column =	\
809fffb55fSDavid Gibson 				YYRHSLOC(Rhs, 0).last_column;			\
819fffb55fSDavid Gibson 			(Current).file = YYRHSLOC (Rhs, 0).file;		\
829fffb55fSDavid Gibson 		}								\
83c2e7075cSRob Herring 		(Current).next = NULL;						\
84658f29a5SJohn Bonesio 	} while (0)
859fffb55fSDavid Gibson 
869fffb55fSDavid Gibson 
87658f29a5SJohn Bonesio extern void srcpos_update(struct srcpos *pos, const char *text, int len);
88658f29a5SJohn Bonesio extern struct srcpos *srcpos_copy(struct srcpos *pos);
89c2e7075cSRob Herring extern struct srcpos *srcpos_extend(struct srcpos *new_srcpos,
90c2e7075cSRob Herring 				    struct srcpos *old_srcpos);
91658f29a5SJohn Bonesio extern char *srcpos_string(struct srcpos *pos);
92c2e7075cSRob Herring extern char *srcpos_string_first(struct srcpos *pos, int level);
93c2e7075cSRob Herring extern char *srcpos_string_last(struct srcpos *pos, int level);
94c2e7075cSRob Herring 
959fffb55fSDavid Gibson 
9689d12310SRob Herring extern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix,
9789d12310SRob Herring 					const char *fmt, va_list va);
9889d12310SRob Herring extern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix,
9989d12310SRob Herring 				      const char *fmt, ...);
1009fffb55fSDavid Gibson 
101cd296721SStephen Warren extern void srcpos_set_line(char *f, int l);
102cd296721SStephen Warren 
1039130ba88SRob Herring #endif /* SRCPOS_H */
104