INIT.2014-12-24
[INIT.git] / src / cmd / INIT / filter.sh
blobb870f8597ab15ba4c610543bb512c5ba84637971
1 ########################################################################
2 # #
3 # This software is part of the ast package #
4 # Copyright (c) 1994-2016 AT&T Intellectual Property #
5 # and is licensed under the #
6 # Eclipse Public License, Version 1.0 #
7 # by AT&T Intellectual Property #
8 # #
9 # A copy of the License is available at #
10 # http://www.eclipse.org/org/documents/epl-v10.html #
11 # (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12 # #
13 # Information and Software Systems Research #
14 # AT&T Research #
15 # Florham Park NJ #
16 # #
17 # Glenn Fowler <glenn.s.fowler@gmail.com> #
18 # #
19 ########################################################################
20 ########################################################################
21 # #
22 # This software is part of the ast package #
23 # Copyright (c) 1994-2011 AT&T Intellectual Property #
24 # and is licensed under the #
25 # Eclipse Public License, Version 1.0 #
26 # by AT&T Intellectual Property #
27 # #
28 # A copy of the License is available at #
29 # http://www.eclipse.org/org/documents/epl-v10.html #
30 # (with md5 checksum b35adb5213ca9657e911e9befb180842) #
31 # #
32 # Information and Software Systems Research #
33 # AT&T Research #
34 # Florham Park NJ #
35 # #
36 # Glenn Fowler <glenn.s.fowler@gmail.com> #
37 # #
38 ########################################################################
39 : convert command that operates on file args to pipeline filter
41 command=filter
43 tmp=/tmp/$command$$
44 suf=
46 case `(getopts '[-][123:xyz]' opt --xyz; echo 0$opt) 2>/dev/null` in
47 0123) ARGV0="-a $command"
48 USAGE=$'
49 [-?
50 @(#)$Id: filter (AT&T Labs Research) 2001-05-31 $
52 '$USAGE_LICENSE$'
53 [+NAME?filter - run a command in stdin/stdout mode]
54 [+DESCRIPTION?\bfilter\b runs \acommand\a in a mode that takes input from
55 the \afile\a operands, or from the standard input if no \afile\a
56 operands are specified, and writes the results to the standard output.
57 It can be used to run commands like \bsplit\b(1), that normally modify
58 \afile\a operands in-place, in pipelines. The \afile\a operands are
59 not modified; \acommand\a is run on copies in \b/tmp\b.]
61 command [ option ... ] [ file ... ]
63 [+SEE ALSO?\bstrip\b(1)]
66 *) ARGV0=""
67 USAGE="command [ option ... ] [ file ... ]"
69 esac
71 usage()
73 OPTIND=0
74 getopts $ARGV0 "$USAGE" OPT '-?'
75 exit 2
78 while getopts $ARGV0 "$USAGE" OPT
79 do case $OPT in
80 *) usage ;;
81 esac
82 done
83 shift `expr $OPTIND - 1`
84 case $# in
85 0) usage ;;
86 esac
88 cmd=$1
89 while :
90 do shift
91 case $# in
92 0) break ;;
93 esac
94 case $1 in
95 -*) cmd="$cmd $1" ;;
96 *) break ;;
97 esac
98 done
99 trap 'rm -f $tmp$suf' 0 1 2 3 15
100 case $# in
101 0) cat > $tmp
102 $cmd $tmp
104 *) for file
105 do suf=${file##*/}
106 case $suf in
107 *.*) suf=.${suf#*.} ;;
108 *) suf= ;;
109 esac
110 cp $file $tmp$suf || exit 1
111 chmod u+rwx $tmp$suf || exit 1
112 $cmd $tmp$suf || exit 1
113 cat $tmp$suf
114 rm -f $tmp$suf
115 done
117 esac