getopt.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* =============================================================================
  2. * PROGRAM: ularn
  3. * FILENAME: getopt.h
  4. *
  5. * DESCRIPTION:
  6. * Command line options processing functions.
  7. *
  8. * =============================================================================
  9. * EXPORTED VARIABLES
  10. *
  11. * optarg - This is set to the argument associated with any option found by
  12. * getopt that takes an argument.
  13. *
  14. * optind - Index in argv of the next element to be scanned.
  15. *
  16. * opterr - Caller sets to 0 to inhibit error status messages.
  17. *
  18. * =============================================================================
  19. * EXPORTED FUNCTIONS
  20. *
  21. * getopt - Arguments processing function.
  22. *
  23. * =============================================================================
  24. */
  25. #ifndef __GETOPT_H
  26. #define __GETOPT_H
  27. /*
  28. * For communication from `getopt' to the caller.
  29. * When `getopt' finds an option that takes an argument,
  30. * the argument value is returned here.
  31. * Also, when `ordering' is RETURN_IN_ORDER,
  32. * each non-option ARGV-element is returned here.
  33. */
  34. extern char *optarg;
  35. /*
  36. * Index in ARGV of the next element to be scanned.
  37. * This is used for communication to and from the caller
  38. * and for communication between successive calls to `getopt'.
  39. *
  40. * On entry to `getopt', zero means this is the first call; initialize.
  41. *
  42. * When `getopt' returns EOF, this is the index of the first of the
  43. * non-option elements that the caller should itself scan.
  44. *
  45. * Otherwise, `optind' communicates from one call to the next
  46. * how much of ARGV has been scanned so far.
  47. */
  48. extern int optind;
  49. /*
  50. * Callers store zero here to inhibit the error message
  51. * for unrecognized options.
  52. */
  53. extern int opterr;
  54. /* =============================================================================
  55. * FUNCTION: getopt
  56. *
  57. * DESCRIPTION:
  58. * Scan elements of ARGV (whose length is ARGC) for option characters
  59. * given in OPTSTRING.
  60. *
  61. * If an element of ARGV starts with '-', and is not exactly "-" or "--",
  62. * then it is an option element. The characters of this element
  63. * (aside from the initial '-') are option characters. If `getopt'
  64. * is called repeatedly, it returns successively each of the option characters
  65. * from each of the option elements.
  66. *
  67. * If `getopt' finds another option character, it returns that character,
  68. * updating `optind' and `nextchar' so that the next call to `getopt' can
  69. * resume the scan with the following option character or ARGV-element.
  70. *
  71. * If there are no more option characters, `getopt' returns `EOF'.
  72. * Then `optind' is the index in ARGV of the first ARGV-element
  73. * that is not an option. (The ARGV-elements have been permuted
  74. * so that those that are not options now come last.)
  75. *
  76. * OPTSTRING is a string containing the legitimate option characters.
  77. * If an option character is seen that is not listed in OPTSTRING,
  78. * return '?' after printing an error message. If you set `opterr' to
  79. * zero, the error message is suppressed but we still return '?'.
  80. *
  81. * If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  82. * so the following text in the same ARGV-element, or the text of the following
  83. * ARGV-element, is returned in `optarg'. Two colons mean an option that
  84. * wants an optional arg; if there is text in the current ARGV-element,
  85. * it is returned in `optarg', otherwise `optarg' is set to zero.
  86. *
  87. * If OPTSTRING starts with `-' or `+', it requests different methods of
  88. * handling the non-option ARGV-elements.
  89. * See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  90. *
  91. * Long-named options begin with `+' instead of `-'.
  92. * Their names may be abbreviated as long as the abbreviation is unique
  93. * or is an exact match for some defined option. If they have an
  94. * argument, it follows the option name in the same ARGV-element, separated
  95. * from the option name by a `=', or else the in next ARGV-element.
  96. * When `getopt' finds a long-named option, it returns 0 if that option's
  97. * `flag' field is nonzero, the value of the option's `val' field
  98. * otherwise.
  99. *
  100. * PARAMETERS:
  101. *
  102. * argc : The argument count
  103. *
  104. * argv : The arguments array
  105. *
  106. * optstring : The options characters allowed.
  107. *
  108. * RETURN VALUE:
  109. *
  110. * See description.
  111. */
  112. int ugetopt(int argc, char **argv, const char *optstring);
  113. #endif