18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#ifndef SRCPOS_H
78c2ecf20Sopenharmony_ci#define SRCPOS_H
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <stdio.h>
108c2ecf20Sopenharmony_ci#include <stdbool.h>
118c2ecf20Sopenharmony_ci#include "util.h"
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_cistruct srcfile_state {
148c2ecf20Sopenharmony_ci	FILE *f;
158c2ecf20Sopenharmony_ci	char *name;
168c2ecf20Sopenharmony_ci	char *dir;
178c2ecf20Sopenharmony_ci	int lineno, colno;
188c2ecf20Sopenharmony_ci	struct srcfile_state *prev;
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciextern FILE *depfile; /* = NULL */
228c2ecf20Sopenharmony_ciextern struct srcfile_state *current_srcfile; /* = NULL */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/**
258c2ecf20Sopenharmony_ci * Open a source file.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * If the source file is a relative pathname, then it is searched for in the
288c2ecf20Sopenharmony_ci * current directory (the directory of the last source file read) and after
298c2ecf20Sopenharmony_ci * that in the search path.
308c2ecf20Sopenharmony_ci *
318c2ecf20Sopenharmony_ci * We work through the search path in order from the first path specified to
328c2ecf20Sopenharmony_ci * the last.
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * If the file is not found, then this function does not return, but calls
358c2ecf20Sopenharmony_ci * die().
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * @param fname		Filename to search
388c2ecf20Sopenharmony_ci * @param fullnamep	If non-NULL, it is set to the allocated filename of the
398c2ecf20Sopenharmony_ci *			file that was opened. The caller is then responsible
408c2ecf20Sopenharmony_ci *			for freeing the pointer.
418c2ecf20Sopenharmony_ci * @return pointer to opened FILE
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ciFILE *srcfile_relative_open(const char *fname, char **fullnamep);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_civoid srcfile_push(const char *fname);
468c2ecf20Sopenharmony_cibool srcfile_pop(void);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/**
498c2ecf20Sopenharmony_ci * Add a new directory to the search path for input files
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * The new path is added at the end of the list.
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * @param dirname	Directory to add
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_civoid srcfile_add_search_path(const char *dirname);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct srcpos {
588c2ecf20Sopenharmony_ci    int first_line;
598c2ecf20Sopenharmony_ci    int first_column;
608c2ecf20Sopenharmony_ci    int last_line;
618c2ecf20Sopenharmony_ci    int last_column;
628c2ecf20Sopenharmony_ci    struct srcfile_state *file;
638c2ecf20Sopenharmony_ci    struct srcpos *next;
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define YYLTYPE struct srcpos
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define YYLLOC_DEFAULT(Current, Rhs, N)						\
698c2ecf20Sopenharmony_ci	do {									\
708c2ecf20Sopenharmony_ci		if (N) {							\
718c2ecf20Sopenharmony_ci			(Current).first_line = YYRHSLOC(Rhs, 1).first_line;	\
728c2ecf20Sopenharmony_ci			(Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
738c2ecf20Sopenharmony_ci			(Current).last_line = YYRHSLOC(Rhs, N).last_line;	\
748c2ecf20Sopenharmony_ci			(Current).last_column  = YYRHSLOC (Rhs, N).last_column;	\
758c2ecf20Sopenharmony_ci			(Current).file = YYRHSLOC(Rhs, N).file;			\
768c2ecf20Sopenharmony_ci		} else {							\
778c2ecf20Sopenharmony_ci			(Current).first_line = (Current).last_line =		\
788c2ecf20Sopenharmony_ci				YYRHSLOC(Rhs, 0).last_line;			\
798c2ecf20Sopenharmony_ci			(Current).first_column = (Current).last_column =	\
808c2ecf20Sopenharmony_ci				YYRHSLOC(Rhs, 0).last_column;			\
818c2ecf20Sopenharmony_ci			(Current).file = YYRHSLOC (Rhs, 0).file;		\
828c2ecf20Sopenharmony_ci		}								\
838c2ecf20Sopenharmony_ci		(Current).next = NULL;						\
848c2ecf20Sopenharmony_ci	} while (0)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciextern void srcpos_update(struct srcpos *pos, const char *text, int len);
888c2ecf20Sopenharmony_ciextern struct srcpos *srcpos_copy(struct srcpos *pos);
898c2ecf20Sopenharmony_ciextern struct srcpos *srcpos_extend(struct srcpos *new_srcpos,
908c2ecf20Sopenharmony_ci				    struct srcpos *old_srcpos);
918c2ecf20Sopenharmony_ciextern char *srcpos_string(struct srcpos *pos);
928c2ecf20Sopenharmony_ciextern char *srcpos_string_first(struct srcpos *pos, int level);
938c2ecf20Sopenharmony_ciextern char *srcpos_string_last(struct srcpos *pos, int level);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciextern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix,
978c2ecf20Sopenharmony_ci					const char *fmt, va_list va);
988c2ecf20Sopenharmony_ciextern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix,
998c2ecf20Sopenharmony_ci				      const char *fmt, ...);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciextern void srcpos_set_line(char *f, int l);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#endif /* SRCPOS_H */
104