⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shelllib.c

📁 VXWORKS源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
	}    return (status);    }/********************************************************************************* shellScriptAbort - signal the shell to stop processing a script** This routine signals the shell to abort processing a script file.* It can be called from within a script if an error is detected.** RETURNS: N/A** SEE ALSO:* .pG "Target Shell"*/void shellScriptAbort (void)    {    shellAbort = TRUE;    }/********************************************************************************* shellHistory - display or set the size of shell history** This routine displays shell history, or resets the default number of* commands displayed by shell history to <size>.  By default, history size* is 20 commands.  Shell history is actually maintained by ledLib.** RETURNS: N/A** SEE ALSO: ledLib, h(),* .pG "Target Shell,"* windsh,* .tG "Shell"*/void shellHistory    (    int size    /* 0 = display, >0 = set history to new size */    )    {    ledControl (ledId, NONE, NONE, size);    }/********************************************************************************* execute - interpret and execute a source line** This routine parses and executes the specified source line.* First any I/O redirection is cracked, then if any text remains,* that text is parsed and executed via the yacc-based interpreter.* If no text remains after the I/O redirection, then the shell itself* is invoked (recursively) with the appropriate redirection.* Note that blank lines, null lines, and comment lines should NOT* be passed to this routine.  Initial blanks should be stripped too!** RETURNS: OK or ERROR.** NOMANUAL*/STATUS execute    (    FAST char *line    )    {    int newInFd;    int newOutFd;    int oldInFd  = ioGlobalStdGet (STD_IN);    int oldOutFd = ioGlobalStdGet (STD_OUT);    STATUS status;    /* get any redirection specs */    if (getRedir (line, &newInFd, &newOutFd) != OK)	return (ERROR);    if (*line == EOS)	{        /* set any redirections specified, call shell, and restore streams */	if (newInFd >= 0)	    ioGlobalStdSet (STD_IN, newInFd);	if (newOutFd >= 0)	    ioGlobalStdSet (STD_OUT, newOutFd);	status = execShell (FALSE);	ioGlobalStdSet (STD_IN, oldInFd);	ioGlobalStdSet (STD_OUT, oldOutFd);	}    else	{	/* set global stream fds for redirection of function calls;	 * a -1 means no redirection	 */	redirInFd = newInFd;	redirOutFd = newOutFd;	/* initialize parse variables and parse and execute line */	yystart (line);	status = (yyparse () == 0) ? OK : ERROR;	}    /* close redirection files */    if (newInFd >= 0)	close (newInFd);    if (newOutFd >= 0 && close (newOutFd) == ERROR)	printf ("can't close output.\n");    return (status);    }/********************************************************************************* getRedir - establish redirection specified on input line** This routines picks the redirection specs off the end of the input line.* The values pointed to by pInFd and pOutFd are set to -1 if the input and* output respectively are not redirected, and to the file descriptor (fd) of* the redirection stream if they are redirected.  Note that this routine* also trucates from the end of the input line any successfully cracked* redirection specs, i.e. an EOS is inserted in the input line at the point* where the redirection specs began.** RETURNS: ERROR if error in redirection spec or opening stream,* OK if successful redirection found, or no redirection found.*/LOCAL STATUS getRedir    (    char *line,    FAST int *pInFd,    /* -1, or fd of of input redirection */    FAST int *pOutFd    /* -1, or fd of of output redirection */    )    {    *pInFd = *pOutFd = -1;    if (get1Redir (line, pInFd, pOutFd) != OK ||        get1Redir (line, pInFd, pOutFd) != OK)	{	if (*pInFd >= 0)	    close (*pInFd);	if (*pOutFd >= 0)	    close (*pOutFd);	return (ERROR);	}    return (OK);    }/********************************************************************************* get1Redir - get a redirection from a line** This routine picks a single redirection specification off the end* of the specified line.** RETURNS: ERROR if error in redirection spec or opening stream,* OK if successful redirection found, or no redirection found.*/LOCAL STATUS get1Redir    (    char *line,         /* line to scan */    int *pInFd,         /* if '<' found then fd is assigned here */    int *pOutFd         /* if '>' found then fd is assigned here */    )    {    FAST char *p;	       /* position in line */    char *name;		       /* name of redirection file if found  */    char *errnoString = NULL;  /* pointer to symTbl's copy of string */     int	 ourErrno;     if (strlen (line) == 0)	return (OK);    /* Set p == end of line */    p = line + strlen (line) - 1;    /* pick off last word and back up to previous non-blank before that */    while (p > line && *p == ' ')	{	p--;		/* skip trailing blanks */	}    *(p + 1) = EOS;    /* stop searching if:     *   reached beginning of line,     *   reached a blank,     *   reached a redirection token,     *   hit a semicolon or quote.     */    while (p > line  && *p != ' ' &&	   *p != '<' && *p != '>' &&	   *p != ')' && *p != ';' &&	   *p != '"')	{	p--;		/* skip back to start of word */	}    name = p + 1;	/* name must begin here */    while (p > line && *p == ' ')	p--;		/* skip back to previous non-blank */    /* is this a redirection? */    if (*p == '>' && *(p -1) != '>')	{	if (*pOutFd >= 0)	    {	    printf ("ambiguous output redirect.\n");	    return (ERROR);	    }	if ((*pOutFd = creat (name, O_WRONLY)) < 0)	    {	    printf ("can't create output '%s'\n", name);	    ourErrno = errno;	    if ((errnoStringGet (ourErrno, errnoString) == OK) &&		(errnoString != NULL))		printf (redirErrMsgWithString, ourErrno, errnoString);	    else		printf (redirErrMsg, ourErrno);	    return (ERROR);	    }	*p = EOS; /* truncate to exclude the redirection stuff just used */	}    else if (*p == '<' && (p == line || *(p -1) != '<'))	{	if (*pInFd >= 0)	    {	    printf ("ambiguous input redirect.\n");	    return (ERROR);	    }	if ((*pInFd = open (name, O_RDONLY, 0)) < 0)	    {	    printf ("can't open input '%s'\n", name);	    ourErrno = errno;	    if ((errnoStringGet (ourErrno, errnoString) == OK) &&		(errnoString != NULL))		printf (redirErrMsgWithString, ourErrno, errnoString);	    else		printf (redirErrMsg, ourErrno);	    return (ERROR);	    }	*p = EOS; /* truncate to exclude the redirection stuff just used */	}    return (OK);    }/********************************************************************************* shellPromptSet - change the shell prompt** This routine changes the shell prompt string to <newPrompt>.** RETURNS: N/A** SEE ALSO:* .pG "Target Shell,"* windsh,* .tG "Shell"*/void shellPromptSet    (    char *newPrompt     /* string to become new shell prompt */    )    {    strncpy (promptString, newPrompt, MAX_PROMPT_LEN);    }/********************************************************************************* shellOrigStdSet - set the shell's default input/output/error file descriptors** This routine is called to change the shell's default standard* input/output/error file descriptor.  Normally, it is used only by the* shell, rlogindTask(), and telnetdTask().  Values for <which> can be* STD_IN, STD_OUT, or STD_ERR, as defined in vxWorks.h.  Values for <fd> can* be the file descriptor for any file or device.** RETURNS: N/A*/void shellOrigStdSet    (    int which,  /* STD_IN, STD_OUT, STD_ERR */    int fd      /* fd to be default */    )    {    origFd [which] = fd;    }/********************************************************************************* shellLock - lock access to the shell** This routine locks or unlocks access to the shell.  When locked, cooperating* tasks, such as telnetdTask() and rlogindTask(), will not take the shell.** RETURNS:* TRUE if <request> is "lock" and the routine successfully locks the shell,* otherwise FALSE.  TRUE if <request> is "unlock" and the routine* successfully unlocks the shell, otherwise FALSE.** SEE ALSO:* .pG "Target Shell"*/BOOL shellLock    (    BOOL request        /* TRUE = lock, FALSE = unlock */    )    {    if (request == shellLocked)	return (FALSE);    shellLocked = request;    return (TRUE);    }/********************************************************************************** shellLogin - login using the user-supplied login routine** RETURNS: OK or ERROR** NOMANUAL*/STATUS shellLogin    (    int fd   /* i/o file descriptor passed from telnetd */    )    {    if (loginRtn != NULL)        {        /*          * The standard i/o of this task context will now be that of the          * specified descriptor.         */        ioTaskStdSet (0, STD_IN, fd);        ioTaskStdSet (0, STD_OUT, fd);        /* Call the user-installed login routine */        if ((*loginRtn)(loginRtnVar) == ERROR)            {	    return (ERROR);            }        printf("\n\n"); /* user already logged in at this point */	return (OK);        }	/* if (loginRtn != NULL) */	return (OK); /* No login routine provided */    }	/********************************************************************************* shellLoginInstall - login hook for network login routine** RETURNS: N/A.** NOMANUAL*/void shellLoginInstall    (    FUNCPTR logRtn,    int logRtnVar    )    {    loginRtn    = logRtn;    loginRtnVar = logRtnVar;    }/********************************************************************************* shellLogoutInstall - logout hook for telnetdTask and rlogindTask** RETURNS: N/A.** NOMANUAL*/void shellLogoutInstall    (    FUNCPTR logRtn,    int logVar    )    {    logoutRtn = logRtn;    logoutVar = logVar;    }/********************************************************************************* shellLogout - log out of the shell** This routine logs out of the VxWorks shell.  If a remote login is active* (via `rlogin' or `telnet'), it is stopped, and standard I/O is restored* to the console.** SEE ALSO: rlogindTask(), telnetdTask(), logout()** RETURNS: N/A** NOMANUAL*/void shellLogout (void)    {    /* Restore original user and password information, saved by shell().     * Reference network indirectly for scalability.     */    shellLock (FALSE);    if (_func_remCurIdSet != NULL)			(* _func_remCurIdSet) (originalUser, originalPasswd);      if (logoutRtn != NULL)	(*logoutRtn) (logoutVar);    }/********************************************************************************* shellIsRemoteConnectedSet - notify shell of remote connection/disconnection** This routine allows a remote session like rlogin or telnet to notify* the shell of a successful remote connection/disconnection.** TRUE = connected to remote session and FALSE = disconnected from remote* session.** RETURNS: N/A** NOMANUAL*/void shellIsRemoteConnectedSet    (    BOOL remoteConnection    /* TRUE = connected, FALSE = disconnected */    )    {    shellIsRemoteConnected = remoteConnection;     }/********************************************************************************* shellIsRemoteConnectedGet - get remote connection status of shell** This routine allows a user to get the remote connection status of the shell.** RETURNS: TRUE if shell is remotely connected or FALSE if the shell is not* remotely connected.** NOMANUAL*/BOOL shellIsRemoteConnectedGet (void)    {    return shellIsRemoteConnected;    }/********************************************************************************* errnoStringGet - get string associated with errno** RETURNS: OK or ERROR.*/LOCAL STATUS errnoStringGet    (    FAST int errnum,     /* status code whose name is to be printed */    char * errnoString    )    {    void *    val;    SYMBOL_ID symId;    /* new symLib api - symbol name lengths are no longer limited */    if ((statSymTbl != NULL) &&	(symFindSymbol (statSymTbl, NULL, (void *)errnum, 			SYM_MASK_NONE, SYM_MASK_NONE, &symId) == OK) &&	(symNameGet (symId, &errnoString) == OK) &&	(symValueGet (symId, &val) == OK) &&	(val == (void *)errnum))	{	return (OK);	}    return (ERROR);    }/********************************************************************************* stringTrimRight - remove trailing white space from a string** RETURNS: void.*/LOCAL void stringTrimRight    (    char *	strToTrim			/* string to trim right */    )    {    register char *	strCursor = NULL;	/* string cursor */    strCursor = strToTrim + strlen(strToTrim) - 1;    while (strCursor > strToTrim)	{	if (isspace ((int)(*strCursor)))	    strCursor--;	else	    break;	}    if (strCursor == strToTrim)	{	if (isspace ((int)(*strCursor)))   /* whole string is white space */	    {	    *strCursor = EOS;	    return;	    }	}    /* Normal return, non-empty string */    *(strCursor+1) = EOS;    return;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -