XCache is a fast, stable PHP opcode cacher that has been proven and is now running on production servers under high load. https://xcache.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

213 lines
3.8 KiB

  1. #! /usr/bin/awk -f
  2. # vim:ts=4:sw=4
  3. BEGIN {
  4. brace = 0;
  5. incomment = 0;
  6. buffer_len = 0;
  7. }
  8. function printstruct(structname) {
  9. printf "define(`ELEMENTSOF_%s', `%s')\n", structname, ELEMENTSOF[structname];
  10. printf "define(`COUNTOF_%s', `%s')\n", structname, COUNTOF[structname];
  11. printf "define(`SIZEOF_%s', `( %s )')\n", structname, SIZEOF[structname];
  12. }
  13. function countBrace(text, len, i, char, braceCount) {
  14. len = length(text);
  15. braceCount = 0;
  16. for (i = 1; i <= len; ++i) {
  17. char = substr(text, i, 1);
  18. if (char == "{") {
  19. braceCount = braceCount + 1;
  20. }
  21. else if (char == "}") {
  22. braceCount = braceCount - 1;
  23. }
  24. }
  25. return braceCount;
  26. }
  27. # multiline comment handling
  28. {
  29. # removes one line comment
  30. gsub(/\/\*(.+?)\*\//, " ");
  31. }
  32. /\*\// {
  33. if (incomment) {
  34. sub(/.*\*\//, "");
  35. incomment = 0;
  36. }
  37. }
  38. incomment {
  39. next;
  40. }
  41. /\/\*/ {
  42. sub(/\/\*.*/, "");
  43. incomment = 1;
  44. # fall through
  45. }
  46. # skip file/line mark here to be faster
  47. /^#/ {
  48. next;
  49. }
  50. /^}.*;/ {
  51. if (instruct) {
  52. sub(";", "");
  53. structname = instruct;
  54. if (structname == 1 && $2) {
  55. structname = $2;
  56. }
  57. if (structname in typedefs) {
  58. structname = typedefs[structname];
  59. }
  60. sizeinfo = "";
  61. elms = "";
  62. for (i = 0; i in buffer; i ++) {
  63. if (i) {
  64. sizeinfo = sizeinfo " + ";
  65. }
  66. sizeinfo = sizeinfo "sizeof(((" structname "*)NULL)->" buffer[i] ")";
  67. if (i == 0) {
  68. elms = "\"" buffer[i] "\"";
  69. }
  70. else {
  71. elms = elms "," "\"" buffer[i] "\"";
  72. }
  73. }
  74. ELEMENTSOF[structname] = elms;
  75. COUNTOF[structname] = i;
  76. SIZEOF[structname] = sizeinfo;
  77. printstruct(structname);
  78. print "\n";
  79. for (i in buffer) {
  80. delete buffer[i];
  81. }
  82. buffer_len = 0;
  83. instruct = 0;
  84. }
  85. next;
  86. }
  87. /.[{}]/ {
  88. brace += countBrace($0);
  89. }
  90. {
  91. if (brace == 1 && instruct) {
  92. gsub(/(^[\t ]+|[\t ]+$)/, ""); # trim whitespaces
  93. sub(/.*[{}]/, "");
  94. gsub(/\[[^\]]+\]/, ""); # ignore [...]
  95. gsub(/:[0-9]+/, ""); # ignore struct bit
  96. if (match($0, /^[^(]*\([ ]*\*([^)]+)\)/)) {
  97. sub(/ +/, "")
  98. sub(/^[^(]*\(\*/, "");
  99. sub(/\).*/, "");
  100. # function pointer
  101. buffer[buffer_len] = $0;
  102. buffer_len ++;
  103. }
  104. else {
  105. # process normal variables
  106. # ignore any ()s
  107. while (gsub(/(\([^)]*\))/, "")) {
  108. }
  109. if (match($0, /[()]/)) {
  110. next;
  111. }
  112. # unsigned int *a, b; int c;
  113. gsub(/[*]/, " ");
  114. # unsigned int a, b; int c;
  115. gsub(/ +/, " ");
  116. # unsigned int a, b; int c;
  117. gsub(/ *[,;]/, ";");
  118. # unsigned int a; b; int c;
  119. if (!match($0, /;/)) {
  120. next;
  121. }
  122. # print "=DEBUG=" $0 "==";
  123. split($0, chunks, ";");
  124. # [unsigned int a, b, c]
  125. for (i = 1; i in chunks; i ++) {
  126. if (chunks[i] == "") {
  127. delete chunks[i];
  128. continue;
  129. }
  130. split(chunks[i], pieces, " ");
  131. # [unsigned, int, a]
  132. # [b]
  133. # [c]
  134. last_piece = "";
  135. for (j = 1; j in pieces; j ++) {
  136. last_piece = pieces[j];
  137. delete pieces[j];
  138. }
  139. if (last_piece == "") {
  140. # print "=ERROR=" chunks[i] "==";
  141. delete chunks[i];
  142. continue;
  143. }
  144. # a
  145. # b
  146. # c
  147. buffer[buffer_len] = last_piece;
  148. buffer_len ++;
  149. delete chunks[i]
  150. }
  151. last_piece = "";
  152. }
  153. next;
  154. }
  155. }
  156. /^typedef struct [^{]*;/ {
  157. sub(";", "");
  158. typename=$3;
  159. newtypename=$4;
  160. typedefs[typename] = newtypename;
  161. if (ELEMENTSOF[typename]) {
  162. ELEMENTSOF[newtypename] = ELEMENTSOF[typename];
  163. COUNTOF[newtypename] = COUNTOF[typename];
  164. sub(/.*/, SIZEOF[typename]);
  165. gsub(typename, newtypename);
  166. SIZEOF[newtypename] = $0;
  167. printstruct(newtypename);
  168. }
  169. next;
  170. }
  171. /^typedef struct .*\{[^}]*$/ {
  172. brace = countBrace($0);
  173. if (brace > 0) {
  174. instruct = 1;
  175. }
  176. else {
  177. brace = 0;
  178. instruct = 0;
  179. }
  180. for (i in buffer) {
  181. delete buffer[i];
  182. }
  183. next;
  184. }
  185. /^struct .*\{.*/ {
  186. brace = countBrace($0);
  187. if (brace > 0) {
  188. instruct = $2;
  189. }
  190. else {
  191. brace = 0;
  192. instruct = 0;
  193. }
  194. for (i in buffer) {
  195. delete buffer[i];
  196. }
  197. next;
  198. }