shuffle some code
Change-Id: Ifb5afe8ef6de7b6de58dfc07a30c1f89b5c1eb08
diff --git a/edifyscripting.c b/edifyscripting.c
index ddbc877..e66df2b 100644
--- a/edifyscripting.c
+++ b/edifyscripting.c
@@ -297,3 +297,133 @@
}
return 0;
}
+
+
+
+#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
+
+int run_and_remove_extendedcommand()
+{
+ char tmp[PATH_MAX];
+ sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
+ __system(tmp);
+ remove(EXTENDEDCOMMAND_SCRIPT);
+ int i = 0;
+ for (i = 20; i > 0; i--) {
+ ui_print("Waiting for SD Card to mount (%ds)\n", i);
+ if (ensure_path_mounted("/sdcard") == 0) {
+ ui_print("SD Card mounted...\n");
+ break;
+ }
+ sleep(1);
+ }
+ remove("/sdcard/clockworkmod/.recoverycheckpoint");
+ if (i == 0) {
+ ui_print("Timed out waiting for SD card... continuing anyways.");
+ }
+
+ ui_print("Verifying SD Card marker...\n");
+ struct stat st;
+ if (stat("/sdcard/clockworkmod/.salted_hash", &st) != 0) {
+ ui_print("SD Card marker not found...\n");
+ if (volume_for_path("/emmc") != NULL) {
+ ui_print("Checking Internal SD Card marker...\n");
+ ensure_path_unmounted("/sdcard");
+ if (ensure_path_mounted_at_mount_point("/emmc", "/sdcard") != 0) {
+ ui_print("Internal SD Card marker not found... continuing anyways.\n");
+ // unmount everything, and remount as normal
+ ensure_path_unmounted("/emmc");
+ ensure_path_unmounted("/sdcard");
+
+ ensure_path_mounted("/sdcard");
+ }
+ }
+ }
+
+ sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
+ int ret;
+#ifdef I_AM_KOUSH
+ if (0 != (ret = before_run_script(tmp))) {
+ ui_print("Error processing ROM Manager script. Please verify that you are performing the backup, restore, or ROM installation from ROM Manager v4.4.0.0 or higher.\n");
+ return ret;
+ }
+#endif
+ return run_script(tmp);
+}
+
+int extendedcommand_file_exists()
+{
+ struct stat file_info;
+ return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
+}
+
+int edify_main(int argc, char** argv) {
+ load_volume_table();
+ process_volumes();
+ RegisterBuiltins();
+ RegisterRecoveryHooks();
+ FinishRegistration();
+
+ if (argc != 2) {
+ printf("edify <filename>\n");
+ return 1;
+ }
+
+ FILE* f = fopen(argv[1], "r");
+ if (f == NULL) {
+ printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
+ return 1;
+ }
+ char buffer[8192];
+ int size = fread(buffer, 1, 8191, f);
+ fclose(f);
+ buffer[size] = '\0';
+
+ Expr* root;
+ int error_count = 0;
+ yy_scan_bytes(buffer, size);
+ int error = yyparse(&root, &error_count);
+ printf("parse returned %d; %d errors encountered\n", error, error_count);
+ if (error == 0 || error_count > 0) {
+
+ //ExprDump(0, root, buffer);
+
+ State state;
+ state.cookie = NULL;
+ state.script = buffer;
+ state.errmsg = NULL;
+
+ char* result = Evaluate(&state, root);
+ if (result == NULL) {
+ printf("result was NULL, message is: %s\n",
+ (state.errmsg == NULL ? "(NULL)" : state.errmsg));
+ free(state.errmsg);
+ } else {
+ printf("result is [%s]\n", result);
+ }
+ }
+ return 0;
+}
+
+int run_script(char* filename)
+{
+ struct stat file_info;
+ if (0 != stat(filename, &file_info)) {
+ printf("Error executing stat on file: %s\n", filename);
+ return 1;
+ }
+
+ int script_len = file_info.st_size;
+ char* script_data = (char*)malloc(script_len + 1);
+ FILE *file = fopen(filename, "rb");
+ fread(script_data, script_len, 1, file);
+ // supposedly not necessary, but let's be safe.
+ script_data[script_len] = '\0';
+ fclose(file);
+ LOGI("Running script:\n");
+ LOGI("\n%s\n", script_data);
+
+ int ret = run_script_from_buffer(script_data, script_len, filename);
+ free(script_data);
+ return ret;
+}