srcpos.c 8.6 KB

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