From 940ad0838da7d5bc2622606a0b42225683494f11 Mon Sep 17 00:00:00 2001 From: patacongo Date: Wed, 23 Feb 2011 03:17:29 +0000 Subject: Back out last change; correct fix is in NSH git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3311 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/examples/nsh/nsh_fscmds.c | 64 +++++++++++++++++++++++++++++-------- nuttx/examples/nsh/nsh_netcmds.c | 69 ++++++++++++++++++++++++++++++---------- nuttx/lib/lib_getopt.c | 65 ++++++++++++++++--------------------- 3 files changed, 130 insertions(+), 68 deletions(-) (limited to 'nuttx') diff --git a/nuttx/examples/nsh/nsh_fscmds.c b/nuttx/examples/nsh/nsh_fscmds.c index f61b084e2..b5b118791 100644 --- a/nuttx/examples/nsh/nsh_fscmds.c +++ b/nuttx/examples/nsh/nsh_fscmds.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/nsh/nsh_fscmds.c * - * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -663,6 +663,7 @@ int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) bool teardown = false; bool readonly = false; off_t offset = 0; + bool badarg = false; int ret = ERROR; int option; @@ -695,10 +696,18 @@ int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) case '?': default: nsh_output(vtbl, g_fmtarginvalid, argv[0]); - return ERROR; + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* If this is not a tear down operation, then additional command line * parameters are required. */ @@ -783,6 +792,7 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) const char *relpath; unsigned int lsflags = 0; char *fullpath; + bool badarg = false; int ret; /* Get the ls options */ @@ -807,10 +817,18 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) case '?': default: nsh_output(vtbl, g_fmtarginvalid, argv[0]); - return ERROR; + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* There may be one argument after the options */ if (optind + 1 < argc) @@ -943,6 +961,7 @@ int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) const char *fmt; uint8_t *buffer; uint32_t nsectors; + bool badarg = false; int sectsize = 512; int minor = 0; int ret; @@ -958,8 +977,8 @@ int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) minor = atoi(optarg); if (minor < 0 || minor > 255) { - fmt = g_fmtargrange; - goto errout_with_fmt; + nsh_output(vtbl, g_fmtargrange, argv[0]); + badarg = true; } break; @@ -967,22 +986,31 @@ int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) sectsize = atoi(optarg); if (minor < 0 || minor > 16384) { - fmt = g_fmtargrange; - goto errout_with_fmt; + nsh_output(vtbl, g_fmtargrange, argv[0]); + badarg = true; } break; case ':': - fmt = g_fmtargrequired; - goto errout_with_fmt; + nsh_output(vtbl, g_fmtargrequired, argv[0]); + badarg = true; + break; case '?': default: - fmt = g_fmtarginvalid; - goto errout_with_fmt; + nsh_output(vtbl, g_fmtarginvalid, argv[0]); + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* There should be exactly on parameter left on the command-line */ if (optind == argc-1) @@ -1043,6 +1071,7 @@ int cmd_mount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) char *source; char *target; char *filesystem = 0; + bool badarg = false; int ret; /* Get the mount options */ @@ -1058,15 +1087,24 @@ int cmd_mount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) case ':': nsh_output(vtbl, g_fmtargrequired, argv[0]); - return ERROR; + badarg = true; + break; case '?': default: nsh_output(vtbl, g_fmtarginvalid, argv[0]); - return ERROR; + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* There are two required arguments after the options */ if (optind + 2 < argc) diff --git a/nuttx/examples/nsh/nsh_netcmds.c b/nuttx/examples/nsh/nsh_netcmds.c index e77fd7604..77caeea82 100644 --- a/nuttx/examples/nsh/nsh_netcmds.c +++ b/nuttx/examples/nsh/nsh_netcmds.c @@ -1,7 +1,7 @@ /**************************************************************************** * examples/nsh/nsh_netcmds.c * - * Copyright (C) 2007-2010 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2011 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -281,6 +281,7 @@ int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv, struct tftpc_args_s *args) { FAR const char *fmt = g_fmtarginvalid; + bool badarg = false; int option; /* Get the ping options */ @@ -306,21 +307,30 @@ int tftpc_parseargs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv, if (!uiplib_ipaddrconv(optarg, (FAR unsigned char*)&args->ipaddr)) { nsh_output(vtbl, g_fmtarginvalid, argv[0]); - goto errout; + badarg = true; } break; case ':': - fmt = g_fmtargrequired; - goto errout; + nsh_output(vtbl, g_fmtargrequired, argv[0]); + badarg = true; + break; case '?': default: - fmt = g_fmtarginvalid; - goto errout; + nsh_output(vtbl, g_fmtarginvalid, argv[0]); + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* There should be exactly on parameter left on the command-line */ if (optind == argc-1) @@ -478,6 +488,7 @@ int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) uint32_t next; uint32_t dsec = 10; uint16_t id; + bool badarg = false; int count = 10; int option; int seqno; @@ -496,8 +507,8 @@ int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) count = atoi(optarg); if (count < 1 || count > 10000) { - fmt = g_fmtargrange; - goto errout; + nsh_output(vtbl, g_fmtargrange, argv[0]); + badarg = true; } break; @@ -505,21 +516,35 @@ int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) tmp = atoi(optarg); if (tmp < 1 || tmp >= 4294) { - fmt = g_fmtargrange; - goto errout; + nsh_output(vtbl, g_fmtargrange, argv[0]); + badarg = true; + } + else + { + dsec = 10 * tmp; } - dsec = 10 * tmp; break; case ':': - fmt = g_fmtargrequired; + nsh_output(vtbl, g_fmtargrequired, argv[0]); + badarg = true; + break; case '?': default: - goto errout; + nsh_output(vtbl, g_fmtarginvalid, argv[0]); + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* There should be exactly on parameter left on the command-line */ if (optind == argc-1) @@ -666,6 +691,7 @@ int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) char *fullpath = NULL; char *url; const char *fmt; + bool badarg = false; int option; int fd = -1; int ret; @@ -681,16 +707,25 @@ int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) break; case ':': - fmt = g_fmtargrequired; - goto errout; + nsh_output(vtbl, g_fmtargrequired, argv[0]); + badarg = true; + break; case '?': default: - fmt = g_fmtarginvalid; - goto errout; + nsh_output(vtbl, g_fmtarginvalid, argv[0]); + badarg = true; + break; } } + /* If a bad argument was encountered, then return without processing the command */ + + if (badarg) + { + return ERROR; + } + /* There should be exactly on parameter left on the command-line */ if (optind == argc-1) diff --git a/nuttx/lib/lib_getopt.c b/nuttx/lib/lib_getopt.c index c75d1d7f5..31ba31f66 100644 --- a/nuttx/lib/lib_getopt.c +++ b/nuttx/lib/lib_getopt.c @@ -1,7 +1,7 @@ /**************************************************************************** * lib/lib_getopt.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -85,7 +85,7 @@ static bool g_binitialized = false; * If there are no more option characters, getopt() returns -1. Then optind * is the index in argv of the first argv-element that is not an option. * - * The 'optstring' argument is a string containing the legitimate option + * The 'optstring argument is a string containing the legitimate option * characters. If such a character is followed by a colon, this indicates * that the option requires an argument. If an argument is required for an * option so getopt() places a pointer to the following text in the same @@ -104,19 +104,6 @@ static bool g_binitialized = false; * first character in optstring: if it is ':', then ':' is returned; * otherwise '?' is returned. * - * Assumptions: - * - getopt() uses global varialbles and, hence, can only be used on a single - * thread. - * - This version of getopt() implements a state machine with certain, strict - * rules. If these rules are not obeyed, the state machine will get out of - * sync and incorrect results will occur. The rules: - * 1. getopt() must be called repeatedly until a terminating value is - * returned. Terminating values include: {-1, ?, : }. - * 2. After a terminating value is returned, getopt() resets the global - * data and is ready for the next command line. - * 3. If the program chooses to violate rule #1, it may do so if it sets - * optind == -1. - * ****************************************************************************/ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) @@ -175,6 +162,9 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* There are no more arguments, we are finished */ g_binitialized = false; + + /* Return -1 with optind == all of the arguments */ + return ERROR; } @@ -186,38 +176,38 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) { /* The argument does not start with '-', we are finished */ + g_optptr = NULL; g_binitialized = false; + + /* Return the -1 with optind set to the non-option argument */ + return ERROR; } /* Skip over the '-' */ g_optptr++; + } - /* Special case handling of "-" */ + /* Special case handling of "-" and "-:" */ - if (!*g_optptr) - { - optopt = '\0'; - g_binitialized = false; - return '?'; - } - } + if (!*g_optptr) + { + optopt = '\0'; /* We'll fix up g_optptr the next time we are called */ + return '?'; + } - /* Handle the case of ":" or '?' in the option list. We need to pick - * these off so that the return values cannot be confused with errors. - */ + /* Handle the case of "-:" */ - if (*g_optptr == ':' || *g_optptr == '?') + if (*g_optptr == ':') { - optopt = *g_optptr; - g_binitialized = false; + optopt = ':'; + g_optptr++; return '?'; } - /* g_optptr now points at the next option and it is not something crazy - * (like NULL or ? or :). Check if the option is in the list of valid - * options. + /* g_optptr now points at the next option and it is not something crazy. + * check if the option is in the list of valid options. */ optchar = strchr(optstring, *g_optptr); @@ -226,7 +216,7 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* No this character is not in the list of valid options */ optopt = *g_optptr; - g_binitialized = false; + g_optptr++; return '?'; } @@ -242,9 +232,7 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) return *optchar; } - /* Yes... It has a required argument. Is the required argument after - * the command in this same argument? - */ + /* Yes. Is the required argument after the command in this same argument? */ if (g_optptr[1] != '\0') { @@ -270,11 +258,12 @@ int getopt(int argc, FAR char *const argv[], FAR const char *optstring) /* No argument was supplied */ + optarg = NULL; optopt = *optchar; - g_binitialized = false; + optind++; return noarg_ret; } - g_binitialized = false; + optind = 1; return ERROR; } -- cgit v1.2.3