home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1874 < prev    next >
Internet Message Format  |  1990-12-28  |  4KB

  1. From: barto@megatek.UUCP (David Barto)
  2. Newsgroups: comp.unix.shell,alt.sources
  3. Subject: Re: Pushd, popd, dirs for stock SysV.3 Bourne shell
  4. Message-ID: <750@tsunami.megatek.uucp>
  5. Date: 27 Sep 90 16:19:05 GMT
  6.  
  7. In article <PCG.90Sep26164749@odin.cs.aber.ac.uk> pcg@cs.aber.ac.uk (Piercarlo Grandi) writes:
  8. >
  9. >You will find appended a small shell script that defines under a stock
  10. >System V Bourne shell the pushd, popd, dirs commands of csh(1). They are
  11. >trivially adapted to the Korn shell. I have taken care to make them as
  12. >robust as possible.
  13. >
  14.  
  15. pushd, popd, and dirs --- written by Chris Bertin
  16. Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris
  17. as modified by Patrick Elam of GTRI
  18. modified by david barto to allow 'pushd +n'
  19.  
  20. cd is a function, which uses the 'builtin' keyword, available for
  21. those who have the BRL mods to the shell.  (I think this can be changed
  22. to 'eval'.)  It will look for PCDPATH and do chdirs down that path if
  23. the directory can be found on it.
  24.  
  25. PCDPATH=". $HOME /burt/prj/common /burt/prj/sparc /archive2"
  26. export PCDPATH
  27. -----
  28.  
  29. cd () {
  30.     PREVDIR="$CWD" 
  31.     result=1
  32.     if [ $# -lt 1 ] ; then
  33.         builtin cd
  34.         result=0
  35.     elif [ -d $1 ] ; then
  36.         builtin cd "$1" 
  37.         result=0
  38.     else
  39.         for i in $PCDPATH
  40.         do
  41.         nxtdir=$i/$1
  42.         # echo "try $nxtdir"
  43.         if [ $result != 0 -a -d $nxtdir ] ; then
  44.             builtin cd "$nxtdir"
  45.             result=0
  46.         fi
  47.         done
  48.         unset nxtdir
  49.     fi
  50.     if [ $result != 0 ] ; then
  51.         echo "$1: directory not found"
  52.     else
  53.         CWD=`pwd` export CWD 
  54.         set +u 
  55.         if [ "$CWD" = "$HOME" ] 
  56.         then
  57.         PFX="$HOST" 
  58.         PS1=:~
  59.         else
  60.         PS1=`echo "$CWD" | sed -e "s!^$HOME!~!"` PFX="$HOST": 
  61.         fi
  62.         umask 022
  63.         echo "$CWD" | egrep -s "/burt/prj/common|/x"
  64.         [ $? = 0 ] && umask 002
  65.         if [ "$TERM" = "wy75" ]
  66.         then
  67.         wy75-title $PS1
  68.         PS1="$PFX %e_ "
  69.         elif [ "$TWM_RUNNING" = 1 ]
  70.         then
  71.         if [ "$TTY" != "" ]
  72.         then
  73.             twm-title "$TTY:$PFX$PS1"
  74.         else
  75.             twm-title "XX:$PFX$PS1"
  76.         fi
  77.         PS1="%e_ "
  78.         else
  79.         PS1="$PFX$PS1 %t%n%e_ " 
  80.         fi
  81.         unset PFX 
  82.         set -u 
  83.     fi
  84.     return $result
  85. }
  86.  
  87. # pushd, popd, and dirs --- written by Chris Bertin
  88. # Pixel Computer Inc. ...!wjh12!pixel!pixutl!chris
  89. # as modified by Patrick Elam of GTRI
  90. # modified by david barto to allow 'pushd +n'
  91.  
  92. pushd () {
  93.     SAVE=`pwd`
  94.     _DSTACK="$SAVE $_DSTACK"
  95.     if [ "$1" = "" ] 
  96.     then
  97.         if [ "$_DSTACK" = "$SAVE " ]
  98.         then
  99.             echo "pushd: directory stack empty."
  100.             _DSTACK=""
  101.             return 1
  102.         fi
  103.         set $_DSTACK
  104.         cd $2
  105.         set $_DSTACK
  106.         shift 2
  107.         _DSTACK="$SAVE $*"
  108.     else
  109.         case "$1" in
  110.         +*)
  111.             count=`echo $1 | sed 's/+//'`
  112.             set $_DSTACK
  113.             if [ $count -ge $# ] ; then
  114.                 echo "pushd: directory stack not that deep"
  115.                 shift
  116.                 _DSTACK=$*
  117.                 return 1
  118.             fi
  119.             TMP=
  120.             while [ $count -gt 0 ]
  121.             do
  122.                 TMP="$TMP $1"
  123.                 shift
  124.                 count=`expr $count - 1`
  125.             done
  126.             TD=$1
  127.             shift
  128.             _DSTACK="$TMP $*"
  129.             unset TMP
  130.             if [ -d $TD ]
  131.             then
  132.                 cd $TD >&-
  133.                 unset TD
  134.             else
  135.                 popd > /dev/null
  136.                 return 1
  137.             fi
  138.             ;;
  139.         *)
  140.             cd $1
  141.             if [ $? -eq 1 ] ; then
  142.                 popd > /dev/null
  143.                 return 1
  144.             fi
  145.             ;;
  146.         esac
  147.     fi
  148.     dirs
  149.     return 0
  150. }
  151.  
  152. popd () {
  153.     if [ "$_DSTACK" = "" ]; then
  154.         echo "popd: Directory stack empty"
  155.         return 1
  156.     fi
  157.     case "$1" in
  158.     +*)
  159.         count=`echo $1 | sed 's/+//'`
  160.         case "$count" in
  161.         0)
  162.             echo "popd: Must have positive count"
  163.             return 1
  164.             ;;
  165.         1)
  166.             popd
  167.             return 0
  168.             ;;
  169.         *)
  170.             _TMPSTK="`pwd` $_DSTACK"
  171.             set $_TMPSTK
  172.             if [ $count -gt $# ]; then
  173.                 echo "pushd: directory stack not that deep"
  174.                 return 1
  175.             fi
  176.             TMP=
  177.             while [ $count -ge 1 ]
  178.             do
  179.                 TMP="$TMP $1"
  180.                 shift
  181.                 count=`expr $count - 1`
  182.             done
  183.             shift        # trash dir requested
  184.             _DSTACK="$* $TMP"
  185.             set $_DSTACK
  186.             shift        # trash 'pwd' from the top of the stack
  187.             _DSTACK="$*"
  188.             ;;
  189.         esac
  190.         ;;
  191.     *)
  192.         set $_DSTACK
  193.         cd $1
  194.         set $_DSTACK
  195.         shift
  196.         _DSTACK=$*
  197.         ;;
  198.     esac
  199.     dirs
  200.     return 0
  201. }
  202.  
  203. dirs () {
  204.     echo "`pwd` $_DSTACK"
  205.     return 0
  206. }
  207.  
  208. xchng () {    # exchanged top two entries on the stack
  209.     if [ "$_DSTACK" = "" ]
  210.     then
  211.         echo exchange directory stack empty
  212.         return 1
  213.     else
  214.         pushd
  215.         return 0
  216.     fi
  217. }
  218. -- 
  219. David Barto    uunet!megatek!barto    barto@network.ucsd.edu
  220.