Skip to content

Commit

Permalink
Change TARG_* to T_ARG_* for consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Nov 22, 2014
1 parent 2d8ae20 commit 507a0c9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
22 changes: 11 additions & 11 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ 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:
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
T_ARG_INT 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
T_ARG_TOKEN one of a set of tokens in the subtokens field
T_ARG_HELP special type for the "help" command, see below
- subtokens
A t_token table contains a set of tokens to be used if this token is
used. For example, The T_SHOW token above might have a set of subtokens
selecting the thing to show (see example). When not used with TARG_TOKEN,
selecting the thing to show (see example). When not used with T_ARG_TOKEN,
this table defines a new context
- help
A freeform string which will be shown when help for this token is requested.
Expand Down Expand Up @@ -93,15 +93,15 @@ Here's a complete example implementing a "show" command:
};
t_token tokens[] = {
{ T_SHOW, 0, tokens_show, "Show information" },
{ T_HELP, TARG_HELP, NULL, "Available commands" },
{ T_HELP, T_ARG_HELP, NULL, "Available commands" },
{ }
};

If you define tokens with the command string "history" or "help", these
will be handled internally, not passed on to your callback. The "history"
command simply prints the command history for this session, and the "help"
command provides help for the current context. Define the help command with
the TARG_HELP type: this lets you do tab completion on keywords after the
the T_ARG_HELP type: this lets you do tab completion on keywords after the
help command.

The above definitions are enough to make this work:
Expand Down Expand Up @@ -156,7 +156,7 @@ void tl_set_callback(t_tokenline *tl, tl_callback callback)

The list of entered tokens is in the tokens field, terminated by a 0 (this
is why token integers must not be 0). An argument is represented by one
of the TARG_* types (see above), followed by an integer representing an
of the T_ARG_* types (see above), followed by an integer representing an
offset into the buf field where the value lives. This value is simply
sizeof(int), sizeof(float) or a NULL-terminated string. For example,
the following command line:
Expand All @@ -165,7 +165,7 @@ void tl_set_callback(t_tokenline *tl, tl_callback callback)

Would be represented like this in the tokens field:

<T_SHOW> <T_DEVICE> <TARG_INT> 0 <T_FILE> <TARG_STRING> 4 0
<T_SHOW> <T_DEVICE> <T_ARG_INT> 0 <T_FILE> <T_ARG_STRING> 4 0

With the buf field containing the following:

Expand Down
8 changes: 4 additions & 4 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_set[] = {
{ T_FREQUENCY, TARG_FREQ, NULL, "Frequency" },
{ T_FREQUENCY, T_ARG_FREQ, NULL, "Frequency" },
{ }
};
t_token tokens_hardware[] = {
Expand All @@ -55,14 +55,14 @@ t_token tokens_hardware[] = {
t_token tokens_show[] = {
{ T_HARDWARE, 0, tokens_hardware, "Hardware information" },
{ T_VERSION, 0, NULL, "Version" },
{ T_DEVICE, TARG_INT, NULL, "Device" },
{ T_DIRECTORY, TARG_STRING, NULL, "Directory" },
{ T_DEVICE, T_ARG_INT, NULL, "Device" },
{ T_DIRECTORY, T_ARG_STRING, NULL, "Directory" },
{ }
};
t_token tokens[] = {
{ T_SHOW, 0, tokens_show, "Show information" },
{ T_SET, 0, tokens_set, "Set things" },
{ T_HELP, TARG_HELP, NULL, "Available commands" },
{ T_HELP, T_ARG_HELP, NULL, "Available commands" },
{ }
};

8 changes: 4 additions & 4 deletions demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ 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 TARG_INT:
case T_ARG_INT:
memcpy(&arg_int, p->buf + p->tokens[++i], sizeof(int));
printf("integer %d\n", arg_int);
break;
case TARG_FLOAT:
case T_ARG_FLOAT:
memcpy(&arg_float, p->buf + p->tokens[++i], sizeof(float));
printf("float %f\n", arg_float);
break;
case TARG_FREQ:
case T_ARG_FREQ:
memcpy(&arg_float, p->buf + p->tokens[++i], sizeof(float));
printf("frequency %f\n", arg_float);
break;
case TARG_STRING:
case T_ARG_STRING:
printf("string '%s'\n", p->buf + p->tokens[++i]);
break;
default:
Expand Down
34 changes: 17 additions & 17 deletions tokenline.c
Original file line number Diff line number Diff line change
Expand Up @@ -327,12 +327,12 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
t = token_stack[cur_tsp][t_idx].token;
p->tokens[cur_tp++] = t;
p->last_token_entry = &token_stack[cur_tsp][t_idx];
if (token_stack[cur_tsp][t_idx].arg_type == TARG_HELP) {
if (token_stack[cur_tsp][t_idx].arg_type == T_ARG_HELP) {
/* Nothing to do, just keep cur_tsp from increasing. */
} else if (token_stack[cur_tsp][t_idx].arg_type) {
/* Token needs an argument */
arg_needed = token_stack[cur_tsp][t_idx].arg_type;
if (arg_needed == TARG_TOKEN)
if (arg_needed == T_ARG_TOKEN)
/* Argument is one of these subtokens. */
arg_tokens = token_stack[cur_tsp][t_idx].subtokens;
} else if (token_stack[cur_tsp][t_idx].subtokens) {
Expand All @@ -350,31 +350,31 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
} else {
/* Parse word as the type in arg_needed */
switch (arg_needed) {
case TARG_INT:
case T_ARG_INT:
arg_int = strtol(word, &suffix, 0);
if (*suffix) {
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
p->tokens[cur_tp++] = TARG_INT;
p->tokens[cur_tp++] = T_ARG_INT;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_int, sizeof(int));
cur_bufsize += sizeof(int);
break;
case TARG_FLOAT:
case T_ARG_FLOAT:
arg_float = strtof(word, &suffix);
if (*suffix) {
if (!complete_tokens)
tl->print(tl->user, "Invalid value."NL);
return FALSE;
}
p->tokens[cur_tp++] = TARG_FLOAT;
p->tokens[cur_tp++] = T_ARG_FLOAT;
p->tokens[cur_tp++] = cur_bufsize;
memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float));
cur_bufsize += sizeof(float);
break;
case TARG_FREQ:
case T_ARG_FREQ:
arg_float = strtof(word, &suffix);
if (*suffix) {
if (!strcmp(suffix, "khz"))
Expand All @@ -389,20 +389,20 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
return FALSE;
}
}
p->tokens[cur_tp++] = TARG_FREQ;
p->tokens[cur_tp++] = T_ARG_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;
case T_ARG_STRING:
p->tokens[cur_tp++] = T_ARG_STRING;
p->tokens[cur_tp++] = cur_bufsize;
size = strlen(word) + 1;
memcpy(p->buf + cur_bufsize, word, size);
cur_bufsize += size;
p->buf[cur_bufsize] = 0;
break;
case TARG_TOKEN:
case T_ARG_TOKEN:
if ((t_idx = find_token(arg_tokens, tl->token_dict, word)) > -1) {
p->tokens[cur_tp++] = arg_tokens[t_idx].token;
p->last_token_entry = &arg_tokens[t_idx];
Expand All @@ -429,7 +429,7 @@ static int tokenize(t_tokenline *tl, int *words, int num_words,
*complete_tokens = NULL;
} else {
/* Fill in the completion token list. */
if (arg_needed == TARG_TOKEN)
if (arg_needed == T_ARG_TOKEN)
*complete_tokens = arg_tokens;
else
*complete_tokens = token_stack[cur_tsp];
Expand Down Expand Up @@ -613,18 +613,18 @@ static void complete(t_tokenline *tl)
if (!split_line(tl, words, &num_words, TRUE) || !num_words)
return;
if (tokenize(tl, words, num_words, &tokens, &arg_needed)) {
if (arg_needed && arg_needed != TARG_TOKEN) {
if (arg_needed && arg_needed != T_ARG_TOKEN) {
switch (arg_needed) {
case TARG_INT:
case T_ARG_INT:
tl->print(tl->user, INDENT NL"<integer>"NL);
break;
case TARG_FLOAT:
case T_ARG_FLOAT:
tl->print(tl->user, INDENT NL"<float>"NL);
break;
case TARG_FREQ:
case T_ARG_FREQ:
tl->print(tl->user, INDENT NL"<frequency>"NL);
break;
case TARG_STRING:
case T_ARG_STRING:
tl->print(tl->user, INDENT NL"<string>"NL);
break;
}
Expand Down
12 changes: 6 additions & 6 deletions tokenline.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ typedef struct tokenline {

/* These share a number space with the tokens. */
enum token_argtypes {
TARG_INT = 10000,
TARG_FLOAT,
TARG_STRING,
T_ARG_INT = 10000,
T_ARG_FLOAT,
T_ARG_STRING,
/* Floating point number optionally followed by khz, mhz or ghz. */
TARG_FREQ,
T_ARG_FREQ,
/* Argument is one of the tokens in subtokens. */
TARG_TOKEN,
TARG_HELP,
T_ARG_TOKEN,
T_ARG_HELP,
};

void tl_init(t_tokenline *tl, t_token *tokens_top, t_token_dict *token_dict,
Expand Down

0 comments on commit 507a0c9

Please sign in to comment.