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