xref: /openbmc/linux/scripts/dtc/srcpos.h (revision 62975d27)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
4  */
5 
6 #ifndef SRCPOS_H
7 #define SRCPOS_H
8 
9 #include <stdio.h>
10 #include <stdbool.h>
11 #include "util.h"
12 
13 struct srcfile_state {
14 	FILE *f;
15 	char *name;
16 	char *dir;
17 	int lineno, colno;
18 	struct srcfile_state *prev;
19 };
20 
21 extern FILE *depfile; /* = NULL */
22 extern struct srcfile_state *current_srcfile; /* = NULL */
23 
24 /**
25  * Open a source file.
26  *
27  * If the source file is a relative pathname, then it is searched for in the
28  * current directory (the directory of the last source file read) and after
29  * that in the search path.
30  *
31  * We work through the search path in order from the first path specified to
32  * the last.
33  *
34  * If the file is not found, then this function does not return, but calls
35  * die().
36  *
37  * @param fname		Filename to search
38  * @param fullnamep	If non-NULL, it is set to the allocated filename of the
39  *			file that was opened. The caller is then responsible
40  *			for freeing the pointer.
41  * @return pointer to opened FILE
42  */
43 FILE *srcfile_relative_open(const char *fname, char **fullnamep);
44 
45 void srcfile_push(const char *fname);
46 bool srcfile_pop(void);
47 
48 /**
49  * Add a new directory to the search path for input files
50  *
51  * The new path is added at the end of the list.
52  *
53  * @param dirname	Directory to add
54  */
55 void srcfile_add_search_path(const char *dirname);
56 
57 struct srcpos {
58     int first_line;
59     int first_column;
60     int last_line;
61     int last_column;
62     struct srcfile_state *file;
63     struct srcpos *next;
64 };
65 
66 #define YYLTYPE struct srcpos
67 
68 #define YYLLOC_DEFAULT(Current, Rhs, N)						\
69 	do {									\
70 		if (N) {							\
71 			(Current).first_line = YYRHSLOC(Rhs, 1).first_line;	\
72 			(Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
73 			(Current).last_line = YYRHSLOC(Rhs, N).last_line;	\
74 			(Current).last_column  = YYRHSLOC (Rhs, N).last_column;	\
75 			(Current).file = YYRHSLOC(Rhs, N).file;			\
76 		} else {							\
77 			(Current).first_line = (Current).last_line =		\
78 				YYRHSLOC(Rhs, 0).last_line;			\
79 			(Current).first_column = (Current).last_column =	\
80 				YYRHSLOC(Rhs, 0).last_column;			\
81 			(Current).file = YYRHSLOC (Rhs, 0).file;		\
82 		}								\
83 		(Current).next = NULL;						\
84 	} while (0)
85 
86 
87 extern void srcpos_update(struct srcpos *pos, const char *text, int len);
88 extern struct srcpos *srcpos_copy(struct srcpos *pos);
89 extern struct srcpos *srcpos_extend(struct srcpos *new_srcpos,
90 				    struct srcpos *old_srcpos);
91 extern char *srcpos_string(struct srcpos *pos);
92 extern char *srcpos_string_first(struct srcpos *pos, int level);
93 extern char *srcpos_string_last(struct srcpos *pos, int level);
94 
95 
96 extern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix,
97 					const char *fmt, va_list va);
98 extern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix,
99 				      const char *fmt, ...);
100 
101 extern void srcpos_set_line(char *f, int l);
102 
103 #endif /* SRCPOS_H */
104