cmdline.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //###########################################################################
  2. //
  3. // FILE: cmdline.c
  4. //
  5. // TITLE: Functions to help with processing command lines.
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. //*****************************************************************************
  43. //
  44. //! \addtogroup cmdline_api
  45. //! @{
  46. //
  47. //*****************************************************************************
  48. //
  49. // Included Files
  50. //
  51. #include <stdint.h>
  52. #include <stdbool.h>
  53. #include <string.h>
  54. #include "utils/cmdline.h"
  55. //
  56. // Defines the maximum number of arguments that can be parsed.
  57. //
  58. #ifndef CMDLINE_MAX_ARGS
  59. #define CMDLINE_MAX_ARGS 8
  60. #endif
  61. //
  62. // An array to hold the pointers to the command line arguments.
  63. //
  64. static char *g_ppcArgv[CMDLINE_MAX_ARGS + 1];
  65. //*****************************************************************************
  66. //
  67. //! Process a command line string into arguments and execute the command.
  68. //!
  69. //! \param pcCmdLine points to a string that contains a command line that was
  70. //! obtained by an application by some means.
  71. //!
  72. //! This function will take the supplied command line string and break it up
  73. //! into individual arguments. The first argument is treated as a command and
  74. //! is searched for in the command table. If the command is found, then the
  75. //! command function is called and all of the command line arguments are passed
  76. //! in the normal argc, argv form.
  77. //!
  78. //! The command table is contained in an array named <tt>g_psCmdTable</tt>
  79. //! containing <tt>tCmdLineEntry</tt> structures which must be provided by the
  80. //! application. The array must be terminated with an entry whose \b pcCmd
  81. //! field contains a NULL pointer.
  82. //!
  83. //! \return Returns \b CMDLINE_BAD_CMD if the command is not found,
  84. //! \b CMDLINE_TOO_MANY_ARGS if there are more arguments than can be parsed.
  85. //! Otherwise it returns the code that was returned by the command function.
  86. //
  87. //*****************************************************************************
  88. int
  89. CmdLineProcess(char *pcCmdLine)
  90. {
  91. char *pcChar;
  92. uint_fast8_t ui8Argc;
  93. bool bFindArg = true;
  94. tCmdLineEntry *psCmdEntry;
  95. //
  96. // Initialize the argument counter, and point to the beginning of the
  97. // command line string.
  98. //
  99. ui8Argc = 0;
  100. pcChar = pcCmdLine;
  101. //
  102. // Advance through the command line until a zero character is found.
  103. //
  104. while(*pcChar)
  105. {
  106. //
  107. // If there is a space, then replace it with a zero, and set the flag
  108. // to search for the next argument.
  109. //
  110. if(*pcChar == ' ')
  111. {
  112. *pcChar = 0;
  113. bFindArg = true;
  114. }
  115. //
  116. // Otherwise it is not a space, so it must be a character that is part
  117. // of an argument.
  118. //
  119. else
  120. {
  121. //
  122. // If bFindArg is set, then that means we are looking for the start
  123. // of the next argument.
  124. //
  125. if(bFindArg)
  126. {
  127. //
  128. // As long as the maximum number of arguments has not been
  129. // reached, then save the pointer to the start of this new arg
  130. // in the argv array, and increment the count of args, argc.
  131. //
  132. if(ui8Argc < CMDLINE_MAX_ARGS)
  133. {
  134. g_ppcArgv[ui8Argc] = pcChar;
  135. ui8Argc++;
  136. bFindArg = false;
  137. }
  138. //
  139. // The maximum number of arguments has been reached so return
  140. // the error.
  141. //
  142. else
  143. {
  144. return(CMDLINE_TOO_MANY_ARGS);
  145. }
  146. }
  147. }
  148. //
  149. // Advance to the next character in the command line.
  150. //
  151. pcChar++;
  152. }
  153. //
  154. // If one or more arguments was found, then process the command.
  155. //
  156. if(ui8Argc)
  157. {
  158. //
  159. // Start at the beginning of the command table, to look for a matching
  160. // command.
  161. //
  162. psCmdEntry = &g_psCmdTable[0];
  163. //
  164. // Search through the command table until a null command string is
  165. // found, which marks the end of the table.
  166. //
  167. while(psCmdEntry->pcCmd)
  168. {
  169. //
  170. // If this command entry command string matches argv[0], then call
  171. // the function for this command, passing the command line
  172. // arguments.
  173. //
  174. if(!strcmp(g_ppcArgv[0], psCmdEntry->pcCmd))
  175. {
  176. return(psCmdEntry->pfnCmd(ui8Argc, g_ppcArgv));
  177. }
  178. //
  179. // Not found, so advance to the next entry.
  180. //
  181. psCmdEntry++;
  182. }
  183. }
  184. //
  185. // Fall through to here means that no matching command was found, so return
  186. // an error.
  187. //
  188. return(CMDLINE_BAD_CMD);
  189. }
  190. //*****************************************************************************
  191. //
  192. // Close the Doxygen group.
  193. //! @}
  194. //
  195. //*****************************************************************************
  196. //
  197. // End of file
  198. //