checktab.awk 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Check tz tables for consistency.
  2. # %W%
  3. # Contributed by Paul Eggert.
  4. BEGIN {
  5. FS = "\t"
  6. if (!iso_table) iso_table = "iso3166.tab"
  7. if (!zone_table) zone_table = "zone.tab"
  8. if (!want_warnings) want_warnings = -1
  9. while (getline <iso_table) {
  10. iso_NR++
  11. if ($0 ~ /^#/) continue
  12. if (NF != 2) {
  13. printf "%s:%d: wrong number of columns\n", \
  14. iso_table, iso_NR >>"/dev/stderr"
  15. status = 1
  16. }
  17. cc = $1
  18. name = $2
  19. if (cc !~ /^[A-Z][A-Z]$/) {
  20. printf "%s:%d: invalid country code `%s'\n", \
  21. iso_table, iso_NR, cc >>"/dev/stderr"
  22. status = 1
  23. }
  24. if (cc <= cc0) {
  25. if (cc == cc0) {
  26. s = "duplicate";
  27. } else {
  28. s = "out of order";
  29. }
  30. printf "%s:%d: country code `%s' is %s\n", \
  31. iso_table, iso_NR, cc, s \
  32. >>"/dev/stderr"
  33. status = 1
  34. }
  35. cc0 = cc
  36. if (name2cc[name]) {
  37. printf "%s:%d: `%s' and `%s' have the sname name\n", \
  38. iso_table, iso_NR, name2cc[name], cc \
  39. >>"/dev/stderr"
  40. status = 1
  41. }
  42. name2cc[name] = cc
  43. cc2name[cc] = name
  44. cc2NR[cc] = iso_NR
  45. }
  46. zone_table = "zone.tab"
  47. cc0 = ""
  48. while (getline <zone_table) {
  49. zone_NR++
  50. if ($0 ~ /^#/) continue
  51. if (NF != 3 && NF != 4) {
  52. printf "%s:%d: wrong number of columns\n", \
  53. zone_table, zone_NR >>"/dev/stderr"
  54. status = 1
  55. }
  56. cc = $1
  57. coordinates = $2
  58. tz = $3
  59. comments = $4
  60. if (cc < cc0) {
  61. printf "%s:%d: country code `%s' is out of order\n", \
  62. zone_table, zone_NR, cc >>"/dev/stderr"
  63. status = 1
  64. }
  65. cc0 = cc
  66. if (tz2cc[tz]) {
  67. printf "%s:%d: %s: duplicate TZ column\n", \
  68. zone_table, zone_NR, tz >>"/dev/stderr"
  69. status = 1
  70. }
  71. tz2cc[tz] = cc
  72. tz2comments[tz] = comments
  73. tz2NR[tz] = zone_NR
  74. if (cc2name[cc]) {
  75. cc_used[cc]++
  76. } else {
  77. printf "%s:%d: %s: unknown country code\n", \
  78. zone_table, zone_NR, cc >>"/dev/stderr"
  79. status = 1
  80. }
  81. if (coordinates !~ /^[-+][0-9][0-9][0-5][0-9][-+][01][0-9][0-9][0-5][0-9]$/ \
  82. && coordinates !~ /^[-+][0-9][0-9][0-5][0-9][0-5][0-9][-+][01][0-9][0-9][0-5][0-9][0-5][0-9]$/) {
  83. printf "%s:%d: %s: invalid coordinates\n", \
  84. zone_table, zone_NR, coordinates >>"/dev/stderr"
  85. status = 1
  86. }
  87. }
  88. for (tz in tz2cc) {
  89. if (cc_used[tz2cc[tz]] == 1) {
  90. if (tz2comments[tz]) {
  91. printf "%s:%d: unnecessary comment `%s'\n", \
  92. zone_table, tz2NR[tz], tz2comments[tz] \
  93. >>"/dev/stderr"
  94. status = 1
  95. }
  96. } else {
  97. if (!tz2comments[tz]) {
  98. printf "%s:%d: missing comment\n", \
  99. zone_table, tz2NR[tz] >>"/dev/stderr"
  100. status = 1
  101. }
  102. }
  103. }
  104. FS = " "
  105. }
  106. {
  107. tz = ""
  108. if ($1 == "Zone") tz = $2
  109. if ($1 == "Link") {
  110. # Ignore Link commands if source and destination basenames
  111. # are identical, e.g. Europe/Istanbul versus Asia/Istanbul.
  112. src = $2
  113. dst = $3
  114. while ((i = index(src, "/"))) src = substr(src, i+1)
  115. while ((i = index(dst, "/"))) dst = substr(dst, i+1)
  116. if (src != dst) tz = $3
  117. }
  118. if (tz && tz ~ /\//) {
  119. if (!tz2cc[tz]) {
  120. printf "%s: no data for `%s'\n", zone_table, tz \
  121. >>"/dev/stderr"
  122. status = 1
  123. }
  124. zoneSeen[tz] = 1
  125. }
  126. }
  127. END {
  128. for (tz in tz2cc) {
  129. if (!zoneSeen[tz]) {
  130. printf "%s:%d: no Zone table for `%s'\n", \
  131. zone_table, tz2NR[tz], tz >>"/dev/stderr"
  132. status = 1
  133. }
  134. }
  135. if (0 < want_warnings) {
  136. for (cc in cc2name) {
  137. if (!cc_used[cc]) {
  138. printf "%s:%d: warning: " \
  139. "no Zone entries for %s (%s)\n", \
  140. iso_table, cc2NR[cc], cc, cc2name[cc]
  141. }
  142. }
  143. }
  144. exit status
  145. }