Index: texi2dvi

===================================================================

--- texi2dvi	(revision 7159)

+++ texi2dvi	(working copy)

@@ -905,38 +905,86 @@

   fi
 }
 
+find_egroup_char()
+{
+  _filename=$1
+  shift
+  while test $# -gt 0; do
+    case "$_filename" in
+      *"${1:0:1}"*)
+	shift;;
+      *)
+	echo "$1"
+	return 0
+	;;
+    esac
+  done
+  return -1
+}
+
 # run_tex - Run TeX as "$tex $in_input", taking care of errors and logs.
 run_tex ()
 {
   make_tex_cmd
 
-  tex_cmd=`(LC_ALL=C
-    if (echo "$in_input" | grep [^a-zA-Z0-9/.]) >/dev/null ; then
-      # If the filename has funny characters, change the TeX category codes of 
-      # some characters within a group, and use \expandafter to input the file
-      # outside of the group.
+  _run_tex_in_input="$in_input"
+  _run_tex_in_input_funnies=`echo "$in_input" | $SED 's![^} #$%&^_{~]!!g'`
+  if test -n "$_run_tex_in_input_funnies" ; then
+    # If the filename has funny characters, change the TeX category codes of 
+    # some characters within a group, and use \expandafter to input the file
+    # outside of the group.
 
-      # Make > an end group character, as it's unlikely to appear in
-      # a filename.
-      tex_cmd="$tex_cmd '${escape}bgroup${escape}catcode62=2'"
-      # make letter {   }   $  #  ~   ^   &
-      for w in      123 125 36 35 126 136 26; do
-        # TODO: check if letter actually in filename in order
-        # to simplifiy TeX command line.
-        tex_cmd="$tex_cmd '${escape}catcode$w=11${escape}relax'"
-      done
-      # Set \toks0 to "\input FILENAME\relax"
-      tex_cmd="$tex_cmd'${escape}toks0${escape}bgroup"
-      tex_cmd="$tex_cmd${escape}input $in_input${escape}relax>"
-      tex_cmd="$tex_cmd${escape}expandafter${escape}egroup"
-      tex_cmd="$tex_cmd${escape}the${escape}toks0${escape}relax'"
-    else
-      # In the case of a simple filename, just pass the filename
-      # with no funny tricks.
-      tex_cmd="$tex_cmd '${escape}input' '$in_input'"
+    
+
+    tex_cmd="$tex_cmd '${escape}bgroup'"
+
+    # seek for _run_tex_egroup_char as a character for egroup (catcode 2)
+    _run_tex_egroup_char=
+    # make other TeX special characters
+    for w in '}125' ' 32' '#35' '$36' %37 '&38' ^94 _95 '{123' '~126'; do
+      case "$_run_tex_in_input" in
+	*"${w:0:1}"*)
+          tex_cmd="$tex_cmd '${escape}catcode${w:1}=12${escape}relax'"
+	  ;;
+	*)
+	  if test -z "$_run_tex_egroup_char" -a "$w" != ' 32'; then
+	    _run_tex_egroup_char="$w"
+	  fi
+	  ;;
+      esac
+    done
+
+    # If the previous loop has failed to find an egroup character we
+    # try a few more, which don't have any special TeX meaning, but
+    # are unlikely in a file name. This is sure to succeed under MSW
+    # as > < | and " are forbidden in file names.
+    if test -z "$_run_tex_egroup_char"; then
+      _run_tex_egroup_char=`find_egroup_char "$_run_tex_in_input" '>62' '<60' '|124' '/47' '"34'`
     fi
-    echo "$tex_cmd")`
+    if test "$_run_tex_egroup_char" != '}125'; then
+      tex_cmd="$tex_cmd '${escape}catcode${_run_tex_egroup_char:1}=2${escape}relax'"
+    fi
+    
+    # Some explicit escaping is needed for the when the FILENAME
+    # contains a ^^XX quadrigram or a simple quote
+    _run_tex_in_input=`echo "$_run_tex_in_input" | \
+$SED 's!\\^\\^\\([0-9A-F]\\{2\\}\\)!\\\\string^\\\\string^\\1!g; \
+s!\\('"'"'\\+\\)!'"'"'"\\1"'"'"'!g'`
+    
+    # Set \toks0 to "\input FILENAME\relax"
+    tex_cmd="$tex_cmd '${escape}toks0${escape}bgroup"
+    tex_cmd="$tex_cmd${escape}input' '$_run_tex_in_input' '${escape}relax${_run_tex_egroup_char:0:1}"
+    tex_cmd="$tex_cmd${escape}expandafter${escape}egroup"
+    tex_cmd="$tex_cmd${escape}the${escape}toks0${escape}relax'"
+  else
+    # Some sh trick is needed in case that the filename contains a simple quote
+    _run_tex_in_input=`echo "$_run_tex_in_input" | $SED 's!\\('"'"'\\+\\)!'"'"'"\\1"'"'"'!g'`
 
+    # In the case of a simple filename, just pass the filename with no
+    # TeX funny tricks.
+    tex_cmd="$tex_cmd '${escape}input' '$_run_tex_in_input'"
+  fi
+  
   verbose "$0: Running $tex_cmd ..."
   if eval "$tex_cmd" >&5; then
     case $out_lang in
