chiark / gitweb /
Initial revision
[runlisp] / txtlib.in
1 #! /bin/sh
2
3 # -*-sh-*-
4 #
5 # $Id: txtlib.in,v 1.1 1999/05/05 19:23:47 mdw Exp $
6 #
7 # Manipulate simple libraries of text chunks
8 #
9 # (c) 1997 Mark Wooding
10 #
11
12 #----- Licensing notice -----------------------------------------------------
13 #
14 # This file is part of the Common Files Distribution (`common').
15 #
16 # `Common' is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # `Common' is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with `common'; if not, write to the Free Software Foundation,
28 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29
30 #----- Revision history -----------------------------------------------------
31 #
32 # $Log: txtlib.in,v $
33 # Revision 1.1  1999/05/05 19:23:47  mdw
34 # Initial revision
35 #
36
37 # --- Handle command line arguments ---
38
39 files=""
40 mode=x
41 out=""
42
43 # --- Parse command line arguments ---
44
45 while [ $# -gt 0 ]; do
46   case $1 in
47     -h | --h | --he | --hel | --help)
48       cat <<EOF
49 Usage: txtlib [-lx] [-o FILE] [LIBRARY...]
50
51 In \`extract' mode (-x, default), extracts chunks named on standard input
52 from the list of libraries, and writes the result to standard output.
53
54 In \`list' mode (-l), lists the chunks defined in the text libraries given.
55
56 Options:
57
58 -h, --help              Print this help text.
59 -v, --version           Print the program's version number.
60 -l, --list              List chunks defined in text libraries.
61 -x, --extract           Extract chunks from text libraries (default).
62 -o, --output=FILE       Extract chunks to FILE, not standard output.
63 EOF
64       exit 0
65       ;;
66     -v | --v | --ve | --ver | --vers | --versi | --versio | --version)
67       version=`echo '$Revision: 1.1 $' |
68         sed -n -e 's;^.*: \([0-9.]*\)\\$;\1;p'`
69       echo "txtlib $version; Common Files Distribution version @VERSION@"
70       exit 0
71       ;;
72     -o | --o | --ou | --out | --outp | --outpu | --output)
73       out="$2";
74       shift
75       ;;
76     -o*)
77       out=`echo $1 | sed -e 's/^-[a-z]//'`
78       ;;
79     --o=* | --ou=* | --out=* | --outp=* | --outpu=* | --output=*)
80       out=`echo $1 | sed -e 's/^--[a-z]*=//'`
81       ;;
82     -l | --l | --li | --lis | --list)
83       mode=l
84       ;;
85     -x | --e | --ex | --ext | --extr | --extra | --extrac | --extract)
86       mode=x
87       ;;
88     --)
89       shift
90       break
91       ;;
92     -)
93       break
94       ;;
95     -*)
96       echo "txtlib: unknown option \`$1'" >&2
97       exit 1
98       ;;
99     *)
100       break
101       ;;
102   esac
103   shift
104 done
105
106 test "$out" = "-" && out=""
107
108 # --- Build a `sed' script ---
109
110 case $mode in
111   l)
112     sed -n -e "/^.*\*@-\([-a-zA-Z0-9_]*\)-@\*.*$/ s//\1/p" "$@"
113     ;;
114   x)
115     t=/tmp/txtlib.$$
116     if mkdir -m 700 $t; then :
117     else
118       echo >&2 "txtlib: could not create temporary directory"
119       exit 1
120     fi
121     sedfile=/$t/sed
122     while read LINE; do
123       echo "/\*@-$LINE-@\*/,/\*@-#-@\*/ p"
124     done >$sedfile
125     test -z "$out" || exec >$out
126     sed -e '/\*@-[-a-zA-Z0-9_]*-@\*/ i\
127     *@-#-@*' "$@" | sed -n -f $sedfile | sed -e '/\*@-#-@\*/ d'
128     rm -rf $t
129     ;;
130 esac