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