getopt.m4 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # getopt.m4 serial 24
  2. dnl Copyright (C) 2002-2006, 2008-2010 Free Software Foundation, Inc.
  3. dnl This file is free software; the Free Software Foundation
  4. dnl gives unlimited permission to copy and/or distribute it,
  5. dnl with or without modifications, as long as this notice is preserved.
  6. # Request a POSIX compliant getopt function.
  7. AC_DEFUN([gl_FUNC_GETOPT_POSIX],
  8. [
  9. m4_divert_text([DEFAULTS], [gl_getopt_required=POSIX])
  10. AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
  11. gl_GETOPT_IFELSE([
  12. gl_REPLACE_GETOPT
  13. ],
  14. [])
  15. ])
  16. # Request a POSIX compliant getopt function with GNU extensions (such as
  17. # options with optional arguments) and the functions getopt_long,
  18. # getopt_long_only.
  19. AC_DEFUN([gl_FUNC_GETOPT_GNU],
  20. [
  21. m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
  22. AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
  23. ])
  24. # Request the gnulib implementation of the getopt functions unconditionally.
  25. # argp.m4 uses this.
  26. AC_DEFUN([gl_REPLACE_GETOPT],
  27. [
  28. dnl Arrange for getopt.h to be created.
  29. gl_GETOPT_SUBSTITUTE_HEADER
  30. dnl Arrange for unistd.h to include getopt.h.
  31. GNULIB_UNISTD_H_GETOPT=1
  32. dnl Arrange to compile the getopt implementation.
  33. AC_LIBOBJ([getopt])
  34. AC_LIBOBJ([getopt1])
  35. gl_PREREQ_GETOPT
  36. ])
  37. # emacs' configure.in uses this.
  38. AC_DEFUN([gl_GETOPT_IFELSE],
  39. [
  40. AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
  41. AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
  42. ])
  43. # Determine whether to replace the entire getopt facility.
  44. AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
  45. [
  46. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  47. dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
  48. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  49. gl_CHECK_NEXT_HEADERS([getopt.h])
  50. AC_CHECK_HEADERS_ONCE([getopt.h])
  51. if test $ac_cv_header_getopt_h = yes; then
  52. HAVE_GETOPT_H=1
  53. else
  54. HAVE_GETOPT_H=0
  55. fi
  56. AC_SUBST([HAVE_GETOPT_H])
  57. gl_replace_getopt=
  58. dnl Test whether <getopt.h> is available.
  59. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  60. AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
  61. fi
  62. dnl Test whether the function getopt_long is available.
  63. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  64. AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
  65. fi
  66. dnl BSD getopt_long uses an incompatible method to reset option processing.
  67. dnl Existence of the variable, in and of itself, is not a reason to replace
  68. dnl getopt, but knowledge of the variable is needed to determine how to
  69. dnl reset and whether a reset reparses the environment.
  70. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  71. AC_CHECK_DECLS([optreset], [], [],
  72. [[#include <getopt.h>]])
  73. fi
  74. dnl mingw's getopt (in libmingwex.a) does weird things when the options
  75. dnl strings starts with '+' and it's not the first call. Some internal state
  76. dnl is left over from earlier calls, and neither setting optind = 0 nor
  77. dnl setting optreset = 1 get rid of this internal state.
  78. dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
  79. if test -z "$gl_replace_getopt"; then
  80. AC_CACHE_CHECK([whether getopt is POSIX compatible],
  81. [gl_cv_func_getopt_posix],
  82. [
  83. dnl This test fails on mingw and succeeds on all other platforms.
  84. AC_RUN_IFELSE([AC_LANG_SOURCE([[
  85. #include <unistd.h>
  86. #include <stdlib.h>
  87. #include <string.h>
  88. #if !HAVE_DECL_OPTRESET
  89. # define OPTIND_MIN 0
  90. #else
  91. # define OPTIND_MIN 1
  92. #endif
  93. int
  94. main ()
  95. {
  96. {
  97. int argc = 0;
  98. char *argv[10];
  99. int c;
  100. argv[argc++] = "program";
  101. argv[argc++] = "-a";
  102. argv[argc++] = "foo";
  103. argv[argc++] = "bar";
  104. argv[argc] = NULL;
  105. optind = OPTIND_MIN;
  106. opterr = 0;
  107. c = getopt (argc, argv, "ab");
  108. if (!(c == 'a'))
  109. return 1;
  110. c = getopt (argc, argv, "ab");
  111. if (!(c == -1))
  112. return 2;
  113. if (!(optind == 2))
  114. return 3;
  115. }
  116. /* Some internal state exists at this point. */
  117. {
  118. int argc = 0;
  119. char *argv[10];
  120. int c;
  121. argv[argc++] = "program";
  122. argv[argc++] = "donald";
  123. argv[argc++] = "-p";
  124. argv[argc++] = "billy";
  125. argv[argc++] = "duck";
  126. argv[argc++] = "-a";
  127. argv[argc++] = "bar";
  128. argv[argc] = NULL;
  129. optind = OPTIND_MIN;
  130. opterr = 0;
  131. c = getopt (argc, argv, "+abp:q:");
  132. if (!(c == -1))
  133. return 4;
  134. if (!(strcmp (argv[0], "program") == 0))
  135. return 5;
  136. if (!(strcmp (argv[1], "donald") == 0))
  137. return 6;
  138. if (!(strcmp (argv[2], "-p") == 0))
  139. return 7;
  140. if (!(strcmp (argv[3], "billy") == 0))
  141. return 8;
  142. if (!(strcmp (argv[4], "duck") == 0))
  143. return 9;
  144. if (!(strcmp (argv[5], "-a") == 0))
  145. return 10;
  146. if (!(strcmp (argv[6], "bar") == 0))
  147. return 11;
  148. if (!(optind == 1))
  149. return 12;
  150. }
  151. return 0;
  152. }
  153. ]])],
  154. [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
  155. [case "$host_os" in
  156. mingw*) gl_cv_func_getopt_posix="guessing no";;
  157. *) gl_cv_func_getopt_posix="guessing yes";;
  158. esac
  159. ])
  160. ])
  161. case "$gl_cv_func_getopt_posix" in
  162. *no) gl_replace_getopt=yes ;;
  163. esac
  164. fi
  165. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  166. AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
  167. [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
  168. # optstring is necessary for programs like m4 that have POSIX-mandated
  169. # semantics for supporting options interspersed with files.
  170. # Also, since getopt_long is a GNU extension, we require optind=0.
  171. gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
  172. POSIXLY_CORRECT=1
  173. export POSIXLY_CORRECT
  174. AC_RUN_IFELSE(
  175. [AC_LANG_PROGRAM([[#include <getopt.h>
  176. #include <stddef.h>
  177. #include <string.h>
  178. ]], [[
  179. /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
  180. and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
  181. OSF/1 5.1, Solaris 10. */
  182. {
  183. char *myargv[3];
  184. myargv[0] = "conftest";
  185. myargv[1] = "-+";
  186. myargv[2] = 0;
  187. opterr = 0;
  188. if (getopt (2, myargv, "+a") != '?')
  189. return 1;
  190. }
  191. /* This code succeeds on glibc 2.8, mingw,
  192. and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
  193. IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
  194. {
  195. char *argv[] = { "program", "-p", "foo", "bar", NULL };
  196. optind = 1;
  197. if (getopt (4, argv, "p::") != 'p')
  198. return 2;
  199. if (optarg != NULL)
  200. return 3;
  201. if (getopt (4, argv, "p::") != -1)
  202. return 4;
  203. if (optind != 2)
  204. return 5;
  205. }
  206. /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
  207. {
  208. char *argv[] = { "program", "foo", "-p", NULL };
  209. optind = 0;
  210. if (getopt (3, argv, "-p") != 1)
  211. return 6;
  212. if (getopt (3, argv, "-p") != 'p')
  213. return 7;
  214. }
  215. return 0;
  216. ]])],
  217. [gl_cv_func_getopt_gnu=yes],
  218. [gl_cv_func_getopt_gnu=no],
  219. [dnl Cross compiling. Guess based on host and declarations.
  220. case $host_os:$ac_cv_have_decl_optreset in
  221. *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
  222. *:yes) gl_cv_func_getopt_gnu=no;;
  223. *) gl_cv_func_getopt_gnu=yes;;
  224. esac
  225. ])
  226. if test "$gl_had_POSIXLY_CORRECT" != yes; then
  227. AS_UNSET([POSIXLY_CORRECT])
  228. fi
  229. ])
  230. if test "$gl_cv_func_getopt_gnu" = "no"; then
  231. gl_replace_getopt=yes
  232. fi
  233. fi
  234. ])
  235. # emacs' configure.in uses this.
  236. AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
  237. [
  238. GETOPT_H=getopt.h
  239. AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
  240. [Define to rpl_ if the getopt replacement functions and variables
  241. should be used.])
  242. AC_SUBST([GETOPT_H])
  243. ])
  244. # Prerequisites of lib/getopt*.
  245. # emacs' configure.in uses this.
  246. AC_DEFUN([gl_PREREQ_GETOPT],
  247. [
  248. AC_CHECK_DECLS_ONCE([getenv])
  249. ])