usr/src/java/adr/radadrgen.sh
changeset 809 8a6fba4105d7
parent 797 a33daeba9b4c
child 811 c65c58b7a1f0
--- a/usr/src/java/adr/radadrgen.sh	Wed Mar 14 04:47:00 2012 -0700
+++ b/usr/src/java/adr/radadrgen.sh	Wed Mar 14 10:45:15 2012 -0400
@@ -24,5 +24,233 @@
 # Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
 #
 
-ADRLOC=$(dirname $0)/../lib/rad/java/adr.jar
-exec java -classpath ${ADRLOC} org.opensolaris.os.adr.adrgen.RadADRGen "$@"
+#
+# Environment
+#
+
+basename="`basename $0`"
+usage="\
+Usage: $basename [-N] <spec.xml>
+       $basename [-N] -c <dir> [-r [-m] [-s]] <spec.xml>
+       $basename [-N] -j <dir> [-i] <spec.xml>
+       $basename [-N] -o docbook|docbook-man|man|rmdoc <spec.xml>
+       $basename -d <baseline.xml> <spec.xml>"
+
+RADADR_SCHEMA="${RADADR_SCHEMA:-/usr/share/lib/xml/rng/radadr.rng.1}"
+RADADR_XSLDIR="${RADADR_XSLDIR:-/usr/share/lib/xml/style}"
+RADADR_JARDIR="${RADADR_JARDIR:-/usr/lib/rad/java}"
+
+CAT=/usr/bin/cat
+EGREP=/usr/bin/egrep
+FIND=/usr/bin/find
+JAVA=/usr/bin/java
+MKTEMP=/usr/bin/mktemp
+RM=/usr/bin/rm
+XMLLINT=/usr/bin/xmllint
+XSLTPROC=/usr/bin/xsltproc
+
+tmpdir=
+
+#
+# Functions
+#
+
+check_dependencies () {
+    typeset dependency="$1"
+    shift
+    typeset opt
+    for opt in "$@"
+    do
+        if [ -n "${opts[$opt]}" -a -z "${opts[$dependency]}" ]
+        then
+            die "$opt requires $dependency to be specified\n\n$usage"
+        fi
+    done
+}
+
+check_exclusive () {
+    typeset found=
+    typeset opt
+    for opt in "$@"
+    do
+        if [ -n "${opts[$opt]}" ]
+        then
+            if [ -n "$found" ]
+            then
+                die "$opt cannot be specified with $found\n\n$usage"
+            fi
+            found="$opt"
+        fi
+    done
+}
+
+cleanup() {
+    if [ -n "$tmpdir" ]
+    then
+        "$RM" -rf "$tmpdir"
+    fi
+}
+
+die() {
+    test $# -gt 0 && echo "$basename: $@" >&2
+    cleanup
+    exit 1
+}
+
+runxsltproc() {
+    typeset ignorere="$1"
+    shift
+
+    # Filter out some of xsltproc's excessive noisiness in a way that preserves
+    # its stdout and stderr.  A consequence of this filtering is that ksh won't
+    # report a failure of xsltproc in $?.  So turn on pipefail, then make sure
+    # the filter exits with 0 so as not to corrupt the reporting of xsltproc's
+    # exit status.
+    set -o pipefail
+    exec 3>&1
+    ("$XSLTPROC" "$@" 1>&3) 2>&1 | ("$EGREP" -v "$ignorere"; exit 0) 1>&2
+
+    return $?
+}
+
+generate_out() {
+    # When processing locale.xsl, xsltproc will complain when optional
+    # locale-based resource files don't exist.  Since xsltproc has no way to
+    # suppress these warnings, filter them out in a way that preserves
+    # stdout and stderr.
+    #
+    # A consequence of this filtering is that ksh won't report a failure of
+    # xsltproc in $?.  So turn on pipefail, then make sure the filter exits
+    # with 0 so as not to corrupt the reporting of xsltproc's exit status.
+    runxsltproc \
+        '^warning: failed to load external entity.*strings[^/]*\.xml' \
+        --stringparam LANG "${LC_ALL:-${LANG}}" \
+        "$RADADR_XSLDIR/radadr-$1.xsl" "$specfile"
+}
+
+#
+# Main
+#
+
+docfmt=
+typeset -A opts
+set -A args
+
+while getopts :[-]c:d:ij:mNo:rs name
+do
+    opts[-$name]=-$name
+    case "$name" in
+    o)
+        case "$OPTARG" in
+        docbook|docbook-man|man|rmdoc)
+            docfmt="$OPTARG"
+        ;;
+        *) die "invalid documentation format: $OPTARG\n\n$usage" ;;
+        esac
+    ;;
+
+    :)
+        die "-$OPTARG requires an argument\n\n$usage"
+    ;;
+
+    '?')
+        die "invalid option: -$OPTARG\n\n$usage"
+    ;;
+
+    c|j)
+        args+=(-d "$OPTARG")
+    ;;
+
+    d)
+        args+=("$OPTARG")
+    ;;
+
+    i|m|N|r|s)
+        args+=(-$name)
+    ;;
+    esac
+done
+
+check_dependencies -j -i
+check_dependencies -c -r
+check_dependencies -r -m -s
+check_exclusive -c -j -d -o
+check_exclusive -d -N
+
+shift $((OPTIND - 1))
+if [ $# -eq 0 ]
+then
+    die "no spec file specified\n\n$usage"
+fi
+
+specfile="$1"
+args+=("$specfile")
+shift
+
+if [ $# -ne 0 ]
+then
+    die "$# unexpected arguments, starting with: $1\n\n$usage"
+fi
+
+# Spec validation
+err="$("$XMLLINT" --noout --relaxng "$RADADR_SCHEMA" "$specfile" 2>&1)" || {
+    # xmllint is noisy even when validation succeeds
+    echo "$err" >&2
+    die "invalid spec file: $specfile"
+}
+
+set -A javacmd "$JAVA" -cp "$RADADR_JARDIR/adr.jar" -Dargv0="$basename"
+
+if [ -n "${opts[-c]}" ]
+then
+    "${javacmd[@]}" org.opensolaris.os.adr.adrgen.CGenerator "${args[@]}"
+
+elif [ -n "${opts[-j]}" ]
+then
+    "${javacmd[@]}" org.opensolaris.os.adr.adrgen.JGenerator "${args[@]}"
+
+elif [ -n "${opts[-d]}" ]
+then
+    "${javacmd[@]}" org.opensolaris.os.adr.APIDiff "${args[@]}"
+
+else
+    "${javacmd[@]}" org.opensolaris.os.adr.adrgen.ADRGen "${args[@]}" || die
+
+    if [ -n "${opts[-o]}" ]
+    then
+        case "$docfmt" in
+        man)
+            tmpdir="$("$MKTEMP" -dqt "$basename".XXXXXX)" ||
+                die "could not create directory: $tmpdir"
+
+            dbman="docbook-man.xml"
+            generate_out docbook-man > "$tmpdir/$dbman" || die
+
+            (
+            cd "$tmpdir"
+            runxsltproc \
+                '^WARNING: cannot add @xml:base|^Note: Writing' \
+                --xinclude \
+                --nonet \
+                --stringparam funcsynopsis.style "ansi" \
+                --stringparam man.authors.section.enabled 0 \
+                --stringparam section.autolabel 1 \
+                --stringparam section.label.includes.component.label 1 \
+                --stringparam segmentedlist.as.table 1 \
+                --stringparam profile.status "private" \
+                --param refentry.meta.get.quietly 1 \
+                http://docbook.sourceforge.net/release/xsl/current/manpages/profile-docbook.xsl \
+                "$dbman"
+            ) || die
+
+            "$FIND" "$tmpdir" -type f \( -name "$dbman" -o -exec "$CAT" {} \; \)
+        ;;
+
+        *)
+            generate_out "$docfmt" || die
+        ;;
+        esac
+    fi
+fi
+
+cleanup