diff tools/data2inc.c @ 2565:d56a0e86067a

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 11:49:58 +0200
parents c6ee41fd98dd
children d75431bf1a7d
line wrap: on
line diff
--- a/tools/data2inc.c	Mon Feb 28 10:18:38 2022 +0200
+++ b/tools/data2inc.c	Mon Feb 28 11:49:58 2022 +0200
@@ -481,7 +481,7 @@
     void *ctx = NULL;
     size_t dataSize;
     off_t totalSize;
-    int res;
+    int res = DMERR_OK;
 
     // Initialize
     dmInitProg("data2inc", "Data to include file converter", "0.7", NULL, NULL);
@@ -490,22 +490,24 @@
     // Parse arguments
     if (!dmArgsProcess(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+        goto out;
 
     // Determine output type, if not specified
     if (setFormat == NULL)
     {
         if (optOutFilename == NULL)
         {
-            dmErrorMsg("Output format not specified and no output filename given (try --help)\n");
-            goto exit;
+            argShowHelp();
+            res = dmError(DMERR_INVALID_ARGS,
+                "Output format not specified and no output filename given.\n");
+            goto out;
         }
 
         if ((setFormat = dmGuessFormatFromName(optOutFilename)) == NULL)
         {
             dmErrorMsg("Could not guess output format from filename '%s'.\n",
                 optOutFilename);
-            goto exit;
+            goto out;
         }
 
         dmMsg(0, "Guessed output format: %s (%s)\n",
@@ -525,7 +527,7 @@
         res = dmGetErrno();
         dmErrorMsg("Error opening input file '%s'. (%s)\n",
             optInFilename, dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     if (optOutFilename == NULL)
@@ -536,7 +538,7 @@
         res = dmGetErrno();
         dmErrorMsg("Error creating output file '%s'. (%s)\n",
             optOutFilename, dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     // Allocate linebuffer
@@ -545,7 +547,7 @@
     {
         dmErrorMsg("Could not allocate %" DM_PRIu_SIZE_T " byte buffer.\n",
             dataSize);
-        goto exit;
+        goto out;
     }
 
     // Get sourcefile size
@@ -558,7 +560,7 @@
         dmErrorMsg("Error initializing format %s (%s) context: %s\n",
             setFormat->name, setFormat->desc,
             dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     // Output header
@@ -567,7 +569,7 @@
     {
         dmErrorMsg("Error writing output header: %s\n",
             dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     if (optAddLine)
@@ -579,7 +581,7 @@
     {
         dmErrorMsg("Error writing output declaration: %s\n",
             dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
     // Output data
@@ -591,7 +593,7 @@
         {
             dmErrorMsg("Error writing output data: %s\n",
                 dmErrorStr(res));
-            goto exit;
+            goto out;
         }
     }
 
@@ -601,10 +603,10 @@
     {
         dmErrorMsg("Error writing output footer: %s\n",
             dmErrorStr(res));
-        goto exit;
+        goto out;
     }
 
-exit:
+out:
     // Cleanup
     if (inFile != NULL)
         fclose(inFile);
@@ -623,5 +625,5 @@
             dmErrorStr(res));
     }
 
-    return 0;
+    return res;
 }