getopt.m4 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # getopt.m4 serial 46
  2. dnl Copyright (C) 2002-2006, 2008-2017 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. AC_REQUIRE([gl_GETOPT_CHECK_HEADERS])
  12. dnl Other modules can request the gnulib implementation of the getopt
  13. dnl functions unconditionally, by defining gl_REPLACE_GETOPT_ALWAYS.
  14. dnl argp.m4 does this.
  15. m4_ifdef([gl_REPLACE_GETOPT_ALWAYS], [
  16. REPLACE_GETOPT=1
  17. ], [
  18. REPLACE_GETOPT=0
  19. if test -n "$gl_replace_getopt"; then
  20. REPLACE_GETOPT=1
  21. fi
  22. ])
  23. if test $REPLACE_GETOPT = 1; then
  24. dnl Arrange for getopt.h to be created.
  25. gl_GETOPT_SUBSTITUTE_HEADER
  26. fi
  27. ])
  28. # Request a POSIX compliant getopt function with GNU extensions (such as
  29. # options with optional arguments) and the functions getopt_long,
  30. # getopt_long_only.
  31. AC_DEFUN([gl_FUNC_GETOPT_GNU],
  32. [
  33. dnl Set the variable gl_getopt_required, so that all invocations of
  34. dnl gl_GETOPT_CHECK_HEADERS in the scope of the current configure file
  35. dnl will check for getopt with GNU extensions.
  36. dnl This means that if one gnulib-tool invocation requests getopt-posix
  37. dnl and another gnulib-tool invocation requests getopt-gnu, it is as if
  38. dnl both had requested getopt-gnu.
  39. m4_divert_text([INIT_PREPARE], [gl_getopt_required=GNU])
  40. dnl No need to invoke gl_FUNC_GETOPT_POSIX here; this is automatically
  41. dnl done through the module dependency getopt-gnu -> getopt-posix.
  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. AC_REQUIRE([AC_PROG_AWK]) dnl for awk that supports ENVIRON
  48. dnl Persuade Solaris <unistd.h> to declare optarg, optind, opterr, optopt.
  49. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  50. gl_CHECK_NEXT_HEADERS([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 POSIX 2008 does not specify leading '+' behavior, but see
  67. dnl http://austingroupbugs.net/view.php?id=191 for a recommendation on
  68. dnl the next version of POSIX. For now, we only guarantee leading '+'
  69. dnl behavior with getopt-gnu.
  70. if test -z "$gl_replace_getopt"; then
  71. AC_CACHE_CHECK([whether getopt is POSIX compatible],
  72. [gl_cv_func_getopt_posix],
  73. [
  74. dnl Merging these three different test programs into a single one
  75. dnl would require a reset mechanism. On BSD systems, it can be done
  76. dnl through 'optreset'; on some others (glibc), it can be done by
  77. dnl setting 'optind' to 0; on others again (HP-UX, IRIX, OSF/1,
  78. dnl Solaris 9, musl libc), there is no such mechanism.
  79. if test $cross_compiling = no; then
  80. dnl Sanity check. Succeeds everywhere (except on MSVC,
  81. dnl which lacks <unistd.h> and getopt() entirely).
  82. AC_RUN_IFELSE(
  83. [AC_LANG_SOURCE([[
  84. #include <unistd.h>
  85. #include <stdlib.h>
  86. #include <string.h>
  87. int
  88. main ()
  89. {
  90. static char program[] = "program";
  91. static char a[] = "-a";
  92. static char foo[] = "foo";
  93. static char bar[] = "bar";
  94. char *argv[] = { program, a, foo, bar, NULL };
  95. int c;
  96. c = getopt (4, argv, "ab");
  97. if (!(c == 'a'))
  98. return 1;
  99. c = getopt (4, argv, "ab");
  100. if (!(c == -1))
  101. return 2;
  102. if (!(optind == 2))
  103. return 3;
  104. return 0;
  105. }
  106. ]])],
  107. [gl_cv_func_getopt_posix=maybe],
  108. [gl_cv_func_getopt_posix=no])
  109. if test $gl_cv_func_getopt_posix = maybe; then
  110. dnl Sanity check with '+'. Succeeds everywhere (except on MSVC,
  111. dnl which lacks <unistd.h> and getopt() entirely).
  112. AC_RUN_IFELSE(
  113. [AC_LANG_SOURCE([[
  114. #include <unistd.h>
  115. #include <stdlib.h>
  116. #include <string.h>
  117. int
  118. main ()
  119. {
  120. static char program[] = "program";
  121. static char donald[] = "donald";
  122. static char p[] = "-p";
  123. static char billy[] = "billy";
  124. static char duck[] = "duck";
  125. static char a[] = "-a";
  126. static char bar[] = "bar";
  127. char *argv[] = { program, donald, p, billy, duck, a, bar, NULL };
  128. int c;
  129. c = getopt (7, argv, "+abp:q:");
  130. if (!(c == -1))
  131. return 4;
  132. if (!(strcmp (argv[0], "program") == 0))
  133. return 5;
  134. if (!(strcmp (argv[1], "donald") == 0))
  135. return 6;
  136. if (!(strcmp (argv[2], "-p") == 0))
  137. return 7;
  138. if (!(strcmp (argv[3], "billy") == 0))
  139. return 8;
  140. if (!(strcmp (argv[4], "duck") == 0))
  141. return 9;
  142. if (!(strcmp (argv[5], "-a") == 0))
  143. return 10;
  144. if (!(strcmp (argv[6], "bar") == 0))
  145. return 11;
  146. if (!(optind == 1))
  147. return 12;
  148. return 0;
  149. }
  150. ]])],
  151. [gl_cv_func_getopt_posix=maybe],
  152. [gl_cv_func_getopt_posix=no])
  153. fi
  154. if test $gl_cv_func_getopt_posix = maybe; then
  155. dnl Detect Mac OS X 10.5, AIX 7.1, mingw bug.
  156. AC_RUN_IFELSE(
  157. [AC_LANG_SOURCE([[
  158. #include <unistd.h>
  159. #include <stdlib.h>
  160. #include <string.h>
  161. int
  162. main ()
  163. {
  164. static char program[] = "program";
  165. static char ab[] = "-ab";
  166. char *argv[3] = { program, ab, NULL };
  167. if (getopt (2, argv, "ab:") != 'a')
  168. return 13;
  169. if (getopt (2, argv, "ab:") != '?')
  170. return 14;
  171. if (optopt != 'b')
  172. return 15;
  173. if (optind != 2)
  174. return 16;
  175. return 0;
  176. }
  177. ]])],
  178. [gl_cv_func_getopt_posix=yes],
  179. [gl_cv_func_getopt_posix=no])
  180. fi
  181. else
  182. case "$host_os" in
  183. darwin* | aix* | mingw*) gl_cv_func_getopt_posix="guessing no";;
  184. *) gl_cv_func_getopt_posix="guessing yes";;
  185. esac
  186. fi
  187. ])
  188. case "$gl_cv_func_getopt_posix" in
  189. *no) gl_replace_getopt=yes ;;
  190. esac
  191. fi
  192. if test -z "$gl_replace_getopt" && test $gl_getopt_required = GNU; then
  193. AC_CACHE_CHECK([for working GNU getopt function], [gl_cv_func_getopt_gnu],
  194. [# Even with POSIXLY_CORRECT, the GNU extension of leading '-' in the
  195. # optstring is necessary for programs like m4 that have POSIX-mandated
  196. # semantics for supporting options interspersed with files.
  197. # Also, since getopt_long is a GNU extension, we require optind=0.
  198. # Bash ties 'set -o posix' to a non-exported POSIXLY_CORRECT;
  199. # so take care to revert to the correct (non-)export state.
  200. dnl GNU Coding Standards currently allow awk but not env; besides, env
  201. dnl is ambiguous with environment values that contain newlines.
  202. gl_awk_probe='BEGIN { if ("POSIXLY_CORRECT" in ENVIRON) print "x" }'
  203. case ${POSIXLY_CORRECT+x}`$AWK "$gl_awk_probe" </dev/null` in
  204. xx) gl_had_POSIXLY_CORRECT=exported ;;
  205. x) gl_had_POSIXLY_CORRECT=yes ;;
  206. *) gl_had_POSIXLY_CORRECT= ;;
  207. esac
  208. POSIXLY_CORRECT=1
  209. export POSIXLY_CORRECT
  210. AC_RUN_IFELSE(
  211. [AC_LANG_PROGRAM([[#include <getopt.h>
  212. #include <stddef.h>
  213. #include <string.h>
  214. ]GL_NOCRASH[
  215. ]], [[
  216. int result = 0;
  217. nocrash_init();
  218. /* This code succeeds on glibc 2.8, OpenBSD 4.0, Cygwin, mingw,
  219. and fails on Mac OS X 10.5, AIX 5.2, HP-UX 11, IRIX 6.5,
  220. OSF/1 5.1, Solaris 10. */
  221. {
  222. static char conftest[] = "conftest";
  223. static char plus[] = "-+";
  224. char *argv[3] = { conftest, plus, NULL };
  225. opterr = 0;
  226. if (getopt (2, argv, "+a") != '?')
  227. result |= 1;
  228. }
  229. /* This code succeeds on glibc 2.8, mingw,
  230. and fails on Mac OS X 10.5, OpenBSD 4.0, AIX 5.2, HP-UX 11,
  231. IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x. */
  232. {
  233. static char program[] = "program";
  234. static char p[] = "-p";
  235. static char foo[] = "foo";
  236. static char bar[] = "bar";
  237. char *argv[] = { program, p, foo, bar, NULL };
  238. optind = 1;
  239. if (getopt (4, argv, "p::") != 'p')
  240. result |= 2;
  241. else if (optarg != NULL)
  242. result |= 4;
  243. else if (getopt (4, argv, "p::") != -1)
  244. result |= 6;
  245. else if (optind != 2)
  246. result |= 8;
  247. }
  248. /* This code succeeds on glibc 2.8 and fails on Cygwin 1.7.0. */
  249. {
  250. static char program[] = "program";
  251. static char foo[] = "foo";
  252. static char p[] = "-p";
  253. char *argv[] = { program, foo, p, NULL };
  254. optind = 0;
  255. if (getopt (3, argv, "-p") != 1)
  256. result |= 16;
  257. else if (getopt (3, argv, "-p") != 'p')
  258. result |= 16;
  259. }
  260. /* This code fails on glibc 2.11. */
  261. {
  262. static char program[] = "program";
  263. static char b[] = "-b";
  264. static char a[] = "-a";
  265. char *argv[] = { program, b, a, NULL };
  266. optind = opterr = 0;
  267. if (getopt (3, argv, "+:a:b") != 'b')
  268. result |= 32;
  269. else if (getopt (3, argv, "+:a:b") != ':')
  270. result |= 32;
  271. }
  272. /* This code dumps core on glibc 2.14. */
  273. {
  274. static char program[] = "program";
  275. static char w[] = "-W";
  276. static char dummy[] = "dummy";
  277. char *argv[] = { program, w, dummy, NULL };
  278. optind = opterr = 1;
  279. if (getopt (3, argv, "W;") != 'W')
  280. result |= 64;
  281. }
  282. return result;
  283. ]])],
  284. [gl_cv_func_getopt_gnu=yes],
  285. [gl_cv_func_getopt_gnu=no],
  286. [dnl Cross compiling. Assume the worst, even on glibc platforms.
  287. gl_cv_func_getopt_gnu="guessing no"
  288. ])
  289. case $gl_had_POSIXLY_CORRECT in
  290. exported) ;;
  291. yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
  292. *) AS_UNSET([POSIXLY_CORRECT]) ;;
  293. esac
  294. ])
  295. if test "$gl_cv_func_getopt_gnu" != yes; then
  296. gl_replace_getopt=yes
  297. else
  298. AC_CACHE_CHECK([for working GNU getopt_long function],
  299. [gl_cv_func_getopt_long_gnu],
  300. [AC_RUN_IFELSE(
  301. [AC_LANG_PROGRAM(
  302. [[#include <getopt.h>
  303. #include <stddef.h>
  304. #include <string.h>
  305. ]],
  306. [[static const struct option long_options[] =
  307. {
  308. { "xtremely-",no_argument, NULL, 1003 },
  309. { "xtra", no_argument, NULL, 1001 },
  310. { "xtreme", no_argument, NULL, 1002 },
  311. { "xtremely", no_argument, NULL, 1003 },
  312. { NULL, 0, NULL, 0 }
  313. };
  314. /* This code fails on OpenBSD 5.0. */
  315. {
  316. static char program[] = "program";
  317. static char xtremel[] = "--xtremel";
  318. char *argv[] = { program, xtremel, NULL };
  319. int option_index;
  320. optind = 1; opterr = 0;
  321. if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
  322. return 1;
  323. }
  324. return 0;
  325. ]])],
  326. [gl_cv_func_getopt_long_gnu=yes],
  327. [gl_cv_func_getopt_long_gnu=no],
  328. [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
  329. case "$host_os" in
  330. openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
  331. *) gl_cv_func_getopt_long_gnu="guessing yes";;
  332. esac
  333. ])
  334. ])
  335. case "$gl_cv_func_getopt_long_gnu" in
  336. *yes) ;;
  337. *) gl_replace_getopt=yes ;;
  338. esac
  339. fi
  340. fi
  341. ])
  342. AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
  343. [
  344. AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
  345. if test $ac_cv_header_sys_cdefs_h = yes; then
  346. HAVE_SYS_CDEFS_H=1
  347. else
  348. HAVE_SYS_CDEFS_H=0
  349. fi
  350. AC_SUBST([HAVE_SYS_CDEFS_H])
  351. AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
  352. [Define to rpl_ if the getopt replacement functions and variables
  353. should be used.])
  354. GETOPT_H=getopt.h
  355. GETOPT_CDEFS_H=getopt-cdefs.h
  356. AC_SUBST([GETOPT_H])
  357. AC_SUBST([GETOPT_CDEFS_H])
  358. ])