getopt.m4 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # getopt.m4 serial 28
  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. dnl Solaris supports neither optreset nor optind=0, but keeps no state that
  71. dnl needs a reset beyond setting optind=1; detect Solaris by getopt_clip.
  72. if test -z "$gl_replace_getopt"; then
  73. AC_CHECK_DECLS([optreset], [],
  74. [AC_CHECK_DECLS([getopt_clip], [], [],
  75. [[#include <getopt.h>]])
  76. ],
  77. [[#include <getopt.h>]])
  78. fi
  79. dnl mingw's getopt (in libmingwex.a) does weird things when the options
  80. dnl strings starts with '+' and it's not the first call. Some internal state
  81. dnl is left over from earlier calls, and neither setting optind = 0 nor
  82. dnl setting optreset = 1 get rid of this internal state.
  83. dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
  84. dnl POSIX 2008 does not specify leading '+' behavior, but see
  85. dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
  86. dnl the next version of POSIX. For now, we only guarantee leading '+'
  87. dnl behavior with getopt-gnu.
  88. if test -z "$gl_replace_getopt"; then
  89. AC_CACHE_CHECK([whether getopt is POSIX compatible],
  90. [gl_cv_func_getopt_posix],
  91. [
  92. dnl This test fails on mingw and succeeds on all other platforms.
  93. AC_RUN_IFELSE([AC_LANG_SOURCE([[
  94. #include <unistd.h>
  95. #include <stdlib.h>
  96. #include <string.h>
  97. #if !HAVE_DECL_OPTRESET && !HAVE_DECL_GETOPT_CLIP
  98. # define OPTIND_MIN 0
  99. #else
  100. # define OPTIND_MIN 1
  101. #endif
  102. int
  103. main ()
  104. {
  105. {
  106. int argc = 0;
  107. char *argv[10];
  108. int c;
  109. argv[argc++] = "program";
  110. argv[argc++] = "-a";
  111. argv[argc++] = "foo";
  112. argv[argc++] = "bar";
  113. argv[argc] = NULL;
  114. optind = OPTIND_MIN;
  115. opterr = 0;
  116. c = getopt (argc, argv, "ab");
  117. if (!(c == 'a'))
  118. return 1;
  119. c = getopt (argc, argv, "ab");
  120. if (!(c == -1))
  121. return 2;
  122. if (!(optind == 2))
  123. return 3;
  124. }
  125. /* Some internal state exists at this point. */
  126. {
  127. int argc = 0;
  128. char *argv[10];
  129. int c;
  130. argv[argc++] = "program";
  131. argv[argc++] = "donald";
  132. argv[argc++] = "-p";
  133. argv[argc++] = "billy";
  134. argv[argc++] = "duck";
  135. argv[argc++] = "-a";
  136. argv[argc++] = "bar";
  137. argv[argc] = NULL;
  138. optind = OPTIND_MIN;
  139. opterr = 0;
  140. c = getopt (argc, argv, "+abp:q:");
  141. if (!(c == -1))
  142. return 4;
  143. if (!(strcmp (argv[0], "program") == 0))
  144. return 5;
  145. if (!(strcmp (argv[1], "donald") == 0))
  146. return 6;
  147. if (!(strcmp (argv[2], "-p") == 0))
  148. return 7;
  149. if (!(strcmp (argv[3], "billy") == 0))
  150. return 8;
  151. if (!(strcmp (argv[4], "duck") == 0))
  152. return 9;
  153. if (!(strcmp (argv[5], "-a") == 0))
  154. return 10;
  155. if (!(strcmp (argv[6], "bar") == 0))
  156. return 11;
  157. if (!(optind == 1))
  158. return 12;
  159. }
  160. /* Detect MacOS 10.5 bug. */
  161. {
  162. char *argv[3] = { "program", "-ab", NULL };
  163. optind = OPTIND_MIN;
  164. opterr = 0;
  165. if (getopt (2, argv, "ab:") != 'a')
  166. return 13;
  167. if (getopt (2, argv, "ab:") != '?')
  168. return 14;
  169. if (optopt != 'b')
  170. return 15;
  171. if (optind != 2)
  172. return 16;
  173. }
  174. return 0;
  175. }
  176. ]])],
  177. [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
  178. [case "$host_os" in
  179. mingw*) gl_cv_func_getopt_posix="guessing no";;
  180. darwin*) gl_cv_func_getopt_posix="guessing no";;
  181. *) gl_cv_func_getopt_posix="guessing yes";;
  182. esac
  183. ])
  184. ])
  185. case "$gl_cv_func_getopt_posix" in
  186. *no) gl_replace_getopt=yes ;;
  187. esac
  188. fi
  189. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  190. AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
  191. [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
  192. # optstring is necessary for programs like m4 that have POSIX-mandated
  193. # semantics for supporting options interspersed with files.
  194. # Also, since getopt_long is a GNU extension, we require optind=0.
  195. gl_had_POSIXLY_CORRECT=${POSIXLY_CORRECT:+yes}
  196. POSIXLY_CORRECT=1
  197. export POSIXLY_CORRECT
  198. AC_RUN_IFELSE(
  199. [AC_LANG_PROGRAM([[#include <getopt.h>
  200. #include <stddef.h>
  201. #include <string.h>
  202. ]], [[
  203. /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
  204. and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
  205. OSF/1 5.1, Solaris 10. */
  206. {
  207. char *myargv[3];
  208. myargv[0] = "conftest";
  209. myargv[1] = "-+";
  210. myargv[2] = 0;
  211. opterr = 0;
  212. if (getopt (2, myargv, "+a") != '?')
  213. return 1;
  214. }
  215. /* This code succeeds on glibc 2.8, mingw,
  216. and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
  217. IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
  218. {
  219. char *argv[] = { "program", "-p", "foo", "bar", NULL };
  220. optind = 1;
  221. if (getopt (4, argv, "p::") != 'p')
  222. return 2;
  223. if (optarg != NULL)
  224. return 3;
  225. if (getopt (4, argv, "p::") != -1)
  226. return 4;
  227. if (optind != 2)
  228. return 5;
  229. }
  230. /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
  231. {
  232. char *argv[] = { "program", "foo", "-p", NULL };
  233. optind = 0;
  234. if (getopt (3, argv, "-p") != 1)
  235. return 6;
  236. if (getopt (3, argv, "-p") != 'p')
  237. return 7;
  238. }
  239. /* This code fails on glibc 2.11. */
  240. {
  241. char *argv[] = { "program", "-b", "-a", NULL };
  242. optind = opterr = 0;
  243. if (getopt (3, argv, "+:a:b") != 'b')
  244. return 8;
  245. if (getopt (3, argv, "+:a:b") != ':')
  246. return 9;
  247. }
  248. return 0;
  249. ]])],
  250. [gl_cv_func_getopt_gnu=yes],
  251. [gl_cv_func_getopt_gnu=no],
  252. [dnl Cross compiling. Guess based on host and declarations.
  253. case $host_os:$ac_cv_have_decl_optreset in
  254. *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
  255. *:yes) gl_cv_func_getopt_gnu=no;;
  256. *) gl_cv_func_getopt_gnu=yes;;
  257. esac
  258. ])
  259. if test "$gl_had_POSIXLY_CORRECT" != yes; then
  260. AS_UNSET([POSIXLY_CORRECT])
  261. fi
  262. ])
  263. if test "$gl_cv_func_getopt_gnu" = "no"; then
  264. gl_replace_getopt=yes
  265. fi
  266. fi
  267. ])
  268. # emacs' configure.in uses this.
  269. AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
  270. [
  271. GETOPT_H=getopt.h
  272. AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
  273. [Define to rpl_ if the getopt replacement functions and variables
  274. should be used.])
  275. AC_SUBST([GETOPT_H])
  276. ])
  277. # Prerequisites of lib/getopt*.
  278. # emacs' configure.in uses this.
  279. AC_DEFUN([gl_PREREQ_GETOPT],
  280. [
  281. AC_CHECK_DECLS_ONCE([getenv])
  282. ])