getopt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 1987, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
  30. #include "tmux.h"
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. int BSDopterr = 1, /* if error message should be printed */
  35. BSDoptind = 1, /* index into parent argv vector */
  36. BSDoptopt, /* character checked for validity */
  37. BSDoptreset; /* reset getopt */
  38. char *BSDoptarg; /* argument associated with option */
  39. #define BADCH (int)'?'
  40. #define BADARG (int)':'
  41. #define EMSG ""
  42. /*
  43. * getopt --
  44. * Parse argc/argv argument vector.
  45. */
  46. int
  47. BSDgetopt(int nargc, char *const *nargv, const char *ostr)
  48. {
  49. static const char *place = EMSG; /* option letter processing */
  50. char *oli; /* option letter list index */
  51. if (ostr == NULL)
  52. return (-1);
  53. if (BSDoptreset || !*place) { /* update scanning pointer */
  54. BSDoptreset = 0;
  55. if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
  56. place = EMSG;
  57. return (-1);
  58. }
  59. if (place[1] && *++place == '-') { /* found "--" */
  60. if (place[1])
  61. return (BADCH);
  62. ++BSDoptind;
  63. place = EMSG;
  64. return (-1);
  65. }
  66. } /* option letter okay? */
  67. if ((BSDoptopt = (int)*place++) == (int)':' ||
  68. !(oli = strchr(ostr, BSDoptopt))) {
  69. /*
  70. * if the user didn't specify '-' as an option,
  71. * assume it means -1.
  72. */
  73. if (BSDoptopt == (int)'-')
  74. return (-1);
  75. if (!*place)
  76. ++BSDoptind;
  77. if (BSDopterr && *ostr != ':')
  78. (void)fprintf(stderr,
  79. "%s: unknown option -- %c\n", __progname, BSDoptopt);
  80. return (BADCH);
  81. }
  82. if (*++oli != ':') { /* don't need argument */
  83. BSDoptarg = NULL;
  84. if (!*place)
  85. ++BSDoptind;
  86. }
  87. else { /* need an argument */
  88. if (*place) /* no white space */
  89. BSDoptarg = (char *)place;
  90. else if (nargc <= ++BSDoptind) { /* no arg */
  91. place = EMSG;
  92. if (*ostr == ':')
  93. return (BADARG);
  94. if (BSDopterr)
  95. (void)fprintf(stderr,
  96. "%s: option requires an argument -- %c\n",
  97. __progname, BSDoptopt);
  98. return (BADCH);
  99. }
  100. else /* white space */
  101. BSDoptarg = nargv[BSDoptind];
  102. place = EMSG;
  103. ++BSDoptind;
  104. }
  105. return (BSDoptopt); /* dump back option letter */
  106. }