PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/m4/getopt.m4

https://gitlab.com/RobertCochran/emacs
m4 | 381 lines | 336 code | 21 blank | 24 comment | 0 complexity | 9b67e21cd68d58a6342462fc792d1f95 MD5 | raw file
  1. # getopt.m4 serial 47
  2. dnl Copyright (C) 2002-2006, 2008-2019 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.
  287. dnl Assume the worst, even on glibc platforms.
  288. dnl But obey --enable-cross-guesses.
  289. gl_cv_func_getopt_gnu="$gl_cross_guess_normal"
  290. ])
  291. case $gl_had_POSIXLY_CORRECT in
  292. exported) ;;
  293. yes) AS_UNSET([POSIXLY_CORRECT]); POSIXLY_CORRECT=1 ;;
  294. *) AS_UNSET([POSIXLY_CORRECT]) ;;
  295. esac
  296. ])
  297. if test "$gl_cv_func_getopt_gnu" != yes; then
  298. gl_replace_getopt=yes
  299. else
  300. AC_CACHE_CHECK([for working GNU getopt_long function],
  301. [gl_cv_func_getopt_long_gnu],
  302. [AC_RUN_IFELSE(
  303. [AC_LANG_PROGRAM(
  304. [[#include <getopt.h>
  305. #include <stddef.h>
  306. #include <string.h>
  307. ]],
  308. [[static const struct option long_options[] =
  309. {
  310. { "xtremely-",no_argument, NULL, 1003 },
  311. { "xtra", no_argument, NULL, 1001 },
  312. { "xtreme", no_argument, NULL, 1002 },
  313. { "xtremely", no_argument, NULL, 1003 },
  314. { NULL, 0, NULL, 0 }
  315. };
  316. /* This code fails on OpenBSD 5.0. */
  317. {
  318. static char program[] = "program";
  319. static char xtremel[] = "--xtremel";
  320. char *argv[] = { program, xtremel, NULL };
  321. int option_index;
  322. optind = 1; opterr = 0;
  323. if (getopt_long (2, argv, "", long_options, &option_index) != 1003)
  324. return 1;
  325. }
  326. return 0;
  327. ]])],
  328. [gl_cv_func_getopt_long_gnu=yes],
  329. [gl_cv_func_getopt_long_gnu=no],
  330. [dnl Cross compiling. Guess no on OpenBSD, yes otherwise.
  331. case "$host_os" in
  332. openbsd*) gl_cv_func_getopt_long_gnu="guessing no";;
  333. *) gl_cv_func_getopt_long_gnu="guessing yes";;
  334. esac
  335. ])
  336. ])
  337. case "$gl_cv_func_getopt_long_gnu" in
  338. *yes) ;;
  339. *) gl_replace_getopt=yes ;;
  340. esac
  341. fi
  342. fi
  343. ])
  344. AC_DEFUN([gl_GETOPT_SUBSTITUTE_HEADER],
  345. [
  346. AC_CHECK_HEADERS_ONCE([sys/cdefs.h])
  347. if test $ac_cv_header_sys_cdefs_h = yes; then
  348. HAVE_SYS_CDEFS_H=1
  349. else
  350. HAVE_SYS_CDEFS_H=0
  351. fi
  352. AC_SUBST([HAVE_SYS_CDEFS_H])
  353. AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
  354. [Define to rpl_ if the getopt replacement functions and variables
  355. should be used.])
  356. GETOPT_H=getopt.h
  357. GETOPT_CDEFS_H=getopt-cdefs.h
  358. AC_SUBST([GETOPT_H])
  359. AC_SUBST([GETOPT_CDEFS_H])
  360. ])