|
@@ -119,7 +119,7 @@ static char peekNextHunk(shellmatta_instance_t *inst)
|
|
|
* @param[in] handle shellmatta handle
|
|
|
* @param[in] optionString option string e.g. "cd:e::"
|
|
|
* @param[out] option pointer to store the detected option to
|
|
|
- * @param[out] argtype pointer to store the argument string to (can be NULL)
|
|
|
+ * @param[out] argtype pointer to var of type #shellmatta_opt_argtype_t != NULL
|
|
|
* @return errorcode #SHELLMATTA_OK - option parsable and found in option String
|
|
|
* #SHELLMATTA_ERROR - format error or option unknown
|
|
|
*/
|
|
@@ -135,30 +135,103 @@ static shellmatta_retCode_t parseShortOpt( shellmatta_instance_t *inst,
|
|
|
/*! -# check for correct syntax */
|
|
|
if((2u == inst->optionParser.len) && ('-' == buffer[0u]) && ('-' != buffer[1u]) && ('\0' != buffer[1u]))
|
|
|
{
|
|
|
+ *option = '\0';
|
|
|
+
|
|
|
/*! -# search for option character in option string */
|
|
|
- for(i = 0u; ('\0' != optionString[i]) && (buffer[1u] != optionString[i]); i ++);
|
|
|
-
|
|
|
- if(buffer[1u] == optionString[i])
|
|
|
+ for(i = 0u; ('\0' != optionString[i]) && ('\0' == *option); i ++)
|
|
|
{
|
|
|
- /*! -# return found option character */
|
|
|
- *option = buffer[1u];
|
|
|
- ret = SHELLMATTA_OK;
|
|
|
-
|
|
|
- /*! -# check if an argument is required or optional */
|
|
|
- if(':' == optionString[i + 1u])
|
|
|
+ if(buffer[1u] == optionString[i])
|
|
|
{
|
|
|
- *argtype = SHELLMATTA_OPT_ARG_REQUIRED;
|
|
|
- if(':' == optionString[i + 2u])
|
|
|
+ ret = SHELLMATTA_OK;
|
|
|
+
|
|
|
+ /*! -# return found option character */
|
|
|
+ *option = buffer[1u];
|
|
|
+
|
|
|
+ /*! -# check if an argument is required or optional */
|
|
|
+ if(':' == optionString[i + 1u])
|
|
|
+ {
|
|
|
+ *argtype = SHELLMATTA_OPT_ARG_REQUIRED;
|
|
|
+ if(':' == optionString[i + 2u])
|
|
|
+ {
|
|
|
+ *argtype = SHELLMATTA_OPT_ARG_OPTIONAL;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
{
|
|
|
- *argtype = SHELLMATTA_OPT_ARG_OPTIONAL;
|
|
|
+ *argtype = SHELLMATTA_OPT_ARG_NONE;
|
|
|
}
|
|
|
}
|
|
|
- else
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*! -# skip "--" */
|
|
|
+ else if((2u == inst->optionParser.len) && ('-' == buffer[0u]) && ('-' == buffer[1u]))
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_CONTINUE;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ *option = '\0';
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * @brief tries to parse the current input hunk and check if this is a configured option
|
|
|
+ * @param[in] handle shellmatta handle
|
|
|
+ * @param[in] longOptions option structure - pointer to array of type #shellmatta_opt_long_t
|
|
|
+ * @param[out] option pointer to store the detected option to
|
|
|
+ * @param[out] argtype pointer to var of type #shellmatta_opt_argtype_t != NULL
|
|
|
+ * @return errorcode #SHELLMATTA_OK - option parsable and found in option String
|
|
|
+ * #SHELLMATTA_ERROR - format error or option unknown
|
|
|
+ */
|
|
|
+static shellmatta_retCode_t parseLongOpt( shellmatta_instance_t *inst,
|
|
|
+ shellmatta_opt_long_t *longOptions,
|
|
|
+ char *option,
|
|
|
+ shellmatta_opt_argtype_t *argtype)
|
|
|
+{
|
|
|
+ shellmatta_retCode_t ret = SHELLMATTA_ERROR;
|
|
|
+ char *buffer = &inst->buffer[inst->optionParser.offset];
|
|
|
+ uint32_t i;
|
|
|
+
|
|
|
+ /*! -# check for correct syntax for short options */
|
|
|
+ if((2u == inst->optionParser.len) && ('-' == buffer[0u]) && ('-' != buffer[1u]) && ('\0' != buffer[1u]))
|
|
|
+ {
|
|
|
+ /*! -# search for option character in option list */
|
|
|
+ for(i = 0u; ('\0' != longOptions[i].paramShort) && ('\0' == *option); i ++)
|
|
|
+ {
|
|
|
+ if(buffer[1u] == longOptions[i].paramShort)
|
|
|
{
|
|
|
- *argtype = SHELLMATTA_OPT_ARG_NONE;
|
|
|
+ ret = SHELLMATTA_OK;
|
|
|
+
|
|
|
+ /*! -# return found option character */
|
|
|
+ *option = longOptions[i].paramShort;
|
|
|
+ *argtype = longOptions[i].argtype;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ /*! -# check for correct syntax for long options */
|
|
|
+ else if((3u <= inst->optionParser.len) && ('-' == buffer[0u]) && ('-' == buffer[1u]))
|
|
|
+ {
|
|
|
+ /*! -# search for long option in option list */
|
|
|
+ for(i = 0u; ('\0' != longOptions[i].paramShort) && ('\0' == *option); i ++)
|
|
|
+ {
|
|
|
+ if(0 == strcmp(&buffer[2u], longOptions[i].paramLong))
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_OK;
|
|
|
+
|
|
|
+ /*! -# return found option character */
|
|
|
+ *option = longOptions[i].paramShort;
|
|
|
+ *argtype = longOptions[i].argtype;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*! -# ignore "--" */
|
|
|
+ else if((2u == inst->optionParser.len) && ('-' == buffer[0u]) && ('-' == buffer[1u]))
|
|
|
+ {
|
|
|
+ *option = '\0';
|
|
|
+ ret = SHELLMATTA_CONTINUE;
|
|
|
+ }
|
|
|
else
|
|
|
{
|
|
|
*option = '\0';
|
|
@@ -168,20 +241,24 @@ static shellmatta_retCode_t parseShortOpt( shellmatta_instance_t *inst,
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @brief scans the current input and parses options in getopt style
|
|
|
+ * @brief Scans the current input and parses options in getopt style - pass either optionString or longOptions
|
|
|
+ * This is an internal funtion to handle both getopt styles and remove duplicated code...
|
|
|
+ * The standard functions are just wrapper around this one.
|
|
|
* @param[in] handle shellmatta handle
|
|
|
* @param[in] optionString option string e.g. "cd:e::"
|
|
|
+ * @param[in] longOptions option structure - pointer to array of type #shellmatta_opt_long_t
|
|
|
* @param[out] option pointer to store the detected option to
|
|
|
* @param[out] argument pointer to store the argument string to (can be NULL)
|
|
|
* @param[out] argLen pointer to store the argument lengh to (can be NULL)
|
|
|
* @return errorcode #SHELLMATTA_OK - no error - keep on calling
|
|
|
* #SHELLMATTA_ERROR - error occured - e.g. argument missing
|
|
|
*/
|
|
|
-shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
|
|
|
- char *optionString,
|
|
|
- char *option,
|
|
|
- char **argument,
|
|
|
- uint32_t *argLen)
|
|
|
+static shellmatta_retCode_t shellmatta_opt_int( shellmatta_handle_t handle,
|
|
|
+ char *optionString,
|
|
|
+ shellmatta_opt_long_t *longOptions,
|
|
|
+ char *option,
|
|
|
+ char **argument,
|
|
|
+ uint32_t *argLen)
|
|
|
{
|
|
|
shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
|
|
|
shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
@@ -190,63 +267,122 @@ shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
|
|
|
/** -# check parameters for plausibility */
|
|
|
if( (NULL != inst)
|
|
|
&& (SHELLMATTA_MAGIC == inst->magic)
|
|
|
- && (NULL != optionString)
|
|
|
&& (NULL != option))
|
|
|
{
|
|
|
*option = '\0';
|
|
|
- *argument = NULL;
|
|
|
- *argLen = 0u;
|
|
|
-
|
|
|
- ret = findNextHunk(inst);
|
|
|
- if(SHELLMATTA_OK == ret)
|
|
|
+ if(NULL != argument)
|
|
|
+ {
|
|
|
+ *argument = NULL;
|
|
|
+ }
|
|
|
+ if(NULL != argLen)
|
|
|
{
|
|
|
- ret = parseShortOpt(inst, optionString, option, &argtype);
|
|
|
+ *argLen = 0u;
|
|
|
+ }
|
|
|
|
|
|
- /*! -# when no option is found return this as raw argument */
|
|
|
- if(SHELLMATTA_ERROR == ret)
|
|
|
- {
|
|
|
- *argument = &(inst->buffer[inst->optionParser.offset]);
|
|
|
- *argLen = inst->optionParser.len;
|
|
|
- ret = SHELLMATTA_OK;
|
|
|
- }
|
|
|
- else
|
|
|
+ /*! -# do this until we find a not skipable argument */
|
|
|
+ do
|
|
|
+ {
|
|
|
+ ret = findNextHunk(inst);
|
|
|
+ if(SHELLMATTA_OK == ret)
|
|
|
{
|
|
|
- switch(argtype)
|
|
|
+ /*! -# call the matching parse function */
|
|
|
+ if(NULL != optionString)
|
|
|
+ {
|
|
|
+ ret = parseShortOpt(inst, optionString, option, &argtype);
|
|
|
+ }
|
|
|
+ else if(NULL != longOptions)
|
|
|
+ {
|
|
|
+ ret = parseLongOpt(inst, longOptions, option, &argtype);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_USE_FAULT;
|
|
|
+ }
|
|
|
+
|
|
|
+ /*! -# when no option is found return this as raw argument */
|
|
|
+ if(SHELLMATTA_ERROR == ret)
|
|
|
+ {
|
|
|
+ if(NULL != argument)
|
|
|
+ {
|
|
|
+ *argument = &(inst->buffer[inst->optionParser.offset]);
|
|
|
+ }
|
|
|
+ if(NULL != argLen)
|
|
|
+ {
|
|
|
+ *argLen = inst->optionParser.len;
|
|
|
+ }
|
|
|
+ ret = SHELLMATTA_OK;
|
|
|
+ }
|
|
|
+ else if(SHELLMATTA_USE_FAULT == ret)
|
|
|
{
|
|
|
- case SHELLMATTA_OPT_ARG_REQUIRED:
|
|
|
- ret = findNextHunk(inst);
|
|
|
- if(SHELLMATTA_OK == ret)
|
|
|
- {
|
|
|
- *argument = &(inst->buffer[inst->optionParser.offset]);
|
|
|
- *argLen = inst->optionParser.len;
|
|
|
- }
|
|
|
- break;
|
|
|
- case SHELLMATTA_OPT_ARG_OPTIONAL:
|
|
|
- /*! -# treat anything not starting with '-' as argument */
|
|
|
- if('-' != peekNextHunk(inst))
|
|
|
- {
|
|
|
+ /*! -# nothing to do - just return errorcode */
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ switch(argtype)
|
|
|
+ {
|
|
|
+ case SHELLMATTA_OPT_ARG_REQUIRED:
|
|
|
ret = findNextHunk(inst);
|
|
|
+ if((NULL == argument) || (NULL == argLen))
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_USE_FAULT;
|
|
|
+ }
|
|
|
if(SHELLMATTA_OK == ret)
|
|
|
{
|
|
|
*argument = &(inst->buffer[inst->optionParser.offset]);
|
|
|
*argLen = inst->optionParser.len;
|
|
|
}
|
|
|
- }
|
|
|
- break;
|
|
|
- default:
|
|
|
- /* nothing to do */
|
|
|
- break;
|
|
|
+ break;
|
|
|
+ case SHELLMATTA_OPT_ARG_OPTIONAL:
|
|
|
+ /*! -# treat anything not starting with '-' as argument */
|
|
|
+ if('-' != peekNextHunk(inst))
|
|
|
+ {
|
|
|
+ ret = findNextHunk(inst);
|
|
|
+ if((NULL == argument) || (NULL == argLen))
|
|
|
+ {
|
|
|
+ ret = SHELLMATTA_USE_FAULT;
|
|
|
+ }
|
|
|
+ if(SHELLMATTA_OK == ret)
|
|
|
+ {
|
|
|
+ *argument = &(inst->buffer[inst->optionParser.offset]);
|
|
|
+ *argLen = inst->optionParser.len;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ /* nothing to do */
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ } while(SHELLMATTA_CONTINUE == ret);
|
|
|
}
|
|
|
-
|
|
|
- (void)argument;
|
|
|
- (void)argLen;
|
|
|
-
|
|
|
return ret;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * @brief scans the current input and parses options in getopt style
|
|
|
+ * @param[in] handle shellmatta handle
|
|
|
+ * @param[in] optionString option string e.g. "cd:e::"
|
|
|
+ * @param[out] option pointer to store the detected option to
|
|
|
+ * @param[out] argument pointer to store the argument string to (can be NULL)
|
|
|
+ * @param[out] argLen pointer to store the argument lengh to (can be NULL)
|
|
|
+ * @return errorcode #SHELLMATTA_OK - no error - keep on calling
|
|
|
+ * #SHELLMATTA_ERROR - error occured - e.g. argument missing
|
|
|
+ */
|
|
|
+shellmatta_retCode_t shellmatta_opt( shellmatta_handle_t handle,
|
|
|
+ char *optionString,
|
|
|
+ char *option,
|
|
|
+ char **argument,
|
|
|
+ uint32_t *argLen)
|
|
|
+{
|
|
|
+ return shellmatta_opt_int( handle,
|
|
|
+ optionString,
|
|
|
+ NULL,
|
|
|
+ option,
|
|
|
+ argument,
|
|
|
+ argLen);
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
* @brief scans the current input and parses options in getopt_long style
|
|
|
* @param[in] handle shellmatta handle
|
|
@@ -261,22 +397,12 @@ shellmatta_retCode_t shellmatta_opt_long( shellmatta_handle_t handle,
|
|
|
char **argument,
|
|
|
uint32_t *argLen)
|
|
|
{
|
|
|
- shellmatta_retCode_t ret = SHELLMATTA_USE_FAULT;
|
|
|
- shellmatta_instance_t *inst = (shellmatta_instance_t*)handle;
|
|
|
-
|
|
|
- /** -# check parameters for plausibility */
|
|
|
- if( (NULL != inst)
|
|
|
- && (SHELLMATTA_MAGIC == inst->magic)
|
|
|
- && (NULL != longOptions)
|
|
|
- && (NULL != option))
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- (void)argument;
|
|
|
- (void)argLen;
|
|
|
-
|
|
|
- return ret;
|
|
|
+ return shellmatta_opt_int( handle,
|
|
|
+ NULL,
|
|
|
+ longOptions,
|
|
|
+ option,
|
|
|
+ argument,
|
|
|
+ argLen);
|
|
|
}
|
|
|
|
|
|
/**
|