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.
 
 
 
 
 
 

105 lines
3.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2004, 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "types.h"
  24. #include "command.h"
  25. #include "configuration.h"
  26. #include "log.h"
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. static size_t num_config_files;
  31. static char** config_file_names;
  32. static size_t num_script_dirs;
  33. static char** script_search_dirs;
  34. void add_script_search_dir (const char *dir)
  35. {
  36. num_script_dirs++;
  37. script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
  38. script_search_dirs[num_script_dirs-1] = strdup(dir);
  39. script_search_dirs[num_script_dirs] = NULL;
  40. }
  41. void add_config_file_name (const char *cfg)
  42. {
  43. num_config_files++;
  44. config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
  45. config_file_names[num_config_files-1] = strdup(cfg);
  46. config_file_names[num_config_files] = NULL;
  47. }
  48. FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)
  49. {
  50. FILE *fp = NULL;
  51. char **search_dirs = script_search_dirs;
  52. char *dir;
  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. command_print(cmd_ctx, "opened %s", full_path);
  68. return fp;
  69. }
  70. int parse_config_file(struct command_context_s *cmd_ctx)
  71. {
  72. char **cfg;
  73. FILE *config_file;
  74. if (!config_file_names)
  75. add_config_file_name ("script openocd.cfg");
  76. cfg = config_file_names;
  77. while (*cfg)
  78. {
  79. command_run_line(cmd_ctx, *cfg);
  80. cfg++;
  81. }
  82. return ERROR_OK;
  83. }