Root/dsv/dsv

1#!/bin/sh
2#
3# dsv - Improved data sheet viewer
4#
5# Written 2010-2013 by Werner Almesberger
6# Copyright 2010-2013 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    inside=${url#*.[Zz][Ii][Pp] }
72    if [ "$inside" = "$url" ]; then
73        do_wget -O "$DSV_DIR/$ds" "$url"
74        # @@@ should handle error
75    else
76        url=${url%`echo x"$inside" | sed 's/./?/g'`}
77        zip=$nm-`basename "$url"`
78        if [ ! -r "$DSV_DIR/$zip" ]; then
79            do_wget -O "$DSV_DIR/$zip" "$url"
80            # @@@ should handle error
81        fi
82        unzip -p "$DSV_DIR/$zip" "$inside" >"$DSV_DIR/$ds" ||
83            { rm -f "$DSV_DIR/$zip" "$DSV_DIR/$ds"; exit 1; }
84    fi
85    fi
86    eval for n in $name $alias\; do \
87        'nm=`echo "$n" | sed "s/%/%25/g;s|/|%2F|g"`;' \
88    \{ echo '"$ds"'\; echo '"$url"'\; \} '>$DSV_DIR/dsv-$nm'\; \
89    done
90    name=
91    alias=
92    url=
93}
94
95
96set_value()
97{
98    case "$tag" in
99    N:|n:) flush
100        name="\"$value\"";;
101    A:|a:) alias="$alias \"$value\"";;
102    D:|d:) url=$value;;
103    "")
104        ;; # first iteration
105    *) echo "unrecognized tag \"$tag\"" 2>&1
106        exit 1;;
107    esac
108    value=
109}
110
111
112eof()
113{
114    flush
115    tag=
116}
117
118
119setup()
120{
121    for n in "$@"; do
122    if [ ! -r "$n" ]; then
123        echo "$n: not found" 2>&1
124        continue
125    fi
126    #
127    # "read" doesn't recognize lines that don't end with a newline.
128    # The cat-sed hack below works around this problems.
129    #
130    cat -E "$n" | sed 's/[^$]$/&\n/;s/$$//' | {
131        while read line; do
132        [ "$line" = "${line###}" ] || continue
133        tmp=`echo "$line" | awk '/^[^\t ]/ { print $1 }'`
134        tail=`echo "$line" | sed 's/^[^\t ]*[\t ]*//;s/[\t ]*$//'`
135        if [ -z "$tmp" ]; then
136            [ -z "$tail" ] || value="$value $tail"
137        else
138            set_value
139            tag=$tmp
140            value=$tail
141        fi
142        done
143        set_value
144        eof
145    }
146    done
147}
148
149
150list()
151{
152     while true; do
153    if [ -d $DSV_DIR ]; then
154        ls -b -1 $DSV_DIR | sed 's/^dsv-//p;d' | sed 's|%2F|/|g;s/%25/%/g'
155    fi
156    up || break
157     done | sort | uniq | column
158}
159
160
161search()
162{
163    while true; do
164    if [ -d $DSV_DIR ]; then
165            name=`echo "$1" | sed 's/%/%25/g;s|/|%2F|g'`
166        if [ -r "$DSV_DIR/dsv-$name" ]; then
167        file=`sed 1q "$DSV_DIR/dsv-$name"`
168        if [ ! -r "$DSV_DIR/$file" ]; then
169            echo "$1 -> $file: does not exist" 2>&1
170            exit 1
171        fi
172        if $path; then
173            echo "`pwd`/$DSV_DIR/$file"
174        elif ! $show_url; then
175            ${DSV_VIEWER:-${DSV_PDFVIEWER:-xdg-open}} "$DSV_DIR/$file"
176        fi
177        if $show_url; then
178            url=`sed 1d "$DSV_DIR/dsv-$name"`
179            if [ "$url" ]; then
180            echo "$url"
181            else
182            echo "$DSV_DIR/dsv-$name" \
183              "does not contain an URL - please regenerate" 1>&2
184            exit 1
185            fi
186        fi
187        exit
188        fi
189    fi
190    if ! up; then
191        echo "no data sheet found for \"$1\"" 2>&1
192        exit 1
193    fi
194    done
195}
196
197
198path=false
199show_url=false
200
201while true; do
202    case "$1" in
203    -p) path=true
204    shift;;
205    -u) show_url=true
206    shift;;
207    -*) usage;;
208    *) break;;
209    esac
210done
211
212case "$1" in
213    help|-*) usage;;
214    ""|ls) list;;
215    setup) shift
216        [ "$1" ] || usage
217        setup "$@";;
218    *) search "$@";;
219esac
220

Archive Download this file

Branches:
master



interactive