Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | void print_version(void) { |
| 27 | printf("TWRP openrecoveryscript command line tool, TWRP version %s\n\n", TW_VERSION_STR); |
| 28 | } |
| 29 | |
| 30 | void 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"); |
| 43 | printf("\nSee more documentation at http://teamw.in/openrecoveryscript\n"); |
| 44 | } |
| 45 | |
| 46 | int main(int argc, char **argv) { |
| 47 | int read_fd, write_fd, index; |
| 48 | char command[1024], result[512]; |
| 49 | |
| 50 | if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "?") == 0 || strcmp(argv[1], "-h") == 0) { |
| 51 | print_usage(); |
| 52 | return 0; |
| 53 | } |
| 54 | if (strcmp(argv[1], "version") == 0 || strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0) { |
| 55 | print_version(); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | sprintf(command, "%s", argv[1]); |
| 60 | for (index = 2; index < argc; index++) { |
| 61 | sprintf(command, "%s %s", command, argv[index]); |
| 62 | } |
| 63 | |
| 64 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 65 | if (write_fd < 0) { |
| 66 | printf("TWRP does not appear to be running. Waiting for TWRP to start . . .\n"); |
| 67 | printf("Press CTRL + C to quit.\n"); |
| 68 | while (write_fd < 0) |
| 69 | write_fd = open(ORS_INPUT_FILE, O_WRONLY); |
| 70 | } |
| 71 | if (write(write_fd, command, sizeof(command)) != sizeof(command)) { |
| 72 | printf("Error sending command.\n"); |
| 73 | close(write_fd); |
| 74 | return -1; |
| 75 | } |
| 76 | read_fd = open(ORS_OUTPUT_FILE, O_RDONLY); |
| 77 | if (read_fd < 0) { |
| 78 | printf("Unable to open %s for read.\n", ORS_OUTPUT_FILE); |
| 79 | return -1; |
| 80 | } |
| 81 | memset(&result, 0, sizeof(result)); |
| 82 | while (read(read_fd, &result, sizeof(result)) > 0) { |
| 83 | result[510] = '\n'; |
| 84 | result[511] = '\0'; |
| 85 | printf("%s", result); |
| 86 | memset(&result, 0, sizeof(result)); |
| 87 | } |
| 88 | close(write_fd); |
| 89 | close(read_fd); |
| 90 | return 0; |
| 91 | } |