command_data.awk 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Copyright 2010, 2011, 2012, 2013, 2014, 2015
  2. # Free Software Foundation, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 3 of the License,
  7. # or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #
  17. #######################################################
  18. # From gawk manual
  19. # ord.awk --- do ord and chr
  20. # Global identifiers:
  21. # _ord_: numerical values indexed by characters
  22. # _ord_init: function to initialize _ord_
  23. BEGIN { _ord_init() }
  24. function _ord_init( low, high, i, t)
  25. {
  26. low = sprintf("%c", 7) # BEL is ascii 7
  27. if (low == "\a") { # regular ascii
  28. low = 0
  29. high = 127
  30. } else if (sprintf("%c", 128 + 7) == "\a") {
  31. # ascii, mark parity
  32. low = 128
  33. high = 255
  34. } else { # ebcdic(!)
  35. low = 0
  36. high = 255
  37. }
  38. for (i = low; i <= high; i++) {
  39. t = sprintf("%c", i)
  40. _ord_[t] = i
  41. }
  42. }
  43. function ord(str, c)
  44. {
  45. # only first character is of interest
  46. c = substr(str, 1, 1)
  47. return _ord_[c]
  48. }
  49. #######################################################
  50. BEGIN {
  51. bs_escapes["\\n"] = "\n"
  52. bs_escapes["\\f"] = "\f"
  53. bs_escapes["\\t"] = "\t"
  54. bs_escapes["\\\\"] = "\\"
  55. bs_escapes["\\\""] = "\""
  56. bs_escapes["\\x20"] = " "
  57. for (v in bs_escapes) {
  58. inv_bs_escapes[bs_escapes[v]] = v
  59. }
  60. print "/* This file automatically generated by command_data.awk */"
  61. print
  62. print "/* Useful aliases */"
  63. print "#define CM_hex_09 CM_TAB"
  64. print "#define CM_hex_0a CM_NEWLINE"
  65. print "#define CM_hex_20 CM_SPACE"
  66. print "#define CM_hex_21 CM_EXCLAMATION_MARK"
  67. print "#define CM_hex_22 CM_POUND_SIGN"
  68. print "#define CM_hex_27 CM_APOSTROPHE"
  69. print "#define CM_hex_2a CM_ASTERISK"
  70. print "#define CM_hex_2c CM_COMMA"
  71. print "#define CM_hex_2d CM_HYPHEN"
  72. print "#define CM_hex_2e CM_FULL_STOP"
  73. print "#define CM_hex_2f CM_SLASH"
  74. print "#define CM_hex_3a CM_COLON"
  75. print "#define CM_hex_3d CM_EQUALS"
  76. print "#define CM_hex_3f CM_QUESTION_MARK"
  77. print "#define CM_hex_40 CM_AT_SIGN"
  78. print "#define CM_hex_5c CM_BACKSLASH"
  79. print "#define CM_hex_5e CM_CIRCUMFLEX"
  80. print "#define CM_hex_60 CM_BACKQUOTE"
  81. print "#define CM_hex_7b CM_OPEN_BRACE"
  82. print "#define CM_hex_7c CM_VERTICAL_BAR"
  83. print "#define CM_hex_7d CM_CLOSE_BRACE"
  84. print "#define CM_hex_7e CM_TILDE"
  85. print
  86. print "enum command_id {"
  87. print "CM_NONE,"
  88. print
  89. }
  90. !/^$/ && !/^#/ {
  91. if ($1 in bs_escapes) {
  92. c = bs_escapes[$1]
  93. } else {
  94. c = $1
  95. }
  96. commands[c] = $2
  97. data[c] = $3
  98. }
  99. END {
  100. print "COMMAND builtin_command_data[] = {" >"command_data.c"
  101. print "0, 0, 0," >"command_data.c"
  102. # We want the output sorted so we can use bsearch
  103. PROCINFO["sorted_in"]="@ind_str_asc"
  104. for (c in commands) {
  105. # Single character commands with unusual names
  106. if (c ~ /^[^[:alpha:]]$/) {
  107. if (c in inv_bs_escapes) {
  108. c2 = inv_bs_escapes[c]
  109. } else
  110. c2 = c
  111. printf "CM_hex_%02x,\n", ord(c)
  112. } else {
  113. c2 = c
  114. print "CM_" c ","
  115. }
  116. if (commands[c] != "") {
  117. flags = "CF_" commands[c]
  118. gsub (/,/, " | CF_", flags)
  119. } else {
  120. flags = "0"
  121. }
  122. if (data[c] != "") {
  123. command_data = data[c]
  124. } else {
  125. command_data = "0"
  126. }
  127. print "\"" c2 "\", " flags ", " command_data "," > "command_data.c"
  128. }
  129. print "};" >"command_data.c"
  130. print "};"
  131. }