Skip to content

Commit

Permalink
Additional fixes in new options library
Browse files Browse the repository at this point in the history
  • Loading branch information
CurlyMoo committed Oct 9, 2018
1 parent 953152e commit b6e7115
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
47 changes: 39 additions & 8 deletions libs/pilight/core/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int options_exists(struct options_t *opt, char *id) {
}

/* Get a certain option argument type identified by the id */
int options_get_argtype(struct options_t *opt, char *id, int is_long, int *out) {
static int options_get_argtype(struct options_t *opt, char *id, int is_long, int *out) {
if(id == NULL) {
return -1;
}
Expand All @@ -152,13 +152,14 @@ int options_get_argtype(struct options_t *opt, char *id, int is_long, int *out)
}

/* Get a certain option argument type identified by the id */
int options_get_conftype(struct options_t *opt, char *id, int *out) {
static int options_get_conftype(struct options_t *opt, char *id, int is_long, int *out) {

struct options_t *temp = opt;
*out = 0;
while(temp) {
if(temp->id != NULL && id != NULL && strcmp(temp->id, id) == 0) {
if(temp->argtype != 0) {
if((is_long == 0 && temp->id != NULL && strcmp(temp->id, id) == 0) ||
(is_long == 1 && temp->name != NULL && strcmp(temp->name, id) == 0)) {
if(temp->conftype != 0) {
*out = temp->conftype;
return 0;
} else {
Expand All @@ -171,6 +172,27 @@ int options_get_conftype(struct options_t *opt, char *id, int *out) {
return -1;
}

/* Get a certain option argument type identified by the id */
// static int options_get_vartype(struct options_t *opt, char *id, int is_long, int *out) {

// struct options_t *temp = opt;
// *out = 0;
// while(temp) {
// if((is_long == 0 && temp->id != NULL && strcmp(temp->id, id) == 0) ||
// (is_long == 1 && temp->name != NULL && strcmp(temp->name, id) == 0)) {
// if(temp->vartype != 0) {
// *out = temp->vartype;
// return 0;
// } else {
// return -1;
// }
// }
// temp = temp->next;
// }

// return -1;
// }

/* Get a certain option name identified by the id */
int options_get_name(struct options_t *opt, char *name, char **out) {

Expand Down Expand Up @@ -278,7 +300,9 @@ int options_parse(struct options_t *opt, int argc, char **argv) {
str[len+x] = '\0';
len += x+1;
}
str[len-1] = '\0';
if(argc > 1) {
str[len-1] = '\0';
}

i = 0;
while(i < len) {
Expand Down Expand Up @@ -339,7 +363,14 @@ int options_parse(struct options_t *opt, int argc, char **argv) {
len -= x;
}

if(is_long == 1 && key != NULL && options_get_name(opt, key, &name) != 0) {
if(key == NULL) {
logprintf(LOG_ERR, "invalid option -- '%s'", str);
if(val != NULL) {
FREE(val);
}
FREE(str);
return -1;
} else if(is_long == 1 && key != NULL && options_get_name(opt, key, &name) != 0) {
logprintf(LOG_ERR, "invalid option -- '--%s'", key);
FREE(key);
if(val != NULL) {
Expand Down Expand Up @@ -477,8 +508,8 @@ void options_add(struct options_t **opt, char *id, const char *name, int argtype
FREE(nname);
exit(EXIT_FAILURE);
} else if(options_get_id(*opt, nname, &sid) == 0 &&
((options_get_conftype(*opt, sid, &itmp) == 0 && itmp == conftype) ||
(options_get_conftype(*opt, sid, &itmp) != 0))) {
((options_get_conftype(*opt, sid, 0, &itmp) == 0 && itmp == conftype) ||
(options_get_conftype(*opt, sid, 0, &itmp) != 0))) {
logprintf(LOG_CRIT, "duplicate option name: %s", name);
FREE(nname);
exit(EXIT_FAILURE);
Expand Down
4 changes: 4 additions & 0 deletions libs/pilight/core/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ int options_get_number(struct options_t *options, char *id, int *out);
int options_get_id(struct options_t *opt, char *id, char **out);
int options_exists(struct options_t *options, char *id);
int options_list(struct options_t *options, int i, char **name);
int options_get_id_by_name(struct options_t *opt, char *name, char **out);
// int options_get_argtype(struct options_t *opt, char *id, int is_long, int *out);
// int options_get_conftype(struct options_t *opt, char *id, int is_long, int *out);
// int options_get_vartype(struct options_t *opt, char *id, int is_long, int *out);
int options_parse1(struct options_t **options, int argc, char **argv, int error_check, char **optarg, char **ret);
int options_parse(struct options_t *options, int argc, char **argv);
void options_add(struct options_t **options, char *id, const char *name, int argtype, int conftype, int vartype, void *def, const char *mask);
Expand Down
23 changes: 23 additions & 0 deletions tests/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@

#include "alltests.h"

static void test_options_absent(CuTest *tc) {
printf("[ %-48s ]\n", __FUNCTION__);
fflush(stdout);

memtrack();

struct options_t *options = NULL;
char *argv[1] = {
"./pilight-unittest"
};
int argc = sizeof(argv)/sizeof(argv[0]);

options_add(&options, "A", "ab", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);

options_parse(options, argc, argv);

options_delete(options);
options_gc();

CuAssertIntEquals(tc, 0, xfree());
}

static void test_options_valid(CuTest *tc) {
printf("[ %-48s ]\n", __FUNCTION__);
fflush(stdout);
Expand Down Expand Up @@ -354,6 +376,7 @@ static void test_options_mask(CuTest *tc) {
CuSuite *suite_options(void) {
CuSuite *suite = CuSuiteNew();

SUITE_ADD_TEST(suite, test_options_absent);
SUITE_ADD_TEST(suite, test_options_valid);
SUITE_ADD_TEST(suite, test_options_invalid);
SUITE_ADD_TEST(suite, test_options_merge);
Expand Down

0 comments on commit b6e7115

Please sign in to comment.