getloadavg: avoid compile failure on AIX 6.1
[gnulib.git] / gnulib-tool
index 2e7b73d..965f516 100755 (executable)
@@ -1,6 +1,6 @@
 #! /bin/sh
 #
-# Copyright (C) 2002-2008 Free Software Foundation, Inc.
+# Copyright (C) 2002-2009 Free Software Foundation, Inc.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -398,11 +398,57 @@ if ( foo=bar; foo+=baz && test "$foo" = barbaz ) >/dev/null 2>&1; then
   {
     eval "$1+=\"\$2\""
   }
+  fast_func_append=true
 else
   func_append ()
   {
     eval "$1=\"\$$1\$2\""
   }
+  fast_func_append=false
+fi
+
+# func_remove_prefix var prefix
+# removes the given prefix from the value of the shell variable var.
+# var should be the name of a shell variable.
+# Its value should not contain a newline and not start or end with whitespace.
+# prefix should not contain the characters "$`\{}|.
+if ( foo=bar; eval 'test "${foo#b}" = ar' ) >/dev/null 2>&1; then
+  func_remove_prefix ()
+  {
+    eval "$1=\${$1#\$2}"
+  }
+  fast_func_remove_prefix=true
+else
+  func_remove_prefix ()
+  {
+    eval "value=\"\$$1\""
+    prefix="$2"
+    value=`echo "$value" | sed -e "s|^${prefix}||"`
+    eval "$1=\"\$value\""
+  }
+  fast_func_remove_prefix=false
+fi
+
+# func_remove_suffix var suffix
+# removes the given suffix from the value of the shell variable var.
+# var should be the name of a shell variable.
+# Its value should not contain a newline and not start or end with whitespace.
+# suffix should not contain the characters "$`\{}|.
+if ( foo=bar; eval 'test "${foo%r}" = ba' ) >/dev/null 2>&1; then
+  func_remove_suffix ()
+  {
+    eval "$1=\${$1%\$2}"
+  }
+  fast_func_remove_suffix=true
+else
+  func_remove_suffix ()
+  {
+    eval "value=\"\$$1\""
+    suffix="$2"
+    value=`echo "$value" | sed -e "s|${suffix}\$||"`
+    eval "$1=\"\$value\""
+  }
+  fast_func_remove_suffix=false
 fi
 
 # func_fatal_error message
@@ -578,7 +624,9 @@ func_ln_if_changed ()
 # will read from this pipe might prematurely exit or close its standard input
 # descriptor.
 if test -n "$BASH_VERSION"; then
-  # The problem has only been reported with bash.
+  # The problem has only been reported with bash. Probably it occurs only with
+  # bash-3.2. For the reasons, see
+  # <http://lists.gnu.org/archive/html/bug-bash/2008-12/msg00050.html>.
   # Note that Solaris sh does not understand "trap - SIGPIPE".
   func_reset_sigpipe ()
   {
@@ -1326,13 +1374,63 @@ func_get_filelist ()
   esac
 }
 
+# func_filter_filelist outputvar separator filelist prefix suffix removed_prefix removed_suffix [added_prefix [added_suffix]]
+# stores in outputvar the filtered and processed filelist. Filtering: Only the
+# elements starting with prefix and ending with suffix are considered.
+# Processing: removed_prefix and removed_suffix are removed from each element,
+# added_prefix and added_suffix are added to each element.
+# removed_prefix, removed_suffix should not contain the characters "$`\{}|.
+# added_prefix, added_suffix should not contain the characters \|.
+func_filter_filelist ()
+{
+  if test "$2" != "$nl" \
+     || { $fast_func_append \
+          && { test -z "$6" || $fast_func_remove_prefix; } \
+          && { test -z "$7" || $fast_func_remove_suffix; }; \
+        }; then
+    ffflist=
+    for fff in $3; do
+      case "$fff" in
+        "$4"*"$5")
+          if test -n "$6"; then
+            func_remove_prefix fff "$6"
+          fi
+          if test -n "$7"; then
+            func_remove_suffix fff "$7"
+          fi
+          fff="$8${fff}$9"
+          if test -z "$ffflist"; then
+            ffflist="${fff}"
+          else
+            func_append ffflist "$2${fff}"
+          fi
+          ;;
+      esac
+    done
+  else
+    sed_fff_filter="s|^$6\(.*\)$7\$|$8\\1$9|"
+    ffflist=`for fff in $3; do
+               case "$fff" in
+                 "$4"*"$5") echo "$fff" ;;
+               esac
+             done | sed -e "$sed_fff_filter"`
+  fi
+  eval "$1=\"\$ffflist\""
+}
+
 # func_get_dependencies module
 # Input:
 # - local_gnulib_dir  from --local-dir
 func_get_dependencies ()
 {
   # ${module}-tests always implicitly depends on ${module}.
-  echo "$1" | sed -n -e 's/-tests$//p'
+  case "$1" in
+    *-tests)
+      fgd1="$1"
+      func_remove_suffix fgd1 '-tests'
+      echo "$fgd1"
+      ;;
+  esac
   # Then the explicit dependencies listed in the module description.
   func_lookup_file "modules/$1"
   sed -n -e "/^Depends-on$sed_extract_prog" < "$lookedup_file"
@@ -1368,11 +1466,7 @@ func_get_automake_snippet ()
       # *-tests module live in tests/, not lib/.
       # Synthesize an EXTRA_DIST augmentation.
       all_files=`func_get_filelist $1`
-      tests_files=`for f in $all_files; do \
-                     case $f in \
-                       tests/*) echo $f ;; \
-                     esac; \
-                   done | sed -e 's,^tests/,,'`
+      func_filter_filelist tests_files " " "$all_files" 'tests/' '' 'tests/' ''
       extra_files="$tests_files"
       if test -n "$extra_files"; then
         echo "EXTRA_DIST +=" $extra_files
@@ -1394,11 +1488,7 @@ func_get_automake_snippet ()
         | sed -e "$sed_combine_lines" \
         | sed -n -e "$sed_extract_mentioned_files" | sed -e 's/#.*//'`
       all_files=`func_get_filelist $1`
-      lib_files=`for f in $all_files; do \
-                   case $f in \
-                     lib/*) echo $f ;; \
-                   esac; \
-                 done | sed -e 's,^lib/,,'`
+      func_filter_filelist lib_files "$nl" "$all_files" 'lib/' '' 'lib/' ''
       # Remove $already_mentioned_files from $lib_files.
       echo "$lib_files" | LC_ALL=C sort -u > "$tmp"/lib-files
       extra_files=`func_reset_sigpipe; \
@@ -1422,8 +1512,7 @@ func_get_automake_snippet ()
       case "$1" in
         relocatable-prog-wrapper) ;;
         *)
-          sed_extract_c_files='/\.c$/p'
-          extra_files=`echo "$extra_files" | sed -n -e "$sed_extract_c_files"`
+          func_filter_filelist extra_files "$nl" "$extra_files" '' '.c' '' ''
           if test -n "$extra_files"; then
             echo "EXTRA_lib_SOURCES +=" $extra_files
             echo
@@ -1431,22 +1520,14 @@ func_get_automake_snippet ()
           ;;
       esac
       # Synthesize an EXTRA_DIST augmentation also for the files in build-aux/.
-      buildaux_files=`for f in $all_files; do \
-                        case $f in \
-                          build-aux/*) echo $f ;; \
-                        esac; \
-                      done | sed -e 's,^build-aux/,,'`
+      func_filter_filelist buildaux_files "$nl" "$all_files" 'build-aux/' '' 'build-aux/' ''
       if test -n "$buildaux_files"; then
         sed_prepend_auxdir='s,^,$(top_srcdir)/'"$auxdir"'/,'
         echo "EXTRA_DIST += "`echo "$buildaux_files" | sed -e "$sed_prepend_auxdir"`
         echo
       fi
       # Synthesize an EXTRA_DIST augmentation also for the files from top/.
-      top_files=`for f in $all_files; do \
-                   case $f in \
-                     top/*) echo $f ;; \
-                   esac; \
-                 done | sed -e 's,^top/,,'`
+      func_filter_filelist top_files "$nl" "$all_files" 'top/' '' 'top/' ''
       if test -n "$top_files"; then
         sed_prepend_topdir='s,^,$(top_srcdir)/,'
         echo "EXTRA_DIST += "`echo "$top_files" | sed -e "$sed_prepend_topdir"`
@@ -1590,6 +1671,7 @@ func_modules_transitive_closure ()
 # - modules         list of modules, including 'dummy' if needed
 func_modules_add_dummy ()
 {
+  # Determine whether any module provides a lib_SOURCES augmentation.
   have_lib_SOURCES=
   sed_remove_backslash_newline=':a
 /\\$/{
@@ -1606,7 +1688,10 @@ ba
         # Ignore .h files since they are not compiled.
         case "$file" in
           *.h) ;;
-          *) have_lib_SOURCES=yes ;;
+          *)
+            have_lib_SOURCES=yes
+            break 2
+            ;;
         esac
       done
     fi
@@ -1769,7 +1854,10 @@ func_emit_lib_Makefile_am ()
         # Test whether there are some source files in subdirectories.
         for f in `func_get_filelist "$module"`; do
           case $f in
-            lib/*/*.c) uses_subdirs=yes ;;
+            lib/*/*.c)
+              uses_subdirs=yes
+              break
+              ;;
           esac
         done
       fi
@@ -2016,7 +2104,10 @@ func_emit_tests_Makefile_am ()
         # Test whether there are some source files in subdirectories.
         for f in `func_get_filelist "$module"`; do
           case $f in
-            lib/*/*.c | tests/*/*.c) uses_subdirs=yes ;;
+            lib/*/*.c | tests/*/*.c)
+              uses_subdirs=yes
+              break
+              ;;
           esac
         done
       fi
@@ -2302,7 +2393,7 @@ func_import ()
       s,^dnl .*$,,
       s, dnl .*$,,
       /gl_LOCAL_DIR(/ {
-        s,^.*gl_LOCAL_DIR([[ ]*\([^])]*\).*$,cached_local_gnulib_dir="\1",p
+        s,^.*gl_LOCAL_DIR([[ ]*\([^]"$`\\)]*\).*$,cached_local_gnulib_dir="\1",p
       }
       /gl_MODULES(/ {
         ta
@@ -2312,55 +2403,55 @@ func_import ()
           N
           ba
         :b
-        s,^.*gl_MODULES([[ ]*\([^])]*\).*$,cached_specified_modules="\1",p
+        s,^.*gl_MODULES([[ ]*\([^]"$`\\)]*\).*$,cached_specified_modules="\1",p
       }
       /gl_WITH_OBSOLETE/ {
         s,^.*$,cached_incobsolete=true,p
       }
       /gl_AVOID(/ {
-        s,^.*gl_AVOID([[ ]*\([^])]*\).*$,cached_avoidlist="\1",p
+        s,^.*gl_AVOID([[ ]*\([^]"$`\\)]*\).*$,cached_avoidlist="\1",p
       }
       /gl_SOURCE_BASE(/ {
-        s,^.*gl_SOURCE_BASE([[ ]*\([^])]*\).*$,cached_sourcebase="\1",p
+        s,^.*gl_SOURCE_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_sourcebase="\1",p
       }
       /gl_M4_BASE(/ {
-        s,^.*gl_M4_BASE([[ ]*\([^])]*\).*$,cached_m4base="\1",p
+        s,^.*gl_M4_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_m4base="\1",p
       }
       /gl_PO_BASE(/ {
-        s,^.*gl_PO_BASE([[ ]*\([^])]*\).*$,cached_pobase="\1",p
+        s,^.*gl_PO_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_pobase="\1",p
       }
       /gl_DOC_BASE(/ {
-        s,^.*gl_DOC_BASE([[ ]*\([^])]*\).*$,cached_docbase="\1",p
+        s,^.*gl_DOC_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_docbase="\1",p
       }
       /gl_TESTS_BASE(/ {
-        s,^.*gl_TESTS_BASE([[ ]*\([^])]*\).*$,cached_testsbase="\1",p
+        s,^.*gl_TESTS_BASE([[ ]*\([^]"$`\\)]*\).*$,cached_testsbase="\1",p
       }
       /gl_WITH_TESTS/ {
         s,^.*$,cached_inctests=true,p
       }
       /gl_LIB(/ {
-        s,^.*gl_LIB([[ ]*\([^])]*\).*$,cached_libname="\1",p
+        s,^.*gl_LIB([[ ]*\([^]"$`\\)]*\).*$,cached_libname="\1",p
       }
       /gl_LGPL(/ {
-        s,^.*gl_LGPL([[ ]*\([^])]*\).*$,cached_lgpl="\1",p
+        s,^.*gl_LGPL([[ ]*\([^]"$`\\)]*\).*$,cached_lgpl="\1",p
       }
       /gl_LGPL/ {
         s,^.*$,cached_lgpl=yes,p
       }
       /gl_MAKEFILE_NAME(/ {
-        s,^.*gl_MAKEFILE_NAME([[ ]*\([^])]*\).*$,cached_makefile_name="\1",p
+        s,^.*gl_MAKEFILE_NAME([[ ]*\([^]"$`\\)]*\).*$,cached_makefile_name="\1",p
       }
       /gl_LIBTOOL/ {
         s,^.*$,cached_libtool=true,p
       }
       /gl_MACRO_PREFIX(/ {
-        s,^.*gl_MACRO_PREFIX([[ ]*\([^])]*\).*$,cached_macro_prefix="\1",p
+        s,^.*gl_MACRO_PREFIX([[ ]*\([^]"$`\\)]*\).*$,cached_macro_prefix="\1",p
       }
       /gl_PO_DOMAIN(/ {
-        s,^.*gl_PO_DOMAIN([[ ]*\([^])]*\).*$,cached_po_domain="\1",p
+        s,^.*gl_PO_DOMAIN([[ ]*\([^]"$`\\)]*\).*$,cached_po_domain="\1",p
       }
       /gl_VC_FILES(/ {
-        s,^.*gl_VC_FILES([[ ]*\([^])]*\).*$,cached_vc_files="\1",p
+        s,^.*gl_VC_FILES([[ ]*\([^]"$`\\)]*\).*$,cached_vc_files="\1",p
       }'
     eval `sed -n -e "$my_sed_traces" < "$destdir"/$m4base/gnulib-cache.m4`
     if test -f "$destdir"/$m4base/gnulib-comp.m4; then
@@ -2375,6 +2466,7 @@ func_import ()
           :a
           s,^\]).*$,",
           tb
+          s,["$`\\],,g
           p
           n
           ba
@@ -2538,15 +2630,15 @@ func_import ()
     func_verify_nontests_module
     if test -n "$module"; then
       all_files=`func_get_filelist $module`
-      lib_files=`for f in $all_files; do \
-                   case $f in \
-                     lib/*) echo $f ;; \
-                   esac; \
-                 done | sed -e 's,^lib/,,'`
-      if test -n "$lib_files"; then
-        use_libtests=true
-        break
-      fi
+      # Test whether some file in $all_files lies in lib/.
+      for f in $all_files; do
+        case $f in
+          lib/*)
+            use_libtests=true
+            break 2
+            ;;
+        esac
+      done
     fi
   done
 
@@ -3456,16 +3548,16 @@ s,//*$,/,'
             sed -e "s|^$anchor||" < "$destdir/$dir$ignore" | LC_ALL=C sort > "$tmp"/ignore
             (func_reset_sigpipe
              echo "$dir_added" | sed -e '/^$/d' | LC_ALL=C sort -u \
-               | LC_ALL=C join -v 2 "$tmp"/ignore - > "$tmp"/ignore-added
+               | LC_ALL=C join -v 1 - "$tmp"/ignore > "$tmp"/ignore-added
              echo "$dir_removed" | sed -e '/^$/d' | LC_ALL=C sort -u \
-               | LC_ALL=C join -v 2 "$tmp"/ignore - > "$tmp"/ignore-removed
+               | LC_ALL=C join -v 1 - "$tmp"/ignore > "$tmp"/ignore-removed
             )
             if test -s "$tmp"/ignore-added || test -s "$tmp"/ignore-removed; then
               if $doit; then
                 echo "Updating $destdir/$dir$ignore (backup in $destdir/$dir${ignore}~)"
                 mv -f "$destdir/$dir$ignore" "$destdir/$dir$ignore"~
-                { sed -e 's,^,/^,' -e 's,$,\$/d,' < "$tmp"/ignore-removed
-                  if test -n "$anchor"; then sed -e "s,^,/^${doubly_escaped_anchor}," -e 's,$,\$/d,' < "$tmp"/ignore-removed; fi
+                { sed -e 's,/,\\/,g' -e 's,^,/^,' -e 's,$,\$/d,' < "$tmp"/ignore-removed
+                  if test -n "$anchor"; then sed -e 's,/,\\/,g' -e "s,^,/^${doubly_escaped_anchor}," -e 's,$,\$/d,' < "$tmp"/ignore-removed; fi
                 } > "$tmp"/sed-ignore-removed
                 { cat "$destdir/$dir$ignore"~
                   sed -e "s|^|$anchor|" < "$tmp"/ignore-added
@@ -3822,7 +3914,14 @@ func_create_testdir ()
      for module in $modules; do
        func_verify_module
        if test -n "$module"; then
-         func_get_autoconf_early_snippet "$module"
+         case $module in
+           gnumakefile | maintainer-makefile)
+             # These modules are meant to be used only in the top-level directory.
+             ;;
+           *)
+             func_get_autoconf_early_snippet "$module"
+             ;;
+         esac
        fi
      done \
        | sed -e '/^$/d;' -e 's/AC_REQUIRE(\[\([^()]*\)\])/\1/'
@@ -3857,8 +3956,15 @@ func_create_testdir ()
      for module in $modules; do
        func_verify_nontests_module
        if test -n "$module"; then
-         func_get_autoconf_snippet "$module" \
-           | sed -e "$sed_replace_build_aux"
+         case $module in
+           gnumakefile | maintainer-makefile)
+             # These modules are meant to be used only in the top-level directory.
+             ;;
+           *)
+             func_get_autoconf_snippet "$module" \
+               | sed -e "$sed_replace_build_aux"
+             ;;
+         esac
        fi
      done
      echo "gl_source_base='.'"
@@ -4018,7 +4124,9 @@ func_create_testdir ()
    if test -f $m4base/gettext.m4; then
      func_execute_command ${AUTOPOINT} --force || func_exit 1
      for f in $m4base/*.m4~; do
-       mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
+       if test -f $f; then
+         mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
+       fi
      done
    fi
    func_execute_command ${ACLOCAL} -I $m4base || func_exit 1
@@ -4037,7 +4145,9 @@ func_create_testdir ()
      if test -f ../$m4base/gettext.m4; then
        func_execute_command ${AUTOPOINT} --force || func_exit 1
        for f in ../$m4base/*.m4~; do
-         mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
+         if test -f $f; then
+           mv -f $f `echo $f | sed -e 's,~$,,'` || func_exit 1
+         fi
        done
      fi
      func_execute_command ${ACLOCAL} -I ../$m4base || func_exit 1
@@ -4217,7 +4327,7 @@ case $mode in
       s,^dnl .*$,,
       s, dnl .*$,,
       /AC_CONFIG_AUX_DIR/ {
-        s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^])]*\).*$,guessed_auxdir="\1",p
+        s,^.*AC_CONFIG_AUX_DIR([[ ]*\([^]"$`\\)]*\).*$,guessed_auxdir="\1",p
       }
       /A[CM]_PROG_LIBTOOL/ {
         s,^.*$,guessed_libtool=true,p