Hamilton LaboratoriesHamilton C shell 2012User guideSamples

which.csh

Oregon Coast

which.csh
Previous | Next

#  Determine which alias, procedure or executable file will run.

#  Copyright (c) 1998-2012 by Hamilton Laboratories.  All rights reserved.

proc possiblename( s )
   local c, i
   @ c = substr( s, 0, 1 )
   if ( c !~ "[a-zA-Z_@]" ) return 0
   for i = 1 to strlen( s ) do
      @ c = substr( s, i, 1 )
      if ( c !~ "[a-zA-Z_@0-9]" ) return 0
   end
   return 1
end

proc which( cmd )

   #  Figure out what will be run if cmd is typed.
   #  The search order is:
   #     1. Aliases
   #     2. Procedures
   #     3. Executable files

   local result
   if ( $#cmd ) then
      if ( possiblename( cmd ) ) then

         #  Turn off errors if looking for aliases and procs
         #  that don't exist.
         local savenonovar
         @ savenonovar = nonovar
         @ nonovar = 2 # discard errors

         @ result = ``eval alias $cmd``
         if ( $#result ) then
            set result = "alias $result" 
         else
            @ result = ``eval proc $cmd``
            if ( $#result ) then
               set result = "proc  $result"
            else
               @ result = ``whereis $cmd``
               if ( $#result > 1 ) @ result = result[ 0 ]
            end

         #  Restore the error handling.
         @ nonovar = savenonovar
         end
      else
         @ result = ``whereis $cmd``
         if ( $#result > 1 ) @ result = result[ 0 ]
      end
   end
   return result
end

which $argv

Previous | Next