getopt_ext.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * getopt_ext.h - long option names
  14. *
  15. *
  16. *
  17. */
  18. #ifndef _GETOPT_EXT_H
  19. #define _GETOPT_EXT_H
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifdef LINUX
  24. #include <getopt.h>
  25. #endif
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /*
  30. * getopt_ext() is a rudimentary extension to getopt() to facilitate
  31. * handling of long (wordier) option names.
  32. *
  33. * A long option is indicated by placing a "--" in front of the option
  34. * name.
  35. *
  36. * Like getopt(), getopt_ext() also returns a single letter (actually an int)
  37. * when an option is recognized. Therefore, the loop for processing long
  38. * options and single letter options can be combined (see example in
  39. * slapd/main.c)
  40. *
  41. * getopt_ext() first processes all the long options it can find. Currently,
  42. * it does a "strcmp" to check for the validity of the option name (i.e.,
  43. * the option name has to match exactly).
  44. *
  45. * Once all the long options are handled, getopt_ext() uses getopt() to
  46. * process the remaining options.
  47. *
  48. * getopt_ext() rearranges "argv" when it finds long options that it
  49. * recognizes. The recognized options (and their parameters) are pushed
  50. * to the end.
  51. *
  52. * Single letter options are specified similar to getopt()
  53. * Long options are specified using a list of "struct opt_ext". Each long
  54. * option consists of string that identifies the option, a type that specifies
  55. * if the option requires an argument and the single letter returned by
  56. * getopt_ext() when the option is encountered. For example,
  57. * {"verbose",ArgNone,'v'} specifies a long option (--verbose) that requires
  58. * no arguments and for which, getopt_ext() returns a 'v' as the return value.
  59. * {"instancedir",ArgRequired,'D'} specifies a long option (--instancedir dir)
  60. * that requires an argument.
  61. *
  62. *
  63. */
  64. extern char *optarg_ext;
  65. extern int optind_ext;
  66. extern int optopt_ext;
  67. extern int opterr_ext;
  68. extern int optind_last;
  69. extern int optind, opterr, optopt;
  70. extern char *optarg;
  71. typedef enum {
  72. ArgNone,
  73. ArgRequired,
  74. ArgOptional
  75. } GetOptExtArgType;
  76. struct opt_ext {
  77. const char *o_string;
  78. const GetOptExtArgType o_type;
  79. const char o_return;
  80. };
  81. int getopt_ext(int argc,
  82. char **argv,
  83. const char *optstring,
  84. const struct opt_ext *longOpts,
  85. int *longOptIndex);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif