162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef SRCPOS_H
762306a36Sopenharmony_ci#define SRCPOS_H
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#include <stdio.h>
1062306a36Sopenharmony_ci#include <stdbool.h>
1162306a36Sopenharmony_ci#include "util.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_cistruct srcfile_state {
1462306a36Sopenharmony_ci	FILE *f;
1562306a36Sopenharmony_ci	char *name;
1662306a36Sopenharmony_ci	char *dir;
1762306a36Sopenharmony_ci	int lineno, colno;
1862306a36Sopenharmony_ci	struct srcfile_state *prev;
1962306a36Sopenharmony_ci};
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ciextern FILE *depfile; /* = NULL */
2262306a36Sopenharmony_ciextern struct srcfile_state *current_srcfile; /* = NULL */
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/**
2562306a36Sopenharmony_ci * Open a source file.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * If the source file is a relative pathname, then it is searched for in the
2862306a36Sopenharmony_ci * current directory (the directory of the last source file read) and after
2962306a36Sopenharmony_ci * that in the search path.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * We work through the search path in order from the first path specified to
3262306a36Sopenharmony_ci * the last.
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * If the file is not found, then this function does not return, but calls
3562306a36Sopenharmony_ci * die().
3662306a36Sopenharmony_ci *
3762306a36Sopenharmony_ci * @param fname		Filename to search
3862306a36Sopenharmony_ci * @param fullnamep	If non-NULL, it is set to the allocated filename of the
3962306a36Sopenharmony_ci *			file that was opened. The caller is then responsible
4062306a36Sopenharmony_ci *			for freeing the pointer.
4162306a36Sopenharmony_ci * @return pointer to opened FILE
4262306a36Sopenharmony_ci */
4362306a36Sopenharmony_ciFILE *srcfile_relative_open(const char *fname, char **fullnamep);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_civoid srcfile_push(const char *fname);
4662306a36Sopenharmony_cibool srcfile_pop(void);
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ci/**
4962306a36Sopenharmony_ci * Add a new directory to the search path for input files
5062306a36Sopenharmony_ci *
5162306a36Sopenharmony_ci * The new path is added at the end of the list.
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * @param dirname	Directory to add
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_civoid srcfile_add_search_path(const char *dirname);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistruct srcpos {
5862306a36Sopenharmony_ci    int first_line;
5962306a36Sopenharmony_ci    int first_column;
6062306a36Sopenharmony_ci    int last_line;
6162306a36Sopenharmony_ci    int last_column;
6262306a36Sopenharmony_ci    struct srcfile_state *file;
6362306a36Sopenharmony_ci    struct srcpos *next;
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define YYLTYPE struct srcpos
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define YYLLOC_DEFAULT(Current, Rhs, N)						\
6962306a36Sopenharmony_ci	do {									\
7062306a36Sopenharmony_ci		if (N) {							\
7162306a36Sopenharmony_ci			(Current).first_line = YYRHSLOC(Rhs, 1).first_line;	\
7262306a36Sopenharmony_ci			(Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
7362306a36Sopenharmony_ci			(Current).last_line = YYRHSLOC(Rhs, N).last_line;	\
7462306a36Sopenharmony_ci			(Current).last_column  = YYRHSLOC (Rhs, N).last_column;	\
7562306a36Sopenharmony_ci			(Current).file = YYRHSLOC(Rhs, N).file;			\
7662306a36Sopenharmony_ci		} else {							\
7762306a36Sopenharmony_ci			(Current).first_line = (Current).last_line =		\
7862306a36Sopenharmony_ci				YYRHSLOC(Rhs, 0).last_line;			\
7962306a36Sopenharmony_ci			(Current).first_column = (Current).last_column =	\
8062306a36Sopenharmony_ci				YYRHSLOC(Rhs, 0).last_column;			\
8162306a36Sopenharmony_ci			(Current).file = YYRHSLOC (Rhs, 0).file;		\
8262306a36Sopenharmony_ci		}								\
8362306a36Sopenharmony_ci		(Current).next = NULL;						\
8462306a36Sopenharmony_ci	} while (0)
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ciextern void srcpos_update(struct srcpos *pos, const char *text, int len);
8862306a36Sopenharmony_ciextern struct srcpos *srcpos_copy(struct srcpos *pos);
8962306a36Sopenharmony_ciextern struct srcpos *srcpos_extend(struct srcpos *new_srcpos,
9062306a36Sopenharmony_ci				    struct srcpos *old_srcpos);
9162306a36Sopenharmony_ciextern char *srcpos_string(struct srcpos *pos);
9262306a36Sopenharmony_ciextern char *srcpos_string_first(struct srcpos *pos, int level);
9362306a36Sopenharmony_ciextern char *srcpos_string_last(struct srcpos *pos, int level);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciextern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix,
9762306a36Sopenharmony_ci					const char *fmt, va_list va);
9862306a36Sopenharmony_ciextern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix,
9962306a36Sopenharmony_ci				      const char *fmt, ...);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ciextern void srcpos_set_line(char *f, int l);
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci#endif /* SRCPOS_H */
104