version 1.2.2.1, 2010/06/04 11:46:40
|
version 1.2.2.2, 2010/06/04 12:46:27
|
Line 11
|
Line 11
|
|
|
#pragma GCC visibility push(hidden) |
#pragma GCC visibility push(hidden) |
|
|
cliCommands_t cli_stdCmds[] = { | /* |
| commands_t cli_stdCmds[] = { |
{ "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, |
{ "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, |
{ "-------", NULL, "---------------------", NULL, NULL }, |
{ "-------", NULL, "---------------------", NULL, NULL }, |
{ "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, |
{ "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, |
{ "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, |
{ "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, |
{ NULL, NULL, NULL, NULL } |
{ NULL, NULL, NULL, NULL } |
}; |
}; |
|
*/ |
|
|
// ------------------------------------------------ |
// ------------------------------------------------ |
|
|
int cli_Errno; |
int cli_Errno; |
char cli_Error[STRSIZ]; |
char cli_Error[STRSIZ]; |
|
|
char cli_pending_special_char; |
|
|
|
#pragma GCC visibility pop |
#pragma GCC visibility pop |
|
|
|
|
static void cli_Null_Prep_Term(int meta) |
|
{ |
|
} |
|
|
|
#include <syslog.h> |
|
static int cli_Net_rl_GetCh(FILE *s) |
|
{ |
|
int ch = rl_getc(s); |
|
|
|
if (!cli_pending_special_char && 0x1b == ch) { |
|
cli_pending_special_char = ch; |
|
return ch; |
|
} |
|
if (cli_pending_special_char && 0x5b == ch) { |
|
cli_pending_special_char = ch; |
|
return ch; |
|
} |
|
if (0x5b == cli_pending_special_char) { |
|
cli_pending_special_char = 0; |
|
return ch; |
|
} |
|
|
|
syslog(LOG_CRIT, "+++++ getc=%0x\n", ch); |
|
cli_pending_special_char = 0; |
|
fputc(ch, rl_outstream); |
|
fflush(rl_outstream); |
|
return ch; |
|
} |
|
|
|
#if 0 |
|
static void cli_Line_Handler(char *line) |
|
{ |
|
int len; |
|
static char cli_Buffer[BUFSIZ]; |
|
static int cli_BufferLen, cli_BufferPos; |
|
|
|
if (!line) { // EOF |
|
fwrite("\x4", 1, 1, rl_outstream); // ctrl+D |
|
goto end; |
|
} else |
|
len = strlen(line); |
|
if (BUFSIZ - 2 < len) |
|
cli_BufferPos = cli_BufferLen = 0; |
|
else { |
|
if (BUFSIZ - 2 < len + cli_BufferLen) { |
|
if (BUFSIZ - 2 >= cli_BufferLen - cli_BufferPos + len) { |
|
cli_BufferLen -= cli_BufferPos; |
|
memmove(cli_Buffer, cli_Buffer + cli_BufferPos, cli_BufferLen); |
|
} else |
|
cli_BufferLen = 0; |
|
|
|
cli_BufferPos = 0; |
|
} |
|
|
|
memcpy(cli_Buffer + cli_BufferLen, line, len); |
|
cli_BufferLen += len; |
|
cli_Buffer[cli_BufferLen++] = '\r'; |
|
cli_Buffer[cli_BufferLen++] = '\n'; |
|
} |
|
|
|
fwrite(cli_Buffer, 1, cli_BufferLen, rl_outstream); |
|
if (!cli_pending_special_char) { |
|
fwrite("\r", 1, 1, rl_outstream); |
|
if (*line) |
|
add_history(line); |
|
} |
|
|
|
free(line); |
|
end: |
|
rl_callback_handler_remove(); |
|
if (cli_pending_special_char) { |
|
fwrite(&cli_pending_special_char, 1, 1, rl_outstream); |
|
cli_pending_special_char = 0; |
|
} |
|
fflush(rl_outstream); |
|
} |
|
#endif |
|
|
|
|
|
// cli_GetErrno() Get error code of last operation |
// cli_GetErrno() Get error code of last operation |
inline int cli_GetErrno() | inline int |
| cli_GetErrno() |
{ |
{ |
return cli_Errno; |
return cli_Errno; |
} |
} |
|
|
// io_GetError() Get error text of last operation |
// io_GetError() Get error text of last operation |
inline const char *cli_GetError() | inline const char * |
| cli_GetError() |
{ |
{ |
return cli_Error; |
return cli_Error; |
} |
} |
|
|
// cli_SetErr() Set error to variables for internal use!!! |
// cli_SetErr() Set error to variables for internal use!!! |
inline void cli_SetErr(int eno, char *estr, ...) | inline void |
| cli_SetErr(int eno, char *estr, ...) |
{ |
{ |
va_list lst; |
va_list lst; |
|
|
Line 134 inline void cli_SetErr(int eno, char *estr, ...)
|
Line 57 inline void cli_SetErr(int eno, char *estr, ...)
|
|
|
// ------------------------------------------------------------ |
// ------------------------------------------------------------ |
|
|
/* | static inline void |
* cli_Printf() Printf CLI features | clrscrEOL(linebuffer_t * __restrict buf) |
* @out = Output stream | |
* @csFormat = Printf format string | |
* return: -1 error, != -1 printed chars | |
*/ | |
inline int cli_Printf(FILE *out, const char *csFormat, ...) | |
{ |
{ |
va_list lst; | register int i; |
int ret; | |
|
|
va_start(lst, csFormat); | if (buf) { |
| write(buf->line_out, K_CR, 1); |
|
|
ret = vfprintf(out, csFormat, lst); | for (i = 0; i < buf->line_len; i++) |
if (-1 == ret) | write(buf->line_out, K_SPACE, 1); |
LOGERR; | } |
| |
va_end(lst); | |
return ret; | |
} |
} |
|
|
| static inline void |
/* | printfEOL(linebuffer_t * __restrict buf, int len, int prompt) |
* cliComp() Initialize completion CLI features | |
* @cmdComplete = Completion function | |
* @cmdEntry = Compentry function | |
* return: none | |
*/ | |
inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry) | |
{ |
{ |
// command completon | if (buf) { |
rl_attempted_completion_function = cmdComplete; | write(buf->line_out, K_CR, 1); |
rl_completion_entry_function = cmdEntry; | |
} | |
|
|
/* | if (prompt && buf->line_prompt) |
* cliTTY() Initialize I/O TTY CLI features | write(buf->line_out, buf->line_prompt, buf->line_bol); |
* @term = terminal name | |
* @inp = input handle | |
* @out = output handle | |
* @win = window size | |
* return: -1 error, != -1 ok | |
*/ | |
inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win) | |
{ | |
if (term) | |
rl_terminal_name = term; | |
|
|
if (inp) | write(buf->line_out, buf->line_buf, len == -1 ? buf->line_eol - buf->line_bol: len); |
rl_instream = inp; | } |
if (out) | |
rl_outstream = out; | |
| |
if (win) | |
if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) { | |
LOGERR; | |
return -1; | |
} | |
| |
return 0; | |
} |
} |
|
|
/* | static inline void |
* cli_ReadHistory() Read CLI History from file | printfCR(linebuffer_t * __restrict buf, int prompt) |
* @csFile = history file name, if NULL default history name is ".aitcli.history" | |
* return: -1 error; != -1 readed ok | |
*/ | |
inline int cli_ReadHistory(const char *csFile) | |
{ |
{ |
return read_history(!csFile ? ".aitcli.history" : csFile); | if (buf) { |
| write(buf->line_out, K_CR, 1); |
| |
| if (prompt) |
| if (prompt && buf->line_prompt) |
| write(buf->line_out, buf->line_prompt, buf->line_bol); |
| } |
} |
} |
|
|
/* | static inline void |
* cli_WriteHistory() Write CLI History to file | printfCLI(linebuffer_t * __restrict buf, const unsigned char *text, int textlen, int prompt) |
* @csFile = history file name, if NULL default history name is ".aitcli.history" | |
* @lineNum = save number of history entry lines, if -1 all lines saved without limit | |
* return: -1 error; != -1 readed ok | |
*/ | |
inline int cli_WriteHistory(const char *csFile, int lineNum) | |
{ |
{ |
int ret; | if (buf && text && textlen) { |
const char *psFile = !csFile ? ".aitcli.history" : csFile; | if (prompt && buf->line_prompt) |
| write(buf->line_out, buf->line_prompt, buf->line_bol); |
|
|
ret = write_history(psFile); | write(buf->line_out, text, textlen); |
if (-1 != ret && -1 != lineNum) | } |
history_truncate_file(psFile, lineNum); | |
| |
return ret; | |
} |
} |
|
|
|
// ------------------------------------------------------------ |
|
|
/* |
/* |
* cliNetInit() Initialize Readline if CLI bind to socket |
* cliNetInit() Initialize Readline if CLI bind to socket |
* @csProg = program name |
* @csProg = program name |
Line 231 inline int cli_WriteHistory(const char *csFile, int li
|
Line 115 inline int cli_WriteHistory(const char *csFile, int li
|
* @term = stdin termios |
* @term = stdin termios |
* return: none |
* return: none |
*/ |
*/ |
|
/* |
void cliNetInit(const char *csProg, int pty, struct termios *term) |
void cliNetInit(const char *csProg, int pty, struct termios *term) |
{ |
{ |
struct termios t; |
struct termios t; |
Line 263 void cliNetInit(const char *csProg, int pty, struct te
|
Line 148 void cliNetInit(const char *csProg, int pty, struct te
|
rl_instream = fdopen(pty, "r"); |
rl_instream = fdopen(pty, "r"); |
rl_outstream = NULL; |
rl_outstream = NULL; |
} |
} |
| */ |
/* |
/* |
* cliNetExec() Execute net CLI main loop |
* cliNetExec() Execute net CLI main loop |
* @cmdList = Commands list |
* @cmdList = Commands list |
Line 274 void cliNetInit(const char *csProg, int pty, struct te
|
Line 159 void cliNetInit(const char *csProg, int pty, struct te
|
* return: -1 error, 0 = exit w/^+D, 1 done. |
* return: -1 error, 0 = exit w/^+D, 1 done. |
*/ |
*/ |
int |
int |
cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) | cliNetExec(commands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) |
{ |
{ |
int pty, ret = 0, r, s, alen, attrlen, flg; |
int pty, ret = 0, r, s, alen, attrlen, flg; |
fd_set fds; |
fd_set fds; |
Line 374 cliNetExec(cliCommands_t *cmdList, const char *csPromp
|
Line 259 cliNetExec(cliCommands_t *cmdList, const char *csPromp
|
* @csPrompt = Prompt text |
* @csPrompt = Prompt text |
* return: -1 error, 0 = exit w/^+D, 1 done. |
* return: -1 error, 0 = exit w/^+D, 1 done. |
*/ |
*/ |
|
/* |
int cliExec(cliCommands_t *cmdList, const char *csPrompt) |
int cliExec(cliCommands_t *cmdList, const char *csPrompt) |
{ |
{ |
char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
Line 423 int cliExec(cliCommands_t *cmdList, const char *csProm
|
Line 309 int cliExec(cliCommands_t *cmdList, const char *csProm
|
{ |
{ |
return NULL; |
return NULL; |
} |
} |
| */ |
/* --- main body of CLI --- */ |
/* --- main body of CLI --- */ |
| /* |
out = rl_outstream; |
out = rl_outstream; |
if (!out) |
if (!out) |
out = stdout; |
out = stdout; |
Line 454 int cliExec(cliCommands_t *cmdList, const char *csProm
|
Line 340 int cliExec(cliCommands_t *cmdList, const char *csProm
|
for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); |
for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); |
*app ? app++ : app); |
*app ? app++ : app); |
|
|
/* |
|
for (i = 0; i < MAX_PROMPT_ITEMS; i++) |
|
cli_Printf(out, "i=%d %s\n", i, items[i]); |
|
*/ |
|
|
|
// exec_cmd ... |
// exec_cmd ... |
for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) |
for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) |
if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { |
if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { |
Line 477 int cliExec(cliCommands_t *cmdList, const char *csProm
|
Line 358 int cliExec(cliCommands_t *cmdList, const char *csProm
|
|
|
return ret; |
return ret; |
} |
} |
|
*/ |