Root/dsv/dsv

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

Archive Download this file

Branches:
master



interactive