From 302532979948a33c1dfbdd7925ff35ce82db21a2 Mon Sep 17 00:00:00 2001 From: d0k3 Date: Wed, 26 Jul 2017 14:08:29 +0200 Subject: [PATCH] Scripting: add CURRDIR environmental variable --- HelloScript.gm9 | 5 +++-- source/filesys/fsscript.c | 11 +++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/HelloScript.gm9 b/HelloScript.gm9 index d5f5ac3..9175ce7 100644 --- a/HelloScript.gm9 +++ b/HelloScript.gm9 @@ -26,11 +26,12 @@ ask -o -s "Really continue running this script?\n(I will completely ignore your # ENVIRONMENTAL VARS # SERIAL is the serial number of your device -# GM9OUT is the standard ouptput path +# GM9OUT is the standard output path +# CURRDIR is the directory the script is running from # SYSID0 is the id0 belonging to your SysNAND # EMUID0 is the id0 belonging to your EmuNAND (if available) # Use $[VAR] to get the *content* of a variable VAR -echo "Your serial number is $[SERIAL]\nYour std output path is $[GM9OUT]\n \nYour sys / emu ID0 is:\n$[SYSID0]\n$[EMUID0]" +echo "Your serial number is $[SERIAL]\nYour std output path is $[GM9OUT]\nCurrent dir is $[CURRDIR]\n \nYour sys / emu ID0 is:\n$[SYSID0]\n$[EMUID0]" # ERRORMSG and SUCCESSMSG / 'set' COMMAND # These two are special environment vars, allowing you to control the message on script failure or success diff --git a/source/filesys/fsscript.c b/source/filesys/fsscript.c index 7abbcc2..1a43683 100644 --- a/source/filesys/fsscript.c +++ b/source/filesys/fsscript.c @@ -134,7 +134,7 @@ char* set_var(const char* name, const char* content) { return vars[n_var].content; } -bool init_vars(void) { +bool init_vars(const char* path_script) { // reset var buffer memset(VAR_BUFFER, 0x00, VAR_BUFFER_SIZE); @@ -144,6 +144,12 @@ bool init_vars(void) { (FileGetData("1:/rw/sys/SecureInfo_B", (u8*) env_serial, 0xF, 0x102) != 0xF)) snprintf(env_serial, 0xF, "UNKNOWN"); + // current path + char curr_dir[_VAR_CNT_LEN]; + strncpy(curr_dir, path_script, _VAR_CNT_LEN); + char* slash = strrchr(curr_dir, '/'); + if (slash) *slash = '\0'; + // device sysnand / emunand id0 char env_sys_id0[32+1]; char env_emu_id0[32+1]; @@ -163,6 +169,7 @@ bool init_vars(void) { set_var("NULL", ""); // this one is special and should not be changed later set_var("SERIAL", env_serial); set_var("GM9OUT", OUTPUT_PATH); + set_var("CURRDIR", curr_dir); set_var("SYSID0", env_sys_id0); set_var("EMUID0", env_emu_id0); @@ -479,7 +486,7 @@ bool ExecuteGM9Script(const char* path_script) { *end = '\0'; // initialise variables - init_vars(); + init_vars(path_script); for (u32 line = 1; ptr < end; line++) { u32 flags = 0;