[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Nifty hack



I don't know if anyone will be very interested in this, but here is
another little hack that I dreamed up this weekend in an effort to
create a single .esrc for every architecture that I commonly use.
It isn't exactly blazingly fast (it adds about 1 second onto my login
time), but it is pretty useful.

The following function, build-fn, is used to dynamically build a 
function, or alias, based off of a selection of similarly functional
programs.  The best way to describe this is using a couple of 
examples:

   build-fn play   player splayer play \
                   default {for (file = $*) {cat $file > /dev/audio}}

This will cause a new function 'play' to be created (actually, $fn-play);
say player exists in your $path (in /usr/audio/bin, like it does on my 
system), then:

    ; echo $fn-play
    @ * {/usr/audio/bin/player $*}

If player doesn't exist in your path, but splayer does exist in
/usr/local/bin, then:

    ; echo $fn-play
    @ * {/usr/local/bin/splayer $*}

If there are no matches, then you get the following:

    ; echo $fn-play
    @ * {for(file=$*){%create 1 <={%one /dev/audio} {cat $file}}}

build-fn also allows you to override the default function created for
programs that it manages to find.  For example:

    build-fn ll   ll { @prog@ -F $* } \
                  ls { @prog@ -lF $* }

So, if you actually have a program called ll on your system (I do,
in /bin), then:

    ; echo $fn-ll
    @ * {/bin/ll -F $*}

If ll doesn't exist in your path, then:

    ; echo $fn-ll
    @ * {/bin/ls -lF $*}

Note, that any instances of @prog@ in your supplied code fragment
are replaced with the full path name of the program that matched within
your list.

Caveats:

   o  You can't search for a program called 'default'.

   o  It doesn't make sense supply @prog@ within a default
      selection.

   o  You can't stop @prog@ from being expanded within a code
      fragment.

Have fun!
-scott

--
   _/    _/   _/_/_/  _/_/_/_/   _/_/_/  Scott C. Gray (gray@nas.nasa.gov)
  _/_/  _/  _/    _/ _/_/      _/    _/  NASA Ames Research M/S 258-6
 _/  _/_/  _/_/_/_/     _/_/  _/_/_/_/   For PGP public key, send me mail
_/    _/  _/    _/ _/_/_/_/  _/    _/    with subject line of "PGP request".

----------------------------[ CUT HERE ]-----------------------------------

fn choose-bin bin-list {
   for ( bin = $bin-list ) {
      for ( dir = $path ) {
         if { access -rx $dir/$bin } {
            return $dir/$bin
         }
      }
   }
   return
}

# build-fn function-name  { prog-name | default } [script] ...
#
# This function is used to dynamically build a function, or alias,
# using the first occurance of prog-name that actually exists. 

fn build-fn function-name args {

   let ( 
      default-fn     =        # script specified by the default key word
      prog-name      =        # current program we are searching for
      prog-name-full =        # fully specified program path
      prog-script    =        # script to be used if prog-name is found
      dir            =        # variable used in a loop
      success        = false
   ) {

      while { ! ~ $#args 0 } {

         prog-name   = $args(1)

         if { ~ $args(2) '{'*'}' } {
            prog-script = $args(2)
            args = $args(3 ...)
         } {
            prog-script = 
            args = $args(2 ...)
         }

         if { ~ $prog-name default } {

            if { ~ $#prog-script 0 } {
               throw error build-fn 'build-fn: No code with default'
            }
            default-fn = $prog-script

         } {

            prog-name-full = <={ choose-bin $prog-name }

            if { ! ~ $#prog-name-full 0 } {
               if { ~ $#prog-script 0 } {
                  prog-script = '@ * {'^$prog-name-full^' $*}'
               } {
                  prog-script = '@ *' `{echo $prog-script | \
                     sed -e 's,@prog@,'^$prog-name-full^',g'}
               }
               fn-$function-name = $^prog-script
               success     = true
               break
            }
         }
      }

      if { ! ~ $success true } {
         if { ! ~ $#default-fn 0 } {
            default-fn   = '@ * ' ^ $^default-fn
            fn-$function-name  = $^default-fn
         } {
            throw error build-fn 'build-fn: No match for '^$function-name
         }
      }
   }
   return 0
}

--
   _/    _/   _/_/_/  _/_/_/_/   _/_/_/  Scott C. Gray (gray@nas.nasa.gov)
  _/_/  _/  _/    _/ _/_/      _/    _/  NASA Ames Research M/S 258-6
 _/  _/_/  _/_/_/_/     _/_/  _/_/_/_/   For PGP public key, send me mail
_/    _/  _/    _/ _/_/_/_/  _/    _/    with subject line of "PGP request".