blob: 53c5bc030f82f66079b8db46a8d98d7c46f9deea [file] [log] [blame]
Ethan Yonker03a42f62014-08-08 11:03:51 -05001/*
2 TWRP is free software: you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation, either version 3 of the License, or
5 (at your option) any later version.
6
7 TWRP is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
14*/
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
20#include <string.h>
21#include <fcntl.h>
22
23#include "orscmd.h"
24#include "../variables.h"
25
26void print_version(void) {
27 printf("TWRP openrecoveryscript command line tool, TWRP version %s\n\n", TW_VERSION_STR);
28}
29
30void print_usage(void) {
31 print_version();
32 printf("Allows command line usage of TWRP via openrecoveryscript commands.\n");
33 printf("Some common commands include:\n");
34 printf(" install /path/to/update.zip\n");
35 printf(" backup BSDC backupname\n");
36 printf(" restore backupname BSDC\n");
37 printf(" factoryreset\n");
38 printf(" wipe cache\n");
39 printf(" sideload\n");
40 printf(" set variable value\n");
41 printf(" get variable\n");
42 printf(" decrypt password\n");
bigbiffce8f83c2015-12-12 18:30:21 -050043 printf(" remountrw\n");
Ethan Yonker03a42f62014-08-08 11:03:51 -050044 printf("\nSee more documentation at http://teamw.in/openrecoveryscript\n");
45}
46
47int main(int argc, char **argv) {
48 int read_fd, write_fd, index;
49 char command[1024], result[512];
50
51 if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) {
52 print_usage();
53 return 0;
54 }
55 if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) {
56 print_version();
57 return 0;
58 }
59
60 sprintf(command, "%s", argv[1]);
61 for (index = 2; index < argc; index++) {
62 sprintf(command, "%s %s", command, argv[index]);
63 }
64
65 write_fd = open(ORS_INPUT_FILE, O_WRONLY);
66 if (write_fd < 0) {
67 printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n");
68 printf("Press CTRL + C to quit.\n");
69 while (write_fd < 0)
70 write_fd = open(ORS_INPUT_FILE, O_WRONLY);
71 }
72 if (write(write_fd, command, sizeof(command)) != sizeof(command)) {
73 printf("Error sending command.\n");
74 close(write_fd);
75 return -1;
76 }
77 read_fd = open(ORS_OUTPUT_FILE, O_RDONLY);
78 if (read_fd < 0) {
79 printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE);
80 return -1;
81 }
82 memset(&result, 0, sizeof(result));
83 while (read(read_fd, &result, sizeof(result)) > 0) {
84 result[510] = '\n';
85 result[511] = '\0';
86 printf("%s", result);
87 memset(&result, 0, sizeof(result));
88 }
89 close(write_fd);
90 close(read_fd);
91 return 0;
92}