getopt.m4 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # getopt.m4 serial 38
  2. dnl Copyright (C) 2002-2006, 2008-2011 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. dnl Other modules can request the gnulib implementation of the getopt
  12. dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
  13. dnl argp.m4 does this.
  14. m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
  15. gl_GETOPT_IFELSE([], [])
  16. REPLACE_GETOPT=1
  17. ], [
  18. REPLACE_GETOPT=0
  19. gl_GETOPT_IFELSE([
  20. REPLACE_GETOPT=1
  21. ],
  22. [])
  23. ])
  24. if test $REPLACE_GETOPT = 1; then
  25. dnl Arrange for getopt.h to be created.
  26. gl_GETOPT_SUBSTITUTE_HEADER
  27. dnl Arrange for unistd.h to include getopt.h.
  28. GNULIB_UNISTD_H_GETOPT=1
  29. fi
  30. ])
  31. # Request a POSIX compliant getopt function with GNU extensions (such as
  32. # options with optional arguments) and the functions getopt_long,
  33. # getopt_long_only.
  34. AC_DEFUN([gl_FUNC_GETOPT_GNU],
  35. [
  36. m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
  37. AC_REQUIRE([gl_FUNC_GETOPT_POSIX])
  38. ])
  39. # emacs' configure.in uses this.
  40. AC_DEFUN([gl_GETOPT_IFELSE],
  41. [
  42. AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
  43. AS_IF([test -n "$gl_replace_getopt"], [$1], [$2])
  44. ])
  45. # Determine whether to replace the entire getopt facility.
  46. AC_DEFUN([gl_GETOPT_CHECK_HEADERS],
  47. [
  48. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  49. AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
  50. dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
  51. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  52. gl_CHECK_NEXT_HEADERS([getopt.h])
  53. if test $ac_cv_header_getopt_h = yes; then
  54. HAVE_GETOPT_H=1
  55. else
  56. HAVE_GETOPT_H=0
  57. fi
  58. AC_SUBST([HAVE_GETOPT_H])
  59. gl_replace_getopt=
  60. dnl Test whether <getopt.h> is available.
  61. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  62. AC_CHECK_HEADERS([getopt.h], [], [gl_replace_getopt=yes])
  63. fi
  64. dnl Test whether the function getopt_long is available.
  65. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  66. AC_CHECK_FUNCS([getopt_long_only], [], [gl_replace_getopt=yes])
  67. fi
  68. dnl mingw's getopt (in libmingwex.a) does weird things when the options
  69. dnl strings starts with '+' and it's not the first call. Some internal state
  70. dnl is left over from earlier calls, and neither setting optind = 0 nor
  71. dnl setting optreset = 1 get rid of this internal state.
  72. dnl POSIX is silent on optind vs. optreset, so we allow either behavior.
  73. dnl POSIX 2008 does not specify leading '+' behavior, but see
  74. dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
  75. dnl the next version of POSIX. For now, we only guarantee leading '+'
  76. dnl behavior with getopt-gnu.
  77. if test -z "$gl_replace_getopt"; then
  78. AC_CACHE_CHECK([whether getopt is POSIX compatible],
  79. [gl_cv_func_getopt_posix],
  80. [
  81. dnl BSD getopt_long uses an incompatible method to reset option
  82. dnl processing. Existence of the optreset variable, in and of
  83. dnl itself, is not a reason to replace getopt, but knowledge
  84. dnl of the variable is needed to determine how to reset and
  85. dnl whether a reset reparses the environment. Solaris
  86. dnl supports neither optreset nor optind=0, but keeps no state
  87. dnl that needs a reset beyond setting optind=1; detect Solaris
  88. dnl by getopt_clip.
  89. AC_LINK_IFELSE(
  90. [AC_LANG_PROGRAM(
  91. [[#include <unistd.h>]],
  92. [[int *p = &optreset; return optreset;]])],
  93. [gl_optind_min=1],
  94. [AC_COMPILE_IFELSE(
  95. [AC_LANG_PROGRAM(
  96. [[#include <getopt.h>]],
  97. [[return !getopt_clip;]])],
  98. [gl_optind_min=1],
  99. [gl_optind_min=0])])
  100. dnl This test fails on mingw and succeeds on many other platforms.
  101. gl_save_CPPFLAGS=$CPPFLAGS
  102. CPPFLAGS="$CPPFLAGS -DOPTIND_MIN=$gl_optind_min"
  103. AC_RUN_IFELSE([AC_LANG_SOURCE([[
  104. #include <unistd.h>
  105. #include <stdlib.h>
  106. #include <string.h>
  107. int
  108. main ()
  109. {
  110. {
  111. static char program[] = "program";
  112. static char a[] = "-a";
  113. static char foo[] = "foo";
  114. static char bar[] = "bar";
  115. char *argv[] = { program, a, foo, bar, NULL };
  116. int c;
  117. optind = OPTIND_MIN;
  118. opterr = 0;
  119. c = getopt (4, argv, "ab");
  120. if (!(c == 'a'))
  121. return 1;
  122. c = getopt (4, argv, "ab");
  123. if (!(c == -1))
  124. return 2;
  125. if (!(optind == 2))
  126. return 3;
  127. }
  128. /* Some internal state exists at this point. */
  129. {
  130. static char program[] = "program";
  131. static char donald[] = "donald";
  132. static char p[] = "-p";
  133. static char billy[] = "billy";
  134. static char duck[] = "duck";
  135. static char a[] = "-a";
  136. static char bar[] = "bar";
  137. char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
  138. int c;
  139. optind = OPTIND_MIN;
  140. opterr = 0;
  141. c = getopt (7, argv, "+abp:q:");
  142. if (!(c == -1))
  143. return 4;
  144. if (!(strcmp (argv[0], "program") == 0))
  145. return 5;
  146. if (!(strcmp (argv[1], "donald") == 0))
  147. return 6;
  148. if (!(strcmp (argv[2], "-p") == 0))
  149. return 7;
  150. if (!(strcmp (argv[3], "billy") == 0))
  151. return 8;
  152. if (!(strcmp (argv[4], "duck") == 0))
  153. return 9;
  154. if (!(strcmp (argv[5], "-a") == 0))
  155. return 10;
  156. if (!(strcmp (argv[6], "bar") == 0))
  157. return 11;
  158. if (!(optind == 1))
  159. return 12;
  160. }
  161. /* Detect MacOS 10.5, AIX 7.1 bug. */
  162. {
  163. static char program[] = "program";
  164. static char ab[] = "-ab";
  165. char *argv[3] = { program, ab, NULL };
  166. optind = OPTIND_MIN;
  167. opterr = 0;
  168. if (getopt (2, argv, "ab:") != 'a')
  169. return 13;
  170. if (getopt (2, argv, "ab:") != '?')
  171. return 14;
  172. if (optopt != 'b')
  173. return 15;
  174. if (optind != 2)
  175. return 16;
  176. }
  177. return 0;
  178. }
  179. ]])],
  180. [gl_cv_func_getopt_posix=yes], [gl_cv_func_getopt_posix=no],
  181. [case "$host_os" in
  182. mingw*) gl_cv_func_getopt_posix="guessing no";;
  183. darwin* | aix*) gl_cv_func_getopt_posix="guessing no";;
  184. *) gl_cv_func_getopt_posix="guessing yes";;
  185. esac
  186. ])
  187. CPPFLAGS=$gl_save_CPPFLAGS
  188. ])
  189. case "$gl_cv_func_getopt_posix" in
  190. *no) gl_replace_getopt=yes ;;
  191. esac
  192. fi
  193. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  194. AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
  195. [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
  196. # optstring is necessary for programs like m4 that have POSIX-mandated
  197. # semantics for supporting options interspersed with files.
  198. # Also, since getopt_long is a GNU extension, we require optind=0.
  199. # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
  200. # so take care to revert to the correct (non-)export state.
  201. dnl GNU Coding Standards currently allow awk but not env; besides, env
  202. dnl is ambiguous with environment values that contain newlines.
  203. gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
  204. case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
  205. xx) gl_had_POSIXLY_CORRECT=exported ;;
  206. x) gl_had_POSIXLY_CORRECT=yes ;;
  207. *) gl_had_POSIXLY_CORRECT= ;;
  208. esac
  209. POSIXLY_CORRECT=1
  210. export POSIXLY_CORRECT
  211. AC_RUN_IFELSE(
  212. [AC_LANG_PROGRAM([[#include <getopt.h>
  213. #include <stddef.h>
  214. #include <string.h>
  215. ]GL_NOCRASH[
  216. ]], [[
  217. int result = 0;
  218. nocrash_init();
  219. /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
  220. and fails on MacOS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
  221. OSF/1 5.1, Solaris 10. */
  222. {
  223. static char conftest[] = "conftest";
  224. static char plus[] = "-+";
  225. char *argv[3] = { conftest, plus, NULL };
  226. opterr = 0;
  227. if (getopt (2, argv, "+a") != '?')
  228. result |= 1;
  229. }
  230. /* This code succeeds on glibc 2.8, mingw,
  231. and fails on MacOS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
  232. IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
  233. {
  234. static char program[] = "program";
  235. static char p[] = "-p";
  236. static char foo[] = "foo";
  237. static char bar[] = "bar";
  238. char *argv[] = { program, p, foo, bar, NULL };
  239. optind = 1;
  240. if (getopt (4, argv, "p::") != 'p')
  241. result |= 2;
  242. else if (optarg != NULL)
  243. result |= 4;
  244. else if (getopt (4, argv, "p::") != -1)
  245. result |= 6;
  246. else if (optind != 2)
  247. result |= 8;
  248. }
  249. /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
  250. {
  251. static char program[] = "program";
  252. static char foo[] = "foo";
  253. static char p[] = "-p";
  254. char *argv[] = { program, foo, p, NULL };
  255. optind = 0;
  256. if (getopt (3, argv, "-p") != 1)
  257. result |= 16;
  258. else if (getopt (3, argv, "-p") != 'p')
  259. result |= 32;
  260. }
  261. /* This code fails on glibc 2.11. */
  262. {
  263. static char program[] = "program";
  264. static char b[] = "-b";
  265. static char a[] = "-a";
  266. char *argv[] = { program, b, a, NULL };
  267. optind = opterr = 0;
  268. if (getopt (3, argv, "+:a:b") != 'b')
  269. result |= 64;
  270. else if (getopt (3, argv, "+:a:b") != ':')
  271. result |= 64;
  272. }
  273. /* This code dumps core on glibc 2.14. */
  274. {
  275. static char program[] = "program";
  276. static char w[] = "-W";
  277. static char dummy[] = "dummy";
  278. char *argv[] = { program, w, dummy, NULL };
  279. optind = opterr = 1;
  280. if (getopt (3, argv, "W;") != 'W')
  281. result |= 128;
  282. }
  283. return result;
  284. ]])],
  285. [gl_cv_func_getopt_gnu=yes],
  286. [gl_cv_func_getopt_gnu=no],
  287. [dnl Cross compiling. Guess based on host and declarations.
  288. case $host_os:$ac_cv_have_decl_optreset in
  289. *-gnu*:* | mingw*:*) gl_cv_func_getopt_gnu=no;;
  290. *:yes) gl_cv_func_getopt_gnu=no;;
  291. *) gl_cv_func_getopt_gnu=yes;;
  292. esac
  293. ])
  294. case $gl_had_POSIXLY_CORRECT in
  295. exported) ;;
  296. yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
  297. *) AS_UNSET([POSIXLY_CORRECT]) ;;
  298. esac
  299. ])
  300. if test "$gl_cv_func_getopt_gnu" = "no"; then
  301. gl_replace_getopt=yes
  302. fi
  303. fi
  304. ])
  305. # emacs' configure.in uses this.
  306. AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
  307. [
  308. GETOPT_H=getopt.h
  309. AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
  310. [Define to rpl_ if the getopt replacement functions and variables
  311. should be used.])
  312. AC_SUBST([GETOPT_H])
  313. ])
  314. # Prerequisites of lib/getopt*.
  315. # emacs' configure.in uses this.
  316. AC_DEFUN([gl_PREREQ_GETOPT],
  317. [
  318. AC_CHECK_DECLS_ONCE([getenv])
  319. ])