# The script_find Script

This appendix contains the complete listing of `script_find` described in Chapter 4, “A Complex Script.” The script executes a second script called `Find.sticky`, which is listed after
`script_find`. There is also a file called `Find.help`, which is a text file accessed when the user clicks the Help button on the main script window. See Chapter 4 for more information on this script.

## Listing for script_find

    #! /usr/dt/bin/dtksh
    set -u

    . /usr/dt/lib/dtksh/DtFuncs.dtsh

    #
    # This sample shell script provides a graphical interface to the
    # `find' command. Each time it is executed, it will attempt to
    # restore the dialog to the last set of values entered by the user.
    # When the `find' command is initiated, the output will be displayed
    # in a dtterm window.
    #

    #
    # Post an# error dialog. The main application window is disabled
    # until the error dialog is unposted. The message to be displayed
    # in the # error dialog is passed in as $1
    #
    PostErrorDialog()
    {
          DtDisplayErrorDialog “Find Error” “$1” \
          DIALOG_PRIMARY_APPLICATION_MODAL
    }

    #
    # This is both the `Ok' and the `Apply' callback; in the case of the
    # `Ok' callback, it unposts the main application window, and then
    # exits, if the dialog contains valid information. For both `Ok' and
    # `Apply', the set of search directories is first validated; if any
    # of the paths are not valid, then an error dialog is posted.
    # Otherwise, the `find' process is started in a terminal window.
    #
    OkCallback()
    {
       RetrieveAndSaveCurrentValues
       if [ “$SD_VAL” = ““ ]; then
          PostErrorDialog “You must specify a directory to search”
       else
          for i in $SD_VAL; do
             if  [ ! -d $i ]; then
                MSG=”The following search directory does not exist:

                $i”
                PostErrorDialog “$MSG”
                return 1
             fi
          done

          if [ $CB_WIDGET = $OK ]; then
             XtPopdown $TOPLEVEL
          fi

          CMD=”/bin/find $SD_VAL”
          if [ ! “$FNP_VAL” = ““ ]; then
             CMD=$CMD” -name $FNP_VAL”
          fi

          if ! $(XmToggleButtonGetState $T1); then
             CMD=$CMD” -xdev”
          fi

          if $(XmToggleButtonGetState $T3); then
             CMD=$CMD” -hidden”
          fi

          if $(XmToggleButtonGetState $T4); then
             CMD=$CMD” -follow”
          fi

          if $(XmToggleButtonGetState $T5); then
             CMD=$CMD” -depth”
          fi

          case $FSTYPE_VAL in
             $NFS)  CMD=$CMD” -fsonly nfs” ;;
             $CDFS) CMD=$CMD” -fsonly cdfs” ;;
             $HFS)  CMD=$CMD” -fsonly hfs” ;;
             *) ;;
          esac

          case $FILETYPE_VAL in
             $REGULAR)   CMD=$CMD” -type f” ;;
             $DIRECTORY) CMD=$CMD” -type d” ;;
             $BLOCK)     CMD=$CMD” -type b” ;;
             $CHAR)      CMD=$CMD” -type c” ;;
             $FIFO)      CMD=$CMD” -type p” ;;
             $SYMLINK)   CMD=$CMD” -type l” ;;
             $SOCKET)    CMD=$CMD” -type s” ;;
             $NET)       CMD=$CMD” -type n” ;;
             $MOUNT)     CMD=$CMD” -type M” ;;
             $HIDDEN)    CMD=$CMD” -type H” ;;
             *) ;;
          esac

          if $(XmToggleButtonGetState $T2); then
             CMD=$CMD” -print”
          fi

          /usr/dt/bin/dtterm -title “Find A File” -e /usr/dt/bin/dtexec
                                                     -open -1 $CMD &

          if [ $CB_WIDGET = $OK ]; then
             exit 0
          fi
       fi
    }

    #
    # This function attempts to load in the previous dialog values.
    # Each line read from the file is then interpreted as a ksh command.
    #
    LoadStickyValues()
    {
       if [ -r “./Find.sticky” ]; then
          exec 6< “./Find.sticky”
          XtAddInput FID 6 “EvalCmd”
       fi
    }

    #
    # This function is invoked for each line in the `sticky' values file.
    # It will evalutate each line as a dtksh command.
    #
    EvalCmd()
    {
       if [ ${#INPUT_LINE} -gt 0 ]; then
          eval “$INPUT_LINE”
       fi

       if [ “$INPUT_EOF” = `true' ]; then
          XtRemoveInput $INPUT_ID
          eval exec $INPUT_SOURCE'<&-'
       fi
    }

    #
    # This function retrieves the current values, and then saves them
    # off into a file, so that they can be restored the next time the
    # dialog is displayed.  It is called anytime the user selects either
    # the “Ok” or “Apply” buttons.
    #
    RetrieveAndSaveCurrentValues()
    {
       XmTextGetString SD_VAL $SD
       XmTextGetString FNP_VAL $FNP
       XtGetValues $FSTYPE menuHistory:FSTYPE_VAL
       XtGetValues $FILETYPE menuHistory:FILETYPE_VAL

       exec 3> “./Find.sticky”
       if [ ! “$SD_VAL” = ““ ]; then
          print -u 3 “XmTextSetString \$SD \”$SD_VAL\””
          print -u 3 “XmTextFieldSetInsertionPosition \$SD ${#SD_VAL}”
       fi
       if [ ! “$FNP_VAL” = ““ ]; then
          print -u 3 “XmTextSetString \$FNP \”$FNP_VAL\””
          print -u 3 “XmTextFieldSetInsertionPosition \$FNP ${#FNP_VAL}”
       fi

       case $FSTYPE_VAL in
          $NFS)  FST=”\$NFS” ;;
          $CDFS) FST=”\$CDFS” ;;
          $HFS)  FST=”\$HFS” ;;
          *)     FST=”\$NODIR” ;;
       esac
       print -u 3 “XtSetValues \$FSTYPE menuHistory:$FST”

       case $FILETYPE_VAL in
          $REGULAR)   FT=”\$REGULAR” ;;
          $DIRECTORY) FT=”\$DIRECTORY” ;;
          $BLOCK)     FT=”\$BLOCK” ;;
          $CHAR)      FT=”\$CHAR” ;;
          $FIFO)      FT=”\$FIFO” ;;
          $SYMLINK)   FT=”\$SYMLINK” ;;
          $SOCKET)    FT=”\$SOCKET” ;;
          $NET)       FT=”\$NET” ;;
          $MOUNT)     FT=”\$MOUNT” ;;
          $HIDDEN)    FT=”\$HIDDEN” ;;
          *)          FT=”\$NOTYPE” ;;
       esac
       print -u 3 “XtSetValues \$FILETYPE menuHistory:$FT”

       if $(XmToggleButtonGetState $T1); then
          print -u 3 “XmToggleButtonSetState \$T1 true false”
       fi

       if $(XmToggleButtonGetState $T2); then
          print -u 3 “XmToggleButtonSetState \$T2 true false”
       fi

       if $(XmToggleButtonGetState $T3); then
          print -u 3 “XmToggleButtonSetState \$T3 true false”
       fi

       if $(XmToggleButtonGetState $T4); then
          print -u 3 “XmToggleButtonSetState \$T4 true false”
       fi

       if $(XmToggleButtonGetState $T5); then
          print -u 3 “XmToggleButtonSetState \$T5 true false”
       fi

       exec 3<&-
    }

    ################ Create the Main UI ####################

    set -f
    XtInitialize TOPLEVEL find Dtksh $0 “${@:-}”
    XtSetValues $TOPLEVEL title:”Find Files”

    XtCreateManagedWidget FORM form XmForm $TOPLEVEL

    XtCreateManagedWidget SDLABEL sdlabel XmLabel $FORM \
       labelString:”Search Directory:” \
       $(DtkshAnchorTop 12) \
       $(DtkshAnchorLeft 10)

    XtCreateManagedWidget SD sd XmText $FORM \
       columns:30 \
       value:”.” \
       $(DtkshAnchorTop 6) \
       $(DtkshRightOf $SDLABEL 10) \
       $(DtkshAnchorRight 10) \
       navigationType:EXCLUSIVE_TAB_GROUP
    XmTextFieldSetInsertionPosition $SD 1

    XtCreateManagedWidget FNPLABEL fnpabel XmLabel $FORM \
       labelString:”Filename Pattern:” \
       $(DtkshUnder $SDLABEL 24) \
       $(DtkshAnchorLeft 10)

    XtCreateManagedWidget FNP fnp XmText $FORM \
       columns:30 \
       $(DtkshUnder $SD 8) \
       $(DtkshRightOf $FNPLABEL 10) \
       $(DtkshAnchorRight 10) \
       navigationType:EXCLUSIVE_TAB_GROUP

    XtCreateManagedWidget SEP sep XmSeparator $FORM \
       separatorType:SINGLE_DASHED_LINE \
       $(DtkshUnder $FNP 10) \
       $(DtkshSpanWidth)

    XtCreateManagedWidget RC rc XmRowColumn $FORM \
            orientation:HORIZONTAL \
            numColumns:3 \
            packing:PACK_COLUMN \
       $(DtkshUnder $SEP 10) \
       $(DtkshSpanWidth 10 10) \
       navigationType:EXCLUSIVE_TAB_GROUP

    DtkshAddButtons -w $RC XmToggleButtonGadget \
       T1 “Cross Mount Points”           ““\
       T2 “Print Matching Filenames”     ““\
       T3 “Search Hidden Subdirectories” ““\
       T4 “Follow Symbolic Links”        ““\
       T5 “Descend Subdirectories First” ““

    XtCreateManagedWidget SEP2 sep XmSeparator $FORM \
       separatorType:SINGLE_DASHED_LINE \
       $(DtkshUnder $RC 10) \
       $(DtkshSpanWidth)

    XmCreatePulldownMenu PANE $FORM pane
    DtkshAddButtons -w $PANE XmPushButtonGadget \
       NODIR “no restrictions” ““\
       NFS   “nfs”             ““\
       CDFS  “cdfs”            ““\
       HFS   “hfs”             ““

    XmCreateOptionMenu FSTYPE $FORM fstype \
            labelString:”Restrict Search To File System Type:” \
            menuHistory:$NODIR \
            subMenuId:$PANE \
       $(DtkshUnder $SEP2 20) \
       $(DtkshSpanWidth 10 10) \
       navigationType:EXCLUSIVE_TAB_GROUP
    XtManageChild $FSTYPE

    XmCreatePulldownMenu PANE2 $FORM pane2
    DtkshAddButtons -w $PANE2 XmPushButtonGadget \
       NOTYPE    “no restrictions”   ““\
       REGULAR   “regular”           ““\
       DIRECTORY “directory”         ““\
       BLOCK     “block special”     ““\
       CHAR      “character special” ““\
       FIFO      “fifo”              ““\
       SYMLINK   “symbolic link”     ““\
       SOCKET    “socket”            ““\
       NET       “network special”   ““\
       MOUNT     “mount point”       ““\
       HIDDEN    “hidden directory”  ““

    XmCreateOptionMenu FILETYPE $FORM filetype \
            labelString:”Match Only Files Of Type:” \
            menuHistory:$NOTYPE \
            subMenuId:$PANE2 \
       $(DtkshUnder $FSTYPE 10) \
       $(DtkshSpanWidth 10 10) \
       navigationType:EXCLUSIVE_TAB_GROUP
    XtManageChild $FILETYPE
    XtSetValues $FILETYPE spacing:90

    XtCreateManagedWidget SEP3 sep3 XmSeparator $FORM \
       $(DtkshUnder $FILETYPE 10) \
       $(DtkshSpanWidth)

    XtCreateManagedWidget OK ok XmPushButton $FORM \
            labelString:”Ok” \
       $(DtkshUnder $SEP3 10) \
       $(DtkshFloatLeft 4) \
       $(DtkshFloatRight 24) \
       $(DtkshAnchorBottom 10)
    XtAddCallback $OK activateCallback “OkCallback”

    XtCreateManagedWidget APPLY apply XmPushButton $FORM \
            labelString:”Apply” \
       $(DtkshUnder $SEP3 10) \
       $(DtkshFloatLeft 28) \
       $(DtkshFloatRight 48) \
       $(DtkshAnchorBottom 10)
    XtAddCallback $APPLY activateCallback “OkCallback”

    XtCreateManagedWidget CLOSE close XmPushButton $FORM \
            labelString:”Close” \
       $(DtkshUnder $SEP3 10) \
       $(DtkshFloatLeft 52) \
       $(DtkshFloatRight 72) \
       $(DtkshAnchorBottom 10)
    XtAddCallback $CLOSE activateCallback “exit 1”

    XtCreateManagedWidget HELP help XmPushButton $FORM \
            labelString:”Help” \
       $(DtkshUnder $SEP3 10) \
       $(DtFloatLeft 76) \
       $(DtkshFloatRight 96) \
       $(DtkshAnchorBottom 10)
    XtAddCallback $HELP activateCallback \
           “DtkshDisplayQuickHelpDialog `Using The Find Command'
    HELP_TYPE_FILE \
       `./Find.help' “

    XtSetValues $FORM \
       initialFocus:$SD \
       defaultButton:$OK \
       cancelButton:$CLOSE \
       navigationType:EXCLUSIVE_TAB_GROUP

    DtkshSetReturnKeyControls $SD $FNP $FORM $OK
    LoadStickyValues

    XtRealizeWidget $TOPLEVEL
    XtMainLoop

## Find.sticky

The following script,
`Find.sticky` is executed by `script_find`. `Find.sticky` remembers the file and directory names used in the most recent execution of `script_find`.

    XmTextSetString $SD “/users/dlm”
    XmTextFieldSetInsertionPosition $SD 10
    XmTextSetString $FNP “elmbug”
    XmTextFieldSetInsertionPosition $FNP 6
    XtSetValues $FSTYPE menuHistory:$NODIR
    XtSetValues $FILETYPE menuHistory:$DIRECTORY
    XmToggleButtonSetState $T1 true false
    XmToggleButtonSetState $T2 true false

## Find.help

`Find.help` is a text file that is displayed on screen when the user clicks the Help button in the main `script_find` window.

    This dialog presents a graphical interface to the
    UNIX `find' command. The only required field is
    the name of the directory to be searched;
    all other fields are optional. Once the fields have
    been set to the desired values, you can use the
    `Ok' or `Apply' button to initiate the find operation.
    The results of the find operation are displayed
    in a dtterm terminal window.