diff --git a/src/mos.c b/src/mos.c index ddc74b9..e16e4cd 100644 --- a/src/mos.c +++ b/src/mos.c @@ -94,6 +94,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)) @@ -336,7 +337,7 @@ int mos_exec(char * buffer) { char path[256]; UINT8 mode; - ptr = mos_trim(buffer); + ptr = mos_trim(buffer); ptr = mos_strtok(ptr, " "); if(ptr != NULL) { func = mos_getCommand(ptr); @@ -408,6 +409,67 @@ 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; + + if (!mos_parseNumber(NULL, &fn_number)) { + + UINT8 key; + printf("Hotkey assignments:\r\n\r\n"); + + for (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; + + } + + if (fn_number < 1 || fn_number > 12) { + printf("Invalid FN-key number.\r\n"); + return 0; + } + + 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("F%u already clear, no hotkey command provided.\r\n", fn_number); + + return 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; +} + // 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