version 1.1.2.1, 2010/03/09 12:44:40
|
version 1.2, 2010/03/22 15:21:20
|
Line 9
|
Line 9
|
#include "global.h" |
#include "global.h" |
|
|
|
|
static int cmd_exit(void *cmds, FILE *out, char ** __restrict args) | /* |
| * io_Cmd_Exit() Builtin helper function for Exit from Cli |
| * @cmds = Commands list |
| * @out = Output handle |
| * @args = Parsed arguments array |
| * return: 1 exit from Cli! |
| */ |
| int io_Cmd_Exit(void *cmds, FILE *out, char ** __restrict args) |
{ |
{ |
return 1; |
return 1; |
} |
} |
|
|
static int cmd_help(void *cmds, FILE *out, char ** __restrict args) | /* |
| * io_Cmd_Help() Builtin helper function for Help screen |
| * @cmds = Commands list |
| * @out = Output handle |
| * @args = Parsed arguments array |
| * return: -1 error, 0 = ok |
| */ |
| int io_Cmd_Help(void *cmds, FILE *out, char ** __restrict args) |
{ |
{ |
register int i; |
register int i; |
ioCommands_t *commands = cmds; |
ioCommands_t *commands = cmds; |
Line 24 static int cmd_help(void *cmds, FILE *out, char ** __r
|
Line 38 static int cmd_help(void *cmds, FILE *out, char ** __r
|
|
|
if (!args) { |
if (!args) { |
fprintf(out, "\n"); |
fprintf(out, "\n"); |
for (i = 0; commands[i].cmd_name; i++) | fflush(out); |
| for (i = 0; commands[i].cmd_name; i++) { |
fprintf(out, "%s\t\t%s\n", commands[i].cmd_name, commands[i].cmd_doc); |
fprintf(out, "%s\t\t%s\n", commands[i].cmd_name, commands[i].cmd_doc); |
|
fflush(out); |
|
} |
} else { |
} else { |
if (!args[1]) | if (!args[1]) { |
fprintf(out, "Help screen::\n"); |
fprintf(out, "Help screen::\n"); |
else | fflush(out); |
| } else |
if (!strncmp(args[1], "---", 3)) |
if (!strncmp(args[1], "---", 3)) |
return 0; |
return 0; |
|
|
Line 39 static int cmd_help(void *cmds, FILE *out, char ** __r
|
Line 57 static int cmd_help(void *cmds, FILE *out, char ** __r
|
|
|
fprintf(out, "%s%s\t\t%s\n", args[1] ? "Syntax::\n\t" : "", commands[i].cmd_name, |
fprintf(out, "%s%s\t\t%s\n", args[1] ? "Syntax::\n\t" : "", commands[i].cmd_name, |
args[1] ? commands[i].cmd_help : commands[i].cmd_doc); |
args[1] ? commands[i].cmd_help : commands[i].cmd_doc); |
|
fflush(out); |
} |
} |
} |
} |
|
|
return 0; |
return 0; |
} |
} |
|
|
static int cmd_unsupported(void *cmds, FILE *out, char ** __restrict args) | /* |
| * io_Cmd_Unsupported() Builtin helper function for unsupported commands |
| * @cmds = Commands list |
| * @out = Output handle |
| * @args = Parsed arguments array |
| * return: -1 error, 0 = ok, 1 exit from Cli! |
| */ |
| int io_Cmd_Unsupported(void *cmds, FILE *out, char ** __restrict args) |
{ |
{ |
fprintf(out, "Command %s not supported in this version ...\n", args[0]); |
fprintf(out, "Command %s not supported in this version ...\n", args[0]); |
|
fflush(out); |
return 0; |
return 0; |
} |
} |
|
|
// ------------------------------------------------------------ |
// ------------------------------------------------------------ |
|
|
|
/* |
|
* io_Comp_Filename() Builtin helper function for filename completion arguments |
|
* @text = Text line |
|
* @state = Position state |
|
* return: NULL not found filename, != NULL filename |
|
*/ |
|
char *io_Comp_Filename(const char *text, int state) |
|
{ |
|
return rl_filename_completion_function(text, state); |
|
} |
|
|
|
// ------------------------------------------------------------ |
|
|
#pragma GCC visibility push(hidden) |
#pragma GCC visibility push(hidden) |
|
|
ioCommands_t io_stdCmds[] = { |
ioCommands_t io_stdCmds[] = { |
{ "test", cmd_unsupported, "Test - Don`t use default command structure!", "test <cr>", NULL }, | { "test", io_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", io_Comp_Filename }, |
{ "-------", NULL, "---------------------", NULL, NULL }, |
{ "-------", NULL, "---------------------", NULL, NULL }, |
{ "help", cmd_help, "Help screen", "help [command] <cr>", NULL }, | { "help", io_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, |
{ "exit", cmd_exit, "Exit from console", "exit <cr>", NULL }, | { "exit", io_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, |
{ NULL, NULL, NULL, NULL } |
{ NULL, NULL, NULL, NULL } |
}; |
}; |
|
|
Line 68 ioCommands_t io_stdCmds[] = {
|
Line 108 ioCommands_t io_stdCmds[] = {
|
// ------------------------------------------------------------ |
// ------------------------------------------------------------ |
|
|
/* |
/* |
* ioCLIInit() Initialize CLI features | * ioCLIComp() Initialize completion CLI features |
* @cmdList = Commands list | |
* @out = Output handle | |
* @cmdComplete = Completion function |
* @cmdComplete = Completion function |
* @cmdEntry = Compentry function |
* @cmdEntry = Compentry function |
* return: none |
* return: none |
*/ |
*/ |
void ioCLIInit(ioCommands_t *cmdList, FILE *out, io_Completion_t *cmdComplete, io_CompEntry_t *cmdEntry) | inline void ioCLIComp(io_Completion_t *cmdComplete, io_CompEntry_t *cmdEntry) |
{ |
{ |
ioCommands_t *cmds = cmdList ? cmdList : io_stdCmds; |
|
|
|
inline int inline_help() |
|
{ |
|
cmd_help(cmds, out, NULL); |
|
rl_on_new_line(); |
|
return 0; |
|
} |
|
|
|
rl_bind_key('?', inline_help); |
|
// command completon |
// command completon |
rl_attempted_completion_function = cmdComplete; |
rl_attempted_completion_function = cmdComplete; |
rl_completion_entry_function = cmdEntry; |
rl_completion_entry_function = cmdEntry; |
Line 106 int ioCLIExec(ioCommands_t *cmdList, FILE *out, const
|
Line 134 int ioCLIExec(ioCommands_t *cmdList, FILE *out, const
|
register int i; |
register int i; |
ioCommands_t *cmd = NULL; |
ioCommands_t *cmd = NULL; |
|
|
if (!cmdList) | inline int inline_help() |
return -1; | { |
| io_Cmd_Help(cmdList ? cmdList : io_stdCmds, out, NULL); |
| rl_on_new_line(); |
| return 0; |
| } |
|
|
|
char **io_stdCompletion(const char *text, int start, int end) |
|
{ |
|
register int i; |
|
char **matches = NULL; |
|
|
|
char *cmdCompGet(const char *text, int state) |
|
{ |
|
int len = strlen(text); |
|
|
|
for (i = state; cmdList[i].cmd_name; i++) { |
|
if (strncmp(cmdList[i].cmd_name, "---", 3) && |
|
!strncmp(cmdList[i].cmd_name, text, len)) |
|
return strdup(cmdList[i].cmd_name); |
|
} |
|
|
|
return NULL; |
|
} |
|
|
|
if (!start) |
|
matches = rl_completion_matches(text, cmdCompGet); |
|
else |
|
for (i = 0; cmdList[i].cmd_name; i++) { |
|
if (!cmdList[i].cmd_comp) |
|
continue; |
|
if (!strncmp(rl_line_buffer, cmdList[i].cmd_name, strlen(cmdList[i].cmd_name))) |
|
matches = rl_completion_matches(text, cmdList[i].cmd_comp); |
|
} |
|
|
|
return matches; |
|
} |
|
char *io_stdCompEntry(const char *ignore, int invoking_key) |
|
{ |
|
return NULL; |
|
} |
|
|
|
rl_bind_key('?', inline_help); |
|
if (!rl_attempted_completion_function) |
|
ioCLIComp(io_stdCompletion, io_stdCompEntry); |
|
|
do { |
do { |
line = readline(csPrompt); |
line = readline(csPrompt); |
if (!line) { // ^+d |
if (!line) { // ^+d |
Line 142 int ioCLIExec(ioCommands_t *cmdList, FILE *out, const
|
Line 213 int ioCLIExec(ioCommands_t *cmdList, FILE *out, const
|
} |
} |
if (!cmd) { |
if (!cmd) { |
fprintf(out, "Command '%s' not found!\n", items[0]); |
fprintf(out, "Command '%s' not found!\n", items[0]); |
|
fflush(out); |
ret = -1; |
ret = -1; |
} else |
} else |
ret = cmd->cmd_func(cmdList, out, items); |
ret = cmd->cmd_func(cmdList, out, items); |