File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / inc / aitcli.h
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Fri Jun 4 11:32:47 2010 UTC (14 years, 1 month ago) by misho
Branches: MAIN
CVS tags: cli2_0, HEAD, CLI1_0
prepare to tag

/*************************************************************************
* (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: aitcli.h,v 1.2 2010/06/04 11:32:47 misho Exp $
*
*************************************************************************/
#ifndef __AITCLI_H
#define __AITCLI_H


struct tagCLICmd {
	const char *cmd_name;
	int (*cmd_func)(void *, int, FILE *, char **);
	const char *cmd_doc;
	const char *cmd_help;
	char *(*cmd_comp)(const char *, int);
};

typedef struct tagCLICmd cliCommands_t;
typedef char *cli_CompEntry_t(const char *, int);
typedef char **cli_Completion_t(const char *, int, int);


// cli_GetErrno() Get error code of last operation
inline int cli_GetErrno();
// cli_GetError() Get error text of last operation
inline const char *cli_GetError();

/*
 * cli_Printf() Printf CLI features
 * @out = Output stream
 * @csFormat = Printf format string
 * return: -1 error, != -1 printed chars
*/
inline int cli_Printf(FILE *out, const char *csFormat, ...);

/*
 * cliNetInit() Initialize Readline if CLI bind to socket
 * @csProg = program name
 * @pty = Master pty
 * @term = stdin termios
 * return: none
*/
void cliNetInit(const char *csProg, int pty, struct termios *term);
/*
 * cliTTY() Initialize I/O TTY CLI features
 * @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);
/*
 * cliComp() Initialize completion CLI features
 * @cmdComplete = Completion function
 * @cmdEntry = Compentry function
 * return: none
*/
inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry);
/*
 * cliExec() Execute CLI main loop
 * @cmdList = Commands list
 * @csPrompt = Prompt text
 * return: -1 error, 0 = exit w/^+D, 1 done.
*/
int cliExec(cliCommands_t *cmdList, const char *csPrompt);
/*
 * cliNetExec() Execute net CLI main loop
 * @cmdList = Commands list
 * @csPrompt = Prompt text
 * @sock = client socket
 * @term = stdin termios
 * @win = window size of tty
 * return: -1 error, 0 = exit w/^+D, 1 done.
*/
int cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win);

/*
 * cli_ReadHistory() Read CLI History from file
 * @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);
/*
 * cli_WriteHistory() Write CLI History to file
 * @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);


/*
 * cli_PrintHelp() Helper print for missing command arguments
 * @out = Output stream
 * @cmds = Commands list
 * @idx = Selected command ID
 * return: -1 error, !=-1 ok
 * return: none
*/
inline int cli_PrintHelp(FILE *out, void *cmds, int idx);

/*
 * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
 * @cmds = Commands list
 * @idx = Selected command ID
 * @out = Output handle
 * @args = Parsed arguments array
 * return: -1 error, 0 = ok, 1 exit from Cli!
*/
int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
/*
 * cli_Cmd_Help() Builtin helper function for Help screen
 * @cmds = Commands list
 * @idx = Selected command ID
 * @out = Output handle
 * @args = Parsed arguments array
 * return: -1 error, 0 = ok
*/
int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
/*
 * cli_Cmd_Exit() Builtin helper function for Exit from Cli
 * @cmds = Commands list
 * @idx = Selected command ID
 * @out = Output handle
 * @args = Parsed arguments array
 * return: 1 exit from Cli!
*/
int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);


/*
 * cli_Register_Commands - Declare helper function for register and export Commands variable
*/
#define CLI_REGISTER_COMMANDS(CMDS)	\
	extern cliCommands_t CMDS[];
/*
 * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
*/
#define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS)	\
	char *FUNC(const char *text, int state) \
	{ \
		register int i; \
		int len = strlen(text); \
		for (i = state; CMDS[i].cmd_name; i++) { \
			if (strncmp(CMDS[i].cmd_name, "---", 3) && \
					!strncmp(CMDS[i].cmd_name, text, len)) \
				return strdup(CMDS[i].cmd_name); \
		} \
		return NULL; \
	}

/*
 * cli_Make_Comp_Args - Declare helper function for Arguments completion
*/
#define CLI_MAKE_COMP_ARGS(FUNC, ARGS)	\
	char *FUNC(const char *text __attribute__((unused)), int state) \
	{ \
		while (ARGS[state]) \
			return strdup(ARGS[state]); \
		return NULL; \
	}

/*
 * cli_Comp_Filename() Builtin helper function for filename completion arguments
 * @text = Text line
 * @state = Position state
 * return: NULL not found filename, != NULL filename
*/
char *cli_Comp_Filename(const char *text, int state);


#endif

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