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
6.1 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. // @todo the inclusion of server.h here is a layering violation
  29. #include "server.h"
  30. #include <getopt.h>
  31. static int help_flag, version_flag;
  32. static struct option long_options[] =
  33. {
  34. {"help", no_argument, &help_flag, 1},
  35. {"version", no_argument, &version_flag, 1},
  36. {"debug", optional_argument, 0, 'd'},
  37. {"file", required_argument, 0, 'f'},
  38. {"search", required_argument, 0, 's'},
  39. {"log_output", required_argument, 0, 'l'},
  40. {"command", required_argument, 0, 'c'},
  41. {"pipe", no_argument, 0, 'p'},
  42. {0, 0, 0, 0}
  43. };
  44. int configuration_output_handler(struct command_context_s *context, const char* line)
  45. {
  46. LOG_USER_N("%s", line);
  47. return ERROR_OK;
  48. }
  49. int add_default_dirs(void)
  50. {
  51. #ifdef _WIN32
  52. /* Add the parent of the directory where openocd.exe resides to the
  53. * config script search path.
  54. * Directory layout:
  55. * bin\openocd.exe
  56. * lib\openocd
  57. * event\at91eb40a_reset.cfg
  58. * target\at91eb40a.cfg
  59. */
  60. {
  61. char strExePath [MAX_PATH];
  62. GetModuleFileName (NULL, strExePath, MAX_PATH);
  63. /* Either this code will *always* work or it will SEGFAULT giving
  64. * excellent information on the culprit.
  65. */
  66. *strrchr(strExePath, '\\')=0;
  67. strcat(strExePath, "\\..");
  68. add_script_search_dir(strExePath);
  69. }
  70. /*
  71. * Add support for the default (as of 20080121) layout when
  72. * using autotools and cygwin to build native MinGW binary.
  73. * Path separator is converted to UNIX style so that MinGW is
  74. * pleased.
  75. *
  76. * bin/openocd.exe
  77. * lib/openocd/event/at91eb40a_reset.cfg
  78. * lib/openocd/target/at91eb40a.cfg
  79. */
  80. {
  81. char strExePath [MAX_PATH];
  82. char *p;
  83. GetModuleFileName (NULL, strExePath, MAX_PATH);
  84. *strrchr(strExePath, '\\')=0;
  85. strcat(strExePath, "/../lib/"PACKAGE);
  86. for (p=strExePath; *p; p++) {
  87. if (*p == '\\')
  88. *p = '/';
  89. }
  90. add_script_search_dir(strExePath);
  91. }
  92. #else
  93. /*
  94. * The directory containing OpenOCD-supplied scripts should be
  95. * listed last in the built-in search order, so the user can
  96. * override these scripts with site-specific customizations.
  97. */
  98. /// @todo Implement @c add_script_search_dir("${HOME}/.openocd").
  99. add_script_search_dir(PKGDATADIR "/site");
  100. add_script_search_dir(PKGDATADIR "/scripts");
  101. #endif
  102. return ERROR_OK;
  103. }
  104. int parse_cmdline_args(struct command_context_s *cmd_ctx, int argc, char *argv[])
  105. {
  106. int c;
  107. char command_buffer[128];
  108. while (1)
  109. {
  110. /* getopt_long stores the option index here. */
  111. int option_index = 0;
  112. c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
  113. /* Detect the end of the options. */
  114. if (c == -1)
  115. break;
  116. switch (c)
  117. {
  118. case 0:
  119. break;
  120. case 'h': /* --help | -h */
  121. help_flag = 1;
  122. break;
  123. case 'v': /* --version | -v */
  124. version_flag = 1;
  125. break;
  126. case 'f': /* --file | -f */
  127. {
  128. snprintf(command_buffer, 128, "script {%s}", optarg);
  129. add_config_command(command_buffer);
  130. break;
  131. }
  132. case 's': /* --search | -s */
  133. add_script_search_dir(optarg);
  134. break;
  135. case 'd': /* --debug | -d */
  136. if (optarg)
  137. snprintf(command_buffer, 128, "debug_level %s", optarg);
  138. else
  139. snprintf(command_buffer, 128, "debug_level 3");
  140. command_run_line(cmd_ctx, command_buffer);
  141. break;
  142. case 'l': /* --log_output | -l */
  143. if (optarg)
  144. {
  145. snprintf(command_buffer, 128, "log_output %s", optarg);
  146. command_run_line(cmd_ctx, command_buffer);
  147. }
  148. break;
  149. case 'c': /* --command | -c */
  150. if (optarg)
  151. {
  152. add_config_command(optarg);
  153. }
  154. break;
  155. case 'p': /* --pipe | -p */
  156. #if BUILD_ECOSBOARD == 1
  157. /* pipes unsupported on hosted platforms */
  158. LOG_WARNING("pipes not supported on this platform");
  159. #else
  160. server_use_pipes = 1;
  161. #endif
  162. break;
  163. }
  164. }
  165. if (help_flag)
  166. {
  167. LOG_OUTPUT("Open On-Chip Debugger\n(c) 2005-2008 by Dominic Rath\n\n");
  168. LOG_OUTPUT("--help | -h\tdisplay this help\n");
  169. LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
  170. LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
  171. LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
  172. LOG_OUTPUT("--debug | -d\tset debug level <0-3>\n");
  173. LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
  174. LOG_OUTPUT("--command | -c\trun <command>\n");
  175. LOG_OUTPUT("--pipe | -p\tuse pipes for gdb communication\n");
  176. exit(-1);
  177. }
  178. if (version_flag)
  179. {
  180. /* Nothing to do, version gets printed automatically. */
  181. // It is not an error to request the VERSION number.
  182. exit(0);
  183. }
  184. return ERROR_OK;
  185. }