jdk/make/java/nio/spp.sh
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
#! /bin/sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
# Copyright 2000-2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
# This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
# under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
# published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
# particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
# by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
# This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
# version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
# accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
# You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
# 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
# CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
# have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
# SPP: A simple/sed-based/stream preprocessor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
# Mark Reinhold / mr@sun.com
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
# Usage: spp [-be] [-Kkey] -Dvar=value ... <in >out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
# Source-file constructs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#   Meaningful only at beginning of line, works with any number of keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#     #if[key]              Includes text between #if/#end if -Kkey specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#     #else[key]               otherwise changes text to blank lines; key test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
#     #end[key]                may be negated by prefixing !, e.g., #if[!key]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#     #begin                If -be is specified then lines up to and including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#     #end                     #begin, and from #end to EOF, are deleted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#     #warn                 Changed into warning that file is generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#     // ##                 Changed into blank line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
#   Meaningful anywhere in line, works only for first two keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#     {#if[key]?yes}        Expands to yes if -Kkey specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#     {#if[key]?yes:no}     Expands to yes if -Kkey, otherwise no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#     {#if[!key]?yes}       Expands to yes if -Kother
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#     {#if[!key]?yes:no}    Expands to yes if -Kother, otherwise no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#     $var$                 Expands to value if -Dvar=value given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#     yes, no must not contain whitespace
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
# If the environment variable SED is defined, uses that instead of sed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
# If the environment variable NAWK is defined, uses that instead of awk
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
SED=${SED:-sed}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
NAWK=${NAWK:-awk}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
# Map a string of the form -Dvar=value into an appropriate sed command
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
subst() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  # The first two lines are to avoid the direct use of echo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  # which does not treat backslashes consistently across platforms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  echo '' \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    | $SED -e "s.*$*" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    | $SED -e 's-D\([a-zA-Z_][-a-zA-Z_]*\)=\(.*\)'"s\\\\\$\\1\\\\\$\2gg" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
           -e 's-D\([a-zA-Z_][-a-zA-Z_]*\)'"s\\\\\$\\1\\\\\$1gg" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
           -e 's/ //g'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
es=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
be=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
keys=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
key1=_1_
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
key2=_2_
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
while [ $# -gt 0 ]; do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
  case "$1" in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    -be)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      be='-e 1,/^#begin$/d -e /^#end$/,$d'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
      ;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    -D*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      es="$es -e `subst $1`"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      ;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    -K*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      nk=`echo $1 | $SED -e 's/-K//'`
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      if [ "x$keys" = x ]; then keys="$nk"; else keys="$keys $nk"; fi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      if [ "x$key1" = x_1_ ]; then key1="$nk";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      elif [ "x$key2" = x_2_ ]; then key2="$nk"; fi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      ;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    *)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      echo "Usage: $0 [-be] [-Kkey] -Dvar=value ... <in >out"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      exit 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      ;;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  esac
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  shift
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
done
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
text='[-a-zA-Z0-9&;,.<>/#() ]'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
$SED $es \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  -e 's// /g' \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  -e "s@^#warn .*@// -- This file was mechanically generated: Do not edit! -- //@" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  -e 's-// ##.*$--' $be \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  -e "s/{#if\[$key1\]?\($text*\):\($text*\)}/\1/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  -e "s/{#if\[!$key1\]?\($text*\):\($text*\)}/\2/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  -e "s/{#if\[$key1\]?\($text*\)}/\1/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  -e "s/{#if\[!$key1\]?\($text*\)}//g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
  -e "s/{#if\[$key2\]?\($text*\):\($text*\)}/\1/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  -e "s/{#if\[!$key2\]?\($text*\):\($text*\)}/\2/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  -e "s/{#if\[$key2\]?\($text*\)}/\1/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
  -e "s/{#if\[!$key2\]?\($text*\)}//g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
  -e "s/{#if\[[a-z]*\]?\($text*\):\($text*\)}/\2/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
  -e "s/{#if\[![a-z]*\]?\($text*\):\($text*\)}/\1/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  -e "s/{#if\[[a-z]*\]?\($text*\)}//g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  -e "s/{#if\[![a-z]*\]?\($text*\)}/\1/g" \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
| $NAWK \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  'function key(s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     i = match(s, "[a-zA-Z][a-zA-Z]*\\]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     if (i > 0) return substr(s, i, RLENGTH - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     return "XYZZY"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
   function neg(s) { return match(s, "!") > 0; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
   BEGIN {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     KEYS = "'"$keys"'"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     n = split(KEYS, ks, "  *");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     for (i = 1; i <= n; i++) keys[ks[i]] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     top = 1; copy[top] = 1 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
   /^#if\[!?[a-zA-Z][a-zA-Z]*\]/ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     { k = key($0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
       n = neg($0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
       stack[++top] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
       if ((k in keys) == !n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
	 copy[top] = copy[top - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
       } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
	 copy[top] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
       print ""; next }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
   /^#else\[!?[a-zA-Z][a-zA-Z]*\]/ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     { k = key($0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
       if (stack[top] == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
	 copy[top] = copy[top - 1] && !copy[top];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
       } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
	 printf "%d: Mismatched #else key\n", NR | "cat 1>&2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
	 exit 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
       print ""; next }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
   /^#end\[!?[a-zA-Z][a-zA-Z]*\]/ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     { k = key($0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
       if (stack[top] == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
	 top--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
       } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
	 printf "%d: Mismatched #end key\n", NR | "cat 1>&2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
	 exit 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
       print ""; next }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
   /^#/ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     printf "%d: Malformed #directive\n", NR | "cat 1>&2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     exit 11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
   { if (copy[top]) print; else print "" }'