|
|
| version 1.1.2.1, 2014/01/24 15:51:32 | version 1.1.2.3, 2014/01/30 08:36:39 |
|---|---|
| Line 60 Usage() | Line 60 Usage() |
| "=== %s === %s@%s ===\n\n" | "=== %s === %s@%s ===\n\n" |
| " Syntax: cfger [options] <file_config> [data]\n\n" | " Syntax: cfger [options] <file_config> [data]\n\n" |
| "\t-v\t\tVerbose ...\n" | "\t-v\t\tVerbose ...\n" |
| "\t-l <attr>\tList A/V pair\n" | "\t-q\t\tQuiet mode\n" |
| "\t-d <attr>\tDelete A/V pair\n" | "\t-o <output>\tOutput result to file\n" |
| "\t-l\t\tList A/V pairs\n" | |
| "\t-s <attr>\tSet A/V pair\n" | "\t-s <attr>\tSet A/V pair\n" |
| "\t-g <attr>\tGet value from A/V pair\n" | "\t-g <attr>\tGet value from A/V pair\n" |
| "*\"attr\" format: [section/]attribute\n" | "*\"attr\" format: [section/]attribute\n" |
| Line 71 Usage() | Line 72 Usage() |
| int | int |
| main(int argc, char **argv) | main(int argc, char **argv) |
| { | { |
| char ch, *str, m = 0; | char ch, *str, m = 0, q = 0, szAttr[STRSIZ], szName[PATH_MAX] = { 0 }; |
| int ret = 0; | int ret = 0; |
| ait_val_t attr = AIT_VAL_INITIALIZER(attr); | |
| ait_val_t data = AIT_VAL_INITIALIZER(data); | ait_val_t data = AIT_VAL_INITIALIZER(data); |
| FILE *out = stdout; | |
| while ((ch = getopt(argc, argv, "hvl:s:g:d:")) != -1) | while ((ch = getopt(argc, argv, "hvqls:g:o:")) != -1) |
| switch (ch) { | switch (ch) { |
| case 'v': | case 'v': |
| Verbose++; | Verbose++; |
| break; | break; |
| case 'q': | |
| q = 42; | |
| break; | |
| case 'o': | |
| strlcpy(szName, optarg, sizeof szName); | |
| break; | |
| case 'l': | case 'l': |
| AIT_FREE_VAL(&attr); | |
| if (m) { | if (m) { |
| Usage(); | Usage(); |
| return 1; | return 1; |
| } else | } else |
| m = 'l'; | m = 'l'; |
| AIT_SET_STR(&attr, optarg); | |
| break; | break; |
| case 'd': | |
| AIT_FREE_VAL(&attr); | |
| if (m) { | |
| Usage(); | |
| return 1; | |
| } else | |
| m = 'd'; | |
| AIT_SET_STR(&attr, optarg); | |
| break; | |
| case 's': | case 's': |
| AIT_FREE_VAL(&attr); | |
| if (m) { | if (m) { |
| Usage(); | Usage(); |
| return 1; | return 1; |
| } else | } else |
| m = 's'; | m = 's'; |
| AIT_SET_STR(&attr, optarg); | strlcpy(szAttr, optarg, sizeof szAttr); |
| break; | break; |
| case 'g': | case 'g': |
| AIT_FREE_VAL(&attr); | |
| if (m) { | if (m) { |
| Usage(); | Usage(); |
| return 1; | return 1; |
| } else | } else |
| m = 'g'; | m = 'g'; |
| AIT_SET_STR(&attr, optarg); | strlcpy(szAttr, optarg, sizeof szAttr); |
| break; | break; |
| case 'h': | case 'h': |
| default: | default: |
| Line 137 main(int argc, char **argv) | Line 131 main(int argc, char **argv) |
| goto end; | goto end; |
| } | } |
| #if 0 | if (*szName) { |
| out = fopen(szName, "w"); | |
| if (!out) { | |
| printf("Error:: Can't create file %s\n", szName); | |
| ret = 2; | |
| out = stdout; | |
| goto end; | |
| } | |
| } | |
| switch (m) { | switch (m) { |
| case 'g': | case 'g': |
| if (!xr.xml_namespace.vallen) { | if (!q) |
| if (ret == 32) { | fprintf(out, "%s = ", szAttr); |
| if (!(ctx = (char*) axl_doc_get_content_at(doc, xr.xml_node.path.value, &ctxlen))) { | str = strchr(szAttr, '/'); |
| printf("GET:: path %s - not found!\n", xr.xml_node.path.value); | if (str) { |
| ret = 1; | *str++ = 0; |
| goto end; | str = (char*) cfg_getAttribute(&cfg, szAttr, str); |
| } | } else |
| } else { | str = (char*) cfg_getAttribute(&cfg, str, szAttr); |
| if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { | fprintf(out, "%s\n", str ? str : "\rError:: Variable not found!"); |
| printf("GET:: node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } else { | |
| strlcpy(str, xr.xml_namespace.value, sizeof str); | |
| strlcat(str, ":", sizeof str); | |
| strlcat(str, xr.xml_node.container.value, sizeof str); | |
| if (ret == 32) { | |
| if (!(ctx = (char*) axl_doc_get_content_at(doc, str, &ctxlen))) { | |
| printf("GET:: path %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, str))) { | |
| printf("GET:: node %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } | |
| if (!(ret & 32) && xr.xml_data.vallen) { | |
| if (!(ctx = (char*) axl_node_get_content(node, &ctxlen))) { | |
| printf("GET:: data %s for node %s - not found!\n", | |
| xr.xml_data.value, xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } else | |
| VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); | |
| VERB(1) printf("\n"); | |
| if (!strcmp(ctx, xr.xml_data.value)) | |
| printf("DATA::1\n"); | |
| else | |
| printf("DATA::0\n"); | |
| } | |
| if (!(ret & 32) && xr.xml_attribute.vallen) { | |
| if ((n = axl_node_num_attributes(node)) < 1) { | |
| printf("GET:: attribute %s for node %s - not found!\n", | |
| xr.xml_attribute.value, xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } else { | |
| VERB(1) printf("Verbose:: node have %d attributes\n", n); | |
| if (!(ctx = (char*) axl_node_get_attribute_value(node, xr.xml_attribute.value))) { | |
| printf("GET:: attribute %s for node %s - not found!\n", | |
| xr.xml_attribute.value, xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| if (xr.xml_value.vallen) { | |
| if (!strcmp(ctx, xr.xml_value.value)) | |
| ctx = "VALUE::1"; | |
| else | |
| ctx = "VALUE::0"; | |
| } | |
| } | |
| } else { | |
| if (!(ret & 32) && !(ctx = (char*) axl_node_get_content(node, &ctxlen))) { | |
| printf("GET:: data for node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } else | |
| VERB(3) printf("Verbose(3):: Returned bytes %d\n", ctxlen); | |
| } | |
| VERB(1) printf("\n"); | |
| printf("%s\n", ctx); | |
| ret = 0; | |
| break; | break; |
| case 'd': | |
| if (!xr.xml_namespace.vallen) { | |
| if (ret == 32) { | |
| if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { | |
| printf("DEL:: path %s - not found!\n", xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { | |
| printf("DEL:: node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } else { | |
| strlcpy(str, xr.xml_namespace.value, sizeof str); | |
| strlcat(str, ":", sizeof str); | |
| strlcat(str, xr.xml_node.container.value, sizeof str); | |
| if (ret == 32) { | |
| if (!(node = axl_doc_get(doc, str))) { | |
| printf("DEL:: path %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, str))) { | |
| printf("DEL:: node %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } | |
| axl_node_remove(node, 1); | |
| ret = ShowXML(doc, NULL); | |
| break; | |
| case 's': | case 's': |
| if (ret == 32) { | str = strchr(szAttr, '/'); |
| if (!xr.xml_data.vallen || !(nnode = axl_node_create(xr.xml_data.value))) { | if (str) { |
| printf("SET:: container %s at path %s - Error!\n", | *str++ = 0; |
| xr.xml_data.value, xr.xml_node.path.value); | if (AIT_ISEMPTY(&data)) |
| ret = 1; | cfg_unsetAttribute(&cfg, szAttr, str); |
| goto end; | else |
| } | cfg_setAttribute(&cfg, szAttr, str, |
| } | AIT_GET_STRZ(&data)); |
| if (!xr.xml_namespace.vallen) { | |
| if (ret == 32) { | |
| // insert new | |
| if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { | |
| printf("SET:: path %s - not found!\n", xr.xml_node.path.value); | |
| axl_node_free(nnode); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| // update old | |
| if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { | |
| printf("SET:: node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } else { | } else { |
| strlcpy(str, xr.xml_namespace.value, sizeof str); | if (AIT_ISEMPTY(&data)) |
| strlcat(str, ":", sizeof str); | cfg_unsetAttribute(&cfg, str, szAttr); |
| strlcat(str, xr.xml_node.container.value, sizeof str); | else |
| if (ret == 32) { | cfg_setAttribute(&cfg, str, szAttr, |
| // insert new | AIT_GET_STRZ(&data)); |
| if (!(node = axl_doc_get(doc, str))) { | |
| printf("SET:: path %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.path.value); | |
| axl_node_free(nnode); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| // update old | |
| if (!(node = axl_doc_find_called(doc, str))) { | |
| printf("SET:: node %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } | } |
| if (!(ret & 32) && xr.xml_data.vallen) { | |
| axl_node_set_is_empty(node, 1); | |
| axl_node_set_content(node, xr.xml_data.value, xr.xml_data.vallen); | |
| } | |
| if (!(ret & 32) && xr.xml_attribute.vallen) { | |
| axl_node_remove_attribute(node, xr.xml_attribute.value); | |
| axl_node_set_attribute(node, xr.xml_attribute.value, xr.xml_value.value); | |
| } | |
| if (ret & 32) | |
| axl_node_set_child(node, nnode); | |
| ret = ShowXML(doc, NULL); | |
| break; | |
| case 'l': | case 'l': |
| if (!xr.xml_namespace.vallen) { | |
| if (ret == 32) { | |
| if (!(node = axl_doc_get(doc, xr.xml_node.path.value))) { | |
| printf("LST:: path %s - not found!\n", xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, xr.xml_node.container.value))) { | |
| printf("LST:: node %s - not found!\n", xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } else { | |
| strlcpy(str, xr.xml_namespace.value, sizeof str); | |
| strlcat(str, ":", sizeof str); | |
| strlcat(str, xr.xml_node.container.value, sizeof str); | |
| if (ret == 32) { | |
| if (!(node = axl_doc_get(doc, str))) { | |
| printf("LST:: path %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.path.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } else { | |
| if (!(node = axl_doc_find_called(doc, str))) { | |
| printf("LST:: node %s:%s - not found!\n", xr.xml_namespace.value, | |
| xr.xml_node.container.value); | |
| ret = 1; | |
| goto end; | |
| } | |
| } | |
| } | |
| ret = ShowItem(node, 0); | |
| break; | |
| default: | default: |
| ret = ShowXML(doc, xr.xml_data.vallen ? xr.xml_data.value : NULL); | cfgWriteConfig(out, &cfg, 42); |
| break; | |
| } | } |
| #endif | |
| end: | end: |
| if (out != stdout) | |
| fclose(out); | |
| AIT_FREE_VAL(&data); | AIT_FREE_VAL(&data); |
| cfgUnloadConfig(&cfg); | cfgUnloadConfig(&cfg); |
| return ret; | return ret; |