diff --git a/CommandLineArgs.cs b/CommandLineArgs.cs index 3fd7313..60b3c51 100644 --- a/CommandLineArgs.cs +++ b/CommandLineArgs.cs @@ -88,12 +88,12 @@ public void AddArguments(IEnumerable args) string argKey = argText[..equalsIndex]; if (!keyValueOptions.TryAdd(argKey, argText[(equalsIndex + 1)..]) && PrintWarnings) { - PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Exists_KeyValue, argKey)); + Program.PrintWarning(Strings.CommandLineArgs_Warning_Exists_KeyValue, argKey); } } else if (!multiCharacterOptions.Add(argText) && PrintWarnings) { - PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Exists_MultiCharacter, argText)); + Program.PrintWarning(Strings.CommandLineArgs_Warning_Exists_MultiCharacter, argText); } } else @@ -102,7 +102,7 @@ public void AddArguments(IEnumerable args) { if (!singleCharacterOptions.Add(c) && PrintWarnings) { - PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Exists_SingleCharacter, c)); + Program.PrintWarning(Strings.CommandLineArgs_Warning_Exists_SingleCharacter, c); } } } @@ -170,15 +170,15 @@ public void WarnUnconsumedOptions() { foreach (char singleArgument in singleCharacterOptions.Except(consumedSingleCharacterOptions)) { - PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Unconsumed_SingleCharacter, singleArgument)); + Program.PrintWarning(Strings.CommandLineArgs_Warning_Unconsumed_SingleCharacter, singleArgument); } foreach (string multiArgument in multiCharacterOptions.Except(consumedMultiCharacterOptions)) { - PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Unconsumed_MultiCharacter, multiArgument)); + Program.PrintWarning(Strings.CommandLineArgs_Warning_Unconsumed_MultiCharacter, multiArgument); } foreach (string key in keyValueOptions.Keys.Except(consumedKeyValueOptions)) { - PrintWarning(string.Format(Strings.CommandLineArgs_Warning_Unconsumed_KeyValue, key)); + Program.PrintWarning(Strings.CommandLineArgs_Warning_Unconsumed_KeyValue, key); } } @@ -194,17 +194,10 @@ public void WarnUnconsumedOptions(int maxPositionalArgsLength) if (positionalArgs.Count > maxPositionalArgsLength) { int excess = positionalArgs.Count - maxPositionalArgsLength; - PrintWarning(excess == 1 - ? string.Format(Strings.CommandLineArgs_Warning_Unconsumed_Positional_Single, excess) - : string.Format(Strings.CommandLineArgs_Warning_Unconsumed_Positional_Multiple, excess)); + Program.PrintWarning(excess == 1 + ? Strings.CommandLineArgs_Warning_Unconsumed_Positional_Single + : Strings.CommandLineArgs_Warning_Unconsumed_Positional_Multiple, excess); } } - - private static void PrintWarning([Localizable(true)] string warningText) - { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(warningText); - Console.ResetColor(); - } } } diff --git a/Debugger.cs b/Debugger.cs index 87e3487..a21749f 100644 --- a/Debugger.cs +++ b/Debugger.cs @@ -72,9 +72,7 @@ public void LoadDebugFile(string debugFilePath) } catch (Exception exc) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_Debug_Info_File, exc.GetType().Name, exc.Message); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_Debug_Info_File, exc.GetType().Name, exc.Message); #if DEBUG throw; #endif @@ -305,18 +303,14 @@ public void StartDebugger() CommandDebugHelp(); break; default: - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Unrecognised_Command, command[0]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Unrecognised_Command, command[0]); break; } } } if (DebuggingProcessor.Execute(false) && !InReplMode) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_HLT_Reached); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_HLT_Reached); Console.Write(Strings.Debugger_Any_Key_Continue); _ = Console.ReadKey(true); Console.WriteLine(); @@ -352,23 +346,17 @@ private void CommandReadMemory(IReadOnlyList command) ? 2 : command[1] == "dword" ? 4 : command[1] == "qword" ? 8 : 0U; if (bytesToRead == 0) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Size, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Size, command[1]); return; } if (!ulong.TryParse(command[2], out ulong address)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Address, command[2]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Address, command[2]); return; } if (address + bytesToRead > (ulong)DebuggingProcessor.Memory.LongLength) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_OutOfRange_Address, command[2]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_OutOfRange_Address, command[2]); return; } ulong value = 0; @@ -380,9 +368,7 @@ private void CommandReadMemory(IReadOnlyList command) } else { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_2); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_2); } } @@ -396,23 +382,17 @@ private void CommandWriteMemReg(IReadOnlyList command) { if (!ulong.TryParse(command[2], out ulong address)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Address, command[2]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Address, command[2]); return; } if (address >= (ulong)DebuggingProcessor.Memory.LongLength) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_OutOfRange_Address, command[2]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_OutOfRange_Address, command[2]); return; } if (!byte.TryParse(command[3], out byte value)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Byte_Value, command[3]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Byte_Value, command[3]); return; } DebuggingProcessor.Memory[address] = value; @@ -423,16 +403,12 @@ private void CommandWriteMemReg(IReadOnlyList command) { if (!Enum.TryParse(command[2], out Register register)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Register, command[2]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Register, command[2]); return; } if (!ulong.TryParse(command[3], out ulong value)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Register_Value, command[3]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Register_Value, command[3]); return; } DebuggingProcessor.Registers[(int)register] = value; @@ -440,17 +416,13 @@ private void CommandWriteMemReg(IReadOnlyList command) break; } default: - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Location, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Location, command[1]); break; } } else { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_3); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_3); } } @@ -462,9 +434,7 @@ private void CommandMapMemory(string[] command) { if (!ulong.TryParse(command[1], out offset)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Offset, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Offset, command[1]); return; } } @@ -472,17 +442,13 @@ private void CommandMapMemory(string[] command) { if (!ulong.TryParse(command[2], out limit)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Limit, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Limit, command[1]); return; } } if (command.Length is not 1 and > 3) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_0to2); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_0to2); return; } Console.ForegroundColor = ConsoleColor.Blue; @@ -597,32 +563,24 @@ private void CommandFormatStack(string[] command) { if (!ulong.TryParse(command[1], out limit)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Limit, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Limit, command[1]); return; } } else if (command.Length != 1) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_0to1); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_0to1); return; } if (DebuggingProcessor.Registers[(int)Register.rso] >= (ulong)DebuggingProcessor.Memory.LongLength) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_Stack_Empty); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_Stack_Empty); return; } if (DebuggingProcessor.Registers[(int)Register.rso] > DebuggingProcessor.Registers[(int)Register.rsb]) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_rso_GT_rsb); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_rso_GT_rsb); return; } @@ -704,9 +662,7 @@ private void CommandFormatStack(string[] command) } else { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_Stack_Bottom); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_Stack_Bottom); } } @@ -714,16 +670,12 @@ private static void CommandDecimalToHexadecimal(string[] command) { if (command.Length != 2) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_1); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_1); return; } if (!ulong.TryParse(command[1], out ulong decValue)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Convert_Value, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Convert_Value, command[1]); return; } Console.WriteLine(Strings.Debugger_Value_In_Hex, decValue, decValue); @@ -733,9 +685,7 @@ private static void CommandHexadecimalToDecimal(string[] command) { if (command.Length != 2) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_1); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_1); return; } try @@ -745,9 +695,7 @@ private static void CommandHexadecimalToDecimal(string[] command) } catch { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Convert_Value, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Convert_Value, command[1]); } } @@ -765,16 +713,12 @@ private void CommandBreakpointManage(string[] command) { if (!Enum.TryParse(command[2], out Register register)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Register, command[2]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Register, command[2]); return; } if (!ulong.TryParse(command[3], out ulong value)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Break_Value, command[3]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Break_Value, command[3]); return; } switch (command[1].ToLower()) @@ -787,17 +731,13 @@ private void CommandBreakpointManage(string[] command) } else { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_Breakpoint_Exists, register, value); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_Breakpoint_Exists, register, value); } break; case "remove": if (Breakpoints.RemoveAll(x => x.Register == register && x.Value == value) == 0) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.Debugger_Warning_Breakpoint_No_Matching); - Console.ResetColor(); + Program.PrintWarning(Strings.Debugger_Warning_Breakpoint_No_Matching); } else { @@ -805,17 +745,13 @@ private void CommandBreakpointManage(string[] command) } break; default: - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Invalid_Breakpoint_Action, command[1]); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Invalid_Breakpoint_Action, command[1]); break; } } else { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.Debugger_Error_Args_Required_Breakpoint); - Console.ResetColor(); + Program.PrintError(Strings.Debugger_Error_Args_Required_Breakpoint); } } diff --git a/Program.Shared.cs b/Program.Shared.cs index 625068d..2fafc9c 100644 --- a/Program.Shared.cs +++ b/Program.Shared.cs @@ -8,7 +8,7 @@ internal static partial class Program public static readonly ulong DefaultMemorySize = 8192; // 8KB // Shared methods that are used by multiple commands - public static AAPFile LoadAAPFile(string appPath, bool ignoreNewerVersion) + public static AAPFile? LoadAAPFile(string appPath, bool ignoreNewerVersion) { AAPFile file; try @@ -17,39 +17,28 @@ public static AAPFile LoadAAPFile(string appPath, bool ignoreNewerVersion) } catch { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_AAP); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Invalid_AAP); return null; } if ((file.Features & AAPFeatures.Incompatible) != 0) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_AAP_Feature_Incompatible); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_AAP_Feature_Incompatible); return null; } if (!ignoreNewerVersion && file.LanguageVersion > (version ?? new Version())) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(Strings.CLI_Warning_Newer_Build_Version, + PrintWarning(Strings.CLI_Warning_Newer_Build_Version, file.LanguageVersion.Major, file.LanguageVersion.Minor, file.LanguageVersion.Build, version?.Major, version?.Minor, version?.Build); - Console.ResetColor(); if (file.LanguageVersion.Major > version?.Major) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Newer_Major_Build_Version, file.LanguageVersion.Major, version.Major); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Newer_Major_Build_Version, file.LanguageVersion.Major, version.Major); return null; } } return file; } - public static Processor LoadExecutableToProcessor(string appPath, ulong memSize, + public static Processor? LoadExecutableToProcessor(string appPath, ulong memSize, bool useV1Format, bool useV1CallStack, bool ignoreNewerVersion, bool mapStack, bool autoEcho) { byte[] program; @@ -63,7 +52,11 @@ public static AAPFile LoadAAPFile(string appPath, bool ignoreNewerVersion) else #endif { - AAPFile file = LoadAAPFile(appPath, ignoreNewerVersion); + AAPFile? file = LoadAAPFile(appPath, ignoreNewerVersion); + if (file is null) + { + return null; + } processor = new Processor(memSize, entryPoint: file.EntryPoint, #if V1_CALL_STACK_COMPAT useV1CallStack: useV1CallStack || file.Features.HasFlag(AAPFeatures.V1CallStack), @@ -83,13 +76,9 @@ public static void LoadProgramIntoProcessor(Processor processor, byte[] program) } catch (Exception e) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Program_Load_Unexpected, e.GetType().Name, e.Message); - Console.ResetColor(); + PrintFatalError(Strings.CLI_Error_Program_Load_Unexpected, e.GetType().Name, e.Message); #if DEBUG throw; -#else - Environment.Exit(1); #endif } } @@ -98,18 +87,12 @@ public static bool CheckInputFileArg(string[] args, [Localizable(true)] string m { if (args.Length < 2) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(missingMessage); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(missingMessage); return false; } if (!File.Exists(args[1])) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_File_Not_Exists); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_File_Not_Exists); return false; } return true; @@ -130,10 +113,7 @@ public static ulong GetMemorySize(CommandLineArgs args) { if (!ulong.TryParse(memSizeString, out ulong memSize)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_Memory_Size, memSizeString); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Invalid_Memory_Size, memSizeString); return 0; } return memSize; @@ -147,10 +127,7 @@ public static int GetMacroLimit(CommandLineArgs args) { if (!int.TryParse(macroLimitString, out int macroLimit)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_Macro_Limit, macroLimitString); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Invalid_Macro_Limit, macroLimitString); return -1; } return macroLimit; @@ -164,10 +141,7 @@ public static int GetWhileLimit(CommandLineArgs args) { if (!int.TryParse(whileLimitString, out int whileLimit)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_While_Limit, whileLimitString); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Invalid_While_Limit, whileLimitString); return -1; } return whileLimit; @@ -186,10 +160,7 @@ public static List<(string Name, ulong Value)> GetVariableDefinitions(CommandLin ulong value = 0; if (split.Length == 2 && !ulong.TryParse(split[1], out value)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_Variable_Value, split[1]); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Invalid_Variable_Value, split[1]); return new List<(string Name, ulong Value)>(); } string name = split[0]; @@ -206,7 +177,6 @@ public static List<(string Name, ulong Value)> GetVariableDefinitions(CommandLin public static void OnExecutionException(Exception e, Processor processor) { - Console.ForegroundColor = ConsoleColor.Red; if (e is IndexOutOfRangeException or ArgumentOutOfRangeException or RuntimeException or DivideByZeroException or FileNotFoundException or DirectoryNotFoundException) { @@ -217,12 +187,13 @@ public static void OnExecutionException(Exception e, Processor processor) FileNotFoundException or DirectoryNotFoundException => e.Message, _ => Strings.CLI_Error_Runtime_Invalid_Address }; - Console.WriteLine(Strings.CLI_Error_Runtime_Known, message); + PrintError(Strings.CLI_Error_Runtime_Known, message); } else { - Console.WriteLine(Strings.CLI_Error_Unexpected_With_Type, e.GetType().Name, e.Message); + PrintError(Strings.CLI_Error_Unexpected_With_Type, e.GetType().Name, e.Message); } + Console.ForegroundColor = ConsoleColor.Red; PrintRegisterStates(processor); Console.ResetColor(); } @@ -233,24 +204,22 @@ public static void ExecuteProcessor(Processor processor) { _ = processor.Execute(true); - Console.ForegroundColor = ConsoleColor.DarkYellow; if (processor.IsFileOpen) { - Console.WriteLine(Strings.CLI_Warning_Processor_Exit_File_Open); + PrintWarning(Strings.CLI_Warning_Processor_Exit_File_Open); } #if EXTENSION_SET_EXTERNAL_ASM if (processor.IsExternalOpen) { - Console.WriteLine(Strings.CLI_Warning_Processor_Exit_External_Open); + PrintWarning(Strings.CLI_Warning_Processor_Exit_External_Open); } #endif #if EXTENSION_SET_HEAP_ALLOCATE if (processor.AnyRegionsMapped) { - Console.WriteLine(Strings.CLI_Warning_Processor_Exit_Region_Mapped, processor.MappedMemoryRanges.Count - 2); + PrintWarning(Strings.CLI_Warning_Processor_Exit_Region_Mapped, processor.MappedMemoryRanges.Count - 2); } #endif - Console.ResetColor(); } catch (Exception e) { @@ -266,16 +235,14 @@ public static void ExecuteProcessor(Processor processor) public static void OnAssemblerException(Exception e) { - Console.ForegroundColor = ConsoleColor.Red; if (e is AssemblerException assemblerException) { - Console.WriteLine(assemblerException.ConsoleMessage); + PrintError(assemblerException.ConsoleMessage); } else { - Console.WriteLine(Strings.CLI_Error_Unexpected_With_Type, e.GetType().Name, e.Message); + PrintError(Strings.CLI_Error_Unexpected_With_Type, e.GetType().Name, e.Message); } - Console.ResetColor(); } public static void PrintRegisterStates(Processor processor) @@ -314,5 +281,30 @@ public static void PrintRegisterStates(Processor processor) Console.WriteLine(); } } + + public static void PrintFatalError([Localizable(true)] string errorText, + [Localizable(true)] params object?[]? formatParams) + { + PrintError(errorText, formatParams); +#if !DEBUG + Environment.Exit(1); +#endif + } + + public static void PrintError([Localizable(true)] string errorText, + [Localizable(true)] params object?[]? formatParams) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine(errorText, formatParams); + Console.ResetColor(); + } + + public static void PrintWarning([Localizable(true)] string warningText, + [Localizable(true)] params object?[]? formatParams) + { + Console.ForegroundColor = ConsoleColor.DarkYellow; + Console.WriteLine(warningText, formatParams); + Console.ResetColor(); + } } } diff --git a/Program.cs b/Program.cs index 3e70425..81073d7 100644 --- a/Program.cs +++ b/Program.cs @@ -40,11 +40,8 @@ private static void Main(string[] args) } if (args.Length < 1) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Missing_Operation_Body); - Console.WriteLine(Strings.CLI_Error_Missing_Operation_Hint); - Console.ResetColor(); - Environment.Exit(1); + PrintError(Strings.CLI_Error_Missing_Operation_Body); + PrintFatalError(Strings.CLI_Error_Missing_Operation_Hint); return; } switch (args[0].ToLowerInvariant()) @@ -77,10 +74,7 @@ private static void Main(string[] args) DisplayLicense(processedArgs); break; default: - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_Operation, args[0]); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Error_Invalid_Operation, args[0]); return; } } @@ -106,10 +100,7 @@ private static void AssembleSourceFile(CommandLineArgs args) { if (!int.TryParse(codeString, out int errorCode)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Assemble_Error_Invalid_Error_Code, codeString); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Assemble_Error_Invalid_Error_Code, codeString); return; } _ = disabledErrors.Add(errorCode); @@ -121,10 +112,7 @@ private static void AssembleSourceFile(CommandLineArgs args) { if (!int.TryParse(codeString, out int errorCode)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Assemble_Error_Invalid_Warning_Code, codeString); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Assemble_Error_Invalid_Warning_Code, codeString); return; } _ = disabledWarnings.Add(errorCode); @@ -136,10 +124,7 @@ private static void AssembleSourceFile(CommandLineArgs args) { if (!int.TryParse(codeString, out int errorCode)) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Assemble_Error_Invalid_Suggestion_Code, codeString); - Console.ResetColor(); - Environment.Exit(1); + PrintFatalError(Strings.CLI_Assemble_Error_Invalid_Suggestion_Code, codeString); return; } _ = disabledSuggestions.Add(errorCode); @@ -267,14 +252,10 @@ private static void AssembleSourceFile(CommandLineArgs args) OnAssemblerException(e); Console.Write(Strings.CLI_Assemble_Result_Header_Start); - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Assemble_Result_Header_Failed); - Console.ResetColor(); - + PrintFatalError(Strings.CLI_Assemble_Result_Header_Failed); #if DEBUG throw; #else - Environment.Exit(1); return; #endif } @@ -391,7 +372,7 @@ private static void ExecuteProgram(CommandLineArgs args) ulong memSize = GetMemorySize(args); - Processor processor = LoadExecutableToProcessor(appPath, memSize, + Processor? processor = LoadExecutableToProcessor(appPath, memSize, #if V1_CALL_STACK_COMPAT args.IsOptionGiven('1', "v1-format"), args.IsMultiCharacterOptionGiven("v1-call-stack"), @@ -402,6 +383,11 @@ private static void ExecuteProgram(CommandLineArgs args) !args.IsOptionGiven('u', "unmapped-stack"), args.IsOptionGiven('a', "auto-echo")); + if (processor is null) + { + return; + } + args.WarnUnconsumedOptions(2); ExecuteProcessor(processor); @@ -482,7 +468,7 @@ private static void RunDebugger(CommandLineArgs args) ulong memSize = GetMemorySize(args); - Processor processor = LoadExecutableToProcessor(appPath, memSize, + Processor? processor = LoadExecutableToProcessor(appPath, memSize, #if V1_CALL_STACK_COMPAT args.IsOptionGiven('1', "v1-format"), args.IsMultiCharacterOptionGiven("v1-call-stack"), @@ -493,6 +479,11 @@ private static void RunDebugger(CommandLineArgs args) !args.IsOptionGiven('u', "unmapped-stack"), args.IsOptionGiven('a', "auto-echo")); + if (processor is null) + { + return; + } + Debugger debugger = new(false, processor); if (positionalArgs.Length >= 3) { @@ -525,8 +516,12 @@ private static void PerformDisassembly(CommandLineArgs args) else #endif { - AAPFile file = LoadAAPFile(sourcePath, + AAPFile? file = LoadAAPFile(sourcePath, args.IsOptionGiven('i', "ignore-newer-version")); + if (file is null) + { + return; + } program = file.Program; } @@ -544,13 +539,10 @@ private static void PerformDisassembly(CommandLineArgs args) } catch (Exception e) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Disassemble_Error_Unexpected, e.GetType().Name, e.Message); - Console.ResetColor(); + PrintFatalError(Strings.CLI_Disassemble_Error_Unexpected, e.GetType().Name, e.Message); #if DEBUG throw; #else - Environment.Exit(1); return; #endif } @@ -678,9 +670,7 @@ private static void DisplayHelp(CommandLineArgs args) PrintOperationHelp("help", Strings.CLI_Help_Description_Help, Strings.CLI_Help_Options_Help); break; default: - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_Error_Invalid_Operation, positionalArgs[1]); - Console.ResetColor(); + PrintError(Strings.CLI_Error_Invalid_Operation, positionalArgs[1]); break; } } @@ -711,13 +701,10 @@ private static void DisplayLicense(CommandLineArgs args) } catch { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(Strings.CLI_License_Error); - Console.ResetColor(); + PrintFatalError(Strings.CLI_License_Error); #if DEBUG throw; #else - Environment.Exit(1); return; #endif }