You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

126 lines
3.6 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2004, 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "configuration.h"
  27. #include "log.h"
  28. static size_t num_config_files;
  29. static char** config_file_names;
  30. static size_t num_script_dirs;
  31. static char** script_search_dirs;
  32. void add_script_search_dir (const char *dir)
  33. {
  34. num_script_dirs++;
  35. script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs + 1) * sizeof (char *));
  36. script_search_dirs[num_script_dirs-1] = strdup(dir);
  37. script_search_dirs[num_script_dirs] = NULL;
  38. }
  39. void add_config_command (const char *cfg)
  40. {
  41. num_config_files++;
  42. config_file_names = (char **)realloc(config_file_names, (num_config_files + 1) * sizeof (char *));
  43. config_file_names[num_config_files-1] = strdup(cfg);
  44. config_file_names[num_config_files] = NULL;
  45. }
  46. /* return full path or NULL according to search rules */
  47. char *find_file(const char *file)
  48. {
  49. FILE *fp = NULL;
  50. char **search_dirs = script_search_dirs;
  51. char *dir;
  52. char const *mode="r";
  53. char full_path[1024];
  54. /* Check absolute and relative to current working dir first.
  55. * This keeps full_path reporting belowing working. */
  56. snprintf(full_path, 1024, "%s", file);
  57. fp = fopen(full_path, mode);
  58. while (!fp)
  59. {
  60. dir = *search_dirs++;
  61. if (!dir)
  62. break;
  63. snprintf(full_path, 1024, "%s/%s", dir, file);
  64. fp = fopen(full_path, mode);
  65. }
  66. if (fp)
  67. {
  68. fclose(fp);
  69. LOG_DEBUG("found %s", full_path);
  70. return strdup(full_path);
  71. }
  72. return NULL;
  73. }
  74. FILE *open_file_from_path (char *file, char *mode)
  75. {
  76. if (mode[0]!='r')
  77. {
  78. return fopen(file, mode);
  79. } else
  80. {
  81. char *full_path = find_file(file);
  82. if (full_path == NULL)
  83. return NULL;
  84. FILE *fp = NULL;
  85. fp = fopen(full_path, mode);
  86. free(full_path);
  87. return fp;
  88. }
  89. }
  90. int parse_config_file(struct command_context_s *cmd_ctx)
  91. {
  92. int retval;
  93. char **cfg;
  94. if (!config_file_names)
  95. add_config_command ("script openocd.cfg");
  96. cfg = config_file_names;
  97. while (*cfg)
  98. {
  99. retval = command_run_line(cmd_ctx, *cfg);
  100. if (retval != ERROR_OK)
  101. return retval;
  102. cfg++;
  103. }
  104. return ERROR_OK;
  105. }