home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / os2 / pushd20.zip / PUSHD.CMD < prev    next >
OS/2 REXX Batch file  |  1993-05-30  |  4KB  |  113 lines

  1. /* PUSHD                                                                     */
  2. /* written:  Ken Neighbors  sometime before 13 May 93                        */
  3. /* revised:  Greg Roelofs   19 May 93  added +n option                       */
  4. /* revised:  Ken Neighbors  30 May 93  comments, bug fixes, beautify         */
  5.  
  6. parse arg Argument rest
  7.  
  8. if ( rest <> '' ) then do
  9.     say 'pushd: Too many arguments.'
  10.     exit 1
  11. end
  12.  
  13. DirStack = value('PUSHD',,'OS2ENVIRONMENT')
  14.  
  15. if ( Argument == '' ) then do
  16.     /*
  17.      * first case:  no argument--swap top two directories
  18.      */
  19.     if ( DirStack == '' ) then do
  20.         say 'pushd: No other directory.'
  21.         exit 1
  22.     end
  23.     else do
  24.         parse var DirStack NewDir OtherDirs
  25.         CurrentDir = _beaut(directory())
  26.         NewDirVerify = _beaut(directory(NewDir))
  27.  
  28.         if ( NewDirVerify == '' ) then do
  29.             say insert(NewDir,': No such directory.')
  30.             /* get rid of bad directory (unlike csh pushd) */
  31.             DirStack = OtherDirs
  32.             call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  33.             say CurrentDir DirStack
  34.             exit 2
  35.         end
  36.         else do
  37.         /* place (old) current directory at beginning of stack */
  38.         DirStack = insert(OtherDirs,CurrentDir,length(CurrentDir)+1)
  39.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  40.         say NewDirVerify DirStack
  41.         end
  42.  
  43.     end
  44. end
  45. else do
  46.  
  47.     /*
  48.      * second case:  argument is "+n"--do cyclic rotation by n positions
  49.      */
  50.  
  51.     if ( substr(Argument,1,1) == '+' ) then do
  52.         n = substr(Argument,2)
  53.  
  54.         /* check that n is a whole number, greater than zero, less than dirs */
  55.         if ( \datatype(n,'Whole number') | (n < 1) ) then do
  56.             say 'pushd: Invalid cyclic parameter'
  57.             exit 3
  58.         end
  59.         NumDirs = words(DirStack)   /* plus one:  current dir */
  60.         if (n > NumDirs) then do
  61.             say 'pushd: Directory stack not that deep'
  62.             exit 3
  63.         end
  64.  
  65.         CurrentDir = _beaut(directory())
  66.  
  67.         /* use subword() to parse according to n */
  68.         NewDir = subword(DirStack,n,1)      /* CurrentDir not in DirStack yet */
  69.         NewDirVerify = _beaut(directory(NewDir))
  70.         if ( NewDirVerify == '' ) then do
  71.             say insert(NewDir,': No such directory.')
  72.             /* get rid of bad directory (unlike csh pushd) */
  73.             DirStack = delword(DirStack,n,1)
  74.             call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  75.             say CurrentDir DirStack
  76.             exit 2
  77.         end
  78.         else do
  79.         /* directory exists and we're now in it:  shift DirStack around */
  80.         /* CurrentDir is no longer current--it's the old directory */
  81.         /* Warning:  this is confusing */
  82.         DirStack = insert(DirStack,CurrentDir,length(CurrentDir)+1)
  83.         firsthalf = subword(DirStack,1,n)
  84.         lasthalf = subword(DirStack,n+1)
  85.         DirStack = insert(firsthalf,lasthalf,length(lasthalf)+1)
  86.         /* get rid of now current dir, "NewDir" */
  87.         DirStack = delword(DirStack,1,1)
  88.  
  89.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  90.         say NewDirVerify DirStack
  91.         end
  92.     end
  93.  
  94.     /*
  95.      * third case:  argument is new directory--switch to it and add to stack
  96.      */
  97.  
  98.     else do
  99.     NewDir = translate(Argument,'\','/')
  100.         CurrentDir = _beaut(directory())
  101.  
  102.         NewDirVerify = _beaut(directory(NewDir))
  103.         if ( NewDirVerify == '' ) then do
  104.             say insert(NewDir,': No such directory.')
  105.             exit 2
  106.         end
  107.         DirStack = insert(DirStack,CurrentDir,length(CurrentDir)+1)
  108.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  109.         say NewDirVerify DirStack
  110.     end
  111. end
  112. exit 0
  113.