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.
 
 
 
 
 
 

215 lines
6.6 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2004, 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007-2010 Ø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. /* @todo the inclusion of server.h here is a layering violation */
  28. #include <server/server.h>
  29. #include <getopt.h>
  30. static int help_flag, version_flag;
  31. static const struct option long_options[] = {
  32. {"help", no_argument, &help_flag, 1},
  33. {"version", no_argument, &version_flag, 1},
  34. {"debug", optional_argument, 0, 'd'},
  35. {"file", required_argument, 0, 'f'},
  36. {"search", required_argument, 0, 's'},
  37. {"log_output", required_argument, 0, 'l'},
  38. {"command", required_argument, 0, 'c'},
  39. {"pipe", no_argument, 0, 'p'},
  40. {0, 0, 0, 0}
  41. };
  42. int configuration_output_handler(struct command_context *context, const char *line)
  43. {
  44. LOG_USER_N("%s", line);
  45. return ERROR_OK;
  46. }
  47. static void add_default_dirs(void)
  48. {
  49. #ifdef _WIN32
  50. char strExePath[MAX_PATH];
  51. char *path;
  52. GetModuleFileName(NULL, strExePath, MAX_PATH);
  53. /* Strip executable file name, leaving path */
  54. *strrchr(strExePath, '\\') = '\0';
  55. /* Convert path separators to UNIX style, should work on Windows also. */
  56. for (char *p = strExePath; *p; p++) {
  57. if (*p == '\\')
  58. *p = '/';
  59. }
  60. /* Add the parent of the directory where openocd.exe resides to the
  61. * config script search path.
  62. *
  63. * bin/openocd.exe
  64. * interface/dummy.cfg
  65. * target/at91eb40a.cfg
  66. */
  67. path = alloc_printf("%s%s", strExePath, "/..");
  68. if (path) {
  69. add_script_search_dir(path);
  70. free(path);
  71. }
  72. /* Add support for the directory layout resulting from a 'make install'.
  73. *
  74. * bin/openocd.exe
  75. * share/openocd/scripts/interface/dummy.cfg
  76. * share/openocd/scripts/target/at91eb40a.cfg
  77. */
  78. path = alloc_printf("%s%s", strExePath, "/../share/" PACKAGE "/scripts");
  79. if (path) {
  80. add_script_search_dir(path);
  81. free(path);
  82. }
  83. /* Add single "scripts" folder to search path for Windows OpenOCD builds that don't use cygwin
  84. *
  85. * bin/openocd.exe
  86. * scripts/interface/dummy.cfg
  87. * scripts/target/at91eb40a.cfg
  88. */
  89. path = alloc_printf("%s%s", strExePath, "/../scripts");
  90. if (path) {
  91. add_script_search_dir(path);
  92. free(path);
  93. }
  94. #else
  95. /*
  96. * The directory containing OpenOCD-supplied scripts should be
  97. * listed last in the built-in search order, so the user can
  98. * override these scripts with site-specific customizations.
  99. */
  100. const char *home = getenv("HOME");
  101. if (home) {
  102. char *path;
  103. path = alloc_printf("%s/.openocd", home);
  104. if (path) {
  105. add_script_search_dir(path);
  106. free(path);
  107. }
  108. }
  109. add_script_search_dir(PKGDATADIR "/site");
  110. add_script_search_dir(PKGDATADIR "/scripts");
  111. #endif
  112. }
  113. int parse_cmdline_args(struct command_context *cmd_ctx, int argc, char *argv[])
  114. {
  115. int c;
  116. char command_buffer[128];
  117. while (1) {
  118. /* getopt_long stores the option index here. */
  119. int option_index = 0;
  120. c = getopt_long(argc, argv, "hvd::l:f:s:c:p", long_options, &option_index);
  121. /* Detect the end of the options. */
  122. if (c == -1)
  123. break;
  124. switch (c) {
  125. case 0:
  126. break;
  127. case 'h': /* --help | -h */
  128. help_flag = 1;
  129. break;
  130. case 'v': /* --version | -v */
  131. version_flag = 1;
  132. break;
  133. case 'f': /* --file | -f */
  134. {
  135. snprintf(command_buffer, 128, "script {%s}", optarg);
  136. add_config_command(command_buffer);
  137. break;
  138. }
  139. case 's': /* --search | -s */
  140. add_script_search_dir(optarg);
  141. break;
  142. case 'd': /* --debug | -d */
  143. if (optarg)
  144. snprintf(command_buffer, 128, "debug_level %s", optarg);
  145. else
  146. snprintf(command_buffer, 128, "debug_level 3");
  147. command_run_line(cmd_ctx, command_buffer);
  148. break;
  149. case 'l': /* --log_output | -l */
  150. if (optarg) {
  151. snprintf(command_buffer, 128, "log_output %s", optarg);
  152. command_run_line(cmd_ctx, command_buffer);
  153. }
  154. break;
  155. case 'c': /* --command | -c */
  156. if (optarg)
  157. add_config_command(optarg);
  158. break;
  159. case 'p':
  160. /* to replicate the old syntax this needs to be synchronous
  161. * otherwise the gdb stdin will overflow with the warning message */
  162. command_run_line(cmd_ctx, "gdb_port pipe; log_output openocd.log");
  163. LOG_WARNING("deprecated option: -p/--pipe. Use '-c \"gdb_port pipe; "
  164. "log_output openocd.log\"' instead.");
  165. break;
  166. }
  167. }
  168. if (help_flag) {
  169. LOG_OUTPUT("Open On-Chip Debugger\nLicensed under GNU GPL v2\n");
  170. LOG_OUTPUT("--help | -h\tdisplay this help\n");
  171. LOG_OUTPUT("--version | -v\tdisplay OpenOCD version\n");
  172. LOG_OUTPUT("--file | -f\tuse configuration file <name>\n");
  173. LOG_OUTPUT("--search | -s\tdir to search for config files and scripts\n");
  174. LOG_OUTPUT("--debug | -d\tset debug level <0-3>\n");
  175. LOG_OUTPUT("--log_output | -l\tredirect log output to file <name>\n");
  176. LOG_OUTPUT("--command | -c\trun <command>\n");
  177. exit(-1);
  178. }
  179. if (version_flag) {
  180. /* Nothing to do, version gets printed automatically. */
  181. /* It is not an error to request the VERSION number. */
  182. exit(0);
  183. }
  184. /* paths specified on the command line take precedence over these
  185. * built-in paths
  186. */
  187. add_default_dirs();
  188. return ERROR_OK;
  189. }