Skip to content

Commit

Permalink
Add TARG_FREQ parameter type: frequency with optional suffix.
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Nov 22, 2014
1 parent dae8ca6 commit 2d5b0da
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Only the token field is required; all others are optional:
be filled in here. The following are available:
TARG_INT integer
TARG_FLOAT floating-point value
TARG_FREQ frequency, as float followed by khz, mhz or ghz
TARG_STRING freeform string, should be quoted if it contains spaces
TARG_TOKEN one of a set of tokens in the subtokens field
TARG_HELP special type for the "help" command, see below
Expand Down
9 changes: 9 additions & 0 deletions demo/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,32 @@

enum {
T_SHOW = 1,
T_SET,
T_HARDWARE,
T_VERSION,
T_DEVICE,
T_DIRECTORY,
T_HELP,
T_CPU,
T_MEMORY,
T_FREQUENCY,
};
t_token_dict dict[] = {
{ /* Dummy entry */ },
{ T_SHOW, "show" },
{ T_SET, "set" },
{ T_HARDWARE, "hardware" },
{ T_VERSION, "version" },
{ T_DEVICE, "device" },
{ T_DIRECTORY, "directory" },
{ T_HELP, "help" },
{ T_CPU, "cpu" },
{ T_MEMORY, "memory" },
{ T_FREQUENCY, "frequency" },
{ }
};
t_token tokens_set[] = {
{ T_FREQUENCY, TARG_FREQ, NULL, "Frequency" },
{ }
};
t_token tokens_hardware[] = {
Expand All @@ -53,6 +61,7 @@ t_token tokens_show[] = {
};
t_token tokens[] = {
{ T_SHOW, 0, tokens_show, "Show information" },
{ T_SET, 0, tokens_set, "Set things" },
{ T_HELP, TARG_HELP, NULL, "Available commands" },
{ }
};
Expand Down
4 changes: 4 additions & 0 deletions demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ static void dump_parsed(void *user, t_tokenline_parsed p)
memcpy(&arg_float, p.buf + p.tokens[++i], sizeof(float));
printf("float %f\n", arg_float);
break;
case TARG_FREQ:
memcpy(&arg_float, p.buf + p.tokens[++i], sizeof(float));
printf("frequency %f\n", arg_float);
break;
case TARG_STRING:
printf("string '%s'\n", p.buf + p.tokens[++i]);
break;
Expand Down
23 changes: 23 additions & 0 deletions tokenline.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,26 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float));
cur_bufsize += sizeof(float);
break;
case TARG_FREQ:
arg_float = strtof(word, &suffix);
if (*suffix) {
if (!strcmp(suffix, "khz"))
arg_float *= 1000;
else if (!strcmp(suffix, "mhz"))
arg_float *= 1000000;
else if (!strcmp(suffix, "ghz"))
arg_float *= 1000000000L;
else {
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
}
p->tokens[cur_tp++] = TARG_FREQ;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float));
cur_bufsize += sizeof(float);
break;
case TARG_STRING:
p->tokens[cur_tp++] = TARG_STRING;
p->tokens[cur_tp++] = cur_bufsize;
Expand Down Expand Up @@ -601,6 +621,9 @@ static void complete(t_tokenline *tl)
case TARG_FLOAT:
tl->print(tl->user, INDENT NL"<float>"NL);
break;
case TARG_FREQ:
tl->print(tl->user, INDENT NL"<frequency>"NL);
break;
case TARG_STRING:
tl->print(tl->user, INDENT NL"<string>"NL);
break;
Expand Down
2 changes: 2 additions & 0 deletions tokenline.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ enum token_argtypes {
TARG_INT = 10000,
TARG_FLOAT,
TARG_STRING,
/* Floating point number optionally followed by khz, mhz or ghz. */
TARG_FREQ,
/* Argument is one of the tokens in subtokens. */
TARG_TOKEN,
TARG_HELP,
Expand Down

0 comments on commit 2d5b0da

Please sign in to comment.