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/Android.mk b/Android.mk
index 05111c0..6d0b840 100644
--- a/Android.mk
+++ b/Android.mk
@@ -400,6 +400,9 @@
 ifeq ($(TW_INCLUDE_CRYPTO), true)
     include $(commands_recovery_local_path)/crypto/ics/Android.mk
 endif
+ifneq ($(TW_OEM_BUILD),true)
+    include $(commands_recovery_local_path)/orscmd/Android.mk
+endif
 
 # FB2PNG
 ifeq ($(TW_INCLUDE_FB2PNG), true)
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);
diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp
index 0147027..1461f2d 100644
--- a/openrecoveryscript.cpp
+++ b/openrecoveryscript.cpp
@@ -60,6 +60,20 @@
 	return 0;
 }
 
+int OpenRecoveryScript::copy_script_file(string filename) {
+	if (TWFunc::Path_Exists(filename)) {
+		LOGINFO("Script file found: '%s'\n", filename.c_str());
+		if (filename == SCRIPT_FILE_TMP)
+			return 1; // file is already in the right place
+		// Copy script file to /tmp
+		TWFunc::copy_file(filename, SCRIPT_FILE_TMP, 0755);
+		// Delete the old file
+		unlink(filename.c_str());
+		return 1;
+	}
+	return 0;
+}
+
 int OpenRecoveryScript::run_script_file(void) {
 	FILE *fp = fopen(SCRIPT_FILE_TMP, "r");
 	int ret_val = 0, cindex, line_len, i, remove_nl, install_cmd = 0, sideload = 0;
@@ -305,7 +319,16 @@
 					ret_val = 1;
 				}
 			} else if (strcmp(command, "reboot") == 0) {
-				// Reboot
+				if (strlen(value) && strcmp(value, "recovery") == 0)
+					TWFunc::tw_reboot(rb_recovery);
+				else if (strlen(value) && strcmp(value, "poweroff") == 0)
+					TWFunc::tw_reboot(rb_poweroff);
+				else if (strlen(value) && strcmp(value, "bootloader") == 0)
+					TWFunc::tw_reboot(rb_bootloader);
+				else if (strlen(value) && strcmp(value, "download") == 0)
+					TWFunc::tw_reboot(rb_download);
+				else
+					TWFunc::tw_reboot(rb_system);
 			} else if (strcmp(command, "cmd") == 0) {
 				DataManager::SetValue("tw_action_text2", "Running Command");
 				if (cindex != 0) {
@@ -350,6 +373,15 @@
 				ret_val = PartitionManager.Fix_Permissions();
 				if (ret_val != 0)
 					ret_val = 1; // failure
+			} else if (strcmp(command, "decrypt") == 0) {
+				if (*value) {
+					ret_val = PartitionManager.Decrypt_Device(value);
+					if (ret_val != 0)
+						ret_val = 1; // failure
+				} else {
+					LOGERR("No password provided.\n");
+					ret_val = 1; // failure
+				}
 			} else {
 				LOGERR("Unrecognized script command: '%s'\n", command);
 				ret_val = 1;
diff --git a/openrecoveryscript.hpp b/openrecoveryscript.hpp
index 45d33e6..c3eabf6 100644
--- a/openrecoveryscript.hpp
+++ b/openrecoveryscript.hpp
@@ -28,6 +28,7 @@
 {
 public:
 	static int check_for_script_file();                                            // Checks to see if the ORS file is present in /cache
+	static int copy_script_file(string filename);                                  // Copies a script file to the temp folder
 	static int run_script_file();                                                  // Executes the commands in the ORS file
 	static int Insert_ORS_Command(string Command);                                 // Inserts the Command into the SCRIPT_FILE_TMP file
 	static int Install_Command(string Zip);                                        // Installs a zip
diff --git a/orscmd/Android.mk b/orscmd/Android.mk
new file mode 100644
index 0000000..8ddf93f
--- /dev/null
+++ b/orscmd/Android.mk
@@ -0,0 +1,12 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	orscmd.cpp
+LOCAL_CFLAGS:= -g -c -W
+LOCAL_MODULE:=orscmd
+LOCAL_MODULE_STEM := twrp
+LOCAL_MODULE_TAGS:= eng
+LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES
+LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin
+include $(BUILD_EXECUTABLE)
diff --git a/orscmd/orscmd.cpp b/orscmd/orscmd.cpp
new file mode 100644
index 0000000..0240ff9
--- /dev/null
+++ b/orscmd/orscmd.cpp
@@ -0,0 +1,91 @@
+/*
+		TWRP is free software: you can redistribute it and/or modify
+		it under the terms of the GNU General Public License as published by
+		the Free Software Foundation, either version 3 of the License, or
+		(at your option) any later version.
+
+		TWRP is distributed in the hope that it will be useful,
+		but WITHOUT ANY WARRANTY; without even the implied warranty of
+		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+		GNU General Public License for more details.
+
+		You should have received a copy of the GNU General Public License
+		along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+
+#include "orscmd.h"
+#include "../variables.h"
+
+void print_version(void) {
+	printf("TWRP openrecoveryscript command line tool, TWRP version %s\n\n", TW_VERSION_STR);
+}
+
+void print_usage(void) {
+	print_version();
+	printf("Allows command line usage of TWRP via openrecoveryscript commands.\n");
+	printf("Some common commands include:\n");
+	printf("  install /path/to/update.zip\n");
+	printf("  backup BSDC backupname\n");
+	printf("  restore backupname BSDC\n");
+	printf("  factoryreset\n");
+	printf("  wipe cache\n");
+	printf("  sideload\n");
+	printf("  set variable value\n");
+	printf("  get variable\n");
+	printf("  decrypt password\n");
+	printf("\nSee more documentation at http://teamw.in/openrecoveryscript\n");
+}
+
+int main(int argc, char **argv) {
+	int read_fd, write_fd, index;
+	char command[1024], result[512];
+
+	if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) {
+		print_usage();
+		return 0;
+	}
+	if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
+		print_version();
+		return 0;
+	}
+
+	sprintf(command, "%s", argv[1]);
+	for (index = 2; index < argc; index++) {
+		sprintf(command, "%s %s", command, argv[index]);
+	}
+
+	write_fd = open(ORS_INPUT_FILE, O_WRONLY);
+	if (write_fd < 0) {
+		printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n");
+		printf("Press CTRL + C to quit.\n");
+		while (write_fd < 0)
+			write_fd = open(ORS_INPUT_FILE, O_WRONLY);
+	}
+	if (write(write_fd, command, sizeof(command)) != sizeof(command)) {
+		printf("Error sending command.\n");
+		close(write_fd);
+		return -1;
+	}
+	read_fd = open(ORS_OUTPUT_FILE, O_RDONLY);
+	if (read_fd < 0) {
+		printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE);
+		return -1;
+	}
+	memset(&result, 0, sizeof(result));
+	while (read(read_fd, &result, sizeof(result)) > 0) {
+		result[510] = '\n';
+		result[511] = '\0';
+		printf("%s", result);
+		memset(&result, 0, sizeof(result));
+	}
+	close(write_fd);
+	close(read_fd);
+	return 0;
+}
diff --git a/orscmd/orscmd.h b/orscmd/orscmd.h
new file mode 100644
index 0000000..f186add
--- /dev/null
+++ b/orscmd/orscmd.h
@@ -0,0 +1,22 @@
+/*
+		TWRP is free software: you can redistribute it and/or modify
+		it under the terms of the GNU General Public License as published by
+		the Free Software Foundation, either version 3 of the License, or
+		(at your option) any later version.
+
+		TWRP is distributed in the hope that it will be useful,
+		but WITHOUT ANY WARRANTY; without even the implied warranty of
+		MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+		GNU General Public License for more details.
+
+		You should have received a copy of the GNU General Public License
+		along with TWRP.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __ORSCMD_H
+#define __ORSCMD_H
+
+#define ORS_INPUT_FILE "/sbin/orsin"
+#define ORS_OUTPUT_FILE "/sbin/orsout"
+
+#endif //__ORSCMD_H
diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk
index 7342534..8a29e14 100644
--- a/prebuilt/Android.mk
+++ b/prebuilt/Android.mk
@@ -51,6 +51,9 @@
 RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libstlport.so
 #RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmincrypt.so
 RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/toolbox
+ifneq ($(TW_OEM_BUILD),true)
+    RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/twrp
+endif
 ifeq ($(TARGET_USERIMAGES_USE_EXT4), true)
     RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libext4_utils.so
 endif
diff --git a/twrp.cpp b/twrp.cpp
index c3baa2e..8b60ee1 100644
--- a/twrp.cpp
+++ b/twrp.cpp
@@ -1,6 +1,4 @@
 /*
-
-ccdd
 		TWRP is free software: you can redistribute it and/or modify
 		it under the terms of the GNU General Public License as published by
 		the Free Software Foundation, either version 3 of the License, or