tw_cli.c.svn-base
资源名称:cli.rar [点击查看]
上传用户:hujq123
上传日期:2016-04-10
资源大小:153k
文件大小:47k
源码类别:
Telnet服务器
开发平台:
Unix_Linux
- /*
- * Copyright (C) 2002 Koninklijke Philips Electronics N.V.,
- * All Rights Reserved.
- *
- * This source code and any compilation or derivative thereof is the
- * proprietary information of Koninklijke Philips Electronics N.V.
- * and is confidential in nature.
- * Under no circumstances is this software to be exposed to or placed
- * under an Open Source License of any type without the expressed
- * written permission of Koninklijke Philips Electronics N.V.
- *
- *###########################################################
- */
- /*!
- * file tw_cli.c
- *
- * brief -
- *
- */
- /*-----------------------------------------------------------*
- *
- * %version: 6 %
- * instance: DS_6
- * %date_created: Fri Feb 21 08:04:49 2003 %
- *
- *###########################################################
- */
- /****************************************************************************
- *
- *
- * This module is writen to support 2 or more sessions. Everything that is
- * specific to a session is stored in a Cli_cmdSession_t object. There are two
- * such objects in sockSession[] array right now.
- *
- * To truely support multisession, the caller of these routines must provide
- * either 2 threads or some kind of nselect() services.
- *
- *
- *
- * CLI Style Guide
- * ---------------
- * All commands and options are all in lower case.
- *
- * In the help string, if there are optional parameters accompanying the
- * command, put the optional parameters within a braces "{}". All lowercase
- * word means that user can type the exact same form. Multiple
- * selections within the same field are seperated by "|". All uppercase
- * word means that user needs to type an appropriate value for the field.
- *
- * Even though the CLI parser support multiple hierachy, try not to have more
- * than 3 levels deep.
- *
- * Philips history
- * who When Why
- * KBL 6/14/99 And God set them in the firmament of the heavens to give
- * light upon the earth,
- *
- *****************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <string.h>
- #include <ctype.h>
- #include <inttypes.h>
- #include <tw_cli.h>
- /* All command structure objects adhere to following naming convention :
- *
- * If the object is at root, meaning that it's a command and not a option,
- * start with "CliRco", otherwise start with "CliCo".
- * Next comes the actual command+option in concatenated form, e.g. "show abc"
- * will be "showAbc".
- *
- * All handler must start with "CliXXXProc_" (handler with no argument) or
- * "CliXXXVoproc" (handler with arguments - variations and options). Help
- * function must start with "CliXXXHelp". XXX is either Rco or Co, depends
- * if the handler is attached to a root command or sub command.
- *
- * All root command objects must be included in the "Cli_AllRootCmdObjList"
- * list in this file. Definition of this object, as well as its child
- * command objects and handlers must be placed in its own file.
- */
- /* Root commands are application specific. */
- extern Cli_CmdStruct_t *Cli_AllRootCmdObjList[];
- /*** STOP ! There is no need to venture below this line if ***
- *** all you need is to add a root command object. ***/
- //#define NUM_ALL_CMD_OBJ (sizeof(Cli_AllRootCmdObjList) / sizeof(Cli_CmdStruct_t *))
- /* Foundation for support for multiple CLI session is here. The following functions;
- * Cli_Sock2sess(), Cli_NewSess(), and Cli_FreeSess() are initially implemented as
- * macros and will only work for 2 sessions.
- */
- #define NUM_OF_CLI_ACCESS_SESSION 3
- static Cli_CmdStruct_t Cli_WorkingRootCmdObj =
- {
- "@root",
- 0,
- Cli_AllRootCmdObjList,
- "--",
- NULL,
- NULL,
- NULL
- };
- static Cli_cmdSession_t sockSession[NUM_OF_CLI_ACCESS_SESSION];
- /* Convert from socket and pointer to a Session, return pSess if successful,
- * otherwise NULL. Writen for 2 sessions only.
- */
- #define Cli_Sock2Sess(so) ( (so) == sockSession[0].pSessFuns ?
- &(sockSession[0]) :
- ((so) == sockSession[1].pSessFuns ? &(sockSession[1]) : NULL) )
- /* Common size text string to be reused.
- */
- char *badPasswordStr = "nInvalid Password.n";
- char *endSessionStr = "nCLI session terminated.n";
- char *noHelpStr = "nNo help available.n";
- char *missingCmdStr = "nMissing command options.n";
- char *invalidCmdStr = "nInvalid command or options.n";
- char *cmdOptionsAreStr = "nAvailable command options:n";
- char *noSuchHistoryStr = "nNo such history.n";
- char *badParamStr = "Bad param.n";
- char *operationFailedStr = "Operation failed.n";
- char *waitForAKeyStr = "<Pause>n";
- char *Cli_FailedStr = "Failed!n";
- static const char init_msg[] = "nPress [ENTER] key to active this console...";
- static const char banner1[] =
- "n"
- "+---------------------------------------+n"
- "| Net camera configuration console v1.2 |n"
- "| GuangZhou Seeland Inc. 2005.5 |n"
- "+---------------------------------------+n";
- /* Telnet session state to track user login progress
- */
- enum
- {
- CLI_ACCESS_CMD_NULL,
- CLI_ACCESS_CMD_INIT,
- CLI_ACCESS_CMD_PROMPT_USER,
- CLI_ACCESS_CMD_PROMPT_PASS,
- CLI_ACCESS_CMD_OPEN
- };
- /* Command mode occupies 3 characters near the start of prompt
- */
- static char *modePrompt[] = { " ", "USR", "OPR", "ENG", "BLO" };
- #define MODE_PROMPT_SIZE 3
- /* Available hardcoded usernames.
- */
- #define CLI_LOGIN_USER "user"
- #define CLI_USER_PASSWORD "eureka"
- #define CLI_LOGIN_OPERATOR "super"
- #define CLI_OPERATOR_PASSWORD "slxj530"
- #define CLI_LOGIN_ENG "eng"
- #define CLI_LOGIN_ENG2 "engineer"
- #define CLI_ENG_PASSWORD "sierra"
- #define CLI_LOGIN_BLO "blo"
- #define CLI_BLO_PASSWORD "whatsup1226"
- #define Is_space(c) ((c) == ' ')
- /* Forward declaration of local static functions
- */
- static Cli_CmdStruct_t *Cli_SeekOptionMatch(Cli_CmdStruct_t *baseCmdObj,
- char *thisCmd,
- int thisCmdLen);
- static void Cli_UpdateCmdHistory(Cli_cmdSession_t *pSess, char *cmdBuf, int len);
- static int Cli_Cmdprocessing(Cli_cmdSession_t *pSess,
- Cli_CmdStruct_t *baseCmdObj,
- char *cmdStr,
- int cmdPtr,
- int recurLevel);
- static int Cli_BuildCmdObj(Cli_CmdStruct_t **rootList,
- Cli_CmdStruct_t **workingList,
- unsigned int flags);
- static int Cli_SetAccessMode(Cli_cmdSession_t *pSess, int mode);
- static int Cli_VerifyPassword(Cli_cmdSession_t *pSess, char *cmdBuf, int len);
- static int Cli_ParseCmd(Cli_cmdSession_t *pSess, char *cmdBuf, int len);
- int Cli_PrepareOpenSession (Cli_cmdSession_t *pSess);
- #ifdef SC_PSOS
- #include <tmlib/AppSem.h>
- static struct _AppSem_Semaphore cli_mutex = _AppSem_INITIALISED_SEM;
- #define LOCKSESSION() AppSem_P( &cli_mutex );
- #define UNLOCKSESSION() AppSem_V( &cli_mutex);
- #else
- #include <pthread.h>
- static pthread_mutex_t cli_mutex = PTHREAD_MUTEX_INITIALIZER;
- #define LOCKSESSION() pthread_mutex_lock( &cli_mutex );
- #define UNLOCKSESSION() pthread_mutex_unlock( &cli_mutex);
- #endif
- /*****************************************************************************
- * int Cli_WriteBuf(Cli_cmdSession_t *pSess, char *str, int len)
- *
- * Description :
- * This function print a text string "str" of length "len". The text string
- * is not necessary to be NULL terminated. It is important to get the length
- * right.
- *
- * This is the basic function that does ALL the CLI text output. Modify this
- * function to support multiple session type.
- *
- * Arguments :
- * pSess - a pointer to a command session
- * str - a character string
- * len - length of the character string.
- *
- * Returns :
- * 0 - OK
- * -1 - Failed
- *
- * History
- * who when why
- * KBL 9/7/99 created
- *
- *****************************************************************************/
- static int Cli_WriteBuf(Cli_cmdSession_t *pSess, char *str, int len)
- {
- char cr_buf[] = "rn";
- int idx, PrintLen;
- char *pStrStart;
- if (pSess->skipPrinting)
- return 0;
- pStrStart = NULL;
- for (idx = 0 ; idx < len; idx++) {
- int cr;
- if (pStrStart == NULL)
- pStrStart = &(str[idx]);
- /* If it's carriage return, perform end-of-line conversion. */
- cr = (str[idx] == 'n');
- if (cr || (idx == (len - 1))) {
- PrintLen = &(str[idx]) - pStrStart + 1;
- PrintLen -= cr;
- pSess->pSessFuns->write(pStrStart, PrintLen);
- if (cr)
- pSess->pSessFuns->write(cr_buf, 2);
- pStrStart = NULL;
- pSess->pSessFuns->yield();
- }
- }
- return 0;
- }
- /*****************************************************************************
- * void Cli_InitDataSet()
- *
- * Description :
- * This function must be called once during system initialization for proper
- * operation of the command line mechanism.
- *
- * Arguments :
- * None
- *
- * Returns :
- * None
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static void Cli_InitDataSet()
- {
- int i;
- for (i = 0; i < NUM_OF_CLI_ACCESS_SESSION; i++)
- sockSession[i].pSessFuns = NULL;
- return;
- }
- static Cli_cmdSession_t *
- Cli_NewSess () {
- int i;
- Cli_cmdSession_t *p = NULL;
- LOCKSESSION()
- for (i = 0; i < NUM_OF_CLI_ACCESS_SESSION; i++) {
- if (!sockSession[i].pSessFuns) {
- sockSession[i].pSessFuns = (Cli_SessionFuns_t*) 1;
- p = &sockSession[i];
- break;
- }
- }
- UNLOCKSESSION()
- return p;
- }
- static int Cli_FreeSess (Cli_cmdSession_t *pSess) {
- if (pSess->pSessFuns->cleanup)
- pSess->pSessFuns->cleanup(pSess);
- return 0;
- }
- /*****************************************************************************
- * int Cli_CloseCmdSession(Cli_cmdSession_t *pSess)
- *
- * Description :
- * This function must be called when a command session is terminated so that
- * the session object associated with the obsolete session is freed.
- *
- * Arguments :
- * the pointer to the command session
- *
- * Returns :
- * 0 if OK, -1 if failed.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static int Cli_CloseCmdSession(Cli_cmdSession_t *pSess)
- {
- LOCKSESSION()
- Cli_FreeSess(pSess);
- /* set pSessFuns to NULL to "free" it */
- pSess->pSessFuns = NULL;
- UNLOCKSESSION()
- return 0;
- }
- /*****************************************************************************
- * Cli_cmdSession_t Cli_InitCmdSession(int nsock)
- *
- * Description :
- * This function must be called before a specific telnet command session can
- * be used. It is typical called after a successful accept operation.
- *
- * Arguments :
- * nsock - socket number returned by accept() or other equipvalent procedure.
- *
- * Returns :
- * NULL if failed
- * pointer to a command session if OK
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static Cli_cmdSession_t *
- Cli_InitCmdSession(Cli_SessionFuns_t *pSessFuns)
- {
- // int i;
- Cli_cmdSession_t *pSess;
- if ((pSess = Cli_NewSess()) == NULL)
- /* No more session space. This should not happen */
- return NULL;
- if (pSessFuns->init)
- pSessFuns->init();
- bzero(pSess, sizeof(Cli_cmdSession_t));
- pSess->pSessFuns = pSessFuns;
- pSess->cmdSessionType = pSessFuns->sessionType;
- pSess->cmdState = CLI_ACCESS_CMD_NULL;
- pSess->historyNextNumber = 1;
- pSess->currentCmdMode = CLI_ACCESS_NO_MODE;
- pSess->myProductType = CLI_ACCESS_PRODUCT_1;
- pSess->pageLength = CLI_DEFAULT_PAGE_LENGTH;
- Cli_WriteBuf(pSess, (char*) init_msg, strlen(init_msg));
- return pSess;
- }
- /*****************************************************************************
- *
- * Jcon_CLI_Task -
- *
- * Jcon DP buffer dump and command line monitor task.
- *
- * Description:
- * Upon starting, console input is read and checked against command list.
- * Most important command functionality is DP buffer dump which
- * is specified in terms of number of lines before the end of the buffer.
- * 'quit' command closes all psos tasks and exits. This is strictly a debug
- * workaround until both tmdbg.exe and Philips telnet Cli work reliably.
- *
- * History:
- * 10/12/99 BM Added header and Jim's code.
- *
- *****************************************************************************/
- static void*
- Jcon_CLI_Task(void *arg)
- {
- char myChar;
- int ret;
- Cli_SessionFuns_t *pSessFuns = (Cli_SessionFuns_t*)arg;
- /* Go straight into CLI now */
- while (1) {
- /* Initialize this command session. Any nsock number will do
- * for JTAG connection.
- */
- Cli_cmdSession_t *pCli_Session = NULL;
- pCli_Session = Cli_InitCmdSession(pSessFuns);
- if (pCli_Session == NULL) {
- pSessFuns->log("****Can't initialize CLI****.n");
- break;
- }
- do {
- ret = pCli_Session->pSessFuns->read_char();
- if ( ret < 0) {
- break;
- }
- if ( !ret )
- pSessFuns->yield();
- myChar = ret;
- /* If session is closed, open another one. */
- } while (Cli_ProcessCmd(&myChar, 1, pCli_Session) != -1);
- Cli_CloseCmdSession(pCli_Session);
- }
- return NULL;
- }
- /************************************************************************
- *
- * Cli_Initialize - Initialize CLI for a JCON session.
- *
- * Returns:
- * int - 0 if no error, nonzero otherwise.
- *
- * Description:
- * Send an echo request to a host. Each TCP/IP stack seems to have
- * their own favorite API for this so we combine them here.
- *
- * Notes:
- * None
- *
- * History:
- * 10/25/01 FD Created.
- *
- ************************************************************************/
- int Cli_Initialize(Cli_SessionFuns_t *pSessFuns)
- {
- static char bInit = 1;
- unsigned long RetVal;
- /* Initialize CLI data set */
- if( bInit ) {
- bInit = 0;
- Cli_InitDataSet ();
- }
- /* Start JCON task now so we have CLI even if boot count suspends root */
- RetVal = pSessFuns->create_task("jcli", Jcon_CLI_Task, 0, 8192, (void*)pSessFuns);
- if (RetVal != 0)
- pSessFuns->log("ERROR: Jcon_CLI_Task(jcli) failed! status = %dn", RetVal);
- return (int) RetVal;
- }
- /*****************************************************************************
- * void Cli_IncrementRowCounter(Cli_cmdSession_t *pSess)
- * void Cli_ResetRowCounter(Cli_cmdSession_t *pSess)
- * void Cli_DisableScrollPause(Cli_cmdSession_t *pSess)
- * void Cli_EnableScrollPause(Cli_cmdSession_t *pSess)
- * int Cli_SetPageLength(Cli_cmdSession_t *pSess, int NewPageLen)
- * int Cli_GetScrollPauseStatus(Cli_cmdSession_t *pSess, int *PageLen)
- *
- * Description :
- * These functions control the pausing of scrolling.
- *
- * Arguments :
- * pSess - the pointer to the command session
- *
- * Returns :
- * 0 if OK, -1 if failed.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static void Cli_IncrementRowCounter(Cli_cmdSession_t *pSess)
- {
- pSess->currentRow++;
- }
- static void Cli_ResetRowCounter(Cli_cmdSession_t *pSess)
- {
- pSess->currentRow = 0;
- }
- void Cli_DisableScrollPause(Cli_cmdSession_t *pSess)
- {
- pSess->scrollPause = 0;
- }
- void Cli_EnableScrollPause(Cli_cmdSession_t *pSess)
- {
- pSess->scrollPause = 1;
- }
- int Cli_SetPageLength(Cli_cmdSession_t *pSess, int NewPageLen)
- {
- int OldLen;
- /* Keep a lower bound for page length. */
- if (NewPageLen < CLI_MINIMAL_PAGE_LENGTH)
- {
- NewPageLen = CLI_MINIMAL_PAGE_LENGTH;
- }
- OldLen = pSess->pageLength;
- pSess->pageLength = NewPageLen;
- return 0;
- }
- int Cli_GetScrollPauseStatus(Cli_cmdSession_t *pSess, int *PageLen)
- {
- if (PageLen)
- {
- *PageLen = pSess->pageLength;
- }
- return (pSess->scrollPause);
- }
- /*****************************************************************************
- * int Cli_Prompt(Cli_cmdSession_t *pSess, int PromptStrOffset)
- *
- * Description :
- * This function prints the prompt with the proper history number embedded.
- *
- * Arguments :
- * the pointer to the command session, and offset into the text string
- * "prompt". The offset thing is a cheap way to print either one or two
- * newlines. The offset should never be larger than 2.
- *
- * Returns :
- * 0 if OK, -1 if failed.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- int Cli_Prompt(Cli_cmdSession_t *pSess, int PromptStrOffset)
- {
- /* Scrolling control can never span commands. Therefore everytime we
- * need to print the prompt, we reset row counter.
- */
- Cli_ResetRowCounter(pSess);
- pSess->skipPrinting = 0;
- if (PromptStrOffset != 0)
- {
- Cli_Printf(pSess, "n") ;
- }
- return (Cli_Printf(pSess, "%s%d>", pSess->prompt, pSess->historyNextNumber));
- }
- /*****************************************************************************
- * int Cli_ProcessCmd(char *msgbuf, int len, Cli_cmdSession_t *pSess)
- *
- * Description :
- * This function is called to pass in a message buffer from user input. It
- * performs all the functionalities for proper command line interface including
- * user login, password validation, CR detection and subsequent command
- * processing, '?' character detectin and subsequent help processing and so
- * forth.
- *
- * Special notice : a char string containing the command is ALWAYS NULL
- * terminated. The length field is also passed in for
- * convineance.
- *
- * Arguments :
- * pointer to message buffer (msgbuf), its length (len) and pointer to session.
- *
- * Returns :
- * 0 if OK, -1 if bad. A return of -1 must trigger a shutdown of this
- * socket.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- int Cli_ProcessCmd(char *msgbuf, int len, Cli_cmdSession_t *pSess)
- {
- int i, j, promptStrOffset;
- for (i = 0; i < len; i++)
- {
- if (msgbuf[i] == 0x0A)
- {
- /* ignore line feed (0x0A), Return (0x0D) is what we are looking for. We
- * will regenerate the combo LF/CR later.
- */
- continue;
- }
- if (msgbuf[i] == 0x0D)
- {
- /* We can now process the CR/LF */
- switch (pSess->cmdState)
- {
- case CLI_ACCESS_CMD_NULL:
- /* Add any special pre-loggin processing code here. Otherwise
- * it just go straight to getting user login.
- */
- if (pSess->cmdSessionType == CLI_SERIAL_SESSION)
- {
- memcpy(pSess->myUserName, CLI_LOGIN_OPERATOR, strlen(CLI_LOGIN_OPERATOR));
- Cli_VerifyPassword(pSess, CLI_OPERATOR_PASSWORD, strlen(CLI_OPERATOR_PASSWORD));
- Cli_PrepareOpenSession(pSess);
- pSess->cmdState = CLI_ACCESS_CMD_OPEN;
- }
- else
- {
- pSess->cmdState = CLI_ACCESS_CMD_INIT;
- }
- break;
- case CLI_ACCESS_CMD_INIT:
- if (pSess->cmdBufIdx == 0) {
- /* user keep hitting Return */
- if (Cli_Printf((Cli_cmdSession_t*)pSess, "nusername:"))
- {
- return -1;
- }
- pSess->cmdState = CLI_ACCESS_CMD_PROMPT_USER;
- }
- break;
- case CLI_ACCESS_CMD_PROMPT_USER:
- /* Make sure the string is NULL terminated */
- pSess->cmdBuf[pSess->cmdBufIdx] = 0;
- if ( (pSess->cmdBufIdx == 0) || (pSess->cmdBufIdx > MAX_USERPASS_LEN) )
- {
- /* Damn user hit Return again */
- if (Cli_Printf((Cli_cmdSession_t*)pSess, "nusername:"))
- {
- return -1;
- }
- }
- else
- {
- /* got username */
- memcpy(pSess->myUserName, pSess->cmdBuf, pSess->cmdBufIdx);
- Cli_SendCrLf(pSess);
- pSess->myUserName[pSess->cmdBufIdx] = 0;
- if (Cli_Printf((Cli_cmdSession_t *)pSess, "password:"))
- {
- return -1;
- }
- pSess->cmdState = CLI_ACCESS_CMD_PROMPT_PASS;
- }
- break;
- case CLI_ACCESS_CMD_PROMPT_PASS:
- /* Make sure the string is NULL terminated */
- pSess->cmdBuf[pSess->cmdBufIdx] = 0;
- Cli_SendCrLf(pSess);
- if ((pSess->cmdBufIdx > MAX_USERPASS_LEN) ||
- Cli_VerifyPassword(pSess, pSess->cmdBuf, pSess->cmdBufIdx) == -1)
- {
- if (Cli_Printf(pSess, badPasswordStr) < 0)
- {
- return -1;
- }
- pSess->cmdState = CLI_ACCESS_CMD_INIT;
- pSess->loginFailedCount++;
- /* Kick this user off if he/she keeps failing */
- if (pSess->loginFailedCount > MAX_LOGIN_FAILURE)
- {
- return -1;
- }
- }
- else
- {
- pSess->cmdState = CLI_ACCESS_CMD_OPEN;
- if (Cli_PrepareOpenSession(pSess) != 0)
- {
- return -1;
- }
- }
- break;
- case CLI_ACCESS_CMD_OPEN:
- /* Make sure the string is NULL terminated */
- pSess->cmdBuf[pSess->cmdBufIdx] = 0;
- /* If user just hit CR then cmdBufIdx is 0. In this case we don't
- * print 2 LF. Otherwise 2 LF is used.
- */
- if(pSess->cmdBufIdx == 0)
- promptStrOffset = 1 ;
- else
- promptStrOffset = 0;
- /* copy the last command into history buffer as well; note that even
- * bogus commands are copied. Anything ended with a CR is a command.
- */
- Cli_UpdateCmdHistory(pSess, pSess->cmdBuf, pSess->cmdBufIdx);
- if (Cli_ParseCmd(pSess, pSess->cmdBuf, pSess->cmdBufIdx) == -1)
- {
- Cli_Printf(pSess, endSessionStr);
- return -1;
- }
- if (Cli_Prompt(pSess, promptStrOffset) < 0)
- {
- return -1;
- }
- break;
- }
- /* Done processing last command, reset index */
- pSess->cmdBufIdx = 0;
- }
- else if ((msgbuf[i] == 't') &&
- (pSess->cmdState == CLI_ACCESS_CMD_OPEN))
- {
- pSess->cmdBuf[pSess->cmdBufIdx] = 't';
- pSess->cmdBufIdx++;
- pSess->cmdBuf[pSess->cmdBufIdx] = 0;
- if (Cli_Printf(pSess, "?") < 0)
- {
- return -1;
- }
- Cli_UpdateCmdHistory(pSess, pSess->cmdBuf, pSess->cmdBufIdx);
- if ((j = Cli_ParseCmd(pSess, pSess->cmdBuf, pSess->cmdBufIdx)) == -1)
- {
- Cli_Printf(pSess, endSessionStr);
- return -1;
- }
- if (Cli_Prompt(pSess, 0) < 0)
- {
- return -1;
- }
- if (j != -2)
- {
- /* now back out the '?' and print what user type */
- pSess->cmdBufIdx--;
- pSess->cmdBuf[pSess->cmdBufIdx] = 0;
- if (Cli_Printf(pSess, pSess->cmdBuf) < 0)
- {
- return -1;
- }
- }
- else
- {
- /* try to get help on bogus command, reset command buffer */
- pSess->cmdBufIdx = 0;
- }
- }
- else
- {
- if (msgbuf[i] == 'b')
- {
- /* If it's backspace, back cmdBufIdx up */
- if (pSess->cmdBufIdx > 0)
- {
- pSess->cmdBufIdx--;
- if (Cli_Printf(pSess, "b ") < 0)
- {
- return -1;
- }
- }
- else
- continue;
- }
- else
- {
- if ( pSess->cmdBufIdx >= (CLI_ACCESS_CMDSTR_SIZE - 2) )
- {
- if (pSess->cmdState == CLI_ACCESS_CMD_NULL)
- {
- pSess->cmdBufIdx = 0;
- }
- else
- {
- /* User has typed too much without hitting return or ?.
- * Don't copy into command buffer and don't echo it back.
- */
- continue;
- }
- }
- pSess->cmdBuf[pSess->cmdBufIdx] = msgbuf[i];
- pSess->cmdBufIdx++;
- }
- if ((pSess->cmdState != CLI_ACCESS_CMD_INIT) &&
- (pSess->cmdState != CLI_ACCESS_CMD_PROMPT_PASS))
- {
- if (Cli_PutChar(pSess, (msgbuf[i])) < 0)
- {
- return -1;
- }
- }
- }
- }
- return 0;
- }
- /*****************************************************************************
- *
- * Cli_PrepareOpenSession - Displays session banner and initializes mode
- *
- * Returns:
- * int - 0 on success, -1 on failure.
- *
- * Description:
- * Called to prepare for entering session Open state after authenticating the
- * user.
- *
- * History:
- * 12/19/99 MA Original version
- *
- *****************************************************************************/
- int Cli_PrepareOpenSession (Cli_cmdSession_t *pSess)
- {
- int j;
- /* Print Philips logo */
- if (Cli_Printf(pSess,(char*) banner1) < 0)
- return -1;
- /* By default turn on scroll control. */
- Cli_EnableScrollPause(pSess);
- Cli_Printf(pSess, "Screen length is set to %d by default"
- ", paging is enabled.n",
- pSess->pageLength);
- if (Cli_Prompt(pSess, 0) < 0)
- {
- return -1;
- }
- return 0;
- }
- /*****************************************************************************
- * static Cli_CmdStruct_t *Cli_SeekOptionMatch(Cli_CmdStruct_t *baseCmdObj,
- * char *thisCmd,
- * int thisCmdLen)
- *
- * Description :
- * This function scans the optionList of the supplied Cli_CmdStruct_t object to
- * find a match for the currently processed command/option.
- *
- * Arguments :
- * pointer to the base Cli_CmdStruct_t object (baseCmdObj), currently processed
- * command (thisCmd) and its length (thisCmdLen).
- *
- * Returns :
- * If a Cli_CmdStruct_t object is in optionList[] matches, it's pointer is
- * returned. If no match, NULL is return.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static Cli_CmdStruct_t *Cli_SeekOptionMatch(Cli_CmdStruct_t *baseCmdObj,
- char *thisCmd,
- int thisCmdLen)
- {
- int i;
- if (baseCmdObj->optionList == NULL)
- {
- return NULL;
- }
- i = 0;
- while (baseCmdObj->optionList[i] != NULL)
- {
- if ( (strlen(baseCmdObj->optionList[i]->name) == (unsigned int) thisCmdLen) &&
- !memcmp(baseCmdObj->optionList[i]->name, thisCmd, thisCmdLen))
- {
- return baseCmdObj->optionList[i];
- }
- i++;
- }
- return NULL;
- }
- /*****************************************************************************
- * static int Cli_Cmdprocessing(Cli_cmdSession_t *pSess,
- * Cli_CmdStruct_t *baseCmdObj,
- * char *cmdStr,
- * int cmdPtr,
- * int recurLevel)
- *
- * Description :
- * This RECURSIVE function performs all help and command processing function.
- * To safeguard deep recursion, a maximum depth of 4 calls is enforced.
- *
- * Arguments :
- * see it for yourself.
- *
- * Returns :
- * 0 if OK, -1 if failed.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static int Cli_Cmdprocessing(Cli_cmdSession_t *pSess,
- Cli_CmdStruct_t *baseCmdObj,
- char *cmdStr,
- int cmdPtr,
- int recurLevel)
- {
- char *thisCmd;
- int i, thisCmdLen, oldCmdPtr;
- Cli_CmdStruct_t *thisCmdObj;
- unsigned int myAccessFlags;
- myAccessFlags = TO_ACCESS_FLAG(pSess->currentCmdMode) |
- TO_ACCESS_FLAG(pSess->myProductType);
- if (recurLevel > 4)
- {
- return 0;
- }
- recurLevel++;
- oldCmdPtr = cmdPtr;
- /* Find out the current command option and size */
- thisCmd = cmdStr + cmdPtr;
- thisCmdLen = 0;
- for (i = cmdPtr; i < CLI_ACCESS_CMDSTR_SIZE; i++)
- {
- if (Is_space(cmdStr[i]) || cmdStr[i] == 0)
- {
- thisCmdLen = i - cmdPtr;
- break;
- }
- }
- if (thisCmdLen <= 0)
- {
- /* no command or option at all, return */
- return 0;
- }
- /* Have cmdPtr indexes the start of next options, or end of string */
- for (i = cmdPtr + thisCmdLen; i < CLI_ACCESS_CMDSTR_SIZE; i++)
- {
- if (!Is_space(cmdStr[i]) || cmdStr[i] == 0)
- {
- cmdPtr = i;
- break;
- }
- }
- /* Perform Help processing */
- if (thisCmd[0] == 't')
- {
- i = 0;
- if (baseCmdObj->optionList == NULL) {
- /* Without a optionList pointer, this is a leaf node. There isn't
- * any other subcommands after this.
- */
- if (baseCmdObj->helpStr != NULL)
- {
- Cli_SendCrLf(pSess);
- if (Cli_Printf(pSess, baseCmdObj->helpStr) < 0)
- {
- return -1;
- }
- Cli_SendCrLf(pSess);
- }
- else if (Cli_Printf(pSess, noHelpStr) < 0)
- {
- return -1;
- }
- } else {
- int PrintVerbage = 1;
- /* print out all available root level commands */
- while (baseCmdObj->optionList[i] != NULL)
- {
- if ((baseCmdObj->optionList[i]->accessFlag & myAccessFlags) == myAccessFlags)
- {
- if (PrintVerbage)
- {
- if (Cli_Printf(pSess, cmdOptionsAreStr) < 0)
- {
- return -1;
- }
- PrintVerbage = 0;
- }
- if (Cli_Printf(pSess, " %-16s ", baseCmdObj->optionList[i]->name) < 0)
- {
- return -1;
- }
- if (Cli_Printf(pSess, "%sn", baseCmdObj->optionList[i]->helpStr) < 0)
- {
- return -1;
- }
- }
- i++;
- }
- }
- /* call additional help function if available */
- if (baseCmdObj->helpFunc != NULL)
- return baseCmdObj->helpFunc(pSess, cmdStr, cmdPtr);
- }
- /* Perform command processing */
- else
- {
- /* Look for the Cli_CmdStruct_t object which represents the command
- * option currently under processing.
- */
- thisCmdObj = Cli_SeekOptionMatch(baseCmdObj, thisCmd, thisCmdLen);
- if (thisCmdObj != NULL)
- {
- if ((cmdStr[cmdPtr] == 0) )
- {
- /* no more command options, or no optionList[],
- * must execute handler.
- */
- if ((thisCmdObj->procFunc != NULL) &&
- ((thisCmdObj->accessFlag & myAccessFlags) == myAccessFlags))
- {
- if (Cli_Printf(pSess, "n") < 0)
- {
- return -1 ;
- }
- return thisCmdObj->procFunc(pSess, cmdStr, cmdPtr);
- }
- else
- {
- if (Cli_Printf(pSess, invalidCmdStr) < 0)
- {
- return -1;
- }
- return 0;
- }
- }
- else
- {
- /* user has typed in some option, run Cli_Cmdprocessing() again.
- */
- return Cli_Cmdprocessing(pSess, thisCmdObj, cmdStr, cmdPtr, recurLevel);
- }
- }
- else
- {
- /* No match in optionList[]. There can be 2 reasons.
- * 1) user type in a bogus option.
- * 2) user type in a variable option.
- *
- * It is assumed that next higher (caller) cmdObj that supports
- * variable option will have a varOptFunc(). If no such handler
- * is defined, caller will determine that option is bogus.
- */
- if ((baseCmdObj->varOptFunc != NULL) &&
- ((baseCmdObj->accessFlag & myAccessFlags) == myAccessFlags))
- {
- if (Cli_Printf(pSess, "n") < 0)
- {
- return -1 ;
- }
- return baseCmdObj->varOptFunc(pSess, cmdStr, oldCmdPtr); //zouyx add
- }
- else
- {
- if (Cli_Printf(pSess, invalidCmdStr) < 0)
- {
- return -1;
- }
- }
- return 0;
- }
- }
- return 0;
- }
- /*****************************************************************************
- * static int Cli_BuildCmdObj(Cli_CmdStruct_t **rootList,
- * Cli_CmdStruct_t **workingList,
- * unsigned int accessflags)
- *
- * Description :
- * This function is called to construct a working root command list based on
- * the supplied accessFlags.
- *
- *
- * Arguments :
- * the root list (typical a list of all command), working list and the
- * access flags.
- *
- * Returns :
- * Always returns 0.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static int Cli_BuildCmdObj(Cli_CmdStruct_t **rootList,
- Cli_CmdStruct_t **workingList,
- unsigned int accessflags)
- {
- int i, j;
- i = j = 0;
- while(rootList[i] != NULL)
- {
- if (rootList[i]->accessFlag & accessflags)
- {
- workingList[j]=rootList[i];
- j++;
- }
- i++;
- }
- return 0;
- }
- /*****************************************************************************
- * static int Cli_SetAccessMode(Cli_cmdSession_t *pSess, int mode)
- *
- * Description :
- * This function must be called when user first login and enter/exit to
- * different access level (e.g. go from operator to admin). Based on
- * current access mode, user access right and product type, a working list
- * of commands is constructed.
- *
- * Arguments :
- * pointer to a session, plus access mode.
- *
- * Returns :
- * Always returns 0.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static int Cli_SetAccessMode(Cli_cmdSession_t *pSess, int accessLevel)
- {
- // unsigned flags;
- pSess->currentCmdMode = accessLevel;
- sprintf(pSess->prompt, "[%s]", modePrompt[accessLevel]);
- return 0;
- }
- /*****************************************************************************
- * static int Cli_VerifyPassword(Cli_cmdSession_t *pSess, char *password, int len)
- *
- * Description :
- * This function verifies password against the current user of this session.
- * More sophisticated method of verification should be used in the future.
- *
- * Arguments :
- * password string plus it's length.
- *
- * Returns :
- * 0 if verified OK, -1 if failed.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static int Cli_VerifyPassword(Cli_cmdSession_t *pSess, char *password, int len)
- {
- /* These are the four level of access to CLI. To make thing easy, it maps
- * to 4 unique username. For example, if you log in as "Eng", you
- * automatically have ENG level of access.
- */
- char UserPassword[] = CLI_USER_PASSWORD;
- char OperPassword[] = CLI_OPERATOR_PASSWORD;
- char EngPassword[] = CLI_ENG_PASSWORD;
- char BloPassword[] = CLI_BLO_PASSWORD;
- int AccessLevel;
- char *DiffPassword;
- if (!memcmp(pSess->myUserName, CLI_LOGIN_USER, strlen(CLI_LOGIN_USER)))
- {
- AccessLevel = CLI_ACCESS_USER_MODE;
- DiffPassword = UserPassword;
- }
- else if (!memcmp(pSess->myUserName, CLI_LOGIN_OPERATOR, strlen(CLI_LOGIN_OPERATOR)))
- {
- AccessLevel = CLI_ACCESS_TECH_MODE; /* operator = technican in my book */
- DiffPassword = OperPassword;
- }
- else if ( (!memcmp(pSess->myUserName, CLI_LOGIN_ENG, strlen(CLI_LOGIN_ENG))) ||
- (!memcmp(pSess->myUserName, CLI_LOGIN_ENG2, strlen(CLI_LOGIN_ENG2))) )
- {
- AccessLevel = CLI_ACCESS_ENG_MODE;
- DiffPassword = EngPassword;
- }
- else if (!memcmp(pSess->myUserName, CLI_LOGIN_BLO, strlen(CLI_LOGIN_BLO)))
- {
- AccessLevel = CLI_ACCESS_BLO_MODE;
- DiffPassword = BloPassword;
- }
- else
- {
- /* Unknown user ! */
- return -1;
- }
- if ( (( unsigned int) len == (strlen(DiffPassword))) &&
- (!memcmp(password, DiffPassword, strlen(DiffPassword))) )
- {
- Cli_SetAccessMode(pSess, AccessLevel);
- return 0;
- }
- return -1;
- }
- /*****************************************************************************
- * static int Cli_ParseCmd(Cli_cmdSession_t *pSess, char *cmdBuf, int len)
- *
- * Description :
- * This function is called when the session is in OPEN state (user successfully
- * logged in) and a command has been entered (CR has been hit). History (!...)
- * processing is also performed here.
- *
- * Arguments :
- * Go take a look.
- *
- * Returns :
- * 0 if OK, -1 if bad thing happened and the socket and session must be
- * terminated.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static int Cli_ParseCmd(Cli_cmdSession_t *pSess, char *cmdBuf, int len)
- {
- int hptr;
- if (len)
- {
- if (cmdBuf[0] == '!')
- {
- /* History processing */
- Cli_SendCrLf(pSess);
- if ((len == 2) && cmdBuf[1] == '!')
- {
- hptr = (pSess->historyPtr + CLI_ACCESS_HISTORY_DEPTH - 1 )% CLI_ACCESS_HISTORY_DEPTH;
- if (Cli_Printf(pSess, pSess->historyBuf[hptr]) < 0)
- {
- return -1;
- }
- }
- else
- {
- int histNum;
- /* Handle case of !XYZ where XYZ is the command history number */
- if (sscanf(cmdBuf+1, "%d", &histNum) == 0)
- {
- if (Cli_Printf(pSess, invalidCmdStr) < 0)
- {
- return -1;
- }
- return 0;
- }
- else
- {
- for (hptr = 0; hptr < CLI_ACCESS_HISTORY_DEPTH; hptr++)
- {
- if(pSess->historyNum[hptr] == histNum)
- break;
- }
- /* Return if no such history is found */
- if (hptr >= CLI_ACCESS_HISTORY_DEPTH)
- {
- if (Cli_Printf(pSess, noSuchHistoryStr) < 0)
- {
- return -1;
- }
- return 0;
- }
- /* Print out the historical command */
- if (Cli_Printf(pSess, pSess->historyBuf[hptr]) < 0)
- {
- return -1;
- }
- }
- }
- /* Record history and process the command as if user just type in the
- * same command rather than !XYZ
- */
- Cli_UpdateCmdHistory(pSess, pSess->historyBuf[hptr], strlen(pSess->historyBuf[hptr]));
- return Cli_Cmdprocessing(pSess, &Cli_WorkingRootCmdObj, pSess->historyBuf[hptr], 0, 0);
- }
- else
- {
- /* Command processing */
- return Cli_Cmdprocessing(pSess, &Cli_WorkingRootCmdObj, cmdBuf, 0, 0);
- }
- }
- return 0;
- }
- /*****************************************************************************
- * static void Cli_UpdateCmdHistory(Cli_cmdSession_t *pSess,
- * char *cmdBuf, int len)
- *
- * Description :
- * This function updates the last command into our ring buffer type command
- * history buffer.
- *
- * Arguments :
- * the session, the command string and its length.
- *
- * Returns :
- * None.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- static void Cli_UpdateCmdHistory(Cli_cmdSession_t *pSess, char *cmdBuf, int len)
- {
- if (cmdBuf[0] == '!')
- {
- /* We don't record history commands */
- return;
- }
- /* Copy the command into an entry in historyBuf[] and null terminated it */
- memcpy(pSess->historyBuf[pSess->historyPtr], cmdBuf, len);
- pSess->historyBuf[pSess->historyPtr][len] = 0;
- pSess->historyNum[pSess->historyPtr] = pSess->historyNextNumber;
- pSess->historyPtr = (pSess->historyPtr + 1) % CLI_ACCESS_HISTORY_DEPTH;
- pSess->historyNextNumber++;
- return;
- }
- /*****************************************************************************
- * int Cli_GetChar(Cli_cmdSession_t *pSess)
- *
- * Description :
- * This function blocks until a character is received unless the connection
- * is discontinued.
- *
- * This is the basic function that does ALL the in-context CLI text input.
- * An in-context CLI text input is used when during processing of a command,
- * additional text input is needed. Normally, text input to a CLI session is
- * via command processing handler.
- *
- * Modify this function to support multiple session type.
- *
- * Arguments :
- * pSess - a pointer to a command session
- *
- * Returns :
- * >= 0 - the character returned
- * -1 - Failed
- *
- * History
- * who when why
- * KBL 11/17/99 created
- *
- *****************************************************************************/
- int Cli_GetChar(Cli_cmdSession_t *pSess)
- {
- char MyChar;
- while ((MyChar = pSess->pSessFuns->read_char()) == 0)
- pSess->pSessFuns->yield();
- return MyChar;
- }
- /*
- * CLI utility functions - based on Cli_PutStr()
- */
- int Cli_PutStr(Cli_cmdSession_t *pSess, char *str, int len)
- {
- int StrIdx, PrintLen;
- char KeyPressed, *pStrStart;
- pStrStart = NULL;
- for (StrIdx = 0; StrIdx < len; StrIdx++)
- {
- if (pStrStart == NULL)
- {
- pStrStart = &(str[StrIdx]);
- }
- if ((str[StrIdx] == 'n') || (StrIdx == (len - 1)))
- {
- PrintLen = &(str[StrIdx]) - pStrStart + 1;
- Cli_WriteBuf(pSess, pStrStart, PrintLen);
- pStrStart = NULL;
- if ((str[StrIdx] == 'n') && pSess->scrollPause)
- {
- pSess->currentRow++;
- if (pSess->currentRow >= (pSess->pageLength - 1))
- {
- Cli_WriteBuf(pSess, waitForAKeyStr, strlen(waitForAKeyStr));
- KeyPressed = Cli_GetChar(pSess);
- /* Clear out the "Press XYZ..." string. */
- /* Cli_Printf(pSess, " r"); */
- /* Check for ESC */
- if (KeyPressed == 0x1b)
- {
- pSess->skipPrinting = 1;
- break;
- }
- /* Check for -1 */
- if (KeyPressed == -1)
- {
- return -1;
- }
- /* Don't forget to reset the row counter ! */
- Cli_ResetRowCounter(pSess);
- }
- }
- }
- }
- return 0;
- }
- /*****************************************************************************
- * int Cli_Printf(Cli_cmdSession_t *pSess, char *fmt, ...)
- *
- * Description :
- * This is the printf function for CLI.
- *
- * Arguments :
- * pCmdSession - opaque pointer to a command session
- * the rest of the parameters are the same as for printf().
- *
- * Returns :
- * 0 - OK
- *
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- int Cli_Printf(Cli_cmdSession_t *pSess, char *fmt, ...)
- {
- va_list ap;
- va_start(ap, fmt);
- vsprintf(pSess->scratchBuf, fmt, ap);
- va_end(ap);
- /* Make sure it's NULL terminated at the very least */
- pSess->scratchBuf[SCRATCH_BUF_SIZE-2] = 0;
- return Cli_PutStr(pSess, pSess->scratchBuf, strlen(pSess->scratchBuf));
- }
- /*****************************************************************************
- * int Cli_PutChar(Cli_cmdSession_t *pSess, char ch)
- *
- * Description :
- * This function print a character.
- *
- * Arguments :
- * pCmdSession - opaque pointer to a command session
- * ch - the character.
- *
- * Returns :
- * 0 - OK
- * -1 - Failed
- *
- * History
- * who when why
- * KBL 9/7/99 created
- *
- *****************************************************************************/
- int Cli_PutChar(Cli_cmdSession_t *pSess, char ch)
- {
- return Cli_PutStr(pSess, &ch, 1);
- }
- /*****************************************************************************
- * int Cli_SendCrLf(Cli_cmdSession_t *pSess)
- *
- * Description :
- * send a CR/LF pair to advance a line.
- *
- * Arguments :
- * pSess - pointer to the session.
- *
- * Returns :
- * 0 if OK
- * -1 if Failed, likely cause is a bad socket.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- int Cli_SendCrLf(Cli_cmdSession_t *pSess)
- {
- return ( Cli_Printf(pSess, "n") );
- }
- /*****************************************************************************
- * int Cli_PrintLine(Cli_cmdSession_t *pSess,
- * int NumChar,
- * char Character,
- * int NumFrontCR,
- * int NumBackCR)
- *
- * Description :
- * This function draws a line made up of "Character" of "NumChar" width.
- * If "NumFrontCR" is 1, a CR is placed before the line. Likewise if
- * "NumBackCR" is 1, a CR is placed after the line.
- *
- * Arguments :
- * pSess - pointer to the session.
- * see above for explantion for the other parameters.
- *
- * Returns :
- * 0 if OK
- * -1 if Failed, likely cause is a bad socket.
- *
- * History
- * who when why
- * KBL 7/7/99 created
- *
- *****************************************************************************/
- int Cli_PrintLine(Cli_cmdSession_t *pSess,
- int NumChar,
- char Character,
- int NumFrontCR,
- int NumBackCR)
- {
- int Cnt;
- for (Cnt = 0; Cnt < NumFrontCR; Cnt++)
- {
- if (Cli_SendCrLf(pSess) == -1) return -1;
- }
- for (Cnt = 0; Cnt < NumChar; Cnt++)
- {
- if (Cli_Printf(pSess, "%c", Character) == -1) return -1;
- }
- for (Cnt = 0; Cnt < NumBackCR; Cnt++)
- {
- if (Cli_SendCrLf(pSess) == -1) return -1;
- }
- return 0;
- }
- /*****************************************************************************
- * int Cli_PrintFileContent(Cli_cmdSession_t *pSess, char *FileName)
- *
- * Description :
- * This function prints out the content of a file. The file must better be
- * a text file, otherwise weird thing may happen.
- *
- * Arguments :
- * pSess - pointer to the session.
- * FileName - pointer to a filename string.
- *
- * Returns :
- * 0 if OK
- * -1 if Failed, likely cause is a bad socket.
- *
- * History
- * who when why
- * KBL 11/24/99 created
- *
- *****************************************************************************/
- int Cli_PrintFileContent(Cli_cmdSession_t *pSess, char *FileName)
- {
- FILE *fp;
- char ScratchPadStr[100];
- if ((fp = fopen(FileName, "rt")) == NULL)
- {
- Cli_Printf(pSess, "Could not open file %sn", FileName);
- return 0;
- }
- Cli_SendCrLf(pSess);
- Cli_SendCrLf(pSess);
- Cli_Printf(pSess, "%s :n", FileName);
- while (fgets(ScratchPadStr, (sizeof(ScratchPadStr) - 1), fp) != NULL )
- {
- if (Cli_Printf(pSess, "%s", ScratchPadStr) != 0)
- {
- fclose(fp);
- return -1;
- }
- }
- fclose(fp);
- return 0;
- }
- /*+***************************************************************************
- *
- * Cli_PrintData - Utility function for dumping memory
- *
- * Returns:
- * void-
- *
- * Description:
- * Formats for display the contents of a memory buffer. The eFormat option
- * indicates the data width and representation (dec, hex). The bUseRelativeAddr
- * parameter determines whether a zero-based index is displayed or the absolute
- * address of the data at the start of each line.
- *
- * History:
- * 1/13/00 MA Original version
- *
- **-**************************************************************************/
- void Cli_PrintData(Cli_cmdSession_t *pSess, void *pvBuffer, int nBytes,
- Cli_PrintFormat_e eFormat, int bUseRelativeAddr)
- {
- int i, j;
- int LineLen;
- char Char;
- int iChar;
- if (eFormat == CLI_PRINT_DATA_FORMAT_HEX8)
- {
- LineLen = 16;
- }
- else if (eFormat <= CLI_PRINT_DATA_FORMAT_HEX32)
- {
- LineLen = 4;
- }
- else
- {
- LineLen = 8;
- }
- for (i = 0; i < nBytes;)
- {
- if (bUseRelativeAddr)
- Cli_Printf(pSess, "0x%04x: ", i);
- else
- Cli_Printf(pSess, "0x%08x: ", (u_int32_t)pvBuffer + i);
- /* Save start offset of line */
- iChar = i;
- for (j = 0; j < LineLen && i < nBytes; j++)
- {
- switch (eFormat)
- {
- case CLI_PRINT_DATA_FORMAT_INT32:
- Cli_Printf(pSess, "%11d ", *((int *)((u_int32_t)pvBuffer + i)));
- i += sizeof(int);
- break;
- case CLI_PRINT_DATA_FORMAT_UINT32:
- Cli_Printf(pSess, "%10lu ", *((unsigned int *)((u_int32_t)pvBuffer + i)));
- i += sizeof(int);
- break;
- case CLI_PRINT_DATA_FORMAT_INT16:
- Cli_Printf(pSess, "%6d ", (int) *((int16_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(int16_t);
- break;
- case CLI_PRINT_DATA_FORMAT_UINT16:
- Cli_Printf(pSess, "%5lu ", (unsigned int) *((u_int16_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(int16_t);
- break;
- case CLI_PRINT_DATA_FORMAT_INT8:
- Cli_Printf(pSess, "%4d ", (int) *((int8_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(int8_t);
- break;
- case CLI_PRINT_DATA_FORMAT_UINT8:
- Cli_Printf(pSess, "%3d ", (unsigned int) *((u_int8_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(int8_t);
- break;
- case CLI_PRINT_DATA_FORMAT_HEX32:
- Cli_Printf(pSess, "%08x ", *((u_int32_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(u_int32_t);
- break;
- case CLI_PRINT_DATA_FORMAT_HEX16:
- Cli_Printf(pSess, "%04x ", (u_int32_t) *((u_int16_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(u_int16_t);
- break;
- default:
- case CLI_PRINT_DATA_FORMAT_HEX8:
- Cli_Printf(pSess, "%02x ", (u_int32_t) *((u_int8_t *)((u_int32_t)pvBuffer + i)));
- i += sizeof(u_int8_t);
- break;
- }
- }
- if (eFormat == CLI_PRINT_DATA_FORMAT_HEX8)
- {
- /* Align to start of text column */
- for (;j < LineLen + 1; j++)
- {
- Cli_Printf(pSess, " ");
- }
- /* Now print ASCII representation of this line */
- for (j = 0; j < LineLen && iChar < nBytes; j++, iChar++)
- {
- Char = *(char *)((u_int32_t)pvBuffer + iChar);
- if (!isprint(Char))
- Char = '.';
- Cli_Printf(pSess, "%c", Char);
- }
- }
- Cli_Printf(pSess, "n");
- }
- }
- /*****************************************************************************
- * int Cli_IpAndMask2int()
- *
- * Description :
- * convert from a string like "A.B.C.D/P.Q.R.S" to its IP address and mask
- * in unsigned int.
- *
- * Arguments :
- * str is where the string start, the values are returned via addr and mask.
- *
- * Returns :
- * 0 if OK, -1 if convertion error. Both addr & mask are returned in network
- * byte order.
- *
- * History
- * who when why
- * KBL 7/15/99 created
- *
- *****************************************************************************/
- int Cli_IpAndMask2int(char *str, unsigned int *addr, unsigned int *mask)
- {
- int iByte, value;
- unsigned int ipaddress = 0;
- unsigned int ipmask = 0;
- char *pb, *pb2;
- pb = str;
- pb2 = strchr(pb, '/') + 1; /* knows where the mask is before messing up */
- /* Parse dotted IP address */
- for (iByte = 0; iByte < 4; iByte++)
- {
- value = atoi(pb);
- if (value > 255)
- {
- return -1;
- }
- ipaddress |= (value << 24);
- if (iByte < 3)
- {
- ipaddress >>= 8;
- pb = strchr (pb, '.');
- if (pb == NULL)
- {
- return -1;
- }
- pb++; /* move to the number field */
- }
- }
- /* If the caller only need IP address, mask is NULL, we return here. */
- if (mask == NULL)
- {
- *addr = ipaddress;
- return 0;
- }
- /* make sure the next "." is after "/" */
- pb = strchr(pb, '.');
- if ((pb == NULL) || (pb < pb2))
- {
- return -1;
- }
- /* Parse dotted IP mask */
- for (iByte = 0; iByte < 4; iByte++)
- {
- value = atoi(pb2);
- if (value > 255)
- {
- return -1;
- }
- ipmask |= (value << 24);
- if (iByte < 3)
- {
- ipmask >>= 8;
- pb2 = strchr (pb2, '.');
- if (pb2 == NULL)
- {
- return -1;
- }
- pb2++;
- }
- }
- /* make sure there is no more "." */
- pb2 = strchr(pb2, '.');
- if (pb2 != NULL)
- {
- return -1;
- }
- *addr = ipaddress;
- *mask = ipmask;
- return 0;
- }