package net.sf.yacas;


import java.io.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.awt.Desktop;

public class YacasConsole extends Thread
{
    static String readLine(InputStream aStream)
  {
    StringBuilder line = new StringBuilder();
    try
    {
      int c = aStream.read();
      while (c != '\n')
      {
      line.append((char)c);
      c = aStream.read();
      }
    }
    catch (Exception e)
    {
      System.out.println(e.toString());
    }
    return line.toString();
  }
  static boolean quitting = false;

  public static void main(String[] argv)
  {
    String defaultDirectory = null;
    String archive = null;
    String expression = null;

    {
      java.net.URL detectURL = java.lang.ClassLoader.getSystemResource("scripts/yacasinit.ys");

      if (detectURL != null)
      {
        String path = detectURL.getPath();
        if (path.contains("!"))
            archive = path.substring(0, path.lastIndexOf('!'));
      }
    }

    int i=0;
    while (i<argv.length)
    {
      if (argv[i].equals("--rootdir"))
      {
        i++;
        defaultDirectory = argv[i];
      }
      else if (argv[i].equals("--archive"))
      {
        i++;
        archive = argv[i];
      }
      else if (argv[i].equals("-i"))
      {
        i++;
        expression = argv[i];
      }
      else
      {
        break;
      }
      i++;
    }
    int scriptsToRun = i;


    CYacas yacas = new CYacas(System.out);
    yacas.env.iCurrentInput = new CachedStdFileInput(yacas.env.iInputStatus);

    try
    {
        if (archive != null) {
            String zipFileName = archive;
            java.util.zip.ZipFile z = new java.util.zip.ZipFile(new File(new java.net.URI(zipFileName)));
            LispStandard.zipFile = z;
        }
    }
    catch(Exception e)
    {
      System.out.println("Failed to find yacas.jar"+e.toString());
    }


    if (defaultDirectory != null)
    {
      String toEvaluate = "DefaultDirectory(\""+defaultDirectory+"\");";
      String result = yacas.Evaluate(toEvaluate);
      if (scriptsToRun == argv.length)
        System.out.println("Out> "+result);
    }
    {
      String result = yacas.Evaluate("Load(\"yacasinit.ys\");");
      if (scriptsToRun == argv.length && expression == null)
        System.out.println("Out> "+result);
    }
    if (expression != null)
    {
        String result = yacas.Evaluate(expression);
        return;
    }
    if (scriptsToRun < argv.length)
    {
      for (;scriptsToRun<argv.length;scriptsToRun++)
      {
        yacas.Evaluate("Load(\""+argv[scriptsToRun]+"\");");
      }
      return;
    }


    System.out.println("This is Yacas version '" + CVersion.VERSION + "'.");

    System.out.println("Yacas is Free Software--Free as in Freedom--so you can redistribute Yacas or");
    System.out.println("modify it under certain conditions. Yacas comes with ABSOLUTELY NO WARRANTY.");
    System.out.println("See the GNU Lesser General Public License (LGPL) version 2.1 or (at your");
    System.out.println("discretion) any later version for the full conditions.");
    System.out.println("Type ?license or ?licence to see the LGPL version 2.1;");
    System.out.println("type ?warranty for warranty info.");
    System.out.println("See http://yacas.sf.net for more information and documentation on Yacas.");
    System.out.println("Type ?? for help. Or type ?function for help on a function.\n");

    System.out.println("To exit Yacas, enter  Exit(); or quit or Ctrl-c.\n");
    //    System.out.println("Type 'restart' to restart Yacas.\n");

    System.out.println("To see example commands, keep typing Example();\n");

    System.out.println("Yacas in Java");

    while (!quitting) {

        System.out.print("In> ");
        String input = readLine(System.in);

        String rs = "True";

        if (input.trim().equals("quit")) {
            quitting = true;
        } else if (input.trim().startsWith("?")) {
            String key = input.trim().substring(1);

            String prefix = "http://yacas.sourceforge.net/";

            try {
                URI uri = new URI(prefix + "ref.html?" + key);

                if (key.equals("license") || key.equals("licence"))
                    uri = new URI(prefix + "refprogchapter9.html");
                else if (key.equals("warranty"))
                    uri = new URI(prefix + "refprogchapter9.html#c9s2");
                else if (key.equals("?"))
                    uri = new URI(prefix + "refmanual.html");

                try {
                    Desktop.getDesktop().browse(uri);
                } catch (IOException e) {
                    // FIXME: report the error
                    rs = "False";
                }

            } catch (URISyntaxException e) {
                // it's a cold night in Hell
                rs = "False";
            }
        } else {
            rs = yacas.Evaluate(input);
        }

        System.out.println("Out> " + rs);
    }
  }
}

@@ -205,21 +205,88 @@
 eam 
-aStream)%0A  %7B%0A
+stream) %7B%0A        InputStreamReader reader = new InputStreamReader(stream);%0A    
     
@@ -327,34 +327,40 @@
 lder();%0A    
+    
 try
-%0A
+ %7B%0A 
     
-%7B%0A
+ 
       int c 
@@ -357,31 +357,30 @@
     int c = 
-aStream
+reader
 .read();%0A   
@@ -378,24 +378,30 @@
 ad();%0A      
+      
 while (c != 
@@ -405,25 +405,29 @@
 != '%5Cn')
+ %7B
 %0A      
-%7B%0A
+    
       li
@@ -442,16 +442,17 @@
 d((char)
+ 
 c);%0A    
@@ -457,19 +457,28 @@
     
-c = aStream
+          c = reader
 .rea
@@ -484,26 +484,26 @@
 ad();%0A      
-%7D%0A
+  
     %7D%0A    ca
@@ -495,24 +495,30 @@
        %7D%0A   
+     %7D
  catch (Exce
@@ -517,39 +517,41 @@
 ch (Exception e)
+ %7B
 %0A    
-%7B%0A
+  
       System.out
@@ -574,26 +574,34 @@
 ing());%0A    
-%7D%0A
+    %7D%0A    
     return l
@@ -618,17 +618,20 @@
 ng();%0A  
-%7D
+  %7D%0A
 %0A  stati

handle unicode in console Task #112 - jyacas doesn't read unicode files