Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "dtc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "srcpos.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) /* A node in our list of directories to search for source/include files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) struct search_path {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 	struct search_path *next;	/* next node in list, NULL for end */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 	const char *dirname;		/* name of directory to search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* This is the list of directories that we search for source files */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static struct search_path *search_path_head, **search_path_tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Detect infinite include recursion. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define MAX_SRCFILE_DEPTH     (200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int srcfile_depth; /* = 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static char *get_dirname(const char *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	const char *slash = strrchr(path, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	if (slash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		int len = slash - path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 		char *dir = xmalloc(len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		memcpy(dir, path, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		dir[len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		return dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) FILE *depfile; /* = NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) struct srcfile_state *current_srcfile; /* = NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static char *initial_path; /* = NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static int initial_pathlen; /* = 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static bool initial_cpp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static void set_initial_path(char *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int i, len = strlen(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	xasprintf(&initial_path, "%s", fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	initial_pathlen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	for (i = 0; i != len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		if (initial_path[i] == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 			initial_pathlen++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static char *shorten_to_initial_path(char *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	char *p1, *p2, *prevslash1 = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int slashes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	for (p1 = fname, p2 = initial_path; *p1 && *p2; p1++, p2++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		if (*p1 != *p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (*p1 == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			prevslash1 = p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			slashes++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	p1 = prevslash1 + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (prevslash1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		int diff = initial_pathlen - slashes, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		int restlen = strlen(fname) - (p1 - fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		char *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		res = xmalloc((3 * diff) + restlen + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		for (i = 0, j = 0; i != diff; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 			res[j++] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			res[j++] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			res[j++] = '/';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		strcpy(res + j, p1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * Try to open a file in a given directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92)  * If the filename is an absolute path, then dirname is ignored. If it is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93)  * relative path, then we look in that directory for the file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * @param dirname	Directory to look in, or NULL for none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  * @param fname		Filename to look for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * @param fp		Set to NULL if file did not open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @return allocated filename on success (caller must free), NULL on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static char *try_open(const char *dirname, const char *fname, FILE **fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	char *fullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (!dirname || fname[0] == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		fullname = xstrdup(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		fullname = join_path(dirname, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	*fp = fopen(fullname, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (!*fp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		free(fullname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		fullname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	return fullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * Open a file for read access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  * If it is a relative filename, we search the full search path for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  * @param fname	Filename to open
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)  * @param fp	Returns pointer to opened FILE, or NULL on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * @return pointer to allocated filename, which caller must free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static char *fopen_any_on_path(const char *fname, FILE **fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	const char *cur_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct search_path *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	char *fullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/* Try current directory first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	assert(fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (current_srcfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		cur_dir = current_srcfile->dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	fullname = try_open(cur_dir, fname, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	/* Failing that, try each search path in turn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	for (node = search_path_head; !*fp && node; node = node->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		fullname = try_open(node->dirname, fname, fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	return fullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) FILE *srcfile_relative_open(const char *fname, char **fullnamep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	char *fullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (streq(fname, "-")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		f = stdin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		fullname = xstrdup("<stdin>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		fullname = fopen_any_on_path(fname, &f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			die("Couldn't open \"%s\": %s\n", fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			    strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	if (depfile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		fprintf(depfile, " %s", fullname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (fullnamep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		*fullnamep = fullname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		free(fullname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	return f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) void srcfile_push(const char *fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	struct srcfile_state *srcfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (srcfile_depth++ >= MAX_SRCFILE_DEPTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		die("Includes nested too deeply");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	srcfile = xmalloc(sizeof(*srcfile));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	srcfile->f = srcfile_relative_open(fname, &srcfile->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	srcfile->dir = get_dirname(srcfile->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	srcfile->prev = current_srcfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	srcfile->lineno = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	srcfile->colno = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	current_srcfile = srcfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (srcfile_depth == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		set_initial_path(srcfile->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) bool srcfile_pop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	struct srcfile_state *srcfile = current_srcfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	assert(srcfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	current_srcfile = srcfile->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (fclose(srcfile->f))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		die("Error closing \"%s\": %s\n", srcfile->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		    strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	/* FIXME: We allow the srcfile_state structure to leak,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	 * because it could still be referenced from a location
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	 * variable being carried through the parser somewhere.  To
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * fix this we could either allocate all the files from a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * table, or use a pool allocator. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	return current_srcfile ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) void srcfile_add_search_path(const char *dirname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct search_path *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	/* Create the node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	node = xmalloc(sizeof(*node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	node->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	node->dirname = xstrdup(dirname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* Add to the end of our list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (search_path_tail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		*search_path_tail = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		search_path_head = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	search_path_tail = &node->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) void srcpos_update(struct srcpos *pos, const char *text, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	pos->file = current_srcfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	pos->first_line = current_srcfile->lineno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	pos->first_column = current_srcfile->colno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	for (i = 0; i < len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		if (text[i] == '\n') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 			current_srcfile->lineno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 			current_srcfile->colno = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			current_srcfile->colno++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	pos->last_line = current_srcfile->lineno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	pos->last_column = current_srcfile->colno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct srcpos *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) srcpos_copy(struct srcpos *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct srcpos *pos_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct srcfile_state *srcfile_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	pos_new = xmalloc(sizeof(struct srcpos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	assert(pos->next == NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	memcpy(pos_new, pos, sizeof(struct srcpos));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	/* allocate without free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	srcfile_state = xmalloc(sizeof(struct srcfile_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	memcpy(srcfile_state, pos->file, sizeof(struct srcfile_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	pos_new->file = srcfile_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	return pos_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	struct srcpos *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (!pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		return newtail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	for (p = pos; p->next != NULL; p = p->next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	p->next = newtail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) srcpos_string(struct srcpos *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	const char *fname = "<no-file>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	char *pos_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (pos->file && pos->file->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		fname = pos->file->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (pos->first_line != pos->last_line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		xasprintf(&pos_str, "%s:%d.%d-%d.%d", fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 			  pos->first_line, pos->first_column,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 			  pos->last_line, pos->last_column);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	else if (pos->first_column != pos->last_column)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		xasprintf(&pos_str, "%s:%d.%d-%d", fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			  pos->first_line, pos->first_column,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 			  pos->last_column);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		xasprintf(&pos_str, "%s:%d.%d", fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 			  pos->first_line, pos->first_column);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return pos_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) srcpos_string_comment(struct srcpos *pos, bool first_line, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	char *pos_str, *fname, *first, *rest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	bool fresh_fname = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (!pos) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (level > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			xasprintf(&pos_str, "<no-file>:<no-line>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			return pos_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (!pos->file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		fname = "<no-file>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	else if (!pos->file->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		fname = "<no-filename>";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	else if (level > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		fname = pos->file->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		fname = shorten_to_initial_path(pos->file->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		if (fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			fresh_fname = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 			fname = pos->file->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (level > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		xasprintf(&first, "%s:%d:%d-%d:%d", fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			  pos->first_line, pos->first_column,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			  pos->last_line, pos->last_column);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		xasprintf(&first, "%s:%d", fname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			  first_line ? pos->first_line : pos->last_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (fresh_fname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		free(fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	if (pos->next != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		rest = srcpos_string_comment(pos->next, first_line, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		xasprintf(&pos_str, "%s, %s", first, rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		free(first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		free(rest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		pos_str = first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return pos_str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) char *srcpos_string_first(struct srcpos *pos, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	return srcpos_string_comment(pos, true, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) char *srcpos_string_last(struct srcpos *pos, int level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return srcpos_string_comment(pos, false, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) void srcpos_verror(struct srcpos *pos, const char *prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		   const char *fmt, va_list va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	char *srcstr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	srcstr = srcpos_string(pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	fprintf(stderr, "%s: %s ", prefix, srcstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	vfprintf(stderr, fmt, va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	fprintf(stderr, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	free(srcstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) void srcpos_error(struct srcpos *pos, const char *prefix,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		  const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	va_list va;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	va_start(va, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	srcpos_verror(pos, prefix, fmt, va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	va_end(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) void srcpos_set_line(char *f, int l)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	current_srcfile->name = f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	current_srcfile->lineno = l;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (initial_cpp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		initial_cpp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		set_initial_path(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }