| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 | #!/usr/bin/env bash# Small script to test fpc to system error numbersaddtoerrno=0errnonew=./errno-new.inctemps="check_errno_list.sh check_reverse_errno_list.sh test-errno* $errnonew"if [ "$1" == "clean" ] ; then  echo "Deleting $temps"  rm -f $temps  exitfiif [ "$1" == "verbose" ] ; then  verbose=1  echo "Being verbose"  shiftelse  verbose=0fiif [ "$1" == "addall" ] ; then  addall=1  echo "Adding all entries to errno-new.inc"  shiftelse  addall=0fi# Location of error number in system headererrno_headers="/usr/include/asm-generic/errno-base.h /usr/include/asm-generic/errno.h"if [ "$1" != "" ] ; then  errno_include=$1  echo "Using $errno_include file"else  errno_include=./errno.incfi# Sustitution made to pass from fpc syscall number# to system define fpc_errno_prefix=ESysEerrno_prefix=E# Test C file to grab all loaded headerscat > test-errno.c <<EOF#include <errno.h>intdummy (){  return 0;}EOF# Default C compiler is gcc# Can be overwritten by setting CC variable# But I don't know if other compilers also generate# .i files with --save-temps optionif [ "$CC" == "" ] ; then  CC=gccfi# Use gcc with --save-temps option to create .i file$CC --save-temps -c ./test-errno.c# list of errno.h headers listederrno_headers_CC=` sed -n "s:.*\"\(.*\.h\)\".*:\1:p" test-errno.i |sort | uniq`echo "Headers found are \"$errno_headers_CC\""if [ "$errno_headers_CC" != "" ] ; then  errno_headers="$errno_headers_CC"fi# You should only need to change the variables abovesed -n "s:^[[:space:]]*${fpc_errno_prefix}\\([_a-zA-Z0-9]*\\)[[:space:]]*=[[:space:]]*\\([0-9][0-9]*\\).*:check_errno_number ${errno_prefix}\1 \2:p" ${errno_include} > check_errno_list.shsed -n "s:#define[[:space:]]*${errno_prefix}\\([_a-zA-Z0-9]*\\)[[:space:]][[:space:]]*\\(-*[0-9A-Za-z_]*\\)[[:space:]]*\(.*\):check_reverse_errno_number ${fpc_errno_prefix}\1 \2 \"\3\":p" ${errno_headers} > check_reverse_errno_list.shfunction rpad {  word="$1"  while [ ${#word} -lt $2 ]; do    word="$word$3";  done;  echo "$word";}function compile_errno (){  errname=$1  errvalue=$2# Test C file to grab all loaded headerscat > test-errno.c <<EOF#include <errno.h>#include <stdio.h>intmain (){  printf ("$errname=%d\n",$errname);  return 0;}EOF$CC -o ./test-errno ./test-errno.ccompiledvalue=`./test-errno`if [ "$compiledvalue" == "$errname=$errvalue" ] ; then  if [ $verbose -ne 0 ]; then    echo "GCC reports $compiledvalue OK"  fielse  echo "GCC reports $compiledvalue, but $errvalue is expected"fi}function check_errno_number (){  sys=$1  value=$2  if [ $verbose -ne 0 ] ; then    echo Testing $sys value $value  fi  # Remember value of this constant  eval ${sys}=${value}  compile_errno $sys $value  found=`sed -n "/#define[[:space:]][[:space:]]*${sys}[^A-Za-z0-9_]/p" ${errno_headers}`  val=`sed -n "s:#define[[:space:]][[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${errno_headers}`  extval=`sed -n "s:#define[[:space:]][[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9A-Za-z_]*\).*:\1:p" ${errno_headers}`  if [ $verbose -ne 0 ] ; then    echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"  fi  if [ "${val}" == "${value}" ] ; then    if [ $verbose -ne 0 ] ; then      echo ${sys} value ${val} is correct    fi  else    if [ $verbose -ne 0 ] ; then      echo "${sys} val=\"$val\", extval=\"$extval\""    fi    if [ "${val}" == "" ] ; then      foundvalue=`sed -n "/#define.*[^A-Za-z0-9_]${value}$/p" ${errno_headers}`      if [ "${foundvalue}" == "" ] ; then        foundvalue=`sed -n "s:\/\* ${value} is compa: ${value} is compa:p" ${errno_headers}`      fi    fi    if [ "$extval" != "$val" ] ; then      eval indirectval=\$$extval      echo "indirectval =\"$indirectval\" for \"$extval\""      if [ "$indirectval" != "$value" ] ; then        echo problem for ${sys} expected ${value}, line is \"${found}\", val found is \"${indirectval}\"      else        echo "$sys is defined as $extval which is $indirectval as expected $value"      fi    else      echo "problem for ${sys} expected ${value}, line is \"${found}\", val found is \"${val}\""    fi  fi}function write_errno_new_head (){  echo "{ File generated by $0" > $errnonew  uname_info=`uname -s -r -m`  echo " generated on \"$uname_info\" machine" >> $errnonew  echo "List of missing system error number found in" >> $errnonew  echo "$errno_headers" >> $errnonew  echo "}" >> $errnonew}function check_reverse_errno_number (){  errname=$1  errvalue=$2  rpaderrname=$(rpad $errname 20 " ")  if ! [[ "$errvalue" =~ ^[0-9]+$ ]] ; then    eval errvalue=\$$errvalue  fi    printf -v padd "%s = %4d" "$rpaderrname" $errvalue  found=`grep -i -w $1 ${errno_include}`  comment="$3"  comment=${comment##\/\*}  comment=${comment%%\*\/}  if [ "${found}" == "" ] ; then    echo "Error ${errname}, value ${errvalue}, not in ${errno_include} file"    if [ $addtoerrno -eq 0 ] ; then      addtoerrno=1      write_errno_new_head    fi    echo "        $padd; { $comment }" >> $errnonew  else    if [ $addall -eq 1 ] ; then      if [ $addtoerrno -eq 0 ] ; then        addtoerrno=1        write_errno_new_head      fi      echo "        $padd; { $comment }" >> $errnonew    fi  fi}set -fecho "Checking all entries of ${errno_include} file"source ./check_errno_list.shecho "Checking all defines from \"$errno_header\""source ./check_reverse_errno_list.shif [ $addtoerrno -eq 1 ] ; then  echo " Missing error number found"  echo " New values are in \"$errnonew\"  echo You might want to add these lines to \"$errno_include\""fi
 |