#!/bin/ksh
#      *********************************************************
#      *                                                       *
#      * Script to investigate the elements of IRIS structures *
#      *                                                       *
#      *********************************************************
#
#            COPYRIGHT (c) 1994, 1997, 2000, 2002, 2004 BY
#         SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
# 
# THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
# ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
# INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
# COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
# OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
# TRANSFERED.
#
#
alias exitcmd="exit 0"

TMP1=/tmp/$$_tmp1 ; TMPC=/tmp/$$_tmp.c ; TMPX=/tmp/$$_tmpx
##TMP1=tmp1 ; TMPC=tmp.c ; TMPX=tmpx

trap "rm -f ${TMP1} ${TMPC} ${TMPX} ; exitcmd" INT EXIT
rm -f ${TMP1} ${TMPC} ${TMPX}

LOCALDIR="`pwd`"
INCLUDELOCALS="`ls -1 *.h 2>&-`"

INCLUDEWORLDS="sigtypes.h
               projection.h
	       dsp.h
	       setup.h
	       ascope.h
	       headers.h
	       sig_rtdisp.h
	       iris_task.h
	       ingest.h
	       product.h
	       output.h
	       share.h
	       uf.h
	       rda.h
	       rcp8.h
	       rvp8.h
	       intelipp_lib.h
	       user_lib.h
	       antenna_lib.h
	       dsp_lib.h
	       pcicards_lib.h
	       softplane_lib.h
	       rdasubs_lib.h
	       nexrad.h"

INCLUDESINGLES="rvp8main.h
	        rvp8proc.h
	        rcp8main.h"

MACHINE=`uname -s`
MACHINE=`sig_uname_filter $MACHINE`

# ------------------------------
# Process command line options
#
ARGLIST=""

PACK="1"
SCAN="false"
INCLUDE="include"
ERRFLAGS="1"
DATA="0"
DIMDATA="1"
OFFSET="0"
SUBNAME=""
RECURSIVE="0"

if [ "$1" = "" ] ; then HELP="true" ; else HELP="false" ; fi

while [ ! "$1" = "" ] ; do
  ARG="$1" ; shift

  if [ "${ARG#-}" = "${ARG}" ] ; then
    ARGLIST="${ARGLIST} ${ARG}"

  else
    if [ "${ARG}" = "-help" ] ; then
      HELP="true"

    elif [ "${ARG}" = "-scan" ] ; then
      SCAN="true" ; SCANLOCAL="false"

    elif [ "${ARG}" = "-scanlocal" ] ; then
      SCAN="true" ; SCANLOCAL="true"

    elif [ "${ARG}" = "-noflags" -o \
           "${ARG}" = "-noflag"  -o \
           "${ARG}" = "-nofla"   -o \
           "${ARG}" = "-nofl"    -o \
           "${ARG}" = "-nof"     ] ; then
      ERRFLAGS="0"

    elif [ "${ARG}" = "-data" ] ; then
      DATA="1"

    elif [ "${ARG}" = "-recursive" -o \
           "${ARG}" = "-recursiv"  -o \
           "${ARG}" = "-recursi"   -o \
           "${ARG}" = "-recurs"    -o \
           "${ARG}" = "-recur"     -o \
           "${ARG}" = "-recu"      -o \
           "${ARG}" = "-rec"       ] ; then
      RECURSIVE="1"

    elif [ "${ARG}" = "-include" -o \
           "${ARG}" = "-includ"  -o \
           "${ARG}" = "-inclu"   -o \
           "${ARG}" = "-incl"    -o \
           "${ARG}" = "-inc"     ] ; then
      if [ "$1" = "" ] ; then
        echo "Missing include file specification" ;
        alias exitcmd="exit 1" ; exit
      else
        INCLUDE="$1" ; shift
      fi

    elif [ "${ARG}" = "-dimension" -o \
           "${ARG}" = "-dimensio"  -o \
           "${ARG}" = "-dimensi"   -o \
           "${ARG}" = "-dimens"    -o \
           "${ARG}" = "-dimen"     -o \
           "${ARG}" = "-dime"      -o \
           "${ARG}" = "-dim"       ] ; then
      if [ "$1" = "" ] ; then
        echo "Missing dimension specification" ;
        alias exitcmd="exit 1" ; exit
      else
        DIMDATA="$1" ; shift
      fi

    elif [ "${ARG}" = "-offset" -o \
           "${ARG}" = "-offse"  -o \
           "${ARG}" = "-offs"   -o \
           "${ARG}" = "-off"    ] ; then
      if [ "$1" = "" ] ; then
        echo "Missing offset specification" ;
        alias exitcmd="exit 1" ; exit
      else
        OFFSET="$1" ; shift
      fi

    elif [ "${ARG}" = "-nopack" ] ; then
      PACK="0"

    elif [ "${ARG}" = "-subname" ] ; then
      if [ "$1" = "" ] ; then
        echo "Missing subname specification" ;
        alias exitcmd="exit 1" ; exit
      else
        SUBNAME="$1" ; shift
      fi

    else
      echo "Unknown option: '${ARG}'" ; HELP="true"
    fi
  fi
done

if [ "${HELP}" = "true" ] ; then
  echo ""
  echo "Command Line Options:"
  echo "   <struct name> : Display internal contents of IRIS structure(s)"
  echo "  -include <dir> : Override default 'include' directory name"
  echo "         -nopack : Force no packing of structure elements"
  echo "           -scan : Produce list of all defined structures"
  echo "      -scanlocal : Like 'scan', but do local directory only"
  echo "        -noflags : Suppress error flags in output"
  echo "      -recursive : Descend into substructures"
  echo "           -data : Show numeric data read from std.input"
  echo "    -dimension N : Use with '-data' for N-dimensional printout"
  echo ""
  echo 'E.G.,  "structmap `structmap -scan`"  displays everything'
  exit
fi

PACKPRAGMA="" ; PACKOPTION=""
if [ "${PACK}" = "1" ] ; then
  if [ "${MACHINE}" = 'HP-UX' ] ; then
    PACKPRAGMA=""
  elif [ "${MACHINE}" = 'AIX' ]  ; then
    PACKPRAGMA="#pragma options align=packed"
  elif [ "${MACHINE}" = 'Linux' ]  ; then
    PACKOPTION="-fpack-struct"
  else
    if [ "${SUBNAME}" = "" ] ; then
      echo "Warning: -nopack is forced on ${MACHINE}..."
    fi
  fi
fi

# ==================================================
# Scan for list of all defined structures
# ==================================================
#
if [ "${SCAN}" = "true" ] ; then

  if [ "${SCANLOCAL}" = "true" ] ; then
    INCLUDES="${INCLUDELOCALS}"
  else
    INCLUDES="${INCLUDEWORLDS} ${INCLUDELOCALS} ${INCLUDESINGLES}"
  fi

  for ITEM in ${INCLUDES} ; do

    FILE="${LOCALDIR}/${ITEM}"
    if [ ! -r ${FILE} ] ; then
      FILE="${HOME}/sigmet/${INCLUDE}/${ITEM}"
      if [ ! -r ${FILE} ] ; then
        FILE="${IRIS_ROOT}/${INCLUDE}/${ITEM}"
        if [ ! -r ${FILE} ] ; then
          FILE="${INCLUDE}/${ITEM}"
          if [ ! -r ${FILE} ] ; then
            echo "Could not locate '...${INCLUDE}/${ITEM}'" ;
            alias exitcmd="exit 1" ; exit
          fi
        fi
      fi
    fi

    egrep "^struct[ 	]+[a-zA-Z0-9_]*$|^struct[ 	]+[a-zA-Z0-9_]*[ 	]" \
      ${FILE} | /lib/cpp -P | tr "\{\};" "   " | sed "s/^struct//" |
      tr -d " 	" >> ${TMP1}

  done

  sort ${TMP1} ; exit
fi

# ==================================================
# Print internal contents of structure(s)
# ==================================================
# Locate the structure within one of the include files.  Build up
# a list of include files that will have to be used in the C
# compilation later.
#
for STRUCT in ${ARGLIST} ; do
  FILELIST="" ; SINGLES="false"
  for ITEM in ${INCLUDEWORLDS} ${INCLUDELOCALS} __SINGLES__ ${INCLUDESINGLES} ; do

    if [ "${ITEM}" = "__SINGLES__" ] ; then SINGLES="true"
    else
      FILE="${LOCALDIR}/${ITEM}"
      if [ ! -r ${FILE} ] ; then
        FILE="${HOME}/sigmet/${INCLUDE}/${ITEM}"
        if [ ! -r ${FILE} ] ; then
          FILE="${IRIS_ROOT}/${INCLUDE}/${ITEM}"
          if [ ! -r ${FILE} ] ; then
            FILE="${INCLUDE}/${ITEM}"
            if [ ! -r ${FILE} ] ; then
              echo "Could not locate '...${INCLUDE}/${ITEM}'" ;
              alias exitcmd="exit 1" ; exit
            fi
          fi
        fi
      fi

      MATCH=`egrep "^struct[ 	]+${STRUCT}$|^struct[ 	]+${STRUCT}[ 	]" ${FILE} |
         head -1 | tr '#\{\}\[\]()^*/$' '...........'`

      if [ "${MATCH}" != "" ] ; then
        FILELIST="${FILELIST} ${FILE}"
        break
      else
        if [ "${SINGLES}" = "false" ] ; then FILELIST="${FILELIST} ${FILE}" ; fi
      fi
    fi
  done

  if [ "${MATCH}" = "" ] ; then
    echo "Could not locate structure '${STRUCT}'" ; continue
  fi

  # ------------------------------
  # Extract the body of the structure.  This results in a clean listing
  # of the structure contents, one item per line.  Give up immediately
  # if there is a semicolon right on this line (it's probably an opaque
  # structure).
  #
  if [ "" = "${MATCH#*;*}" ] ; then
    echo ""
    echo "===== struct ${STRUCT} from ${FILE} =====" ;
    echo "  (Opaque Structure)"
    continue ;
  fi

  sed "1,/^${MATCH}$/d" < ${FILE} | sed -n "1,/} *;/P" | /lib/cpp -P |
    sed "s/ *\[.*\]/\[\]/g" | tr -d "\n" | tr ",\{\}	;" "    \n" |
    egrep -v "^ *$" | sed -e "s/  */ /g" -e "s/^ *//" > ${TMP1}

  # ------------------------------
  # Make up a list of all elements within the structure.  This
  # consists of just the names of the elements, plus any square
  # brackets for dimensions.
  #
  BUILTINS="^volatile$|^const$|^void$|^signed$|^unsigned$|^char$|^int$|^long$|^short$|^float$|^double$"

  TYPEDEFS="^SINT1$|^UINT1$|^SINT2$|^UINT2$|^SINT4$|^UINT4$|^FLT4$|^FLT8$|^BIN2$|^BIN4$|^MESSAGE$|^VTV$|^PSI$|^SPSI$|^PCF$|^TCF$|^FILE$|^pthread_.*$|^Ipp.*$"

  ELEMENTS=`
    sed "s/struct [A-Za-z0-9_]*//g" < ${TMP1} | sed "s/union [A-Za-z0-9_]*//g" |
    tr -s " " "\n" | egrep -v "^[ 	]*$" | egrep -v "${BUILTINS}|${TYPEDEFS}"`

  if [ "${ELEMENTS}" != "${ELEMENTS#*\(*}" ] ; then
    echo ""
    echo "===== struct ${STRUCT} from ${FILE} =====" ;
    echo "  (STRUCTMAP can't yet handle function pointers)"
    continue ;
  fi

  # ------------------------------
  # Compose the C file to print the results
  #
  rm -f ${TMPC}

  # Start with list of include files
  #
  echo "#include <stdio.h>"    >> ${TMPC}
  echo "#include <stdlib.h>"   >> ${TMPC}
  echo "#include <string.h>"   >> ${TMPC}
  echo "#include <unistd.h>"   >> ${TMPC}
  echo "#include <ctype.h>"    >> ${TMPC}
  echo "#include <termios.h>"  >> ${TMPC}
  echo "#include <setjmp.h>"   >> ${TMPC}

  echo "#define INCLUDE_STDIO_PROTOTYPES   1"  >> ${TMPC}
  echo "#define INCLUDE_TERMIOS_PROTOTYPES 1"  >> ${TMPC}

  if [ -r "/usr/include/pthread.h" ] ; then
    echo "#include <pthread.h>"  >> ${TMPC}
    echo "#define INCLUDE_PTHREAD_PROTOTYPES 1"  >> ${TMPC}
  fi

  for ITEM in ${FILELIST} ; do
    if [ "${PACKPRAGMA}" != "" ] ; then echo ${PACKPRAGMA} >> ${TMPC} ; fi
    echo "#include \"${ITEM}\"" >> ${TMPC}
  done

  # Fixed header text and declarations
  #
  cat >> ${TMPC} <<EOF

static int lerrflags_c  = ${ERRFLAGS} ;
static int lhasflt8_c   = 0 ;
static int ldata_c      = ${DATA} ;
static int idimdata_c   = ${DIMDATA} ;
static int ioffset_c    = ${OFFSET} ;
static int lrecursive_c = ${RECURSIVE} ;
static int lpack_c      = ${PACK} ;
static const char *subname_c  = "${SUBNAME}" ;

static UINT1 lStructSizeError
( const char *type_a, int offset_a, int unitsize_a ) ;

static void show_element
( const char *type_a, const char *printname_a, const char *name_a,
  const char *subname_a, int size_a, int unitsize_a, int offset_a ) ;

static struct ${STRUCT} item_c ;

int main( int argc_a, char **argv_a ) {
  int size, unitsize, offset, idimdata=0 ;
  const char *prefix ; char subname[160] ;

again:
  if( ldata_c ) {
    if( sizeof(item_c) != read( STDIN_FILENO, &item_c, sizeof(item_c) ) ) {
      printf( "Could not read %d bytes into '${STRUCT}' from standard input\n",
        sizeof(item_c) ) ; exit(1) ;
    }
  }

  if( idimdata_c > 1 ) sprintf( subname, "%s[%d]", subname_c, idimdata ) ;
  else                 sprintf( subname, "%s"    , subname_c           ) ;

  printf( "\n" ) ;
  if( subname[0]   ) printf( "%s ", subname ) ;
  if( subname_c[0] ) printf( "(struct ${STRUCT})\n" ) ;
  else               printf( "===== struct ${STRUCT} from ${FILE} =====\n\n" ) ;
EOF

  # Process each of the elements in the structure
  #
  for ELEMENT in ${ELEMENTS} ; do
    PRINTNAME=`echo ${ELEMENT} | sed "s/^\*//"`
    NAME=`echo ${PRINTNAME} | sed "s/\[\]//g"`
    XXX="" ; TYPE=""

    # Figure out what type of item this is.  This is tricky because the
    # declaration may have been done with a comma-delimited-list in the
    # original code.  Therefore, based on the current name, lookup the
    # line that defined that name, and extract out the relevant prefix.
    # Also, put in asterisks if we have a pointer type.
    #
    REGEXP="" ; LINE=""
    while [ "${LINE}" = "" ] ; do
      LINE=`egrep "${NAME}\[*\]* *${REGEXP}$" ${TMP1}`
      REGEXP="${REGEXP} [^ ]*"
    done
    for ITEM in `echo "${LINE}"` ; do
      if [ "${ITEM}" = "struct"   -o \
           "${ITEM}" = "union"    -o \
           "${ITEM}" = "volatile" -o \
           "${ITEM}" = "const"    -o \
           "${ITEM}" = "unsigned" -o \
           "${ITEM}" = "signed"    ] ; then TYPE="${TYPE} ${ITEM}" ;
      else
        if [ "${XXX}" = "" ] ; then TYPE="${TYPE} ${ITEM}" ; XXX="X"
        else
          if [ "${ITEM#\*}" != "${ITEM}" ] ; then TYPE="${TYPE} *" ; fi
          break
        fi
      fi
    done

    # If the type ends with a "*", then make sure it is a " *".  Also
    # remove any leading space.
    #
    if [ "${TYPE}" != "${TYPE%\*}" ] ; then
      if [ "${TYPE}" = "${TYPE% \*}" ] ; then TYPE="${TYPE%\*} *" ; fi
    fi
    TYPE="${TYPE# }"

    echo ""                                                               >> ${TMPC}
    echo "  size = sizeof(item_c.${NAME}) ;"                              >> ${TMPC}
    echo "  unitsize = sizeof(${TYPE}) ;"                                 >> ${TMPC}
    echo "  offset = ((int)&item_c.${NAME}) - (int)&item_c ;"             >> ${TMPC}

    echo "  show_element( \"${TYPE}\", \"${PRINTNAME}\", \"${NAME}\",
                 subname, size, unitsize, offset ) ;"                     >> ${TMPC}
  done

  # Append the fixed ending code.  A lot of interesting stuff is coded
  # in this section.
  #
  cat >> ${TMPC} <<EOF

  if( ++idimdata < idimdata_c ) {
    ioffset_c += sizeof(item_c) ; goto again ;
  }
  /* Print statistics for the overall structure
   */
  prefix="   " ;
  if( lerrflags_c ) {
    if( lStructSizeError( "struct ${STRUCT}", 0, sizeof(item_c) ) ) prefix="***" ;
    else {
      if( 0 /**lhasflt8_c**/ ) { if( sizeof(item_c) % 8 ) prefix = "***" ; }
      else                     { if( sizeof(item_c) % 4 ) prefix = "(?)" ; }
    }
  }
  printf( "%s%7d", prefix, (sizeof(item_c) + ioffset_c) ) ;
  if( lrecursive_c ) {
    if( idimdata_c > 1 ) printf( " (End array of struct ${STRUCT})" ) ;
    else                 printf( " (End of struct ${STRUCT})"       ) ;
  }
  printf( "\n" ) ;

  exit(0) ;
}

/* ==================================================
 * Routine to check whether there is an error in the size or alignment of
 * a structure whose name contains a clue as to where it must be placed.
 */
static UINT1 lStructSizeError
( const char *type_a, int offset_a, int unitsize_a )
{
  /* Check whether this is a structure whose name ends in 2, 4, or 8, but
   * for which that number is an isolated numeric suffix (i.e., not part
   * of some larger numeric suffix).
   */
  int lError = 0 ;
  if( strncmp( type_a, "struct ", 7 ) == 0 ) {
    int iLen = strlen( type_a ) ;
    if( ! isdigit( type_a[iLen-2] ) ) {
      int iAlign = 0 ;
      switch( type_a[iLen-1] ) {
        case '2': iAlign = 2 ; break ;
        case '4': iAlign = 4 ; break ;
        case '8': iAlign = 8 ; break ;
      }
      if( iAlign )
        lError = ((offset_a   % iAlign) != 0) ||
                 ((unitsize_a % iAlign) != 0) ;
    }
  }
  return( lError ) ;
}

/* ==================================================
 * Routine to show the properties of a single element.  That element
 * may be a simple scalar, or a substructure.
 */
static void show_element
( const char *type_a, const char *printname_a, const char *name_a,
  const char *subname_a, int size_a, int unitsize_a, int offset_a )
{
  char stext[160] ;
  int icol, idim = size_a / unitsize_a ; const char *prefix ;

  /* The detection of alignment errors is somewhat complicated by the use of
   * typedefs, i.e., we can not easily distinguish between a scalar definition
   * such as 'UINT1', and a structure definition such as 'PSI'.  Therefore,
   * unless, this is an obvious 8-byte type, assume that items of four bytes
   * or less are scalars and must be aligned on their unitsize, and larger
   * items are structures and must be aligned on multiples of four bytes.
   */
  prefix="   " ;
  if( lerrflags_c ) {
    if( (strcmp( type_a, "double" ) == 0) ||
        (strcmp( type_a, "FLT8"   ) == 0) ) {

      lhasflt8_c = 1 ;
      if( offset_a % unitsize_a ) prefix = "***" ;

    } else {
      if( lStructSizeError( type_a, offset_a, unitsize_a ) ) prefix="***" ;
      else {
        if( (unitsize_a <= 4) && (offset_a % unitsize_a) ) prefix="***" ;
        if( (unitsize_a >  4) && (offset_a % 4         ) ) prefix="(?)" ;
      }
    }
  }

  if( size_a != (idim * unitsize_a)) prefix="DIM" ;

  sprintf( stext, "%s%7d %7d %7d  %s %s",
           prefix, (offset_a + ioffset_c), size_a, idim, type_a, printname_a ) ;
  printf( "%s", stext ) ; icol = strlen( stext ) ;

  /* Optionally descend recursively into substructures.
   */
  if( lrecursive_c ) {
    if( (strncmp( type_a, "struct ", 7 ) == 0    ) &&
        ( strchr( type_a, '*'          ) == NULL ) ) {
      void * pelement = offset_a + (char *)&item_c ; FILE *fp ;

      printf( "\n" ) ; fflush(stdout) ;

      sprintf( stext, "structmap %s -dimension %d -offset %d -subname ",
        &type_a[7], idim, (offset_a + ioffset_c) ) ;
      if( subname_a[0] ) { strcat( stext, subname_a ) ; strcat( stext, "." ) ; }
      strcat( stext, name_a ) ;
      if(   ldata_c      ) strcat( stext, " -data"      ) ;
      if(   lrecursive_c ) strcat( stext, " -recursive" ) ;
      if( ! lerrflags_c  ) strcat( stext, " -noflags"   ) ;
      if( ! lpack_c      ) strcat( stext, " -nopack"    ) ;

      if( NULL == (fp = popen( stext, "w" ) ) ) {
        printf( "Error in recursive call to 'structmap'\n" ) ; exit(1) ;
      } else {
        fwrite( pelement, unitsize_a, idim, fp ) ; pclose(fp) ;
      }
    }
  }

  /* Optionally print the numeric data from the structure itself.
   */
  if( ldata_c ) {
    int index ;
    for( index=0 ; index < idim ; index++ ) {
      void * pelement = (index * unitsize_a) + offset_a + (char *)&item_c ;

      if(        (strcmp( type_a, "double" ) == 0) ||
                 (strcmp( type_a, "FLT8"   ) == 0) ) {
        sprintf( stext, "%f", *(FLT8 *)pelement ) ;

      } else if( (strcmp( type_a, "float" ) == 0) ||
                 (strcmp( type_a, "FLT4"  ) == 0) ) {
        sprintf( stext, "%f", *(FLT4 *)pelement ) ;

      } else if( (strcmp( type_a, "signed int" ) == 0) ||
                 (strcmp( type_a, "int"        ) == 0) ||
                 (strcmp( type_a, "SINT4"      ) == 0) ||
                 (strcmp( type_a, "BIN4"       ) == 0) ) {
        sprintf( stext, "%d", *(SINT4 *)pelement ) ;

      } else if( (strcmp( type_a, "unsigned int" ) == 0) ||
                 (strcmp( type_a, "UINT4"        ) == 0) ) {
        sprintf( stext, "%d", *(UINT4 *)pelement ) ;

      } else if( (strcmp( type_a, "signed short" ) == 0) ||
                 (strcmp( type_a, "short"        ) == 0) ||
                 (strcmp( type_a, "BIN2"         ) == 0) ||
                 (strcmp( type_a, "SINT2"        ) == 0) ) {
        sprintf( stext, "%d", *(SINT2 *)pelement ) ;

      } else if( (strcmp( type_a, "unsigned short" ) == 0) ||
                 (strcmp( type_a, "UINT2"          ) == 0) ) {
        sprintf( stext, "%d", *(UINT2 *)pelement ) ;

      /* Character handling includes optional printout of the ASCII
       * printing character, if that is well defined.
       */
      } else if( (strcmp( type_a, "signed char"   ) == 0) ||
                 (strcmp( type_a, "unsigned char" ) == 0) ||
                 (strcmp( type_a, "char"          ) == 0) ||
                 (strcmp( type_a, "SINT1"         ) == 0) ||
                 (strcmp( type_a, "UINT1"         ) == 0) ) {
        int sprint = 0x7F & (int)*(char *)pelement ;

        /* Try not to print anything for structure pads
         */
        if( (strncmp( name_a, "ipad", 4 ) == 0) && (
              (name_a[4] == '_') || (name_a[4] == '\0') ||
              ((name_a[4] >= '0') && (name_a[4] <= '9')) ) ) break ;
        if( (strncmp( name_a, "pad" , 3 ) == 0) && (
              (name_a[3] == '_') || (name_a[3] == '\0') ||
              ((name_a[3] >= '0') && (name_a[3] <= '9')) ) ) break ;

        if( ( sprint < ' ') || (sprint >= 0x7F) )
          sprintf( stext, "%d"    , *(UINT1 *)pelement ) ;
        else
          sprintf( stext, "%d(%c)", *(UINT1 *)pelement, sprint ) ;

      } else break ;

      if( index == 0 ) { printf( " =" ) ; icol += 2 ; }
      if( (icol + strlen(stext)) >= 88 ) {
        int isp ; printf( "\n" ) ;
        icol = 29 ; for( isp=0 ; isp < icol ; isp++ ) printf( " " ) ;
      }
      printf( " %s", stext ) ; icol += (1 + strlen(stext)) ;
    }
  }
  printf( "\n" ) ;
}
EOF

  # Finally, compile and run the C program.
  #
  rm -f ${TMPX} ;
  ${CXX} ${C_OPT} ${PACKOPTION} -D${MACH_OS} -DSTRUCTMAP -o ${TMPX} ${TMPC}
  if [ "$?" = "0" -a -x ${TMPX} ] ; then
    ${TMPX} ; alias exitcmd="exit $?"
  else
    echo "C Compiler errors for '${STRUCT}'" ; alias exitcmd="exit 1"
  fi

  rm -f ${TMP1} ${TMPC} ${TMPX}

done
