Bug 5215 - [regression] ld/genscripts.sh rev 1.24 breaks on FreeBSD's /bin/sh
Summary: [regression] ld/genscripts.sh rev 1.24 breaks on FreeBSD's /bin/sh
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.18
: P2 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-10-24 07:54 UTC by Joerg Wunsch
Modified: 2007-10-26 10:07 UTC (History)
1 user (show)

See Also:
Host: freebsd-*-*
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joerg Wunsch 2007-10-24 07:54:41 UTC
Revision 1.24 of ld/genscripts.sh introduced the following code:

if test -n "${BASH+set}"; then
  source_em()
  {
    local current_script="$em_script"
    em_script=$1
    . $em_script
    em_script=$current_script
  }
  fragment()
  {
    local lineno=$[${BASH_LINENO[0]} + 1]  ###### <---
    echo >> e${EMULATION_NAME}.c "#line $lineno \"$em_script\""
    cat >> e${EMULATION_NAME}.c
  }

This breaks on FreeBSD's /bin/sh with the following message:

.././ld/genscripts.sh: 403: Syntax error: Bad substitution

(Line 403 is marked with ###### <--- above.)

The issue here is that FreeBSD's /bin/sh simply does not recognize the
*syntax* of the array substitution, so the test for ${BASH} above
doesn't matter at all -- the shell aborts parsing the script at that
point.

So somehow, the test for using a bash should either be moved into a
configure-time substitution, or the array (and math, too?) substitution
should be replaced by something else that can be handled by any flavor
of /bin/sh.

FreeBSD's /bin/sh doesn't choke on the $[] math invoked (it simply doesn't
sustitute anything), yet I figure some other /bin/sh might also consider
that a syntax error.  IMHO it's just riding on the safe side to use plain
old expr(1).
Comment 1 Alan Modra 2007-10-25 12:36:44 UTC
Would /bin/sh be happy with the following?

if test -n "${BASH+set}"; then
  . ${srcdir}/gen.bash
else
...

ie. hide the bash specific syntax in another file?
Comment 2 Joerg Wunsch 2007-10-25 19:52:20 UTC
(In reply to comment #1)

> Would /bin/sh be happy with the following?
> 
> if test -n "${BASH+set}"; then
>   . ${srcdir}/gen.bash
> else

Yes, that appears to work for FreeBSD's /bin/sh.  Currently cannot test
an archaic /bin/sh (like Solaris) though, and it might be wise to at
least try finding out what Posix says about it.