#!/bin/ksh # # $Id: rcsd.sh,v 1.8 2003/10/07 00:44:46 vogelke Exp $ # # NAME: # rcsd # # SYNOPSIS: # rcsd [-v] [file] # # DESCRIPTION: # Gets the description field from a C program or shell script. # Requires nawk. # # OPTIONS: # "-v" prints the version and exits. # # AUTHOR: # Karl Vogel # Sumaria Systems, Inc. PATH=/bin:/usr/bin:/usr/local/bin:/coll/local/bin:/usr/ccs/lib : ${EDITOR="/bin/vi"} : ${PAGER="/bin/more"} export PATH EDITOR PAGER tmp=`mktemp /tmp/rcsdXXXXXX` test -f $tmp || exit 1 trap "rm -f $tmp; exit 0" 0 1 2 3 15 # ============================= FUNCTIONS ================================== # # FUNCTION: # die # # SYNOPSIS: # die string # # DESCRIPTION: # "die" prints a string to stderr and exits. The most # common use for "die" is with a test: # test -f /etc/passwd || die "cannot find passwd file" tag=`basename $0` die () { echo "$tag: ERROR -- $*" >& 2 exit 1 } # FUNCTION: # version: prints the current version to stdout. version () { vsedscr='s/RCSfile: // s/,v / / s/\$//g' vrevname='$RCSfile: rcsd.sh,v $' echo "$vrevname $vrevstr" | sed -e "$vsedscr" } # FUNCTION: # cprog: gets the comment header from a C program or header. cprog () { code=/tmp/rcsd.code.$$ # Holds just code, no comments. sed -e 's/^#/ #/' $filename | cpp -P | sed -e 's/^ #/#/' | uniq > $code SCR='{ if (match ($0, /^\.$/)) exit else { gsub (/\/\*/, "") gsub (/\*\//, "") gsub (/\*/, "") gsub (/\t/, "") if (NR > 1 && NF > 0 && k == 0) header = header "\n" $0 } } END { k = match (header, /AUTHOR:/) if (k > 0) { k = k + length ("AUTHOR:") + 1 str = substr (header, k) m = match (str, /[A-Z][A-Z]*:/) str = substr (str, 0, m-1) gsub (/\n/, "", str) gsub (/^ /, "", str) print str } else { gsub (/^\n/, "", header) gsub (/^ /, "", header) print header } }' diff -f $code $filename | nawk "$SCR" rm -f $code } # FUNCTION: # shellprog: gets the comment header from a script of some sort. shellprog () { echo got here sed -n -e '/NAME:/,/OPTIONS:/p' $filename | sed -e '/OPTIONS:/d' -e 's/^#//g' } # ============================= MAIN PROGRAM =============================== # Handle command line arguments. set -- `getopt v $*` case "$?" in 0) ;; *) usage "unrecognized option" ;; esac for i in $* do case $i in -v) version; exit 0;; --) shift; break;; esac done case "$#" in 0) filename=$tmp; cat > $filename ;; 1) filename=$1 ;; *) die "I need a filename as an argument." ;; esac # # Find the filetype and get the appropriate section. If we don't have # a recognizable filetype, just grab the first 10 lines. # test -f $filename || die "$filename: can't read" set X `file $filename 2> /dev/null | sed -e 's/^.*: //' -e 's/ /-/g'` filetype="$2" case "$filetype" in [Cc]-program*) cprog $filename ;; *shell-script*) shellprog $filename ;; *) head $filename ;; esac exit 0