command_parser.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*******************************************************************************
  2. Copyright 2019 Yepkit Lda (www.yepkit.com)
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. *******************************************************************************/
  13. #include <command_parser.h>
  14. #include <string>
  15. #include <cstring>
  16. #include <ykush_help.h>
  17. #include <cstdlib>
  18. /**
  19. * CommandLine class constructor
  20. *
  21. */
  22. CommandLine::CommandLine()
  23. {
  24. }
  25. CommandLine::~CommandLine()
  26. {
  27. //free command line structures
  28. struct command_option *cur_opt = command.options;
  29. while (cur_opt) {
  30. struct command_option *next_opt = cur_opt->next;
  31. free(cur_opt->name);
  32. struct command_parameter *cur_param = cur_opt->parameters;
  33. while ( cur_param ) {
  34. struct command_parameter *next_param = cur_param->next;
  35. free( cur_param->name );
  36. free( cur_param );
  37. cur_param = next_param;
  38. }
  39. free( cur_opt );
  40. cur_opt = next_opt;
  41. }
  42. }
  43. int CommandLine::parse(int argc, char **argv)
  44. {
  45. command.app_name = argv[0];
  46. if ( argc < 2 )
  47. return -1;
  48. int i = 1;
  49. struct command_option *cur_opt = NULL;
  50. struct command_option *root_opt = NULL;
  51. bool exit_while;
  52. while ( i < ( argc - 1 ) ) {
  53. exit_while = false;
  54. if ( std::strlen(argv[i]) > 1 ) {
  55. if ( argv[i][0] == '-') { //is an option
  56. //add new option to command.options
  57. struct command_option *tmp_opt;
  58. tmp_opt = (struct command_option *) std::calloc(1, sizeof(struct command_option));
  59. if (cur_opt)
  60. cur_opt->next = tmp_opt;
  61. else
  62. root_opt = tmp_opt;
  63. cur_opt = tmp_opt;
  64. cur_opt->next = NULL;
  65. std::memcpy( cur_opt->name, argv[i], std::strlen( argv[i] ) + 1 ); //copy string allocating memory for destination
  66. i++;
  67. //get option parameters, if they exist
  68. struct command_parameter *cur_param = NULL;
  69. struct command_parameter *root_param = NULL;
  70. while ( ( i < ( argc - 1 ) ) && !exit_while ) {
  71. if ( argv[i][0] == '-' ) { //is another option
  72. i--;
  73. exit_while = true;
  74. } else { //is a parameter
  75. //add parameter to option
  76. struct command_parameter *tmp_param;
  77. tmp_param = (struct command_parameter *) std::calloc(1, sizeof(struct command_parameter));
  78. if (cur_param)
  79. cur_param->next = tmp_param;
  80. else
  81. root_param = tmp_param;
  82. cur_param = tmp_param;
  83. cur_param->next = NULL;
  84. std::memcpy( cur_param->name, argv[i], std::strlen( argv[i] ) + 1 ); //copy string allocating memory for destination
  85. i++;
  86. }
  87. }
  88. cur_opt->parameters = root_param;
  89. }
  90. }
  91. }
  92. command.options = root_opt;
  93. return 0;
  94. }
  95. int CommandLine::is_board(char *board_name)
  96. {
  97. int i = 0;
  98. std::string str1 ("-b");
  99. std::string str2 ("--board");
  100. std::string str3 (board_name);
  101. //look for -b or --board option
  102. struct command_option *cur_opt;
  103. struct command_parameter *cur_param;
  104. cur_opt = command.options;
  105. while ( cur_opt ) {
  106. if ( (str1.compare( cur_opt->name ) == 0) || (str2.compare( cur_opt->name ) == 0) ) {
  107. //board option was found.
  108. //Now let's check if the board name parameter matches the board_name
  109. cur_param = cur_opt->parameters;
  110. while ( cur_param ) {
  111. if ( str3.compare(cur_param->name) )
  112. return 0; //board match
  113. cur_param = cur_param->next;
  114. }
  115. }
  116. cur_opt = cur_opt->next;
  117. }
  118. return -1;
  119. }