check_sys.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env bash
  2. # Script to test fpc to system syscall numbers
  3. # Location of syscall header in system
  4. syscall_header=/usr/include/syscall.h
  5. fpc_sysnr=./sysnr.inc
  6. os=`uname -s`
  7. if [ "$os" == "OpenBSD" ] ; then
  8. c_syscall_header=sys/syscall.h
  9. else
  10. c_syscall_header=syscall.h
  11. fi
  12. if ! [ -f $fpc_sysnr ] ; then
  13. cpu=`fpc -iTP`
  14. fpc_sysnr=./$cpu/sysnr.inc
  15. fi
  16. verbose=0
  17. os=`uname -s`
  18. # Test C file to grab all loaded headers
  19. cat > test-syscall.c <<EOF
  20. #include <${c_syscall_header}>
  21. int
  22. main ()
  23. {
  24. return 0;
  25. }
  26. EOF
  27. # Default C compiler is gcc
  28. # Can be overwritten by setting CC variable
  29. # But I don't know if other compilers also generate
  30. # .i files with --save-temps option
  31. if [ "$CC" == "" ] ; then
  32. CC=gcc
  33. fi
  34. # Use gcc with --save-temps option to create .i file
  35. $CC --save-temps -o test-syscall ./test-syscall.c
  36. res=$?
  37. if [ $res -ne 0 ] ; then
  38. echo "Call to $CC failed"
  39. exit
  40. fi
  41. # list of errno.h headers listed
  42. syscall_headers=` sed -n "s:.*\"\(.*/.*\.h\)\".*:\1:p" test-syscall.i |sort | uniq`
  43. echo "Headers found are \"$syscall_headers\""
  44. if [ "$syscall_headers" != "" ] ; then
  45. syscall_header="$syscall_headers"
  46. fi
  47. fpc_syscall_prefix=syscall_nr_
  48. if [ "$os" == "Linux" ] ; then
  49. # On Linux system, system call number are defined indirectly
  50. # with #define SYS_XXX __NR_XXX
  51. # We look directly for the __NT_ version
  52. syscall_prefix=__NR_
  53. else
  54. syscall_prefix=SYS_
  55. fi
  56. # You should only need to change the variables above
  57. sed -n "s:^[ \t]*${fpc_syscall_prefix}\\([_a-zA-Z0-9]*\\)[ \t]*=[ \t]*\\([0-9]*\\).*:check_syscall_number ${syscall_prefix}\1 \2:p" ${fpc_sysnr} > check_sys_list.sh
  58. sed -n "s:^.*define[[:space:]]*${syscall_prefix}\\([_a-zA-Z0-9]*\\)[[:space:]]*\\([0-9]*\\).*:check_syscall_number_reverse ${fpc_syscall_prefix}\1 \2:p" ${syscall_header} > check_sys_list_reverse.sh
  59. function check_syscall_number ()
  60. {
  61. sys=$1
  62. value=$2
  63. obsolete=0
  64. if [[ "$value" =~ ^[0-9]+$ ]] ; then
  65. eval $sys=\$$value
  66. if [ $verbose -ne 0 ] ; then
  67. echo "$sys is $value"
  68. fi
  69. else
  70. eval $sys=$value
  71. if [ $verbose -ne 0 ] ; then
  72. echo "$sys set to \"${$sys}\" trough \"$value\""
  73. fi
  74. fi
  75. # Remember this value for later
  76. eval $sys=$value
  77. if [ $verbose -ne 0 ] ; then
  78. echo Testing $sys value $value
  79. fi
  80. found=`sed -n "/#define[[:space:]]*${sys}[^A-Za-z0-9_]/p" ${syscall_header}`
  81. val=`sed -n "s:#define[[:space:]]*${sys}[^A-Za-z0-9_][^A-Za-z0-9_]*\([0-9]*\).*:\1:p" ${syscall_header}`
  82. if [ $verbose -ne 0 ] ; then
  83. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  84. fi
  85. if [ "${val}" == "${value}" ] ; then
  86. if [ $verbose -ne 0 ] ; then
  87. echo ${sys} value ${val} is correct
  88. fi
  89. else
  90. if [ "${val}" == "" ] ; then
  91. found=`sed -n "/#define.*[^A-Za-z0-9_]${value}$/p" ${syscall_header}`
  92. if [ "${found}" == "" ] ; then
  93. found=`sed -n "s:\/\* ${value} is compa:/* ${value} is compa:p" ${syscall_header}`
  94. if [ "$found" != "" ] ; then
  95. obsolete=1
  96. fi
  97. fi
  98. fi
  99. if [ "$found" == "" ] ; then
  100. found=`grep -n -w $value ${syscall_header}`
  101. fi
  102. if [ $obsolete -eq 1 ] ; then
  103. echo Warning: ${sys} expected ${value}, is obsolete line is \"${found}\"
  104. else
  105. echo Problem: ${sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
  106. fi
  107. fi
  108. }
  109. function check_syscall_number_reverse ()
  110. {
  111. sys=$1
  112. value=$2
  113. if [ $verbose -ne 0 ] ; then
  114. echo Testing syscall header entry $sys value $value
  115. fi
  116. found=`sed -n "/.*${sys}/p" ${fpc_sysnr}`
  117. val=`sed -n "s:.*${sys}[ \t]*=[ \t]*\([0-9]*\).*:\1:p" ${fpc_sysnr}`
  118. if [ $verbose -ne 0 ] ; then
  119. echo Test for $sys found \"${found}\" \"${value}\" \"${val}\"
  120. fi
  121. if [ "${val}" == "${value}" ] ; then
  122. if [ $verbose -ne 0 ] ; then
  123. echo ${sys} value ${val} is correct
  124. fi
  125. else
  126. if [ "${val}" == "" ] ; then
  127. found=`sed -n "/#define.*[^A-Za-z0-9_]${value}$/p" ${syscall_header}`
  128. if [ "${found}" == "" ] ; then
  129. found=`sed -n "s:\/\* ${value} is compa: ${value} is compa:p" ${syscall_header}`
  130. fi
  131. fi
  132. echo Problem: ${sys} expected ${value}, line is \"${found}\", val found is \"${val}\"
  133. fi
  134. }
  135. # Sustitution made to pass from fpc syscall number
  136. # to system define
  137. set -f
  138. echo "Checking values from \"${fpc_sysnr}\" in \"${syscall_header}\""
  139. source ./check_sys_list.sh
  140. echo "Checking if values in \"${syscall_header}\" are missing in \"${fpc_sysnr}\""
  141. source ./check_sys_list_reverse.sh