From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1448) id 925CC3858C2C; Wed, 20 Dec 2023 10:53:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 925CC3858C2C Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Andreas Krebbel To: bfd-cvs@sourceware.org Subject: [binutils-gdb] s390: Enhance error handling in s390-mkopc X-Act-Checkin: binutils-gdb X-Git-Author: Jens Remus X-Git-Refname: refs/heads/master X-Git-Oldrev: 2ff609b4ce8f3142b4e5592116f28c83a07066c3 X-Git-Newrev: 730e7ddc243edff2755548c89dedac7293e5a5f6 Message-Id: <20231220105316.925CC3858C2C@sourceware.org> Date: Wed, 20 Dec 2023 10:53:16 +0000 (GMT) X-BeenThere: binutils-cvs@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Dec 2023 10:53:16 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D730e7ddc243e= dff2755548c89dedac7293e5a5f6 commit 730e7ddc243edff2755548c89dedac7293e5a5f6 Author: Jens Remus Date: Wed Dec 20 11:17:11 2023 +0100 s390: Enhance error handling in s390-mkopc =20 When the s390-mkopc utility detects an error it prints an error message to strerr and either continues processing or exists with a non-zero return code. If it continues without detecting any further error the final return code was zero, potentially hiding the detected error. =20 Introduce a global variable to hold the final return code and initialize it to EXIT_SUCCESS. Introduce a helper function print_error() that prints an error message to stderr and sets the final return code to EXIT_FAILURE. Use it to print all error messages. Return the final return code at the end of the processing. =20 While at it enhance error messages to state more clearly which mnemonic an error was detected for. Also continue processing for cases where subsequent mnemonics can be processed. =20 opcodes/ * s390-mkopc.c: Enhance error handling. Return EXIT_FAILURE in case of an error, otherwise EXIT_SUCCESS. =20 Signed-off-by: Jens Remus Reviewed-by: Andreas Krebbel Diff: --- opcodes/s390-mkopc.c | 49 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/opcodes/s390-mkopc.c b/opcodes/s390-mkopc.c index 5f921ee0628..aad093f18ff 100644 --- a/opcodes/s390-mkopc.c +++ b/opcodes/s390-mkopc.c @@ -21,9 +21,27 @@ =20 #include #include +#include #include #include "opcode/s390.h" =20 +/* Return code. */ +int return_code =3D EXIT_SUCCESS; + +/* Helper to print an error message and set the return code. */ +static void __attribute__ ((format (printf, 1, 2))) +print_error (const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fprintf(stderr, "Error: "); + vfprintf(stderr, fmt, ap); + va_end(ap); + + return_code =3D EXIT_FAILURE; +} + struct op_struct { char opcode[16]; @@ -223,8 +241,7 @@ insertExpandedMnemonic (char *opcode, char *mnemonic, c= har *format, =20 if (mask_start & 3) { - fprintf (stderr, "Conditional mask not at nibble boundary in: %s\n", - mnemonic); + print_error ("Mnemonic \"%s\": Conditional mask not at nibble bounda= ry\n", mnemonic); return; } =20 @@ -257,7 +274,7 @@ insertExpandedMnemonic (char *opcode, char *mnemonic, c= har *format, return; =20 malformed_mnemonic: - fprintf (stderr, "Malformed mnemonic: %s\n", mnemonic); + print_error ("Malformed mnemonic: %s\n", mnemonic); } =20 static const char file_header[] =3D @@ -343,8 +360,8 @@ main (void) cpu_string, modes_string, flags_string); if (num_matched !=3D 6 && num_matched !=3D 7) { - fprintf (stderr, "Couldn't scan line %s\n", currentLine); - exit (1); + print_error ("Couldn't scan line %s\n", currentLine); + exit (EXIT_FAILURE); } =20 if (strcmp (cpu_string, "g5") =3D=3D 0 @@ -385,8 +402,9 @@ main (void) || strcmp (cpu_string, "arch14") =3D=3D 0) min_cpu =3D S390_OPCODE_ARCH14; else { - fprintf (stderr, "Couldn't parse cpu string %s\n", cpu_string); - exit (1); + print_error ("Mnemonic \"%s\": Couldn't parse CPU string: %s\n", + mnemonic, cpu_string); + goto continue_loop; } =20 str =3D modes_string; @@ -401,9 +419,9 @@ main (void) mode_bits |=3D 1 << S390_OPCODE_ZARCH; str +=3D 5; } else { - fprintf (stderr, "Couldn't parse modes string %s\n", - modes_string); - exit (1); + print_error ("Mnemonic \"%s\": Couldn't parse modes string: %s\n", + mnemonic, modes_string); + goto continue_loop; } if (*str =3D=3D ',') str++; @@ -444,17 +462,20 @@ main (void) flag_bits |=3D S390_INSTR_FLAGS_CLASS_JUMPSR; str +=3D 6; } else { - fprintf (stderr, "Couldn't parse flags string %s\n", - flags_string); - exit (1); + print_error ("Mnemonic \"%s\": Couldn't parse flags string: %s\n", + mnemonic, flags_string); + goto continue_loop; } if (*str =3D=3D ',') str++; } while (*str !=3D 0); } insertExpandedMnemonic (opcode, mnemonic, format, min_cpu, mode_bits= , flag_bits); + + continue_loop: + ; } =20 dumpTable (); - return 0; + return return_code; }