Skip to content

Commit

Permalink
Begin adding script features
Browse files Browse the repository at this point in the history
* Update SycoraxKernel.java to have scripting features (which is in a dormant state)
  • Loading branch information
DAK404 committed May 14, 2024
1 parent f103a71 commit 7427620
Showing 1 changed file with 85 additions and 14 deletions.
99 changes: 85 additions & 14 deletions Source/Cataphract/Core/SycoraxKernel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package Cataphract.Core;

import java.io.BufferedReader;
import java.io.Console;
import java.io.File;
import java.io.FileReader;

import Cataphract.API.Anvil;
import Cataphract.API.Build;
Expand All @@ -14,19 +17,23 @@
import Cataphract.API.Dragon.Login;

import Cataphract.API.Minotaur.Cryptography;
import Cataphract.API.Minotaur.PolicyCheck;

public class SycoraxKernel
{

private String _accountName = "";
private String _username = "";
private String _accountName = "DEFAULT_USER";
private String _username = "DEFAULT_USERNAME";
private String _userUnlockPIN = "";
private String _systemName = "DEFAULT_SYSNAME";

private boolean _isUserAdministrator = false;
private boolean _scriptMode = false;

private char _prompt = '?';

private int _loginAttemptsRemaining = 5;
private int _scriptLineNumber = 0;

private Console console = System.console();

Expand Down Expand Up @@ -56,16 +63,20 @@ public void startSycoraxKernel() throws Exception

private void userShell() throws Exception
{
String[] commandArray;
String input = "";

do
{
input = console.readLine(_accountName + "@Cataphract" + _prompt + "> ");
input = console.readLine(_accountName + "@" + _systemName + _prompt + "> ");
commandProcessor(input);
}
while(!input.equalsIgnoreCase("logout"));
}

commandArray = Anvil.splitStringToArray(input);
private void commandProcessor(String input)throws Exception
{
String[] commandArray = Anvil.splitStringToArray(input);

switch(commandArray[0].toLowerCase())
switch(commandArray[0].toLowerCase())
{
case "refresh":
fetchUserDetails();
Expand All @@ -79,9 +90,9 @@ private void userShell() throws Exception
lockConsole();
break;

case "clear":
viewSycoraxInformation();
break;
// case "clear":
// viewSycoraxInformation();
// break;

case "exit":
System.exit(0);
Expand Down Expand Up @@ -118,8 +129,6 @@ private void userShell() throws Exception
Anvil.anvilInterpreter(commandArray);
break;
}
}
while(!input.equalsIgnoreCase("logout"));
}

private boolean login() throws Exception
Expand Down Expand Up @@ -148,6 +157,7 @@ private void fetchUserDetails()throws Exception
_accountName = new Login(_username).getNameLogic();
_isUserAdministrator = new Login(_username).checkPrivilegeLogic();
_userUnlockPIN = new Login(_username).getPINLogic();
_systemName = new PolicyCheck().retrievePolicyValue("sysname");

_prompt = _isUserAdministrator?'!':'*';
}
Expand Down Expand Up @@ -187,8 +197,69 @@ private boolean challengePIN() throws Exception
return userPIN.equals(_userUnlockPIN);
}

private boolean scriptParseLogic(String scriptFileName) throws Exception
private boolean anvilScriptEngine(String scriptFileName)throws Exception
{
return false;
boolean status = false;

if(! _isUserAdministrator && ! new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("script").equals("on"))
{
//to process the anvil script files
try
{
//Check if the name of the script file is a valid string
if(scriptFileName == null || scriptFileName.equalsIgnoreCase("") || scriptFileName.startsWith(" "))
IOStreams.printError("The name of the script file cannot be be blank.");

//Check if the script file specified exists.
else if(! new File(scriptFileName).exists())
//Return an error and pass the control back in case the file is not found.
IOStreams.printAttention("Script file "+scriptFileName.replace(_username, _accountName)+" has not been found.\nPlease check the directory of the script file and try again.");

else
{
//Activate the script mode.
_scriptMode = true;

//Initialize a stream to read the given file.
BufferedReader br = new BufferedReader(new FileReader(scriptFileName));

//Initialize a string to hold the contents of the script file being executed.
String scriptLine = "";

//Read the script file, line by line.
while ((scriptLine = br.readLine()) != null)
{
//Check if the line is a comment or is blank in the script file and skip the line.
if(scriptLine.startsWith("#") || scriptLine.equalsIgnoreCase(""))
continue;

//Check if End Script command is encountered, which will stop the execution of the script.
else if(scriptLine.equalsIgnoreCase("End Script"))
break;

//Read the command in the script file, and pass it on to menuLogic(<command>) for it to be processed.
commandProcessor(scriptLine);
_scriptLineNumber++;
}

//Close the streams, run the garbage collector and clean.
br.close();
_scriptLineNumber = 0;
System.gc();

//Deactivate the script mode.
_scriptMode = false;
}
}
catch(Exception e)
{
//handle exception
}
}
else
{
IOStreams.printError("Insufficient Privileges to run scripts! Please contact the Administrator for more information.");
}
return status;
}
}

0 comments on commit 7427620

Please sign in to comment.