pubdir.c

来自「MPC5200 BSP 支持ATA,USB, I2C,扩展网口」· C语言 代码 · 共 2,219 行 · 第 1/5 页

C
2,219
字号
    g_openTimes++;
    return fId;
}
int _mkdir (char *dirName)
{
    int fd;

    if (ERROR == (fd = open (dirName, O_RDWR | O_CREAT, FSTAT_DIR | DEFAULT_DIR_PERM)))
    {
        return (ERROR);
    }

    return (close (fd));
}
#define DIR_EXIST       1
#define DIR_NOTEXIST    2
#define DIR_CREATERR    3

/*
 * check if a specified dir exist, if not, create it
 * 有适当的打印信息输出
 */
unsigned long _checkDir(unsigned char* v_pcDir)
{
    DIR     *pDir;
    ULONG   retVal = DIR_EXIST;

    if (ISNULL (pDir = opendir (v_pcDir)))  /* exist? */
    {
        retVal = DIR_NOTEXIST;
        if (_mkdir(v_pcDir) == ERROR)
        {
            retVal = DIR_CREATERR;
        }
        else
        {
        }
    }
    else
        closedir(pDir);
    
    return (retVal);
}

int my_close (int fd)
{
    g_closeTimes++;
    return close(fd);
}

void Prompt(char *string, int type, void *parm_ptr)
{
    char line[80]; /* 存放用户输入的内容 */
    char *line_ptr, *seg_ptr, *pLine;
    UCHAR *ptr;
    ULONG ip_byte[4];
    int i, iValue;

    printf("%s",string);
    switch (type)
    {
        case PROMPT_STRING:
            printf(" [%s] ", (UCHAR *)parm_ptr); /* display the origin value */
        case 11:    /* PROMPT_STRING_NO_ORI_VALUE: */
            fioRdString(STD_IN, line, sizeof(line));
            /* take blanks off end of line */
            pLine = line + strlen (line) - 1;       /* point at last char */
            while ((pLine >= line) && (*pLine == ' '))
            {
                *pLine = EOS;
                pLine--;
            }
            pLine = line;
            skipSpace (&pLine);

            if (strlen(pLine))
            {
                line_ptr = pLine;
                ptr = (UCHAR *)parm_ptr;
                while (0 != (*ptr++ = *line_ptr++));
            }
            break;

        case PROMPT_FLAG:/* only from 0 to 255 as unsigned char */
              printf(" [%d] ", *(unsigned char *)parm_ptr); /* display the origin value */	
              fioRdString(STD_IN, line, sizeof(line));
              /* take blanks off end of line */
             pLine = line + strlen (line) - 1;       /* point at last char */
             while ((pLine >= line) && (*pLine == ' '))
             {
                 *pLine = EOS;
                 pLine--;
             }
             pLine = line;
             skipSpace (&pLine);

             /*printf( "debug value is %s\n", pLine );*/
             if (strlen(pLine) ) /* press enter directly, what happen? */
            {
                /* printf("you provided an UCHAR.\n"); */
                line_ptr = pLine;
                iValue = atoi(line_ptr);
               if ((iValue > 255) || (iValue < 0))
               {
                    printf("Invalid UCHAR\n");
                    return;
               }
                *((unsigned char *)parm_ptr) = (unsigned char)iValue;
            }
             break;
             
        case PROMPT_CHAR:
            printf(" [%c] ", *(char *)parm_ptr); /* display the origin value */
            fioRdString(STD_IN, line, sizeof(line));
            /* take blanks off end of line */
            pLine = line + strlen (line) - 1;       /* point at last char */
            while ((pLine >= line) && (*pLine == ' '))
            {
                *pLine = EOS;
                pLine--;
            }
            pLine = line;
            skipSpace (&pLine);

            if (strlen(pLine))
            {
                line_ptr = pLine;
                ptr = (UCHAR *)parm_ptr;
                *ptr = *line_ptr; /* 只取第一个字符*/ /* while (*ptr++ = *line_ptr++); */
            }
            break;

        case PROMPT_IP:
            printf(" [%ld.%ld.%ld.%ld] ", (*((ULONG *)parm_ptr) >> 24) & 0xFF,
             (*((ULONG *)parm_ptr) >> 16) & 0xFF,
             (*((ULONG *)parm_ptr) >> 8)  & 0xFF,
             *((ULONG *)parm_ptr) & 0xFF); /* display the origin value */
            fioRdString(STD_IN, line, sizeof (line)); /* if (read_string(line)) */ /*press enter directly, rv is 0*/
            /* take blanks off end of line */
            pLine = line + strlen (line) - 1;       /* point at last char */
            while ((pLine >= line) && (*pLine == ' '))
            {
                *pLine = EOS;
                pLine--;
            }
            pLine = line;
            skipSpace (&pLine);

            if (strlen(pLine) > 0) /* press enter directly, what happen? */
            {
                /* printf("you provided an IP.\n"); */
                line_ptr = pLine;
                for (i = 3; i >= 0; i--)
                {
                    seg_ptr = line_ptr;
                    while ((*line_ptr) && (*line_ptr != '.'))
                        line_ptr++;
                    if ((*line_ptr == '.') && (line_ptr[1]))
                       *line_ptr++ = 0;
                    ip_byte[i] = atoi(seg_ptr);
                    if (ip_byte[i] > 255)
                    {
                        printf("Invalid IP address\n");
                        return;
                    }
                }
                *((ULONG *)parm_ptr) = (ip_byte[3] << 24)
                                      + (ip_byte[2] << 16)
                                      + (ip_byte[1] << 8)
                                      + ip_byte[0];
            }
            break;
        default:
            printf ("unknown prompt type!\n");
            break;
    } /* switch */
}
/*
 * 改变一个指针的位置
 */
 #ifdef __VER
LOCAL void skipSpace(char **v_strptr)
{
    while (isspace ((UINT) ** v_strptr))
    ++ * v_strptr;
}
/*
 * 把我们关于IP, SUBNETMASK的修改也更新到DEFAULT_BOOT_LINE中
 * 参数:
 *   v_pcBootLine, DEFAULT_BOOT_LINE地址
 */
 #endif
void buildBootLine( char *v_pcBootLine, int flag_bootconfig)
{
    unsigned long ulIp, ulSubnetMask;
    char *p = NULL;

    if (v_pcBootLine == NULL)
        return;

    InitBootCfgPara ((char *)&s_structCfgPara); /* where? */

    ulIp = getIp();
    if (flag_bootconfig || 0xFFFFFFFF == ulIp)
    {
        ulIp = 0x02020200 + 0x80;
        setIp (ulIp);
    }

    ulSubnetMask = getSubnetMask();
    if (flag_bootconfig || 0xFFFFFFFF == ulSubnetMask)
    {
	       ulSubnetMask = 0xff000000; /* 255.0.0.0 */
        setSubnetMask (ulSubnetMask);
    }
    saveBootParaToFlash ();
    /* printf ("ftp user: %s\n", s_structCfgPara.FtpUserName); */
    
    p = strchr (DEFAULT_BOOT_LINE, ':');
    if (NULL == p)
    {
        sprintf(v_pcBootLine,"cpm(0,0)target:vxWorks e=%ld.%ld.%ld.%ld:%08lx",
              (ulIp >> 24) & 0xFF,
              (ulIp >> 16) & 0xFF,
              (ulIp >> 8)  & 0xFF,
              ulIp & 0xFF,
              ulSubnetMask
              );
    }
    else
    {
        int len;
        len = (int)p - (int)DEFAULT_BOOT_LINE;
        strncpy (v_pcBootLine, DEFAULT_BOOT_LINE, len);
        sprintf(v_pcBootLine + len,":vxWorks e=%ld.%ld.%ld.%ld:%08lx",
              (ulIp >> 24) & 0xFF,
              (ulIp >> 16) & 0xFF,
              (ulIp >> 8)  & 0xFF,
              ulIp & 0xFF,
              ulSubnetMask
              );
    }

    printf( "bootline =%s\n", v_pcBootLine );
    return;
}
unsigned long getIp()
{
    return s_structCfgPara.OutBandIpAddr;
}
void setIp(ULONG newip)
{
    s_structCfgPara.OutBandIpAddr = newip;
    ReCalcCheckSum(&s_structCfgPara);
}
void setSubnetMask(ULONG newmask)
{
    s_structCfgPara.OutBandSubnetMask = newmask;
    ReCalcCheckSum(&s_structCfgPara);
}
unsigned long getSubnetMask()
{
    return s_structCfgPara.OutBandSubnetMask;
}
void ReCalcCheckSum( CFG_PARA *pCfgPara)
{
    pCfgPara->csum = calcCheckSum((void *)pCfgPara);
}
/* -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> */
/* 访问配置参数相关 */
ULONG calcCheckSum( void *pCfgPara)
{
    ULONG           csum = 0;
    unsigned char   *pTemp;
    int             i;
    int             iValidParamLen;

    pTemp = (unsigned char*)pCfgPara;
    iValidParamLen = (int)&((CFG_PARA *)0)->reserve;
    for (i = 0; i < iValidParamLen; i++)
        csum += pTemp[i];
    return csum;
}
void saveBootParaToFlash()
{
    ReCalcCheckSum((void *)&s_structCfgPara);
    WriteBootCfgPara((char *)(&s_structCfgPara)); /* 合的时候出了问题 */
}
/*
 * entry point of shell
 */
void cmdLoop( void )
{
#define MAX_COMMAND_LEN 256
    BYTE ucData[MAX_COMMAND_LEN]; /* 存放要执行的命令 */
    char acCwd[50];
    char *boardName = "MPU5200";
    
    init();
	  
	  /* set root dir as current directory */
    if (OK != chdir (OWN_VER_DIR))
    {
        printf ("cd failed\n");
        /* return; */
    }

    /* 将函数表中的命令字符串都改成小写 */
    /* not necessary now, use _stricmp instead of strcmp */

    while (1)
    {
        Print( "\n"); /* 在命令之间空出一行 */
				Print("[%s]", boardName);

        getCwd(acCwd);
        if (EOS != acCwd[0])
            Print ("%s>", acCwd);

        /* 检查批处理缓冲区中是否有内容? */
        if (GET_BAT_CMD == getCommandFromBatBuf(ucData))
        {
            Print("%s\n", ucData);
        }
        else
        {
            BYTE    tmp_buf[MAX_COMMAND_LEN];
            int     i, index;
            int     cmd_len;

            gets(ucData);
            /* 2005.2.19 过滤到不可显示字符 */
            cmd_len = strlen (ucData);
            memcpy (tmp_buf, ucData, MAX_COMMAND_LEN);
            index = 0;
            for (i = 0; i < cmd_len; i++)
            {
                if (isprint (ucData[i]))
                    tmp_buf[index++] = ucData[i];
            }
            tmp_buf[index] = '\0';
            memcpy (ucData, tmp_buf, MAX_COMMAND_LEN);
        }
        execCmd( ucData );
    }
}
static
void init()
{
    /* open and close switches */
    TURN_ALL_SWITCH_OFF;
    checkAllDirs ();
    Print ("bootShell made in %s %s\n\n", __DATE__, __TIME__); /* 这也可以用来检验burn是否成功完成 */
    switchShell ();
}
/*
 * 4.0.0/ or 4.0.0/dirname注意最后是否有/
 * 由于vxworks没有函数get_fn,给该函数的实现带来了困难
 * sys\stat.h中struct stat, st_ino
 * getcwd()是vxworks的系统调用
 */
static
void getCwd(char *pcpwd)
{
    char cwd[MAX_PATH_NAME_LEN];

    ioDefPathGet(cwd);
    if (ISNULL(cwd)) /* no mentioned in manual */
    {
        printf ("buffer is too small\n");
        pcpwd[0] = '\0';
        return;
    }
    else
    {
        strcpy(pcpwd, cwd);
        return;
    }
}
/*
 * getCommandFromBatBuf
 * 从批处理命令的缓冲池中取出要执行的命令
 * 1, get something, -1, get nothing
 * output: pccmd存放取回的命令
 */
static
int getCommandFromBatBuf(char *pccmd)
{
    char *p, *q;
    int lineLength;

    while (1)
    {
        p = &s_acBatBuf[0];
        /* seek line head */
        while ('\0' != *p)
        {
            if ((0x20 != *p) &&
                (0x09 != *p) &&
                (0x0d != *p) &&
                (0x0a != *p))
                break;
            p++;
        }
        if ('\0' == *p) /* no more command in bat buffer */
        {
            return NO_BAT_CMD;
        }

        /* seek line end */
        q = p;
        while (('\0' != *q) &&
            (0x0d != *q) &&
            (0x0a != *q))
        {
            q++;
        }

        /* assert(q - p > 1); */
        lineLength = q - p;
        if (lineLength > 3) /* line length */
        {
            /* is it commented */
            if (0 == strncmp("rem", p, 3) || 0 == strncmp("REM", p, 3))
            {
                memset(p, 0x20, lineLength);
                continue;
            }
        }
        strncpy(pccmd, p, lineLength);
        pccmd[lineLength] = '\0'; /* notice */
        memset(p, 0x20, lineLength);
        return GET_BAT_CMD;
    } /* while */
}
/*
 * Execute command, note that some commands have parameters
 */
static
void execCmd( BYTE *strCmd )
{
    CMD_FUNC_PTR funCmdEntry;
    DWORD flag_HaveOnlySpace;
    char *pCursor; /* 用来辅助分析输入的命令串 */

⌨️ 快捷键说明

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