Root/scripts/dsv

Source at commit 56a95040ca2fe2a265bd27fce365515f7656383e created 13 years 6 months ago.
By Werner Almesberger, Generate commit entry and show diffs also for the first commit. Plus cleanup.
1#!/bin/sh
2#
3# dsv - Improved data sheet viewer
4#
5
6#
7# Theory of operation:
8#
9# We download data sheets from the Internet into local caches and provide a
10# quick access mechanism for viewing the data sheets. A cache is a directory
11# called .dsv/. Caches can be hierarchical. "dsv setup" generates a cache in
12# the local directory. "dsv <component>" and "dsv ls" search the hierarchy,
13# starting with the current directory.
14#
15# Caches contain two types of files: dsv-<ref> is the name or alias with
16# which a data sheet is referenced. <ref>-<filename> is the actual data
17# sheet, with <filename> being the name of the file we downloaded.
18#
19
20DSV_DIR=.dsv
21
22
23usage()
24{
25    echo "usage: $0 <component>" 2>&1
26    echo " $0 help" 2>&1
27    echo " $0 [ls]" 2>&1
28    echo " $0 setup <info-file> .." 2>&1
29    exit 1
30}
31
32
33up()
34{
35    old="`pwd`"
36    cd ..
37    new="`pwd`"
38    [ "$old" != "$new" ]
39}
40
41
42flush()
43{
44    [ -z "$name" ] && return
45    if [ -z "$url" ]; then
46    echo "$name: no URL" 2>&1
47    exit 1
48    fi
49    ds="$name-`basename "$url"`"
50    mkdir -p $DSV_DIR
51    if [ ! -r "$DSV_DIR/$ds" ]; then
52    wget -nv -O "$DSV_DIR/$ds" "$url"
53    # @@@ should handle error
54    fi
55    for n in $name $alias; do
56    echo "$ds" >$DSV_DIR/dsv-$n
57    done
58    name=
59    alias=
60    url=
61}
62
63
64set_value()
65{
66    case "$tag" in
67    N:|n:) flush
68        name="$value";;
69    A:|a:) alias="$alias $value";;
70    D:|d:) url="$value";;
71    "")
72        ;; # first iteration
73    *) echo "unrecognized tag \"$tag\"" 2>&1
74        exit 1;;
75    esac
76    value=
77}
78
79
80eof()
81{
82    flush
83    tag=
84}
85
86
87setup()
88{
89    for n in "$@"; do
90    if [ ! -r "$n" ]; then
91        echo "$n: not found" 2>&1
92        continue
93    fi
94        while read line; do
95        [ "$line" = "${line###}" ] || continue
96        tmp=`echo "$line" | awk '/^[^\t ]/ { print $1 }'`
97        tail="`echo "$line" | sed 's/^[^\t ]*[\t ]*//'`"
98        if [ -z "$tmp" ]; then
99        [ -z "$tail" ] || value="$value $tail"
100        else
101        set_value
102        tag=$tmp
103        value="$tail"
104        fi
105    done <"$n"
106    set_value
107    eof
108    done
109}
110
111
112list()
113{
114     while true; do
115    if [ -d $DSV_DIR ]; then
116        ls -1 $DSV_DIR | sed 's/^dsv-//p;d'
117    fi
118    up || break
119     done | sort | uniq | column
120}
121
122
123search()
124{
125    while true; do
126    if [ -d $DSV_DIR ]; then
127        if [ -r "$DSV_DIR/dsv-$1" ]; then
128        file="`cat $DSV_DIR/dsv-$1`"
129        if [ ! -r "$DSV_DIR/$file" ]; then
130            echo "$1 -> $file: does not exist" 2>&1
131            exit 1
132        fi
133        xpdf "$DSV_DIR/$file"
134        exit
135        fi
136    fi
137    if ! up; then
138        echo "no data sheet found for \"$1\"" 2>&1
139        exit 1
140    fi
141    done
142}
143
144
145case "$1" in
146    help|-*) usage;;
147    ""|ls) list;;
148    setup) shift
149        setup "$@";;
150    *) search "$@";;
151esac
152

Archive Download this file



interactive