checksym.awk 5.6 KB

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