srcpos.c 8.6 KB

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