File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / src / cli.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Fri Apr 16 13:20:29 2010 UTC (14 years, 3 months ago) by misho
Branches: start
CVS tags: misho, cli2_0, cli1_0_readlineNET, cli1_0, CLI1_0
libaitcli AIT CLI API

    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: cli.c,v 1.1.1.1 2010/04/16 13:20:29 misho Exp $
    7: *
    8: *************************************************************************/
    9: #include "global.h"
   10: 
   11: 
   12: /*
   13:  * cli_Cmd_Exit() Builtin helper function for Exit from Cli
   14:  * @cmds = Commands list
   15:  * @idx = Selected command ID
   16:  * @out = Output handle
   17:  * @args = Parsed arguments array
   18:  * return: 1 exit from Cli!
   19: */
   20: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args)
   21: {
   22: 	return 1;
   23: }
   24: 
   25: /*
   26:  * cli_Cmd_Help() Builtin helper function for Help screen
   27:  * @cmds = Commands list
   28:  * @idx = Selected command ID
   29:  * @out = Output handle
   30:  * @args = Parsed arguments array
   31:  * return: -1 error, 0 = ok
   32: */
   33: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args)
   34: {
   35: 	register int i;
   36: 	cliCommands_t *commands = cmds;
   37: 
   38: 	if (!cmds)
   39: 		return -1;
   40: 
   41: 	if (!args) {
   42: 		cli_Printf(out, "\n");
   43: 		for (i = 0; commands[i].cmd_name; i++)
   44: 			cli_Printf(out, "%s\t\t%s\n", commands[i].cmd_name, commands[i].cmd_doc);
   45: 	} else {
   46: 		if (!args[1])
   47: 			cli_Printf(out, "Help screen::\n");
   48: 		else
   49: 			if (!strncmp(args[1], "---", 3))
   50: 				return 0;
   51: 
   52: 		for (i = 0; commands[i].cmd_name; i++) {
   53: 			if (args[1] && strcmp(args[1], commands[i].cmd_name))
   54: 				continue;
   55: 
   56: 			cli_Printf(out, "%s%s\t\t%s\n", args[1] ? "Syntax::\n\t" : "", commands[i].cmd_name, 
   57: 					args[1] ? commands[i].cmd_help : commands[i].cmd_doc);
   58: 		}
   59: 	}
   60: 
   61: 	return 0;
   62: }
   63: 
   64: /*
   65:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
   66:  * @cmds = Commands list
   67:  * @idx = Selected command ID
   68:  * @out = Output handle
   69:  * @args = Parsed arguments array
   70:  * return: -1 error, 0 = ok, 1 exit from Cli!
   71: */
   72: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args)
   73: {
   74: 	cli_Printf(out, "Command %s not supported in this version ...\n", args[0]);
   75: 	return 0;
   76: }
   77: 
   78: // ------------------------------------------------------------
   79: 
   80: /*
   81:  * cli_Comp_Filename() Builtin helper function for filename completion arguments
   82:  * @text = Text line
   83:  * @state = Position state
   84:  * return: NULL not found filename, != NULL filename
   85: */
   86: char *cli_Comp_Filename(const char *text, int state)
   87: {
   88: 	return rl_filename_completion_function(text, state);
   89: }
   90: 
   91: 
   92: // ------------------------------------------------------------
   93: 
   94: /*
   95:  * cli_PrintHelp() Helper print for missing command arguments
   96:  * @out = Output stream
   97:  * @cmds = Commands list
   98:  * @idx = Selected command ID
   99:  * return: -1 error, !=-1 ok
  100:  * return: none
  101: */
  102: inline int cli_PrintHelp(FILE *out, void *cmds, int idx)
  103: {
  104: 	cliCommands_t *c = cmds;
  105: 
  106: 	return cli_Printf(out, "%s\n", c[idx].cmd_help);
  107: }
  108: 

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