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/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