changeset 2537:e96e757ab01e

Make program return values handling a bit more consistent.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 26 Dec 2023 02:42:39 +0200
parents a23fba3fa943
children 2198c5e6dd62
files src/colormap.c src/combine.c src/diffmap.c src/map2ppm.c src/mapsearch.c src/mapstats.c src/mkcitymap.c src/mkloc.c src/patchmap.c src/stitchmap.c
diffstat 10 files changed, 116 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/src/colormap.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/colormap.c	Tue Dec 26 02:42:39 2023 +0200
@@ -737,6 +737,7 @@
  */
 int main(int argc, char *argv[])
 {
+    int res = 0;
     FILE *inFile = NULL, *outFile = NULL;
     CMapOutFormat *fmt = NULL;
 
@@ -748,12 +749,16 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto out;
+    }
 
     if (inFilename == NULL)
     {
         THERR("Nothing to do. (try --help)\n");
-        exit(0);
+        res = 1;
+        goto out;
     }
 
     // Do statistical analysis, if needed
@@ -772,7 +777,8 @@
         {
             THERR("Error opening input file '%s'!\n",
                 inFilename);
-            exit(1);
+            res = -9;
+            goto out;
         }
 
         // Initialize counters
@@ -798,6 +804,7 @@
         }
 
         fclose(inFile);
+        inFile = NULL;
 
         // Find highest frequency
         THMSG(2, "Computing results.\n");
@@ -860,6 +867,8 @@
     if (!optNoHeaders && fmt->putFileEnd)
         fmt->putFileEnd(outFile);
 
+    THMSG(1, "Done.\n");
+
 out:
     if (outFile != NULL)
         fclose(outFile);
@@ -867,8 +876,6 @@
     if (inFile != NULL)
         fclose(inFile);
 
-    THMSG(1, "Done.\n");
 
-    exit(0);
-    return 0;
+    return res;
 }
--- a/src/combine.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/combine.c	Tue Dec 26 02:42:39 2023 +0200
@@ -227,6 +227,7 @@
 
 int main(int argc, char *argv[])
 {
+    int res = 0;
     int i, worldX0, worldY0, worldX1, worldY1, worldW, worldH;
     MapBlock *worldMap = NULL;
     FILE *tmpFile = NULL;
@@ -237,11 +238,15 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
+    {
+        res = 1;
         goto out;
+    }
 
     if (nsrcFiles < 1)
     {
         THERR("Nothing to do. (try --help)\n");
+        res = 1;
         goto out;
     }
 
@@ -254,6 +259,7 @@
         {
             THERR("Error reading input file '%s'!\n",
                 file->filename);
+            res = -3;
             goto out;
         }
 
@@ -267,6 +273,7 @@
     if (nsrcFiles <= 0)
     {
         THERR("No maps, nothing to do.\n");
+        res = -1;
         goto out;
     }
 
@@ -315,6 +322,7 @@
     if ((worldMap = mapBlockAlloc(worldW, worldH)) == NULL)
     {
         THERR("Error allocating world map!\n");
+        res = -8;
         goto out;
     }
 
@@ -334,6 +342,7 @@
         {
             THERR("Mapblock #%d ['%s' @ %d,%d] placement failed!\n",
                 i, file->filename, file->blk->xc, file->blk->yc);
+            res = -12;
             goto out;
         }
     }
@@ -352,6 +361,7 @@
         if ((tmpFile = fopen(dstFile, "wb")) == NULL)
         {
             THERR("Error opening output file '%s'!\n", dstFile);
+            res = -16;
             goto out;
         }
 
@@ -363,6 +373,7 @@
     else
     {
         THERR("No map generated?\n");
+        res = -20;
     }
 
 out:
@@ -371,5 +382,5 @@
     if (tmpFile != NULL)
         fclose(tmpFile);
 
-    return 0;
+    return res;
 }
--- a/src/diffmap.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/diffmap.c	Tue Dec 26 02:42:39 2023 +0200
@@ -180,13 +180,13 @@
 
 int main(int argc, char *argv[])
 {
+    int res = 0;
     MapBlock
         *srcMap1 = NULL,
         *srcMap2 = NULL,
         *resMap = NULL;
     FILE *outFile = NULL;
     size_t count;
-    int res = 0;
 
     // Initialize
     th_init("diffmap", "Create a diff between two ASCII mapfiles", "0.5", NULL, NULL);
@@ -211,7 +211,7 @@
     if ((srcMap1 = mapBlockParseFile(srcFilename1, false)) == NULL ||
         (srcMap2 = mapBlockParseFile(srcFilename2, false)) == NULL)
     {
-        res = 2;
+        res = -2;
         goto out;
     }
 
@@ -221,13 +221,13 @@
         optUseOldFormat, optFilter1, optFilter2, &count)) == NULL)
     {
         THERR("Could not create diff between inputs!\n");
-        res = 3;
+        res = -3;
         goto out;
     }
     THMSG(1, "%" PRIu_SIZE_T " differences.\n", count);
 
     // Open output file
-    res = count > 0 ? 0 : 4;
+    res = count > 0 ? 0 : -4;
     if (optAlwaysDiff || count > 0)
     {
         THMSG(2, "Outputting map diff %d x %d...\n",
@@ -240,7 +240,7 @@
         {
             THERR("Error opening output file '%s'!\n",
                 destFilename);
-            res = 6;
+            res = -6;
             goto out;
         }
 
--- a/src/map2ppm.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/map2ppm.c	Tue Dec 26 02:42:39 2023 +0200
@@ -349,9 +349,9 @@
  */
 int main(int argc, char *argv[])
 {
+    int res = 0;
     FILE *outFile = NULL;
     MapBlock *map = NULL;
-    int ret = 0;
 
     // Initialize
     th_init("map2ppm", "ASCII map to PPM/PNG image converter", "0.5", NULL, NULL);
@@ -360,12 +360,16 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto out;
+    }
 
     if (srcFilename == NULL)
     {
         THERR("Nothing to do. (try --help)\n");
-        exit(0);
+        res = 1;
+        goto out;
     }
 
     // Read input file
@@ -375,6 +379,7 @@
     {
         THERR("Error reading map file '%s'!\n",
             srcFilename);
+        res = -9;
         goto out;
     }
 
@@ -386,6 +391,7 @@
     {
         THERR("Error opening output file '%s'!\n",
             dstFilename);
+        res = -8;
         goto out;
     }
 
@@ -394,13 +400,13 @@
 
 #ifdef HAVE_LIBPNG
     if (optPNGLevel >= 0)
-        ret = writePNGFile(outFile, map, optScale);
+        res = writePNGFile(outFile, map, optScale);
     else
 #endif
-        ret = writePPMFile(outFile, map, optScale);
+        res = writePPMFile(outFile, map, optScale);
 
-    if (ret != 0)
-        THERR("Image write failed, code=%d\n", ret);
+    if (res != 0)
+        THERR("Image write failed, code=%d\n", res);
     else
         THMSG(1, "Done.\n");
 
@@ -409,5 +415,5 @@
         fclose(outFile);
 
     mapBlockFree(map);
-    return ret;
+    return res;
 }
--- a/src/mapsearch.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/mapsearch.c	Tue Dec 26 02:42:39 2023 +0200
@@ -1808,7 +1808,7 @@
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, 0))
     {
-        res = -10;
+        res = 1;
         goto out;
     }
 
@@ -1816,14 +1816,14 @@
     {
         argShowHelp();
         mapERR("No maps specified.\n");
-        res = -10;
+        res = 1;
         goto out;
     }
 
     if (optNListenTo == 0 && optTest == NULL)
     {
         mapERR("No listeners specified.\n");
-        res = -10;
+        res = 1;
         goto out;
     }
 
--- a/src/mapstats.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/mapstats.c	Tue Dec 26 02:42:39 2023 +0200
@@ -108,7 +108,7 @@
         else
         {
             THERR("Unknown sorting method name '%s'!\n", optArg);
-            exit(1);
+            return false;
         }
 
         THMSG(2, "Sorting via method %d\n", optSortBy);
@@ -210,6 +210,7 @@
     MapBlock *map;
     unsigned int statTotal, statUnknown;
     MapPieceStat statPieces[nmapPieces];
+    int res = 0;
 
     // Initialize
     th_init("mapstats", "ASCII map statistics generator", "0.2", NULL, NULL);
@@ -218,12 +219,16 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto out;
+    }
 
     if (nsrcFilenames == 0)
     {
         THERR("Nothing to do. (try --help)\n");
-        exit(0);
+        res = 1;
+        goto out;
     }
 
     // Read input file
@@ -242,7 +247,8 @@
         {
             THERR("Error reading map file '%s'!\n",
                 srcFilenames[n]);
-            exit(1);
+            res = -9;
+            goto out;
         }
 
         THMSG(1, "Analyzing %dx%d area ...\n",
@@ -279,8 +285,8 @@
         case SORT_AMOUNT: tmpFunc = compareByAmount; break;
         default:
             THERR("Internal error, no sort function for sort type %d.\n", optSortBy);
-            exit(2);
-            break;
+            res = -7;
+            goto out;
         }
         qsort(statPieces, nmapPieces, sizeof(MapPieceStat), tmpFunc);
     }
@@ -293,7 +299,7 @@
     {
         THERR("Error opening output file '%s'!\n",
             dstFilename);
-        exit(1);
+        goto out;
     }
 
     for (int i = 0; i < nmapPieces; i++)
@@ -313,10 +319,9 @@
     fprintf(outFile, " %d total, %d unknown.\n",
         statTotal, statUnknown);
 
-    fclose(outFile);
-
-    THMSG(1, "Done.\n");
+out:
+    if (outFile != NULL)
+        fclose(outFile);
 
-    exit(0);
-    return 0;
+    return res;
 }
--- a/src/mkcitymap.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/mkcitymap.c	Tue Dec 26 02:42:39 2023 +0200
@@ -369,6 +369,7 @@
 
 int main(int argc, char *argv[])
 {
+    int res = 0;
     MapBlock *map = NULL;
     FILE *outFile = NULL, *inFile = NULL;
     LocFileInfo info;
@@ -383,11 +384,15 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        exit(1);
+    {
+        res = 1;
+        goto out;
+    }
 
     if (optMapFilename == NULL || optLocFilename == NULL)
     {
         THERR("You need to specify at least map and loc filenames. (try --help)\n");
+        res = 1;
         goto out;
     }
 
@@ -397,11 +402,15 @@
     {
         THERR("Could not open location file '%s' for reading.\n",
             info.filename);
+        res = -3;
         goto out;
     }
 
     if (!locParseLocStream(inFile, &info, &locations, info.xoffs, info.yoffs))
+    {
+        res = -4;
         goto out;
+    }
 
 
     // Open mapfile
@@ -409,6 +418,7 @@
     {
         THERR("Error parsing/opening mapfile '%s'.\n",
             optMapFilename);
+        res = -5;
         goto out;
     }
 
@@ -419,6 +429,7 @@
     {
         THERR("Error opening output file '%s'!\n",
             optDestFilename);
+        res = -6;
         goto out;
     }
 
@@ -440,5 +451,5 @@
     locFreeMapLocations(&locations);
     locFreeFileInfo(&info);
 
-    return 0;
+    return res;
 }
--- a/src/mkloc.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/mkloc.c	Tue Dec 26 02:42:39 2023 +0200
@@ -1140,7 +1140,8 @@
 
 int main(int argc, char *argv[])
 {
-    FILE *outFile = NULL;
+    int res = 0;
+    FILE *outFile = NULL, *inFile = NULL;
     MapBlock *worldMap = NULL;
     MapLocations worldLoc;
 
@@ -1154,30 +1155,37 @@
     // Parse arguments
     if (!th_args_process(argc, argv, optList, optListN,
         argHandleOpt, NULL, OPTH_BAILOUT))
+    {
+        res = 1;
         goto out;
+    }
 
     // Check the mode and arguments
     if (optInFilename == NULL && optGetUpdateLoc && optOutput == OUTFMT_LOCFILE)
     {
         THERR("Map file required for location update mode!\n");
+        res = 1;
         goto out;
     }
 
     if (optOutput == OUTFMT_LOCFILE && noptLocFiles < 0 && !optGetUpdateLoc)
     {
         THERR("Location file or location update mode required for location file output!\n");
+        res = 1;
         goto out;
     }
 
     if ((optOutput == OUTFMT_SCRIPT || optOutput == OUTFMT_MAPLOC) && noptLocFiles < 0)
     {
         THERR("Location file required for script or MapLoc HTML output!\n");
+        res = 1;
         goto out;
     }
 
     if (optInFilename == NULL && optOutput == OUTFMT_MAP)
     {
         THERR("Mapfile required for map generation.\n");
+        res = 1;
         goto out;
     }
 
@@ -1189,6 +1197,7 @@
         if (worldMap == NULL)
         {
             THERR("World map could not be loaded!\n");
+            res = -2;
             goto out;
         }
 
@@ -1199,12 +1208,12 @@
     for (int i = 0; i <= noptLocFiles; i++)
     {
         LocFileInfo *f = &optLocFiles[i];
-        FILE *inFile;
 
         if (optOutput == OUTFMT_GMAPS && f->continent == NULL)
         {
             THERR("Required continent name not set for #%d '%s'.\n",
                 i, f->filename);
+            res = -3;
             goto out;
         }
 
@@ -1213,13 +1222,18 @@
         {
             THERR("Could not open location file '%s' for reading.\n",
                 f->filename);
+            res = -3;
             goto out;
         }
 
         if (!locParseLocStream(inFile, f, &worldLoc, f->xoffs, f->yoffs))
+        {
+            res = -4;
             goto out;
+        }
 
         fclose(inFile);
+        inFile = NULL;
     }
 
     // Update locations
@@ -1248,6 +1262,7 @@
     {
         THERR("Error opening output file '%s'!\n",
             optOutFilename);
+        res = -5;
         goto out;
     }
 
@@ -1298,8 +1313,11 @@
     if (outFile != NULL)
         fclose(outFile);
 
+    if (inFile != NULL)
+        fclose(inFile);
+
     mapBlockFree(worldMap);
     locFreeMapLocations(&worldLoc);
 
-    return 0;
+    return res;
 }
--- a/src/patchmap.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/patchmap.c	Tue Dec 26 02:42:39 2023 +0200
@@ -93,9 +93,9 @@
 
 int main(int argc, char *argv[])
 {
+    int res = 0;
     MapBlock *map = NULL, *patch = NULL;
     FILE *outFile = NULL;
-    int res = 0;
 
     // Initialize
     th_init("patchmap", "Patch a mapfile with a diff", "0.1", NULL, NULL);
@@ -120,7 +120,7 @@
     if ((map = mapBlockParseFile(mapFilename, false)) == NULL ||
         (patch = mapBlockParseFile(patchFilename, true)) == NULL)
     {
-        res = 2;
+        res = -2;
         goto out;
     }
 
@@ -131,7 +131,7 @@
             map->width, map->height,
             patch->width, patch->height);
 
-        res = 2;
+        res = -2;
         goto out;
     }
 
@@ -156,7 +156,7 @@
             {
                 THERR("[%d,%d] invalid symbol index %d (%d) in patch\n",
                     xc+1, yc+1, *sp, i);
-                res = 7;
+                res = -7;
                 goto out;
             }
 
@@ -181,7 +181,7 @@
     {
         THERR("Error opening output file '%s'!\n",
             destFilename);
-        res = 6;
+        res = -6;
         goto out;
     }
 
--- a/src/stitchmap.c	Tue Dec 26 02:35:07 2023 +0200
+++ b/src/stitchmap.c	Tue Dec 26 02:42:39 2023 +0200
@@ -414,11 +414,10 @@
 
 int main(int argc, char *argv[])
 {
-    bool isOK;
-    int i, currRounds, nmapBlocks = 0, currBlocks,
+    int res = 0, i, currRounds, nmapBlocks = 0, currBlocks,
         worldX0, worldY0, worldX1, worldY1,
-        offsetX = 0, offsetY = 0,
-        res = 0;
+        offsetX, offsetY;
+    bool isOK;
     MapBlock *worldMap = NULL, *initialMap = NULL;
     MapBlock **mapBlocks = NULL;
 
@@ -455,7 +454,7 @@
         else
         {
             THERR("Initial map could not be loaded!\n");
-            res = 1;
+            res = -1;
             goto out;
         }
     }
@@ -482,7 +481,7 @@
         {
             THERR("Error opening input file '%s'!\n",
                 srcFiles[i]);
-            res = 16;
+            res = -16;
             goto out;
         }
 
@@ -504,7 +503,7 @@
                     {
                         fclose(tmpFile);
                         THERR("Could not allocate/extend mapblock pointer structure (#%d)\n", nmapBlocks);
-                        res = 18;
+                        res = -18;
                         goto out;
                     }
 
@@ -522,7 +521,7 @@
     if (nmapBlocks <= 0)
     {
         THERR("No mapblocks, nothing to do.\n");
-        res = 11;
+        res = -11;
         goto out;
     }
 
@@ -578,7 +577,7 @@
     if ((worldMap = mapBlockAlloc(worldX1 - worldX0 + 1, worldY1 - worldY0 + 1)) == NULL)
     {
         THERR("Error allocating world map!\n");
-        res = 4;
+        res = -4;
         goto out;
     }
 
@@ -588,7 +587,7 @@
         if (mapBlockPut(&worldMap, initialMap, offsetX + initialMap->xc, offsetY + initialMap->yc) < 0)
         {
             THERR("Initial map mapBlockPut() failed!\n");
-            res = 9;
+            res = -9;
             goto out;
         }
     }
@@ -598,7 +597,7 @@
         if (mapBlockPut(&worldMap, mapBlocks[i], offsetX + mapBlocks[i]->xc, offsetY + mapBlocks[i]->yc) < 0)
         {
             THERR("Initial map mapBlockPut() failed!\n");
-            res = 9;
+            res = -9;
             goto out;
         }
         mapBlocks[i]->mark = true;
@@ -643,7 +642,7 @@
                     {
                         THERR("mapBlockPut(%d, %d, %d) failed!\n",
                             offsetX, offsetY, i);
-                        res = 9;
+                        res = -9;
                         goto out;
                     }
                     tmp->mark = true;
@@ -689,7 +688,7 @@
         {
             THERR("Error opening output file '%s'!\n",
                 destFile);
-            res = 1;
+            res = -1;
             goto out;
         }
 
@@ -719,7 +718,7 @@
     else
     {
         THERR("No map generated?\n");
-        res = 6;
+        res = -6;
     }
 
 out: