File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / src / aitcli.c
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Fri Apr 16 13:20:29 2010 UTC (14 years, 1 month ago) by misho
Branches: MAIN
CVS tags: HEAD
Initial revision

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: aitcli.c,v 1.1 2010/04/16 13:20:29 misho Exp $
*
*************************************************************************/
#include "global.h"


#pragma GCC visibility push(hidden)

cliCommands_t cli_stdCmds[] = {
	{ "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, 
	{ "-------", NULL, "---------------------", NULL, NULL }, 
	{ "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, 
	{ "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, 
	{ NULL, NULL, NULL, NULL }
};

// ------------------------------------------------

int cli_Errno;
char cli_Error[STRSIZ];

#pragma GCC visibility pop


// cli_GetErrno() Get error code of last operation
inline int cli_GetErrno()
{
	return cli_Errno;
}

// io_GetError() Get error text of last operation
inline const char *cli_GetError()
{
	return cli_Error;
}

// cli_SetErr() Set error to variables for internal use!!!
inline void cli_SetErr(int eno, char *estr, ...)
{
	va_list lst;

	cli_Errno = eno;
	memset(cli_Error, 0, STRSIZ);
	va_start(lst, estr);
	vsnprintf(cli_Error, STRSIZ, estr, lst);
	va_end(lst);
}

// ------------------------------------------------------------

/*
 * cli_Printf() Printf CLI features
 * @out = Output stream
 * @csFormat = Printf format string
 * return: none
*/
inline int cli_Printf(FILE *out, const char *csFormat, ...)
{
	va_list lst;
	int ret;

	va_start(lst, csFormat);

	ret = vfprintf(out, csFormat, lst);
	if (-1 == ret)
		LOGERR;

	va_end(lst);
	return ret;
}


/*
 * 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
	rl_attempted_completion_function = cmdComplete;
	rl_completion_entry_function = cmdEntry;
}

/*
 * cliExec() Execute CLI main loop
 * @cmdList = Commands list
 * @out = Output handle
 * @csPrompt = Prompt text
 * return: -1 error, 0 = exit w/^+D, 1 done.
*/
int cliExec(cliCommands_t *cmdList, FILE *out, const char *csPrompt)
{
	char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
	int ret = 0;
	register int i;
	cliCommands_t *cmd = NULL;

	inline int inline_help()
	{
		cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL);
		rl_on_new_line();
		return 0;
	}

	char **cli_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 *cli_stdCompEntry(const char *ignore, int invoking_key)
	{
		return NULL;
	}

	/* --- main body of CLI --- */

	rl_bind_key('?', inline_help);
	if (!rl_attempted_completion_function) 
		cliComp(cli_stdCompletion, cli_stdCompEntry);

	do {
		line = readline(csPrompt);
		if (!line) {	// ^+d
			cli_Printf(out, "\n");
			break;
		}
		// clear whitespaces
		for (s = line; isspace(*s); s++);
		if (*s) {
			for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
			*++t = 0;
		}

		if (*s) {
			add_history(s);

			memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
			for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); 
					*app ? app++ : app);

			/*
			for (i = 0; i < MAX_PROMPT_ITEMS; i++)
				cli_Printf(out, "i=%d %s\n", i, items[i]);
				*/

			// exec_cmd ...
			for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
				if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
					cmd = &cmdList[i];
					break;
				}
			if (!cmd) {
				cli_Printf(out, "Command '%s' not found!\n", items[0]);
				ret = -1;
			} else
				ret = cmd->cmd_func(cmdList, i, out, items);
		}

		free(line);
	} while (ret < 1);

	return ret;
}

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