Root/dsv/dsv

1#!/bin/sh
2#
3# dsv - Improved data sheet viewer
4#
5# Written 2010-2014 by Werner Almesberger
6# Copyright 2010-2014 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14#
15# Theory of operation:
16#
17# We download data sheets from the Internet into local caches and provide a
18# quick access mechanism for viewing the data sheets. A cache is a directory
19# called .dsv/. Caches can be hierarchical. "dsv setup" generates a cache in
20# the local directory. "dsv <component>" and "dsv ls" search the hierarchy,
21# starting with the current directory.
22#
23# Caches contain two types of files: dsv-<ref> is the name or alias with
24# which a data sheet is referenced. <ref>-<filename> is the actual data
25# sheet, with <filename> being the name of the file we downloaded.
26#
27
28DSV_DIR=.dsv
29
30
31usage()
32{
33    echo "usage: $0 [-p] [-u] <component>" 2>&1
34    echo " $0 help" 2>&1
35    echo " $0 [ls]" 2>&1
36    echo " $0 setup <info-file> ..." 2>&1
37    echo 2>&1
38    echo " -p show the path instead of displaying the file " 2>&1
39    echo " -u show source URL instead of displaying the file " 2>&1
40    exit 1
41}
42
43
44do_wget()
45{
46    wget -nv -U "dsv/1.0" "$@"
47}
48
49
50up()
51{
52    old=`pwd`
53    cd ..
54    new=`pwd`
55    [ "$old" != "$new" ]
56}
57
58
59flush()
60{
61    eval nm=$name
62    nm=`echo "$nm" | sed 's/%/%25/g;s|/|%2F|g'`
63    [ -z "$nm" ] && return
64    if [ -z "$url" ]; then
65        echo "$nm: no URL" 2>&1
66        exit 1
67    fi
68    ds=$nm-`basename "$url"`
69    mkdir -p $DSV_DIR
70    if [ ! -r "$DSV_DIR/$ds" ]; then
71        if [ "$zip" ]; then
72            inside=$zip
73            zip=$ds.zip
74        else
75            inside=${url#*.[Zz][Ii][Pp] }
76        fi
77        if [ "$inside" = "$url" ]; then
78            do_wget -O "$DSV_DIR/$ds" "$url"
79            # @@@ should handle error
80        else
81            if [ -z "$zip" ]; then
82                url=${url%`echo x"$inside" | sed 's/./?/g'`}
83                zip=$nm-`basename "$url"`
84            fi
85            if [ ! -r "$DSV_DIR/$zip" ]; then
86                do_wget -O "$DSV_DIR/$zip" "$url"
87                # @@@ should handle error
88            fi
89            unzip -p "$DSV_DIR/$zip" "$inside" >"$DSV_DIR/$ds" ||
90                { rm -f "$DSV_DIR/$zip" "$DSV_DIR/$ds"; exit 1; }
91        fi
92    fi
93    eval for n in $name $alias\; do \
94        'nm=`echo "$n" | sed "s/%/%25/g;s|/|%2F|g"`;' \
95        \{ echo '"$ds"'\; echo '"$url"'\; \} '>$DSV_DIR/dsv-$nm'\; \
96        done
97    name=
98    alias=
99    url=
100    zip=
101}
102
103
104set_value()
105{
106    case "$tag" in
107        N:|n:) flush
108            name="\"$value\"";;
109        A:|a:) alias="$alias \"$value\"";;
110        D:|d:) url=$value;;
111        Z:|z:) zip=$value;;
112        "") ;; # first iteration
113        *) echo "unrecognized tag \"$tag\"" 2>&1
114            exit 1;;
115    esac
116    value=
117}
118
119
120eof()
121{
122    flush
123    tag=
124}
125
126
127setup()
128{
129    for n in "$@"; do
130        if [ ! -r "$n" ]; then
131            echo "$n: not found" 2>&1
132            continue
133        fi
134        #
135        # "read" doesn't recognize lines that don't end with a newline.
136        # The cat-sed hack below works around this problems.
137        #
138        cat -E "$n" | sed 's/[^$]$/&\n/;s/$$//' | {
139            while read line; do
140                [ "$line" = "${line###}" ] || continue
141                tmp=`echo "$line" |
142                    awk '/^[^\t ]/ { print $1 }'`
143                tail=`echo "$line" |
144                    sed 's/^[^\t ]*[\t ]*//;s/[\t ]*$//'`
145                if [ -z "$tmp" ]; then
146                    [ -z "$tail" ] || value="$value $tail"
147                else
148                    set_value
149                    tag=$tmp
150                    value=$tail
151                fi
152            done
153            set_value
154            eof
155        }
156    done
157}
158
159
160list()
161{
162    while true; do
163        if [ -d $DSV_DIR ]; then
164            ls -b -1 $DSV_DIR | sed 's/^dsv-//p;d' |
165                sed 's|%2F|/|g;s/%25/%/g'
166        fi
167        up || break
168    done | sort | uniq | column
169}
170
171
172view()
173{
174    ${DSV_VIEWER:-${DSV_PDFVIEWER:-xdg-open}} "$@"
175}
176
177
178search()
179{
180    while true; do
181        if [ -d $DSV_DIR ]; then
182            name=`echo "$1" | sed 's/%/%25/g;s|/|%2F|g'`
183            if [ -r "$DSV_DIR/dsv-$name" ]; then
184                file=`sed 1q "$DSV_DIR/dsv-$name"`
185                if [ ! -r "$DSV_DIR/$file" ]; then
186                    echo "$1 -> $file: does not exist" 2>&1
187                    exit 1
188                fi
189                if $path; then
190                    echo "`pwd`/$DSV_DIR/$file"
191                elif ! $show_url; then
192                    view "$DSV_DIR/$file"
193                fi
194                if $show_url; then
195                    url=`sed 1d "$DSV_DIR/dsv-$name"`
196                    if [ "$url" ]; then
197                        echo "$url"
198                    else
199                        echo "$DSV_DIR/dsv-$name" \
200                            "does not contain a URL" \
201                            "- please regenerate" 1>&2
202                        exit 1
203                    fi
204                fi
205                exit
206            fi
207        fi
208        if ! up; then
209            echo "no data sheet found for \"$1\"" 2>&1
210            exit 1
211        fi
212    done
213}
214
215
216path=false
217show_url=false
218
219while true; do
220    case "$1" in
221        -p) path=true
222            shift;;
223        -u) show_url=true
224            shift;;
225        -*) usage;;
226        *) break;;
227    esac
228done
229
230case "$1" in
231    help|-*) usage;;
232    ""|ls) list;;
233    setup) shift
234            [ "$1" ] || usage
235            setup "$@";;
236    *) search "$@";;
237esac
238

Archive Download this file

Branches:
master



interactive