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.
 
 
 
 
 
 

201 lines
5.6 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 <getopt.h>
  30. #include <string.h>
  31. static size_t num_config_files;
  32. static char** config_file_names;
  33. static size_t num_script_dirs;
  34. static char** script_search_dirs;
  35. static int help_flag;
  36. static struct option long_options[] =
  37. {
  38. {"help", no_argument, &help_flag, 1},
  39. {"debug", optional_argument, 0, 'd'},
  40. {"file", required_argument, 0, 'f'},
  41. {"search", required_argument, 0, 's'},
  42. {"log_output", required_argument, 0, 'l'},
  43. {"command", required_argument, 0, 'c'},
  44. {0, 0, 0, 0}
  45. };
  46. int configuration_output_handler(struct command_context_s *context, char* line)
  47. {
  48. INFO(line);
  49. return ERROR_OK;
  50. }
  51. void add_script_search_dir (const char *dir)
  52. {
  53. num_script_dirs++;
  54. script_search_dirs = (char **)realloc(script_search_dirs, (num_script_dirs+1) * sizeof (char *));
  55. script_search_dirs[num_script_dirs-1] = strdup(dir);
  56. script_search_dirs[num_script_dirs] = NULL;
  57. }
  58. void add_config_file_name (const char *cfg)
  59. {
  60. num_config_files++;
  61. config_file_names = (char **)realloc(config_file_names, (num_config_files+1) * sizeof (char *));
  62. config_file_names[num_config_files-1] = strdup(cfg);
  63. config_file_names[num_config_files] = NULL;
  64. }
  65. int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
  66. {
  67. int c;
  68. char command_buffer[128];
  69. while (1)
  70. {
  71. /* getopt_long stores the option index here. */
  72. int option_index = 0;
  73. c = getopt_long(argc, argv, "hd::l:f:s:c:", long_options, &option_index);
  74. /* Detect the end of the options. */
  75. if (c == -1)
  76. break;
  77. switch (c)
  78. {
  79. case 0:
  80. break;
  81. case 'h': /* --help | -h */
  82. help_flag = 1;
  83. break;
  84. case 'f': /* --file | -f */
  85. snprintf(command_buffer, 128, "script %s", optarg);
  86. add_config_file_name(command_buffer);
  87. break;
  88. case 's': /* --search | -s */
  89. add_script_search_dir(optarg);
  90. break;
  91. case 'd': /* --debug | -d */
  92. if (optarg)
  93. snprintf(command_buffer, 128, "debug_level %s", optarg);
  94. else
  95. snprintf(command_buffer, 128, "debug_level 3");
  96. command_run_line(cmd_ctx, command_buffer);
  97. break;
  98. case 'l': /* --log_output | -l */
  99. if (optarg)
  100. {
  101. snprintf(command_buffer, 128, "log_output %s", optarg);
  102. command_run_line(cmd_ctx, command_buffer);
  103. }
  104. break;
  105. case 'c': /* --command | -c */
  106. if (optarg)
  107. {
  108. add_config_file_name(optarg);
  109. }
  110. break;
  111. }
  112. }
  113. if (help_flag)
  114. {
  115. printf("Open On-Chip Debugger\n(c) 2005 by Dominic Rath\n\n");
  116. printf("--help | -h\tdisplay this help\n");
  117. printf("--file | -f\tuse configuration file <name>\n");
  118. printf("--search | -s\tdir to search for config files and scripts.\n");
  119. printf("--debug | -d\tset debug level <0-3>\n");
  120. printf("--log_output | -l\tredirect log output to file <name>\n");
  121. printf("--command | -c\trun <command>\n");
  122. exit(-1);
  123. }
  124. /* Add dir for openocd supplied scripts last so that user can over
  125. ride those scripts if desired. */
  126. add_script_search_dir(PKGDATADIR);
  127. add_script_search_dir(PKGLIBDIR);
  128. return ERROR_OK;
  129. }
  130. FILE *open_file_from_path (command_context_t *cmd_ctx, char *file, char *mode)
  131. {
  132. FILE *fp = NULL;
  133. char **search_dirs = script_search_dirs;
  134. char *dir;
  135. char full_path[1024];
  136. /* Check absolute and relative to current working dir first.
  137. * This keeps full_path reporting belowing working. */
  138. snprintf(full_path, 1024, "%s", file);
  139. fp = fopen(full_path, mode);
  140. while (!fp)
  141. {
  142. dir = *search_dirs++;
  143. if (!dir)
  144. break;
  145. snprintf(full_path, 1024, "%s/%s", dir, file);
  146. fp = fopen(full_path, mode);
  147. }
  148. if (fp)
  149. command_print(cmd_ctx, "opened %s", full_path);
  150. return fp;
  151. }
  152. int parse_config_file(struct command_context_s *cmd_ctx)
  153. {
  154. char **cfg;
  155. FILE *config_file;
  156. if (!config_file_names)
  157. add_config_file_name ("script openocd.cfg");
  158. cfg = config_file_names;
  159. while (*cfg)
  160. {
  161. command_run_line(cmd_ctx, *cfg);
  162. cfg++;
  163. }
  164. return ERROR_OK;
  165. }