package com.fidesmo.gradle.plugin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Pattern;

import jline.Terminal;
import jline.TerminalFactory;
import jline.console.ConsoleReader;

import com.fidesmo.sec.utils.Hex;
import nordpol.IsoCard;

/** Simple command line to communicate with card.
 *
 */
public class Console {

    public static void main(String args[]) {
        try {
            (new Console()).run();
        } catch (IOException e) {
            System.err.println(e);
            System.exit(1);
        }
    }

    public void run() throws IOException {
        Terminal terminal = TerminalFactory.create();
        terminal.setEchoEnabled(true);
        ConsoleReader reader = new ConsoleReader("Fidesmo console",
                                                 System.in,
                                                 System.out,
                                                 terminal);

        IsoCard card = (new SmartcardioTransceiver())
            .getCard()
            .toBlocking()
            .first();

        String line;
        PrintWriter out = new PrintWriter(reader.getOutput());

        while((line = reader.readLine("> ")) != null) {
            String[] tokens = line.split(" ");
            if (tokens[0].equals("send")) {
                if (tokens.length < 2) {
                    out.println("To few arguments for send command");
                }

                byte[] apdu = Hex.decodeHex(tokens[1].toUpperCase());
                card.transceive(apdu); // output displayed through log
            } else if (line.equals("exit")) {
                break;
            } else if (line.equals("help")) {
                out.println("send <hex string>  -- send apdu to card");
                out.println("help               -- display this message");
                out.println("exit               -- exit this shell");
            } else {
                out.println("Unknown command. Type 'exit' to leave.");
            }
        }
    }
}

@@ -207,16 +207,60 @@
 Reader;%0A
+import jline.console.UserInterruptException;
 %0Aimport 
@@ -985,16 +985,60 @@
 minal);%0A
+        reader.setHandleUserInterrupt(true);
 %0A       
@@ -1240,17 +1240,34 @@
 put());%0A
-%0A
+        try %7B%0A    
         
@@ -1322,24 +1322,28 @@
             
+    
 String%5B%5D tok
@@ -1369,32 +1369,36 @@
 %22);%0A            
+    
 if (tokens%5B0%5D.eq
@@ -1421,32 +1421,36 @@
                 
+ 
+   
 if (tokens.lengt
@@ -1454,24 +1454,28 @@
 ngth %3C 2) %7B%0A
+    
             
@@ -1544,27 +1544,35 @@
             
+ 
-%7D%0A%0A
+   %7D%0A%0A    
             
@@ -1645,16 +1645,20 @@
         
+  
+  
 card.tra
@@ -1708,32 +1708,36 @@
 log%0A            
+    
 %7D else if (line.
@@ -1774,15 +1774,23 @@
     
+  
+  
 break;%0A
+    
     
@@ -1839,32 +1839,36 @@
                 
+   
+ 
 out.println(%22sen
@@ -1903,24 +1903,28 @@
  to card%22);%0A
+    
             
@@ -1994,32 +1994,36 @@
                 
+   
+ 
 out.println(%22exi
@@ -2068,24 +2068,28 @@
             
+    
 %7D else %7B%0A   
@@ -2085,16 +2085,20 @@
  else %7B%0A
+    
         
@@ -2156,24 +2156,218 @@
 o leave.%22);%0A
+                %7D%0A            %7D%0A        %7D catch(UserInterruptException uie) %7B%0A            try %7B%0A                card.close();%0A            %7D catch(IOException ioe) %7B%0A                /* Ignore */%0A
             

Handle end user interrupt