+/** Retrieve either static or dynamic token at a given index. */
+static cmdline_parse_token_hdr_t *
+get_token(cmdline_parse_inst_t *inst,
+ unsigned int index,
+ cmdline_parse_token_hdr_t
+ *(*dyn_tokens)[CMDLINE_PARSE_DYNAMIC_TOKENS])
+{
+ /* check presence of static tokens first */
+ if (inst->tokens[0] || !inst->f)
+ return inst->tokens[index];
+ /* for dynamic tokens, make sure index does not overflow */
+ if (index >= CMDLINE_PARSE_DYNAMIC_TOKENS - 1)
+ return NULL;
+ /* in case token is already generated, return it */
+ if ((*dyn_tokens)[index])
+ return (*dyn_tokens)[index];
+ /* generate token */
+ inst->f(&(*dyn_tokens)[index], NULL, dyn_tokens);
+ /* return immediately if there are no more tokens to expect */
+ if (!(*dyn_tokens)[index])
+ return NULL;
+ /* terminate list with a NULL entry */
+ (*dyn_tokens)[index + 1] = NULL;
+ return (*dyn_tokens)[index];
+}
+