mkdevc (1346B)
1 #!/bin/sh 2 3 trap 'rm -f $$.tmp' EXIT HUP INT QUIT TERM 4 5 ./section dev $@ | 6 awk ' 7 /^\t\t/ {devs[dev]++ 8 phys[$1] = dev 9 drivers[n++] = dev FS $1 FS $2 10 next} 11 /^\t/ {devs[dev = $1] = 0} 12 END {typedef() 13 extern() 14 devtab() 15 devlink()} 16 17 function capital(x) 18 { 19 return toupper(substr(x, 1, 1)) substr(x, 2) 20 } 21 22 function typedef() 23 { 24 print "typedef struct dev Dev;" 25 print "typedef struct attr Attr;" 26 for (i in devs) 27 printf "typedef struct %sphy %sphy;\n", i, capital(i) 28 print "\nstruct attr {" 29 print "\tchar *key;" 30 print "\tchar *value;" 31 print "};\n" 32 } 33 34 function extern() 35 { 36 for (i in devs) 37 printf "extern Dev %sdevtab;\n", i 38 for (i in phys) 39 printf "extern %sphy %s;\n", capital(phys[i]), i "phy" 40 for (i in devs) { 41 if (devs[i] > 0) { 42 printf "extern void %slink(%sphy *, Attr *);\n", 43 i, capital(i) 44 } 45 } 46 } 47 48 function devtab() 49 { 50 print "\nDev *const devtab[] = {" 51 for (i in devs) 52 printf "\t&%sdevtab,\n", i 53 print "\t0\n};\n" 54 } 55 56 function devlink() 57 { 58 printf "void\ndevlink(void)\n{\n" 59 for (i in drivers) { 60 split(drivers[i], links) 61 printf "\t%slink(&%s, (Attr []) {", links[1], links[2] "phy" 62 n = split(links[3], args, / *, */) 63 for (j = 1; j <= n; j++) { 64 split(args[j], keys, / *= */) 65 printf "{\"%s\", \"%s\"},", keys[1], keys[2] 66 } 67 print "{0}});" 68 } 69 print "}" 70 }' > $$.tmp && mv $$.tmp devc.c