#!/bin/sh
#
#  @(#)as.sh 1.10 95/11/07
#
#  Wrapper for PowerPC assembler
#

#
# Variables which are constant
#
VERSION='@(#)RELEASE VERSION WorkShop Compilers 4.2 10/15/96'
ENDIANFLAG='__LITTLE_ENDIAN'
CPP="/usr/ccs/lib/cpp"
M4="/usr/ccs/bin/m4"
tmp1="/tmp/asppc1.$$"
tmp2="/tmp/asppc2.$$"
INVOCATIONNAME="$0"

#
# Variables which depend on options
#
runm4="no"
runcpp="no"
cpp_args="-undef -D${ENDIANFLAG} -Dunix -Dsun -D__unix -D__sun -D__ppc"
m4_args="-D${ENDIANFLAG}"
as_args="-c"
srcfiles=""
lastsrc=""
verbose="no"
dryrun="no"
keeptmp=no
asout=""
retcode=0
if test "LITTLE" = "BIG" ; then
  endianarg=""
else
  endianarg="-le"
fi
passline=no

#
# function to display an error message to stderr and quit
#
error () {
  echo "${INVOCATIONNAME}: error, $*" 1>&2
  retcode=1
  exit 1
}

#
# function to display a warning message to stderr
#
warning () {
  echo "${INVOCATIONNAME}: warning, $*" 1>&2
}

#
# procedure to run a program
#
runpass () {
  if test ${verbose} = "yes" ; then
    echo "$*" 2>&1
  fi
  if test ${dryrun} != "yes" ; then
    eval $*
    retcode=$?
  else
    retcode=0
  fi
  return ${retcode}
}

#
# determine path to actual assembler
#
if test `expr $0 : '\(.\)'` = '/' ; then
  ASPPC=`dirname $0`/asppc
else
  ASPPC=asppc
fi

#
# process arguments
#
while test $# -gt 0 ; do
  case $1 in
  -m)  runm4="yes"
       ;;
  -o)  shift
       asout="$1"
       as_args="${as_args} -o ${asout}"
       ;;
  -P)  runcpp="yes"
       ;;
  -D*) cpp_args="${cpp_args} $1"
       m4_args="${m4_args} $1"
       ;;
  -I*) cpp_args="${cpp_args} $1"
       m4_args="${m4_args} $1"
       ;;
  -U*) cpp_args="${cpp_args} $1"
       m4_args="${m4_args} $1"
       ;;
  -s)  as_args="${as_args} $1"
       ;;
  -xl) passline=yes
       ;;
  -V)  echo "PowerPC assembler wrapper, version ${VERSION}"
       ;;
# unimplemented options, ignore them
  -Q)  true
       ;;
  -Qy) true
       ;;
  -Qn) true
       ;;
# Metaware assembler options
  -big_si) as_args="${as_args} $1"
       ;;
  -be) endianarg=""
       ;;
  -c)  true
       ;;
  -L)  as_args="${as_args} $1"
       ;;
  -l)  as_args="${as_args} $1"
       ;;
  -le) endianarg="-le"
       ;;
  -pa)  as_args="${as_args} $1"
       ;;
# options for debugging this script
  -#)  verbose="yes"
       ;;
  -###) verbose="yes"
       dryrun="yes"
       ;;
  -keeptmp)
       keeptmp=yes
       ;;
  -*)  warning  invalid option $1
       ;;
# source file
   *)  srcfiles="${srcfiles} $1"
       lastsrc=$1
       ;;
  esac
  shift
done
as_args="${as_args} ${endianarg}"

#
# set up input and output files for the various passes
#
if test \( ${runcpp} = "yes" \) -a \( ${runm4} = "yes" \) ; then
  cppin="${srcfiles}"
  cppout="${tmp1}"
  m4in="${cppout}"
  m4out="${tmp2}"
  asin="${m4out}"
elif test ${runcpp} = "yes" ; then
  cppin="${srcfiles}"
  cppout="${tmp1}"
  asin="${cppout}"
elif test ${runm4} = "yes" ; then
  m4in="${srcfiles}"
  m4out="${tmp1}"
  asin="${m4out}"
else
  asin="${srcfiles}"
fi
if test \( X${asout} = X \) -a \( X${lastsrc} != X \) ; then
  asout=`basename $lastsrc .s`.o
  as_args="${as_args} -o ${asout}"
fi

if test ${keeptmp} = yes ; then
  rmtmp=/bin/true
else
  rmtmp="rm -f ${tmp1} ${tmp2}"
fi
trap '${rmtmp} ; exit ${retcode}' 0
trap '${rmtmp} ; exit 1' 1 2 3 15

#
#  Invoke cpp if specified
#
if test ${runcpp} = "yes" ; then
  rm -f ${cppout}
  for f in ${cppin} ; do
    if test ${passline}  = "yes" ; then
      runpass "${CPP} ${cpp_args} ${f} | sed 's/^#[ 	]*\([0-9]\)/#line \1/' >> ${cppout}" || error "execution of cpp failed"
    else
      runpass "${CPP} -P ${cpp_args} ${f} >> ${cppout}" || error "execution of cpp failed"
    fi
  done
fi

#
#  Invoke m4 if specified
#
if test ${runm4} = "yes" ; then
  runpass "${M4} ${m4_args} ${m4in} > ${m4out}" || error "execution of m4 failed"
fi

#
# Invoke the assembler
#
runpass "${ASPPC} ${as_args} ${asin}"
