2017-09-04 16:18:07 +02:00
# include "scripting.h"
2017-11-03 16:11:29 +01:00
# include "fs.h"
# include "utils.h"
2017-09-04 02:15:45 +02:00
# include "nand.h"
2021-10-23 14:38:01 +02:00
# include "gamecart.h"
2017-08-21 21:33:22 +02:00
# include "bootfirm.h"
2017-09-19 15:57:29 +02:00
# include "qrcodegen.h"
2018-04-18 02:14:59 +02:00
# include "game.h"
2017-06-09 01:45:00 +02:00
# include "power.h"
2017-09-21 01:21:02 +02:00
# include "unittype.h"
2017-11-03 16:11:29 +01:00
# include "region.h"
2017-08-04 18:21:56 +02:00
# include "rtc.h"
2017-06-23 02:13:22 +02:00
# include "sha.h"
2017-09-04 02:15:45 +02:00
# include "hid.h"
2017-06-09 01:45:00 +02:00
# include "ui.h"
2019-06-03 01:37:10 +02:00
# include "swkbd.h"
2018-03-29 22:12:53 -03:00
# include "png.h"
2018-04-11 11:30:43 -05:00
# include "ips.h"
# include "bps.h"
2019-06-30 23:47:17 +02:00
# include "pxi.h"
2026-03-20 15:29:18 +01:00
# include "gyro.h"
2024-06-27 23:17:31 -04:00
# include "utf.h"
2017-06-09 01:45:00 +02:00
2017-10-29 14:00:02 +09:00
# define _MAX_ARGS 4
2017-09-21 01:21:02 +02:00
# define _ARG_MAX_LEN 512
2017-06-09 01:45:00 +02:00
# define _VAR_CNT_LEN 256
# define _VAR_NAME_LEN 32
2018-02-06 02:10:25 +01:00
# define _VAR_MAX_BUFF 256
2021-08-01 23:23:01 -05:00
# define _ERR_STR_LEN 256
2017-06-09 01:45:00 +02:00
2017-12-20 02:08:40 +01:00
# define _CHOICE_STR_LEN 32
# define _CHOICE_MAX_N 12
2017-12-31 03:26:50 +01:00
# define _CMD_NOT "not"
2017-10-29 14:00:02 +09:00
# define _CMD_IF "if"
2017-12-19 02:58:58 +01:00
# define _CMD_ELIF "elif"
2017-10-29 14:00:02 +09:00
# define _CMD_ELSE "else"
# define _CMD_END "end"
# define _CMD_FOR "for"
# define _CMD_NEXT "next"
2017-12-19 02:58:58 +01:00
2017-10-29 14:00:02 +09:00
# define _ARG_TRUE "TRUE"
# define _ARG_FALSE "FALSE"
2018-01-12 03:19:04 +01:00
# define _VAR_FORPATH "FORPATH"
2017-10-29 14:00:02 +09:00
2017-12-19 02:58:58 +01:00
# define _SKIP_BLOCK 1
# define _SKIP_TILL_END 2
2018-01-12 03:19:04 +01:00
# define _SKIP_TO_NEXT 3
# define _SKIP_TO_FOR 4
2017-12-19 02:58:58 +01:00
2018-01-18 02:23:46 +01:00
# define _MAX_FOR_DEPTH 16
2017-09-06 00:46:01 +02:00
// macros for textviewer
# define TV_VPAD 1 // vertical padding per line (above / below)
# define TV_HPAD 0 // horizontal padding per line (left)
# define TV_LNOS 4 // # of digits in line numbers (0 to disable)
# define TV_NLIN_DISP (SCREEN_HEIGHT / (FONT_HEIGHT_EXT + (2*TV_VPAD)))
# define TV_LLEN_DISP (((SCREEN_WIDTH_TOP - (2*TV_HPAD)) / FONT_WIDTH_EXT) - (TV_LNOS + 1))
2024-06-27 23:17:31 -04:00
# define MAX_CHAR_SIZE 4 // Max number of bytes needed for a UTF-8 character.
2017-09-06 00:46:01 +02:00
2017-06-09 01:45:00 +02:00
// some useful macros
# define IS_WHITESPACE(c) ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n'))
2017-10-29 14:00:02 +09:00
# define MATCH_STR(s,l,c) ((l == strlen(c)) && (strncmp(s, c, l) == 0))
2021-09-30 13:12:54 -04:00
# define _FLG(c) ((c >= 'a') ? (1 << (c - 'a')) : ((c >= '0') ? (1 << (26 + c - '0')) : 0))
2017-06-09 01:45:00 +02:00
2018-01-10 00:49:28 +01:00
# define IS_CTRLFLOW_CMD(id) ((id == CMD_ID_IF) || (id == CMD_ID_ELIF) || (id == CMD_ID_ELSE) || (id == CMD_ID_END) || \
2018-07-23 01:02:39 +02:00
( id = = CMD_ID_GOTO ) | | ( id = = CMD_ID_LABELSEL ) | | \
2018-01-10 00:49:28 +01:00
( id = = CMD_ID_FOR ) | | ( id = = CMD_ID_NEXT ) )
2017-12-20 02:08:40 +01:00
2018-04-18 02:14:59 +02:00
// command ids (also entry into the cmd_list array below)
2017-06-09 01:45:00 +02:00
typedef enum {
CMD_ID_NONE = 0 ,
2017-12-31 03:26:50 +01:00
CMD_ID_NOT ,
2017-10-29 14:00:02 +09:00
CMD_ID_IF ,
2017-12-19 02:58:58 +01:00
CMD_ID_ELIF ,
2017-10-29 14:00:02 +09:00
CMD_ID_ELSE ,
CMD_ID_END ,
2018-01-10 00:49:28 +01:00
CMD_ID_FOR ,
CMD_ID_NEXT ,
2017-10-29 14:00:02 +09:00
CMD_ID_GOTO ,
2017-12-20 02:08:40 +01:00
CMD_ID_LABELSEL ,
2018-04-25 23:32:22 +02:00
CMD_ID_KEYCHK ,
2017-06-09 01:45:00 +02:00
CMD_ID_ECHO ,
2017-09-19 15:57:29 +02:00
CMD_ID_QR ,
2017-06-09 01:45:00 +02:00
CMD_ID_ASK ,
2017-07-26 14:14:12 +02:00
CMD_ID_INPUT ,
2017-09-13 02:45:00 +02:00
CMD_ID_FILESEL ,
2018-01-09 01:39:37 +01:00
CMD_ID_DIRSEL ,
2017-06-09 01:45:00 +02:00
CMD_ID_SET ,
2017-12-20 12:33:18 +09:00
CMD_ID_STRSPLIT ,
2017-12-31 02:09:43 +01:00
CMD_ID_STRREP ,
2017-09-21 01:21:02 +02:00
CMD_ID_CHK ,
2017-06-09 01:45:00 +02:00
CMD_ID_ALLOW ,
CMD_ID_CP ,
CMD_ID_MV ,
CMD_ID_INJECT ,
2018-01-02 02:26:49 +01:00
CMD_ID_FILL ,
CMD_ID_FDUMMY ,
2017-06-09 01:45:00 +02:00
CMD_ID_RM ,
CMD_ID_MKDIR ,
CMD_ID_MOUNT ,
CMD_ID_UMOUNT ,
CMD_ID_FIND ,
CMD_ID_FINDNOT ,
2018-04-11 01:28:34 +02:00
CMD_ID_FGET ,
CMD_ID_FSET ,
2017-06-09 01:45:00 +02:00
CMD_ID_SHA ,
2017-09-04 02:15:45 +02:00
CMD_ID_SHAGET ,
2019-03-14 01:50:27 +01:00
CMD_ID_DUMPTXT ,
2017-07-27 14:29:03 +02:00
CMD_ID_FIXCMAC ,
2017-06-09 01:45:00 +02:00
CMD_ID_VERIFY ,
2017-07-27 14:29:03 +02:00
CMD_ID_DECRYPT ,
CMD_ID_ENCRYPT ,
CMD_ID_BUILDCIA ,
2020-07-31 12:14:13 +02:00
CMD_ID_INSTALL ,
2017-09-08 15:27:10 +02:00
CMD_ID_EXTRCODE ,
2018-04-22 12:28:37 -05:00
CMD_ID_CMPRCODE ,
2018-04-18 02:14:59 +02:00
CMD_ID_SDUMP ,
2018-04-11 11:30:43 -05:00
CMD_ID_APPLYIPS ,
CMD_ID_APPLYBPS ,
CMD_ID_APPLYBPM ,
2019-01-16 01:26:40 +01:00
CMD_ID_TEXTVIEW ,
2021-10-23 14:38:01 +02:00
CMD_ID_CARTDUMP ,
2018-01-12 02:52:39 +01:00
CMD_ID_ISDIR ,
CMD_ID_EXIST ,
2017-08-21 19:56:30 +02:00
CMD_ID_BOOT ,
2017-09-04 02:15:45 +02:00
CMD_ID_SWITCHSD ,
2018-06-14 00:24:07 +02:00
CMD_ID_NEXTEMU ,
2017-06-09 01:45:00 +02:00
CMD_ID_REBOOT ,
2017-09-19 15:57:29 +02:00
CMD_ID_POWEROFF ,
Lua support (#878)
* Test implementation of lua
* Trust that lua knows what its doing with this
Silence warnings
* actually update top screen when Thingy is called, disable unnecessary ShowPrompt calls
* readme
* change init for a simple test, print error on top screen too
* Readme
* change init for a simple test, print error on top screen too
* enable more lua libs, edit init.lua with string examples
* one more readme edit before bed
* Readme
* change init for a simple test, print error on top screen too
* enable more lua libs, edit init.lua with string examples
* make lua a proper file type, add test UI library with two functions, remove luacmd command
* remove old attempts at editing lauxlib and liolib
* README
* README
* FS lib, new UI stuff
* consistency with "type* ptr" maybe
* add custom package searcher, reset package.path
* new functions for UI including basic print output buffer, add "Lua scripts..." option to home/power menu
* build vram0.tar including subdirs of data
* move default path to GM9LUA_DEFAULT_PATH
* FS_FileGetData
* testfgd, add GM9VERSION global, update README, fix indentation
* FS_FileGetData will return a nil instead if it fails
* it's actually luaL_pushfail
* os
* use luaL_tolstring instead of lua_tostring
* fix test/remove debugging showprompt
* os.clock float attempt
* fix print for real
* fix swapped offset and size for FileGetData
* finish OS stuff
* fix os.clock
* shorten table in/out
* remove .vscode dir
* enum test
* support building without lua
* NO_LUA hides menu options (except when you directly select a lua file)
* update UI lib to better match the ideas on #1
* dockermake
* add DrawPNG function
* whoops its ShowPNG
* minor fixes, add DrawPNG
* fix AskPrompt, add all showprompts mentioned in #1
* add newly added functions to readme
* try to keep separate code and data
* update lua to 5.4.7
* remove test libraries now that i want to attempt to implement in a real api
* add nix flake for building
* add various lua functions, some of them taken from the old attempt but with new names
* add dev shell to flake.nix
* remove test lua scripts in root
* add test lua scripts in data/luascripts
* add a whole bunch more lua functions and stuff
* add more test lua scripts
* add more functions, add preload script, add test io compatibility module
* add more functions and test scripts
* more functions and stuff
* more functions and stuff
* more functions and stuff, plus a wip ctrcheck reimpl
* yet more functions and stuff
* even more functions and stuff
* command comparison table.ods
* update command comparison table.ods
* update command comparison table.ods
* Add files via upload
* update command comparison table.ods
* update ui.show_text to use DrawStringCenter, update ctrcheck rewrite
* Split up the ARM9 code (.text, .vectors) and data (.rodata, .data, .bss) sections into their own ELFs.
This allows us to use more ARM9 WRAM while leaving the 128k BootROM mirror intact.
* use the makefile definition
* add title module, move around some functions, update command comparison table
* add readme for lua
* remove liolib.c and loslib.c
* more functions and things, use CheckWritePermissionsLuaError in place of more manual checks, update command comparison table
* add missing constant
* set CURRDIR to nil instead of "(null)" if not found
* remove gm9enum (unused since the restart)
* add ui.check_key
* split fs module to lua overlay and _fs internal module, and add a check for fs.write_file in lua
* add fs.ask_select_file and fs.ask_select_dir
* add fs.key_dump, replace overwrite_all and append_all with overwrite and append
* add fs.cart_dump and sys.emu_base
* add ctrtool, update flake.lock
* add io append mode
* make sure io.open with write mode starts with an empty file, add os.remove and os.rename aliases
* properly implement os.remove compatibility
* add fs.verify_with_sha_file, fix PathIsDirectory by using stat instead of opendir
* add util.running_as_module (untested)
* move scripts over to https://github.com/ihaveamac/GM9-lua-script-experiments
* remove ods and dockermake.sh
* remove data/scripts
* add lua autorun (untested)
* fix syntax error
* add sys.check_embedded_backup
* remove accidental symlink
* add sys.check_raw_rtc
* fix ui.show_file_text_viewer not freeing memory or reporting an error if OOM happens
* add todo notes for ui
* work-in-progress lua doc
* formatting fix
* up heading level for all sections
* Revert "up heading level for all sections"
This reverts commit 6ef14b619536b4253e341ba40b4dea728358979d.
* separators
* fix name and error for fs.move
* do explicit permission checks in fs.move
* fix error string for fs.copy
* fix function name for fs.dir_info
* fix error string for fs.find and fs.find_not
* fix function name for fs.img_umount
* partial fs doc
* finished fs doc, string fixes for fs module
* document fs.cart_dump encrypted opt, remove stat from fs.verify_with_sha_file
* title doc
* sys doc, error string updates
* util doc
* add json.lua to lua-doc
* add fs.find_all
* add 3dstool to flake
* make fs.find_all recursive actually recursive, document fs.find_all
* add "for" to comparison table
* change fs.find to return nil if no path was found, instead of raising an error
* change ui.echo to automatically word wrap (untested)
* Revert "change ui.echo to automatically word wrap (untested)"
This reverts commit 2524e7707708e9818162c31f9f004b6301a3061b.
* switch devkitNix to upstream
* flake.lock: Update
Flake lock file updates:
• Updated input 'devkitNix':
'github:ihaveamac/devkitNix/883d173b94e3da8dc4cc0860cdda8c36b738817c' (2024-12-05)
→ 'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
• Updated input 'devkitNix/nixpkgs':
'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
→ 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'hax-nur':
'github:ihaveamac/nur-packages/c570b3830f7dd4d655afb109300529c896cd8855' (2024-12-05)
→ 'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
• Updated input 'hax-nur/nixpkgs':
'github:NixOS/nixpkgs/2c15aa59df0017ca140d9ba302412298ab4bf22a' (2024-12-02)
→ 'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
• Updated input 'nixpkgs':
'github:NixOS/nixpkgs/566e53c2ad750c84f6d31f9ccb9d00f823165550' (2024-12-03)
→ 'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)
* flake.lock: Update
Flake lock file updates:
• Updated input 'devkitNix':
'github:bandithedoge/devkitNix/95fd44f4ac7cecf24edf22daa899a516df73c6b7' (2025-01-11)
→ 'github:bandithedoge/devkitNix/a344b0200a044f2d2ff99685f13ff7c53106428e' (2025-02-06)
• Updated input 'devkitNix/nixpkgs':
'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
→ 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)
• Updated input 'hax-nur':
'github:ihaveamac/nur-packages/cd49afba206c2eb10a349d92470fdf2cc942ae23' (2025-01-11)
→ 'github:ihaveamac/nur-packages/2ce890cab4e948109ad1ad82ba18e69240a0d352' (2025-02-06)
• Updated input 'hax-nur/nixpkgs':
'github:NixOS/nixpkgs/4bc9c909d9ac828a039f288cf872d16d38185db8' (2025-01-08)
→ 'github:NixOS/nixpkgs/8532db2a88ba56de9188af72134d93e39fd825f3' (2025-02-02)
• Added input 'hax-nur/treefmt-nix':
'github:numtide/treefmt-nix/bebf27d00f7d10ba75332a0541ac43676985dea3' (2025-01-28)
• Added input 'hax-nur/treefmt-nix/nixpkgs':
follows 'hax-nur/nixpkgs'
• Updated input 'nixpkgs':
'github:NixOS/nixpkgs/32af3611f6f05655ca166a0b1f47b57c762b5192' (2025-01-09)
→ 'github:NixOS/nixpkgs/5b2753b0356d1c951d7a3ef1d086ba5a71fff43c' (2025-02-05)
* make devkitNix and hax-nur inputs follow nixpkgs
* Also dump section headers on .dis file
* prepare for upstream merge
* Restore original README.
* Remove flake, was only used for my own testing
* fix accidental removal of LIBS
* copy lua-doc.md into release archive
* README: update to mention Lua in place of GM9Script, add credits
* lua-doc: fix typo
* add sample HelloScript
* lua-doc: remove wip notice, since all gm9script features are replicated
* remove accidental inclusion of language.inl
* Fix mixture of tabs and spaces
* remove accidental nix leftover
* re-add @ for add2tar command
---------
Co-authored-by: luigoalma <luigoalma@hotmail.com>
Co-authored-by: Gruetzig <florianavilov@gmail.com>
Co-authored-by: Florian <88926852+Gruetzig@users.noreply.github.com>
Co-authored-by: Wolfvak <soherrera1@hotmail.com>
2025-03-21 02:25:04 -05:00
CMD_ID_BKPT ,
2017-06-09 01:45:00 +02:00
} cmd_id ;
typedef struct {
cmd_id id ;
char cmd [ 16 ] ;
u32 n_args ;
u32 allowed_flags ;
} Gm9ScriptCmd ;
typedef struct {
char name [ _VAR_NAME_LEN ] ; // variable name
char content [ _VAR_CNT_LEN ] ;
} Gm9ScriptVar ;
2020-07-23 13:46:42 -03:00
static const Gm9ScriptCmd cmd_list [ ] = {
2017-10-29 14:00:02 +09:00
{ CMD_ID_NONE , " # " , 0 , 0 } , // dummy entry
2017-12-31 03:26:50 +01:00
{ CMD_ID_NOT , _CMD_NOT , 0 , 0 } , // inverts the output of the following command
2017-10-29 14:00:02 +09:00
{ CMD_ID_IF , _CMD_IF , 1 , 0 } , // control flow commands at the top of the list
2017-12-19 02:58:58 +01:00
{ CMD_ID_ELIF , _CMD_ELIF , 1 , 0 } ,
2017-10-29 14:00:02 +09:00
{ CMD_ID_ELSE , _CMD_ELSE , 0 , 0 } ,
{ CMD_ID_END , _CMD_END , 0 , 0 } ,
2018-01-18 02:23:46 +01:00
{ CMD_ID_FOR , _CMD_FOR , 2 , _FLG ( ' r ' ) } ,
2018-01-10 00:49:28 +01:00
{ CMD_ID_NEXT , _CMD_NEXT , 0 , 0 } ,
2017-10-29 14:00:02 +09:00
{ CMD_ID_GOTO , " goto " , 1 , 0 } ,
2018-07-23 01:02:39 +02:00
{ CMD_ID_LABELSEL , " labelsel " , 2 , _FLG ( ' k ' ) } ,
2018-04-25 23:32:22 +02:00
{ CMD_ID_KEYCHK , " keychk " , 1 , 0 } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_ECHO , " echo " , 1 , 0 } ,
2017-09-19 15:57:29 +02:00
{ CMD_ID_QR , " qr " , 2 , 0 } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_ASK , " ask " , 1 , 0 } ,
2017-07-26 14:14:12 +02:00
{ CMD_ID_INPUT , " input " , 2 , 0 } ,
2018-09-08 17:31:54 +09:00
{ CMD_ID_FILESEL , " filesel " , 3 , _FLG ( ' d ' ) | _FLG ( ' x ' ) } ,
{ CMD_ID_DIRSEL , " dirsel " , 3 , _FLG ( ' x ' ) } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_SET , " set " , 2 , 0 } ,
2017-12-20 12:33:18 +09:00
{ CMD_ID_STRSPLIT , " strsplit " , 3 , _FLG ( ' b ' ) | _FLG ( ' f ' ) } ,
2017-12-31 02:09:43 +01:00
{ CMD_ID_STRREP , " strrep " , 3 , 0 } ,
2017-09-21 01:21:02 +02:00
{ CMD_ID_CHK , " chk " , 2 , _FLG ( ' u ' ) } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_ALLOW , " allow " , 1 , _FLG ( ' a ' ) } ,
2021-09-30 13:12:54 -04:00
{ CMD_ID_CP , " cp " , 2 , _FLG ( ' h ' ) | _FLG ( ' 1 ' ) | _FLG ( ' w ' ) | _FLG ( ' k ' ) | _FLG ( ' s ' ) | _FLG ( ' n ' ) | _FLG ( ' p ' ) } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_MV , " mv " , 2 , _FLG ( ' w ' ) | _FLG ( ' k ' ) | _FLG ( ' s ' ) | _FLG ( ' n ' ) } ,
{ CMD_ID_INJECT , " inject " , 2 , _FLG ( ' n ' ) } ,
2019-06-02 11:24:54 +02:00
{ CMD_ID_FILL , " fill " , 2 , _FLG ( ' n ' ) } ,
2018-01-02 02:26:49 +01:00
{ CMD_ID_FDUMMY , " fdummy " , 2 , 0 } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_RM , " rm " , 1 , 0 } ,
{ CMD_ID_MKDIR , " mkdir " , 1 , 0 } ,
2017-07-11 20:54:30 +02:00
{ CMD_ID_MOUNT , " imgmount " , 1 , 0 } ,
{ CMD_ID_UMOUNT , " imgumount " , 0 , 0 } ,
2017-09-06 02:01:42 +02:00
{ CMD_ID_FIND , " find " , 2 , _FLG ( ' f ' ) } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_FINDNOT , " findnot " , 2 , 0 } ,
2018-04-11 01:28:34 +02:00
{ CMD_ID_FGET , " fget " , 2 , _FLG ( ' e ' ) } ,
{ CMD_ID_FSET , " fset " , 2 , _FLG ( ' e ' ) } ,
2021-09-30 13:12:54 -04:00
{ CMD_ID_SHA , " sha " , 2 , _FLG ( ' 1 ' ) } ,
{ CMD_ID_SHAGET , " shaget " , 2 , _FLG ( ' 1 ' ) } ,
2019-03-14 01:50:27 +01:00
{ CMD_ID_DUMPTXT , " dumptxt " , 2 , _FLG ( ' p ' ) } ,
2017-08-21 19:56:30 +02:00
{ CMD_ID_FIXCMAC , " fixcmac " , 1 , 0 } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_VERIFY , " verify " , 1 , 0 } ,
2017-07-27 14:29:03 +02:00
{ CMD_ID_DECRYPT , " decrypt " , 1 , 0 } ,
{ CMD_ID_ENCRYPT , " encrypt " , 1 , 0 } ,
{ CMD_ID_BUILDCIA , " buildcia " , 1 , _FLG ( ' l ' ) } ,
2020-07-31 12:14:13 +02:00
{ CMD_ID_INSTALL , " install " , 1 , _FLG ( ' e ' ) } ,
2017-09-08 15:27:10 +02:00
{ CMD_ID_EXTRCODE , " extrcode " , 2 , 0 } ,
2018-04-22 12:28:37 -05:00
{ CMD_ID_CMPRCODE , " cmprcode " , 2 , 0 } ,
2020-07-31 12:14:13 +02:00
{ CMD_ID_SDUMP , " sdump " , 1 , _FLG ( ' w ' ) } ,
2018-04-11 11:30:43 -05:00
{ CMD_ID_APPLYIPS , " applyips " , 3 , 0 } ,
{ CMD_ID_APPLYBPS , " applybps " , 3 , 0 } ,
{ CMD_ID_APPLYBPM , " applybpm " , 3 , 0 } ,
2019-01-16 01:26:40 +01:00
{ CMD_ID_TEXTVIEW , " textview " , 1 , 0 } ,
2021-10-23 14:38:01 +02:00
{ CMD_ID_CARTDUMP , " cartdump " , 2 , _FLG ( ' e ' ) } ,
2018-04-25 15:25:07 +02:00
{ CMD_ID_ISDIR , " isdir " , 1 , 0 } ,
{ CMD_ID_EXIST , " exist " , 1 , 0 } ,
2017-08-21 21:33:22 +02:00
{ CMD_ID_BOOT , " boot " , 1 , 0 } ,
2017-09-04 02:15:45 +02:00
{ CMD_ID_SWITCHSD , " switchsd " , 1 , 0 } ,
2018-06-14 00:24:07 +02:00
{ CMD_ID_NEXTEMU , " nextemu " , 0 , 0 } ,
2017-06-09 01:45:00 +02:00
{ CMD_ID_REBOOT , " reboot " , 0 , 0 } ,
2017-09-19 15:57:29 +02:00
{ CMD_ID_POWEROFF , " poweroff " , 0 , 0 } ,
{ CMD_ID_BKPT , " bkpt " , 0 , 0 }
2017-09-16 17:08:26 +02:00
} ;
2024-06-27 23:17:31 -04:00
// off-screen string indicators
static const char al_str [ ] = " << " ;
static const char ar_str [ ] = " >> " ;
2017-09-16 17:08:26 +02:00
// global vars for preview
static u32 preview_mode = 0 ; // 0 -> off 1 -> quick 2 -> full
2017-09-25 00:50:44 +02:00
static u32 script_color_active = 0 ;
static u32 script_color_comment = 0 ;
static u32 script_color_code = 0 ;
2017-06-09 01:45:00 +02:00
2017-10-29 14:00:02 +09:00
// global vars for control flow
static bool syntax_error = false ; // if true, severe error, script has to stop
static char * jump_ptr = NULL ; // next position after a jump
2018-01-12 03:19:04 +01:00
static char * for_ptr = NULL ; // position of the active 'for' command
2017-12-19 02:58:58 +01:00
static u32 skip_state = 0 ; // zero, _SKIP_BLOCK, _SKIP_TILL_END
2017-10-29 14:00:02 +09:00
static u32 ifcnt = 0 ; // current # of 'if' nesting
2018-02-06 02:10:25 +01:00
// script / var buffers
static void * script_buffer = NULL ;
static void * var_buffer = NULL ;
2017-10-29 14:00:02 +09:00
2018-01-03 02:21:56 +01:00
static inline bool isntrboot ( void ) {
// taken over from Luma 3DS:
// https://github.com/AuroraWright/Luma3DS/blob/bb5518b0f68d89bcd8efaf326355a770d5e57856/source/main.c#L58-L62
const vu8 * bootMediaStatus = ( const vu8 * ) 0x1FFFE00C ;
const vu32 * bootPartitionsStatus = ( const vu32 * ) 0x1FFFE010 ;
// shell closed, no error booting NTRCARD, NAND partitions not even considered
return ( bootMediaStatus [ 3 ] = = 2 ) & & ! bootMediaStatus [ 1 ] & & ! bootPartitionsStatus [ 0 ] & & ! bootPartitionsStatus [ 1 ] ;
}
2018-04-11 01:28:34 +02:00
static inline u32 strntohex ( const char * str , u8 * hex , u32 len ) {
2017-06-09 01:45:00 +02:00
if ( ! len ) {
2020-08-24 21:27:19 -07:00
len = strlen ( str ) ;
2018-04-11 01:28:34 +02:00
if ( len % 1 ) return 0 ;
2017-06-09 01:45:00 +02:00
else len > > = 1 ;
} else if ( len * 2 ! = strnlen ( str , ( len * 2 ) + 1 ) ) {
2018-04-11 01:28:34 +02:00
return 0 ;
2017-06-09 01:45:00 +02:00
}
for ( u32 i = 0 ; i < len ; i + + ) {
char bytestr [ 2 + 1 ] = { 0 } ;
u32 bytehex ;
memcpy ( bytestr , str + ( i * 2 ) , 2 ) ;
if ( sscanf ( bytestr , " %02lx " , & bytehex ) ! = 1 )
2018-04-11 01:28:34 +02:00
return 0 ;
2017-06-09 01:45:00 +02:00
hex [ i ] = ( u8 ) bytehex ;
}
2018-04-11 01:28:34 +02:00
return len ;
}
static inline u32 hexntostr ( const u8 * hex , char * str , u32 len ) {
if ( ! len ) return 0 ;
for ( u32 i = 0 ; i < len ; i + + )
snprintf ( str + ( i < < 1 ) , 2 + 1 , " %02lx " , ( u32 ) hex [ i ] ) ;
return len ;
2017-06-09 01:45:00 +02:00
}
2024-07-08 23:16:30 -04:00
// we determine the line endings of a text file by simple majority;
// correcting mixed line endings is outside the scope of this program.
2024-06-27 23:17:31 -04:00
static inline bool is_crlf ( const char * str ) {
u32 crlf = 0 , lf = 0 ;
do if ( str [ 0 ] = = ' \n ' ) + + lf ; else if ( str [ 0 ] = = ' \r ' & & str [ 1 ] = = ' \n ' ) + + crlf , + + str ;
while ( * str + + ) ;
return crlf > lf ;
}
static inline bool is_newline ( const char * chr ) {
return chr [ 0 ] = = ' \n ' | | ( chr [ 0 ] = = ' \r ' & & chr [ 1 ] = = ' \n ' ) ;
}
2024-07-08 23:16:30 -04:00
// to ease calculations related to supporting mutlibyte UTF-8 characters
2024-06-27 23:17:31 -04:00
static inline u32 bytes_in_chars_u32 ( const char * str , u32 nchars ) {
u32 bytes = 0 ;
for ( u32 i = 0 ; str [ bytes ] & & i < nchars ; bytes + = GetCharSize ( str + bytes ) , + + i ) ;
return bytes ;
}
static inline int bytes_in_chars_int ( const char * str , int nchars ) {
int bytes = 0 ;
for ( int i = 0 ; str [ bytes ] & & i < nchars ; bytes + = GetCharSize ( str + bytes ) , + + i ) ;
return bytes ;
}
static inline int chars_in_bytes ( const char * str , int nbytes ) {
int chars = 0 ;
for ( int i = 0 ; str [ chars ] & & i < nbytes ; i + = GetCharSize ( str + i ) , + + chars ) ;
return chars ;
}
static inline u32 chars_between_pointers ( const char * start , const char * end ) {
u32 chars = 0 ;
for ( const char * i = start ; * i & & i < end ; IncChar ( & i ) , + + chars ) ;
return chars ;
}
static inline u32 line_len_chars ( const char * text , u32 len , u32 ww , const char * line , const char * * eol ) {
2017-09-06 00:46:01 +02:00
u32 llen = 0 ;
2024-06-27 23:17:31 -04:00
const char * lf = NULL ;
const char * spc = NULL ;
u32 spc_len = 0 ;
const char * lptr = line ;
2019-12-15 22:01:00 +01:00
2024-06-27 23:17:31 -04:00
if ( line > text + len )
2019-12-17 16:33:10 -03:00
return 0 ; // early exit
2019-12-15 22:01:00 +01:00
// search line feeds, spaces (only relevant for wordwrapped)
2024-06-27 23:17:31 -04:00
for ( ; ! ww | | ( llen < ww ) ; IncChar ( & lptr ) , + + llen ) {
if ( ww & & ( * lptr = = ' ' ) ) {
spc = lptr ;
spc_len = llen ;
}
if ( is_newline ( lptr ) | | lptr > = text + len ) {
lf = lptr ;
2019-12-15 22:01:00 +01:00
break ;
}
2017-09-06 00:46:01 +02:00
}
2019-12-15 22:01:00 +01:00
// line feed found, truncate trailing "empty" chars
// for wordwrapped, stop line after last space (if any)
2024-06-27 23:17:31 -04:00
if ( lf ) for ( ; lptr > line & & * GetPrevChar ( lptr ) < ' ' ; DecChar ( & lptr ) , - - llen ) ;
else if ( ww & & spc ) llen = spc_len + 1 ;
2019-12-15 22:01:00 +01:00
// signal eol if required
if ( eol ) * eol = lf ;
2017-09-06 00:46:01 +02:00
return llen ;
}
2024-06-27 23:17:31 -04:00
static inline const char * line_seek_chars ( const char * text , u32 len , u32 ww , const char * line , int add ) {
// safety check
if ( ( line < = text & & add < = 0 ) | | ( line > = text + len & & add > = 0 ) ) return line ;
2024-07-02 23:46:37 -04:00
2024-06-27 23:17:31 -04:00
const char * l0 = line ;
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
if ( ! ww ) { // non wordwrapped mode
2024-06-27 23:17:31 -04:00
for ( ; add < 0 & & l0 > text ; add + + )
for ( DecChar ( & l0 ) ; l0 > text & & l0 [ - 1 ] ! = ' \n ' ; DecChar ( & l0 ) ) ;
2020-08-24 21:27:19 -07:00
2024-06-27 23:17:31 -04:00
for ( ; add > 0 & & l0 < text + len ; add - - )
for ( IncChar ( & l0 ) ; l0 < text + len & & l0 [ - 1 ] ! = ' \n ' ; IncChar ( & l0 ) ) ;
2020-08-24 21:27:19 -07:00
2024-06-27 23:17:31 -04:00
return l0 ;
2017-09-06 00:46:01 +02:00
} else { // wordwrapped mode
// handle forwards wordwrapped search
2024-06-27 23:17:31 -04:00
for ( ; add > 0 & & l0 < text + len ; add - - ) {
const char * eol = NULL ;
u32 llenww_chars = line_len_chars ( text , len , ww , l0 , & eol ) ;
if ( eol | | ! llenww_chars ) l0 = line_seek_chars ( text , len , 0 , l0 , 1 ) ;
else l0 + = bytes_in_chars_u32 ( l0 , llenww_chars ) ;
2017-09-06 00:46:01 +02:00
}
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// handle backwards wordwrapped search
2024-06-27 23:17:31 -04:00
while ( add < 0 & & l0 > text ) {
const char * l1 = line_seek_chars ( text , len , 0 , l0 , - 1 ) ;
if ( l0 > text + len ) {
l0 = text + len ;
if ( l1 = = l0 ) + + add ;
}
const char * l0_minus1 = l1 ;
2019-12-15 22:01:00 +01:00
int nlww = 0 ; // no of wordwrapped lines in paragraph
2024-06-27 23:17:31 -04:00
for ( const char * ld = l1 ; ld < l0 ; ld = line_seek_chars ( text , len , ww , ld , 1 ) , nlww + + )
2019-12-15 22:01:00 +01:00
l0_minus1 = ld ;
2024-06-27 23:17:31 -04:00
if ( line > text + len & & l0 = = text + len & & chars_in_bytes ( l0_minus1 , l0 - l0_minus1 ) = = ( int ) ww ) + + add ;
2019-12-15 22:01:00 +01:00
if ( add + nlww < 0 ) { // haven't reached the desired line yet
2017-09-06 00:46:01 +02:00
add + = nlww ;
l0 = l1 ;
2019-12-15 22:01:00 +01:00
} else { // reached the desired line
2024-06-27 23:17:31 -04:00
l0 = ( add = = - 1 ) ? l0_minus1 : line_seek_chars ( text , len , ww , l1 , nlww + add ) ;
2017-09-06 00:46:01 +02:00
add = 0 ;
}
}
2019-12-15 22:01:00 +01:00
2017-09-06 00:46:01 +02:00
return l0 ;
}
}
2024-06-27 23:17:31 -04:00
static inline const char * line_start ( const char * text , u32 len , u32 ww , const char * ptr ) {
return line_seek_chars ( text , len , ww , GetNextChar ( ptr ) , - 1 ) ;
}
2017-10-29 14:00:02 +09:00
static inline u32 get_lno ( const char * text , u32 len , const char * line ) {
u32 lno = 1 ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
for ( u32 i = 0 ; i < len ; i + + ) {
if ( line < = text + i ) return lno ;
else if ( text [ i ] = = ' \n ' ) lno + + ;
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
return 0 ;
}
2017-09-16 17:08:26 +02:00
void set_preview ( const char * name , const char * content ) {
if ( strncmp ( name , " PREVIEW_MODE " , _VAR_NAME_LEN ) = = 0 ) {
2017-12-20 00:13:31 +01:00
if ( strncasecmp ( content , " quick " , _VAR_CNT_LEN ) = = 0 ) preview_mode = 1 ;
2017-09-16 17:08:26 +02:00
else if ( strncasecmp ( content , " full " , _VAR_CNT_LEN ) = = 0 ) preview_mode = 2 ;
2017-12-20 00:13:31 +01:00
else preview_mode = 0xFF ; // unknown preview mode
2017-09-16 17:08:26 +02:00
} else if ( strncmp ( name , " PREVIEW_COLOR_ACTIVE " , _VAR_NAME_LEN ) = = 0 ) {
2019-07-23 15:36:37 -03:00
u8 rgb [ 4 ] ;
2017-09-16 17:08:26 +02:00
if ( strntohex ( content , rgb , 3 ) )
2019-07-23 15:36:37 -03:00
script_color_active = rgb888_buf_to_rgb565 ( rgb ) ;
2017-09-16 17:08:26 +02:00
} else if ( strncmp ( name , " PREVIEW_COLOR_COMMENT " , _VAR_NAME_LEN ) = = 0 ) {
2019-07-23 15:36:37 -03:00
u8 rgb [ 4 ] ;
2017-09-16 17:08:26 +02:00
if ( strntohex ( content , rgb , 3 ) )
2019-07-23 15:36:37 -03:00
script_color_comment = rgb888_buf_to_rgb565 ( rgb ) ;
2017-09-16 17:08:26 +02:00
} else if ( strncmp ( name , " PREVIEW_COLOR_CODE " , _VAR_NAME_LEN ) = = 0 ) {
2019-07-23 15:36:37 -03:00
u8 rgb [ 4 ] ;
2017-09-16 17:08:26 +02:00
if ( strntohex ( content , rgb , 3 ) )
2019-07-23 15:36:37 -03:00
script_color_code = rgb888_buf_to_rgb565 ( rgb ) ;
2017-09-16 17:08:26 +02:00
}
}
2017-08-24 15:46:36 +02:00
char * set_var ( const char * name , const char * content ) {
2018-02-06 02:10:25 +01:00
Gm9ScriptVar * vars = ( Gm9ScriptVar * ) var_buffer ;
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
if ( ( strnlen ( name , _VAR_NAME_LEN ) > ( _VAR_NAME_LEN - 1 ) ) | | ( strnlen ( content , _VAR_CNT_LEN ) > ( _VAR_CNT_LEN - 1 ) ) | |
( strchr ( name , ' [ ' ) | | strchr ( name , ' ] ' ) ) )
return NULL ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
u32 n_var = 0 ;
2018-02-06 02:10:25 +01:00
for ( Gm9ScriptVar * var = vars ; n_var < _VAR_MAX_BUFF ; n_var + + , var + + )
2017-08-24 15:46:36 +02:00
if ( ! * ( var - > name ) | | ( strncmp ( var - > name , name , _VAR_NAME_LEN ) = = 0 ) ) break ;
2018-02-06 02:10:25 +01:00
if ( n_var > = _VAR_MAX_BUFF ) return NULL ;
2017-08-24 15:46:36 +02:00
strncpy ( vars [ n_var ] . name , name , _VAR_NAME_LEN ) ;
2018-05-24 00:14:37 +02:00
vars [ n_var ] . name [ _VAR_NAME_LEN - 1 ] = ' \0 ' ;
2017-08-24 15:46:36 +02:00
strncpy ( vars [ n_var ] . content , content , _VAR_CNT_LEN ) ;
2018-05-24 00:14:37 +02:00
vars [ n_var ] . content [ _VAR_CNT_LEN - 1 ] = ' \0 ' ;
2017-08-24 15:46:36 +02:00
if ( ! n_var ) * ( vars [ n_var ] . content ) = ' \0 ' ; // NULL var
2020-08-24 21:27:19 -07:00
2017-09-16 17:08:26 +02:00
// update preview stuff
set_preview ( name , content ) ;
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
return vars [ n_var ] . content ;
}
void upd_var ( const char * name ) {
2017-10-25 15:17:09 +01:00
// device serial / region
if ( ! name | | ( strncmp ( name , " SERIAL " , _VAR_NAME_LEN ) = = 0 ) | |
( strncmp ( name , " REGION " , _VAR_NAME_LEN ) = = 0 ) ) {
u8 secinfo_data [ 1 + 1 + 16 ] = { 0 } ;
char * env_serial = ( char * ) secinfo_data + 2 ;
2018-05-24 00:14:37 +02:00
char env_region [ 3 + 1 ] = { 0 } ;
2020-08-24 21:27:19 -07:00
2017-10-25 15:17:09 +01:00
snprintf ( env_region , 0x4 , " UNK " ) ;
if ( ( FileGetData ( " 1:/rw/sys/SecureInfo_A " , secinfo_data , 0x11 , 0x100 ) ! = 0x11 ) & &
( FileGetData ( " 1:/rw/sys/SecureInfo_B " , secinfo_data , 0x11 , 0x100 ) ! = 0x11 ) )
2017-08-24 15:46:36 +02:00
snprintf ( env_serial , 0xF , " UNKNOWN " ) ;
2017-10-25 15:17:09 +01:00
else if ( * secinfo_data < SMDH_NUM_REGIONS )
2018-05-24 00:14:37 +02:00
strncpy ( env_region , g_regionNamesShort [ * secinfo_data ] , countof ( env_region ) - 1 ) ;
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
set_var ( " SERIAL " , env_serial ) ;
2017-10-25 15:17:09 +01:00
set_var ( " REGION " , env_region ) ;
2017-06-09 01:45:00 +02:00
}
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
// device sysnand / emunand id0
for ( u32 emu = 0 ; emu < = 1 ; emu + + ) {
const char * env_id0_name = ( emu ) ? " EMUID0 " : " SYSID0 " ;
if ( ! name | | ( strncmp ( name , env_id0_name , _VAR_NAME_LEN ) = = 0 ) ) {
const char * path = emu ? " 4:/private/movable.sed " : " 1:/private/movable.sed " ;
char env_id0 [ 32 + 1 ] ;
2019-05-08 00:18:34 +02:00
u8 sd_keyy [ 0x10 ] __attribute__ ( ( aligned ( 4 ) ) ) ;
2017-08-24 15:46:36 +02:00
if ( FileGetData ( path , sd_keyy , 0x10 , 0x110 ) = = 0x10 ) {
u32 sha256sum [ 8 ] ;
sha_quick ( sha256sum , sd_keyy , 0x10 , SHA256_MODE ) ;
2023-03-20 23:13:51 -05:00
snprintf ( env_id0 , sizeof ( env_id0 ) , " %08lx%08lx%08lx%08lx " ,
2017-08-24 15:46:36 +02:00
sha256sum [ 0 ] , sha256sum [ 1 ] , sha256sum [ 2 ] , sha256sum [ 3 ] ) ;
} else snprintf ( env_id0 , 0xF , " UNKNOWN " ) ;
set_var ( env_id0_name , env_id0 ) ;
}
}
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
// datestamp & timestamp
if ( ! name | | ( strncmp ( name , " DATESTAMP " , _VAR_NAME_LEN ) = = 0 ) | | ( strncmp ( name , " TIMESTAMP " , _VAR_NAME_LEN ) = = 0 ) ) {
2017-08-04 18:21:56 +02:00
DsTime dstime ;
get_dstime ( & dstime ) ;
2017-08-24 15:46:36 +02:00
char env_date [ 16 + 1 ] ;
char env_time [ 16 + 1 ] ;
2023-03-20 23:13:51 -05:00
snprintf ( env_date , sizeof ( env_date ) , " %02lX%02lX%02lX " , ( u32 ) dstime . bcd_Y , ( u32 ) dstime . bcd_M , ( u32 ) dstime . bcd_D ) ;
snprintf ( env_time , sizeof ( env_time ) , " %02lX%02lX%02lX " , ( u32 ) dstime . bcd_h , ( u32 ) dstime . bcd_m , ( u32 ) dstime . bcd_s ) ;
2017-08-24 15:46:36 +02:00
if ( ! name | | ( strncmp ( name , " DATESTAMP " , _VAR_NAME_LEN ) = = 0 ) ) set_var ( " DATESTAMP " , env_date ) ;
if ( ! name | | ( strncmp ( name , " TIMESTAMP " , _VAR_NAME_LEN ) = = 0 ) ) set_var ( " TIMESTAMP " , env_time ) ;
2017-08-04 18:21:56 +02:00
}
2018-06-14 00:24:07 +02:00
// emunand base sector
if ( ! name | | ( strncmp ( name , " EMUBASE " , _VAR_NAME_LEN ) = = 0 ) ) {
u32 emu_base = GetEmuNandBase ( ) ;
char emu_base_str [ 8 + 1 ] ;
2023-03-20 23:13:51 -05:00
snprintf ( emu_base_str , sizeof ( emu_base_str ) , " %08lX " , emu_base ) ;
2018-06-14 00:24:07 +02:00
set_var ( " EMUBASE " , emu_base_str ) ;
}
2021-03-31 17:33:05 +02:00
// SD card storage
if ( ! name | | ( strncmp ( name , " SDSIZE " , _VAR_NAME_LEN ) = = 0 ) ) {
u64 sdsize = GetTotalSpace ( " 0: " ) ;
char sdsize_str [ 32 + 1 ] ;
2026-03-18 10:39:29 -05:00
FormatBytes ( sdsize_str , sdsize , false ) ;
2021-03-31 17:33:05 +02:00
set_var ( " SDSIZE " , sdsize_str ) ;
}
// SD card free storage
if ( ! name | | ( strncmp ( name , " SDFREE " , _VAR_NAME_LEN ) = = 0 ) ) {
u64 sdfree = GetFreeSpace ( " 0: " ) ;
char sdfree_str [ 32 + 1 ] ;
2026-03-18 10:39:29 -05:00
FormatBytes ( sdfree_str , sdfree , false ) ;
2021-03-31 17:33:05 +02:00
set_var ( " SDFREE " , sdfree_str ) ;
}
// NAND size
if ( ! name | | ( strncmp ( name , " NANDSIZE " , _VAR_NAME_LEN ) = = 0 ) ) {
u64 nandsize = GetNandSizeSectors ( NAND_SYSNAND ) * 0x200 ;
char nandsize_str [ 32 + 1 ] ;
2026-03-18 10:39:29 -05:00
FormatBytes ( nandsize_str , nandsize , false ) ;
2021-03-31 17:33:05 +02:00
set_var ( " NANDSIZE " , nandsize_str ) ;
}
2017-06-09 01:45:00 +02:00
}
2017-08-24 15:46:36 +02:00
char * get_var ( const char * name , char * * endptr ) {
2018-02-06 02:10:25 +01:00
Gm9ScriptVar * vars = ( Gm9ScriptVar * ) var_buffer ;
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
u32 name_len = 0 ;
char * pname = NULL ;
if ( ! endptr ) { // no endptr, varname is verbatim
pname = ( char * ) name ;
name_len = strnlen ( pname , _VAR_NAME_LEN ) ;
} else { // endptr given, varname is in [VAR] format
pname = ( char * ) name + 1 ;
if ( * name ! = ' [ ' ) return NULL ;
for ( name_len = 0 ; pname [ name_len ] ! = ' ] ' ; name_len + + )
if ( ( name_len > = _VAR_NAME_LEN ) | | ! pname [ name_len ] ) return NULL ;
* endptr = pname + name_len + 1 ;
}
2020-08-24 21:27:19 -07:00
2017-08-24 15:46:36 +02:00
char vname [ _VAR_NAME_LEN ] ;
strncpy ( vname , pname , name_len ) ;
2017-12-26 18:24:25 +01:00
vname [ name_len ] = ' \0 ' ;
2017-08-24 15:46:36 +02:00
upd_var ( vname ) ; // handle dynamic env vars
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
u32 n_var = 0 ;
2018-02-06 02:10:25 +01:00
for ( Gm9ScriptVar * var = vars ; n_var < _VAR_MAX_BUFF ; n_var + + , var + + ) {
2017-12-26 18:24:25 +01:00
if ( ! * ( var - > name ) | | ( strncmp ( var - > name , vname , _VAR_NAME_LEN ) = = 0 ) ) break ;
2017-08-24 15:46:36 +02:00
}
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
if ( n_var > = _VAR_MAX_BUFF | | ! * ( vars [ n_var ] . name ) ) n_var = 0 ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
return vars [ n_var ] . content ;
}
2017-07-26 14:08:29 +02:00
bool init_vars ( const char * path_script ) {
2017-06-09 01:45:00 +02:00
// reset var buffer
2018-02-06 02:10:25 +01:00
memset ( var_buffer , 0x00 , sizeof ( Gm9ScriptVar ) * _VAR_MAX_BUFF ) ;
2020-08-24 21:27:19 -07:00
2017-07-26 14:08:29 +02:00
// current path
char curr_dir [ _VAR_CNT_LEN ] ;
2017-09-21 01:21:02 +02:00
if ( path_script ) {
strncpy ( curr_dir , path_script , _VAR_CNT_LEN ) ;
2018-05-24 00:14:37 +02:00
curr_dir [ _VAR_CNT_LEN - 1 ] = ' \0 ' ;
2017-09-21 01:21:02 +02:00
char * slash = strrchr ( curr_dir , ' / ' ) ;
if ( slash ) * slash = ' \0 ' ;
2018-05-24 00:14:37 +02:00
} else strncpy ( curr_dir , " (null) " , _VAR_CNT_LEN - 1 ) ;
2020-08-24 21:27:19 -07:00
2017-06-23 02:13:22 +02:00
// set env vars
2020-08-24 21:27:19 -07:00
set_var ( " NULL " , " " ) ; // this one is special and should not be changed later
2017-08-24 15:46:36 +02:00
set_var ( " CURRDIR " , curr_dir ) ; // script path, never changes
set_var ( " GM9OUT " , OUTPUT_PATH ) ; // output path, never changes
2019-06-03 00:34:26 +02:00
set_var ( " HAX " , IS_UNLOCKED ? ( isntrboot ( ) ? " ntrboot " : " sighax " ) : " " ) ; // type of hax running from
2017-09-21 01:21:02 +02:00
set_var ( " ONTYPE " , IS_O3DS ? " O3DS " : " N3DS " ) ; // type of the console
set_var ( " RDTYPE " , IS_DEVKIT ? " devkit " : " retail " ) ; // devkit / retail
2026-03-20 15:29:18 +01:00
set_var ( " GYROMODEL " , GetGyroModelString ( ) ) ; // gyro model
2018-01-18 15:55:37 +01:00
char * ptr = set_var ( " GM9VER " , VERSION ) ; // GodMode9 version, truncated below
2018-01-18 15:13:24 +01:00
while ( * ( ptr + + ) ! = ' \0 ' ) if ( * ptr = = ' - ' ) * ptr = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
return true ;
}
bool expand_arg ( char * argex , const char * arg , u32 len ) {
char * out = argex ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
for ( char * in = ( char * ) arg ; in - arg < ( int ) len ; in + + ) {
u32 out_len = out - argex ;
2017-09-21 01:21:02 +02:00
if ( out_len > = ( _ARG_MAX_LEN - 1 ) ) return false ; // maximum arglen reached
2020-08-24 21:27:19 -07:00
2020-12-30 11:33:14 +01:00
if ( * in = = ' \\ ' ) { // escape line breaks & quotes
2017-06-09 01:45:00 +02:00
if ( * ( + + in ) = = ' n ' ) * ( out + + ) = ' \n ' ;
2020-12-30 11:33:14 +01:00
else if ( * in = = ' \" ' ) * ( out + + ) = ' \" ' ;
2017-06-09 01:45:00 +02:00
else {
* ( out + + ) = ' \\ ' ;
* ( out + + ) = * in ;
}
} else if ( * in = = ' $ ' ) { // replace vars
char * content = get_var ( in + 1 , & in ) ;
if ( content ) {
u32 clen = strnlen ( content , 256 ) ;
strncpy ( out , content , clen ) ;
out + = clen ;
in - - ; // go back one char
} else * ( out + + ) = * in ;
} else * ( out + + ) = * in ;
}
* out = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
return true ;
}
cmd_id get_cmd_id ( char * cmd , u32 len , u32 flags , u32 argc , char * err_str ) {
2020-07-23 13:46:42 -03:00
const Gm9ScriptCmd * cmd_entry = NULL ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
for ( u32 i = 0 ; i < ( sizeof ( cmd_list ) / sizeof ( Gm9ScriptCmd ) ) ; i + + ) {
if ( strncmp ( cmd_list [ i ] . cmd , cmd , len ) = = 0 ) {
cmd_entry = cmd_list + i ;
break ;
}
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
if ( ! cmd_entry ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_UNKNOWN_CMD ) ;
2017-06-09 01:45:00 +02:00
} else if ( cmd_entry - > n_args ! = argc ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_BAD_NUMBER_OF_ARGS ) ;
2017-06-09 01:45:00 +02:00
} else if ( ~ ( cmd_entry - > allowed_flags | _FLG ( ' o ' ) | _FLG ( ' s ' ) ) & flags ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_UNRECOGNIZED_FLAGS ) ;
2017-06-09 01:45:00 +02:00
} else return cmd_entry - > id ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
return CMD_ID_NONE ;
2017-06-09 01:45:00 +02:00
}
u32 get_flag ( char * str , u32 len , char * err_str ) {
char flag_char = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
if ( ( len < 2 ) | | ( * str ! = ' - ' ) ) flag_char = ' \0 ' ;
else if ( len = = 2 ) flag_char = str [ 1 ] ;
2021-09-30 13:12:54 -04:00
else if ( strncmp ( str , " --sha1 " , len ) = = 0 ) flag_char = ' 1 ' ;
2017-06-09 01:45:00 +02:00
else if ( strncmp ( str , " --all " , len ) = = 0 ) flag_char = ' a ' ;
2017-12-20 12:33:18 +09:00
else if ( strncmp ( str , " --before " , len ) = = 0 ) flag_char = ' b ' ;
2018-01-02 01:02:09 +01:00
else if ( strncmp ( str , " --include_dirs " , len ) = = 0 ) flag_char = ' d ' ;
2021-10-23 14:38:01 +02:00
else if ( strncmp ( str , " --encrypted " , len ) = = 0 ) flag_char = ' e ' ;
2018-04-11 01:28:34 +02:00
else if ( strncmp ( str , " --flip_endian " , len ) = = 0 ) flag_char = ' e ' ;
2020-07-31 12:14:13 +02:00
else if ( strncmp ( str , " --to_emunand " , len ) = = 0 ) flag_char = ' e ' ;
2017-09-06 02:01:42 +02:00
else if ( strncmp ( str , " --first " , len ) = = 0 ) flag_char = ' f ' ;
2017-06-09 01:45:00 +02:00
else if ( strncmp ( str , " --hash " , len ) = = 0 ) flag_char = ' h ' ;
2018-07-23 01:02:39 +02:00
else if ( strncmp ( str , " --keysel " , len ) = = 0 ) flag_char = ' k ' ;
2017-06-09 01:45:00 +02:00
else if ( strncmp ( str , " --skip " , len ) = = 0 ) flag_char = ' k ' ;
2017-07-27 14:29:03 +02:00
else if ( strncmp ( str , " --legit " , len ) = = 0 ) flag_char = ' l ' ;
2017-06-09 01:45:00 +02:00
else if ( strncmp ( str , " --no_cancel " , len ) = = 0 ) flag_char = ' n ' ;
else if ( strncmp ( str , " --optional " , len ) = = 0 ) flag_char = ' o ' ;
2018-04-22 12:28:37 -05:00
else if ( strncmp ( str , " --append " , len ) = = 0 ) flag_char = ' p ' ;
2018-01-18 02:23:46 +01:00
else if ( strncmp ( str , " --recursive " , len ) = = 0 ) flag_char = ' r ' ;
2017-06-09 01:45:00 +02:00
else if ( strncmp ( str , " --silent " , len ) = = 0 ) flag_char = ' s ' ;
2017-09-21 01:21:02 +02:00
else if ( strncmp ( str , " --unequal " , len ) = = 0 ) flag_char = ' u ' ;
2017-06-09 01:45:00 +02:00
else if ( strncmp ( str , " --overwrite " , len ) = = 0 ) flag_char = ' w ' ;
2018-09-08 17:31:54 +09:00
else if ( strncmp ( str , " --explorer " , len ) = = 0 ) flag_char = ' x ' ;
2020-08-24 21:27:19 -07:00
2021-09-30 13:12:54 -04:00
if ( ( ( flag_char < ' a ' ) | | ( flag_char > ' z ' ) ) & & ( ( flag_char < ' 0 ' ) | | ( flag_char > ' 5 ' ) ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ILLEGAL_FLAG ) ;
2017-06-09 01:45:00 +02:00
return 0 ;
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
return _FLG ( flag_char ) ;
}
char * get_string ( char * ptr , const char * line_end , u32 * len , char * * next , char * err_str ) {
char * str = NULL ;
* len = 0 ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// skip whitespaces
for ( ; IS_WHITESPACE ( * ptr ) & & ( ptr < line_end ) ; ptr + + ) ;
if ( ptr > = line_end ) return ( * next = ( char * ) line_end ) ; // end reached, all whitespaces
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// handle string
if ( * ptr = = ' \" ' ) { // quotes
str = + + ptr ;
2020-12-30 11:33:14 +01:00
for ( ; ( ( * ptr ! = ' \" ' ) | | ( * ( ptr - 1 ) = = ' \\ ' ) ) & & ( ptr < line_end ) ; ptr + + , ( * len ) + + ) ;
2017-06-09 01:45:00 +02:00
if ( ptr > = line_end ) { // failed if unresolved quotes
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_UNRESOLVED_QUOTES ) ;
2017-06-09 01:45:00 +02:00
return NULL ;
}
* next = ptr + 1 ;
} else { // no quotes, no whitespace
2020-08-24 21:27:19 -07:00
str = ptr ;
2017-06-09 01:45:00 +02:00
for ( ; ! IS_WHITESPACE ( * ptr ) & & ( ptr < line_end ) ; ptr + + , ( * len ) + + ) ;
* next = ptr ;
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
return str ;
}
2017-10-29 14:00:02 +09:00
char * skip_block ( char * ptr , bool ignore_else , bool stop_after_end ) {
while ( * ptr ) {
// store line start / line end
char * line_start = ptr ;
char * line_end = strchr ( ptr , ' \n ' ) ;
if ( ! line_end ) line_end = ptr + strlen ( ptr ) ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// grab first string
char * str = NULL ;
2022-03-22 18:13:18 -05:00
u32 STR_SCRIPTERR_len = 0 ;
if ( ! ( str = get_string ( ptr , line_end , & STR_SCRIPTERR_len , & ptr , NULL ) ) | | ( str > = line_end ) ) {
2017-10-29 14:00:02 +09:00
// string error or empty line
ptr = line_end + 1 ;
2020-08-24 21:27:19 -07:00
continue ;
2017-10-29 14:00:02 +09:00
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// check string
2022-03-22 18:13:18 -05:00
if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_END ) ) { // stop at end
2017-10-29 14:00:02 +09:00
return line_start ; // end of block found
2022-03-22 18:13:18 -05:00
} else if ( ! ignore_else & & MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_ELSE ) ) { // stop at else
2017-10-29 14:00:02 +09:00
return line_start ; // end of block found
2022-03-22 18:13:18 -05:00
} else if ( ! ignore_else & & MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_ELIF ) ) { // stop at elif
2017-12-19 02:58:58 +01:00
return line_start ; // end of block found
2022-03-22 18:13:18 -05:00
} else if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_IF ) ) {
2017-10-29 14:00:02 +09:00
ptr = line_start = skip_block ( line_end + 1 , true , false ) ;
if ( ptr = = NULL ) return NULL ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
line_end = strchr ( ptr , ' \n ' ) ;
if ( ! line_end ) line_end = ptr + strlen ( ptr ) ;
2020-08-24 21:27:19 -07:00
2022-03-22 18:13:18 -05:00
str = get_string ( ptr , line_end , & STR_SCRIPTERR_len , & ptr , NULL ) ;
if ( ! ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_END ) ) ) return NULL ;
2017-10-29 14:00:02 +09:00
if ( stop_after_end ) return line_end + 1 ;
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// move on to the next line
ptr = line_end + 1 ;
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// end of block not found
return NULL ;
}
2018-01-12 03:19:04 +01:00
char * find_next ( char * ptr ) {
while ( ptr & & * ptr ) {
// store line start / line end
char * line_start = ptr ;
char * line_end = strchr ( ptr , ' \n ' ) ;
if ( ! line_end ) line_end = ptr + strlen ( ptr ) ;
2020-08-24 21:27:19 -07:00
2018-01-12 03:19:04 +01:00
// grab first string
char * str = NULL ;
2022-03-22 18:13:18 -05:00
u32 STR_SCRIPTERR_len = 0 ;
if ( ! ( str = get_string ( ptr , line_end , & STR_SCRIPTERR_len , & ptr , NULL ) ) | | ( str > = line_end ) ) {
2018-01-12 03:19:04 +01:00
// string error or empty line
ptr = line_end + 1 ;
2020-08-24 21:27:19 -07:00
continue ;
2018-01-12 03:19:04 +01:00
}
2020-08-24 21:27:19 -07:00
2018-01-12 03:19:04 +01:00
// check string
2022-03-22 18:13:18 -05:00
if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_IF ) ) { // skip 'if' blocks
2018-01-12 03:19:04 +01:00
ptr = skip_block ( ptr , true , true ) ;
2022-03-22 18:13:18 -05:00
} else if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_END ) | | MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_FOR ) ) {
2018-01-12 03:19:04 +01:00
ptr = NULL ; // this should not happen here
2022-03-22 18:13:18 -05:00
} else if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_NEXT ) ) {
2018-01-12 03:19:04 +01:00
return line_start ;
}
2020-08-24 21:27:19 -07:00
2018-01-12 03:19:04 +01:00
// move on to the next line
ptr = line_end + 1 ;
}
2020-08-24 21:27:19 -07:00
2018-01-12 03:19:04 +01:00
// 'next' not found
return NULL ;
}
2017-10-29 14:00:02 +09:00
char * find_label ( const char * label , const char * last_found ) {
2018-02-06 02:10:25 +01:00
char * script = ( char * ) script_buffer ;
2017-10-29 14:00:02 +09:00
char * ptr = script ;
2018-02-07 23:07:23 +01:00
u32 label_len = strnlen ( label , _ARG_MAX_LEN ) ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
if ( last_found ) {
ptr = strchr ( last_found , ' \n ' ) ;
if ( ! ptr ) return NULL ;
ptr + + ;
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
char * next = ptr ;
for ( ; next & & * ptr ; ptr = next ) {
// store line start / get line end
char * line_start = ptr ;
char * line_end = strchr ( ptr , ' \n ' ) ;
if ( ! line_end ) line_end = ptr + strlen ( ptr ) ;
next = line_end + 1 ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// search for label
char * str = NULL ;
2022-03-22 18:13:18 -05:00
u32 STR_SCRIPTERR_len = 0 ;
if ( ! ( str = get_string ( ptr , line_end , & STR_SCRIPTERR_len , & ptr , NULL ) ) ) continue ; // string error, ignore line
2017-10-29 14:00:02 +09:00
else if ( str > = line_end ) continue ; // empty line
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
if ( * str = = ' @ ' ) {
// label found
2022-03-22 18:13:18 -05:00
str + + ; STR_SCRIPTERR_len - - ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// compare it manually (also check for '*' at end)
u32 pdiff = 0 ;
2022-03-22 18:13:18 -05:00
for ( ; ( pdiff < STR_SCRIPTERR_len ) & & ( label [ pdiff ] = = str [ pdiff ] ) ; pdiff + + ) ;
2018-02-07 23:07:23 +01:00
if ( ( pdiff < label_len ) & & ( label [ pdiff ] ! = ' * ' ) ) continue ; // no match
2017-10-29 14:00:02 +09:00
// otherwise: potential regular or wildcard match
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// may be a match, see if there are more strings after it
2022-03-22 18:13:18 -05:00
if ( ! ( str = get_string ( ptr , line_end , & STR_SCRIPTERR_len , & ptr , NULL ) ) ) continue ; // string error, ignore line
2017-10-29 14:00:02 +09:00
else if ( ( str < line_end ) & & ( * str ! = ' # ' ) ) continue ; // neither end of line nor comment
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
return line_start ; // match found
2022-03-22 18:13:18 -05:00
} else if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_IF ) ) {
2017-10-29 14:00:02 +09:00
next = skip_block ( line_start , true , true ) ;
2022-03-22 18:13:18 -05:00
} else if ( MATCH_STR ( str , STR_SCRIPTERR_len , _CMD_FOR ) ) {
2018-01-12 03:19:04 +01:00
next = find_next ( line_start ) ;
2017-12-19 02:58:58 +01:00
} // otherwise: irrelevant line
2017-10-29 14:00:02 +09:00
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
return NULL ;
}
2018-01-18 02:23:46 +01:00
bool for_handler ( char * path , const char * dir , const char * pattern , bool recursive ) {
static DIR fdir [ _MAX_FOR_DEPTH ] ;
2018-01-12 03:19:04 +01:00
static DIR * dp = NULL ;
static char ldir [ 256 ] ;
static char lpattern [ 64 ] ;
2018-01-18 02:23:46 +01:00
static bool rec = false ;
2020-08-24 21:27:19 -07:00
2018-01-18 02:23:46 +01:00
if ( ! path & & ! dir & & ! pattern ) { // close all dirs
while ( dp > = fdir ) fvx_closedir ( dp - - ) ;
2018-01-12 03:19:04 +01:00
dp = NULL ;
return true ;
}
2020-08-24 21:27:19 -07:00
2018-01-18 02:23:46 +01:00
if ( dir ) { // open a dir
2023-03-20 23:13:51 -05:00
snprintf ( lpattern , sizeof ( lpattern ) , " %s " , pattern ) ;
snprintf ( ldir , sizeof ( ldir ) , " %s " , dir ) ;
2018-01-12 03:19:04 +01:00
if ( dp ) return false ; // <- this should never happen
2018-01-18 02:23:46 +01:00
if ( fvx_opendir ( & fdir [ 0 ] , dir ) ! = FR_OK )
2018-01-12 03:19:04 +01:00
return false ;
2018-01-18 02:23:46 +01:00
dp = & fdir [ 0 ] ;
rec = recursive ;
} else if ( dp ) { // traverse dir
2018-01-12 03:19:04 +01:00
FILINFO fno ;
2018-01-18 02:23:46 +01:00
while ( ( fvx_preaddir ( dp , & fno , lpattern ) ! = FR_OK ) | | ! * ( fno . fname ) ) {
* path = ' \0 ' ;
if ( dp = = fdir ) return true ;
fvx_closedir ( dp - - ) ;
char * slash = strrchr ( ldir , ' / ' ) ;
if ( ! slash ) return false ;
* slash = ' \0 ' ;
}
2020-08-24 21:27:19 -07:00
2018-01-18 02:23:46 +01:00
snprintf ( path , 256 , " %s/%.254s " , ldir , fno . fname ) ;
if ( rec & & ( fno . fattrib & AM_DIR ) & & ( dp - fdir < _MAX_FOR_DEPTH - 1 ) ) {
if ( fvx_opendir ( + + dp , path ) ! = FR_OK ) dp - - ;
2021-02-05 11:21:55 +01:00
else strncpy ( ldir , path , 256 ) ;
2018-01-18 02:23:46 +01:00
}
2018-01-12 03:19:04 +01:00
} else return false ;
2020-08-24 21:27:19 -07:00
2018-01-12 03:19:04 +01:00
return true ;
}
2017-06-09 01:45:00 +02:00
bool parse_line ( const char * line_start , const char * line_end , cmd_id * cmdid , u32 * flags , u32 * argc , char * * argv , char * err_str ) {
char * ptr = ( char * ) line_start ;
char * str ;
u32 len ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// set everything to initial values
* cmdid = 0 ;
* flags = 0 ;
* argc = 0 ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// search for cmd
char * cmd = NULL ;
u32 cmd_len = 0 ;
if ( ! ( cmd = get_string ( ptr , line_end , & cmd_len , & ptr , err_str ) ) ) return false ; // string error
2017-10-29 14:00:02 +09:00
if ( ( cmd > = line_end ) | | ( * cmd = = ' # ' ) | | ( * cmd = = ' @ ' ) ) return true ; // empty line or comment or label
2020-08-24 21:27:19 -07:00
2017-12-31 03:26:50 +01:00
// special handling for "if", "elif" and "not"
if ( MATCH_STR ( cmd , cmd_len , _CMD_NOT ) ) {
* cmdid = CMD_ID_NOT ;
return true ;
} else if ( MATCH_STR ( cmd , cmd_len , _CMD_IF ) ) {
2017-10-29 14:00:02 +09:00
* cmdid = CMD_ID_IF ;
return true ;
2017-12-19 02:58:58 +01:00
} else if ( MATCH_STR ( cmd , cmd_len , _CMD_ELIF ) ) {
* cmdid = CMD_ID_ELIF ;
return true ;
2017-10-29 14:00:02 +09:00
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// got cmd, now parse flags & args
while ( ( str = get_string ( ptr , line_end , & len , & ptr , err_str ) ) ) {
2018-04-17 01:42:00 +02:00
bool in_quotes = ( ( ptr - str ) ! = ( int ) len ) ; // hacky
if ( ( str > = line_end ) | | ( ( * str = = ' # ' ) & & ! in_quotes ) ) // end of line or comment
2017-06-09 01:45:00 +02:00
return ( * cmdid = get_cmd_id ( cmd , cmd_len , * flags , * argc , err_str ) ) ;
2018-04-17 01:42:00 +02:00
if ( ( * str = = ' - ' ) & & ! in_quotes ) { // flag
2017-06-09 01:45:00 +02:00
u32 flag_add = get_flag ( str , len , err_str ) ;
if ( ! flag_add ) return false ; // not a proper flag
* flags | = flag_add ;
} else if ( * argc > = _MAX_ARGS ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_TOO_MANY_ARGUMENTS ) ;
2017-06-09 01:45:00 +02:00
return false ; // too many arguments
} else if ( ! expand_arg ( argv [ ( * argc ) + + ] , str , len ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ARGUMENT_EXPAND_FAILED ) ;
2017-06-09 01:45:00 +02:00
return false ; // arg expand failed
}
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// end reached with a failed get_string()
return false ;
}
bool run_cmd ( cmd_id id , u32 flags , char * * argv , char * err_str ) {
bool ret = true ; // true unless some cmd messes up
2020-08-24 21:27:19 -07:00
2017-08-23 02:04:53 +02:00
// process arg0 @string
u64 at_org = 0 ;
u64 sz_org = 0 ;
2018-04-11 01:28:34 +02:00
if ( ( id = = CMD_ID_FGET ) | | ( id = = CMD_ID_FSET ) | | ( id = = CMD_ID_SHA ) | | ( id = = CMD_ID_SHAGET ) | | ( id = = CMD_ID_INJECT ) | | ( id = = CMD_ID_FILL ) ) {
2017-08-23 02:04:53 +02:00
char * atstr_org = strrchr ( argv [ 0 ] , ' @ ' ) ;
if ( atstr_org ) {
* ( atstr_org + + ) = ' \0 ' ;
if ( sscanf ( atstr_org , " %llX:%llX " , & at_org , & sz_org ) ! = 2 ) {
if ( sscanf ( atstr_org , " %llX " , & at_org ) ! = 1 ) at_org = 0 ;
sz_org = 0 ;
}
}
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// perform command
2017-12-31 03:26:50 +01:00
if ( id = = CMD_ID_NOT ) {
// check the argument
// "not true" or "not false"
ret = ( strncmp ( argv [ 0 ] , _ARG_FALSE , _ARG_MAX_LEN ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_QUOTE_NOT_AN_ERROR ) ;
2017-12-31 03:26:50 +01:00
}
else if ( id = = CMD_ID_IF ) {
2017-10-29 14:00:02 +09:00
// check the argument
2017-12-19 02:58:58 +01:00
// "if true" or "if false"
skip_state = ( strncmp ( argv [ 0 ] , _ARG_TRUE , _ARG_MAX_LEN ) = = 0 ) ? 0 : _SKIP_BLOCK ;
2017-10-29 14:00:02 +09:00
ifcnt + + ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
if ( syntax_error & & err_str )
2022-03-22 18:13:18 -05:00
snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SYNTAX_ERROR_AFTER_IF ) ;
2017-10-29 14:00:02 +09:00
ret = ! syntax_error ;
}
2017-12-19 02:58:58 +01:00
else if ( id = = CMD_ID_ELIF ) {
// check syntax errors
if ( ifcnt = = 0 ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ELIF_WITHOUT_IF ) ;
2017-12-19 02:58:58 +01:00
syntax_error = true ;
return false ;
}
2020-08-24 21:27:19 -07:00
2017-12-19 02:58:58 +01:00
// skip state handling, check the argument if required
// "if true" or "if false"
skip_state = ! skip_state ? _SKIP_TILL_END :
( ( strncmp ( argv [ 0 ] , _ARG_TRUE , _ARG_MAX_LEN ) = = 0 ) ? 0 : _SKIP_BLOCK ) ;
2020-08-24 21:27:19 -07:00
2017-12-19 02:58:58 +01:00
if ( syntax_error & & err_str )
2022-03-22 18:13:18 -05:00
snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SYNTAX_ERROR_AFTER_ELIF ) ;
2017-12-19 02:58:58 +01:00
ret = ! syntax_error ;
}
2017-10-29 14:00:02 +09:00
else if ( id = = CMD_ID_ELSE ) {
// check syntax errors
if ( ifcnt = = 0 ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ELSE_WITHOUT_IF ) ;
2017-10-29 14:00:02 +09:00
syntax_error = true ;
return false ;
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// turn the skip state
2017-12-19 02:58:58 +01:00
skip_state = skip_state ? 0 : _SKIP_TILL_END ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
ret = true ;
}
else if ( id = = CMD_ID_END ) {
// check syntax errors
if ( ifcnt = = 0 ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_END_WITHOUT_IF ) ;
2017-10-29 14:00:02 +09:00
syntax_error = true ;
return false ;
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// close last "if"
2017-12-19 02:58:58 +01:00
skip_state = 0 ;
2017-10-29 14:00:02 +09:00
ifcnt - - ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
ret = true ;
}
2018-01-12 03:19:04 +01:00
else if ( id = = CMD_ID_FOR ) {
// cheating alert(!): actually this does nothing much
// just sets up the for_handler and skips to 'next'
if ( for_ptr ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FOR_INSIDE_FOR ) ;
2018-01-12 03:19:04 +01:00
syntax_error = true ;
return false ;
2018-01-18 02:23:46 +01:00
} else if ( ! for_handler ( NULL , argv [ 0 ] , argv [ 1 ] , flags & _FLG ( ' r ' ) ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_DIR_NOT_FOUND ) ;
2018-01-12 03:19:04 +01:00
skip_state = _SKIP_TO_NEXT ;
ret = false ;
} else {
skip_state = _SKIP_TO_NEXT ;
ret = true ;
}
}
else if ( id = = CMD_ID_NEXT ) {
// actual work is done here
char * var = set_var ( _VAR_FORPATH , " " ) ;
ret = true ;
if ( ! for_ptr ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_NEXT_WITHOUT_FOR ) ;
2018-01-12 03:19:04 +01:00
syntax_error = true ;
return false ;
} else if ( ! var ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FORPATH_ERROR ) ;
2018-01-12 03:19:04 +01:00
ret = false ;
} else {
2018-01-18 02:23:46 +01:00
if ( ! for_handler ( var , NULL , NULL , false ) ) * var = ' \0 ' ;
2018-01-12 03:19:04 +01:00
if ( ! * var ) {
2018-01-18 02:23:46 +01:00
for_handler ( NULL , NULL , NULL , false ) ; // finish for_handler
2018-01-12 03:19:04 +01:00
for_ptr = NULL ;
skip_state = 0 ;
} else {
skip_state = _SKIP_TO_FOR ;
}
ret = true ;
}
}
2017-10-29 14:00:02 +09:00
else if ( id = = CMD_ID_GOTO ) {
jump_ptr = find_label ( argv [ 0 ] , NULL ) ;
if ( ! jump_ptr ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_LABEL_NOT_FOUND ) ;
2017-10-29 14:00:02 +09:00
}
}
2018-07-23 01:02:39 +02:00
else if ( id = = CMD_ID_LABELSEL ) {
2017-12-20 02:08:40 +01:00
const char * options [ _CHOICE_MAX_N ] = { NULL } ;
char * options_jmp [ _CHOICE_MAX_N ] = { NULL } ;
char options_str [ _CHOICE_MAX_N ] [ _CHOICE_STR_LEN + 1 ] ;
2018-04-25 15:25:07 +02:00
u32 options_keys [ _CHOICE_MAX_N ] = { 0 } ;
2020-08-24 21:27:19 -07:00
2017-12-20 02:08:40 +01:00
char * ast = strchr ( argv [ 1 ] , ' * ' ) ;
char * ptr = NULL ;
u32 n_opt = 0 ;
while ( ( ptr = find_label ( argv [ 1 ] , ptr ) ) ) {
options_jmp [ n_opt ] = ptr ;
2020-08-24 21:27:19 -07:00
2017-12-20 02:08:40 +01:00
while ( * ( ptr + + ) ! = ' @ ' ) ;
if ( ast ) ptr + = ( ast - argv [ 1 ] ) ;
2020-08-24 21:27:19 -07:00
2017-12-20 02:08:40 +01:00
char * choice = options_str [ n_opt ] ;
for ( u32 i = 0 ; i < _CHOICE_STR_LEN ; choice [ + + i ] = ' \0 ' ) {
if ( IS_WHITESPACE ( ptr [ i ] ) ) break ;
else if ( ptr [ i ] = = ' _ ' ) choice [ i ] = ' ' ;
else choice [ i ] = ptr [ i ] ;
}
2018-07-23 01:02:39 +02:00
if ( flags & _FLG ( ' k ' ) ) {
2018-04-25 15:25:07 +02:00
char * keystr = choice ;
for ( ; * choice ! = ' ' & & * choice ! = ' \0 ' ; choice + + ) ;
if ( * choice ! = ' \0 ' ) * ( choice + + ) = ' \0 ' ;
options_keys [ n_opt ] = StringToButton ( keystr ) ;
2020-08-24 21:27:19 -07:00
if ( ! options_keys [ n_opt ] ) continue ;
2018-04-25 15:25:07 +02:00
}
options [ n_opt ] = choice ;
2017-12-20 02:08:40 +01:00
if ( + + n_opt > = _CHOICE_MAX_N ) break ;
}
2020-08-24 21:27:19 -07:00
2018-07-23 01:02:39 +02:00
u32 result = ( flags & _FLG ( ' k ' ) ) ? ShowHotkeyPrompt ( n_opt , options , options_keys , " %s " , argv [ 0 ] ) :
ShowSelectPrompt ( n_opt , options , " %s " , argv [ 0 ] ) ;
2018-04-25 15:25:07 +02:00
2017-12-20 02:08:40 +01:00
if ( ! result ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_USER_ABORT ) ;
2017-12-20 02:08:40 +01:00
} else jump_ptr = options_jmp [ result - 1 ] ;
}
2018-04-25 23:32:22 +02:00
else if ( id = = CMD_ID_KEYCHK ) {
ret = CheckButton ( StringToButton ( argv [ 0 ] ) ) ;
2022-03-22 18:13:18 -05:00
if ( ! ret & & err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_KEY_NOT_PRESSED ) ;
2018-04-25 23:32:22 +02:00
}
2017-10-29 14:00:02 +09:00
else if ( id = = CMD_ID_ECHO ) {
2018-04-17 23:44:53 +02:00
ShowPrompt ( false , " %s " , argv [ 0 ] ) ;
2017-09-13 02:45:00 +02:00
}
2017-09-19 15:57:29 +02:00
else if ( id = = CMD_ID_QR ) {
2018-01-24 23:32:06 +01:00
const u32 screen_size = SCREEN_SIZE ( ALT_SCREEN ) ;
u8 * screen_copy = ( u8 * ) malloc ( screen_size ) ;
2017-09-19 15:57:29 +02:00
u8 qrcode [ qrcodegen_BUFFER_LEN_MAX ] ;
u8 temp [ qrcodegen_BUFFER_LEN_MAX ] ;
2018-01-24 23:32:06 +01:00
ret = screen_copy & & qrcodegen_encodeText ( argv [ 1 ] , temp , qrcode , qrcodegen_Ecc_LOW ,
2017-09-19 15:57:29 +02:00
qrcodegen_VERSION_MIN , qrcodegen_VERSION_MAX , qrcodegen_Mask_AUTO , true ) ;
if ( ret ) {
2018-01-24 23:32:06 +01:00
memcpy ( screen_copy , ALT_SCREEN , screen_size ) ;
2017-09-19 15:57:29 +02:00
DrawQrCode ( ALT_SCREEN , qrcode ) ;
2018-04-17 23:44:53 +02:00
ShowPrompt ( false , " %s " , argv [ 0 ] ) ;
2018-01-24 23:32:06 +01:00
memcpy ( ALT_SCREEN , screen_copy , screen_size ) ;
2022-03-22 18:13:18 -05:00
} else if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_OUT_OF_MEMORY ) ;
2018-01-24 23:32:06 +01:00
free ( screen_copy ) ;
2017-09-19 15:57:29 +02:00
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_ASK ) {
2018-04-17 23:44:53 +02:00
ret = ShowPrompt ( true , " %s " , argv [ 0 ] ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_USER_ABORT ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_INPUT ) {
2017-08-25 00:20:50 +02:00
char input [ _VAR_CNT_LEN ] = { 0 } ;
2017-07-26 14:14:12 +02:00
char * var = get_var ( argv [ 1 ] , NULL ) ;
2017-08-25 00:20:50 +02:00
strncpy ( input , var , _VAR_CNT_LEN ) ;
2018-05-24 00:14:37 +02:00
input [ _VAR_CNT_LEN - 1 ] = ' \0 ' ;
2019-06-03 01:37:10 +02:00
ret = ShowKeyboardOrPrompt ( input , _VAR_CNT_LEN , " %s " , argv [ 0 ] ) ;
2017-08-25 00:20:50 +02:00
if ( ret ) set_var ( argv [ 1 ] , " " ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_USER_ABORT ) ;
2017-08-25 00:20:50 +02:00
if ( ret ) {
ret = set_var ( argv [ 1 ] , input ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2017-08-25 00:20:50 +02:00
}
2017-09-13 02:45:00 +02:00
}
2018-01-09 01:39:37 +01:00
else if ( ( id = = CMD_ID_FILESEL ) | | ( id = = CMD_ID_DIRSEL ) ) {
2018-05-24 00:14:37 +02:00
char choice [ _VAR_CNT_LEN ] ;
2017-09-13 02:45:00 +02:00
char * var = get_var ( argv [ 2 ] , NULL ) ;
strncpy ( choice , var , _VAR_CNT_LEN ) ;
2018-05-24 00:14:37 +02:00
choice [ _VAR_CNT_LEN - 1 ] = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-09-13 02:45:00 +02:00
char path [ _VAR_CNT_LEN ] ;
strncpy ( path , argv [ 1 ] , _VAR_CNT_LEN ) ;
2018-05-24 00:14:37 +02:00
path [ _VAR_CNT_LEN - 1 ] = ' \0 ' ;
2017-10-25 00:36:58 +02:00
if ( strncmp ( path , " Z: " , 2 ) = = 0 ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FORBIDDEN_DRIVE ) ;
2018-01-09 01:39:37 +01:00
} else if ( id = = CMD_ID_FILESEL ) {
2018-01-09 15:46:37 +01:00
char * npattern = strrchr ( path , ' / ' ) ;
if ( ! npattern ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_INVALID_PATH ) ;
2018-01-09 15:46:37 +01:00
} else {
u32 flags_ext = ( flags & _FLG ( ' d ' ) ) ? 0 : NO_DIRS ;
* ( npattern + + ) = ' \0 ' ;
2018-09-08 17:31:54 +09:00
ret = FileSelector ( choice , argv [ 0 ] , path , npattern , flags_ext , ( flags & _FLG ( ' x ' ) ) ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FILESELECT_ABORT ) ;
2018-01-09 15:46:37 +01:00
}
2018-01-09 01:39:37 +01:00
} else {
2018-09-08 17:31:54 +09:00
ret = FileSelector ( choice , argv [ 0 ] , path , NULL , NO_FILES | SELECT_DIRS , ( flags & _FLG ( ' x ' ) ) ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_DIRSELECT_ABORT ) ;
2017-09-13 02:45:00 +02:00
}
2020-08-24 21:27:19 -07:00
2017-09-13 02:45:00 +02:00
if ( ret ) {
ret = set_var ( argv [ 2 ] , choice ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
}
else if ( id = = CMD_ID_SET ) {
2017-06-09 01:45:00 +02:00
ret = set_var ( argv [ 0 ] , argv [ 1 ] ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SET_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
2017-12-20 12:33:18 +09:00
else if ( id = = CMD_ID_STRSPLIT ) {
char str [ _ARG_MAX_LEN ] ;
strncpy ( str , argv [ 1 ] , _ARG_MAX_LEN ) ;
2018-05-24 00:14:37 +02:00
str [ _ARG_MAX_LEN - 1 ] = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-12-20 12:33:18 +09:00
ret = false ;
if ( strlen ( argv [ 2 ] ) = = 1 ) { // argv[2] must be one char
char * found ;
if ( flags & _FLG ( ' f ' ) ) found = strchr ( str , * argv [ 2 ] ) ;
else found = strrchr ( str , * argv [ 2 ] ) ;
2022-03-22 18:13:18 -05:00
if ( ! found & & err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_CHAR_NOT_FOUND ) ;
2020-08-24 21:27:19 -07:00
2017-12-20 12:33:18 +09:00
if ( found ) {
if ( flags & _FLG ( ' b ' ) ) {
* found = ' \0 ' ;
ret = set_var ( argv [ 0 ] , str ) ;
} else ret = set_var ( argv [ 0 ] , found + 1 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2017-12-20 12:33:18 +09:00
}
2022-03-22 18:13:18 -05:00
} else if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ARGV_2_IS_NOT_CHAR ) ;
2017-12-20 12:33:18 +09:00
}
2017-12-31 02:09:43 +01:00
else if ( id = = CMD_ID_STRREP ) {
char str [ _ARG_MAX_LEN ] ;
strncpy ( str , argv [ 1 ] , _ARG_MAX_LEN ) ;
2018-05-24 00:14:37 +02:00
str [ _ARG_MAX_LEN - 1 ] = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-12-31 02:09:43 +01:00
if ( strnlen ( argv [ 2 ] , _ARG_MAX_LEN ) ! = 2 ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ARGV_2_MUST_BE_2_CHARS ) ;
2017-12-31 02:09:43 +01:00
ret = false ;
} else {
2018-02-06 00:29:21 +01:00
for ( u32 i = 0 ; ( i < _ARG_MAX_LEN ) & & str [ i ] ; i + + ) {
2017-12-31 02:09:43 +01:00
if ( str [ i ] = = argv [ 2 ] [ 0 ] ) str [ i ] = argv [ 2 ] [ 1 ] ;
}
ret = set_var ( argv [ 0 ] , str ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2017-12-31 02:09:43 +01:00
}
}
2017-09-21 01:21:02 +02:00
else if ( id = = CMD_ID_CHK ) {
if ( flags & _FLG ( ' u ' ) ) {
ret = ( strncasecmp ( argv [ 0 ] , argv [ 1 ] , _VAR_CNT_LEN ) ! = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ARG_MATCH ) ;
2017-09-21 01:21:02 +02:00
} else {
ret = ( strncasecmp ( argv [ 0 ] , argv [ 1 ] , _VAR_CNT_LEN ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_NO_ARG_MATCH ) ;
2017-09-21 01:21:02 +02:00
}
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_ALLOW ) {
2017-06-09 01:45:00 +02:00
if ( flags & _FLG ( ' a ' ) ) ret = CheckDirWritePermissions ( argv [ 0 ] ) ;
else ret = CheckWritePermissions ( argv [ 0 ] ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_PERMISSION_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_CP ) {
2017-06-09 01:45:00 +02:00
u32 flags_ext = BUILD_PATH ;
2017-06-23 00:28:45 +02:00
if ( flags & _FLG ( ' h ' ) ) flags_ext | = CALC_SHA ;
2021-09-30 13:12:54 -04:00
if ( flags & _FLG ( ' 1 ' ) ) flags_ext | = USE_SHA1 ;
2017-06-23 02:13:22 +02:00
if ( flags & _FLG ( ' n ' ) ) flags_ext | = NO_CANCEL ;
2017-06-23 00:28:45 +02:00
if ( flags & _FLG ( ' s ' ) ) flags_ext | = SILENT ;
if ( flags & _FLG ( ' w ' ) ) flags_ext | = OVERWRITE_ALL ;
else if ( flags & _FLG ( ' k ' ) ) flags_ext | = SKIP_ALL ;
2018-04-22 12:28:37 -05:00
else if ( flags & _FLG ( ' p ' ) ) flags_ext | = APPEND_ALL ;
2017-06-09 01:45:00 +02:00
ret = PathMoveCopy ( argv [ 1 ] , argv [ 0 ] , & flags_ext , false ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_COPY_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_MV ) {
2017-06-09 01:45:00 +02:00
u32 flags_ext = BUILD_PATH ;
2017-06-23 00:28:45 +02:00
if ( flags & _FLG ( ' n ' ) ) flags_ext | = NO_CANCEL ;
if ( flags & _FLG ( ' s ' ) ) flags_ext | = SILENT ;
if ( flags & _FLG ( ' w ' ) ) flags_ext | = OVERWRITE_ALL ;
else if ( flags & _FLG ( ' k ' ) ) flags_ext | = SKIP_ALL ;
2017-06-09 01:45:00 +02:00
ret = PathMoveCopy ( argv [ 1 ] , argv [ 0 ] , & flags_ext , true ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_MOVE_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_INJECT ) {
2017-06-09 01:45:00 +02:00
char * atstr_dst = strrchr ( argv [ 1 ] , ' @ ' ) ;
2017-08-23 01:24:55 +02:00
u64 at_dst = 0 ;
if ( atstr_dst ) {
2017-06-09 01:45:00 +02:00
* ( atstr_dst + + ) = ' \0 ' ;
2017-08-23 01:24:55 +02:00
if ( sscanf ( atstr_dst , " %llX " , & at_dst ) ! = 1 ) at_dst = 0 ;
} else fvx_unlink ( argv [ 1 ] ) ; // force new file when no offset is given
u32 flags_ext = ALLOW_EXPAND ;
if ( flags & _FLG ( ' n ' ) ) flags_ext | = NO_CANCEL ;
ret = FileInjectFile ( argv [ 1 ] , argv [ 0 ] , at_dst , at_org , sz_org , & flags_ext ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_INJECT_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
2018-01-02 02:26:49 +01:00
else if ( id = = CMD_ID_FILL ) {
u32 flags_ext = ALLOW_EXPAND ;
2019-06-02 11:24:54 +02:00
if ( flags & _FLG ( ' n ' ) ) flags_ext | = NO_CANCEL ;
2018-01-02 02:26:49 +01:00
u8 fillbyte = 0 ;
if ( ( strnlen ( argv [ 1 ] , _ARG_MAX_LEN ) ! = 2 ) | | ! strntohex ( argv [ 1 ] , & fillbyte , 1 ) ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FILLBYTE_FAIL ) ;
2018-01-02 02:26:49 +01:00
} else {
if ( flags & _FLG ( ' n ' ) ) flags_ext | = NO_CANCEL ;
ret = FileSetByte ( argv [ 0 ] , at_org , sz_org , fillbyte , & flags_ext ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FILL_FAIL ) ;
2018-01-02 02:26:49 +01:00
}
}
else if ( id = = CMD_ID_FDUMMY ) {
u32 fsize ;
if ( sscanf ( argv [ 1 ] , " %lX " , & fsize ) ! = 1 ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_BAD_FILESIZE ) ;
2018-01-02 02:26:49 +01:00
} else {
ret = FileCreateDummy ( argv [ 0 ] , NULL , fsize ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_CREATE_DUMMY_FILE ) ;
2018-01-02 02:26:49 +01:00
}
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_RM ) {
2017-06-09 01:45:00 +02:00
char pathstr [ _ERR_STR_LEN ] ;
TruncateString ( pathstr , argv [ 0 ] , 24 , 8 ) ;
ShowString ( " Deleting %s... " , pathstr ) ;
ret = PathDelete ( argv [ 0 ] ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_REMOVE_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_MKDIR ) {
2017-09-08 20:58:20 +02:00
ret = ( CheckWritePermissions ( argv [ 0 ] ) ) & & ( fvx_rmkdir ( argv [ 0 ] ) = = FR_OK ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_MAKEDIR_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_MOUNT ) {
2017-06-09 01:45:00 +02:00
ret = InitImgFS ( argv [ 0 ] ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_MOUNT_FAIL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_UMOUNT ) {
2017-06-09 01:45:00 +02:00
InitImgFS ( NULL ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_FIND ) {
2017-08-25 00:20:50 +02:00
char path [ _VAR_CNT_LEN ] ;
2017-09-06 02:01:42 +02:00
u8 mode = ( flags & _FLG ( ' f ' ) ) ? FN_LOWEST : FN_HIGHEST ;
ret = ( fvx_findpath ( path , argv [ 0 ] , mode ) = = FR_OK ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FIND_FAIL ) ;
2017-08-25 00:20:50 +02:00
if ( ret ) {
ret = set_var ( argv [ 1 ] , path ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2017-06-09 01:45:00 +02:00
}
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_FINDNOT ) {
2017-08-25 00:20:50 +02:00
char path [ _VAR_CNT_LEN ] ;
ret = ( fvx_findnopath ( path , argv [ 0 ] ) = = FR_OK ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FINDNOT_FAIL ) ;
2017-08-25 00:20:50 +02:00
if ( ret ) {
ret = set_var ( argv [ 1 ] , path ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2017-06-09 01:45:00 +02:00
}
2017-09-13 02:45:00 +02:00
}
2018-04-11 01:28:34 +02:00
else if ( id = = CMD_ID_FGET ) {
u8 data [ ( _VAR_CNT_LEN - 1 ) / 2 ] ;
if ( sz_org = = 0 ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_NO_SIZE_GIVEN ) ;
2018-04-11 01:28:34 +02:00
} else if ( sz_org > ( _VAR_CNT_LEN - 1 ) / 2 ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_FIRM_TOO_BIG ) ;
2018-04-11 01:28:34 +02:00
} else if ( FileGetData ( argv [ 0 ] , data , sz_org , at_org ) ! = sz_org ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_READ_FAIL ) ;
2018-04-11 01:28:34 +02:00
} else {
char * var = set_var ( argv [ 1 ] , " " ) ;
if ( ! var ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2018-04-11 01:28:34 +02:00
} else {
if ( flags & _FLG ( ' e ' ) ) { // flip data
for ( u32 i = 0 ; i < ( sz_org > > 1 ) ; i + + ) {
u8 tmp = data [ i ] ;
data [ i ] = data [ sz_org - 1 - i ] ;
data [ sz_org - 1 - i ] = tmp ;
}
}
ret = hexntostr ( data , var , sz_org ) ;
2022-03-22 18:13:18 -05:00
if ( ! ret & & err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_CONVERSION_FAIL ) ;
2018-04-11 01:28:34 +02:00
}
}
}
else if ( id = = CMD_ID_FSET ) {
u8 data [ ( _ARG_MAX_LEN - 1 ) / 2 ] ;
u32 len = strntohex ( argv [ 1 ] , data , 0 ) ;
if ( ! sz_org ) sz_org = len ;
if ( ( sz_org < = len ) & & ( flags & _FLG ( ' e ' ) ) ) { // flip data
for ( u32 i = 0 ; i < ( sz_org > > 1 ) ; i + + ) {
u8 tmp = data [ i ] ;
data [ i ] = data [ sz_org - 1 - i ] ;
data [ sz_org - 1 - i ] = tmp ;
}
}
if ( ! len ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_INVALID_DATA ) ;
2018-04-11 01:28:34 +02:00
} else if ( sz_org > len ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SIZE_TOO_BIG ) ;
2018-04-11 01:28:34 +02:00
} else if ( ! FileSetData ( argv [ 0 ] , data , sz_org , at_org , false ) ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_WRITE_FAIL ) ;
2018-04-11 01:28:34 +02:00
}
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_SHA ) {
2021-09-30 13:12:54 -04:00
const u8 hashlen = ( flags & _FLG ( ' 1 ' ) ) ? 20 : 32 ;
u8 hash_fil [ 0x20 ] ;
u8 hash_cmp [ 0x20 ] ;
if ( ! FileGetSha ( argv [ 0 ] , hash_fil , at_org , sz_org , flags & _FLG ( ' 1 ' ) ) ) {
2017-06-09 01:45:00 +02:00
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SHA_ARG0_FAIL ) ;
2021-09-30 13:12:54 -04:00
} else if ( ( FileGetData ( argv [ 1 ] , hash_cmp , hashlen , 0 ) ! = hashlen ) & & ! strntohex ( argv [ 1 ] , hash_cmp , hashlen ) ) {
2017-06-09 01:45:00 +02:00
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SHA_ARG1_FAIL ) ;
2017-06-09 01:45:00 +02:00
} else {
2021-09-30 13:12:54 -04:00
ret = ( memcmp ( hash_fil , hash_cmp , hashlen ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SHA_DOES_NOT_MATCH ) ;
2017-06-09 01:45:00 +02:00
}
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_SHAGET ) {
2021-09-30 13:12:54 -04:00
const u8 hashlen = ( flags & _FLG ( ' 1 ' ) ) ? 20 : 32 ;
u8 hash_fil [ 0x20 ] ;
if ( ! ( ret = FileGetSha ( argv [ 0 ] , hash_fil , at_org , sz_org , flags & _FLG ( ' 1 ' ) ) ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SHA_ARG0_FAIL ) ;
2017-09-24 13:44:49 +02:00
} else if ( ! strchr ( argv [ 1 ] , ' : ' ) ) {
2021-09-30 13:12:54 -04:00
char hash_str [ 64 + 1 ] ;
if ( flags & _FLG ( ' 1 ' ) )
2023-03-20 23:13:51 -05:00
snprintf ( hash_str , sizeof ( hash_str ) , " %016llX%016llX%08lX " , getbe64 ( hash_fil + 0 ) , getbe64 ( hash_fil + 8 ) ,
2021-09-30 13:12:54 -04:00
getbe32 ( hash_fil + 16 ) ) ;
else
2023-03-20 23:13:51 -05:00
snprintf ( hash_str , sizeof ( hash_str ) , " %016llX%016llX%016llX%016llX " , getbe64 ( hash_fil + 0 ) , getbe64 ( hash_fil + 8 ) ,
2021-09-30 13:12:54 -04:00
getbe64 ( hash_fil + 16 ) , getbe64 ( hash_fil + 24 ) ) ;
ret = set_var ( argv [ 1 ] , hash_str ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_VAR_FAIL ) ;
2021-09-30 13:12:54 -04:00
} else if ( ! ( ret = FileSetData ( argv [ 1 ] , hash_fil , hashlen , 0 , true ) ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SHA_WRITE_FAIL ) ;
2017-09-04 02:15:45 +02:00
}
2017-09-13 02:45:00 +02:00
}
2019-03-14 01:50:27 +01:00
else if ( id = = CMD_ID_DUMPTXT ) {
size_t offset = 0 ;
u32 len = strnlen ( argv [ 1 ] , _ARG_MAX_LEN ) ;
if ( flags & _FLG ( ' p ' ) ) offset = FileGetSize ( argv [ 0 ] ) ;
if ( ! ( ret = FileSetData ( argv [ 0 ] , argv [ 1 ] , len , offset , offset = = 0 ) ) | |
! ( ret = FileSetData ( argv [ 0 ] , " \n " , 1 , offset + len , false ) ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_WRITE_FAIL ) ;
2019-03-14 01:50:27 +01:00
}
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_FIXCMAC ) {
2022-03-22 18:13:18 -05:00
ShowString ( " %s " , STR_FIXING_CMACS_PLEASE_WAIT ) ;
2017-08-21 19:56:30 +02:00
ret = ( RecursiveFixFileCmac ( argv [ 0 ] ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FIXCMAC_FAILED ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_VERIFY ) {
2017-10-16 02:02:24 +02:00
u64 filetype = IdentifyFileType ( argv [ 0 ] ) ;
2017-06-09 01:45:00 +02:00
if ( filetype & IMG_NAND ) ret = ( ValidateNandDump ( argv [ 0 ] ) = = 0 ) ;
2025-10-19 14:38:20 +02:00
else ret = ( VerifyGameFile ( argv [ 0 ] , false ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_VERIFICATION_FAILED ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_DECRYPT ) {
2017-10-16 02:02:24 +02:00
u64 filetype = IdentifyFileType ( argv [ 0 ] ) ;
2017-07-27 14:29:03 +02:00
if ( filetype & BIN_KEYDB ) ret = ( CryptAesKeyDb ( argv [ 0 ] , true , false ) = = 0 ) ;
2025-10-20 16:53:42 +02:00
else ret = ( CryptGameFile ( argv [ 0 ] , true , false , false ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_DECRYPT_FAILED ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_ENCRYPT ) {
2017-10-16 02:02:24 +02:00
u64 filetype = IdentifyFileType ( argv [ 0 ] ) ;
2017-07-27 14:29:03 +02:00
if ( filetype & BIN_KEYDB ) ret = ( CryptAesKeyDb ( argv [ 0 ] , true , true ) = = 0 ) ;
2025-10-20 16:53:42 +02:00
else ret = ( CryptGameFile ( argv [ 0 ] , true , true , false ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ENCRYPT_FAILED ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_BUILDCIA ) {
2019-05-21 00:37:00 +02:00
ret = ( BuildCiaFromGameFile ( argv [ 0 ] , ( flags & _FLG ( ' l ' ) ) ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_BUILD_CIA_FAILED ) ;
2017-09-13 02:45:00 +02:00
}
2020-07-31 12:14:13 +02:00
else if ( id = = CMD_ID_INSTALL ) {
ret = ( InstallGameFile ( argv [ 0 ] , ( flags & _FLG ( ' e ' ) ) ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_INSTALL_GAME_FAILED ) ;
2020-07-31 12:14:13 +02:00
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_EXTRCODE ) {
2017-10-16 02:02:24 +02:00
u64 filetype = IdentifyFileType ( argv [ 0 ] ) ;
2018-04-22 12:28:37 -05:00
if ( ! FTYPE_HASCODE ( filetype ) ) {
2017-09-08 15:27:10 +02:00
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_DOES_NOT_CONTAIN_DOT_CODE ) ;
2017-09-08 15:27:10 +02:00
} else {
2022-03-22 18:13:18 -05:00
ShowString ( " %s " , STR_EXTRACTING_DOT_CODE ) ;
2017-10-16 02:02:24 +02:00
ret = ( ExtractCodeFromCxiFile ( argv [ 0 ] , argv [ 1 ] , NULL ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_EXTRACT_DOT_CODE_FAILED ) ;
2017-09-08 15:27:10 +02:00
}
2017-09-13 02:45:00 +02:00
}
2018-04-22 12:28:37 -05:00
else if ( id = = CMD_ID_CMPRCODE ) {
2022-03-22 18:13:18 -05:00
ShowString ( " %s " , STR_COMPRESSING_DOT_CODE ) ;
2018-04-22 12:28:37 -05:00
ret = ( CompressCode ( argv [ 0 ] , argv [ 1 ] ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_COMPRESS_DOT_CODE_FAILED ) ;
2018-04-22 12:28:37 -05:00
}
2018-04-18 02:14:59 +02:00
else if ( id = = CMD_ID_SDUMP ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_BUILD_FAILED ) ;
2018-04-18 02:14:59 +02:00
if ( ( strncasecmp ( argv [ 0 ] , TIKDB_NAME_ENC , _ARG_MAX_LEN ) = = 0 ) | |
( strncasecmp ( argv [ 0 ] , TIKDB_NAME_DEC , _ARG_MAX_LEN ) = = 0 ) ) {
bool tik_dec = ( strncasecmp ( argv [ 0 ] , TIKDB_NAME_DEC , _ARG_MAX_LEN ) = = 0 ) ;
2018-04-18 16:25:37 +02:00
if ( flags & _FLG ( ' w ' ) ) fvx_unlink ( tik_dec ? OUTPUT_PATH " / " TIKDB_NAME_DEC : OUTPUT_PATH " / " TIKDB_NAME_ENC ) ;
2018-04-18 02:14:59 +02:00
if ( BuildTitleKeyInfo ( NULL , tik_dec , false ) = = 0 ) {
2022-03-22 18:13:18 -05:00
ShowString ( STR_BUILDING_TO_OUT_ARG , OUTPUT_PATH , argv [ 0 ] ) ;
2018-04-18 02:14:59 +02:00
if ( ( ( BuildTitleKeyInfo ( " 1:/dbs/ticket.db " , tik_dec , false ) = = 0 ) | |
( BuildTitleKeyInfo ( " 4:/dbs/ticket.db " , tik_dec , false ) = = 0 ) ) & &
( BuildTitleKeyInfo ( NULL , tik_dec , true ) = = 0 ) )
ret = true ;
}
2020-10-26 23:10:36 +01:00
} else if ( strncasecmp ( argv [ 0 ] , SEEDINFO_NAME , _ARG_MAX_LEN ) = = 0 ) {
if ( flags & _FLG ( ' w ' ) ) fvx_unlink ( OUTPUT_PATH " / " SEEDINFO_NAME ) ;
2018-04-18 02:14:59 +02:00
if ( BuildSeedInfo ( NULL , false ) = = 0 ) {
2022-03-22 18:13:18 -05:00
ShowString ( STR_BUILDING_TO_OUT_ARG , OUTPUT_PATH , argv [ 0 ] ) ;
2018-04-18 02:14:59 +02:00
if ( ( ( BuildSeedInfo ( " 1: " , false ) = = 0 ) | |
( BuildSeedInfo ( " 4: " , false ) = = 0 ) ) & &
( BuildSeedInfo ( NULL , true ) = = 0 ) )
ret = true ;
}
} else {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_UNKNOWN_FILE ) ;
2018-04-18 02:14:59 +02:00
}
}
2018-04-11 11:30:43 -05:00
else if ( id = = CMD_ID_APPLYIPS ) {
ret = ( ApplyIPSPatch ( argv [ 0 ] , argv [ 1 ] , argv [ 2 ] ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_APPLY_IPS_FAILD ) ;
2018-04-11 11:30:43 -05:00
}
else if ( id = = CMD_ID_APPLYBPS ) {
ret = ( ApplyBPSPatch ( argv [ 0 ] , argv [ 1 ] , argv [ 2 ] ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_APPLY_BPS_FAILED ) ;
2018-04-11 11:30:43 -05:00
}
else if ( id = = CMD_ID_APPLYBPM ) {
ret = ( ApplyBPMPatch ( argv [ 0 ] , argv [ 1 ] , argv [ 2 ] ) = = 0 ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_APPLY_BPM_FAILED ) ;
2018-04-11 11:30:43 -05:00
}
2019-01-16 01:26:40 +01:00
else if ( id = = CMD_ID_TEXTVIEW ) {
ret = FileTextViewer ( argv [ 0 ] , false ) ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_TEXTVIEWER_FAILED ) ;
2019-01-16 01:26:40 +01:00
}
2021-10-23 14:38:01 +02:00
else if ( id = = CMD_ID_CARTDUMP ) {
CartData * cdata = ( CartData * ) malloc ( sizeof ( CartData ) ) ;
u8 * buf = ( u8 * ) malloc ( STD_BUFFER_SIZE ) ;
u64 fsize ;
ret = false ;
if ( ! cdata | | ! buf ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_OUT_OF_MEMORY ) ;
2021-10-23 14:38:01 +02:00
} else if ( sscanf ( argv [ 1 ] , " %llX " , & fsize ) ! = 1 ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_BAD_DUMPSIZE ) ;
2021-10-23 14:38:01 +02:00
} else if ( InitCartRead ( cdata ) ! = 0 ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_CART_INIT_FAIL ) ;
2021-10-23 14:38:01 +02:00
} else {
SetSecureAreaEncryption ( flags & _FLG ( ' e ' ) ) ;
fvx_unlink ( argv [ 0 ] ) ;
ret = true ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_CART_DUMP_FAILED ) ;
2021-10-23 14:38:01 +02:00
for ( u64 p = 0 ; p < fsize ; p + = STD_BUFFER_SIZE ) {
u64 len = min ( ( fsize - p ) , STD_BUFFER_SIZE ) ;
ShowProgress ( p , fsize , argv [ 0 ] ) ;
if ( ! ShowProgress ( p , fsize , argv [ 0 ] ) | |
( ReadCartBytes ( buf , p , len , cdata , false ) ! = 0 ) | |
( fvx_qwrite ( argv [ 0 ] , buf , p , len , NULL ) ! = FR_OK ) ) {
ret = false ;
break ;
}
}
}
free ( buf ) ;
free ( cdata ) ;
}
2018-01-12 02:52:39 +01:00
else if ( id = = CMD_ID_ISDIR ) {
DIR fdir ;
if ( fvx_opendir ( & fdir , argv [ 0 ] ) = = FR_OK ) {
fvx_closedir ( & fdir ) ;
ret = true ;
} else {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_NOT_A_DIR ) ;
2018-01-12 02:52:39 +01:00
ret = false ;
}
}
else if ( id = = CMD_ID_EXIST ) {
if ( fvx_stat ( argv [ 0 ] , NULL ) = = FR_OK ) {
ret = true ;
} else {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FILE_NOT_FOUND ) ;
2018-01-12 02:52:39 +01:00
ret = false ;
}
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_BOOT ) {
2018-01-24 23:32:06 +01:00
u8 * firm = ( u8 * ) malloc ( FIRM_MAX_SIZE ) ;
if ( ! firm ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_OUT_OF_MEMORY ) ;
2018-01-24 23:32:06 +01:00
} else {
size_t firm_size = FileGetData ( argv [ 0 ] , firm , FIRM_MAX_SIZE , 0 ) ;
ret = firm_size & & IsBootableFirm ( firm , firm_size ) ;
if ( ret ) {
char fixpath [ 256 ] = { 0 } ;
if ( ( * argv [ 0 ] = = ' 0 ' ) | | ( * argv [ 0 ] = = ' 1 ' ) )
2023-03-20 23:13:51 -05:00
snprintf ( fixpath , sizeof ( fixpath ) , " %s%s " , ( * argv [ 0 ] = = ' 0 ' ) ? " sdmc " : " nand " , argv [ 0 ] + 1 ) ;
2018-01-24 23:32:06 +01:00
else strncpy ( fixpath , argv [ 0 ] , 256 ) ;
2018-05-24 00:14:37 +02:00
fixpath [ 255 ] = ' \0 ' ;
2020-05-21 15:44:16 -04:00
DeinitExtFS ( ) ;
DeinitSDCardFS ( ) ;
2020-08-25 10:49:52 -03:00
PXI_DoCMD ( PXICMD_LEGACY_BOOT , NULL , 0 ) ;
2020-08-19 23:11:45 -03:00
PXI_Barrier ( PXI_FIRMLAUNCH_BARRIER ) ;
2018-01-24 23:32:06 +01:00
BootFirm ( ( FirmHeader * ) ( void * ) firm , fixpath ) ;
while ( 1 ) ;
2022-03-22 18:13:18 -05:00
} else if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_NOT_A_BOOTABLE_FIRM ) ;
2018-01-24 23:32:06 +01:00
free ( firm ) ;
}
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_SWITCHSD ) {
2017-09-04 02:15:45 +02:00
DeinitExtFS ( ) ;
if ( ! ( ret = CheckSDMountState ( ) ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_SD_NOT_MOUNTED ) ;
2017-09-04 02:15:45 +02:00
} else {
u32 pad_state ;
DeinitSDCardFS ( ) ;
2022-03-22 18:13:18 -05:00
ShowString ( " %s \n \n %s " , argv [ 0 ] , STR_EJECT_SD_CARD ) ;
2017-09-04 02:15:45 +02:00
while ( ! ( ( pad_state = InputWait ( 0 ) ) & ( BUTTON_B | SD_EJECT ) ) ) ;
if ( pad_state & SD_EJECT ) {
2022-03-22 18:13:18 -05:00
ShowString ( " %s \n \n %s " , argv [ 0 ] , STR_INSERT_SD_CARD ) ;
2017-09-04 02:15:45 +02:00
while ( ! ( ( pad_state = InputWait ( 0 ) ) & ( BUTTON_B | SD_INSERT ) ) ) ;
}
if ( pad_state & BUTTON_B ) {
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_USER_ABORT ) ;
2017-09-04 02:15:45 +02:00
}
}
InitSDCardFS ( ) ;
AutoEmuNandBase ( true ) ;
InitExtFS ( ) ;
2017-09-13 02:45:00 +02:00
}
2018-06-14 00:24:07 +02:00
else if ( id = = CMD_ID_NEXTEMU ) {
DismountDriveType ( DRV_EMUNAND ) ;
AutoEmuNandBase ( false ) ;
InitExtFS ( ) ;
}
2017-09-13 02:45:00 +02:00
else if ( id = = CMD_ID_REBOOT ) {
2017-09-08 15:24:29 +02:00
DeinitExtFS ( ) ;
DeinitSDCardFS ( ) ;
2017-06-09 01:45:00 +02:00
Reboot ( ) ;
2017-09-13 02:45:00 +02:00
}
else if ( id = = CMD_ID_POWEROFF ) {
2017-09-08 15:24:29 +02:00
DeinitExtFS ( ) ;
DeinitSDCardFS ( ) ;
2017-06-09 01:45:00 +02:00
PowerOff ( ) ;
2017-09-13 02:45:00 +02:00
}
2017-09-19 15:57:29 +02:00
else if ( id = = CMD_ID_BKPT ) {
2018-01-24 23:32:06 +01:00
bkpt ;
2017-09-19 15:57:29 +02:00
while ( 1 ) ;
}
2017-09-13 02:45:00 +02:00
else { // command not recognized / bad number of arguments
2017-06-09 01:45:00 +02:00
ret = false ;
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_UNKNOWN_ERROR ) ;
2017-06-09 01:45:00 +02:00
}
2020-08-24 21:27:19 -07:00
2022-03-22 18:13:18 -05:00
if ( ret & & err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_COMMAND_SUCCESS ) ;
2017-06-09 01:45:00 +02:00
return ret ;
}
2017-10-29 14:00:02 +09:00
bool run_line ( const char * line_start , const char * line_end , u32 * flags , char * err_str , bool if_cond ) {
2017-09-21 01:21:02 +02:00
char args [ _MAX_ARGS ] [ _ARG_MAX_LEN ] ;
char * argv [ _MAX_ARGS ] ;
2017-06-09 01:45:00 +02:00
u32 argc = 0 ;
cmd_id cmdid ;
2017-10-29 14:00:02 +09:00
2017-09-21 01:21:02 +02:00
// set up argv array
for ( u32 i = 0 ; i < _MAX_ARGS ; i + + )
argv [ i ] = args [ i ] ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// flags handling (if no pointer given)
u32 lflags ;
if ( ! flags ) flags = & lflags ;
* flags = 0 ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// parse current line, grab cmd / flags / args
if ( ! parse_line ( line_start , line_end , & cmdid , flags , & argc , argv , err_str ) ) {
2017-10-29 14:00:02 +09:00
syntax_error = true ;
return false ;
}
2020-08-24 21:27:19 -07:00
2017-12-20 01:06:56 +01:00
// control flow command handling
2017-12-31 03:26:50 +01:00
// block out of control flow commands
if ( if_cond & & IS_CTRLFLOW_CMD ( cmdid ) ) {
2022-03-22 18:13:18 -05:00
if ( err_str ) snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_CONTROL_FLOW_ERROR ) ;
2017-12-31 03:26:50 +01:00
syntax_error = true ;
return false ;
}
2020-08-24 21:27:19 -07:00
2017-12-31 03:26:50 +01:00
// shortcuts for "elif" / "else"
if ( ( ( cmdid = = CMD_ID_ELIF ) | | ( cmdid = = CMD_ID_ELSE ) ) & & ! skip_state ) {
skip_state = _SKIP_TILL_END ;
cmdid = 0 ;
}
2020-08-24 21:27:19 -07:00
2017-12-31 03:26:50 +01:00
// handle "if" / "elif" / "not"
if ( ( cmdid = = CMD_ID_IF ) | | ( cmdid = = CMD_ID_ELIF ) | | ( cmdid = = CMD_ID_NOT ) ) {
// set defaults
argc = 1 ;
2018-05-24 00:14:37 +02:00
strncpy ( argv [ 0 ] , _ARG_FALSE , _ARG_MAX_LEN - 1 ) ;
2020-08-24 21:27:19 -07:00
2017-12-31 03:26:50 +01:00
// skip to behind the command
char * line_start_next = ( char * ) line_start ;
for ( ; IS_WHITESPACE ( * line_start_next ) ; line_start_next + + ) ;
for ( ; * line_start_next & & ! IS_WHITESPACE ( * line_start_next ) ; line_start_next + + ) ;
2020-08-24 21:27:19 -07:00
2017-12-31 03:26:50 +01:00
// run condition, take over result
if ( run_line ( line_start_next , line_end , flags , err_str , true ) )
2018-05-24 00:14:37 +02:00
strncpy ( argv [ 0 ] , _ARG_TRUE , _ARG_MAX_LEN - 1 ) ;
2017-06-09 01:45:00 +02:00
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// run the command (if available)
if ( cmdid & & ! run_cmd ( cmdid , * flags , argv , err_str ) ) {
2017-07-26 14:14:12 +02:00
char * msg_fail = get_var ( " ERRORMSG " , NULL ) ;
2017-06-09 01:45:00 +02:00
if ( msg_fail & & * msg_fail ) * err_str = ' \0 ' ; // use custom error message
return false ;
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// success if we arrive here
return true ;
}
2017-06-26 01:44:16 +02:00
// checks for illegal ASCII symbols
bool ValidateText ( const char * text , u32 len ) {
2024-06-27 23:17:31 -04:00
if ( ! len ) return true ;
2017-06-26 01:44:16 +02:00
for ( u32 i = 0 ; i < len ; i + + ) {
char c = text [ i ] ;
if ( ( c = = ' \r ' ) & & ( ( i + 1 ) < len ) & & ( text [ i + 1 ] ! = ' \n ' ) ) return false ; // CR without LF
if ( ( c < 0x20 ) & & ( c ! = ' \t ' ) & & ( c ! = ' \r ' ) & & ( c ! = ' \n ' ) ) return false ; // illegal control char
2017-11-01 18:05:30 +01:00
if ( c = = 0xFF ) return false ; // 0xFF illegal char
2017-06-26 01:44:16 +02:00
}
return true ;
}
2024-07-02 23:46:37 -04:00
void MemTextView ( const char * text , u32 len , const char * line0 , int off_disp_chars , int lno , u32 ww , u32 mno , bool is_script , const char * cursor , const char * cursor_end ) {
2017-09-06 00:46:01 +02:00
// block placements
u32 x_txt = ( TV_LNOS > = 0 ) ? TV_HPAD + ( ( TV_LNOS + 1 ) * FONT_WIDTH_EXT ) : TV_HPAD ;
u32 x_lno = TV_HPAD ;
2024-06-27 23:17:31 -04:00
u32 x_al = x_txt ;
u32 x_ar = x_txt + ( ( TV_LLEN_DISP - strlen ( ar_str ) ) * FONT_WIDTH_EXT ) ;
2019-12-17 16:33:10 -03:00
2017-09-06 00:46:01 +02:00
// display text on screen
2024-06-27 23:17:31 -04:00
char txtstr [ TV_LLEN_DISP * MAX_CHAR_SIZE + 1 ] ;
const char * ptr = line0 ;
2017-09-06 00:46:01 +02:00
u32 nln = lno ;
2024-06-27 23:17:31 -04:00
bool last_empty_line_drawn = false ;
2017-09-06 00:46:01 +02:00
for ( u32 y = TV_VPAD ; y < SCREEN_HEIGHT ; y + = FONT_HEIGHT_EXT + ( 2 * TV_VPAD ) ) {
2024-07-08 23:16:30 -04:00
// account for multibyte chacters and word wrap when drawing lines
2024-06-27 23:17:31 -04:00
int off_disp_bytes = bytes_in_chars_int ( ptr , off_disp_chars ) ;
const char * ptr_next = line_seek_chars ( text , len , ww , ptr , 1 ) ;
u32 llen_chars = line_len_chars ( text , len , ww , ptr , NULL ) ;
u32 llen_bytes = bytes_in_chars_u32 ( ptr , llen_chars ) ;
u32 tv_llen_disp_chars = llen_chars ;
if ( tv_llen_disp_chars > TV_LLEN_DISP ) tv_llen_disp_chars = TV_LLEN_DISP ;
u32 tv_llen_disp_bytes = bytes_in_chars_u32 ( ptr + off_disp_bytes , tv_llen_disp_chars ) ;
u32 ncpy_bytes = ( ( int ) llen_bytes < off_disp_bytes ) ? 0 : ( llen_bytes - off_disp_bytes ) ;
if ( ncpy_bytes > tv_llen_disp_bytes ) ncpy_bytes = tv_llen_disp_bytes ;
bool al = ! ww & & off_disp_chars & & ( ptr ! = ptr_next ) ;
bool ar = ! ww & & ( llen_chars + 1 > off_disp_chars + TV_LLEN_DISP ) ;
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// set text color / find start of comment of scripts
2017-10-09 15:20:45 +02:00
u32 color_text = ( nln = = mno ) ? script_color_active : ( is_script ) ? script_color_code : ( u32 ) COLOR_TVTEXT ;
2024-06-27 23:17:31 -04:00
int cmt_start_bytes = TV_LLEN_DISP ; // start of comment in current displayed line (may be negative)
int cmt_start_chars = 0 ;
2017-09-06 00:46:01 +02:00
if ( is_script & & ( nln ! = mno ) ) {
2024-06-27 23:17:31 -04:00
const char * hash = line_start ( text , len , 0 , ptr ) ;
for ( ; * hash ! = ' # ' & & hash - ptr < ( int ) llen_bytes ; hash + + ) ;
cmt_start_bytes = hash - ( ptr + off_disp_bytes ) ;
if ( cmt_start_bytes < = 0 ) color_text = script_color_comment ;
else cmt_start_chars = chars_in_bytes ( ptr + off_disp_bytes , cmt_start_bytes ) ;
2017-09-06 00:46:01 +02:00
}
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// build text string
2024-06-27 23:17:31 -04:00
snprintf ( txtstr , sizeof ( txtstr ) , " %-*.*s " , ( int ) ( TV_LLEN_DISP * MAX_CHAR_SIZE ) , ( int ) ( TV_LLEN_DISP * MAX_CHAR_SIZE ) , " " ) ;
if ( ncpy_bytes ) {
memcpy ( txtstr , ptr + off_disp_bytes , ncpy_bytes ) ;
}
2017-09-06 00:46:01 +02:00
for ( char * d = txtstr ; * d ; d + + ) if ( * d < ' ' ) * d = ' ' ;
2024-06-27 23:17:31 -04:00
if ( ar ) {
char * textstr_end = txtstr + ncpy_bytes ;
u32 txtstr_ar_bytes = 0 ;
for ( int txtstr_ar_i = 0 ; txtstr_ar_i < ( int ) strlen ( ar_str ) ; txtstr_ar_bytes + = GetPrevCharSize ( textstr_end - txtstr_ar_bytes ) , + + txtstr_ar_i ) ;
memcpy ( textstr_end - txtstr_ar_bytes , ar_str , sizeof ( ar_str ) ) ;
}
if ( al ) {
u32 txtstr_al_bytes = bytes_in_chars_u32 ( txtstr , strlen ( al_str ) ) ;
memmove ( txtstr + strlen ( al_str ) , txtstr + txtstr_al_bytes , sizeof ( txtstr ) - txtstr_al_bytes ) ;
memcpy ( txtstr , al_str , strlen ( al_str ) ) ;
}
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// draw line number & text
2021-08-01 23:23:01 -05:00
DrawString ( TOP_SCREEN , txtstr , x_txt , y , color_text , COLOR_STD_BG ) ;
2017-09-06 00:46:01 +02:00
if ( TV_LNOS > 0 ) { // line number
2024-07-08 23:16:30 -04:00
// edge case to draw line number when the last null-byte character is word-wrapped onto its own line...
2024-06-27 23:17:31 -04:00
bool prev_ww_line_full = ww & & ww = = chars_between_pointers ( line_seek_chars ( text , len , ww , ptr , - 1 ) , ptr ) ;
2024-07-08 23:16:30 -04:00
bool last_line_empty = ptr = = text + len & & ( ! len | | ptr [ - 1 ] = = ' \n ' | | prev_ww_line_full ) & & ! last_empty_line_drawn ;
2024-06-27 23:17:31 -04:00
if ( ptr ! = ptr_next | | last_line_empty ) {
DrawStringF ( TOP_SCREEN , x_lno , y , ( ( ptr = = text ) | | ( ptr [ - 1 ] = = ' \n ' ) ) ? COLOR_TVOFFS : COLOR_TVOFFSL , COLOR_STD_BG , " %0*lu " , TV_LNOS , nln ) ;
if ( last_line_empty ) last_empty_line_drawn = true ;
}
2017-09-06 00:46:01 +02:00
else DrawStringF ( TOP_SCREEN , x_lno , y , COLOR_TVOFFSL , COLOR_STD_BG , " %*.*s " , TV_LNOS , TV_LNOS , " " ) ;
}
2020-08-24 21:27:19 -07:00
2024-07-02 23:46:37 -04:00
const int x_txt_end = x_txt + TV_LLEN_DISP * FONT_WIDTH_EXT ;
bool draw_ar_select = false , draw_al_select = false ;
// account for selections drawn across lines
DrawPixel ( TOP_SCREEN , x_txt , y - 1 , COLOR_STD_BG ) ;
DrawPixel ( TOP_SCREEN , x_txt_end - 1 , y - 1 , COLOR_STD_BG ) ;
DrawPixel ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT , COLOR_STD_BG ) ;
DrawPixel ( TOP_SCREEN , x_txt_end - 1 , y + FONT_HEIGHT_EXT , COLOR_STD_BG ) ;
2024-07-08 23:16:30 -04:00
// draw selection
2024-07-02 23:46:37 -04:00
if ( cursor_end ) {
int x_hline_start = - 1 , x_hline_end = - 1 ;
u32 cursor_line_offset_chars = chars_between_pointers ( ptr + off_disp_bytes , cursor ) ;
bool draw_cursor = cursor > = ptr + off_disp_bytes & & cursor < = ptr + off_disp_bytes + ncpy_bytes & & cursor_line_offset_chars < TV_LLEN_DISP
& & ( cursor ! = ptr + off_disp_bytes + ncpy_bytes | | is_newline ( cursor ) | | cursor = = text + len ) ;
if ( draw_cursor ) {
x_hline_start = x_txt + cursor_line_offset_chars * FONT_WIDTH_EXT ;
DrawRectangle ( TOP_SCREEN , x_hline_start , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
} else if ( cursor < ptr + off_disp_bytes ) x_hline_start = x_txt ;
const char * cursor_end_prev = GetPrevChar ( cursor_end ) ;
u32 cursor_end_line_offset_chars = chars_between_pointers ( ptr + off_disp_bytes , cursor_end_prev ) ;
bool draw_cursor_end = cursor_end_prev > = ptr + off_disp_bytes & & cursor_end_prev < = ptr + off_disp_bytes + ncpy_bytes & & cursor_end_line_offset_chars < TV_LLEN_DISP
& & ( ! ww | | cursor_end_prev ! = ptr + off_disp_bytes + ncpy_bytes | | is_newline ( cursor_end_prev ) ) ;
if ( draw_cursor_end ) {
x_hline_end = x_txt + ( cursor_end_line_offset_chars + 1 ) * FONT_WIDTH_EXT ;
2024-07-08 23:16:30 -04:00
// account for selections drawn at the end of the screen; they should be one pixel thinner so that they are drawn over by text on the next screen redraw
2024-07-02 23:46:37 -04:00
DrawRectangle ( TOP_SCREEN , x_hline_end - ( ( cursor_end_line_offset_chars = = TV_LLEN_DISP - 1 ) ? 1 : 0 ) , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
cursor_end = NULL ; // prevent cursor from being drawn multiple times at the end of the screen
} else if ( cursor_end_prev > = ptr + off_disp_bytes + ncpy_bytes ) x_hline_end = x_txt_end ;
2024-07-08 23:16:30 -04:00
// edge cases related to drawing multi-line selections
2024-07-02 23:46:37 -04:00
if ( draw_cursor & & draw_cursor_end ) {
DrawRectangle ( TOP_SCREEN , x_hline_start , y , x_hline_end - x_hline_start , 1 , COLOR_YELLOW ) ;
DrawRectangle ( TOP_SCREEN , x_hline_start , y + FONT_HEIGHT_EXT - 1 , x_hline_end - x_hline_start , 1 , COLOR_YELLOW ) ;
} else if ( draw_cursor ) {
DrawRectangle ( TOP_SCREEN , x_hline_start , y , x_txt_end - x_hline_start , 1 , COLOR_YELLOW ) ;
if ( cursor_end > ptr_next ) {
DrawRectangle ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT - 1 , x_hline_start - x_txt , 1 , COLOR_YELLOW ) ;
DrawPixel ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
DrawPixel ( TOP_SCREEN , x_txt_end - 1 , y + FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
} else {
DrawRectangle ( TOP_SCREEN , x_hline_start , y + FONT_HEIGHT_EXT - 1 , x_txt_end - x_hline_start , 1 , COLOR_YELLOW ) ;
cursor_end = NULL ;
}
DrawRectangle ( TOP_SCREEN , x_txt_end - 1 , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
draw_ar_select = true ;
} else if ( draw_cursor_end ) {
if ( cursor < ptr ) {
DrawRectangle ( TOP_SCREEN , x_hline_end , y , x_txt_end - x_hline_end , 1 , COLOR_YELLOW ) ;
DrawPixel ( TOP_SCREEN , x_txt , y - 1 , COLOR_YELLOW ) ;
DrawPixel ( TOP_SCREEN , x_txt_end - 1 , y - 1 , COLOR_YELLOW ) ;
} else {
DrawRectangle ( TOP_SCREEN , x_txt , y , x_hline_end - x_txt , 1 , COLOR_YELLOW ) ;
}
DrawRectangle ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT - 1 , x_hline_end - x_txt , 1 , COLOR_YELLOW ) ;
DrawRectangle ( TOP_SCREEN , x_txt , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
draw_al_select = true ;
} else if ( cursor < ptr_next ) {
if ( cursor < ptr + off_disp_bytes ) {
DrawRectangle ( TOP_SCREEN , x_txt , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
if ( cursor > = ptr ) DrawRectangle ( TOP_SCREEN , x_txt , y , x_txt_end - x_txt , 1 , COLOR_YELLOW ) ;
else {
DrawPixel ( TOP_SCREEN , x_txt , y - 1 , COLOR_YELLOW ) ;
DrawPixel ( TOP_SCREEN , x_txt_end - 1 , y - 1 , COLOR_YELLOW ) ;
}
draw_al_select = true ;
} else if ( cursor > = ptr + off_disp_bytes + ncpy_bytes ) {
DrawRectangle ( TOP_SCREEN , x_txt_end - 1 , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
DrawRectangle ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT - 1 , x_txt_end - x_txt , 1 , COLOR_YELLOW ) ;
draw_ar_select = true ;
}
if ( cursor_end_prev < ptr + off_disp_bytes ) {
DrawRectangle ( TOP_SCREEN , x_txt , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
DrawRectangle ( TOP_SCREEN , x_txt , y , x_txt_end - x_txt , 1 , COLOR_YELLOW ) ;
cursor_end = NULL ;
draw_al_select = true ;
} else if ( cursor_end_prev > = ptr + off_disp_bytes + ncpy_bytes ) {
DrawRectangle ( TOP_SCREEN , x_txt_end - 1 , y , 1 , FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
if ( cursor_end_prev < ptr_next ) {
DrawRectangle ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT - 1 , x_txt_end - x_txt , 1 , COLOR_YELLOW ) ;
cursor_end = NULL ;
} else {
DrawPixel ( TOP_SCREEN , x_txt , y + FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
DrawPixel ( TOP_SCREEN , x_txt_end - 1 , y + FONT_HEIGHT_EXT , COLOR_YELLOW ) ;
}
draw_ar_select = true ;
}
}
if ( ! cursor_end ) cursor = NULL ;
2024-07-08 23:16:30 -04:00
} else if ( cursor ) { // draw cursor
2024-06-27 23:17:31 -04:00
u32 cursor_line_offset_chars = chars_between_pointers ( ptr + off_disp_bytes , cursor ) ;
if ( cursor > = ptr + off_disp_bytes & & cursor < = ptr + off_disp_bytes + ncpy_bytes & & cursor_line_offset_chars < TV_LLEN_DISP
& & ( cursor ! = ptr + off_disp_bytes + ncpy_bytes | | is_newline ( cursor ) | | cursor = = text + len ) ) {
DrawRectangle ( TOP_SCREEN , x_txt + cursor_line_offset_chars * FONT_WIDTH_EXT , y , FONT_WIDTH_EXT , 1 , COLOR_RED ) ;
2024-07-08 23:16:30 -04:00
// account for cursors drawn at the end of the screen; they should be one pixel thinner so that they are drawn over by text on the next screen redraw
2024-06-27 23:17:31 -04:00
DrawRectangle ( TOP_SCREEN , x_txt + ( cursor_line_offset_chars + 1 ) * FONT_WIDTH_EXT - ( ( cursor_line_offset_chars = = TV_LLEN_DISP - 1 ) ? 1 : 0 ) , y , 1 , FONT_HEIGHT_EXT , COLOR_RED ) ;
DrawRectangle ( TOP_SCREEN , x_txt + cursor_line_offset_chars * FONT_WIDTH_EXT , y , 1 , FONT_HEIGHT_EXT , COLOR_RED ) ;
DrawRectangle ( TOP_SCREEN , x_txt + cursor_line_offset_chars * FONT_WIDTH_EXT , y + FONT_HEIGHT_EXT - 1 , FONT_WIDTH_EXT , 1 , COLOR_RED ) ;
cursor = NULL ; // prevent cursor from being drawn multiple times at the end of a file
}
}
2017-09-06 00:46:01 +02:00
// colorize comment if is_script
2024-06-27 23:17:31 -04:00
if ( cmt_start_chars > 0 & & cmt_start_chars < ( int ) TV_LLEN_DISP ) {
memset ( txtstr , ' ' , cmt_start_bytes ) ;
memmove ( txtstr + cmt_start_chars , txtstr + cmt_start_bytes , sizeof ( txtstr ) - cmt_start_bytes ) ;
2021-08-01 23:23:01 -05:00
DrawString ( TOP_SCREEN , txtstr , x_txt , y , script_color_comment , COLOR_TRANSPARENT ) ;
2017-09-06 00:46:01 +02:00
}
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// colorize arrows
2024-07-02 23:46:37 -04:00
if ( al ) DrawStringF ( TOP_SCREEN , x_al , y , draw_al_select ? COLOR_TINTEDYELLOW : COLOR_TVOFFS , COLOR_TRANSPARENT , " %s " , al_str ) ;
if ( ar ) DrawStringF ( TOP_SCREEN , x_ar , y , draw_ar_select ? COLOR_TINTEDYELLOW : COLOR_TVOFFS , COLOR_TRANSPARENT , " %s " , ar_str ) ;
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// advance pointer / line number
2024-06-27 23:17:31 -04:00
for ( const char * c = ptr ; c < ptr_next ; c + + ) if ( * c = = ' \n ' ) + + nln ;
2017-09-06 00:46:01 +02:00
ptr = ptr_next ;
}
}
2024-06-27 23:17:31 -04:00
bool MemTextViewer ( const char * text , u32 len , u32 start , bool as_script , u32 max_len , const char * save_path ) {
2017-10-02 03:08:47 +02:00
u32 ww = TV_LLEN_DISP ;
2019-12-17 16:33:10 -03:00
2017-09-06 00:46:01 +02:00
// check if this really is text
if ( ! ValidateText ( text , len ) ) {
2022-03-22 18:13:18 -05:00
ShowPrompt ( false , " %s " , STR_ERROR_INVALID_TEXT_DATA ) ;
2017-09-06 00:46:01 +02:00
return false ;
}
2019-12-17 16:33:10 -03:00
2024-06-27 23:17:31 -04:00
static const char keys_ml_alphabet [ ] = { SWKBD_KEYS_ML_ALPHABET } ;
static const char keys_special [ ] = { SWKBD_KEYS_SPECIAL } ;
static const char keys_numpad [ ] = { SWKBD_KEYS_NUMPAD } ;
static const u8 layout_ml_alphabet [ ] = { SWKBD_LAYOUT_ML_ALPHABET } ;
static const u8 layout_special [ ] = { SWKBD_LAYOUT_SPECIAL } ;
static const u8 layout_numpad [ ] = { SWKBD_LAYOUT_NUMPAD } ;
TouchBox swkbd_alphabet [ 64 ] ;
TouchBox swkbd_special [ 32 ] ;
TouchBox swkbd_numpad [ 32 ] ;
u32 uppercase = 0 ;
TouchBox * swkbd = NULL ;
TouchBox * swkbd_prev = NULL ;
2019-12-17 16:33:10 -03:00
2024-06-27 23:17:31 -04:00
// generate keyboards
if ( ! BuildKeyboard ( swkbd_alphabet , keys_ml_alphabet , layout_ml_alphabet , true ) ) return false ;
if ( ! BuildKeyboard ( swkbd_special , keys_special , layout_special , true ) ) return false ;
if ( ! BuildKeyboard ( swkbd_numpad , keys_numpad , layout_numpad , true ) ) return false ;
char * text_cpy = NULL ;
u32 text_cpy_len = 0 ;
if ( max_len ) {
// create a copy to check for changes against on exit
text_cpy = malloc ( len + 1 ) ;
text_cpy_len = len ;
if ( ! text_cpy ) return false ;
memcpy ( text_cpy , text , len + 1 ) ;
}
// clear screens
ClearScreen ( TOP_SCREEN , COLOR_STD_BG ) ;
2019-12-17 16:33:10 -03:00
2017-09-25 00:50:44 +02:00
// set script colors
if ( as_script ) {
script_color_active = COLOR_TVRUN ;
script_color_comment = COLOR_TVCMT ;
script_color_code = COLOR_TVCMD ;
}
2019-12-17 16:33:10 -03:00
2024-06-27 23:17:31 -04:00
bool crlf = is_crlf ( text ) ;
bool display_view_instructions = true ;
const char * cursor = NULL ;
2024-07-02 23:46:37 -04:00
const char * cursor_end = NULL ;
const char * * last_cursor = & cursor ;
char * clipboard = NULL ;
const char * instructions = NULL ;
2024-06-27 23:17:31 -04:00
const char * line0 = text ;
int lcurr = 1 ; // Current line number
int off_disp_chars = 0 ; // non-word-wrapped offset
for ( ; lcurr < ( int ) start ; line0 = line_seek_chars ( text , len , 0 , line0 , 1 ) , lcurr + + ) ;
2017-09-06 00:46:01 +02:00
while ( true ) {
// display text on screen
2024-07-02 23:46:37 -04:00
MemTextView ( text , len , line0 , off_disp_chars , lcurr , ww , 0 , as_script , cursor , cursor_end ) ;
2020-08-24 21:27:19 -07:00
2024-06-27 23:17:31 -04:00
const char * line0_next = line0 ;
if ( ! cursor ) { // view mode
if ( display_view_instructions ) {
ClearScreen ( BOT_SCREEN , COLOR_STD_BG ) ;
ShowString ( " %s " , max_len ? STR_TEXTEDITOR_CONTROLS_DETAILS : STR_TEXTVIEWER_CONTROLS_DETAILS ) ;
display_view_instructions = false ;
}
// handle user input
u32 pad_state = InputWait ( 0 ) ;
u32 step_ud = ( pad_state & BUTTON_R1 ) ? TV_NLIN_DISP : 1 ;
u32 step_lr = ( pad_state & BUTTON_R1 ) ? TV_LLEN_DISP : 1 ;
bool switched = ( pad_state & BUTTON_R1 ) ;
if ( pad_state & BUTTON_DOWN ) line0_next = line_seek_chars ( text , len , ww , line0 , step_ud ) ;
else if ( pad_state & BUTTON_UP ) line0_next = line_seek_chars ( text , len , ww , line0 , - step_ud ) ;
else if ( pad_state & BUTTON_RIGHT ) off_disp_chars + = step_lr ;
else if ( pad_state & BUTTON_LEFT ) off_disp_chars - = step_lr ;
else if ( max_len & & pad_state & BUTTON_A ) {
cursor = line0 ;
off_disp_chars = 0 ;
uppercase = 0 ;
2024-07-02 23:46:37 -04:00
swkbd = swkbd_alphabet ;
2024-06-27 23:17:31 -04:00
swkbd_prev = NULL ;
2024-07-02 23:46:37 -04:00
instructions = clipboard ? STR_TEXTEDITOR_CONTROLS_CLIPBOARD : STR_TEXTEDITOR_CONTROLS_KEYBOARD ;
2024-06-27 23:17:31 -04:00
}
else if ( switched & & pad_state & BUTTON_X ) {
u64 lnext64 = ShowNumberPrompt ( lcurr , STR_CURRENT_LINE_N_ENTER_NEW_LINE_BELOW , lcurr ) ;
if ( lnext64 & & ( lnext64 ! = ( u64 ) - 1 ) )
line0_next = line_seek_chars ( text , len , 0 , line_start ( text , len , 0 , line0 ) , ( int ) lnext64 - lcurr ) ;
ShowString ( " %s " , STR_TEXTVIEWER_CONTROLS_DETAILS ) ;
} else if ( switched & & pad_state & BUTTON_Y ) {
ww = ww ? 0 : TV_LLEN_DISP ;
line0_next = line_start ( text , len , ww , line0 ) ;
} else if ( pad_state & ( BUTTON_B | BUTTON_START ) ) break ;
} else { // edit mode
2024-07-02 23:46:37 -04:00
char key_pressed = ShowMultiLineKeyboard ( instructions , swkbd_alphabet , swkbd_special , swkbd_numpad , & swkbd , & swkbd_prev , & uppercase ) ;
instructions = NULL ;
2024-06-27 23:17:31 -04:00
char key_character = 0 ;
2024-07-02 23:46:37 -04:00
u32 pad_state = HID_ReadState ( ) ;
bool switched = pad_state & BUTTON_R1 ;
bool selected = pad_state & BUTTON_L1 | | ( cursor_end & & switched ) ;
const char * * prev_last_cursor = last_cursor ;
last_cursor = & cursor ;
2024-06-27 23:17:31 -04:00
if ( key_pressed = = KEY_ESCAPE ) {
cursor = NULL ;
2024-07-02 23:46:37 -04:00
cursor_end = NULL ;
2024-06-27 23:17:31 -04:00
display_view_instructions = true ;
} else if ( key_pressed = = KEY_DOWN ) {
2024-07-02 23:46:37 -04:00
const char * * down_cursor = selected ? last_cursor = & cursor_end : & cursor ;
if ( ! * down_cursor ) * down_cursor = cursor ;
const char * down_cursor_char = down_cursor = = & cursor_end & & cursor_end > cursor ? GetPrevChar ( * down_cursor ) : * down_cursor ;
const char * cursor_line_start = line_start ( text , len , ww , down_cursor_char ) ;
u32 cursor_chars_from_line_start = chars_between_pointers ( cursor_line_start , down_cursor_char ) ;
* down_cursor = line_seek_chars ( text , len , ww , cursor_line_start , switched ? TV_NLIN_DISP : 1 ) ;
* down_cursor = line_start ( text , len , ww , * down_cursor ) ;
const char * next_line_start = line_seek_chars ( text , len , ww , * down_cursor , 1 ) ;
if ( down_cursor = = & cursor_end ) for ( u32 i = 0 ; * down_cursor < next_line_start & & i < = cursor_chars_from_line_start ; IncChar ( down_cursor ) , + + i ) ;
else for ( u32 i = 0 ; GetNextChar ( * down_cursor ) < next_line_start & & i < cursor_chars_from_line_start ; IncChar ( down_cursor ) , + + i ) ;
if ( ! selected ) cursor_end = NULL ;
2024-06-27 23:17:31 -04:00
} else if ( key_pressed = = KEY_UP ) {
2024-07-02 23:46:37 -04:00
if ( selected & & ! cursor_end ) cursor_end = cursor ;
2024-06-27 23:17:31 -04:00
const char * cursor_line_start = line_start ( text , len , ww , cursor ) ;
u32 cursor_chars_from_line_start = chars_between_pointers ( cursor_line_start , cursor ) ;
cursor = line_seek_chars ( text , len , ww , cursor_line_start , - ( switched ? TV_NLIN_DISP : 1 ) ) ;
const char * next_line_start = line_seek_chars ( text , len , ww , cursor , 1 ) ;
for ( u32 i = 0 ; GetNextChar ( cursor ) < next_line_start & & i < cursor_chars_from_line_start ; IncChar ( & cursor ) , + + i ) ;
2024-07-02 23:46:37 -04:00
if ( ! selected ) cursor_end = NULL ;
2024-06-27 23:17:31 -04:00
} else if ( key_pressed = = KEY_RIGHT ) {
2024-07-02 23:46:37 -04:00
const char * * right_cursor = selected ? last_cursor = & cursor_end : & cursor ;
if ( ! * right_cursor ) * right_cursor = cursor ;
2024-06-27 23:17:31 -04:00
if ( switched ) {
2024-07-02 23:46:37 -04:00
const char * cursor_line_start = line_start ( text , len , ww , right_cursor = = & cursor_end & & cursor_end > cursor ? GetPrevChar ( * right_cursor ) : * right_cursor ) ;
2024-06-27 23:17:31 -04:00
const char * next_line_start = line_seek_chars ( text , len , ww , cursor_line_start , 1 ) ;
2024-07-02 23:46:37 -04:00
if ( right_cursor = = & cursor & & next_line_start = = text + len & & ( ! ww | | chars_between_pointers ( cursor_line_start , next_line_start ) ! = TV_LLEN_DISP ) )
IncChar ( & next_line_start ) ;
if ( right_cursor = = & cursor_end ) for ( u32 chars = 0 ; * right_cursor < next_line_start & & chars < TV_LLEN_DISP ; + + chars ) IncChar ( right_cursor ) ;
else for ( u32 chars = 0 ; GetNextChar ( * right_cursor ) < next_line_start & & ! is_newline ( * right_cursor ) & & chars < TV_LLEN_DISP ; + + chars ) IncChar ( right_cursor ) ;
2024-06-27 23:17:31 -04:00
}
2024-07-02 23:46:37 -04:00
else if ( * right_cursor < text + len ) IncChar ( right_cursor ) ;
if ( ! selected ) cursor_end = NULL ;
2024-06-27 23:17:31 -04:00
} else if ( key_pressed = = KEY_LEFT ) {
2024-07-02 23:46:37 -04:00
if ( selected & & ! cursor_end ) cursor_end = cursor ;
2024-06-27 23:17:31 -04:00
if ( switched ) {
const char * cursor_line_start = line_start ( text , len , ww , cursor ) ;
while ( cursor > cursor_line_start ) DecChar ( & cursor ) ;
}
else if ( cursor > text ) DecChar ( & cursor ) ;
2024-07-02 23:46:37 -04:00
if ( ! selected ) cursor_end = NULL ;
2024-06-27 23:17:31 -04:00
} else if ( key_pressed = = KEY_BKSPC ) {
2024-07-02 23:46:37 -04:00
if ( cursor_end & & cursor_end > cursor ) {
memmove ( ( char * ) cursor , cursor_end , text + len - cursor_end + 1 ) ;
len - = cursor_end - cursor ;
cursor_end = NULL ;
} else if ( cursor > text ) {
2024-06-27 23:17:31 -04:00
u32 size = GetPrevCharSize ( cursor ) ;
memmove ( ( char * ) cursor - size , cursor , text + len - cursor + 1 ) ;
len - = size ;
cursor - = size ;
}
} else if ( key_pressed = = KEY_UNICODE ) {
2024-07-02 23:46:37 -04:00
if ( cursor_end ) last_cursor = prev_last_cursor ;
else if ( cursor > = text + 4 & & cursor < = text + len ) {
2024-06-27 23:17:31 -04:00
u16 codepoint = 0 ;
for ( const char * c = cursor - 4 ; c < cursor ; c + + ) {
if ( ( * c > = ' 0 ' & & * c < = ' 9 ' ) | | ( * c > = ' A ' & & * c < = ' F ' ) | | ( * c > = ' a ' & & * c < = ' f ' ) ) {
codepoint < < = 4 ;
codepoint | = * c - ( * c < = ' 9 ' ? ' 0 ' : ( ( * c < = ' F ' ? ' A ' : ' a ' ) - 10 ) ) ;
} else {
codepoint = 0 ;
break ;
}
}
if ( codepoint ! = 0 ) {
char character [ 5 ] = { 0 } ;
u16 input [ 2 ] = { codepoint , 0 } ;
utf16_to_utf8 ( ( u8 * ) character , input , 4 , 1 ) ;
u32 char_size = GetCharSize ( character ) ;
memmove ( ( char * ) cursor - 4 + char_size , cursor , text + len - cursor + 1 ) ;
memcpy ( ( char * ) cursor - 4 , character , char_size ) ;
cursor - = 4 - char_size ;
len - = 4 - char_size ;
}
}
2024-07-02 23:46:37 -04:00
} else if ( key_pressed = = KEY_CLIP ) { // clipboard
if ( clipboard ) {
if ( switched ) { // clear
free ( clipboard ) ;
clipboard = NULL ;
instructions = STR_TEXTEDITOR_CONTROLS_KEYBOARD ;
last_cursor = prev_last_cursor ;
} else { // paste
int clip_size = strlen ( clipboard ) ;
int select_size = cursor_end ? cursor_end - cursor : 0 ;
if ( clip_size & & len - select_size + clip_size < = max_len ) {
if ( clip_size ! = select_size ) {
const char * select_end = cursor_end ? cursor_end : cursor ;
memmove ( ( char * ) cursor + clip_size , select_end , text + len - select_end + 1 ) ;
}
memcpy ( ( char * ) cursor , clipboard , clip_size ) ;
cursor + = clip_size ;
cursor_end = NULL ;
len + = clip_size - select_size ;
}
}
} else if ( cursor_end & & cursor_end > cursor ) { // copy
int select_size = cursor_end - cursor ;
clipboard = malloc ( select_size + 1 ) ;
if ( clipboard ) {
memcpy ( clipboard , cursor , select_size ) ;
clipboard [ select_size ] = ' \0 ' ;
if ( switched ) { // cut
memmove ( ( char * ) cursor , cursor_end , text + len - cursor_end + 1 ) ;
len - = select_size ;
cursor_end = NULL ;
} else last_cursor = prev_last_cursor ;
instructions = STR_TEXTEDITOR_CONTROLS_CLIPBOARD ;
}
}
2024-06-27 23:17:31 -04:00
} else if ( key_pressed = = KEY_ENTER ) key_character = crlf ? ' \r ' : ' \n ' ;
else if ( key_pressed < 0x80 ) key_character = key_pressed ;
2024-07-02 23:46:37 -04:00
// delete selection if typing standard char
if ( key_character & & cursor_end & & cursor_end > cursor ) {
memmove ( ( char * ) cursor , cursor_end , text + len - cursor_end + 1 ) ;
len - = cursor_end - cursor ;
cursor_end = NULL ;
}
// type standard char
2024-06-27 23:17:31 -04:00
if ( key_character & & len + ( key_character = = ' \r ' ? 1 : 0 ) < max_len ) {
if ( uppercase = = 1 ) {
uppercase = 0 ;
}
memmove ( ( char * ) cursor + 1 , cursor , text + len + + - cursor + 1 ) ;
* ( ( char * ) cursor + + ) = key_character ;
if ( key_character = = ' \r ' ) {
memmove ( ( char * ) cursor + 1 , cursor , text + len + + - cursor + 1 ) ;
* ( ( char * ) cursor + + ) = ' \n ' ;
}
}
2024-07-02 23:46:37 -04:00
if ( cursor_end < = cursor ) cursor_end = NULL ;
// adjust screen to view last cursor moved
const char * last_cursor_value = cursor_end & & last_cursor = = & cursor_end ? GetPrevChar ( cursor_end ) : cursor ;
if ( last_cursor_value & & ! ww ) {
const char * cursor_line_start = line_start ( text , len , ww , last_cursor_value ) ;
u32 cursor_chars_from_line_start = chars_between_pointers ( cursor_line_start , last_cursor_value ) ;
2024-06-27 23:17:31 -04:00
if ( cursor_chars_from_line_start < off_disp_chars + strlen ( al_str ) ) off_disp_chars = cursor_chars_from_line_start - strlen ( al_str ) ;
if ( cursor_chars_from_line_start > = off_disp_chars + TV_LLEN_DISP - strlen ( ar_str ) ) off_disp_chars = cursor_chars_from_line_start + strlen ( ar_str ) - TV_LLEN_DISP + 1 ;
}
2024-07-02 23:46:37 -04:00
while ( last_cursor_value & & last_cursor_value < line0_next ) line0_next = line_seek_chars ( text , len , ww , line0_next , - 1 ) ;
while ( last_cursor_value & & line0_next < line_seek_chars ( text , len , ww , GetNextChar ( last_cursor_value ) , - TV_NLIN_DISP ) ) line0_next = line_seek_chars ( text , len , ww , line0_next , 1 ) ;
2024-06-27 23:17:31 -04:00
}
// find last allowed lines (ww and nonww)
const char * llast_nww = line_seek_chars ( text , len , 0 , text + len + 1 , - TV_NLIN_DISP ) ;
const char * llast_ww = line_seek_chars ( text , len , TV_LLEN_DISP , text + len + 1 , - TV_NLIN_DISP ) ;
2020-08-24 21:27:19 -07:00
2018-03-07 01:15:12 +01:00
// check for problems, apply changes
if ( ! ww & & ( line0_next > llast_nww ) ) line0_next = llast_nww ;
else if ( ww & & ( line0_next > llast_ww ) ) line0_next = llast_ww ;
if ( line0_next < line0 ) { // fix line number for decrease
2024-06-27 23:17:31 -04:00
do {
DecChar ( & line0 ) ;
if ( is_newline ( line0 ) ) lcurr - - ;
}
2018-03-07 01:15:12 +01:00
while ( line0 > line0_next ) ;
} else { // fix line number for increase / same
2024-06-27 23:17:31 -04:00
for ( ; line0_next > line0 ; IncChar ( & line0 ) ) if ( is_newline ( line0 ) ) lcurr + + ;
}
// find maximum line length
u32 llen_max = 0 ;
for ( const char * ptr = text ; ptr < text + len ; ptr = line_seek_chars ( text , len , 0 , ptr , 1 ) ) {
u32 llen = line_len_chars ( text , len , 0 , ptr , NULL ) + 1 ;
if ( llen > llen_max ) llen_max = llen ;
2017-09-06 00:46:01 +02:00
}
2024-06-27 23:17:31 -04:00
if ( off_disp_chars + TV_LLEN_DISP > llen_max ) off_disp_chars = llen_max - TV_LLEN_DISP ;
if ( off_disp_chars < 0 | | ww ) off_disp_chars = 0 ;
}
2024-07-02 23:46:37 -04:00
if ( clipboard ) free ( clipboard ) ;
2024-06-27 23:17:31 -04:00
// check for user edits
if ( text_cpy ) {
if ( save_path ) {
bool diffs = false ;
if ( len ! = text_cpy_len ) diffs = true ;
else for ( u32 i = 0 ; i < len ; + + i ) if ( text [ i ] ! = text_cpy [ i ] ) { diffs = true ; break ; }
if ( diffs & & ShowPrompt ( true , " %s " , STR_TEXT_EDITS_SAVE_CHANGES ) & & ! FileSetData ( save_path , text , len , 0 , true ) )
ShowPrompt ( false , " %s " , STR_FAILED_WRITING_TO_FILE ) ;
}
free ( text_cpy ) ;
2017-09-06 00:46:01 +02:00
}
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// clear screens
ClearScreenF ( true , true , COLOR_STD_BG ) ;
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
return true ;
}
2017-10-02 03:08:47 +02:00
// right now really only intended for use with the GodMode9 readme
// (misses safety checks for wider compatibility)
bool MemToCViewer ( const char * text , u32 len , const char * title ) {
const u32 max_captions = 24 ; // we assume this is enough
2024-06-27 23:17:31 -04:00
const char * captions [ max_captions ] ;
2017-10-02 03:08:47 +02:00
u32 lineno [ max_captions ] ;
u32 ww = TV_LLEN_DISP ;
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
// check if this really is text
if ( ! ValidateText ( text , len ) ) {
2022-03-22 18:13:18 -05:00
ShowPrompt ( false , " %s " , STR_ERROR_INVALID_TEXT_DATA ) ;
2017-10-02 03:08:47 +02:00
return false ;
}
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
// clear screens / view start of readme on top
ClearScreenF ( true , true , COLOR_STD_BG ) ;
2024-07-02 23:46:37 -04:00
MemTextView ( text , len , text , 0 , 1 , ww , 0 , false , NULL , NULL ) ;
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
// parse text for markdown captions
u32 n_captions = 0 ;
2024-06-27 23:17:31 -04:00
const char * ptr = text ;
2017-10-02 03:08:47 +02:00
for ( u32 lno = 1 ; ; lno + + ) {
2024-06-27 23:17:31 -04:00
const char * ptr_next = line_seek_chars ( text , len , 0 , ptr , 1 ) ;
2017-10-02 03:08:47 +02:00
if ( ptr = = ptr_next ) break ;
if ( * ptr = = ' # ' ) {
captions [ n_captions ] = ptr ;
lineno [ n_captions ] = lno ;
if ( ( lno > 1 ) & & ( + + n_captions > = max_captions ) ) break ;
}
ptr = ptr_next ;
}
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
int cursor = - 1 ;
while ( true ) {
// display ToC
u32 y0 = TV_VPAD ;
u32 x0 = ( SCREEN_WIDTH_BOT - GetDrawStringWidth ( title ) ) / 2 ;
DrawStringF ( BOT_SCREEN , x0 , y0 , COLOR_TVTEXT , COLOR_STD_BG , " %s \n %*.*s " , title ,
strnlen ( title , 40 ) , strnlen ( title , 40 ) , " ======================================== " ) ;
y0 + = 2 * ( FONT_HEIGHT_EXT + ( 2 * TV_VPAD ) ) ;
for ( u32 i = 0 ; ( i < n_captions ) & & ( y0 < SCREEN_HEIGHT ) ; i + + ) {
u32 text_color = ( ( int ) i = = cursor ) ? COLOR_TVRUN : COLOR_TVTEXT ;
2024-06-27 23:17:31 -04:00
const char * caption = captions [ i ] ;
2017-10-02 03:08:47 +02:00
u32 len = 0 ;
u32 lvl = 0 ;
for ( ; * caption = = ' # ' ; caption + + , lvl + + ) ;
for ( ; IS_WHITESPACE ( * caption ) ; caption + + ) ;
for ( ; caption [ len ] ! = ' \n ' & & caption [ len ] ! = ' \r ' ; len + + ) ;
DrawStringF ( BOT_SCREEN , x0 + ( lvl - 1 ) * ( FONT_WIDTH_EXT / 2 ) , y0 , text_color , COLOR_STD_BG ,
2022-03-22 19:40:47 +01:00
" %*.*s " , ( int ) len , ( int ) len , caption ) ;
2017-10-02 03:08:47 +02:00
y0 + = FONT_HEIGHT_EXT + ( 2 * TV_VPAD ) ;
}
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
// handle user input
u32 pad_state = InputWait ( 0 ) ;
if ( ( cursor > = 0 ) & & ( pad_state & BUTTON_A ) ) {
2024-06-27 23:17:31 -04:00
if ( ! MemTextViewer ( text , len , lineno [ cursor ] , false , 0 , NULL ) ) return false ;
2024-07-02 23:46:37 -04:00
MemTextView ( text , len , captions [ cursor ] , 0 , lineno [ cursor ] , ww , 0 , false , NULL , NULL ) ;
2017-10-02 03:08:47 +02:00
} else if ( pad_state & BUTTON_B ) {
break ;
} else if ( pad_state & BUTTON_UP ) {
cursor = ( cursor < = 0 ) ? ( ( int ) n_captions - 1 ) : cursor - 1 ;
2024-07-02 23:46:37 -04:00
MemTextView ( text , len , captions [ cursor ] , 0 , lineno [ cursor ] , ww , 0 , false , NULL , NULL ) ;
2017-10-02 03:08:47 +02:00
} else if ( pad_state & BUTTON_DOWN ) {
if ( + + cursor > = ( int ) n_captions ) cursor = 0 ;
2024-07-02 23:46:37 -04:00
MemTextView ( text , len , captions [ cursor ] , 0 , lineno [ cursor ] , ww , 0 , false , NULL , NULL ) ;
2017-10-02 03:08:47 +02:00
}
}
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
// clear screens
ClearScreenF ( true , true , COLOR_STD_BG ) ;
2020-08-24 21:27:19 -07:00
2017-10-02 03:08:47 +02:00
return true ;
}
2017-09-06 00:46:01 +02:00
bool FileTextViewer ( const char * path , bool as_script ) {
// load text file (completely into memory)
2018-01-24 23:32:06 +01:00
// text file needs to fit inside the STD_BUFFER_SIZE
2024-06-27 23:17:31 -04:00
size_t fileSize = FileGetSize ( path ) ;
if ( fileSize > = STD_BUFFER_SIZE ) {
ShowPrompt ( false , STR_ERROR_TEXT_FILE_TOO_BIG , fileSize , STD_BUFFER_SIZE - 1 ) ;
return false ;
}
2019-12-17 16:33:10 -03:00
char * text = malloc ( STD_BUFFER_SIZE ) ;
2018-01-24 23:32:06 +01:00
if ( ! text ) return false ;
2019-12-17 16:33:10 -03:00
2024-06-27 23:17:31 -04:00
u32 flen = FileGetData ( path , text , STD_BUFFER_SIZE - 1 , 0 ) ;
2019-12-17 16:33:10 -03:00
text [ flen ] = ' \0 ' ;
2017-09-06 00:46:01 +02:00
// let MemTextViewer take over
2024-06-27 23:17:31 -04:00
bool result = MemTextViewer ( text , flen , 1 , as_script , STD_BUFFER_SIZE - 1 , path ) ;
2019-12-17 16:33:10 -03:00
2018-01-24 23:32:06 +01:00
free ( text ) ;
return result ;
2017-09-06 00:46:01 +02:00
}
2017-06-09 01:45:00 +02:00
bool ExecuteGM9Script ( const char * path_script ) {
2021-08-02 14:50:46 -05:00
char path_str [ UTF_BUFFER_BYTESIZE ( 32 ) ] ;
2017-10-29 14:00:02 +09:00
TruncateString ( path_str , path_script , 32 , 12 ) ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// reset control flow global vars
ifcnt = 0 ;
2018-01-12 03:19:04 +01:00
jump_ptr = NULL ;
for_ptr = NULL ;
2017-12-19 02:58:58 +01:00
skip_state = 0 ;
2017-10-29 14:00:02 +09:00
syntax_error = false ;
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
// allocate && check memory
var_buffer = ( void * ) malloc ( sizeof ( Gm9ScriptVar ) * _VAR_MAX_BUFF ) ;
script_buffer = ( void * ) malloc ( SCRIPT_MAX_SIZE ) ;
char * script = ( char * ) script_buffer ;
char * ptr = script ;
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
if ( ! var_buffer | | ! script_buffer ) {
if ( var_buffer ) free ( var_buffer ) ;
if ( script_buffer ) free ( script_buffer ) ;
2022-03-22 18:13:18 -05:00
ShowPrompt ( false , " %s " , STR_OUT_OF_MEMORY ) ;
2018-02-06 02:10:25 +01:00
return false ;
}
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
// fetch script from path
u32 script_size = FileGetData ( path_script , ( u8 * ) script , SCRIPT_MAX_SIZE , 0 ) ;
if ( ! script_size | | ( script_size > = SCRIPT_MAX_SIZE ) ) {
free ( var_buffer ) ;
free ( script_buffer ) ;
2017-06-09 01:45:00 +02:00
return false ;
2018-02-06 02:10:25 +01:00
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
char * end = script + script_size ;
* end = ' \0 ' ;
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// initialise variables
2017-07-26 14:08:29 +02:00
init_vars ( path_script ) ;
2020-08-24 21:27:19 -07:00
2017-09-16 17:08:26 +02:00
// setup script preview (only if used)
u32 preview_mode_local = 0 ;
if ( MAIN_SCREEN ! = TOP_SCREEN ) {
2017-09-06 00:46:01 +02:00
ClearScreen ( TOP_SCREEN , COLOR_STD_BG ) ;
2017-09-16 17:08:26 +02:00
preview_mode = 2 ; // 0 -> off 1 -> quick 2 -> full
2017-09-25 00:50:44 +02:00
script_color_active = COLOR_TVRUN ;
script_color_comment = COLOR_TVCMT ;
script_color_code = COLOR_TVCMD ;
2017-09-16 17:08:26 +02:00
}
2020-08-24 21:27:19 -07:00
2017-09-06 00:46:01 +02:00
// script execute loop
2017-10-29 14:00:02 +09:00
u32 lno = 1 ;
2018-02-06 02:10:25 +01:00
bool result = true ;
2017-10-29 14:00:02 +09:00
while ( ptr < end ) {
2017-06-09 01:45:00 +02:00
u32 flags = 0 ;
2018-03-29 22:12:53 -03:00
2017-06-09 01:45:00 +02:00
// find line end
char * line_end = strchr ( ptr , ' \n ' ) ;
if ( ! line_end ) line_end = ptr + strlen ( ptr ) ;
2018-03-29 22:12:53 -03:00
2017-09-06 00:46:01 +02:00
// update script viewer
if ( MAIN_SCREEN ! = TOP_SCREEN ) {
2017-09-16 17:08:26 +02:00
if ( preview_mode ! = preview_mode_local ) {
2017-12-20 00:13:31 +01:00
if ( ! preview_mode | | ( preview_mode > 2 ) | | ! preview_mode_local )
ClearScreen ( TOP_SCREEN , COLOR_STD_BG ) ;
if ( preview_mode > 2 ) {
2022-03-22 18:13:18 -05:00
const char * preview_str = get_var ( " PREVIEW_MODE " , NULL ) ;
2018-03-29 22:12:53 -03:00
u32 bitmap_width , bitmap_height ;
2019-05-25 19:10:45 -03:00
u16 * bitmap = NULL ;
2018-03-29 22:12:53 -03:00
u8 * png = ( u8 * ) malloc ( SCREEN_SIZE_TOP ) ;
if ( png ) {
u32 png_size = FileGetData ( preview_str , png , SCREEN_SIZE_TOP , 0 ) ;
if ( png_size & & png_size < SCREEN_SIZE_TOP )
bitmap = PNG_Decompress ( png , png_size , & bitmap_width , & bitmap_height ) ;
free ( png ) ;
}
if ( bitmap ) {
DrawBitmap ( TOP_SCREEN , - 1 , - 1 , bitmap_width , bitmap_height , bitmap ) ;
free ( bitmap ) ;
2021-03-18 22:44:33 +01:00
} else if ( ShowGameFileIcon ( preview_str , TOP_SCREEN ) ! = 0 ) {
2022-03-22 18:13:18 -05:00
if ( strncmp ( preview_str , " off " , _VAR_CNT_LEN ) = = 0 ) preview_str = STR_PREVIEW_DISABLED ;
2018-05-12 08:47:06 +09:00
DrawStringCenter ( TOP_SCREEN , COLOR_STD_FONT , COLOR_STD_BG , " %s " , preview_str ) ;
2017-12-20 00:13:31 +01:00
}
2018-03-29 22:12:53 -03:00
2017-12-20 00:13:31 +01:00
preview_mode = 0 ;
}
2017-09-16 17:08:26 +02:00
preview_mode_local = preview_mode ;
}
2018-03-29 22:12:53 -03:00
2017-12-20 00:13:31 +01:00
bool show_preview = preview_mode ;
2017-09-16 17:08:26 +02:00
if ( preview_mode = = 1 ) {
show_preview = false ;
for ( char * c = ptr ; ( c < line_end ) & & ! show_preview ; c + + ) {
2017-12-20 00:13:31 +01:00
// check for comments / labels
2017-09-16 17:08:26 +02:00
if ( IS_WHITESPACE ( * c ) ) continue ;
2017-12-20 00:13:31 +01:00
else if ( ( * c = = ' # ' ) | | ( * c = = ' @ ' ) ) break ;
2017-09-16 17:08:26 +02:00
else show_preview = true ;
}
}
if ( show_preview ) {
if ( lno < = ( TV_NLIN_DISP / 2 ) ) {
2024-07-02 23:46:37 -04:00
MemTextView ( script , script_size , script , 0 , 1 , 0 , lno , true , NULL , NULL ) ;
2017-09-16 17:08:26 +02:00
} else {
2024-06-27 23:17:31 -04:00
const char * ptr_view = line_seek_chars ( script , script_size , 0 , ptr , - ( TV_NLIN_DISP / 2 ) ) ;
2020-08-24 21:27:19 -07:00
u32 lno_view = lno - ( TV_NLIN_DISP / 2 ) ;
2024-07-02 23:46:37 -04:00
MemTextView ( script , script_size , ptr_view , 0 , lno_view , 0 , lno , true , NULL , NULL ) ;
2017-09-16 17:08:26 +02:00
}
2017-09-06 00:46:01 +02:00
}
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// run command
char err_str [ _ERR_STR_LEN + 1 ] = { 0 } ;
2018-02-26 23:29:00 +01:00
result = run_line ( ptr , line_end , & flags , err_str , false ) ;
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
// skip state handling
char * skip_ptr = ptr ;
2018-01-12 03:19:04 +01:00
if ( ( skip_state = = _SKIP_BLOCK ) | | ( skip_state = = _SKIP_TILL_END ) ) {
2017-12-19 02:58:58 +01:00
skip_ptr = skip_block ( line_end + 1 , ( skip_state = = _SKIP_TILL_END ) , false ) ;
2018-01-10 00:49:28 +01:00
if ( ! skip_ptr ) {
2022-03-22 18:13:18 -05:00
snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_UNCLOSED_CONDITIONAL ) ;
2018-01-10 00:49:28 +01:00
result = false ;
syntax_error = true ;
}
2018-01-12 03:19:04 +01:00
} else if ( skip_state = = _SKIP_TO_NEXT ) {
skip_ptr = find_next ( ptr ) ;
if ( ! skip_ptr ) {
2022-03-22 18:13:18 -05:00
snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_FOR_WITHOUT_NEXT ) ;
2018-01-12 03:19:04 +01:00
result = false ;
syntax_error = true ;
}
for_ptr = ( char * ) line_end + 1 ;
} else if ( skip_state = = _SKIP_TO_FOR ) {
skip_ptr = for_ptr ;
if ( ! skip_ptr ) {
2022-03-22 18:13:18 -05:00
snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_NEXT_WITHOUT_FOR ) ;
2018-01-12 03:19:04 +01:00
result = false ;
syntax_error = true ;
}
skip_state = 0 ;
2017-10-29 14:00:02 +09:00
}
2020-08-24 21:27:19 -07:00
2017-10-29 14:00:02 +09:00
if ( ! result ) { // error handling
if ( syntax_error ) // severe error, can't continue
flags & = ~ ( _FLG ( ' o ' ) | _FLG ( ' s ' ) ) ; // never silent or optional
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
if ( ! ( flags & _FLG ( ' s ' ) ) ) { // not silent
if ( ! * err_str ) {
2017-07-26 14:14:12 +02:00
char * msg_fail = get_var ( " ERRORMSG " , NULL ) ;
2018-05-12 08:47:06 +09:00
if ( msg_fail & & * msg_fail ) ShowPrompt ( false , " %s " , msg_fail ) ;
2022-03-22 18:13:18 -05:00
else snprintf ( err_str , _ERR_STR_LEN , " %s " , STR_SCRIPTERR_ERROR_MESSAGE_FAIL ) ;
2017-06-09 01:45:00 +02:00
}
if ( * err_str ) {
char line_str [ 32 + 1 ] ;
char * lptr0 = ptr ;
char * lptr1 = line_end ;
for ( ; IS_WHITESPACE ( * lptr0 ) & & ( lptr0 < lptr1 ) ; lptr0 + + ) ; // skip whitespaces
if ( ( lptr1 > lptr0 ) & & ( * ( lptr1 - 1 ) = = ' \r ' ) ) lptr1 - - ; // handle \r
2023-03-20 23:13:51 -05:00
if ( lptr1 - lptr0 > 32 ) snprintf ( line_str , sizeof ( line_str ) , " %.29s... " , lptr0 ) ;
else snprintf ( line_str , sizeof ( line_str ) , " %.*s " , lptr1 - lptr0 , lptr0 ) ;
2022-03-22 18:13:18 -05:00
ShowPrompt ( false , STR_PATH_LINE_N_ERR_LINE , path_str , lno , err_str , line_str ) ;
2017-06-09 01:45:00 +02:00
}
}
2018-02-06 02:10:25 +01:00
if ( ! ( flags & _FLG ( ' o ' ) ) ) { // failed if not optional
for_handler ( NULL , NULL , NULL , false ) ; // make sure we don't have an open 'for'
break ;
2018-02-26 23:29:00 +01:00
} else result = true ; // set back the result otherwise
2017-06-09 01:45:00 +02:00
}
2020-08-24 21:27:19 -07:00
2017-06-09 01:45:00 +02:00
// reposition pointer
2017-10-29 14:00:02 +09:00
if ( skip_ptr ! = ptr ) {
ptr = skip_ptr ;
lno = get_lno ( script , script_size , ptr ) ;
} else if ( jump_ptr ) {
ptr = jump_ptr ;
lno = get_lno ( script , script_size , ptr ) ;
ifcnt = 0 ; // jumping into conditional block is unexpected/unsupported
jump_ptr = NULL ;
2018-01-12 03:19:04 +01:00
for_ptr = NULL ;
2018-01-18 02:23:46 +01:00
for_handler ( NULL , NULL , NULL , false ) ;
2017-10-29 14:00:02 +09:00
} else {
ptr = line_end + 1 ;
lno + + ;
}
}
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
if ( result ) { // all fine(?) up to this point
if ( ifcnt ) { // check for unresolved 'if'
2022-03-22 18:13:18 -05:00
ShowPrompt ( false , " %s \n %s " , path_str , STR_END_OF_SCRIPT_UNRESOLVED_IF ) ;
2018-02-06 02:10:25 +01:00
result = false ;
}
if ( for_ptr ) { // check for unresolved 'for'
2022-03-22 18:13:18 -05:00
ShowPrompt ( false , " %s \n %s " , path_str , STR_END_OF_SCRIPT_UNRESOLVED_FOR ) ;
2018-02-06 02:10:25 +01:00
for_handler ( NULL , NULL , NULL , false ) ;
result = false ;
}
2017-06-09 01:45:00 +02:00
}
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
if ( result ) { // success message if applicable
char * msg_okay = get_var ( " SUCCESSMSG " , NULL ) ;
2018-05-12 08:47:06 +09:00
if ( msg_okay & & * msg_okay ) ShowPrompt ( false , " %s " , msg_okay ) ;
2018-02-06 02:10:25 +01:00
}
2020-08-24 21:27:19 -07:00
2018-02-06 02:10:25 +01:00
free ( var_buffer ) ;
free ( script_buffer ) ;
return result ;
2017-06-09 01:45:00 +02:00
}