home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / pushd21.zip / pushd.cmd < prev    next >
OS/2 REXX Batch file  |  1993-07-28  |  4KB  |  135 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. /* revised:  Ken Neighbors  28 jul 93  added support for spaces in dir       */
  6.  
  7. parse arg '"'Argument'"' rest
  8.  
  9. if ( Argument == '' ) then do
  10.     parse arg Argument rest
  11. end
  12.  
  13. if ( rest <> '' ) then do
  14.     say 'pushd: Too many arguments.'
  15.     exit 1
  16. end
  17.  
  18. DirStack = value('PUSHD',,'OS2ENVIRONMENT')
  19.  
  20. if ( Argument == '' ) then do
  21.     /*
  22.      * first case:  no argument--swap top two directories
  23.      */
  24.     if ( DirStack == '' ) then do
  25.         say 'pushd: No other directory.'
  26.         exit 1
  27.     end
  28.     else do
  29.         parse var DirStack CodedNewDir OtherDirs
  30.         NewDir = decode(CodedNewDir)
  31.         CurrentDir = _beaut(directory())
  32.         NewDirVerify = _beaut(directory(NewDir))
  33.  
  34.         if ( NewDirVerify == '' ) then do
  35.             say NewDir': No such directory.'
  36.             /* get rid of bad directory (unlike csh pushd) */
  37.             DirStack = OtherDirs
  38.             call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  39.             say CurrentDir decode(DirStack)
  40.             exit 2
  41.         end
  42.         else do
  43.         /* place (old) current directory at beginning of stack */
  44.         CodedCurrentDir = encode(CurrentDir)
  45.         DirStack = insert(OtherDirs,CodedCurrentDir,length(CurrentDir)+1)
  46.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  47.         say NewDirVerify decode(DirStack)
  48.         end
  49.  
  50.     end
  51. end
  52. else do
  53.  
  54.     /*
  55.      * second case:  argument is "+n"--do cyclic rotation by n positions
  56.      */
  57.  
  58.     if ( substr(Argument,1,1) == '+' ) then do
  59.         n = substr(Argument,2)
  60.  
  61.         /* check that n is a whole number, greater than zero, less than dirs */
  62.         if ( \datatype(n,'Whole number') | (n < 1) ) then do
  63.             say 'pushd: Invalid cyclic parameter'
  64.             exit 3
  65.         end
  66.         NumDirs = words(DirStack)   /* plus one:  current dir */
  67.         if (n > NumDirs) then do
  68.             say 'pushd: Directory stack not that deep'
  69.             exit 3
  70.         end
  71.  
  72.         CurrentDir = _beaut(directory())
  73.  
  74.         /* use subword() to parse according to n */
  75.         CodedNewDir = subword(DirStack,n,1)      /* CurrentDir not in DirStack yet */
  76.         NewDir = decode(CodedNewDir)
  77.         NewDirVerify = _beaut(directory(NewDir))
  78.         if ( NewDirVerify == '' ) then do
  79.             say NewDir': No such directory.'
  80.             /* get rid of bad directory (unlike csh pushd) */
  81.             DirStack = delword(DirStack,n,1)
  82.             call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  83.             say CurrentDir decode(DirStack)
  84.             exit 2
  85.         end
  86.         else do
  87.         /* directory exists and we're now in it:  shift DirStack around */
  88.         /* CurrentDir is no longer current--it's the old directory */
  89.         /* Warning:  this is confusing */
  90.         CodedCurrentDir = encode(CurrentDir)
  91.         DirStack = insert(DirStack,CodedCurrentDir,length(CurrentDir)+1)
  92.         firsthalf = subword(DirStack,1,n)
  93.         lasthalf = subword(DirStack,n+1)
  94.         DirStack = insert(firsthalf,lasthalf,length(lasthalf)+1)
  95.         /* get rid of now current dir, "NewDir" */
  96.         DirStack = delword(DirStack,1,1)
  97.  
  98.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  99.             say NewDir decode(DirStack)
  100.         end
  101.     end
  102.  
  103.     /*
  104.      * third case:  argument is new directory--switch to it and add to stack
  105.      */
  106.  
  107.     else do
  108.     NewDir = translate(Argument,'\','/')
  109.         CurrentDir = _beaut(directory())
  110.  
  111.         NewDirVerify = _beaut(directory(NewDir))
  112.         if ( NewDirVerify == '' ) then do
  113.             say NewDir': No such directory.'
  114.             exit 2
  115.         end
  116.         CodedCurrentDir = encode(CurrentDir)
  117.         DirStack = insert(DirStack,CodedCurrentDir,length(CurrentDir)+1)
  118.         call value 'PUSHD',DirStack,'OS2ENVIRONMENT'
  119.         say NewDirVerify decode(DirStack)
  120.     end
  121. end
  122. exit 0
  123.  
  124. encode:
  125.     parse arg InputString
  126.  
  127.     OutputString = translate(InputString,'|',' ')
  128.     return OutputString
  129.  
  130. decode:
  131.     parse arg InputString
  132.  
  133.     OutputString = translate(InputString,' ','|')
  134.     return OutputString
  135.