srcpos.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include "dtc.h"
  20. #include "srcpos.h"
  21. /*
  22. * Like yylineno, this is the current open file pos.
  23. */
  24. struct dtc_file *srcpos_file;
  25. static int dtc_open_one(struct dtc_file *file,
  26. const char *search,
  27. const char *fname)
  28. {
  29. char *fullname;
  30. if (search) {
  31. fullname = xmalloc(strlen(search) + strlen(fname) + 2);
  32. strcpy(fullname, search);
  33. strcat(fullname, "/");
  34. strcat(fullname, fname);
  35. } else {
  36. fullname = strdup(fname);
  37. }
  38. file->file = fopen(fullname, "r");
  39. if (!file->file) {
  40. free(fullname);
  41. return 0;
  42. }
  43. file->name = fullname;
  44. return 1;
  45. }
  46. struct dtc_file *dtc_open_file(const char *fname,
  47. const struct search_path *search)
  48. {
  49. static const struct search_path default_search = { NULL, NULL, NULL };
  50. struct dtc_file *file;
  51. const char *slash;
  52. file = xmalloc(sizeof(struct dtc_file));
  53. slash = strrchr(fname, '/');
  54. if (slash) {
  55. char *dir = xmalloc(slash - fname + 1);
  56. memcpy(dir, fname, slash - fname);
  57. dir[slash - fname] = 0;
  58. file->dir = dir;
  59. } else {
  60. file->dir = NULL;
  61. }
  62. if (streq(fname, "-")) {
  63. file->name = "stdin";
  64. file->file = stdin;
  65. return file;
  66. }
  67. if (fname[0] == '/') {
  68. file->file = fopen(fname, "r");
  69. if (!file->file)
  70. goto fail;
  71. file->name = strdup(fname);
  72. return file;
  73. }
  74. if (!search)
  75. search = &default_search;
  76. while (search) {
  77. if (dtc_open_one(file, search->dir, fname))
  78. return file;
  79. if (errno != ENOENT)
  80. goto fail;
  81. search = search->next;
  82. }
  83. fail:
  84. die("Couldn't open \"%s\": %s\n", fname, strerror(errno));
  85. }
  86. void dtc_close_file(struct dtc_file *file)
  87. {
  88. if (fclose(file->file))
  89. die("Error closing \"%s\": %s\n", file->name, strerror(errno));
  90. free(file->dir);
  91. free(file);
  92. }