Skip to content

Commit

Permalink
Change T_ARG_INT to T_ARG_UINT, supporting only unsigned integers.
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Mar 26, 2016
1 parent 0f775b6 commit 4f09bdc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Only the token field is required; all others are optional:
- arg_type
If the token takes an argument, The type of that argument should
be filled in here. The following are available:
T_ARG_INT integer
T_ARG_UINT unsigned integer
T_ARG_FLOAT floating-point value
T_ARG_FREQ frequency, as float followed by khz, mhz or ghz
T_ARG_STRING freeform string, should be quoted if it contains spaces
Expand Down Expand Up @@ -87,7 +87,7 @@ Only the token field is required; all others are optional:
requested. This can be more than one line; it is intended to be a more
detailed help text than the summary provided by the "help" field.

The T_ARG_INT type may be specified as a token in the table: this allows
The T_ARG_UINT type may be specified as a token in the table: this allows
free-standing numbers to be entered on the command line. See the demo
application's 'calc' command for an example.

Expand Down Expand Up @@ -188,7 +188,7 @@ void tl_set_callback(t_tokenline *tl, tl_callback callback)

Would be represented like this in the tokens field:

<T_SHOW> <T_DEVICE> <T_ARG_INT> 0 <T_FILE> <T_ARG_STRING> 4 0
<T_SHOW> <T_DEVICE> <T_ARG_UINT> 0 <T_FILE> <T_ARG_STRING> 4 0
^ ^
argument offset in buffer --| --|

Expand Down
6 changes: 3 additions & 3 deletions demo/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ t_token_dict dict[] = {
};

t_token tokens_mode_calc[] = {
{ T_ARG_INT,
{ T_ARG_UINT,
.help = "Operand" },
{ T_ARG_STRING,
.help = "Comment" },
Expand Down Expand Up @@ -81,7 +81,7 @@ t_token tokens_set[] = {
.arg_type = T_ARG_FLOAT,
.help = "Frequency" },
{ T_NUMBER,
.arg_type = T_ARG_INT,
.arg_type = T_ARG_UINT,
.help = "Number" },
{ }
};
Expand All @@ -100,7 +100,7 @@ t_token tokens_show[] = {
{ T_VERSION,
.help = "Version" },
{ T_DEVICE,
.arg_type = T_ARG_INT,
.arg_type = T_ARG_UINT,
.help = "Device" },
{ T_DIRECTORY,
.arg_type = T_ARG_STRING,
Expand Down
2 changes: 1 addition & 1 deletion demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void dump_parsed(void *user, t_tokenline_parsed *p)
for (i = 0; p->tokens[i]; i++) {
printf("%d: ", i);
switch (p->tokens[i]) {
case T_ARG_INT:
case T_ARG_UINT:
memcpy(&arg_int, p->buf + p->tokens[++i], sizeof(int));
printf("integer %d\n", arg_int);
break;
Expand Down
34 changes: 17 additions & 17 deletions tokenline.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static int str_to_uint(char *s, uint32_t *out)

static char *arg_type_to_string(int arg_type)
{
if (arg_type == T_ARG_INT)
if (arg_type == T_ARG_UINT)
return "<integer>";
else if (arg_type == T_ARG_FLOAT)
return "<float>";
Expand All @@ -303,10 +303,10 @@ static int find_token(t_token *tokens, t_token_dict *token_dict, char *word)
/* Find exact match. */
for (i = 0; tokens[i].token; i++) {
token = tokens[i].token;
if (token == T_ARG_INT) {
if (token == T_ARG_UINT) {
if (str_to_uint(word, &arg_uint))
return i;
} else if (token > T_ARG_INT) {
} else if (token > T_ARG_UINT) {
continue;
} else {
if (!strcmp(word, token_dict[token].tokenstr))
Expand All @@ -318,7 +318,7 @@ static int find_token(t_token *tokens, t_token_dict *token_dict, char *word)
partial = -1;
for (i = 0; tokens[i].token; i++) {
token = tokens[i].token;
if (token >= T_ARG_INT)
if (token >= T_ARG_UINT)
continue;
if (strlen(word) >= strlen(token_dict[token].tokenstr))
continue;
Expand All @@ -345,7 +345,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
t_tokenline_parsed *p;
float arg_float;
uint32_t arg_uint, suffix_uint;
int done, arg_needed, arg_int, w, t, t_idx, size;
int done, arg_needed, w, t, t_idx, size;
int cur_tsp, cur_tp, cur_bufsize, i;
char *word, *suffix;

Expand Down Expand Up @@ -378,7 +378,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1) {
t = token_stack[cur_tsp][t_idx].token;
p->tokens[cur_tp++] = t;
if (t == T_ARG_INT) {
if (t == T_ARG_UINT) {
/* Integer token. */
str_to_uint(word, &arg_uint);
p->tokens[cur_tp++] = cur_bufsize;
Expand Down Expand Up @@ -443,30 +443,30 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
} else {
/* Parse word as the type in arg_needed */
switch (arg_needed) {
case T_ARG_INT:
arg_int = strtol(word, &suffix, 0);
case T_ARG_UINT:
str_to_uint(word, &arg_uint);
if (*suffix) {
switch(*suffix)
{
case 'k':
arg_int *= 1000;
arg_uint *= 1000;
break;
case 'm':
arg_int *= 1000000;
arg_uint *= 1000000;
break;
case 'g':
arg_int *= 1000000000L;
arg_uint *= 1000000000L;
break;
default:
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
}
p->tokens[cur_tp++] = T_ARG_INT;
p->tokens[cur_tp++] = T_ARG_UINT;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_int, sizeof(int));
cur_bufsize += sizeof(int);
memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t));
cur_bufsize += sizeof(uint32_t);
break;
case T_ARG_FLOAT:
arg_float = strtof(word, &suffix);
Expand Down Expand Up @@ -567,7 +567,7 @@ static void show_help(t_tokenline *tl, int *words, int num_words)
if (tokens) {
for (i = 0; tokens[i].token; i++) {
tl->print(tl->user, INDENT);
if (tokens[i].token < T_ARG_INT)
if (tokens[i].token < T_ARG_UINT)
s = tl->token_dict[tokens[i].token].tokenstr;
else
s = arg_type_to_string(tokens[i].token);
Expand Down Expand Up @@ -679,7 +679,7 @@ static void print_token_and_help(t_tokenline *tl, t_token *token)
char *s;

tl->print(tl->user, INDENT);
if (token->token < T_ARG_INT)
if (token->token < T_ARG_UINT)
s = tl->token_dict[token->token].tokenstr;
else
s = arg_type_to_string(token->token);
Expand Down Expand Up @@ -718,7 +718,7 @@ static void complete(t_tokenline *tl)
partial = NULL;
multiple = FALSE;
for (t = 0; tokens[t].token; t++) {
if (tokens[t].token >= T_ARG_INT)
if (tokens[t].token >= T_ARG_UINT)
continue;
if (!strncmp(word, tl->token_dict[tokens[t].token].tokenstr, strlen(word))) {
if (partial) {
Expand Down
2 changes: 1 addition & 1 deletion tokenline.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typedef struct tokenline {
/* These share a number space with the tokens. */
enum token_argtypes {
/* Optionally followed by k (kilo), m (mega) or g (giga). */
T_ARG_INT = 10000,
T_ARG_UINT = 10000,
/* Optionally followed by k (kilo), m (mega) or g (giga). */
T_ARG_FLOAT,
T_ARG_STRING,
Expand Down

0 comments on commit 4f09bdc

Please sign in to comment.