File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / inc / aitcli.h
Revision 1.1.1.1.2.8: download - view: text, annotated - select for diffs - revision graph
Wed Apr 28 13:54:45 2010 UTC (14 years, 2 months ago) by misho
Branches: cli1_0
CVS tags: cli1_0_readlineNET
Diff to: branchpoint 1.1.1.1: preferred, unified
removed silly func

    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.h,v 1.1.1.1.2.8 2010/04/28 13:54:45 misho Exp $
    7: *
    8: *************************************************************************/
    9: #ifndef __AITCLI_H
   10: #define __AITCLI_H
   11: 
   12: 
   13: struct tagCLICmd {
   14: 	const char *cmd_name;
   15: 	int (*cmd_func)(void *, int, FILE *, char **);
   16: 	const char *cmd_doc;
   17: 	const char *cmd_help;
   18: 	char *(*cmd_comp)(const char *, int);
   19: };
   20: 
   21: typedef struct tagCLICmd cliCommands_t;
   22: typedef char *cli_CompEntry_t(const char *, int);
   23: typedef char **cli_Completion_t(const char *, int, int);
   24: 
   25: 
   26: // cli_GetErrno() Get error code of last operation
   27: inline int cli_GetErrno();
   28: // cli_GetError() Get error text of last operation
   29: inline const char *cli_GetError();
   30: 
   31: /*
   32:  * cli_Printf() Printf CLI features
   33:  * @out = Output stream
   34:  * @csFormat = Printf format string
   35:  * return: -1 error, != -1 printed chars
   36: */
   37: inline int cli_Printf(FILE *out, const char *csFormat, ...);
   38: 
   39: /*
   40:  * cliNetInit() Initialize Readline if CLI bind to socket
   41:  * @csProg = program name
   42:  * @pty = Master pty
   43:  * @term = stdin termios
   44:  * return: none
   45: */
   46: void cliNetInit(const char *csProg, int pty, struct termios *term);
   47: /*
   48:  * cliTTY() Initialize I/O TTY CLI features
   49:  * @term = terminal name
   50:  * @inp = input handle
   51:  * @out = output handle
   52:  * @win = window size
   53:  * return: -1 error, != -1 ok
   54: */
   55: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win);
   56: /*
   57:  * cliComp() Initialize completion CLI features
   58:  * @cmdComplete = Completion function
   59:  * @cmdEntry = Compentry function
   60:  * return: none
   61: */
   62: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry);
   63: /*
   64:  * cliExec() Execute CLI main loop
   65:  * @cmdList = Commands list
   66:  * @csPrompt = Prompt text
   67:  * return: -1 error, 0 = exit w/^+D, 1 done.
   68: */
   69: int cliExec(cliCommands_t *cmdList, const char *csPrompt);
   70: /*
   71:  * cliNetExec() Execute net CLI main loop
   72:  * @cmdList = Commands list
   73:  * @csPrompt = Prompt text
   74:  * @sock = client socket
   75:  * @term = stdin termios
   76:  * @win = window size of tty
   77:  * return: -1 error, 0 = exit w/^+D, 1 done.
   78: */
   79: int cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win);
   80: 
   81: /*
   82:  * cli_ReadHistory() Read CLI History from file
   83:  * @csFile = history file name, if NULL default history name is ".aitcli.history"
   84:  * return: -1 error; != -1 readed ok
   85: */
   86: inline int cli_ReadHistory(const char *csFile);
   87: /*
   88:  * cli_WriteHistory() Write CLI History to file
   89:  * @csFile = history file name, if NULL default history name is ".aitcli.history"
   90:  * @lineNum = save number of history entry lines, if -1 all lines saved without limit
   91:  * return: -1 error; != -1 readed ok
   92: */
   93: inline int cli_WriteHistory(const char *csFile, int lineNum);
   94: 
   95: 
   96: /*
   97:  * cli_PrintHelp() Helper print for missing command arguments
   98:  * @out = Output stream
   99:  * @cmds = Commands list
  100:  * @idx = Selected command ID
  101:  * return: -1 error, !=-1 ok
  102:  * return: none
  103: */
  104: inline int cli_PrintHelp(FILE *out, void *cmds, int idx);
  105: 
  106: /*
  107:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
  108:  * @cmds = Commands list
  109:  * @idx = Selected command ID
  110:  * @out = Output handle
  111:  * @args = Parsed arguments array
  112:  * return: -1 error, 0 = ok, 1 exit from Cli!
  113: */
  114: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
  115: /*
  116:  * cli_Cmd_Help() Builtin helper function for Help screen
  117:  * @cmds = Commands list
  118:  * @idx = Selected command ID
  119:  * @out = Output handle
  120:  * @args = Parsed arguments array
  121:  * return: -1 error, 0 = ok
  122: */
  123: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
  124: /*
  125:  * cli_Cmd_Exit() Builtin helper function for Exit from Cli
  126:  * @cmds = Commands list
  127:  * @idx = Selected command ID
  128:  * @out = Output handle
  129:  * @args = Parsed arguments array
  130:  * return: 1 exit from Cli!
  131: */
  132: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
  133: 
  134: 
  135: /*
  136:  * cli_Register_Commands - Declare helper function for register and export Commands variable
  137: */
  138: #define CLI_REGISTER_COMMANDS(CMDS)	\
  139: 	extern cliCommands_t CMDS[];
  140: /*
  141:  * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
  142: */
  143: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS)	\
  144: 	char *FUNC(const char *text, int state) \
  145: 	{ \
  146: 		register int i; \
  147: 		int len = strlen(text); \
  148: 		for (i = state; CMDS[i].cmd_name; i++) { \
  149: 			if (strncmp(CMDS[i].cmd_name, "---", 3) && \
  150: 					!strncmp(CMDS[i].cmd_name, text, len)) \
  151: 				return strdup(CMDS[i].cmd_name); \
  152: 		} \
  153: 		return NULL; \
  154: 	}
  155: 
  156: /*
  157:  * cli_Make_Comp_Args - Declare helper function for Arguments completion
  158: */
  159: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS)	\
  160: 	char *FUNC(const char *text __attribute__((unused)), int state) \
  161: 	{ \
  162: 		while (ARGS[state]) \
  163: 			return strdup(ARGS[state]); \
  164: 		return NULL; \
  165: 	}
  166: 
  167: /*
  168:  * cli_Comp_Filename() Builtin helper function for filename completion arguments
  169:  * @text = Text line
  170:  * @state = Position state
  171:  * return: NULL not found filename, != NULL filename
  172: */
  173: char *cli_Comp_Filename(const char *text, int state);
  174: 
  175: 
  176: #endif

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