[PATCH]use “getopt” function instead of manually parsing in boot_linux.c
在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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
~~~~~~ no TAB ?
no need to patch getopt in this case, pls just fix command line parsing in boot_linux.c instead.
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))
{