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