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

📄 console.c

📁 闻停开发板视频程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//--------------------------------------------------------------------------
// IP Stack Console Demonstration Program
//--------------------------------------------------------------------------
// Console.c
//
// Example router console
//
// Author: Michael A. Denio
// Copyright 1999 by Texas Instruments Inc.
//-------------------------------------------------------------------------
#include <netmain.h>
#include <_stack.h>
#include <_oskern.h>
#include "console.h"

extern char *VerStr;

static void console( HANDLE hCon, PSA pClient );
static char *StrBusy  = "\nConsole is busy\n\n";
static char *StrError = "\nCould not spawn console\n\n";
static char Password[32] = {0};

//-------------------------------------------------------------------------
// Console IO
//
// The following routines form a basic standard IO for console functions
//-------------------------------------------------------------------------
#define         INMAX  32
static char     InBuf[INMAX];
static int      InIdx = 0;
static int      InCnt = 0;
static SOCKET   scon  = INVALID_SOCKET;
static HANDLE   hConsole = 0;

// PPPOE Function is only available when working with a stack build that
// includes PPPOE
#ifdef _INCLUDE_PPPOE_CODE
static void     ConCmdPPPOE( int ntok, char *tok1, char *tok2, char *tok3 );
static HANDLE   hPPPOE   = 0;
#endif

//--------------------------------------------------------------
// ConPrintf()
//
// Formatted print to console output
//--------------------------------------------------------------
int ConPrintf(const char *format, ...)
{
   va_list ap;
   char    buffer[128];
   int     size;

   va_start(ap, format);
   size = vsprintf(buffer, (char *)format, ap);
   va_end(ap);

   send( scon, buffer, size, 0 );
   return( size );
}

//--------------------------------------------------------------
// ConPrintIPN
//
// Quick routine to print out an IPN addr
//--------------------------------------------------------------
void ConPrintIPN( IPN IPAddr )
{
    IPAddr = htonl( IPAddr );
    ConPrintf( "%d.%d.%d.%d",
               (UINT8)((IPAddr>>24)&0xFF), (UINT8)((IPAddr>>16)&0xFF),
               (UINT8)((IPAddr>>8)&0xFF), (UINT8)(IPAddr&0xFF) );
}

//--------------------------------------------------------------
// ConGetCh()
//
// Read a character from console input
//--------------------------------------------------------------
char ConGetCh()
{
    char   c;
    struct timeval timeout;

    // Configure our console timeout to be 5 minutes
    timeout.tv_sec  = 5 * 60;
    timeout.tv_usec = 0;

    while( 1 )
    {
        while( !InCnt )
        {
            fd_set ibits;
            int    cnt;

            FD_ZERO(&ibits);
            FD_SET(scon, &ibits);

            // Wait for io
            cnt = fdSelect( (int)scon, &ibits, 0, 0, &timeout );
            if( cnt <= 0 )
                goto abort_console;

            // Check for input data
            if( FD_ISSET(scon, &ibits) )
            {
                // We have characters to input
                cnt = (int)recv( scon, InBuf, INMAX, 0 );
                if( cnt > 0 )
                {
                    InIdx = 0;
                    InCnt = cnt;
                }
                // If the socket was closed or error, major abort
                if( !cnt || (cnt<0 && fdError()!=EWOULDBLOCK) )
                    goto abort_console;
            }
        }

        InCnt--;
        c = InBuf[InIdx++];

        if( c != '\n' )
            return( c );
    }

abort_console:
    ConsoleClose();

    fdClose( scon );
    TaskExit();

    return(0);
}

//--------------------------------------------------------------
// ConGetString()
//
// Read a string from console input (with various echo options)
//--------------------------------------------------------------
int ConGetString( char *buf, int max, int echo )
{
    int idx=0, eat=0;
    char c;

    while( idx < (max-1) )
    {
        c = ConGetCh();

        // Eat char if we're eating
        if( eat )
        {
            if( eat == 27 && c == 79 )
                eat = 1;
            else
                eat = 0;
            continue;
        }

        // Start eating if this is an extended char
        if( !c )
        {
            eat = 255;
            continue;
        }

        // Start eating if this is an escape code
        if( c == 27 )
        {
            eat = 27;
            continue;
        }

        // Back up on backspace
        if( c == 8 )
        {
            if( idx )
            {
                idx--;
                ConPrintf("%c %c",8,8);
            }
            continue;
        }

        // Return on CR
        if( c == '\r' )
            break;

        buf[idx++] = c;
        if( echo == CGSECHO_INPUT )
           ConPrintf("%c",c);
        else if( echo == CGSECHO_PASSWORD )
           ConPrintf("*");
    }

    buf[idx] = 0;
    return( idx );
}

//--------------------------------------------------------------
// ConGetIP()
//
// Prompt for and read an IP adress from console input
//--------------------------------------------------------------
IPN ConGetIP()
{
    int    haveit = 0;
    char   c,str[32];
    IPN    IPTmp;

    while( !haveit )
    {
        ConPrintf("Enter IP as x.x.x.x : ");
        ConGetString( str, 20, CGSECHO_INPUT );
        IPTmp = inet_addr( str );
        ConPrintf("\nYou Entered ");
        ConPrintIPN( IPTmp );
        ConPrintf("\nIs this correct (y/n)\n");

        do { c=ConGetCh(); }
            while( c != 'y' && c !='Y' && c != 'N' && c != 'n' );

        if( c=='Y' || c=='y' )
            haveit = 1;
    }
    return( IPTmp );
}

//---------------------------------------------------------------------
// ConsoleOpen()
//
// Launch a console connection to the speicified client
//
// Returns local socket, or INVALID_SOCKET on error
//---------------------------------------------------------------------
SOCKET ConsoleOpen( PSA pClient )
{
    HANDLE fd1, fd2;

    // Create the local pipe - abort on error
    if( pipe( &fd1, &fd2 ) != 0 )
        return( INVALID_SOCKET );

    // If an instance is already running, abort
    if( hConsole )
    {
        // If the console is already running, return a quick message and
        // close the pipe.
        send( fd2, StrBusy, strlen(StrBusy), 0 );
        fdClose( fd2 );
    }
    else
    {
        // Create the console thread
        hConsole = TaskCreate( console, "Console", OS_TASKPRINORM, 0x1000,
                               (UINT32)fd2, (UINT32)pClient, 0 );

        // Close the pipe and abort on an error
        if( !hConsole )
        {
            send( fd2, StrError, strlen(StrError), 0 );
            fdClose( fd2 );
        }
    }

    // Return the local fd
    return( fd1 );
}

//---------------------------------------------------------------------
// ConsoleClose()
//
// Close the console task when active
//
//---------------------------------------------------------------------

⌨️ 快捷键说明

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