lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # vi: ft=awk : |
| 3 | # |
| 4 | # Script to extract functions and external variables off SUS html docs |
| 5 | # |
| 6 | # Copyright (C) 2010 Bernhard Reutner-Fischer |
| 7 | # Public Domain |
| 8 | |
| 9 | # Usage: |
| 10 | # wget http://www.opengroup.org/onlinepubs/9699919799/download/susv4.tgz |
| 11 | # tar xzf susv4.tgz |
| 12 | # SUS=susv4 html2input.sh -vFULL_DECLARATIONS=1 |
| 13 | # or |
| 14 | # SUS=susv4 html2input.sh -vFULL_DECLARATIONS=0 -vSTDNAME=SUSv4 |
| 15 | # |
| 16 | # Bug in time.h.html of SUSv4: |
| 17 | # It inconsistently reads "as variables" instead of "external variables" that |
| 18 | # is used everywhere except in time.h.html |
| 19 | |
| 20 | test "x$SUS" = "x" && SUS="susv4" |
| 21 | test "x$AWK" = "x" && AWK="AWK" |
| 22 | test "x$GREP" = "x" && GREP="GREP" |
| 23 | for h in \ |
| 24 | $($GREP -l "shall be declared as functions" $SUS/basedefs/*.h.html) \ |
| 25 | $($GREP -l "shall declare the following as variables" $SUS/basedefs/*.h.html) \ |
| 26 | $($GREP -l "shall declare the following external variables" $SUS/basedefs/*.h.html) |
| 27 | do |
| 28 | $AWK $* ' |
| 29 | function get_filename () { |
| 30 | if (NR == 1) { |
| 31 | x=FILENAME |
| 32 | sub(".*/", "", x) |
| 33 | split(x, f , ".") |
| 34 | fname=f[1] |
| 35 | if (STDNAME) |
| 36 | fname=fname "." STDNAME |
| 37 | fname=fname ".in" |
| 38 | printf "" > fname |
| 39 | } |
| 40 | } |
| 41 | function unhtml (l) { |
| 42 | sub("<tt>", "", l) |
| 43 | sub("</tt>", "", l) |
| 44 | sub("<sup>", "", l) |
| 45 | sub("</sup>", "", l) |
| 46 | sub("<a [^>]*>", "", l) |
| 47 | sub("</a>", "", l) |
| 48 | if (l ~ /<img[^>]*Option[[:space:]][[:space:]]*Start[^>]*>/) { |
| 49 | sub("<img[^>]*>", "[Option Start]", l) |
| 50 | } else if (l ~ /<img[^>]*Option[[:space:]][[:space:]]*End[^>]*>/) { |
| 51 | sub("<img[^>]*>", "[Option End]", l) |
| 52 | } |
| 53 | sub("<.*>", "", l) |
| 54 | return l |
| 55 | } |
| 56 | function get_funcname (l) { |
| 57 | if (FULL_DECLARATIONS) |
| 58 | return l |
| 59 | if (l !~ /;$/) |
| 60 | return l |
| 61 | cnt = split(l, foo, " ") |
| 62 | if (cnt >= 2 && foo[2] ~ /^\(\*/) { |
| 63 | cnt = split(l, foo, "(") |
| 64 | # good enough for signal() and sigset() |
| 65 | if (cnt >= 2) |
| 66 | l=foo[2] |
| 67 | } else { |
| 68 | sub("\\(.*", "", l) |
| 69 | } |
| 70 | gsub("[[\\]\\*]", "", l) |
| 71 | i = split(l, a, " ") |
| 72 | if (i) |
| 73 | l = a[i] |
| 74 | return l |
| 75 | } |
| 76 | function get_varname (l) { |
| 77 | if (FULL_DECLARATIONS) |
| 78 | return l |
| 79 | if (l !~ /;$/) |
| 80 | return l |
| 81 | gsub(",[[:space:]][[:space:]]*", ",", l) |
| 82 | sub(";$", "", l) |
| 83 | i = split(l, a, " ") |
| 84 | if (i) |
| 85 | l = a[i] |
| 86 | gsub("[[\\]\\*]", "", l) |
| 87 | gsub(",", "\n", l) |
| 88 | return l |
| 89 | } |
| 90 | BEGIN{data=0;l=""} |
| 91 | get_filename() |
| 92 | /shall be declared as functions/{data=1;isvar=0;next;} |
| 93 | /shall declare the following as variables/{data=1;isvar=1;next;} |
| 94 | /shall declare the following external variables/{data=1;isvar=1;next;} |
| 95 | /<pre>/{data++;next;} |
| 96 | /<\/pre>/{data=0;next;} |
| 97 | /.*/{ |
| 98 | if (data == 2 && fname) { |
| 99 | tmp = $0 |
| 100 | sub("^[[:space:]][[:space:]]*", " ", tmp) |
| 101 | l = l tmp |
| 102 | tmp = unhtml(l) |
| 103 | if (!tmp) |
| 104 | next |
| 105 | l = tmp |
| 106 | if (tmp !~ /;$/ && tmp !~ />$/ && |
| 107 | tmp !~ /Option Start\]$/ && tmp !~ /Option End\]$/) |
| 108 | next |
| 109 | if (!isvar) |
| 110 | l = get_funcname(l) |
| 111 | else |
| 112 | l = get_varname(l) |
| 113 | if (l) |
| 114 | print l >> fname |
| 115 | l="" |
| 116 | } |
| 117 | } |
| 118 | ' $h |
| 119 | done |