| [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 | } |
|---|
| [837] | 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 | } |
|---|
| [187] | 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 |
|---|
| [169] | 35 | /^#/ { |
|---|
| 36 | next; |
|---|
| 37 | } |
|---|
| [187] | 38 | |
|---|
| [1] | 39 | /^}.*;/ { |
|---|
| 40 | if (instruct) { |
|---|
| 41 | sub(";", ""); |
|---|
| [837] | 42 | structname = instruct; |
|---|
| 43 | if (structname == 1 && $2) { |
|---|
| 44 | structname = $2; |
|---|
| [1] | 45 | } |
|---|
| [837] | 46 | if (structname in typedefs) { |
|---|
| 47 | structname = typedefs[structname]; |
|---|
| [1] | 48 | } |
|---|
| [42] | 49 | sizeinfo = ""; |
|---|
| [1] | 50 | elms = ""; |
|---|
| 51 | for (i = 0; i in buffer; i ++) { |
|---|
| [42] | 52 | if (i) { |
|---|
| 53 | sizeinfo = sizeinfo " + "; |
|---|
| 54 | } |
|---|
| [837] | 55 | sizeinfo = sizeinfo "sizeof(((" structname "*)NULL)->" buffer[i] ")"; |
|---|
| [4] | 56 | |
|---|
| [42] | 57 | if (i == 0) { |
|---|
| [719] | 58 | elms = "\"" buffer[i] "\""; |
|---|
| [4] | 59 | } |
|---|
| 60 | else { |
|---|
| [719] | 61 | elms = elms "," "\"" buffer[i] "\""; |
|---|
| [4] | 62 | } |
|---|
| [1] | 63 | } |
|---|
| [837] | 64 | ELEMENTSOF[structname] = elms; |
|---|
| 65 | COUNTOF[structname] = i; |
|---|
| 66 | SIZEOF[structname] = sizeinfo; |
|---|
| 67 | printstruct(structname); |
|---|
| [1] | 68 | print "\n"; |
|---|
| [23] | 69 | for (i in buffer) { |
|---|
| 70 | delete buffer[i]; |
|---|
| 71 | } |
|---|
| [1] | 72 | buffer_len = 0; |
|---|
| 73 | instruct = 0; |
|---|
| 74 | } |
|---|
| 75 | next; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| [23] | 78 | /.\{/ { |
|---|
| [1] | 79 | brace = brace + 1; |
|---|
| 80 | } |
|---|
| 81 | /.}/ { |
|---|
| 82 | brace = brace - 1; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | { |
|---|
| 86 | if (brace == 1 && instruct) { |
|---|
| [187] | 87 | gsub(/(^[\t ]+|[\t ]+$)/, ""); # trim whitespaces |
|---|
| [1] | 88 | sub(/.*[{}]/, ""); |
|---|
| [42] | 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; |
|---|
| [1] | 97 | buffer_len ++; |
|---|
| 98 | } |
|---|
| 99 | else { |
|---|
| [187] | 100 | # process normal variables |
|---|
| 101 | |
|---|
| [42] | 102 | # ignore any ()s |
|---|
| 103 | while (gsub(/(\([^)]*\))/, "")) { |
|---|
| [1] | 104 | } |
|---|
| [42] | 105 | if (match($0, /[()]/)) { |
|---|
| 106 | next; |
|---|
| 107 | } |
|---|
| [169] | 108 | # unsigned int *a, b; int c; |
|---|
| [42] | 109 | gsub(/[*]/, " "); |
|---|
| [169] | 110 | # unsigned int a, b; int c; |
|---|
| [42] | 111 | gsub(/ +/, " "); |
|---|
| [169] | 112 | # unsigned int a, b; int c; |
|---|
| [42] | 113 | gsub(/ *[,;]/, ";"); |
|---|
| [169] | 114 | # unsigned int a; b; int c; |
|---|
| [42] | 115 | if (!match($0, /;/)) { |
|---|
| 116 | next; |
|---|
| 117 | } |
|---|
| [187] | 118 | # print "=DEBUG=" $0 "=="; |
|---|
| [42] | 119 | split($0, chunks, ";"); |
|---|
| [169] | 120 | # [unsigned int a, b, c] |
|---|
| 121 | |
|---|
| 122 | for (i = 1; i in chunks; i ++) { |
|---|
| [42] | 123 | if (chunks[i] == "") { |
|---|
| 124 | delete chunks[i]; |
|---|
| 125 | continue; |
|---|
| 126 | } |
|---|
| 127 | split(chunks[i], pieces, " "); |
|---|
| [169] | 128 | # [unsigned, int, a] |
|---|
| 129 | # [b] |
|---|
| 130 | # [c] |
|---|
| [42] | 131 | |
|---|
| [169] | 132 | last_piece = ""; |
|---|
| 133 | for (j = 1; j in pieces; j ++) { |
|---|
| [42] | 134 | last_piece = pieces[j]; |
|---|
| [169] | 135 | delete pieces[j]; |
|---|
| [42] | 136 | } |
|---|
| 137 | if (last_piece == "") { |
|---|
| [187] | 138 | # print "=ERROR=" chunks[i] "=="; |
|---|
| 139 | delete chunks[i]; |
|---|
| 140 | continue; |
|---|
| [42] | 141 | } |
|---|
| [169] | 142 | # a |
|---|
| 143 | # b |
|---|
| 144 | # c |
|---|
| 145 | |
|---|
| [42] | 146 | buffer[buffer_len] = last_piece; |
|---|
| [1] | 147 | buffer_len ++; |
|---|
| [169] | 148 | delete chunks[i] |
|---|
| [1] | 149 | } |
|---|
| [169] | 150 | last_piece = ""; |
|---|
| [1] | 151 | } |
|---|
| 152 | next; |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /^typedef struct [^{]*;/ { |
|---|
| 157 | sub(";", ""); |
|---|
| [837] | 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 | } |
|---|
| [1] | 169 | next; |
|---|
| 170 | } |
|---|
| [187] | 171 | /^typedef struct .*\{[^}]*$/ { |
|---|
| [1] | 172 | brace = 1; |
|---|
| 173 | instruct = 1; |
|---|
| 174 | next; |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| [23] | 177 | /^struct .*\{/ { |
|---|
| [1] | 178 | instruct = $2; |
|---|
| 179 | brace = 1; |
|---|
| 180 | next; |
|---|
| 181 | } |
|---|