Package com.zaranux.os.client.programs

Source Code of com.zaranux.os.client.programs.shell

package com.zaranux.os.client.programs;


import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.java.io.FileNotFoundException;
import com.zaranux.client.java.io.FileOutputStream;
import com.zaranux.client.java.io.InputStream;
import com.zaranux.client.java.io.InputStreamReader;
import com.zaranux.client.java.io.OutputStream;

import com.zaranux.os.client.core.Program;
import com.zaranux.os.client.zterminal.EnvironmentVariables;
import com.allen_sauer.gwt.log.client.Log;


public class shell extends Program{

  private InputStreamReader stdinr;
  private InputStream stdin;
  private OutputStream stdout;
 
    private final EnvironmentVariables ev = new EnvironmentVariables();

    public shell()
    {
    }
   
    public void setIO(InputStream stdin,OutputStream stdout)
    {
    this.stdin = stdin;
    this.stdinr = new InputStreamReader(stdin);
    this.stdout = stdout;
    }
   
    /*
  public shell(InputStream stdin,OutputStream stdout)
  {
    this.stdin = stdin;
    this.stdinr = new InputStreamReader(stdin);
    this.stdout = stdout;
  }
  */
  private StringBuffer pendingCommands;
  public void main(String[] args)
  {
    this.setTitle("Viewer");
  }
 
  public void execute(final AsyncCallback<Boolean> callback)
  {
    pendingCommands = new StringBuffer();
    readPendingCommands(new AsyncCallback<Boolean>(){
      public void onSuccess(Boolean b)
      {
        String commands = pendingCommands.toString().trim(); // remove extra new lines
       
        Log.debug("Commands to be executed : >" + pendingCommands + "<");
       
        //skip empty commands
        if( pendingCommands.length() == 1 &&  pendingCommands.charAt(0) == 10) {
          callback.onSuccess(true); return;
        }
       
        String command = commands;
        String[] cmd_file = commands.split(">>",2);
        OutputStream os = stdout;

        if(cmd_file.length == 2)
        {
          os = new FileOutputStream(cmd_file[1].trim(),true);
          command = cmd_file[0];
        }
       
        executeCommand(command,stdin,os,callback);
      }
      public void onFailure(Throwable t)
      {
        callback.onFailure(t);
      }
    });
  }
 
  private OutputStream getOutputStream(String command)
  {
    OutputStream os = stdout;
    String filename = null;
    boolean append = false;
    int i;
    if ((i = command.lastIndexOf(">>"))  > -1)
    {
       filename = command.substring(i+1);
       append = true;
    }
    if((i = command.lastIndexOf('>') ) > -1)
    {
       filename = command.substring(i+1);
    }
   
    if(filename != null)
    {
      filename = filename.trim();
      os = new FileOutputStream(filename,append);
    }
   
    return os;
  }
  private void executeCommand(String command,InputStream is, OutputStream os, final AsyncCallback<Boolean> callback)
  {
    String s[] = command.split("[\\s]+",2);
   
    //if(s.length < 1) {callback.onFailure(new Exception("wrong command")); return;}
   
    String cmd = s[0];
    String args = (s.length == 2) ?  s[1] : "";   
    com.zaranux.os.client.commands.Command c = null;

    if(cmd.equals("ls"))
    {
      c = new com.zaranux.os.client.commands.ls();
    }else if(cmd.equals("cd"))
    {
      c = new com.zaranux.os.client.commands.cd();
    }else if( cmd.equals("echo"))
    {
      c = new com.zaranux.os.client.commands.echo();
    }else if(cmd.equals("cat"))
    {
      c = new com.zaranux.os.client.commands.cat();
    }else if(cmd.equals("login"))
    {
      c = new com.zaranux.os.client.commands.login();
    }else if(cmd.equals("adduser"))
    {
      c = new com.zaranux.os.client.commands.adduser();
    }else if(cmd.equals("ls") || cmd.equals("dir")) // just to keep windows users happy! ;-)
    {
      c = new com.zaranux.os.client.commands.ls();
    }else if(cmd.equals("explorer"))
    {
      c = new com.zaranux.os.client.commands.explorer();
    }else if(cmd.equals("mkdir"))//new directory
    {
      c = new com.zaranux.os.client.commands.mkdir();
    }/*else if(cmd.equals("touch"))//new file
    {
      c = new com.zaranux.client.programs.commands.touch();
    }*/
    else if(cmd.equals("rm"))//delete file
    {
      c = new com.zaranux.os.client.commands.rm();
    }
    else if(cmd.equals("rmdir"))//delete directory
    {
      c = new com.zaranux.os.client.commands.rmdir();
    }else if(cmd.equals("ps"))
    {
      c = new com.zaranux.os.client.commands.ps();
    }else if(cmd.equals("kill"))
    {
      c = new com.zaranux.os.client.commands.kill();
    }else if(cmd.equals("edit"))
    {
      c = new com.zaranux.os.client.commands.edit();
    }else if(cmd.equals("chmod"))
    {
      c = new com.zaranux.os.client.commands.chmod();
    }else if(cmd.equals("notify"))
    {
      c = new com.zaranux.os.client.commands.notify();
    }else
    {
      c = new com.zaranux.os.client.commands.run(command);
    }
    c.setStreams(is,os,null);
    c.setEnvironmentVariables(ev);
    c.setArgs(args);
    c.executed(this, callback);
  }

  private void readPendingCommands(final AsyncCallback<Boolean> callback)
  {
    final char[] c = new char[100];
    stdinr.read(c, new AsyncCallback<Integer>(){
      public void onSuccess(Integer n)
      {
        if(n == -1)
        {
          callback.onSuccess(true);
        }else
        {
          for(int i =0;i < n; i++)
          {
            Log.debug("chars of command : " + (int) c[i]);
            pendingCommands.append(c[i]);
          }
          readPendingCommands(callback);
        }
       
      }
      public void onFailure(Throwable t)
      {
        callback.onFailure(t);
      }
    });
  }
 

}

TOP

Related Classes of com.zaranux.os.client.programs.shell

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.