File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / src / aitcli.c
Revision 1.1.1.1.2.6: download - view: text, annotated - select for diffs - revision graph
Tue Apr 20 12:16:52 2010 UTC (14 years, 3 months ago) by misho
Branches: cli1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
add feature

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitcli.c,v 1.1.1.1.2.6 2010/04/20 12:16:52 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: 
   11: 
   12: #pragma GCC visibility push(hidden)
   13: 
   14: cliCommands_t cli_stdCmds[] = {
   15: 	{ "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, 
   16: 	{ "-------", NULL, "---------------------", NULL, NULL }, 
   17: 	{ "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, 
   18: 	{ "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, 
   19: 	{ NULL, NULL, NULL, NULL }
   20: };
   21: 
   22: // ------------------------------------------------
   23: 
   24: int cli_Errno;
   25: char cli_Error[STRSIZ];
   26: 
   27: #pragma GCC visibility pop
   28: 
   29: 
   30: static void cli_Null_Prep_Term(int meta)
   31: {
   32: }
   33: 
   34: static void cli_Null_Deprep_Term()
   35: {
   36: }
   37: 
   38: static int cli_Pre_Input_Change_Mode()
   39: {
   40: 	return 0;
   41: }
   42: 
   43: static int cli_GetC(FILE *dummy)
   44: {
   45: 	int ch = rl_getc(stdin);
   46: 
   47: 	/*
   48: 	if (is_special_char(ch)) {
   49: 		pending_special_char = ch;
   50: 		return '\r';
   51: 	}
   52: 	*/
   53: 
   54: 	return ch;
   55: }
   56: 
   57: 
   58: // cli_GetErrno() Get error code of last operation
   59: inline int cli_GetErrno()
   60: {
   61: 	return cli_Errno;
   62: }
   63: 
   64: // io_GetError() Get error text of last operation
   65: inline const char *cli_GetError()
   66: {
   67: 	return cli_Error;
   68: }
   69: 
   70: // cli_SetErr() Set error to variables for internal use!!!
   71: inline void cli_SetErr(int eno, char *estr, ...)
   72: {
   73: 	va_list lst;
   74: 
   75: 	cli_Errno = eno;
   76: 	memset(cli_Error, 0, STRSIZ);
   77: 	va_start(lst, estr);
   78: 	vsnprintf(cli_Error, STRSIZ, estr, lst);
   79: 	va_end(lst);
   80: }
   81: 
   82: // ------------------------------------------------------------
   83: 
   84: /*
   85:  * cli_Printf() Printf CLI features
   86:  * @out = Output stream
   87:  * @csFormat = Printf format string
   88:  * return: -1 error, != -1 printed chars
   89: */
   90: inline int cli_Printf(FILE *out, const char *csFormat, ...)
   91: {
   92: 	va_list lst;
   93: 	int ret;
   94: 
   95: 	va_start(lst, csFormat);
   96: 
   97: 	ret = vfprintf(out, csFormat, lst);
   98: 	if (-1 == ret)
   99: 		LOGERR;
  100: 
  101: 	va_end(lst);
  102: 	return ret;
  103: }
  104: 
  105: 
  106: /*
  107:  * cliComp() Initialize completion CLI features
  108:  * @cmdComplete = Completion function
  109:  * @cmdEntry = Compentry function
  110:  * return: none
  111: */
  112: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry)
  113: {
  114: 	// command completon
  115: 	rl_attempted_completion_function = cmdComplete;
  116: 	rl_completion_entry_function = cmdEntry;
  117: }
  118: 
  119: /*
  120:  * cliTTY() Initialize I/O TTY CLI features
  121:  * @term = terminal name
  122:  * @inp = input handle
  123:  * @out = output handle
  124:  * @win = window size
  125:  * return: -1 error, != -1 ok
  126: */
  127: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win)
  128: {
  129: 	if (term)
  130: 		rl_terminal_name = term;
  131: 
  132: 	if (inp)
  133: 		rl_instream = inp;
  134: 	if (out)
  135: 		rl_outstream = out;
  136: 
  137: 	if (win)
  138: 	       if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) {
  139: 		       LOGERR;
  140: 		       return -1;
  141: 	       }
  142: 
  143: 	return 0;
  144: }
  145: 
  146: /*
  147:  * cli_ReadHistory() Read CLI History from file
  148:  * @csFile = history file name, if NULL default history name is ".aitcli.history"
  149:  * return: -1 error; != -1 readed ok
  150: */
  151: inline int cli_ReadHistory(const char *csFile)
  152: {
  153: 	return read_history(!csFile ? ".aitcli.history" : csFile);
  154: }
  155: 
  156: /*
  157:  * cli_WriteHistory() Write CLI History to file
  158:  * @csFile = history file name, if NULL default history name is ".aitcli.history"
  159:  * @lineNum = save number of history entry lines, if -1 all lines saved without limit
  160:  * return: -1 error; != -1 readed ok
  161: */
  162: inline int cli_WriteHistory(const char *csFile, int lineNum)
  163: {
  164: 	int ret;
  165: 	const char *psFile = !csFile ? ".aitcli.history" : csFile;
  166: 
  167: 	ret = write_history(psFile);
  168: 	if (-1 != ret && -1 != lineNum)
  169: 		history_truncate_file(psFile, lineNum);
  170: 
  171: 	return ret;
  172: }
  173: 
  174: /*
  175:  * cliInit() Initialize Readline
  176:  * @csProg = program name
  177:  * return: none
  178: */
  179: inline void cliInit(const char *csProg)
  180: {
  181: 	rl_readline_name = csProg;
  182: 
  183: 	rl_variable_bind("editing-mode", "emacs");
  184: }
  185: 
  186: /*
  187:  * cliNetInit() Initialize Readline if CLI bind to socket
  188:  * @csProg = program name
  189:  * @pty = Master pty
  190:  * @term = stdin termios
  191:  * return: none
  192: */
  193: void cliNetInit(const char *csProg, int pty, struct termios *term)
  194: {
  195: 	struct termios t;
  196: 
  197: 	if (term) {
  198: 		t = *term;
  199: 		t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
  200: 		t.c_iflag &= ~ICRNL;
  201: 		t.c_iflag |= IGNBRK;
  202: 		t.c_cc[VMIN] = 1;
  203: 		t.c_cc[VTIME] = 0;
  204: 		tcsetattr(pty, TCSANOW, &t);
  205: 	}
  206: 
  207: 	cliInit(csProg);
  208: 
  209: 	rl_instream = fdopen(pty, "r");
  210: 
  211: 	rl_prep_term_function = cli_Null_Prep_Term;
  212: 	rl_deprep_term_function = cli_Null_Deprep_Term;
  213: 	rl_pre_input_hook = cli_Pre_Input_Change_Mode;
  214: 
  215: 	rl_getc_function = cli_GetC;
  216: 
  217: }
  218: 
  219: /*
  220:  * cliExec() Execute CLI main loop
  221:  * @cmdList = Commands list
  222:  * @csPrompt = Prompt text
  223:  * return: -1 error, 0 = exit w/^+D, 1 done.
  224: */
  225: int cliExec(cliCommands_t *cmdList, const char *csPrompt)
  226: {
  227: 	char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
  228: 	int ret = 0;
  229: 	register int i;
  230: 	cliCommands_t *cmd = NULL;
  231: 	FILE *out;
  232: 
  233: 	inline int inline_help()
  234: 	{
  235: 		cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL);
  236: 		rl_on_new_line();
  237: 		return 0;
  238: 	}
  239: 
  240: 	char **cli_stdCompletion(const char *text, int start, int end)
  241: 	{
  242: 		register int i;
  243: 		char **matches = NULL;
  244: 
  245: 		char *cmdCompGet(const char *text, int state)
  246: 		{
  247: 			int len = strlen(text);
  248: 
  249: 			for (i = state; cmdList[i].cmd_name; i++) {
  250: 				if (strncmp(cmdList[i].cmd_name, "---", 3) && 
  251: 						!strncmp(cmdList[i].cmd_name, text, len))
  252: 					return strdup(cmdList[i].cmd_name);
  253: 			}
  254: 
  255: 			return NULL;
  256: 		}
  257: 
  258: 		if (!start)
  259: 			matches = rl_completion_matches(text, cmdCompGet);
  260: 		else
  261: 			for (i = 0; cmdList[i].cmd_name; i++) {
  262: 				if (!cmdList[i].cmd_comp)
  263: 					continue;
  264: 				if (!strncmp(rl_line_buffer, cmdList[i].cmd_name, strlen(cmdList[i].cmd_name)))
  265: 					matches = rl_completion_matches(text, cmdList[i].cmd_comp);
  266: 			}
  267: 
  268: 		return matches;
  269: 	}
  270: 	char *cli_stdCompEntry(const char *ignore, int invoking_key)
  271: 	{
  272: 		return NULL;
  273: 	}
  274: 
  275: 	/* --- main body of CLI --- */
  276: 
  277: 	out = rl_outstream;
  278: 	if (!out)
  279: 		out = stdout;
  280: 
  281: 	rl_bind_key('?', inline_help);
  282: 	if (!rl_attempted_completion_function) 
  283: 		cliComp(cli_stdCompletion, cli_stdCompEntry);
  284: 
  285: 	do {
  286: 		line = readline(csPrompt);
  287: 		if (!line) {	// ^+d
  288: 			cli_Printf(out, "\n");
  289: 			break;
  290: 		}
  291: 		// clear whitespaces
  292: 		for (s = line; isspace(*s); s++);
  293: 		if (*s) {
  294: 			for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
  295: 			*++t = 0;
  296: 		}
  297: 
  298: 		if (*s) {
  299: 			add_history(s);
  300: 
  301: 			memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
  302: 			for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); 
  303: 					*app ? app++ : app);
  304: 
  305: 			/*
  306: 			for (i = 0; i < MAX_PROMPT_ITEMS; i++)
  307: 				cli_Printf(out, "i=%d %s\n", i, items[i]);
  308: 				*/
  309: 
  310: 			// exec_cmd ...
  311: 			for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
  312: 				if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
  313: 					cmd = &cmdList[i];
  314: 					break;
  315: 				}
  316: 			if (!cmd) {
  317: 				cli_Printf(out, "Command '%s' not found!\n", items[0]);
  318: 				ret = -1;
  319: 			} else
  320: 				ret = cmd->cmd_func(cmdList, i, out, items);
  321: 		}
  322: 
  323: 		free(line);
  324: 	} while (ret < 1);
  325: 
  326: 	return ret;
  327: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>