#!/bin/ksh # ============================================================================ # pgm: rfsdoc_index .. Search an index text file for the RFS documentation # # use: rfsdoc_index [-c] [-f fil] [[-e editor] or [-s str] or [string]] # # in: -c .......... (optional) if given with the string option, be case # in: specific in searches (i.e. turn off the grep -i option) # in: -e editor ... (optional) if given, use either the given editor, or the # in: default editor defined by env variable EDITOR to edit # in: the RFS documentation index file; thus "-e" alone # in: uses the default editor # in: -s str ...... (optional) if given, use grep to find all occurrences of # in: the given string in the RFS documentation index file # in: -f fil ...... (optional) if given, output lines found in the search # in: into the file given by "fil"; if "fil" exists, it is # in: appended with the new lines find # in: string ...... (optional) if the string is given without a minus option, # in: it is the same as the "-s" option - the RFS doc file # in: is searched for all occurrences of the string # out: (stdout) .... all output for searches and for errors goes to stdout # # cmt: If no arguments are given, a minue of the above options is given. # cmt: For all searches case is NOT considered (i.e. the -i option is used # cmt: in the grep searches). # cmt: If "-" or "--" is given, options are turned off and the next command # cmt: line string will be used as the search string (i.e. so that a string # cmt: starting with a minus can be used). # ============================================================================ type dd_help 1>/dev/null 2>&1 && . dd_help 2>/dev/null ## Set index file name and alternate (obsolete) file name. ## Set index file directory name and again an old alterante name. ## Set the apps-token used to find the directory name. PriFilnam="nwsrfs_outline" SecFilnam="rfsdoc_index.txt" RfsDocTkn="rfs_doc" dirname=$(gad $RfsDocTkn) filename="" ## If the directory is found, obtain the most current file. ## If a file is not found set an error code. Err=0 if [ ! -d "$dirname" ]; then Err=1 else PriDirnam="$dirname" SecDirnam="$dirname" [ -d "$SecDirnam/txt" ] && SecDirnam="$SecDirnam/txt" if [ -f "$PriDirnam/$PriFilnam" ]; then filename="$PriDirnam/$PriFilnam" elif [ -f "$PriDirnam/$SecFilnam" ]; then filename="$PriDirnam/$SecFilnam" elif [ -f "$SecDirnam/$SecFilnam" ]; then filename="$SecDirnam/$SecFilnam" elif [ -f "$SecDirnam/$PriFilnam" ]; then filename="$SecDirnam/$PriFilnam" else filename=$(ls -1t $PriDirnam/$PriFilnam.release* 2>/dev/null | sed -n 1p) if [ -z "$filename" ]; then Err=2 elif [ ! -r "$filename" ]; then Err=3 fi fi fi ## Output header, then check if an error has occurred and end ## if the index file cannot be found. echo echo "Script to search the NWSRFS User's Manual outline to find strings." if [ $Err = 0 ] ; then echo echo "NOTE: file to be searched: $filename" #set -o xtrace filenamez=$filename while [ "$filenamez" != "" ] ; do if [ -L $filenamez ] ; then string=$(ls -l $filenamez) #print "string=$string" linkname=$(echo $string | cut -d" " -f11) #print "linkname=$linkname" print " which is linked to : $linkname" filenamez=$linkname else break fi done fi if [ $Err != 0 ]; then echo if [ $Err = 1 ] ; then echo "ERROR: Cannot find the documentation directory defined by" echo " apps-token \"$RfsDocTkn\" as \"$dirname\"!" elif [ $Err = 2 ] ; then echo "ERROR: Neither file \"$PriFilnam\" nor \"$SecFilnam\" was found in" [ "$PriDirnam" = "$SecDirnam" ] \ && echo " directory \"$PriDirnam\"!" \ || echo " directories \"$PriDirnam\" or \"SecDirnam\"!" elif [ $Err = 3 ] ; then echo "ERROR: Cannot read file \"$filename\"!" fi echo exit $Err fi ## If no command line input is given, output to stdout a menu of ## options and prompt the user for input; then reset the command ## line. if [ "$#" = 0 ]; then echo echo "The following options can be used on the command line but you can" echo "also use them here or type (q) to exit:" echo echo " -e [editor] ...... to edit the index file with your" echo " favorite editor (default is var EDITOR)" echo " -s [string] ...... (or just type the \"string\" alone) to" echo " grep for all occurrences in the index" echo " -f [filenm] ...... to write or append search lines found" echo " to the given filename (not used with" echo " option -e)" echo " -c ............... if given with the string option, use case" echo " specific searches" echo Ans="" while [ -z "$Ans" ]; do printf "Enter options (or q): " read Ans if [ "$Ans" = "" ] ; then Ans="-s" fi done echo [[ "$Ans" = [qQ]* ]] && { echo; exit $Err; } set -- $Ans fi ## Choose the given option and execute: ## if -e ... then get the default or given editor and use the ## "exec" script command to leave this script and use ## the editor to search the index file, ## if -s ... (or if - or --) then use the given string or later ## prompt for a string and use grep or fgrep to search ## for that string. ## if -f ... then reset output action variable to output the ## given file defined by the next argument ## if -c ... then reset the grep option to null so that case ## makes a difference grepopt="-i" Outtype_file="" [ "`uname`" = Linux ] && Outtype="| more -d" || Outtype="| more -d -e" ifindstr=0 while [[ "$1" = -* ]]; do if [[ "$1" = - || "$1" = -- ]]; then shift; break elif [[ "$1" = -c ]]; then shift; grepopt="" elif [[ "$1" = -e ]]; then shift Editr=$EDITOR [[ -n "$1" ]] && [[ "$1" != -* ]] && { Editr=$1; shift; } u_Sed='/not found/d;/.* \([^ ][^ ]*\) *$/s//\1/p' Editr=$(type "$Editr" 2>/dev/null | sed -n "$u_Sed") if [[ -n "$Editr" ]]; then echo echo "Run editor \"$Editr\" on file \"$filename\"." echo exec $Editr $filename fi Err=4 elif [[ "$1" = -s || "$1" = - || "$1" = -- ]]; then shift if [[ -n "$1" ]]; then [[ "$1" = -c ]] && { grepopt=""; shift; } [[ "$1" = - || "$1" = -- ]] && shift findstr="$1" shift ifindstr=1 else # Err=5 : fi elif [[ "$1" = -f && -n "$2" ]]; then Outtype=">> $2" Outtype_file="$2" shift 2 else Err=6 shift break fi done if [ $Err != 0 ]; then if [ $Err = 6 ]; then echo "ERROR: A bad command line option was given!" echo exit $Err fi fi if [ -n "$1" ]; then findstr="$1" ifindstr=1 fi set +o xtrace # get script name scrname=${0##*/} #set -o xtrace if [ "$grepopt" = "-i" ] ; then echo "NOTE: uppercase and lowercase distinctions will be ignored." else echo "NOTE: uppercase and lowercase will be considered." fi print quote='"' ; string="double" print "NOTE: enclose search string in $string quotes ($quote)" \ "if it contains blanks." nfindstr=0 ido=1 while [ $ido = 1 ] ; do # set commands to execute if interrupt encountered signal=2 trap " \ print 'NOTE: in $scrname - interrupt encountered.' ; \ exit ; \ " \ $signal if [ $ifindstr = 0 ] ; then echo read findstr?"Enter search string or to quit: " if [ "$findstr" = "" ] ; then exit 1 fi fi # set commands to execute if interrupt encountered signal=2 trap " \ print 'NOTE: in $scrname - interrupt encountered.' ; \ continue ; \ " \ $signal if [ $nfindstr = 0 ] ; then # print header print "Document Identifier " \ "Release Date " \ "Document Title" print "________________________________ " \ "____________ " \ "______________" let nfindstr=$nfindstr+1 fi # search for string #set -o xtrace cmdstr="grep $grepopt "$findstr" $filename" eval $cmdstr 1>/dev/null 2>/dev/null condcode=$? if [ $condcode -eq 0 ] ; then # [ "`uname`" = Linux ] && Eopt="" || Eopt="-e" eval $cmdstr $Outtype if [ -n "$Outtype_file" ]; then if [ -f $Outtype_file ]; then echo " File \"$Outtype_file\" created or appended." fi fi else print "String '$findstr' not found." fi ifindstr=0 done