Skip to content

Commit

Permalink
demo: Update example to use tl_mode_push/_pop.
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Nov 22, 2014
1 parent acc8acb commit f883caf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 15 deletions.
32 changes: 20 additions & 12 deletions demo/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,8 @@
*/

#include "tokenline.h"
#include "commands.h"

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" },
Expand All @@ -41,14 +30,31 @@ t_token_dict dict[] = {
{ T_CPU, "cpu" },
{ T_MEMORY, "memory" },
{ T_FREQUENCY, "frequency" },
{ T_MKDIR, "mkdir" },
{ T_LS, "ls" },
{ T_EXIT, "exit" },
{ }
};

t_token tokens_mode_device[] = {
{ T_SHOW,
.help = "Show device information" },
{ T_MKDIR,
.help = "Create directory" },
{ T_LS,
.help = "List files and directories" },
{ T_EXIT,
.help = "Exit device mode" },
{ }
};

t_token tokens_set[] = {
{ T_FREQUENCY,
.arg_type = T_ARG_FREQ,
.help = "Frequency" },
{ }
};

t_token tokens_hardware[] = {
{ T_CPU,
.help = "CPU" },
Expand Down Expand Up @@ -77,6 +83,8 @@ t_token tokens[] = {
{ T_SET,
.subtokens = tokens_set,
.help = "Set things" },
{ T_DEVICE,
.help = "Device mode" },
{ T_HELP,
.arg_type = T_ARG_HELP,
.help = "Available commands" },
Expand Down
33 changes: 33 additions & 0 deletions demo/commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2014 Bert Vermeulen <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http:https://www.gnu.org/licenses/>.
*/

enum {
T_SHOW = 1,
T_SET,
T_HARDWARE,
T_VERSION,
T_DEVICE,
T_DIRECTORY,
T_HELP,
T_CPU,
T_MEMORY,
T_FREQUENCY,
T_MKDIR,
T_LS,
T_EXIT,
};

33 changes: 30 additions & 3 deletions demo/demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,23 @@
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include "commands.h"

#include "tokenline.h"

extern t_token tokens[];
extern t_token_dict dict[];
extern t_token tokens_mode_device[];

enum {
MODE_TOP,
MODE_DEVICE,
};

struct demo_context {
t_tokenline *tl;
int mode;
} ctx = { };

void print(void *user, const char *str)
{
Expand All @@ -34,11 +46,11 @@ void print(void *user, const char *str)

static void dump_parsed(void *user, t_tokenline_parsed *p)
{
struct demo_context *ctx;
float arg_float;
int arg_int, i;

(void)user;

ctx = user;
for (i = 0; p->tokens[i]; i++) {
printf("%d: ", i);
switch (p->tokens[i]) {
Expand All @@ -62,6 +74,20 @@ static void dump_parsed(void *user, t_tokenline_parsed *p)
dict[p->tokens[i]].tokenstr);
}
}

switch (p->tokens[0]) {
case T_DEVICE:
ctx->mode = MODE_DEVICE;
tl_set_prompt(ctx->tl, "device> ");
tl_mode_push(ctx->tl, tokens_mode_device);
break;
case T_EXIT:
ctx->mode = MODE_TOP;
tl_set_prompt(ctx->tl, "> ");
tl_mode_pop(ctx->tl);
break;
}

}

int main(int argc, char **argv)
Expand All @@ -79,7 +105,8 @@ int main(int argc, char **argv)
new_termios.c_cc[VMIN] = 1;
new_termios.c_cc[VTIME] = 0;
tcsetattr(0, TCSAFLUSH, &new_termios);
tl_init(&tl, tokens, dict, print, NULL);
ctx.tl = &tl;
tl_init(&tl, tokens, dict, print, &ctx);
tl_set_prompt(&tl, "> ");
tl_set_callback(&tl, dump_parsed);
while (TRUE) {
Expand Down

0 comments on commit f883caf

Please sign in to comment.