This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [JAVA] /bin/sh portability issues in scripts/check_jni_methods.sh


* Roger Sayle wrote on Fri, Jul 07, 2006 at 06:44:19AM CEST:
> 
> However, I've no idea what the correct way to fix this problem is.  I
> did discover that this issue is related to PR other/2141, which was a
> similar use of "set -C" in gccbug.  The solution there was to reorganize
> the script to avoid the need for this functionality.

Here's a patch (well, it turned more or less into a rewrite), to use
a temporary directory and avoid 'set -C' and some other glitches and
non-Posix flags, as a suggestion.  (There is no need for backslash-
newline after a pipe.)

I think the noclobber may have been used to prevent a security issue
(the file names below /tmp were easily guessed).

I don't know whether it would be better for you to have configure
compute $EGREP, $FGREP and use a check_jni_methods.sh.in.

Beware, I have only lightly tested this patch.

(Note I do have papers for GCC in place, but I'm no regular contributor,
nor have any commit rights or anything.)

Cheers,
Ralf

2006-07-07  Ralf Wildenhues  <Ralf.Wildenhues@gmx.de>

	* scripts/check_jni_methods.sh: Don't use `set -C'; rewrite to
	use a temporary directory, honor $TMPDIR.  Do not use `diff -U',
	`grep -h', cope with either of egrep/fgrep or `grep -E'/`grep -F'.
	Cope with special characters in file names.

Index: scripts/check_jni_methods.sh
===================================================================
--- scripts/check_jni_methods.sh	(revision 115253)
+++ scripts/check_jni_methods.sh	(working copy)
@@ -2,61 +2,88 @@
 
 # Fail if any command fails
 set -e
-# Don't override existing files
-set -C
 
-TMPFILE=/tmp/check-jni-methods.$$.1
-TMPFILE2=/tmp/check-jni-methods.$$.2
-TMPFILE3=/tmp/check-jni-methods.$$.3
+# The following code taken from the Autoconf manual.
+# Create a temporary directory $tmp in $TMPDIR (default /tmp).
+# Use mktemp if possible; otherwise fall back on mkdir,
+# with $RANDOM to make collisions less likely.
+: ${TMPDIR=/tmp}
+{
+  tmp=`
+    (umask 077 && mktemp -d "$TMPDIR/check-jni-methods-XXXXXX") 2>/dev/null
+  ` &&
+  test -n "$tmp" && test -d "$tmp"
+} || {
+  tmp=$TMPDIR/check-jni-methods-$$-$RANDOM
+  (umask 077 && mkdir "$tmp")
+} || exit $?
 
+TMPFILE=$tmp/headers
+TMPFILE2=$tmp/sources
+TMPFILE3=$tmp/ignore
+
+if echo 'ab*c' | grep -F 'ab*c' >/dev/null 2>&1; then
+  FGREP='grep -F'
+else
+  FGREP=fgrep
+fi
+if echo a | grep -E '(a|b)' >/dev/null 2>&1; then
+  EGREP='grep -E'
+else
+  EGREP=egrep
+fi
+
 # Find all methods defined in the header files generated
 # from the java source files.
-grep -h '^JNIEXPORT .* Java_' include/*.h | \
-        LC_ALL=C sed -e 's,.*JNICALL \(Java_[a-z_A-Z0-9]*\).*$,\1,' | \
+cat include/*.h | grep '^JNIEXPORT .* Java_' |
+        LC_ALL=C sed 's,.*JNICALL \(Java_[a-z_A-Z0-9]*\).*$,\1,' |
 	sort > $TMPFILE
 
 # Find all methods in the JNI C source files.
-find native/jni -name \*.c | \
-	xargs grep -h '^Java_' | \
-        LC_ALL=C sed -e 's,^\(Java_[a-z_A-Z0-9]*\) *(.*$,\1,' > $TMPFILE2
+find native/jni -name \*.c |
+	xargs cat |
+	grep '^Java_' |
+        LC_ALL=C sed 's,^\(Java_[a-z_A-Z0-9]*\) *(.*$,\1,' > $TMPFILE2
 # Or in the the C++ files. (Note that cpp doesn't follow gnu conventions atm)
 # So we try to match both GNU style and some other style.
-find native/jni -name \*.cpp | \
-	xargs grep -h '^Java_' | \
-        LC_ALL=C sed -e 's,^\(Java_[a-z_A-Z0-9]*\) *(.*$,\1,' >> $TMPFILE2
-find native/jni -name \*.cpp | \
-	xargs egrep -h '^(JNIEXPORT .* JNICALL )?Java_' | \
-	cut -f4 -d\  | \
-        LC_ALL=C sed -e 's,^\JNIEXPORT .* JNICALL \(Java_[a-z_A-Z0-9]*\) *(.*$,\1,' >> $TMPFILE2
-mv $TMPFILE2 $TMPFILE3
-sort $TMPFILE3 > $TMPFILE2
-rm $TMPFILE3
+find native/jni -name \*.cpp |
+	xargs cat |
+	grep '^Java_' |
+        LC_ALL=C sed 's,^\(Java_[a-z_A-Z0-9]*\) *(.*$,\1,' >> $TMPFILE2
+find native/jni -name \*.cpp |
+	xargs cat |
+	$EGREP '^(JNIEXPORT .* JNICALL )?Java_' |
+	cut -f4 -d\  |
+        LC_ALL=C sed 's,^\JNIEXPORT .* JNICALL \(Java_[a-z_A-Z0-9]*\) *(.*$,\1,' >> $TMPFILE2
+mv "$TMPFILE2" "$TMPFILE3"
+sort "$TMPFILE3" > $TMPFILE2
+rm "$TMPFILE3"
 
 # Write temporary ignore file.
 cat > $TMPFILE3 << EOF
--Java_gnu_java_awt_peer_gtk_GtkMenuComponentPeer_dispose
--Java_java_lang_VMSystem_arraycopy
--Java_java_lang_VMSystem_identityHashCode
+< Java_gnu_java_awt_peer_gtk_GtkMenuComponentPeer_dispose
+< Java_java_lang_VMSystem_arraycopy
+< Java_java_lang_VMSystem_identityHashCode
 EOF
 
 # Compare again silently.
 # Use fgrep and direct the output to /dev/null for compatibility with older
 # grep instead of using the non portable -q.
-if diff -b -U 0 $TMPFILE $TMPFILE2 | grep '^[+-]Java' | \
-    fgrep -v -f $TMPFILE3 > /dev/null;
+if diff -b "$TMPFILE" "$TMPFILE2" | grep '^[<>] Java' |
+    $FGREP -v -f "$TMPFILE3" > /dev/null
 then
   PROBLEM=1
   echo "Found a problem with the JNI methods declared and implemented."
-  echo "(-) missing in implementation, (+) missing in header files"
+  echo "(<) missing in implementation, (>) missing in header files"
 
   # Compare the found method lists.
-  diff -b -U 0 $TMPFILE $TMPFILE2  | grep '^[+-]Java' | fgrep -v -f $TMPFILE3
+  diff -b "$TMPFILE" "$TMPFILE2"  | grep '^[<>] Java' | $FGREP -v -f "$TMPFILE3"
 fi
 
 # Cleanup.
-rm -f $TMPFILE $TMPFILE2 $TMPFILE3
+rm -rf "$tmp"
 
-if test "$PROBLEM" = "1" ; then
+if test "$PROBLEM" = 1; then
   exit 1
 fi
 


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]