public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
From: Jens Remus <jremus@linux.ibm.com>
To: binutils@sourceware.org
Cc: Jens Remus <jremus@linux.ibm.com>,
	Andreas Krebbel <krebbel@linux.ibm.com>,
	Nick Clifton <nickc@redhat.com>
Subject: [PATCH v2 4/7] s390: Enhance error handling in s390-mkopc
Date: Fri, 15 Dec 2023 15:36:13 +0100	[thread overview]
Message-ID: <20231215143616.820185-5-jremus@linux.ibm.com> (raw)
In-Reply-To: <20231215143616.820185-1-jremus@linux.ibm.com>

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.

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.

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.

opcodes/
	* s390-mkopc.c: Enhance error handling. Return EXIT_FAILURE
	  in case of an error, otherwise EXIT_SUCCESS.

Signed-off-by: Jens Remus <jremus@linux.ibm.com>
Reviewed-by: Andreas Krebbel <krebbel@linux.ibm.com>
---
 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 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include "opcode/s390.h"
 
+/* Return code.  */
+int return_code = 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 = EXIT_FAILURE;
+}
+
 struct op_struct
   {
     char  opcode[16];
@@ -223,8 +241,7 @@ insertExpandedMnemonic (char *opcode, char *mnemonic, char *format,
 
   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 boundary\n", mnemonic);
       return;
     }
 
@@ -257,7 +274,7 @@ insertExpandedMnemonic (char *opcode, char *mnemonic, char *format,
   return;
 
  malformed_mnemonic:
-  fprintf (stderr, "Malformed mnemonic: %s\n", mnemonic);
+  print_error ("Malformed mnemonic: %s\n", mnemonic);
 }
 
 static const char file_header[] =
@@ -343,8 +360,8 @@ main (void)
 		cpu_string, modes_string, flags_string);
       if (num_matched != 6 && num_matched != 7)
 	{
-	  fprintf (stderr, "Couldn't scan line %s\n", currentLine);
-	  exit (1);
+	  print_error ("Couldn't scan line %s\n", currentLine);
+	  exit (EXIT_FAILURE);
 	}
 
       if (strcmp (cpu_string, "g5") == 0
@@ -385,8 +402,9 @@ main (void)
 	       || strcmp (cpu_string, "arch14") == 0)
 	min_cpu = 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;
       }
 
       str = modes_string;
@@ -401,9 +419,9 @@ main (void)
 	  mode_bits |= 1 << S390_OPCODE_ZARCH;
 	  str += 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 == ',')
 	  str++;
@@ -444,17 +462,20 @@ main (void)
 	      flag_bits |= S390_INSTR_FLAGS_CLASS_JUMPSR;
 	      str += 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 == ',')
 	      str++;
 	  } while (*str != 0);
 	}
       insertExpandedMnemonic (opcode, mnemonic, format, min_cpu, mode_bits, flag_bits);
+
+ continue_loop:
+      ;
     }
 
   dumpTable ();
-  return 0;
+  return return_code;
 }
-- 
2.40.1


  parent reply	other threads:[~2023-12-15 14:36 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-15 14:36 [PATCH v2 0/7] s390: Optionally print instruction description in disassembly Jens Remus
2023-12-15 14:36 ` [PATCH v2 1/7] s390: Fix build when using EXEEXT_FOR_BUILD Jens Remus
2023-12-15 14:36 ` [PATCH v2 2/7] s390: Align letter case of instruction descriptions Jens Remus
2023-12-15 14:36 ` [PATCH v2 3/7] s390: Provide IBM z16 (arch14) " Jens Remus
2023-12-15 14:36 ` Jens Remus [this message]
2023-12-15 14:36 ` [PATCH v2 5/7] s390: Use safe string functions and length macros in s390-mkopc Jens Remus
2023-12-15 14:36 ` [PATCH v2 6/7] s390: Optionally print instruction description in disassembly Jens Remus
2023-12-15 14:36 ` [PATCH v2 7/7] s390: Add suffix to conditional branch instruction descriptions Jens Remus
2023-12-18 12:14 ` [PATCH v2 0/7] s390: Optionally print instruction description in disassembly Nick Clifton
2023-12-20 10:56 ` Andreas Krebbel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231215143616.820185-5-jremus@linux.ibm.com \
    --to=jremus@linux.ibm.com \
    --cc=binutils@sourceware.org \
    --cc=krebbel@linux.ibm.com \
    --cc=nickc@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).