Add command line capabilities

Allows sending openrecoveryscript commands to TWRP via shell.
This may be handy for visually impaired users, for various one
click utilities to drive TWRP commands from a computer, for using
TWRP when a catastrophic hardware failure like a shattered screen
prevents you from being able to use touch, or even on devices like
a TV stick where touch and USB mouse input is unavailable.

This patch also includes a few minor changes to openrecoveryscript
including proper support for rebooting via the script and for
decrypting the device via the command line.

Change-Id: I796ad168efdd2da9c25334ac93d1079daaa7b0bc
diff --git a/gui/console.cpp b/gui/console.cpp
index b1f16c4..897c582 100644
--- a/gui/console.cpp
+++ b/gui/console.cpp
@@ -28,6 +28,7 @@
 
 static std::vector<std::string> gConsole;
 static std::vector<std::string> gConsoleColor;
+static FILE* ors_file;
 
 extern "C" void __gui_print(const char *color, char *buf)
 {
@@ -57,6 +58,10 @@
 		gConsole.push_back(start);
 		gConsoleColor.push_back(color);
 	}
+	if (ors_file) {
+		fprintf(ors_file, "%s\n", buf);
+		fflush(ors_file);
+	}
 }
 
 extern "C" void gui_print(const char *fmt, ...)
@@ -89,6 +94,11 @@
 	return;
 }
 
+extern "C" void gui_set_FILE(FILE* f)
+{
+	ors_file = f;
+}
+
 GUIConsole::GUIConsole(xml_node<>* node) : GUIObject(node)
 {
 	xml_attribute<>* attr;
diff --git a/gui/gui.cpp b/gui/gui.cpp
index b768090..8006339 100644
--- a/gui/gui.cpp
+++ b/gui/gui.cpp
@@ -53,6 +53,8 @@
 #include "../variables.h"
 #include "../partitions.hpp"
 #include "../twrp-functions.hpp"
+#include "../openrecoveryscript.hpp"
+#include "../orscmd/orscmd.h"
 #ifndef TW_NO_SCREEN_TIMEOUT
 #include "blanktimer.hpp"
 #endif
@@ -73,6 +75,7 @@
 pthread_mutex_t gForceRendermutex;
 static int gNoAnimation = 1;
 static int gGuiInputRunning = 0;
+static int gCmdLineRunning = 0;
 #ifndef TW_NO_SCREEN_TIMEOUT
 blanktimer blankTimer;
 #endif
@@ -434,6 +437,85 @@
 	return NULL;
 }
 
+static void * command_thread(void *cookie)
+{
+	int read_fd;
+	FILE* orsout;
+	char command[1024], result[512];
+
+	LOGINFO("Starting command line thread\n");
+
+	unlink(ORS_INPUT_FILE);
+	if (mkfifo(ORS_INPUT_FILE, 06660) != 0) {
+		LOGINFO("Unable to mkfifo %s\n", ORS_INPUT_FILE);
+		return 0;
+	}
+	unlink(ORS_OUTPUT_FILE);
+	if (mkfifo(ORS_OUTPUT_FILE, 06666) != 0) {
+		LOGINFO("Unable to mkfifo %s\n", ORS_OUTPUT_FILE);
+		unlink(ORS_INPUT_FILE);
+		return 0;
+	}
+
+	read_fd = open(ORS_INPUT_FILE, O_RDONLY);
+	if (read_fd < 0) {
+		LOGINFO("Unable to open %s\n", ORS_INPUT_FILE);
+		unlink(ORS_INPUT_FILE);
+		unlink(ORS_OUTPUT_FILE);
+		return 0;
+	}
+
+	while (!gGuiRunning)
+		sleep(1);
+
+	for (;;) {
+		while (read(read_fd, &command, sizeof(command)) > 0) {
+			command[1022] = '\n';
+			command[1023] = '\0';
+			LOGINFO("Command '%s' received\n", command);
+			orsout = fopen(ORS_OUTPUT_FILE, "w");
+			if (!orsout) {
+				close(read_fd);
+				LOGINFO("Unable to fopen %s\n", ORS_OUTPUT_FILE);
+				unlink(ORS_INPUT_FILE);
+				unlink(ORS_OUTPUT_FILE);
+				return 0;
+			}
+			if (DataManager::GetIntValue("tw_busy") != 0) {
+				strcpy(result, "Failed, operation in progress\n");
+				fprintf(orsout, "%s", result);
+				LOGINFO("Command cannot be performed, operation in progress.\n");
+			} else {
+				if (gui_console_only() == 0) {
+					LOGINFO("Console started successfully\n");
+					gui_set_FILE(orsout);
+					if (strlen(command) > 11 && strncmp(command, "runscript", 9) == 0) {
+						char* filename = command + 11;
+						if (OpenRecoveryScript::copy_script_file(filename) == 0) {
+							LOGERR("Unable to copy script file\n");
+						} else {
+							OpenRecoveryScript::run_script_file();
+						}
+					} else if (strlen(command) > 5 && strncmp(command, "get", 3) == 0) {
+						char* varname = command + 4;
+						string temp;
+						DataManager::GetValue(varname, temp);
+						gui_print("%s = %s\n", varname, temp.c_str());
+					} else if (OpenRecoveryScript::Insert_ORS_Command(command)) {
+						OpenRecoveryScript::run_script_file();
+					}
+					gui_set_FILE(NULL);
+					gGuiConsoleTerminate = 1;
+				}
+			}
+			fclose(orsout);
+		}
+	}
+	close(read_fd);
+	LOGINFO("Command thread exiting\n");
+	return 0;
+}
+
 // This special function will return immediately the first time, but then
 // always returns 1/30th of a second (or immediately if called later) from
 // the last time it was called
@@ -495,6 +577,10 @@
 	{
 		loopTimer();
 
+		if (gGuiConsoleRunning) {
+			continue;
+		}
+
 		if (!gForceRender)
 		{
 			int ret;
@@ -769,7 +855,15 @@
 		pthread_create(&t, NULL, input_thread, NULL);
 		gGuiInputRunning = 1;
 	}
-
+#ifndef TW_OEM_BUILD
+	if (!gCmdLineRunning)
+	{
+		// Start by spinning off an input handler.
+		pthread_t t;
+		pthread_create(&t, NULL, command_thread, NULL);
+		gCmdLineRunning = 1;
+	}
+#endif
 	return runPages();
 }
 
@@ -830,6 +924,9 @@
 		}
 	}
 	gGuiConsoleRunning = 0;
+	gForceRender = 1; // this will kickstart the GUI to render again
+	PageManager::EndConsole();
+	LOGINFO("Console stopping\n");
 	return NULL;
 }
 
diff --git a/gui/gui.h b/gui/gui.h
index a927cf3..8e0371e 100644
--- a/gui/gui.h
+++ b/gui/gui.h
@@ -19,6 +19,8 @@
 #ifndef _GUI_HEADER
 #define _GUI_HEADER
 
+#include <stdio.h>
+
 int gui_console_only();
 int gui_init();
 int gui_loadResources();
@@ -26,6 +28,7 @@
 int gui_startPage(const char* page_name);
 void gui_print(const char *fmt, ...);
 void gui_print_color(const char *color, const char *fmt, ...);
+void gui_set_FILE(FILE* f);
 
 #endif  // _GUI_HEADER
 
diff --git a/gui/pages.cpp b/gui/pages.cpp
index 72ffda3..3632cc3 100644
--- a/gui/pages.cpp
+++ b/gui/pages.cpp
@@ -1110,6 +1110,16 @@
 	return 0;
 }
 
+int PageManager::EndConsole(void)
+{
+	if (mCurrentSet && mBaseSet) {
+		delete mCurrentSet;
+		mCurrentSet = mBaseSet;
+		return 0;
+	}
+	return -1;
+}
+
 int PageManager::IsCurrentPage(Page* page)
 {
 	return (mCurrentSet ? mCurrentSet->IsCurrentPage(page) : 0);
diff --git a/gui/pages.hpp b/gui/pages.hpp
index c384cf0..2afbe78 100644
--- a/gui/pages.hpp
+++ b/gui/pages.hpp
@@ -120,8 +120,9 @@
 	static Resource* FindResource(std::string name);
 	static Resource* FindResource(std::string package, std::string name);
 
-	// Used for console-only mode - Can be reverted via ChangePage
+	// Used for console-only mode
 	static int SwitchToConsole(void);
+	static int EndConsole(void);
 
 	// Helper to identify if a particular page is the active page
 	static int IsCurrentPage(Page* page);