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