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

📄 getopt.c

📁 GNUnet是一个安全的点对点网络框架
💻 C
📖 第 1 页 / 共 3 页
字号:
            }          else if (pfound->has_arg == 1)            {              if (GNoptind < argc)                {                  GNoptarg = argv[GNoptind++];                }              else                {                  if (GNopterr)                    {                      fprintf (stderr,                               _("%s: option `%s' requires an argument\n"),                               argv[0], argv[GNoptind - 1]);                    }                  nextchar += strlen (nextchar);                  GNoptopt = pfound->val;                  return (optstring[0] == ':') ? ':' : '?';                }            }          nextchar += strlen (nextchar);          if (longind != NULL)            *longind = option_index;          if (pfound->flag)            {              *(pfound->flag) = pfound->val;              return 0;            }          return pfound->val;        }      /* Can't find it as a long option.  If this is not getopt_long_only,         or the option starts with '--' or is not a valid short         option, then it's an error.         Otherwise interpret it as a short option.  */      if (!long_only || argv[GNoptind][1] == '-'          || my_index (optstring, *nextchar) == NULL)        {          if (GNopterr)            {              if (argv[GNoptind][1] == '-')                /* --option */                fprintf (stderr, _("%s: unrecognized option `--%s'\n"),                         argv[0], nextchar);              else                /* +option or -option */                fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),                         argv[0], argv[GNoptind][0], nextchar);            }          nextchar = (char *) "";          GNoptind++;          GNoptopt = 0;          return '?';        }    }  /* Look at and handle the next short option-character.  */  {    char c = *nextchar++;    char *temp = my_index (optstring, c);    /* Increment `GNoptind' when we start to process its last character.  */    if (*nextchar == '\0')      ++GNoptind;    if (temp == NULL || c == ':')      {        if (GNopterr)          {            if (posixly_correct)              /* 1003.2 specifies the format of this message.  */              fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);            else              fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);          }        GNoptopt = c;        return '?';      }    /* Convenience. Treat POSIX -W foo same as long option --foo */    if (temp[0] == 'W' && temp[1] == ';')      {        char *nameend;        const struct GNoption *p;        const struct GNoption *pfound = NULL;        int exact = 0;        int ambig = 0;        int indfound = 0;        int option_index;        /* This is an option that requires an argument.  */        if (*nextchar != '\0')          {            GNoptarg = nextchar;            /* If we end this ARGV-element by taking the rest as an arg,               we must advance to the next element now.  */            GNoptind++;          }        else if (GNoptind == argc)          {            if (GNopterr)              {                /* 1003.2 specifies the format of this message.  */                fprintf (stderr, _("%s: option requires an argument -- %c\n"),                         argv[0], c);              }            GNoptopt = c;            if (optstring[0] == ':')              c = ':';            else              c = '?';            return c;          }        else          /* We already incremented `GNoptind' once;             increment it again when taking next ARGV-elt as argument.  */          GNoptarg = argv[GNoptind++];        /* GNoptarg is now the argument, see if it's in the           table of longopts.  */        for (nextchar = nameend = GNoptarg; *nameend && *nameend != '=';             nameend++)          /* Do nothing.  */ ;        /* Test all long options for either exact match           or abbreviated matches.  */        for (p = longopts, option_index = 0; p->name; p++, option_index++)          if (!strncmp (p->name, nextchar, nameend - nextchar))            {              if ((unsigned int) (nameend - nextchar) == strlen (p->name))                {                  /* Exact match found.  */                  pfound = p;                  indfound = option_index;                  exact = 1;                  break;                }              else if (pfound == NULL)                {                  /* First nonexact match found.  */                  pfound = p;                  indfound = option_index;                }              else                /* Second or later nonexact match found.  */                ambig = 1;            }        if (ambig && !exact)          {            if (GNopterr)              fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),                       argv[0], argv[GNoptind]);            nextchar += strlen (nextchar);            GNoptind++;            return '?';          }        if (pfound != NULL)          {            option_index = indfound;            if (*nameend)              {                /* Don't test has_arg with >, because some C compilers don't                   allow it to be used on enums.  */                if (pfound->has_arg)                  GNoptarg = nameend + 1;                else                  {                    if (GNopterr)                      fprintf (stderr, _("\%s: option `-W %s' does not allow an argument\n"), argv[0], pfound->name);                    nextchar += strlen (nextchar);                    return '?';                  }              }            else if (pfound->has_arg == 1)              {                if (GNoptind < argc)                  GNoptarg = argv[GNoptind++];                else                  {                    if (GNopterr)                      fprintf (stderr,                               _("%s: option `%s' requires an argument\n"),                               argv[0], argv[GNoptind - 1]);                    nextchar += strlen (nextchar);                    return optstring[0] == ':' ? ':' : '?';                  }              }            nextchar += strlen (nextchar);            if (longind != NULL)              *longind = option_index;            if (pfound->flag)              {                *(pfound->flag) = pfound->val;                return 0;              }            return pfound->val;          }        nextchar = NULL;        return 'W';             /* Let the application handle it.   */      }    if (temp[1] == ':')      {        if (temp[2] == ':')          {            /* This is an option that accepts an argument optionally.  */            if (*nextchar != '\0')              {                GNoptarg = nextchar;                GNoptind++;              }            else              GNoptarg = NULL;            nextchar = NULL;          }        else          {            /* This is an option that requires an argument.  */            if (*nextchar != '\0')              {                GNoptarg = nextchar;                /* If we end this ARGV-element by taking the rest as an arg,                   we must advance to the next element now.  */                GNoptind++;              }            else if (GNoptind == argc)              {                if (GNopterr)                  {                    /* 1003.2 specifies the format of this message.  */                    fprintf (stderr,                             _("%s: option requires an argument -- %c\n"),                             argv[0], c);                  }                GNoptopt = c;                if (optstring[0] == ':')                  c = ':';                else                  c = '?';              }            else              /* We already incremented `GNoptind' once;                 increment it again when taking next ARGV-elt as argument.  */              GNoptarg = argv[GNoptind++];            nextchar = NULL;          }      }    return c;  }}static intGNgetopt_long (int argc,               char *const *argv,               const char *options,               const struct GNoption *long_options, int *opt_index){  return GN_getopt_internal (argc, argv, options, long_options, opt_index, 0);}/* ******************** now the GNUnet specific modifications... ********************* *//** * Parse the command line. * * @param binaryName name of the binary / application with options * @param ectx for reporting errors * @param cfg for storing/accessing configuration data * @param allOptions defined options and handlers * @param argc number of arguments * @param argv actual arguments * @return index into argv with first non-option *   argument, or -1 on error */intGNUNET_parse_options (const char *binaryOptions,                      struct GNUNET_GE_Context *ectx,                      struct GNUNET_GC_Configuration *cfg,                      const GNUNET_CommandLineOption * allOptions,                      unsigned int argc, char *const *argv){  struct GNoption *long_options;  GNUNET_CommandLineProcessorContext clpc;  int count;  int i;  char *shorts;  int spos;  int cont;  int c;  GNUNET_GE_ASSERT (ectx, argc > 0);  clpc.binaryName = argv[0];  clpc.binaryOptions = binaryOptions;  clpc.allOptions = allOptions;  clpc.argv = argv;  clpc.argc = argc;  clpc.ectx = ectx;  clpc.cfg = cfg;  count = 0;  while (allOptions[count].name != NULL)    count++;  long_options = GNUNET_malloc (sizeof (struct GNoption) * (count + 1));  shorts = GNUNET_malloc (count * 2 + 1);  spos = 0;  for (i = 0; i < count; i++)    {      long_options[i].name = allOptions[i].name;      long_options[i].has_arg = allOptions[i].require_argument;      long_options[i].flag = NULL;      long_options[i].val = allOptions[i].shortName;      shorts[spos++] = allOptions[i].shortName;      if (allOptions[i].require_argument != 0)        shorts[spos++] = ':';    }  long_options[count].name = NULL;  long_options[count].has_arg = 0;  long_options[count].flag = NULL;  long_options[count].val = '\0';  shorts[spos++] = '\0';  cont = GNUNET_OK;  /* main getopt loop */  while (cont == GNUNET_OK)    {      int option_index = 0;      c = GNgetopt_long (argc, argv, shorts, long_options, &option_index);      if (c == GNUNET_SYSERR)        break;                  /* No more flags to process */      for (i = 0; i < count; i++)        {          clpc.currentArgument = GNoptind - 1;          if ((char) c == allOptions[i].shortName)            {              cont = allOptions[i].processor (&clpc,                                              allOptions[i].scls,                                              allOptions[i].name, GNoptarg);              break;            }        }      if (i == count)        {          GNUNET_GE_LOG (ectx,                         GNUNET_GE_INFO | GNUNET_GE_USER |                         GNUNET_GE_IMMEDIATE,                         _("Use --help to get a list of options.\n"));          cont = GNUNET_SYSERR;        }    }  GNUNET_free (shorts);  GNUNET_free (long_options);  if (cont == GNUNET_SYSERR)    return GNUNET_SYSERR;  return GNoptind;}/* end of getopt.c */

⌨️ 快捷键说明

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