getopt.m4 12 KB

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