checksym.awk 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/awk -f
  2. # Check a list of symbols against the master definition
  3. # (official) list. Arguments:
  4. #
  5. # awk -f checksym.awk official-def list-to-check
  6. #
  7. # Output is a file in the current directory called 'symbols.new',
  8. # stdout holds error messages. Error code indicates success or
  9. # failure.
  10. #
  11. # NOTE: this is a pure, old fashioned, awk script. It will
  12. # work with any awk
  13. BEGIN{
  14. err=0
  15. master="" # master file
  16. official[1] = "" # defined symbols from master file
  17. symbol[1] = "" # defined symbols from png.h
  18. removed[1] = "" # removed symbols from png.h
  19. lasto = 0 # last ordinal value from png.h
  20. mastero = 0 # highest ordinal in master file
  21. symbolo = 0 # highest ordinal in png.h
  22. missing = "error"# log an error on missing symbols
  23. }
  24. # Read existing definitions from the master file (the first
  25. # file on the command line.) This must be a def file and it
  26. # has definition lines (others are ignored) of the form:
  27. #
  28. # symbol @ordinal
  29. #
  30. master == "" {
  31. master = FILENAME
  32. }
  33. FILENAME==master && NF==2 && $2~/^@/ && $1!~/^;/ {
  34. o=0+substr($2,2)
  35. if (o > 0) {
  36. if (official[o] == "") {
  37. official[o] = $1
  38. if (o > mastero) mastero = o
  39. next
  40. } else
  41. print master ": duplicated symbol:", official[o] ":", $0
  42. } else
  43. print master ": bad export line format:", $0
  44. err = 1
  45. }
  46. FILENAME==master && $1==";missing" && NF==2{
  47. # This allows the master file to control how missing symbols
  48. # are handled; symbols that aren't in either the master or
  49. # the new file. Valid values are 'ignore', 'warning' and
  50. # 'error'
  51. missing = $2
  52. }
  53. FILENAME==master {
  54. next
  55. }
  56. # Read new definitions, these are free form but the lines must
  57. # just be symbol definitions. Lines will be commented out for
  58. # 'removed' symbols, introduced in png.h using PNG_REMOVED rather
  59. # than PNG_EXPORT. Use symbols.dfn or pngwin.dfn to generate the
  60. # input file.
  61. #
  62. # symbol @ordinal # two fields, exported symbol
  63. # ; symbol @ordinal # three fields, removed symbol
  64. # ; @ordinal # two fields, the last ordinal
  65. NF==2 && $1 == ";" && $2 ~ /^@[1-9][0-9]*$/ { # last ordinal
  66. o=0+substr($2,2)
  67. if (lasto == 0 || lasto == o)
  68. lasto=o
  69. else {
  70. print "png.h: duplicated last ordinal:", lasto, o
  71. err = 1
  72. }
  73. next
  74. }
  75. NF==3 && $1 == ";" && $3 ~ /^@[1-9][0-9]*$/ { # removed symbol
  76. o=0+substr($3,2)
  77. if (removed[o] == "" || removed[o] == $2) {
  78. removed[o] = $2
  79. if (o > symbolo) symbolo = o
  80. } else {
  81. print "png.h: duplicated removed symbol", o ": '" removed[o] "' != '" $2 "'"
  82. err = 1
  83. }
  84. next
  85. }
  86. NF==2 && $2 ~ /^@[1-9][0-9]*$/ { # exported symbol
  87. o=0+substr($2,2)
  88. if (symbol[o] == "" || symbol[o] == $1) {
  89. symbol[o] = $1
  90. if (o > symbolo) symbolo = o
  91. } else {
  92. print "png.h: duplicated symbol", o ": '" symbol[o] "' != '" $1 "'"
  93. err = 1
  94. }
  95. }
  96. {
  97. next # skip all other lines
  98. }
  99. # At the end check for symbols marked as both duplicated and removed
  100. END{
  101. if (symbolo > lasto) {
  102. print "highest symbol ordinal in png.h,", symbolo ", exceeds last ordinal from png.h", lasto
  103. err = 1
  104. }
  105. if (mastero > lasto) {
  106. print "highest symbol ordinal in", master ",", mastero ", exceeds last ordinal from png.h", lasto
  107. err = 1
  108. }
  109. unexported=0
  110. for (o=1; o<=lasto; ++o) {
  111. if (symbol[o] == "" && removed[o] == "") {
  112. if (unexported == 0) unexported = o
  113. if (official[o] == "") {
  114. # missing in export list too, so ok
  115. if (o < lasto) continue
  116. }
  117. }
  118. if (unexported != 0) {
  119. # Symbols in the .def but not in the new file are errors, but
  120. # the 'unexported' symbols aren't in either. By default this
  121. # is an error too (see the setting of 'missing' at the start),
  122. # but this can be reset on the command line or by stuff in the
  123. # file - see the comments above.
  124. if (missing != "ignore") {
  125. if (o-1 > unexported)
  126. print "png.h:", missing ": missing symbols:", unexported "-" o-1
  127. else
  128. print "png.h:", missing ": missing symbol:", unexported
  129. if (missing != "warning")
  130. err = 1
  131. }
  132. unexported = 0
  133. }
  134. if (symbol[o] != "" && removed[o] != "") {
  135. print "png.h: symbol", o, "both exported as '" symbol[o] "' and removed as '" removed[o] "'"
  136. err = 1
  137. } else if (symbol[o] != official[o]) {
  138. # either the symbol is missing somewhere or it changed
  139. err = 1
  140. if (symbol[o] == "")
  141. print "png.h: symbol", o, "is exported as '" official[o] "' in", master
  142. else if (official[o] == "")
  143. print "png.h: exported symbol", o, "'" symbol[o] "' not present in", master
  144. else
  145. print "png.h: exported symbol", o, "'" symbol[o] "' exists as '" official[o] "' in", master
  146. }
  147. # Finally generate symbols.new
  148. if (symbol[o] != "")
  149. print " " symbol[o], "@" o > "symbols.new"
  150. }
  151. if (err != 0) {
  152. print "*** A new list is in symbols.new ***"
  153. exit 1
  154. }
  155. }