version 1.9, 2013/10/08 12:04:42
|
version 1.16, 2019/02/04 21:22:31
|
Line 12 terms:
|
Line 12 terms:
|
All of the documentation and software included in the ELWIX and AITNET |
All of the documentation and software included in the ELWIX and AITNET |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
|
|
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 | Copyright 2004 - 2019 |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
by Michael Pounov <misho@elwix.org>. All rights reserved. |
|
|
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
Line 106 printfEOL(linebuffer_t * __restrict buf, int len, int
|
Line 106 printfEOL(linebuffer_t * __restrict buf, int len, int
|
} |
} |
|
|
write(buf->line_out, buf->line_buf, len == -1 ? |
write(buf->line_out, buf->line_buf, len == -1 ? |
buf->line_eol - buf->line_bol: len); | buf->line_eol - buf->line_bol : len); |
} |
} |
} |
} |
|
|
Line 154 bufCHAR(int idx, void * __restrict cli_buffer)
|
Line 154 bufCHAR(int idx, void * __restrict cli_buffer)
|
memcpy(buf->line_buf + pos, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); |
memcpy(buf->line_buf + pos, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); |
buf->line_buf[buf->line_len - 1] = 0; |
buf->line_buf[buf->line_len - 1] = 0; |
|
|
write(buf->line_out, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); | if (buf->line_prompt) |
| write(buf->line_out, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len); |
|
|
if (buf->line_mode == LINEMODE_INS) { |
if (buf->line_mode == LINEMODE_INS) { |
write(buf->line_out, (const u_char*) buf->line_buf + pos + buf->line_keys[idx].key_len, |
write(buf->line_out, (const u_char*) buf->line_buf + pos + buf->line_keys[idx].key_len, |
Line 977 cliInit(int fin, int fout, const char *prompt)
|
Line 978 cliInit(int fin, int fout, const char *prompt)
|
linebuffer_t *cli_buffer; |
linebuffer_t *cli_buffer; |
bindkey_t *keys; |
bindkey_t *keys; |
register int i; |
register int i; |
|
char szPrompt[STRSIZ] = {[0 ... STRSIZ - 1] = 0}; |
|
|
/* init buffer */ |
/* init buffer */ |
cli_buffer = e_malloc(sizeof(linebuffer_t)); |
cli_buffer = e_malloc(sizeof(linebuffer_t)); |
Line 993 cliInit(int fin, int fout, const char *prompt)
|
Line 995 cliInit(int fin, int fout, const char *prompt)
|
SLIST_INIT(&cli_buffer->line_cmds); |
SLIST_INIT(&cli_buffer->line_cmds); |
|
|
if (prompt) { |
if (prompt) { |
cli_buffer->line_prompt = e_strdup(prompt); | strlcpy(cli_buffer->line_porigin, prompt, sizeof cli_buffer->line_porigin); |
| snprintf(szPrompt, sizeof szPrompt, "%s{%d}> ", cli_buffer->line_porigin, cli_buffer->line_level); |
| cli_buffer->line_prompt = e_strdup(szPrompt); |
if (!cli_buffer->line_prompt) { |
if (!cli_buffer->line_prompt) { |
LOGERR; |
LOGERR; |
e_free(cli_buffer); |
e_free(cli_buffer); |
Line 1050 cliInit(int fin, int fout, const char *prompt)
|
Line 1054 cliInit(int fin, int fout, const char *prompt)
|
keys[i].key_func = bufEOL; |
keys[i].key_func = bufEOL; |
if (cli_buffer->line_prompt && (i == *K_CTRL_H || i == *K_BACKSPACE)) |
if (cli_buffer->line_prompt && (i == *K_CTRL_H || i == *K_BACKSPACE)) |
keys[i].key_func = bufBS; |
keys[i].key_func = bufBS; |
if (cli_buffer->line_prompt && i == *K_CTRL_C) | if (i == *K_CTRL_C) |
keys[i].key_func = bufCLR; |
keys[i].key_func = bufCLR; |
if (cli_buffer->line_prompt && i == *K_CTRL_A) |
if (cli_buffer->line_prompt && i == *K_CTRL_A) |
keys[i].key_func = bufBEGIN; |
keys[i].key_func = bufBEGIN; |
Line 1058 cliInit(int fin, int fout, const char *prompt)
|
Line 1062 cliInit(int fin, int fout, const char *prompt)
|
keys[i].key_func = bufEND; |
keys[i].key_func = bufEND; |
if (cli_buffer->line_prompt && i == *K_TAB) |
if (cli_buffer->line_prompt && i == *K_TAB) |
keys[i].key_func = bufComp; |
keys[i].key_func = bufComp; |
if (cli_buffer->line_prompt && i == *K_CTRL_Z) | if (i == *K_CTRL_Z) |
keys[i].key_func = bufEndNode; |
keys[i].key_func = bufEndNode; |
if (i >= *K_SPACE && i < *K_BACKSPACE) |
if (i >= *K_SPACE && i < *K_BACKSPACE) |
keys[i].key_func = bufCHAR; |
keys[i].key_func = bufCHAR; |
Line 1266 cliInitLine(linebuffer_t * __restrict cli_buffer)
|
Line 1270 cliInitLine(linebuffer_t * __restrict cli_buffer)
|
* cliReadLine() - Read line from opened CLI session |
* cliReadLine() - Read line from opened CLI session |
* |
* |
* @cli_buffer = CLI buffer |
* @cli_buffer = CLI buffer |
|
* @timeout = Session timeout (-1 infinit) |
* return: NULL if error or !=NULL readed line, must be e_free after use! |
* return: NULL if error or !=NULL readed line, must be e_free after use! |
*/ |
*/ |
char * |
char * |
cliReadLine(linebuffer_t * __restrict cli_buffer) | cliReadLine(linebuffer_t * __restrict cli_buffer, int timeout) |
{ |
{ |
int code, readLen; | int code, readLen, ret; |
register int i; |
register int i; |
struct pollfd fds; |
struct pollfd fds; |
char buf[BUFSIZ], *str = NULL; |
char buf[BUFSIZ], *str = NULL; |
Line 1279 cliReadLine(linebuffer_t * __restrict cli_buffer)
|
Line 1284 cliReadLine(linebuffer_t * __restrict cli_buffer)
|
if (!cli_buffer) { |
if (!cli_buffer) { |
cli_SetErr(EINVAL, "Invalid input parameters ..."); |
cli_SetErr(EINVAL, "Invalid input parameters ..."); |
return NULL; |
return NULL; |
} | } else if (timeout > 0) |
| timeout *= 1000; /* convert from sec to ms */ |
|
|
memset(&fds, 0, sizeof fds); |
memset(&fds, 0, sizeof fds); |
fds.fd = cli_buffer->line_in; |
fds.fd = cli_buffer->line_in; |
Line 1287 cliReadLine(linebuffer_t * __restrict cli_buffer)
|
Line 1293 cliReadLine(linebuffer_t * __restrict cli_buffer)
|
|
|
printfCR(cli_buffer, 1); |
printfCR(cli_buffer, 1); |
while (42) { |
while (42) { |
if (poll(&fds, 1, -1) < 1) { | if ((ret = poll(&fds, 1, timeout)) < 1) { |
LOGERR; | if (!ret) { |
| cli_buffer->line_kill = 1; |
| if (str) { |
| e_free(str); |
| str = NULL; |
| } |
| } else |
| LOGERR; |
return str; |
return str; |
} |
} |
|
|
memset(buf, 0, sizeof buf); |
memset(buf, 0, sizeof buf); |
readLen = read(cli_buffer->line_in, buf, BUFSIZ); |
readLen = read(cli_buffer->line_in, buf, BUFSIZ); |
if (readLen == -1) { | if (readLen < 1) { |
LOGERR; | if (readLen) |
return str; | LOGERR; |
| return NULL; |
} |
} |
if (!readLen) { |
|
if (cli_buffer->line_buf) |
|
str = e_strdup(cli_buffer->line_buf); |
|
else |
|
cli_SetErr(EPIPE, "Unknown state ..."); |
|
return str; |
|
} |
|
|
|
recheck: |
recheck: |
for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--) |
for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--) |
Line 1343 recheck:
|
Line 1350 recheck:
|
* @cli_buffer = CLI buffer |
* @cli_buffer = CLI buffer |
* @csHistFile = History file name |
* @csHistFile = History file name |
* @sock = client socket |
* @sock = client socket |
|
* @timeout = Session timeout (-1 infinit) |
* return: RETCODE_ERR error, RETCODE_OK ok |
* return: RETCODE_ERR error, RETCODE_OK ok |
*/ |
*/ |
int |
int |
cliNetLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int sock) | cliNetLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, |
| int sock, int timeout) |
{ |
{ |
u_char buf[BUFSIZ]; |
u_char buf[BUFSIZ]; |
int pid, stat, pty, r, s, alen, flg, attrlen = 0, ret = 0; | int pid, stat, pty, s, alen, flg, attrlen = 0, ret = 0; |
fd_set fds; |
fd_set fds; |
struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; |
struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; |
struct telnetAttrs *a, Attr[10]; |
struct telnetAttrs *a, Attr[10]; |
Line 1365 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
Line 1374 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
} else |
} else |
close(sock); |
close(sock); |
|
|
ret = cliLoop(cli_buffer, csHistFile) < 0 ? 1 : 0; | ret = cliLoop(cli_buffer, csHistFile, timeout) < 0 ? 1 : 0; |
cliEnd(cli_buffer); |
cliEnd(cli_buffer); |
|
|
_exit(ret); |
_exit(ret); |
Line 1394 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
Line 1403 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
break; |
break; |
} |
} |
|
|
r = FD_ISSET(sock, &fds) ? sock : pty; |
|
s = FD_ISSET(sock, &fds) ? pty : sock; |
s = FD_ISSET(sock, &fds) ? pty : sock; |
|
|
if (FD_ISSET(sock, &fds)) { |
if (FD_ISSET(sock, &fds)) { |
Line 1434 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
Line 1442 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
|
|
if (FD_ISSET(pty, &fds)) { |
if (FD_ISSET(pty, &fds)) { |
memset(buf, 0, BUFSIZ); |
memset(buf, 0, BUFSIZ); |
if ((ret = read(pty, buf, BUFSIZ)) == -1) { | if ((ret = read(pty, buf, BUFSIZ)) < 1) { |
LOGERR; | if (ret) |
| LOGERR; |
break; |
break; |
} |
} |
|
|
Line 1458 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
Line 1467 cliNetLoop(linebuffer_t * __restrict cli_buffer, const
|
* |
* |
* @cli_buffer = CLI buffer |
* @cli_buffer = CLI buffer |
* @csHistFile = History file name |
* @csHistFile = History file name |
|
* @timeout = Session timeout (-1 infinit) |
* return: RETCODE_ERR error, RETCODE_OK ok |
* return: RETCODE_ERR error, RETCODE_OK ok |
*/ |
*/ |
int |
int |
cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile) | cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int timeout) |
{ |
{ |
char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; |
register int i; |
register int i; |
Line 1475 cliLoop(linebuffer_t * __restrict cli_buffer, const ch
|
Line 1485 cliLoop(linebuffer_t * __restrict cli_buffer, const ch
|
return RETCODE_ERR; |
return RETCODE_ERR; |
|
|
do { |
do { |
line = cliReadLine(cli_buffer); | line = cliReadLine(cli_buffer, timeout); |
if (!line) { |
if (!line) { |
printfNL(cli_buffer, 0); |
printfNL(cli_buffer, 0); |
break; |
break; |
Line 1505 cliLoop(linebuffer_t * __restrict cli_buffer, const ch
|
Line 1515 cliLoop(linebuffer_t * __restrict cli_buffer, const ch
|
} |
} |
|
|
if (!cmd) { |
if (!cmd) { |
cli_Printf(cli_buffer, "\nCommand '%s' not found!\n", items[0]); | cli_Printf(cli_buffer, "%sCommand '%s' not found!\n", |
| cli_buffer->line_prompt ? "\n" : "", items[0]); |
ret = -1; |
ret = -1; |
} else |
} else |
if (cmd->cmd_func) { |
if (cmd->cmd_func) { |
cli_Printf(cli_buffer, "\n"); | if (cli_buffer->line_prompt) |
| cli_Printf(cli_buffer, "\n"); |
ret = cmd->cmd_func(cli_buffer, |
ret = cmd->cmd_func(cli_buffer, |
cli_buffer->line_level, items); |
cli_buffer->line_level, items); |
} else { |
} else { |
Line 1521 cliLoop(linebuffer_t * __restrict cli_buffer, const ch
|
Line 1533 cliLoop(linebuffer_t * __restrict cli_buffer, const ch
|
cli_freeLine(cli_buffer); |
cli_freeLine(cli_buffer); |
cli_resetHistory(cli_buffer); |
cli_resetHistory(cli_buffer); |
e_free(line); |
e_free(line); |
} while (ret < 1); | } while (cli_buffer->line_kill || ret < 1); |
|
|
cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES); |
cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES); |
return ret; |
return ret; |