Blame srcpos.c

Packit Service 0ee8e1
// SPDX-License-Identifier: GPL-2.0-or-later
Packit 2ad57b
/*
Packit 2ad57b
 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
Packit 2ad57b
 */
Packit 2ad57b
Packit 2ad57b
#define _GNU_SOURCE
Packit 2ad57b
Packit 2ad57b
#include <stdio.h>
Packit 2ad57b
Packit 2ad57b
#include "dtc.h"
Packit 2ad57b
#include "srcpos.h"
Packit 2ad57b
Packit 2ad57b
/* A node in our list of directories to search for source/include files */
Packit 2ad57b
struct search_path {
Packit 2ad57b
	struct search_path *next;	/* next node in list, NULL for end */
Packit 2ad57b
	const char *dirname;		/* name of directory to search */
Packit 2ad57b
};
Packit 2ad57b
Packit 2ad57b
/* This is the list of directories that we search for source files */
Packit 2ad57b
static struct search_path *search_path_head, **search_path_tail;
Packit 2ad57b
Packit Service 0ee8e1
/* Detect infinite include recursion. */
Packit Service 0ee8e1
#define MAX_SRCFILE_DEPTH     (100)
Packit Service 0ee8e1
static int srcfile_depth; /* = 0 */
Packit 2ad57b
Packit 2ad57b
static char *get_dirname(const char *path)
Packit 2ad57b
{
Packit 2ad57b
	const char *slash = strrchr(path, '/');
Packit 2ad57b
Packit 2ad57b
	if (slash) {
Packit 2ad57b
		int len = slash - path;
Packit 2ad57b
		char *dir = xmalloc(len + 1);
Packit 2ad57b
Packit 2ad57b
		memcpy(dir, path, len);
Packit 2ad57b
		dir[len] = '\0';
Packit 2ad57b
		return dir;
Packit 2ad57b
	}
Packit 2ad57b
	return NULL;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
FILE *depfile; /* = NULL */
Packit 2ad57b
struct srcfile_state *current_srcfile; /* = NULL */
Packit Service 0ee8e1
static char *initial_path; /* = NULL */
Packit Service 0ee8e1
static int initial_pathlen; /* = 0 */
Packit Service 0ee8e1
static bool initial_cpp = true;
Packit 2ad57b
Packit Service 0ee8e1
static void set_initial_path(char *fname)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	int i, len = strlen(fname);
Packit 2ad57b
Packit Service 0ee8e1
	xasprintf(&initial_path, "%s", fname);
Packit Service 0ee8e1
	initial_pathlen = 0;
Packit Service 0ee8e1
	for (i = 0; i != len; i++)
Packit Service 0ee8e1
		if (initial_path[i] == '/')
Packit Service 0ee8e1
			initial_pathlen++;
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
static char *shorten_to_initial_path(char *fname)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	char *p1, *p2, *prevslash1 = NULL;
Packit Service 0ee8e1
	int slashes = 0;
Packit Service 0ee8e1
Packit Service 0ee8e1
	for (p1 = fname, p2 = initial_path; *p1 && *p2; p1++, p2++) {
Packit Service 0ee8e1
		if (*p1 != *p2)
Packit Service 0ee8e1
			break;
Packit Service 0ee8e1
		if (*p1 == '/') {
Packit Service 0ee8e1
			prevslash1 = p1;
Packit Service 0ee8e1
			slashes++;
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
	p1 = prevslash1 + 1;
Packit Service 0ee8e1
	if (prevslash1) {
Packit Service 0ee8e1
		int diff = initial_pathlen - slashes, i, j;
Packit Service 0ee8e1
		int restlen = strlen(fname) - (p1 - fname);
Packit Service 0ee8e1
		char *res;
Packit Service 0ee8e1
Packit Service 0ee8e1
		res = xmalloc((3 * diff) + restlen + 1);
Packit Service 0ee8e1
		for (i = 0, j = 0; i != diff; i++) {
Packit Service 0ee8e1
			res[j++] = '.';
Packit Service 0ee8e1
			res[j++] = '.';
Packit Service 0ee8e1
			res[j++] = '/';
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
		strcpy(res + j, p1);
Packit Service 0ee8e1
		return res;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
	return NULL;
Packit Service 0ee8e1
}
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Try to open a file in a given directory.
Packit 2ad57b
 *
Packit 2ad57b
 * If the filename is an absolute path, then dirname is ignored. If it is a
Packit 2ad57b
 * relative path, then we look in that directory for the file.
Packit 2ad57b
 *
Packit 2ad57b
 * @param dirname	Directory to look in, or NULL for none
Packit 2ad57b
 * @param fname		Filename to look for
Packit 2ad57b
 * @param fp		Set to NULL if file did not open
Packit 2ad57b
 * @return allocated filename on success (caller must free), NULL on failure
Packit 2ad57b
 */
Packit 2ad57b
static char *try_open(const char *dirname, const char *fname, FILE **fp)
Packit 2ad57b
{
Packit 2ad57b
	char *fullname;
Packit 2ad57b
Packit 2ad57b
	if (!dirname || fname[0] == '/')
Packit 2ad57b
		fullname = xstrdup(fname);
Packit 2ad57b
	else
Packit 2ad57b
		fullname = join_path(dirname, fname);
Packit 2ad57b
Packit 2ad57b
	*fp = fopen(fullname, "rb");
Packit 2ad57b
	if (!*fp) {
Packit 2ad57b
		free(fullname);
Packit 2ad57b
		fullname = NULL;
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	return fullname;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
/**
Packit 2ad57b
 * Open a file for read access
Packit 2ad57b
 *
Packit 2ad57b
 * If it is a relative filename, we search the full search path for it.
Packit 2ad57b
 *
Packit 2ad57b
 * @param fname	Filename to open
Packit 2ad57b
 * @param fp	Returns pointer to opened FILE, or NULL on failure
Packit 2ad57b
 * @return pointer to allocated filename, which caller must free
Packit 2ad57b
 */
Packit 2ad57b
static char *fopen_any_on_path(const char *fname, FILE **fp)
Packit 2ad57b
{
Packit 2ad57b
	const char *cur_dir = NULL;
Packit 2ad57b
	struct search_path *node;
Packit 2ad57b
	char *fullname;
Packit 2ad57b
Packit 2ad57b
	/* Try current directory first */
Packit 2ad57b
	assert(fp);
Packit 2ad57b
	if (current_srcfile)
Packit 2ad57b
		cur_dir = current_srcfile->dir;
Packit 2ad57b
	fullname = try_open(cur_dir, fname, fp);
Packit 2ad57b
Packit 2ad57b
	/* Failing that, try each search path in turn */
Packit 2ad57b
	for (node = search_path_head; !*fp && node; node = node->next)
Packit 2ad57b
		fullname = try_open(node->dirname, fname, fp);
Packit 2ad57b
Packit 2ad57b
	return fullname;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
FILE *srcfile_relative_open(const char *fname, char **fullnamep)
Packit 2ad57b
{
Packit 2ad57b
	FILE *f;
Packit 2ad57b
	char *fullname;
Packit 2ad57b
Packit 2ad57b
	if (streq(fname, "-")) {
Packit 2ad57b
		f = stdin;
Packit 2ad57b
		fullname = xstrdup("<stdin>");
Packit 2ad57b
	} else {
Packit 2ad57b
		fullname = fopen_any_on_path(fname, &f);
Packit 2ad57b
		if (!f)
Packit 2ad57b
			die("Couldn't open \"%s\": %s\n", fname,
Packit 2ad57b
			    strerror(errno));
Packit 2ad57b
	}
Packit 2ad57b
Packit 2ad57b
	if (depfile)
Packit 2ad57b
		fprintf(depfile, " %s", fullname);
Packit 2ad57b
Packit 2ad57b
	if (fullnamep)
Packit 2ad57b
		*fullnamep = fullname;
Packit 2ad57b
	else
Packit 2ad57b
		free(fullname);
Packit 2ad57b
Packit 2ad57b
	return f;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
void srcfile_push(const char *fname)
Packit 2ad57b
{
Packit 2ad57b
	struct srcfile_state *srcfile;
Packit 2ad57b
Packit 2ad57b
	if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
Packit 2ad57b
		die("Includes nested too deeply");
Packit 2ad57b
Packit 2ad57b
	srcfile = xmalloc(sizeof(*srcfile));
Packit 2ad57b
Packit 2ad57b
	srcfile->f = srcfile_relative_open(fname, &srcfile->name);
Packit 2ad57b
	srcfile->dir = get_dirname(srcfile->name);
Packit 2ad57b
	srcfile->prev = current_srcfile;
Packit 2ad57b
Packit 2ad57b
	srcfile->lineno = 1;
Packit 2ad57b
	srcfile->colno = 1;
Packit 2ad57b
Packit 2ad57b
	current_srcfile = srcfile;
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (srcfile_depth == 1)
Packit Service 0ee8e1
		set_initial_path(srcfile->name);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
bool srcfile_pop(void)
Packit 2ad57b
{
Packit 2ad57b
	struct srcfile_state *srcfile = current_srcfile;
Packit 2ad57b
Packit 2ad57b
	assert(srcfile);
Packit 2ad57b
Packit 2ad57b
	current_srcfile = srcfile->prev;
Packit 2ad57b
Packit 2ad57b
	if (fclose(srcfile->f))
Packit 2ad57b
		die("Error closing \"%s\": %s\n", srcfile->name,
Packit 2ad57b
		    strerror(errno));
Packit 2ad57b
Packit 2ad57b
	/* FIXME: We allow the srcfile_state structure to leak,
Packit 2ad57b
	 * because it could still be referenced from a location
Packit 2ad57b
	 * variable being carried through the parser somewhere.  To
Packit 2ad57b
	 * fix this we could either allocate all the files from a
Packit 2ad57b
	 * table, or use a pool allocator. */
Packit 2ad57b
Packit 2ad57b
	return current_srcfile ? true : false;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
void srcfile_add_search_path(const char *dirname)
Packit 2ad57b
{
Packit 2ad57b
	struct search_path *node;
Packit 2ad57b
Packit 2ad57b
	/* Create the node */
Packit 2ad57b
	node = xmalloc(sizeof(*node));
Packit 2ad57b
	node->next = NULL;
Packit 2ad57b
	node->dirname = xstrdup(dirname);
Packit 2ad57b
Packit 2ad57b
	/* Add to the end of our list */
Packit 2ad57b
	if (search_path_tail)
Packit 2ad57b
		*search_path_tail = node;
Packit 2ad57b
	else
Packit 2ad57b
		search_path_head = node;
Packit 2ad57b
	search_path_tail = &node->next;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
void srcpos_update(struct srcpos *pos, const char *text, int len)
Packit 2ad57b
{
Packit 2ad57b
	int i;
Packit 2ad57b
Packit 2ad57b
	pos->file = current_srcfile;
Packit 2ad57b
Packit 2ad57b
	pos->first_line = current_srcfile->lineno;
Packit 2ad57b
	pos->first_column = current_srcfile->colno;
Packit 2ad57b
Packit 2ad57b
	for (i = 0; i < len; i++)
Packit 2ad57b
		if (text[i] == '\n') {
Packit 2ad57b
			current_srcfile->lineno++;
Packit 2ad57b
			current_srcfile->colno = 1;
Packit 2ad57b
		} else {
Packit 2ad57b
			current_srcfile->colno++;
Packit 2ad57b
		}
Packit 2ad57b
Packit 2ad57b
	pos->last_line = current_srcfile->lineno;
Packit 2ad57b
	pos->last_column = current_srcfile->colno;
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
struct srcpos *
Packit 2ad57b
srcpos_copy(struct srcpos *pos)
Packit 2ad57b
{
Packit 2ad57b
	struct srcpos *pos_new;
Packit Service 0ee8e1
	struct srcfile_state *srcfile_state;
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (!pos)
Packit Service 0ee8e1
		return NULL;
Packit 2ad57b
Packit 2ad57b
	pos_new = xmalloc(sizeof(struct srcpos));
Packit Service 0ee8e1
	assert(pos->next == NULL);
Packit 2ad57b
	memcpy(pos_new, pos, sizeof(struct srcpos));
Packit 2ad57b
Packit Service 0ee8e1
	/* allocate without free */
Packit Service 0ee8e1
	srcfile_state = xmalloc(sizeof(struct srcfile_state));
Packit Service 0ee8e1
	memcpy(srcfile_state, pos->file, sizeof(struct srcfile_state));
Packit Service 0ee8e1
	pos_new->file = srcfile_state;
Packit Service 0ee8e1
Packit 2ad57b
	return pos_new;
Packit 2ad57b
}
Packit 2ad57b
Packit Service 0ee8e1
struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	struct srcpos *p;
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (!pos)
Packit Service 0ee8e1
		return newtail;
Packit Service 0ee8e1
Packit Service 0ee8e1
	for (p = pos; p->next != NULL; p = p->next);
Packit Service 0ee8e1
	p->next = newtail;
Packit Service 0ee8e1
	return pos;
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit 2ad57b
char *
Packit 2ad57b
srcpos_string(struct srcpos *pos)
Packit 2ad57b
{
Packit 2ad57b
	const char *fname = "<no-file>";
Packit 2ad57b
	char *pos_str;
Packit 2ad57b
Packit 2ad57b
	if (pos->file && pos->file->name)
Packit 2ad57b
		fname = pos->file->name;
Packit 2ad57b
Packit 2ad57b
Packit 2ad57b
	if (pos->first_line != pos->last_line)
Packit 2ad57b
		xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
Packit 2ad57b
			  pos->first_line, pos->first_column,
Packit 2ad57b
			  pos->last_line, pos->last_column);
Packit 2ad57b
	else if (pos->first_column != pos->last_column)
Packit 2ad57b
		xasprintf(&pos_str, "%s:%d.%d-%d", fname,
Packit 2ad57b
			  pos->first_line, pos->first_column,
Packit 2ad57b
			  pos->last_column);
Packit 2ad57b
	else
Packit 2ad57b
		xasprintf(&pos_str, "%s:%d.%d", fname,
Packit 2ad57b
			  pos->first_line, pos->first_column);
Packit 2ad57b
Packit 2ad57b
	return pos_str;
Packit 2ad57b
}
Packit 2ad57b
Packit Service 0ee8e1
static char *
Packit Service 0ee8e1
srcpos_string_comment(struct srcpos *pos, bool first_line, int level)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	char *pos_str, *fname, *first, *rest;
Packit Service 0ee8e1
	bool fresh_fname = false;
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (!pos) {
Packit Service 0ee8e1
		if (level > 1) {
Packit Service 0ee8e1
			xasprintf(&pos_str, "<no-file>:<no-line>");
Packit Service 0ee8e1
			return pos_str;
Packit Service 0ee8e1
		} else {
Packit Service 0ee8e1
			return NULL;
Packit Service 0ee8e1
		}
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (!pos->file)
Packit Service 0ee8e1
		fname = "<no-file>";
Packit Service 0ee8e1
	else if (!pos->file->name)
Packit Service 0ee8e1
		fname = "<no-filename>";
Packit Service 0ee8e1
	else if (level > 1)
Packit Service 0ee8e1
		fname = pos->file->name;
Packit Service 0ee8e1
	else {
Packit Service 0ee8e1
		fname = shorten_to_initial_path(pos->file->name);
Packit Service 0ee8e1
		if (fname)
Packit Service 0ee8e1
			fresh_fname = true;
Packit Service 0ee8e1
		else
Packit Service 0ee8e1
			fname = pos->file->name;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (level > 1)
Packit Service 0ee8e1
		xasprintf(&first, "%s:%d:%d-%d:%d", fname,
Packit Service 0ee8e1
			  pos->first_line, pos->first_column,
Packit Service 0ee8e1
			  pos->last_line, pos->last_column);
Packit Service 0ee8e1
	else
Packit Service 0ee8e1
		xasprintf(&first, "%s:%d", fname,
Packit Service 0ee8e1
			  first_line ? pos->first_line : pos->last_line);
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (fresh_fname)
Packit Service 0ee8e1
		free(fname);
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (pos->next != NULL) {
Packit Service 0ee8e1
		rest = srcpos_string_comment(pos->next, first_line, level);
Packit Service 0ee8e1
		xasprintf(&pos_str, "%s, %s", first, rest);
Packit Service 0ee8e1
		free(first);
Packit Service 0ee8e1
		free(rest);
Packit Service 0ee8e1
	} else {
Packit Service 0ee8e1
		pos_str = first;
Packit Service 0ee8e1
	}
Packit Service 0ee8e1
Packit Service 0ee8e1
	return pos_str;
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
char *srcpos_string_first(struct srcpos *pos, int level)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	return srcpos_string_comment(pos, true, level);
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit Service 0ee8e1
char *srcpos_string_last(struct srcpos *pos, int level)
Packit Service 0ee8e1
{
Packit Service 0ee8e1
	return srcpos_string_comment(pos, false, level);
Packit Service 0ee8e1
}
Packit Service 0ee8e1
Packit 2ad57b
void srcpos_verror(struct srcpos *pos, const char *prefix,
Packit 2ad57b
		   const char *fmt, va_list va)
Packit 2ad57b
{
Packit 2ad57b
	char *srcstr;
Packit 2ad57b
Packit 2ad57b
	srcstr = srcpos_string(pos);
Packit 2ad57b
Packit 2ad57b
	fprintf(stderr, "%s: %s ", prefix, srcstr);
Packit 2ad57b
	vfprintf(stderr, fmt, va);
Packit 2ad57b
	fprintf(stderr, "\n");
Packit 2ad57b
Packit 2ad57b
	free(srcstr);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
void srcpos_error(struct srcpos *pos, const char *prefix,
Packit 2ad57b
		  const char *fmt, ...)
Packit 2ad57b
{
Packit 2ad57b
	va_list va;
Packit 2ad57b
Packit 2ad57b
	va_start(va, fmt);
Packit 2ad57b
	srcpos_verror(pos, prefix, fmt, va);
Packit 2ad57b
	va_end(va);
Packit 2ad57b
}
Packit 2ad57b
Packit 2ad57b
void srcpos_set_line(char *f, int l)
Packit 2ad57b
{
Packit 2ad57b
	current_srcfile->name = f;
Packit 2ad57b
	current_srcfile->lineno = l;
Packit Service 0ee8e1
Packit Service 0ee8e1
	if (initial_cpp) {
Packit Service 0ee8e1
		initial_cpp = false;
Packit Service 0ee8e1
		set_initial_path(f);
Packit Service 0ee8e1
	}
Packit 2ad57b
}