home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / pushd21.zip / popd.cmd < prev    next >
OS/2 REXX Batch file  |  1993-07-28  |  2KB  |  84 lines

  1. /* POPD                                                                      */
  2. /* written:  Ken Neighbors  sometime before 13 May 93                        */
  3. /* revised:  Ken Neighbors  30 May 93  +n, comments, bug fixes, beautify     */
  4. /* revised:  Ken Neighbors  28 jul 93  added support for spaces in dir       */
  5.  
  6. parse arg Argument rest
  7.  
  8. if ( rest <> '' ) then do
  9.     say 'popd:  Too many arguments.'
  10.     exit 1
  11. end
  12.  
  13. DirStack = value('PUSHD',,'OS2ENVIRONMENT')
  14.  
  15. /*
  16.  * first case: argument is "+n"--delete nth entry from stack
  17.  */
  18. if ( Argument <> '' ) then do
  19.     if ( substr(Argument,1,1) == '+' ) then do
  20.     n = substr(Argument,2)
  21.  
  22.     /* check that n is a whole number, greater than zero, less than dirs */
  23.     if ( \datatype(n,'Whole number') | (n < 1) ) then do
  24.         say 'popd: Invalid cyclic parameter'
  25.         exit 3
  26.     end
  27.     NumDirs = words(DirStack)   /* plus one:  current dir */
  28.     if (n > NumDirs) then do
  29.         say 'popd: Directory stack not that deep'
  30.         exit 3
  31.     end
  32.  
  33.     DirStack = delword(DirStack,n,1)
  34.     call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  35.     CurrentDir = _beaut(directory())
  36.     say CurrentDir decode(DirStack)
  37.     end
  38.     else do
  39.     say 'popd:  Too many arguments.'
  40.     exit 1
  41.     end
  42. end
  43. /*
  44.  * second case: no argument--pop top directory
  45.  */
  46. else do
  47.     parse var DirStack CodedNewDir DirStack
  48.     NewDir = decode(CodedNewDir)
  49.     if ( NewDir == '' ) then do
  50.     /* The stack is empty */
  51.     say 'There is no directory to pop.'
  52.     exit 1
  53.     end
  54.     else do
  55.     /* Switch current directory with the one on top of stack, NewDir */
  56.     NewDirVerify = _beaut(directory(NewDir))
  57.     if ( NewDirVerify <> '' ) then do
  58.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  59.         say NewDirVerify decode(DirStack)
  60.     end
  61.     else do
  62.         /* could not change to NewDir, so just delete it from stack */
  63.         say NewDir': No such directory.'
  64.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  65.         CurrentDir = _beaut(directory())
  66.         say CurrentDir decode(DirStack)
  67.         exit 2
  68.     end
  69.     end
  70. end
  71. exit 0
  72.  
  73. encode:
  74.     parse arg InputString
  75.  
  76.     OutputString = translate(InputString,'|',' ')
  77.     return OutputString
  78.  
  79. decode:
  80.     parse arg InputString
  81.  
  82.     OutputString = translate(InputString,' ','|')
  83.     return OutputString
  84.