1   // joi/7/juno/CdCommand.java                         
2   //                                                            
3   //                                                            
4   // Copyright 2003, Bill Campbell and Ethan Bolker                         
5                                                               
6   import java.util.*;
7   
8   /**
9    * The Juno shell command to change directory.
10   * Usage:
11   * <pre>
12   *     cd [directory]
13   * </pre>
14   * for moving to the named directory.
15   *
16   * @version 7
17   */
18  
19  class CdCommand extends ShellCommand 
20  {
21      CdCommand()
22      {
23          super( "change current directory", "[ directory ]" );
24      }
25      
26      /**
27       * Move to the named directory
28       *
29       * @param args the remainder of the command line.
30       * @param sh   the current shell
31       *
32       * @exception JunoException for reporting errors
33       */
34  
35      public void doIt( StringTokenizer args, Shell sh )
36           throws JunoException 
37      {
38          String dirname = "";
39          Directory d = sh.getUser().getHome(); // default
40          if ( args.hasMoreTokens() ) {
41              dirname = args.nextToken();
42              if (dirname.equals("..")) {
43                  if (sh.getDot().isRoot()) {
44                      d = sh.getDot(); // no change
45                  }
46                  else {
47                      d = sh.getDot().getParent();
48                  }
49              }
50              else if (dirname.equals(".")) {
51                  d = sh.getDot(); // no change
52              }
53              else {
54                  d = (Directory)(sh.getDot().retrieveJFile(dirname));
55              }
56          }
57          sh.setDot( d );
58      }
59  }            
60