mkalltypes.awk (2927B)
1 # mkalltypes.awk — port of musl-1.2.5/tools/mkalltypes.sed. 2 # 3 # Reads alltypes.h.in (per-arch + generic, concatenated). Lines starting 4 # with TYPEDEF / STRUCT / UNION are expanded into the standard 5 # #if defined(__NEED_X) && !defined(__DEFINED_X) ... #endif 6 # guard blocks. All other lines pass through unchanged. 7 # 8 # Output is byte-identical to running `sed -f tools/mkalltypes.sed` on 9 # the same inputs (the original sed preserves the input's internal 10 # alignment whitespace, so this awk does the same — no field splitting). 11 12 # Match sed `TYPEDEF \(.*\) \([^ ]*\);$` — greedy first capture, last 13 # space-delimited token is the name. Returns 1 on match, sets t and n. 14 function split_typedef(line, _, pos, last) { 15 if (substr(line, length(line)) != ";") return 0 16 line = substr(line, 1, length(line) - 1) 17 # locate the last space 18 last = 0 19 for (pos = length(line); pos >= 1; pos--) { 20 if (substr(line, pos, 1) == " ") { last = pos; break } 21 } 22 if (last == 0) return 0 23 t = substr(line, 1, last - 1) 24 n = substr(line, last + 1) 25 return 1 26 } 27 28 /^TYPEDEF / { 29 rest = substr($0, 9) 30 if (split_typedef(rest)) { 31 printf "#if defined(__NEED_%s) && !defined(__DEFINED_%s)\n", n, n 32 printf "typedef %s %s;\n", t, n 33 printf "#define __DEFINED_%s\n", n 34 printf "#endif\n\n" 35 next 36 } 37 } 38 39 # sed: STRUCT * \([^ ]*\) \(.*\);$ 40 # After STRUCT and any spaces, first non-space-run is the name; rest 41 # (space then body) ends with ;. Note: sed's `STRUCT *` allows zero or 42 # more spaces after STRUCT, but in practice input always has at least 43 # one — match either way. 44 /^STRUCT / { 45 rest = substr($0, 8) # skip "STRUCT " 46 sub(/^ +/, "", rest) # collapse extra spaces 47 if (substr(rest, length(rest)) == ";") { 48 rest = substr(rest, 1, length(rest) - 1) 49 # name = first space-delimited token 50 sp = index(rest, " ") 51 if (sp > 0) { 52 sname = substr(rest, 1, sp - 1) 53 sbody = substr(rest, sp + 1) 54 printf "#if defined(__NEED_struct_%s) && !defined(__DEFINED_struct_%s)\n", sname, sname 55 printf "struct %s %s;\n", sname, sbody 56 printf "#define __DEFINED_struct_%s\n", sname 57 printf "#endif\n\n" 58 next 59 } 60 } 61 } 62 63 /^UNION / { 64 rest = substr($0, 7) # skip "UNION " 65 sub(/^ +/, "", rest) 66 if (substr(rest, length(rest)) == ";") { 67 rest = substr(rest, 1, length(rest) - 1) 68 sp = index(rest, " ") 69 if (sp > 0) { 70 uname = substr(rest, 1, sp - 1) 71 ubody = substr(rest, sp + 1) 72 printf "#if defined(__NEED_union_%s) && !defined(__DEFINED_union_%s)\n", uname, uname 73 printf "union %s %s;\n", uname, ubody 74 printf "#define __DEFINED_union_%s\n", uname 75 printf "#endif\n\n" 76 next 77 } 78 } 79 } 80 81 { print }