chiark / gitweb /
Include stdlib.
[runlisp] / txtlib.in
1 #! /bin/sh
2
3 # -*-sh-*-
4 #
5 # $Id: txtlib.in,v 1.2 1999/11/11 17:49:15 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.2  1999/11/11 17:49:15  mdw
34 # Regular expression fixes for parsing version numbers.
35 #
36 # Revision 1.1.1.1  1999/05/05 19:23:47  mdw
37 # New import.  The old CVS repository was lost in a disk disaster.
38 #
39
40 # --- Handle command line arguments ---
41
42 files=""
43 mode=x
44 out=""
45
46 # --- Parse command line arguments ---
47
48 while [ $# -gt 0 ]; do
49   case $1 in
50     -h | --h | --he | --hel | --help)
51       cat <<EOF
52 Usage: txtlib [-lx] [-o FILE] [LIBRARY...]
53
54 In \`extract' mode (-x, default), extracts chunks named on standard input
55 from the list of libraries, and writes the result to standard output.
56
57 In \`list' mode (-l), lists the chunks defined in the text libraries given.
58
59 Options:
60
61 -h, --help              Print this help text.
62 -v, --version           Print the program's version number.
63 -l, --list              List chunks defined in text libraries.
64 -x, --extract           Extract chunks from text libraries (default).
65 -o, --output=FILE       Extract chunks to FILE, not standard output.
66 EOF
67       exit 0
68       ;;
69     -v | --v | --ve | --ver | --vers | --versi | --versio | --version)
70       version=`echo '$Revision: 1.2 $' |
71         sed -n -e 's;^.*: \([0-9.]*\) *\\$;\1;p'`
72       echo "txtlib $version; Common Files Distribution version @VERSION@"
73       exit 0
74       ;;
75     -o | --o | --ou | --out | --outp | --outpu | --output)
76       out="$2";
77       shift
78       ;;
79     -o*)
80       out=`echo $1 | sed -e 's/^-[a-z]//'`
81       ;;
82     --o=* | --ou=* | --out=* | --outp=* | --outpu=* | --output=*)
83       out=`echo $1 | sed -e 's/^--[a-z]*=//'`
84       ;;
85     -l | --l | --li | --lis | --list)
86       mode=l
87       ;;
88     -x | --e | --ex | --ext | --extr | --extra | --extrac | --extract)
89       mode=x
90       ;;
91     --)
92       shift
93       break
94       ;;
95     -)
96       break
97       ;;
98     -*)
99       echo "txtlib: unknown option \`$1'" >&2
100       exit 1
101       ;;
102     *)
103       break
104       ;;
105   esac
106   shift
107 done
108
109 test "$out" = "-" && out=""
110
111 # --- Build a `sed' script ---
112
113 case $mode in
114   l)
115     sed -n -e "/^.*\*@-\([-a-zA-Z0-9_]*\)-@\*.*$/ s//\1/p" "$@"
116     ;;
117   x)
118     t=/tmp/txtlib.$$
119     if mkdir -m 700 $t; then :
120     else
121       echo >&2 "txtlib: could not create temporary directory"
122       exit 1
123     fi
124     sedfile=/$t/sed
125     while read LINE; do
126       echo "/\*@-$LINE-@\*/,/\*@-#-@\*/ p"
127     done >$sedfile
128     test -z "$out" || exec >$out
129     sed -e '/\*@-[-a-zA-Z0-9_]*-@\*/ i\
130     *@-#-@*' "$@" | sed -n -f $sedfile | sed -e '/\*@-#-@\*/ d'
131     rm -rf $t
132     ;;
133 esac