2010-10-04 03:54:57 +03:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# dsv - Improved data sheet viewer
|
|
|
|
#
|
2016-03-01 03:44:26 +02:00
|
|
|
# Written 2010-2014, 2016 by Werner Almesberger
|
|
|
|
# Copyright 2010-2014, 2016 Werner Almesberger
|
2010-10-20 13:56:10 +03:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2010-10-04 03:54:57 +03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Theory of operation:
|
|
|
|
#
|
|
|
|
# We download data sheets from the Internet into local caches and provide a
|
|
|
|
# quick access mechanism for viewing the data sheets. A cache is a directory
|
|
|
|
# called .dsv/. Caches can be hierarchical. "dsv setup" generates a cache in
|
|
|
|
# the local directory. "dsv <component>" and "dsv ls" search the hierarchy,
|
|
|
|
# starting with the current directory.
|
|
|
|
#
|
|
|
|
# Caches contain two types of files: dsv-<ref> is the name or alias with
|
|
|
|
# which a data sheet is referenced. <ref>-<filename> is the actual data
|
|
|
|
# sheet, with <filename> being the name of the file we downloaded.
|
|
|
|
#
|
|
|
|
|
|
|
|
DSV_DIR=.dsv
|
|
|
|
|
|
|
|
|
|
|
|
usage()
|
|
|
|
{
|
2014-12-24 22:35:08 +02:00
|
|
|
echo "usage: $0 [-p] [-u] <component>" 1>&2
|
|
|
|
echo " $0 help" 1>&2
|
|
|
|
echo " $0 [ls]" 1>&2
|
|
|
|
echo " $0 setup <info-file> ..." 1>&2
|
|
|
|
echo 1>&2
|
|
|
|
echo " -p show the path instead of displaying the file " 1>&2
|
|
|
|
echo " -u show source URL instead of displaying the file " 1>&2
|
2014-03-07 13:34:15 +02:00
|
|
|
exit 1
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-10 17:55:49 +03:00
|
|
|
do_wget()
|
|
|
|
{
|
|
|
|
wget -nv -U "dsv/1.0" "$@"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-04 03:54:57 +03:00
|
|
|
up()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
old=`pwd`
|
|
|
|
cd ..
|
|
|
|
new=`pwd`
|
|
|
|
[ "$old" != "$new" ]
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
flush()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
eval nm=$name
|
|
|
|
nm=`echo "$nm" | sed 's/%/%25/g;s|/|%2F|g'`
|
|
|
|
[ -z "$nm" ] && return
|
|
|
|
if [ -z "$url" ]; then
|
2014-12-24 22:35:08 +02:00
|
|
|
echo "$nm: no URL" 1>&2
|
2014-03-07 13:34:15 +02:00
|
|
|
exit 1
|
2014-03-07 13:19:28 +02:00
|
|
|
fi
|
2016-03-01 03:44:26 +02:00
|
|
|
ds=$nm-`basename "$url" | tr '?&' _`
|
2014-03-07 13:34:15 +02:00
|
|
|
mkdir -p $DSV_DIR
|
|
|
|
if [ ! -r "$DSV_DIR/$ds" ]; then
|
|
|
|
if [ "$zip" ]; then
|
|
|
|
inside=$zip
|
|
|
|
zip=$ds.zip
|
|
|
|
else
|
|
|
|
inside=${url#*.[Zz][Ii][Pp] }
|
2014-03-07 13:19:28 +02:00
|
|
|
fi
|
2014-03-07 13:34:15 +02:00
|
|
|
if [ "$inside" = "$url" ]; then
|
|
|
|
do_wget -O "$DSV_DIR/$ds" "$url"
|
2012-07-20 15:48:36 +03:00
|
|
|
# @@@ should handle error
|
2014-03-07 13:34:15 +02:00
|
|
|
else
|
|
|
|
if [ -z "$zip" ]; then
|
|
|
|
url=${url%`echo x"$inside" | sed 's/./?/g'`}
|
|
|
|
zip=$nm-`basename "$url"`
|
|
|
|
fi
|
|
|
|
if [ ! -r "$DSV_DIR/$zip" ]; then
|
|
|
|
do_wget -O "$DSV_DIR/$zip" "$url"
|
|
|
|
# @@@ should handle error
|
|
|
|
fi
|
|
|
|
unzip -p "$DSV_DIR/$zip" "$inside" >"$DSV_DIR/$ds" ||
|
|
|
|
{ rm -f "$DSV_DIR/$zip" "$DSV_DIR/$ds"; exit 1; }
|
2012-07-20 15:48:36 +03:00
|
|
|
fi
|
2012-07-19 17:29:49 +03:00
|
|
|
fi
|
2014-03-07 13:34:15 +02:00
|
|
|
eval for n in $name $alias\; do \
|
|
|
|
'nm=`echo "$n" | sed "s/%/%25/g;s|/|%2F|g"`;' \
|
|
|
|
\{ echo '"$ds"'\; echo '"$url"'\; \} '>$DSV_DIR/dsv-$nm'\; \
|
|
|
|
done
|
|
|
|
name=
|
|
|
|
alias=
|
|
|
|
url=
|
|
|
|
zip=
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set_value()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
case "$tag" in
|
|
|
|
N:|n:) flush
|
|
|
|
name="\"$value\"";;
|
|
|
|
A:|a:) alias="$alias \"$value\"";;
|
|
|
|
D:|d:) url=$value;;
|
|
|
|
Z:|z:) zip=$value;;
|
|
|
|
"") ;; # first iteration
|
2014-12-24 22:35:08 +02:00
|
|
|
*) echo "unrecognized tag \"$tag\"" 1>&2
|
2014-03-07 13:34:15 +02:00
|
|
|
exit 1;;
|
|
|
|
esac
|
|
|
|
value=
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
eof()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
flush
|
|
|
|
tag=
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setup()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
for n in "$@"; do
|
|
|
|
if [ ! -r "$n" ]; then
|
2014-12-24 22:35:08 +02:00
|
|
|
echo "$n: not found" 1>&2
|
2014-03-07 13:34:15 +02:00
|
|
|
continue
|
2010-12-01 00:27:59 +02:00
|
|
|
fi
|
2014-03-07 13:34:15 +02:00
|
|
|
#
|
|
|
|
# "read" doesn't recognize lines that don't end with a newline.
|
|
|
|
# The cat-sed hack below works around this problems.
|
|
|
|
#
|
|
|
|
cat -E "$n" | sed 's/[^$]$/&\n/;s/$$//' | {
|
|
|
|
while read line; do
|
|
|
|
[ "$line" = "${line###}" ] || continue
|
|
|
|
tmp=`echo "$line" |
|
|
|
|
awk '/^[^\t ]/ { print $1 }'`
|
|
|
|
tail=`echo "$line" |
|
|
|
|
sed 's/^[^\t ]*[\t ]*//;s/[\t ]*$//'`
|
|
|
|
if [ -z "$tmp" ]; then
|
|
|
|
[ -z "$tail" ] || value="$value $tail"
|
|
|
|
else
|
|
|
|
set_value
|
|
|
|
tag=$tmp
|
|
|
|
value=$tail
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
set_value
|
|
|
|
eof
|
|
|
|
}
|
|
|
|
done
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
while true; do
|
|
|
|
if [ -d $DSV_DIR ]; then
|
|
|
|
ls -b -1 $DSV_DIR | sed 's/^dsv-//p;d' |
|
|
|
|
sed 's|%2F|/|g;s/%25/%/g'
|
|
|
|
fi
|
|
|
|
up || break
|
|
|
|
done | sort | uniq | column
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
view()
|
|
|
|
{
|
|
|
|
${DSV_VIEWER:-${DSV_PDFVIEWER:-xdg-open}} "$@"
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
search()
|
|
|
|
{
|
2014-03-07 13:34:15 +02:00
|
|
|
while true; do
|
|
|
|
if [ -d $DSV_DIR ]; then
|
|
|
|
name=`echo "$1" | sed 's/%/%25/g;s|/|%2F|g'`
|
|
|
|
if [ -r "$DSV_DIR/dsv-$name" ]; then
|
|
|
|
file=`sed 1q "$DSV_DIR/dsv-$name"`
|
|
|
|
if [ ! -r "$DSV_DIR/$file" ]; then
|
2014-12-24 22:35:08 +02:00
|
|
|
echo "$1 -> $file: does not exist" 1>&2
|
2014-03-07 13:34:15 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
if $path; then
|
|
|
|
echo "`pwd`/$DSV_DIR/$file"
|
|
|
|
elif ! $show_url; then
|
|
|
|
view "$DSV_DIR/$file"
|
|
|
|
fi
|
|
|
|
if $show_url; then
|
|
|
|
url=`sed 1d "$DSV_DIR/dsv-$name"`
|
|
|
|
if [ "$url" ]; then
|
|
|
|
echo "$url"
|
|
|
|
else
|
|
|
|
echo "$DSV_DIR/dsv-$name" \
|
|
|
|
"does not contain a URL" \
|
|
|
|
"- please regenerate" 1>&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
exit
|
|
|
|
fi
|
2012-12-23 03:31:27 +02:00
|
|
|
fi
|
2014-03-07 13:34:15 +02:00
|
|
|
if ! up; then
|
2014-12-24 22:35:08 +02:00
|
|
|
echo "no data sheet found for \"$1\"" 1>&2
|
2012-12-21 22:33:23 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2014-03-07 13:34:15 +02:00
|
|
|
done
|
2010-10-04 03:54:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-16 19:55:19 +03:00
|
|
|
path=false
|
2012-12-21 22:33:23 +02:00
|
|
|
show_url=false
|
2012-12-23 03:31:27 +02:00
|
|
|
|
|
|
|
while true; do
|
2014-03-07 13:34:15 +02:00
|
|
|
case "$1" in
|
|
|
|
-p) path=true
|
|
|
|
shift;;
|
|
|
|
-u) show_url=true
|
|
|
|
shift;;
|
|
|
|
-*) usage;;
|
|
|
|
*) break;;
|
|
|
|
esac
|
2012-12-23 03:31:27 +02:00
|
|
|
done
|
2011-08-16 19:55:19 +03:00
|
|
|
|
2010-10-04 03:54:57 +03:00
|
|
|
case "$1" in
|
2014-03-07 13:34:15 +02:00
|
|
|
help|-*) usage;;
|
|
|
|
""|ls) list;;
|
|
|
|
setup) shift
|
|
|
|
[ "$1" ] || usage
|
|
|
|
setup "$@";;
|
|
|
|
*) search "$@";;
|
2010-10-04 03:54:57 +03:00
|
|
|
esac
|