免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2105 | 回复: 2

[PATCH]use “getopt” function instead of manually parsing in boot_linux.c [复制链接]

论坛徽章:
0
发表于 2009-06-26 14:16 |显示全部楼层
在boot_linux.c中用getopt函数取代其中的手动参数解析。

signed-off-by :  Zhuyifeng         <zhu_wenfeng@163.com>
signed-off-by :  Linking_shen      <linking.shen@gmail.com>
--------------------------------------------------------------------------------------------------------------------------------


Index: app/boot/boot_linux.c
===================================================================
--- app/boot/boot_linux.c        (revision 421)
+++ app/boot/boot_linux.c        (working copy)
@@ -452,7 +452,7 @@

static int main(int argc, char *argv[])
{
-        int   i, ret;
+        int   opt, ret;
        ULONG ulDevNum;
        char szCmdline[DEFAULT_KCMDLINE_LEN];
        BOOL isShowArg = FALSE;
@@ -464,60 +464,53 @@
        struct LinuxParam *pKeParam;
        struct Atag *pTag;
        LINUX_KERNEL_ENTRY pfExecKernel;
+        char *arg;


        GuSysGetPartInfo(&pPartInfo);
        GuSysGetNetConf(&pNetConf);
        GuSysGetLinuxParam(&pKeParam);

-        // fixme
-        for (i = 1; i < argc; i++)
-        {
-                char *pArg = argv;
-                UINT32 dwNfsIp;

-
-                if ('-' != pArg[0] || strlen(pArg) != 2)
+while ((opt = getopt(argc, argv, "t:r:n:f:c:vh", &arg)) != -1)
+{
+        UINT32 dwNfsIp;
+       
+        switch (opt)
                {
-                        BootUsage();
-                        return -EINVAL;
-                }
-
-                switch (pArg[1])
-                {
                case 't':
-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')       
                        {
                                pKeParam->szImgKernel[0] = '\0';
                                break;
                        }

-                        strcpy(pKeParam->szImgKernel, argv[++i]);
+                        strcpy(pKeParam->szImgKernel, arg);

                        break;

                case 'r':
                        pKeParam->dwBootMode = BM_RAMDISK;

-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                        {
                                pKeParam->szImgRd[0] = '\0';
                                break;
                        }

-                        strcpy(pKeParam->szImgRd, argv[++i]);
+                        strcpy(pKeParam->szImgRd, arg);

                        break;

                case 'f':
                        pKeParam->dwBootMode = BM_FLASHDISK;

-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                                break;

-                        if (GuStrToVal(argv[++i], &ulDevNum) < 0)
+                        if (GuStrToVal(arg, &ulDevNum) < 0)
                        {
-                                printf("Invalid partition number (%s)!\n", argv);
+                                printf("Invalid partition number (%s)!\n", arg);
                                break;
                        }

@@ -529,10 +522,10 @@
                case 'n':
                        pKeParam->dwBootMode = BM_NFS;

-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                                break;

-                        pArg = argv[++i];
+                        char *pArg = arg;

                        while (*pArg && *pArg != ':' && *pArg != '/') pArg++;

@@ -543,29 +536,29 @@
                                strcpy(pKeParam->szNfsPath, pArg + 1);                               

                        case '\0':
-                                if (GuStrToIp((BYTE *)&dwNfsIp, argv) >= 0)
-                                        strcpy(pKeParam->szNfsSvr, argv);
+                                if (GuStrToIp((BYTE *)&dwNfsIp, arg) >= 0)
+                                        strcpy(pKeParam->szNfsSvr, arg);
                                else
-                                        printf("wrong ip format! (%s)\n", argv);
+                                        printf("wrong ip format! (%s)\n", arg);

                                break;

                        default:
-                                strcpy(pKeParam->szNfsPath, argv);
+                                strcpy(pKeParam->szNfsPath, arg);
                                break;
                        }

                        break;

                case 'c':
-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                        {
-                                printf("Invalid option: %s", argv);
+                                printf("Invalid option: %s", opt);
                                BootUsage();
                                return -EINVAL;
                        }

-                        strcpy(pKeParam->szConDev, argv[++i]);
+                        strcpy(pKeParam->szConDev, arg);

                        break;

@@ -578,13 +571,11 @@
                        return 0;

                default:
-                        printf("Invalid option: \"%s\"!\n", pArg);
                        BootUsage();
                        return -EINVAL;
                }
-        }
+}

-
        if (argc > 2 || (2 == argc && FALSE == isShowArg))
        {
                GuSysConfStore();
Index: lib/stdlib/getopt.c
===================================================================
--- lib/stdlib/getopt.c        (revision 421)
+++ lib/stdlib/getopt.c        (working copy)
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
+#include <core/errno.h>


static char *optarg;
@@ -79,6 +80,8 @@
                                non_arg_start = non_arg_end;
                       
                        ++optind;
+                        printf("option error, pls check it!\n");
+                        return -EINVAL;
                }
               

@@ -86,7 +89,9 @@
                {
                        //fixme
                        if (non_arg_start)
+                                {
                                optind = non_arg_start;
+                                }

                        return -1;
                }

论坛徽章:
0
发表于 2009-06-26 14:39 |显示全部楼层
+while ((opt = getopt(argc, argv, "t:r:n:f:c:vh", &arg)) != -1)

~~~~~~ no TAB ?




no need to patch getopt in this case, pls just fix command line parsing in boot_linux.c instead.
===================================================================
--- lib/stdlib/getopt.c        (revision 421)
+++ lib/stdlib/getopt.c        (working copy)
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
+#include <core/errno.h>

论坛徽章:
0
发表于 2009-06-26 16:23 |显示全部楼层

[PATCH]use “getopt” function instead of manually parsing in boot_linux.c

tab问题已经更正, getopt函数复原。另附patch
-----------------------------------------------------------------------------------------------------------------------
signed-off-by :  Zhuyifeng         <zhu_wenfeng@163.com>
signed-off-by :  Linking_shen      <linking.shen@gmail.com>
---------------------------------------------------------------------------------------------------------------------------
Index: app/boot/boot_linux.c
===================================================================
--- app/boot/boot_linux.c        (revision 421)
+++ app/boot/boot_linux.c        (working copy)
@@ -452,7 +452,7 @@

static int main(int argc, char *argv[])
{
-        int   i, ret;
+        int   opt, ret, flag;
        ULONG ulDevNum;
        char szCmdline[DEFAULT_KCMDLINE_LEN];
        BOOL isShowArg = FALSE;
@@ -464,60 +464,53 @@
        struct LinuxParam *pKeParam;
        struct Atag *pTag;
        LINUX_KERNEL_ENTRY pfExecKernel;
+        char *arg;


        GuSysGetPartInfo(&pPartInfo);
        GuSysGetNetConf(&pNetConf);
        GuSysGetLinuxParam(&pKeParam);

-        // fixme
-        for (i = 1; i < argc; i++)
+
+        while ((opt = getopt(argc, argv, "t:r:n:f:c:vh", &arg)) != -1)
        {
-                char *pArg = argv;
                UINT32 dwNfsIp;
-
-
-                if ('-' != pArg[0] || strlen(pArg) != 2)
+       
+                switch (opt)
                {
-                        BootUsage();
-                        return -EINVAL;
-                }
-
-                switch (pArg[1])
-                {
                case 't':
-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')       
                        {
                                pKeParam->szImgKernel[0] = '\0';
                                break;
                        }

-                        strcpy(pKeParam->szImgKernel, argv[++i]);
+                        strcpy(pKeParam->szImgKernel, arg);

                        break;

                case 'r':
                        pKeParam->dwBootMode = BM_RAMDISK;

-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                        {
                                pKeParam->szImgRd[0] = '\0';
                                break;
                        }

-                        strcpy(pKeParam->szImgRd, argv[++i]);
+                        strcpy(pKeParam->szImgRd, arg);

                        break;

                case 'f':
                        pKeParam->dwBootMode = BM_FLASHDISK;

-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                                break;

-                        if (GuStrToVal(argv[++i], &ulDevNum) < 0)
+                        if (GuStrToVal(arg, &ulDevNum) < 0)
                        {
-                                printf("Invalid partition number (%s)!\n", argv);
+                                printf("Invalid partition number (%s)!\n", arg);
                                break;
                        }

@@ -529,10 +522,10 @@
                case 'n':
                        pKeParam->dwBootMode = BM_NFS;

-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                                break;

-                        pArg = argv[++i];
+                        char *pArg = arg;

                        while (*pArg && *pArg != ':' && *pArg != '/') pArg++;

@@ -543,29 +536,29 @@
                                strcpy(pKeParam->szNfsPath, pArg + 1);                               

                        case '\0':
-                                if (GuStrToIp((BYTE *)&dwNfsIp, argv) >= 0)
-                                        strcpy(pKeParam->szNfsSvr, argv);
+                                if (GuStrToIp((BYTE *)&dwNfsIp, arg) >= 0)
+                                        strcpy(pKeParam->szNfsSvr, arg);
                                else
-                                        printf("wrong ip format! (%s)\n", argv);
+                                        printf("wrong ip format! (%s)\n", arg);

                                break;

                        default:
-                                strcpy(pKeParam->szNfsPath, argv);
+                                strcpy(pKeParam->szNfsPath, arg);
                                break;
                        }

                        break;

                case 'c':
-                        if (i + 1 == argc || '-' == argv[i + 1][0])
+                        if(arg == NULL || *arg == '-')
                        {
-                                printf("Invalid option: %s", argv);
+                                printf("Invalid option: %s", opt);
                                BootUsage();
                                return -EINVAL;
                        }

-                        strcpy(pKeParam->szConDev, argv[++i]);
+                        strcpy(pKeParam->szConDev, arg);

                        break;

@@ -578,12 +571,16 @@
                        return 0;

                default:
-                        printf("Invalid option: \"%s\"!\n", pArg);
                        BootUsage();
                        return -EINVAL;
                }
        }

+        if ((flag = get_optind()) < argc)
+        {
+                BootUsage();
+                return -EINVAL;
+        }

        if (argc > 2 || (2 == argc && FALSE == isShowArg))
        {
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP