et_c.awk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. BEGIN {
  2. char_shift=64
  3. ## "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  4. c2n["A"]=1
  5. c2n["B"]=2
  6. c2n["C"]=3
  7. c2n["D"]=4
  8. c2n["E"]=5
  9. c2n["F"]=6
  10. c2n["G"]=7
  11. c2n["H"]=8
  12. c2n["I"]=9
  13. c2n["J"]=10
  14. c2n["K"]=11
  15. c2n["L"]=12
  16. c2n["M"]=13
  17. c2n["N"]=14
  18. c2n["O"]=15
  19. c2n["P"]=16
  20. c2n["Q"]=17
  21. c2n["R"]=18
  22. c2n["S"]=19
  23. c2n["T"]=20
  24. c2n["U"]=21
  25. c2n["V"]=22
  26. c2n["W"]=23
  27. c2n["X"]=24
  28. c2n["Y"]=25
  29. c2n["Z"]=26
  30. c2n["a"]=27
  31. c2n["b"]=28
  32. c2n["c"]=29
  33. c2n["d"]=30
  34. c2n["e"]=31
  35. c2n["f"]=32
  36. c2n["g"]=33
  37. c2n["h"]=34
  38. c2n["i"]=35
  39. c2n["j"]=36
  40. c2n["k"]=37
  41. c2n["l"]=38
  42. c2n["m"]=39
  43. c2n["n"]=40
  44. c2n["o"]=41
  45. c2n["p"]=42
  46. c2n["q"]=43
  47. c2n["r"]=44
  48. c2n["s"]=45
  49. c2n["t"]=46
  50. c2n["u"]=47
  51. c2n["v"]=48
  52. c2n["w"]=49
  53. c2n["x"]=50
  54. c2n["y"]=51
  55. c2n["z"]=52
  56. c2n["0"]=53
  57. c2n["1"]=54
  58. c2n["2"]=55
  59. c2n["3"]=56
  60. c2n["4"]=57
  61. c2n["5"]=58
  62. c2n["6"]=59
  63. c2n["7"]=60
  64. c2n["8"]=61
  65. c2n["9"]=62
  66. c2n["_"]=63
  67. }
  68. /^#/ { next }
  69. /^[ \t]*(error_table|et)[ \t]+[a-zA-Z][a-zA-Z0-9_]+/ {
  70. table_number = 0
  71. table_name = $2
  72. mod_base = 1000000
  73. for(i=1; i<=length(table_name); i++) {
  74. table_number=(table_number*char_shift)+c2n[substr(table_name,i,1)]
  75. }
  76. # We start playing *_high, *low games here because the some
  77. # awk programs do not have the necessary precision (sigh)
  78. tab_base_low = table_number % mod_base
  79. tab_base_high = int(table_number / mod_base)
  80. tab_base_sign = 1;
  81. # figure out: table_number_base=table_number*256
  82. tab_base_low = tab_base_low * 256
  83. tab_base_high = (tab_base_high * 256) + \
  84. int(tab_base_low / mod_base)
  85. tab_base_low = tab_base_low % mod_base
  86. if (table_number > 128*256*256) {
  87. # figure out: table_number_base -= 256*256*256*256
  88. # sub_high, sub_low is 256*256*256*256
  89. sub_low = 256*256*256 % mod_base
  90. sub_high = int(256*256*256 / mod_base)
  91. sub_low = sub_low * 256
  92. sub_high = (sub_high * 256) + int(sub_low / mod_base)
  93. sub_low = sub_low % mod_base
  94. tab_base_low = sub_low - tab_base_low;
  95. tab_base_high = sub_high - tab_base_high;
  96. tab_base_sign = -1;
  97. if (tab_base_low < 0) {
  98. tab_base_low = tab_base_low + mod_base
  99. tab_base_high--
  100. }
  101. }
  102. print "/*" > outfile
  103. print " * " outfile ":" > outfile
  104. print " * This file is automatically generated; please do not edit it." > outfile
  105. print " */" > outfile
  106. print "#if defined(_WIN32)" > outfile
  107. print "# include \"win-mac.h\"" > outfile
  108. print "#endif" > outfile
  109. print "" > outfile
  110. print "#if !defined(_WIN32)" > outfile
  111. print "extern void initialize_" table_name "_error_table (void);" > outfile
  112. print "#endif" > outfile
  113. print "" > outfile
  114. print "#define N_(x) (x)" > outfile
  115. print "" > outfile
  116. print "/* Lclint doesn't handle null annotations on arrays" > outfile
  117. print " properly, so we need this typedef in each" > outfile
  118. print " generated .c file. */" > outfile
  119. print "/*@-redef@*/" > outfile
  120. print "typedef /*@null@*/ const char *ncptr;" > outfile
  121. print "/*@=redef@*/" > outfile
  122. print "" > outfile
  123. print "static ncptr const text[] = {" > outfile
  124. table_item_count = 0
  125. }
  126. (continuation == 1) && ($0 ~ /\\[ \t]*$/) {
  127. text=substr($0,1,length($0)-1);
  128. # printf "\t\t\"%s\"\n", text > outfile
  129. cont_buf=cont_buf text;
  130. }
  131. (continuation == 1) && ($0 ~ /"[ \t]*$/) {
  132. # printf "\t\t\"%s,\n", $0 > outfile
  133. printf "\tN_(%s),\n", cont_buf $0 > outfile
  134. continuation = 0;
  135. }
  136. /^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*$/ {
  137. table_item_count++
  138. skipone=1
  139. next
  140. }
  141. /^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*".*"[ \t]*$/ {
  142. text=""
  143. for (i=3; i<=NF; i++) {
  144. text = text FS $i
  145. }
  146. text=substr(text,2,length(text)-1);
  147. printf "\tN_(%s),\n", text > outfile
  148. table_item_count++
  149. }
  150. /^[ \t]*(error_code|ec)[ \t]+[A-Z_0-9]+,[ \t]*".*\\[ \t]*$/ {
  151. text=""
  152. for (i=3; i<=NF; i++) {
  153. text = text FS $i
  154. }
  155. text=substr(text,2,length(text)-2);
  156. # printf "\t%s\"\n", text > outfile
  157. cont_buf=text
  158. continuation++;
  159. }
  160. /^[ \t]*".*\\[ \t]*$/ {
  161. if (skipone) {
  162. text=substr($0,1,length($0)-1);
  163. # printf "\t%s\"\n", text > outfile
  164. cont_buf=text
  165. continuation++;
  166. }
  167. skipone=0
  168. }
  169. {
  170. if (skipone) {
  171. printf "\tN_(%s),\n", $0 > outfile
  172. }
  173. skipone=0
  174. }
  175. END {
  176. if (table_item_count > 256) {
  177. print "Error table too large!" | "cat 1>&2"
  178. exit 1
  179. }
  180. # Put text domain and/or localedir at the end of the list.
  181. if (textdomain) {
  182. printf " \"%s\", /* Text domain */\n", textdomain > outfile
  183. if (localedir) {
  184. printf " \"%s\", /* Locale dir */\n", localedir > outfile
  185. }
  186. }
  187. print " 0" > outfile
  188. print "};" > outfile
  189. print "" > outfile
  190. print "#include <com_err.h>" > outfile
  191. print "" > outfile
  192. if (tab_base_high == 0) {
  193. print "const struct error_table et_" table_name "_error_table = { text, " \
  194. sprintf("%dL, %d };", tab_base_sign*tab_base_low, \
  195. table_item_count) > outfile
  196. } else {
  197. print "const struct error_table et_" table_name "_error_table = { text, " \
  198. sprintf("%d%06dL, %d };", tab_base_sign*tab_base_high, \
  199. tab_base_low, table_item_count) > outfile
  200. }
  201. print "" > outfile
  202. print "#if !defined(_WIN32)" > outfile
  203. print "void initialize_" table_name "_error_table (void)" > outfile
  204. print " /*@modifies internalState@*/" > outfile
  205. print "{" > outfile
  206. print " (void) add_error_table (&et_" table_name "_error_table);" > outfile
  207. print "}" > outfile
  208. print "#endif" > outfile
  209. }