Ken Yourek / ucmd

Dependents:   nucleo_ucmd_helloworld

Committer:
kyourek
Date:
Mon Oct 12 21:09:07 2015 +0000
Revision:
0:9e2fc73e5a12
Initial commit of the ucmd library for mbed.; https://github.com/kyourek/ucmd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kyourek 0:9e2fc73e5a12 1 #include <string.h>
kyourek 0:9e2fc73e5a12 2 #include "ucmd_internal.h"
kyourek 0:9e2fc73e5a12 3
kyourek 0:9e2fc73e5a12 4 ucMemoryManager_INIT(ucSwitchOpt, ucSwitchOpt_COUNT);
kyourek 0:9e2fc73e5a12 5
kyourek 0:9e2fc73e5a12 6 static ucSwitchOpt *create_switch_opt(void) {
kyourek 0:9e2fc73e5a12 7 return ucMemoryManager_create();
kyourek 0:9e2fc73e5a12 8 }
kyourek 0:9e2fc73e5a12 9
kyourek 0:9e2fc73e5a12 10 ucArgOpt *ucSwitchOpt_get_arg_opt(ucSwitchOpt *p) {
kyourek 0:9e2fc73e5a12 11 return ucArgOptOwner_get_arg_opt((ucArgOptOwner*)p);
kyourek 0:9e2fc73e5a12 12 }
kyourek 0:9e2fc73e5a12 13
kyourek 0:9e2fc73e5a12 14 ucSwitchOpt *ucSwitchOpt_get_next(ucSwitchOpt *p) {
kyourek 0:9e2fc73e5a12 15 if (NULL == p) return NULL;
kyourek 0:9e2fc73e5a12 16 return p->next;
kyourek 0:9e2fc73e5a12 17 }
kyourek 0:9e2fc73e5a12 18
kyourek 0:9e2fc73e5a12 19 ucSwitchOpt *ucSwitchOpt_find(ucSwitchOpt *p, const char *name) {
kyourek 0:9e2fc73e5a12 20 while (NULL != p) {
kyourek 0:9e2fc73e5a12 21 if (0 == strcmp(ucOpt_get_name((ucOpt*)p), name)) {
kyourek 0:9e2fc73e5a12 22 return p;
kyourek 0:9e2fc73e5a12 23 }
kyourek 0:9e2fc73e5a12 24 p = ucSwitchOpt_get_next(p);
kyourek 0:9e2fc73e5a12 25 }
kyourek 0:9e2fc73e5a12 26 return NULL;
kyourek 0:9e2fc73e5a12 27 }
kyourek 0:9e2fc73e5a12 28
kyourek 0:9e2fc73e5a12 29 ucSwitchOpt *ucSwitchOpt_init(ucSwitchOpt *p, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt, ucSwitchOpt *next) {
kyourek 0:9e2fc73e5a12 30 if (NULL == p) return NULL;
kyourek 0:9e2fc73e5a12 31 if (NULL == ucArgOptOwner_init((ucArgOptOwner*)p, name, desc, is_required, arg_opt)) return NULL;
kyourek 0:9e2fc73e5a12 32
kyourek 0:9e2fc73e5a12 33 p->next = next;
kyourek 0:9e2fc73e5a12 34 return p;
kyourek 0:9e2fc73e5a12 35 }
kyourek 0:9e2fc73e5a12 36
kyourek 0:9e2fc73e5a12 37 ucSwitchOpt *ucSwitchOpt_create(const char *name, const char *desc, ucArgOpt *arg_opt, ucSwitchOpt *next) {
kyourek 0:9e2fc73e5a12 38 return ucSwitchOpt_init(create_switch_opt(), name, desc, ucBool_FALSE, arg_opt, next);
kyourek 0:9e2fc73e5a12 39 }
kyourek 0:9e2fc73e5a12 40
kyourek 0:9e2fc73e5a12 41 ucSwitchOpt *ucSwitchOpt_create_required(const char *name, const char *desc, ucArgOpt *arg_opt, ucSwitchOpt *next) {
kyourek 0:9e2fc73e5a12 42 return ucSwitchOpt_init(create_switch_opt(), name, desc, ucBool_TRUE, arg_opt, next);
kyourek 0:9e2fc73e5a12 43 }
kyourek 0:9e2fc73e5a12 44
kyourek 0:9e2fc73e5a12 45 void ucSwitchOpt_destroy(ucSwitchOpt *p) {
kyourek 0:9e2fc73e5a12 46 ucMemoryManager_destroy(p);
kyourek 0:9e2fc73e5a12 47 }
kyourek 0:9e2fc73e5a12 48
kyourek 0:9e2fc73e5a12 49 void ucSwitchOpt_destroy_chain(ucSwitchOpt *p) {
kyourek 0:9e2fc73e5a12 50 ucSwitchOpt *next = p;
kyourek 0:9e2fc73e5a12 51 while (NULL != next) {
kyourek 0:9e2fc73e5a12 52 p = next;
kyourek 0:9e2fc73e5a12 53 next = ucSwitchOpt_get_next(p);
kyourek 0:9e2fc73e5a12 54 ucArgOpt_destroy_chain(ucSwitchOpt_get_arg_opt(p));
kyourek 0:9e2fc73e5a12 55 ucSwitchOpt_destroy(p);
kyourek 0:9e2fc73e5a12 56 }
kyourek 0:9e2fc73e5a12 57 }
kyourek 0:9e2fc73e5a12 58
kyourek 0:9e2fc73e5a12 59 const char *ucSwitchOpt_format_validation_err(ucSwitchOpt *p, ucCmdLine *cmd, ucSwitchTok *switch_tok) {
kyourek 0:9e2fc73e5a12 60
kyourek 0:9e2fc73e5a12 61 /* check if the switch option is required */
kyourek 0:9e2fc73e5a12 62 if (ucOpt_is_required((ucOpt*)p)) {
kyourek 0:9e2fc73e5a12 63
kyourek 0:9e2fc73e5a12 64 /* check if the switch token is missing */
kyourek 0:9e2fc73e5a12 65 if (NULL == switch_tok) {
kyourek 0:9e2fc73e5a12 66
kyourek 0:9e2fc73e5a12 67 /* the switch is required, but it is not
kyourek 0:9e2fc73e5a12 68 present, so send the error */
kyourek 0:9e2fc73e5a12 69 return ucCmdLine_format_response(cmd, "%sthe switch \"%s\" is required.", ucOpt_validation_err_invalid_switch_prefix, ucOpt_get_name((ucOpt*)p));
kyourek 0:9e2fc73e5a12 70 }
kyourek 0:9e2fc73e5a12 71 }
kyourek 0:9e2fc73e5a12 72
kyourek 0:9e2fc73e5a12 73 /* return the result of the argument validation */
kyourek 0:9e2fc73e5a12 74 return ucArgOptOwner_format_validation_err(
kyourek 0:9e2fc73e5a12 75 (ucArgOptOwner*)p,
kyourek 0:9e2fc73e5a12 76 cmd,
kyourek 0:9e2fc73e5a12 77 ucSwitchTok_get_arg(switch_tok),
kyourek 0:9e2fc73e5a12 78 ucOpt_get_name((ucOpt*)p)
kyourek 0:9e2fc73e5a12 79 );
kyourek 0:9e2fc73e5a12 80 }
kyourek 0:9e2fc73e5a12 81