From be4e67961ba5d14f3ae0ae7d281e0cf039101eff Mon Sep 17 00:00:00 2001 From: HeathenUK Date: Tue, 21 Nov 2023 17:31:57 +0000 Subject: [PATCH 1/4] Add KEY hotkey command and editor calling. --- src/mos.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++ src/mos.h | 9 +++++ src/mos_editor.c | 62 +++++++++++++++++++++++++++++++++++ src/mos_editor.h | 2 ++ 4 files changed, 158 insertions(+) diff --git a/src/mos.c b/src/mos.c index ddc74b9..2b97c15 100644 --- a/src/mos.c +++ b/src/mos.c @@ -62,6 +62,8 @@ extern volatile BYTE keyascii; // In globals.asm extern volatile BYTE vpd_protocol_flags; // In globals.asm extern BYTE rtc; // In globals.asm +char cmd_shadow[256]; // Will hold preserved edit line + static FATFS fs; // Handle for the file system static char * mos_strtok_ptr; // Pointer for current position in string tokeniser @@ -94,6 +96,7 @@ static t_mosCommand mosCommands[] = { { "CLS", &mos_cmdCLS, NULL, HELP_CLS, NULL }, { "MOUNT", &mos_cmdMOUNT, NULL, HELP_MOUNT, NULL }, { "HELP", &mos_cmdHELP, HELP_HELP_ARGS, HELP_HELP, NULL }, + { "KEY", &mos_cmdKEY, HELP_KEY_ARGS, HELP_KEY, NULL }, }; #define mosCommands_count (sizeof(mosCommands)/sizeof(t_mosCommand)) @@ -337,6 +340,9 @@ int mos_exec(char * buffer) { UINT8 mode; ptr = mos_trim(buffer); + + strcpy (cmd_shadow, ptr); + ptr = mos_strtok(ptr, " "); if(ptr != NULL) { func = mos_getCommand(ptr); @@ -408,6 +414,85 @@ int mos_cmdDIR(char * ptr) { return mos_DIR(path); } +// KEY command +// Parameters: +// - ptr: Pointer to the argument string in the line edit buffer +// Returns: +// - MOS error code +// +int mos_cmdKEY(char *ptr) { + + UINT24 fn_number = 0; + char *hotkey_string; + char *hotkey_string_token; + + if (!mos_parseNumber(NULL, &fn_number)) { + + printf("Hotkey assignments:\r\n\r\n"); + + if (hotkey_strings[0] != NULL) printf("F1: %s\r\n", hotkey_strings[0]); + else printf("F1: N/A\r\n"); + if (hotkey_strings[1] != NULL) printf("F2: %s\r\n", hotkey_strings[1]); + else printf("F2: N/A\r\n"); + if (hotkey_strings[2] != NULL) printf("F3: %s\r\n", hotkey_strings[2]); + else printf("F3: N/A\r\n"); + if (hotkey_strings[3] != NULL) printf("F4: %s\r\n", hotkey_strings[3]); + else printf("F4: N/A\r\n"); + if (hotkey_strings[4] != NULL) printf("F5: %s\r\n", hotkey_strings[4]); + else printf("F5: N/A\r\n"); + if (hotkey_strings[5] != NULL) printf("F6: %s\r\n", hotkey_strings[5]); + else printf("F6: N/A\r\n"); + if (hotkey_strings[6] != NULL) printf("F7: %s\r\n", hotkey_strings[6]); + else printf("F7: N/A\r\n"); + if (hotkey_strings[7] != NULL) printf("F8: %s\r\n", hotkey_strings[7]); + else printf("F8: N/A\r\n"); + if (hotkey_strings[8] != NULL) printf("F9: %s\r\n", hotkey_strings[8]); + else printf("F9: N/A\r\n"); + if (hotkey_strings[9] != NULL) printf("F10: %s\r\n", hotkey_strings[9]); + else printf("F10: N/A\r\n"); + if (hotkey_strings[10] != NULL) printf("F11: %s\r\n", hotkey_strings[10]); + else printf("F11: N/A\r\n"); + if (hotkey_strings[11] != NULL) printf("F12: %s\r\n", hotkey_strings[11]); + else printf("F12: N/A\r\n"); + + printf("\r\n"); + return 0; + + } + + if (fn_number < 1 || fn_number > 12) { + printf("Invalid FN-key number.\r\n"); + return 0; + } + + if (!mos_parseString(NULL, &hotkey_string)) { + + if (hotkey_strings[fn_number - 1] != NULL) { + free(hotkey_strings[fn_number - 1]); + hotkey_strings[fn_number - 1] = NULL; + printf("F%u cleared.\r\n", fn_number); + } else printf("No string hotkey provided.\r\n"); + + return 0; + + } + + //"key x " = 6 chars + //"key xx " = 7 chars + + if (fn_number < 10) { + hotkey_strings[fn_number - 1] = malloc((strlen(cmd_shadow) - strlen("key x ") + 1) * sizeof(char)); + strncpy(hotkey_strings[fn_number - 1], cmd_shadow + strlen("key x "), strlen(cmd_shadow) - strlen("key x ")); + hotkey_strings[fn_number - 1][strlen(cmd_shadow) - strlen("key x ")] = '\0'; + } else { + hotkey_strings[fn_number - 1] = malloc((strlen(cmd_shadow) - strlen("key xx ") + 1) * sizeof(char)); + strncpy(hotkey_strings[fn_number - 1], cmd_shadow + strlen("key xx "), strlen(cmd_shadow) - strlen("key xx ")); + hotkey_strings[fn_number - 1][strlen(cmd_shadow) - strlen("key xx ")] = '\0'; + } + + return 0; +} + // LOAD command // Parameters: // - ptr: Pointer to the argument string in the line edit buffer diff --git a/src/mos.h b/src/mos.h index 0a7f7dc..718af80 100644 --- a/src/mos.h +++ b/src/mos.h @@ -84,6 +84,7 @@ int mos_cmdTYPE(char *ptr); int mos_cmdCLS(char *ptr); int mos_cmdMOUNT(char *ptr); int mos_cmdHELP(char *ptr); +int mos_cmdKEY(char *ptr); UINT24 mos_LOAD(char * filename, UINT24 address, UINT24 size); UINT24 mos_SAVE(char * filename, UINT24 address, UINT24 size); @@ -193,6 +194,14 @@ UINT8 fat_EOF(FIL * fp); #define HELP_TYPE "Display the contents of a file on the screen\r\n" #define HELP_TYPE_ARGS "" +#define HELP_KEY "Store a command in one of 12 hotkey slots assigned to F1-F12\r\n\r\n" \ + "Optionally, the command string can include \"%s\" as a marker\r\n" \ + "in which case the hotkey command will be built either side.\r\n\r\n" \ + "KEY without any arguments will list the currently assigned\r\n" \ + "command strings.\r\n" + +#define HELP_KEY_ARGS " \r\n" + #define HELP_CLS "Clear the screen\r\n" #define HELP_MOUNT "(Re-)mount the MicroSD card\r\n" diff --git a/src/mos_editor.c b/src/mos_editor.c index 9d06c95..972766d 100644 --- a/src/mos_editor.c +++ b/src/mos_editor.c @@ -43,6 +43,8 @@ extern BYTE scrcols; // static char cmd_history[cmd_historyDepth][cmd_historyWidth + 1]; +char *hotkey_strings[12] = NULL; + // Get the current cursor position from the VPD // void getCursorPos() { @@ -240,6 +242,66 @@ UINT24 mos_EDITLINE(char * buffer, int bufferLength, UINT8 clear) { case 0x87: { // END insertPos = gotoEditLineEnd(insertPos, len); } break; + + case 0x9F: //F1 + case 0xA0: //F2 + case 0xA1: //F3 + case 0xA2: //F4 + case 0xA3: //F5 + case 0xA4: //F6 + case 0xA5: //F7 + case 0xA6: //F8 + case 0xA7: //F9 + case 0xA8: //F10 + case 0xA9: //F11 + case 0xAA: //F12 + { + if (hotkey_strings[keyc - 159] != NULL) { + + char *wildcardPos = strstr(hotkey_strings[keyc - 159], "%s"); + + if (wildcardPos == NULL) { //No wildcard in the hotkey string + + removeEditLine(buffer, insertPos, len); + strcpy(buffer, hotkey_strings[keyc - 159]); + printf("%s", buffer); + len = strlen(buffer); + insertPos = len; + keya = 0x0D; + } else { + + UINT8 prefixLength = wildcardPos - hotkey_strings[keyc - 159]; + UINT8 replacementLength = strlen(buffer); + UINT8 suffixLength = strlen(wildcardPos + 2); + char *result; + + if (prefixLength + replacementLength + suffixLength + 1 >= bufferLength) { + break; // Exceeds max command length (256 chars) + } + + result = malloc(prefixLength + replacementLength + suffixLength + 1); // +1 for null terminator + + strncpy(result, hotkey_strings[keyc - 159], prefixLength); //Copy the portion preceding the wildcard to the buffer + result[prefixLength] = '\0'; //Terminate + + strcat(result, buffer); + strcat(result, wildcardPos + 2); + + removeEditLine(buffer, insertPos, len); + strcpy(buffer, result); + printf("%s", buffer); + len = strlen(buffer); + insertPos = len; + + keya = 0x0D; + + free(result); + + } + + } else break; + } + // // Now the ASCII keys // diff --git a/src/mos_editor.h b/src/mos_editor.h index dd6eace..61ba801 100644 --- a/src/mos_editor.h +++ b/src/mos_editor.h @@ -17,4 +17,6 @@ UINT24 mos_EDITLINE(char * filename, int bufferLength, UINT8 clear); +extern char *hotkey_strings[12]; + #endif MOS_EDITOR_H \ No newline at end of file From 8cddb160eca512ab07b373e46aa912d77c50fd7e Mon Sep 17 00:00:00 2001 From: HeathenUK <65458864+HeathenUK@users.noreply.github.com> Date: Tue, 21 Nov 2023 18:24:48 +0000 Subject: [PATCH 2/4] Re-roll hotkey status into a loop Co-authored-by: Steve Sims --- src/mos.c | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/src/mos.c b/src/mos.c index 2b97c15..8bbd31f 100644 --- a/src/mos.c +++ b/src/mos.c @@ -430,30 +430,9 @@ int mos_cmdKEY(char *ptr) { printf("Hotkey assignments:\r\n\r\n"); - if (hotkey_strings[0] != NULL) printf("F1: %s\r\n", hotkey_strings[0]); - else printf("F1: N/A\r\n"); - if (hotkey_strings[1] != NULL) printf("F2: %s\r\n", hotkey_strings[1]); - else printf("F2: N/A\r\n"); - if (hotkey_strings[2] != NULL) printf("F3: %s\r\n", hotkey_strings[2]); - else printf("F3: N/A\r\n"); - if (hotkey_strings[3] != NULL) printf("F4: %s\r\n", hotkey_strings[3]); - else printf("F4: N/A\r\n"); - if (hotkey_strings[4] != NULL) printf("F5: %s\r\n", hotkey_strings[4]); - else printf("F5: N/A\r\n"); - if (hotkey_strings[5] != NULL) printf("F6: %s\r\n", hotkey_strings[5]); - else printf("F6: N/A\r\n"); - if (hotkey_strings[6] != NULL) printf("F7: %s\r\n", hotkey_strings[6]); - else printf("F7: N/A\r\n"); - if (hotkey_strings[7] != NULL) printf("F8: %s\r\n", hotkey_strings[7]); - else printf("F8: N/A\r\n"); - if (hotkey_strings[8] != NULL) printf("F9: %s\r\n", hotkey_strings[8]); - else printf("F9: N/A\r\n"); - if (hotkey_strings[9] != NULL) printf("F10: %s\r\n", hotkey_strings[9]); - else printf("F10: N/A\r\n"); - if (hotkey_strings[10] != NULL) printf("F11: %s\r\n", hotkey_strings[10]); - else printf("F11: N/A\r\n"); - if (hotkey_strings[11] != NULL) printf("F12: %s\r\n", hotkey_strings[11]); - else printf("F12: N/A\r\n"); + for (int key = 0; key < 12; key++) { + printf("F%d: %s\r\n", key+1, hotkey_strings[key] == NULL ? "N/A" : hotkey_strings[key]); + } printf("\r\n"); return 0; From 6d0a56af4cb15cd09ecbcf48fa581b50cce1eb19 Mon Sep 17 00:00:00 2001 From: HeathenUK Date: Tue, 21 Nov 2023 18:30:48 +0000 Subject: [PATCH 3/4] Adapt to ZDS ancient C requirements for declarations. --- src/mos.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mos.c b/src/mos.c index 8bbd31f..ce3c94a 100644 --- a/src/mos.c +++ b/src/mos.c @@ -428,9 +428,10 @@ int mos_cmdKEY(char *ptr) { if (!mos_parseNumber(NULL, &fn_number)) { + UINT8 key; printf("Hotkey assignments:\r\n\r\n"); - for (int key = 0; key < 12; key++) { + for (key = 0; key < 12; key++) { printf("F%d: %s\r\n", key+1, hotkey_strings[key] == NULL ? "N/A" : hotkey_strings[key]); } From 8fb216c3d95a99755a4b24592c2ab22b6d60cbc4 Mon Sep 17 00:00:00 2001 From: HeathenUK Date: Sat, 2 Dec 2023 13:56:19 +0000 Subject: [PATCH 4/4] Switch to global mos_strtok, eliminating shadow edit line (saves 256 bytes and simplifies), strip quotes if both leading and following are found.) --- src/mos.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/src/mos.c b/src/mos.c index ce3c94a..e16e4cd 100644 --- a/src/mos.c +++ b/src/mos.c @@ -62,8 +62,6 @@ extern volatile BYTE keyascii; // In globals.asm extern volatile BYTE vpd_protocol_flags; // In globals.asm extern BYTE rtc; // In globals.asm -char cmd_shadow[256]; // Will hold preserved edit line - static FATFS fs; // Handle for the file system static char * mos_strtok_ptr; // Pointer for current position in string tokeniser @@ -339,10 +337,7 @@ int mos_exec(char * buffer) { char path[256]; UINT8 mode; - ptr = mos_trim(buffer); - - strcpy (cmd_shadow, ptr); - + ptr = mos_trim(buffer); ptr = mos_strtok(ptr, " "); if(ptr != NULL) { func = mos_getCommand(ptr); @@ -424,7 +419,6 @@ int mos_cmdKEY(char *ptr) { UINT24 fn_number = 0; char *hotkey_string; - char *hotkey_string_token; if (!mos_parseNumber(NULL, &fn_number)) { @@ -445,31 +439,34 @@ int mos_cmdKEY(char *ptr) { return 0; } - if (!mos_parseString(NULL, &hotkey_string)) { - + if (strlen(mos_strtok_ptr) < 1) { + if (hotkey_strings[fn_number - 1] != NULL) { free(hotkey_strings[fn_number - 1]); hotkey_strings[fn_number - 1] = NULL; printf("F%u cleared.\r\n", fn_number); - } else printf("No string hotkey provided.\r\n"); - + } else printf("F%u already clear, no hotkey command provided.\r\n", fn_number); + return 0; - + } - - //"key x " = 6 chars - //"key xx " = 7 chars - - if (fn_number < 10) { - hotkey_strings[fn_number - 1] = malloc((strlen(cmd_shadow) - strlen("key x ") + 1) * sizeof(char)); - strncpy(hotkey_strings[fn_number - 1], cmd_shadow + strlen("key x "), strlen(cmd_shadow) - strlen("key x ")); - hotkey_strings[fn_number - 1][strlen(cmd_shadow) - strlen("key x ")] = '\0'; - } else { - hotkey_strings[fn_number - 1] = malloc((strlen(cmd_shadow) - strlen("key xx ") + 1) * sizeof(char)); - strncpy(hotkey_strings[fn_number - 1], cmd_shadow + strlen("key xx "), strlen(cmd_shadow) - strlen("key xx ")); - hotkey_strings[fn_number - 1][strlen(cmd_shadow) - strlen("key xx ")] = '\0'; + + + if (mos_strtok_ptr[0] == '\"' && mos_strtok_ptr[strlen(mos_strtok_ptr) - 1] == '\"') { + + mos_strtok_ptr[strlen(mos_strtok_ptr) - 1] = '\0'; + mos_strtok_ptr++; + } + if (hotkey_strings[fn_number - 1] != NULL) free(hotkey_strings[fn_number - 1]); + + hotkey_strings[fn_number - 1] = malloc((strlen(mos_strtok_ptr) + 1) * sizeof(char)); + + strncpy(hotkey_strings[fn_number - 1], mos_strtok_ptr, strlen(mos_strtok_ptr)); + + hotkey_strings[fn_number - 1][strlen(mos_strtok_ptr)] = '\0'; + return 0; }