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