Skip to content

Commit

Permalink
Merge pull request #28 from allanjude/nofoldcase
Browse files Browse the repository at this point in the history
Don't fold case by default
  • Loading branch information
allanjude authored Dec 6, 2021
2 parents e61e069 + a90d4e5 commit 14f881f
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ EXECUTABLE=uclcmd

all: $(SRCS) $(EXECUTABLE)

$(EXECUTABLE): $(OBJS)
$(EXECUTABLE): $(OBJS) uclcmd.h
$(CC) $(LDFLAGS) $(LIBS) -o $(EXECUTABLE) $(OBJS)

clean:
Expand Down
10 changes: 2 additions & 8 deletions tests/include-merge-high_01.res
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
vm1 {
cpus [
1,
2,
]
cpus = 2;
origin = 4;
mode = "merge";
sub {
a = "b";
c [
"d",
"e",
]
c = "e";
foo = "bar";
}
from = "test";
Expand Down
10 changes: 2 additions & 8 deletions tests/include-merge-low_01.res
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
vm1 {
cpus [
1,
2,
]
cpus = 1;
origin = 4;
mode = "merge";
sub {
a = "b";
c [
"d",
"e",
]
c = "d";
foo = "bar";
}
from = "test";
Expand Down
2 changes: 1 addition & 1 deletion tests/set_08.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ rootkey {
"b",
"c",
]
new = 1103760000.0;
new = 157680000.0;
}
18 changes: 14 additions & 4 deletions uclcmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

int debug = 0, expand = 0, mode = 0, noop = 0, nonewline = 0;
int show_keys = 0, show_raw = 0;
int pflags = 0;
bool firstline = true, shvars = false;
int output_type = 254;
ucl_object_t *root_obj = NULL;
Expand Down Expand Up @@ -70,6 +71,7 @@ main(int argc, char *argv[])
{ "del", remove_main },
{ "dump", output_main },
{ "help", (verb_func_t) usage },
{ "version", (verb_func_t) version },
{ NULL, NULL }
};

Expand Down Expand Up @@ -109,17 +111,18 @@ void
usage()
{
fprintf(stderr, "%s\n",
"Usage: uclcmd get [-cdejklmNquy] [-D char] [-f file] [-o file] variable\n"
" uclcmd set [-cdjmnuy] [-t type] [-D char] [-f file] [-i file] [-o file] variable [UCL]\n"
" uclcmd merge [-cdjmnuy] [-D char] [-f file] [-i file] [-o file] variable\n"
" uclcmd remove [-cdjmnuy] [-D char] [-f file] [-o file] variable\n"
"Usage: uclcmd get [-cdeIjklmNquy] [-D char] [-f file] [-o file] variable\n"
" uclcmd set [-cdIjmnuy] [-t type] [-D char] [-f file] [-i file] [-o file] variable [UCL]\n"
" uclcmd merge [-cdIjmnuy] [-D char] [-f file] [-i file] [-o file] variable\n"
" uclcmd remove [-cdIjmnuy] [-D char] [-f file] [-o file] variable\n"
"\n"
"COMMON OPTIONS:\n"
" -c --cjson output compacted JSON\n"
" -d --debug enable verbose debugging output\n"
" -D --delimiter character to use as element delimiter (default is .)\n"
" -e --expand Output the list of keys when encountering an object\n"
" -f --file path to a file to read or write\n"
" -I --foldcase fold all keys to lowercase (make matching insensitive)\n"
" -j --json output pretty JSON\n"
" -k --keys show key=value rather than just the value\n"
" -l --shellvars keys are output with underscores as delimiter\n"
Expand Down Expand Up @@ -157,6 +160,13 @@ usage()
exit(1);
}

void
version()
{

fprintf(stderr, "uclcmd version %s\n", UCLCMD_VERSION_STRING);
}

void
cleanup()
{
Expand Down
10 changes: 10 additions & 0 deletions uclcmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,23 @@

#include <ucl.h>

#define QUOTE(str) #str
#define EXPAND_AND_QUOTE(str) QUOTE(str)
#ifndef __DECONST
#define __DECONST(type, var) ((type)(uintptr_t)(const void *)(var))
#endif

#define UCLCMD_VERSION_MAJOR 0
#define UCLCMD_VERSION_MINOR 2
#define UCLCMD_VERSION_PATCH 20211204
#define UCLCMD_VERSION UCLCMD_VERSION_MAJOR.UCLCMD_VERSION_MINOR.UCLCMD_VERSION_PATCH
#define UCLCMD_VERSION_STRING EXPAND_AND_QUOTE(UCLCMD_VERSION)

#define UCLCMD_PARSER_FLAGS UCL_PARSER_NO_IMPLICIT_ARRAYS | \
UCL_PARSER_SAVE_COMMENTS

extern int debug, expand, noop, nonewline, show_keys, show_raw;
extern int pflags;
extern bool firstline, shvars;
extern int output_type;
extern ucl_object_t *root_obj;
Expand Down Expand Up @@ -106,6 +115,7 @@ char * objtype_as_string (const ucl_object_t *obj);
void ucl_obj_dump(const ucl_object_t *obj, unsigned int shift);
void ucl_obj_dump_safe(const ucl_object_t *obj, unsigned int shift);
void usage();
void version();

int get_cmd_each(const ucl_object_t *obj, char *nodepath,
const char *command_str, char *remaining_commands, int recurse);
Expand Down
23 changes: 12 additions & 11 deletions uclcmd_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ get_main(int argc, char *argv[])
{
int ret = 0, k = 0, ch;

/* Initialize parser */
parser = ucl_parser_new(UCLCMD_PARSER_FLAGS | UCL_PARSER_KEY_LOWERCASE);

/* options descriptor */
static struct option longopts[] = {
{ "cjson", no_argument, &output_type,
Expand All @@ -48,6 +45,7 @@ get_main(int argc, char *argv[])
UCL_EMIT_JSON },
{ "keys", no_argument, &show_keys, 1 },
{ "input", no_argument, NULL, 'i' },
{ "foldcase", no_argument, NULL, 'I' },
{ "msgpack", no_argument, &output_type,
UCL_EMIT_MSGPACK },
{ "noop", no_argument, &noop, 1 },
Expand All @@ -61,7 +59,7 @@ get_main(int argc, char *argv[])
{ NULL, 0, NULL, 0 }
};

while ((ch = getopt_long(argc, argv, "cdD:ef:i:jklmnNo:quy", longopts, NULL)) != -1) {
while ((ch = getopt_long(argc, argv, "cdD:ef:i:IjklmnNo:quy", longopts, NULL)) != -1) {
switch (ch) {
case 'c':
output_type = UCL_EMIT_JSON_COMPACT;
Expand All @@ -82,17 +80,14 @@ get_main(int argc, char *argv[])
break;
case 'f':
filename = optarg;
if (strcmp(optarg, "-") == 0) {
/* Input from STDIN */
root_obj = parse_input(parser, stdin);
} else {
root_obj = parse_file(parser, filename);
}
break;
case 'i':
fprintf(stderr, "Not implemented yet\n");
exit(1);
break;
case 'I':
pflags |= UCL_PARSER_KEY_LOWERCASE;
break;
case 'j':
output_type = UCL_EMIT_JSON;
break;
Expand Down Expand Up @@ -140,8 +135,14 @@ get_main(int argc, char *argv[])
usage();
}

if (filename == NULL) {
/* Initialize parser */
parser = ucl_parser_new(UCLCMD_PARSER_FLAGS | pflags);

if (filename == NULL || strcmp(filename, "-") == 0) {
/* Input from STDIN */
root_obj = parse_input(parser, stdin);
} else {
root_obj = parse_file(parser, filename);
}

for (k = 0; k < argc; k++) {
Expand Down
23 changes: 12 additions & 11 deletions uclcmd_merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ merge_main(int argc, char *argv[])
int ret = 0, ch;
bool success = false;

/* Initialize parser */
parser = ucl_parser_new(UCLCMD_PARSER_FLAGS);

/* Set the default output type */
output_type = UCL_EMIT_CONFIG;

Expand All @@ -52,6 +49,7 @@ merge_main(int argc, char *argv[])
UCL_EMIT_JSON },
{ "keys", no_argument, &show_keys, 1 },
{ "input", no_argument, NULL, 'i' },
{ "foldcase", no_argument, NULL, 'I' },
{ "msgpack", no_argument, &output_type,
UCL_EMIT_MSGPACK },
{ "noop", no_argument, &noop, 1 },
Expand All @@ -65,7 +63,7 @@ merge_main(int argc, char *argv[])
{ NULL, 0, NULL, 0 }
};

while ((ch = getopt_long(argc, argv, "cdD:ef:i:jklmnNo:quy", longopts, NULL)) != -1) {
while ((ch = getopt_long(argc, argv, "cdD:ef:i:IjklmnNo:quy", longopts, NULL)) != -1) {
switch (ch) {
case 'c':
output_type = UCL_EMIT_JSON_COMPACT;
Expand All @@ -86,16 +84,13 @@ merge_main(int argc, char *argv[])
break;
case 'f':
filename = optarg;
if (strcmp(optarg, "-") == 0) {
/* Input from STDIN */
root_obj = parse_input(parser, stdin);
} else {
root_obj = parse_file(parser, filename);
}
break;
case 'i':
include_file = optarg;
break;
case 'I':
pflags |= UCL_PARSER_KEY_LOWERCASE;
break;
case 'j':
output_type = UCL_EMIT_JSON;
break;
Expand Down Expand Up @@ -142,8 +137,14 @@ merge_main(int argc, char *argv[])
usage();
}

if (filename == NULL) {
/* Initialize parser */
parser = ucl_parser_new(UCLCMD_PARSER_FLAGS | pflags);

if (filename == NULL || strcmp(filename, "-") == 0) {
/* Input from STDIN */
root_obj = parse_input(parser, stdin);
} else {
root_obj = parse_file(parser, filename);
}

if (argc > 1) { /* XXX: need test for if > 2 inputs */
Expand Down
23 changes: 12 additions & 11 deletions uclcmd_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,26 @@ output_main(int argc, char *argv[])
const char *filename = NULL;
int ret = 0, ch;

/* Initialize parser */
parser = ucl_parser_new(UCLCMD_PARSER_FLAGS);

/* options descriptor */
static struct option longopts[] = {
{ "file", required_argument, NULL, 'f' },
{ "input", no_argument, NULL, 'i' },
{ "foldcase", no_argument, NULL, 'I' },
{ NULL, 0, NULL, 0 }
};

while ((ch = getopt_long(argc, argv, "f:i:", longopts, NULL)) != -1) {
while ((ch = getopt_long(argc, argv, "f:i:I", longopts, NULL)) != -1) {
switch (ch) {
case 'f':
filename = optarg;
if (strcmp(optarg, "-") == 0) {
/* Input from STDIN */
root_obj = parse_input(parser, stdin);
} else {
root_obj = parse_file(parser, filename);
}
break;
case 'i':
fprintf(stderr, "Not implemented yet\n");
exit(1);
break;
case 'I':
pflags |= UCL_PARSER_KEY_LOWERCASE;
break;
default:
fprintf(stderr, "Error: Unexpected option: %i\n", ch);
usage();
Expand All @@ -68,8 +63,14 @@ output_main(int argc, char *argv[])
argc -= optind;
argv += optind;

if (filename == NULL) {
/* Initialize parser */
parser = ucl_parser_new(UCLCMD_PARSER_FLAGS | pflags);

if (filename == NULL || strcmp(filename, "-") == 0) {
/* Input from STDIN */
root_obj = parse_input(parser, stdin);
} else {
root_obj = parse_file(parser, filename);
}

ucl_obj_dump(root_obj, 0);
Expand Down
Loading

0 comments on commit 14f881f

Please sign in to comment.