2
0

check_consts.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env bash
  2. temps="./check_const_list.sh"
  3. if [ "$1" == "clean" ] ; then
  4. echo Removing $temps
  5. rm -f $temps
  6. exit
  7. fi
  8. if [ "$1" == "verbose" ] ; then
  9. verbose=1
  10. shift
  11. else
  12. verbose=0
  13. fi
  14. os=`uname -s`
  15. if [ "$os" == "NetBSD" ] ; then
  16. needgsed=1
  17. else
  18. needgsed=0
  19. fi
  20. SED=sed
  21. if [ $needgsed -eq 1 ] ; then
  22. SED=
  23. SED=`which gsed`
  24. if [ "$SED" == "" ] ; then
  25. echo "GNU sed not found, this script might not work optimally"
  26. SED=sed
  27. fi
  28. fi
  29. for file in $@ ; do
  30. echo "Looking for constants in \"$file\""
  31. $SED -n -e "s:.*[[:space:]]\([a-zA-Z_][a-zA-Z_0-9]*\)[[:space:]]*=[[:space:]]*\([&%$]*\)\([-+]*[0-9][xX]*[-0-9+[:space:]]*\)[[:space:]]*;.*:test_const \1 \"\3\" \"\2\" :p" $file > check_const_list.sh
  32. test_const ()
  33. {
  34. name=$1
  35. value="$2"
  36. prefix="$3"
  37. if [[ "x$prefix" = "x\$" ]] ; then
  38. if [ $verbose -eq 1 ]; then
  39. echo "Hexadecimal value"
  40. fi
  41. value=0x$value
  42. fi
  43. if [[ "x$prefix" = "x&" ]] ; then
  44. if [ $verbose -eq 1 ]; then
  45. echo "Octal value"
  46. fi
  47. if ! [ "${value:0:1}" == "0" ] ; then
  48. value=0$value
  49. fi
  50. fi
  51. if [[ "x$prefix" = "x%" ]] ; then
  52. if [ $verbose -eq 1 ]; then
  53. echo "Binary value"
  54. fi
  55. value=0b$value
  56. fi
  57. if [ $verbose -eq 1 ]; then
  58. echo "Looking for $name, should be $value"
  59. fi
  60. ok=0
  61. matchvalue=
  62. source=
  63. namelist=`grep -i -n -r "#[[:space:]]*define[[:space:]]*$name[[:space:]]" /usr/include`
  64. # Remove comments
  65. namelist=${namelist//\/\**/}
  66. # Remove trailing spaces
  67. namelist=${namelist%%[[:space:]]}
  68. if [ -n "$namelist" ]; then
  69. source=${namelist//#define*/}
  70. if [ $verbose -eq 1 ] ; then
  71. echo "Exact match found: \"$namelist\""
  72. fi
  73. matchvalue=`echo ${namelist} |$SED "s|.*#[[:space:]]*define[[:space:]]*$name[[:space:]]*||I" `
  74. if [ $verbose -eq 1 ] ; then
  75. echo "matchvalue=\"$matchvalue\""
  76. fi
  77. matchvalue="${matchvalue#"${matchvalue%%[![:space:]]*}"}"
  78. else
  79. if [ $verbose -eq 1 ] ; then
  80. echo "$name not found"
  81. fi
  82. namelist2=`grep -i $name -r -n /usr/include`
  83. if [ -n "$namelist2" ]; then
  84. echo "Match found: \"$namelist2\""
  85. source=${namelist2//:*/}
  86. matchvalue=${namelist//#define*$name2/}
  87. fi
  88. fi
  89. if [ "$matchvalue" == "$value" ] ; then
  90. echo "OK: Constant \"$name\" value OK: \"$value\" location \"$source\""
  91. else
  92. if [ "$source" != "" ] ; then
  93. echo "Warning: Constant \"$name\" has value \"$value\" inside \"$file\" but \"$matchvalue\" location \"$source\""
  94. else
  95. echo "Warning: Constant \"$name\" has value \"$value\" inside \"$file\" but \"$matchvalue\" in system headers"
  96. fi
  97. fi
  98. }
  99. set -f
  100. . ./check_const_list.sh
  101. done