Stop leaking memory
[open-adventure.git] / main.c
diff --git a/main.c b/main.c
index 4ebe719b646947b0cb1e6d423559d8230a94e168..61a2267a59b183f6879f132c9001493938888111 100644 (file)
--- a/main.c
+++ b/main.c
@@ -13,7 +13,7 @@
  * and for the offensive globals.  Applying the Structured Program
  * Theorem can be hard.
  */
-#define DEFINE_GLOBALS_FROM_INCLUDES
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
@@ -29,9 +29,6 @@
 
 struct game_t game;
 
-long LNLENG, LNPOSN;
-char rawbuf[LINESIZE], INLINE[LINESIZE + 1];
-
 FILE  *logfp = NULL, *rfp = NULL;
 bool oldstyle = false;
 bool editline = true;
@@ -45,7 +42,7 @@ static void sig_handler(int signo)
         if (logfp != NULL)
             fflush(logfp);
     }
-    exit(0);
+    exit(EXIT_FAILURE);
 }
 // LCOV_EXCL_STOP
 
@@ -116,7 +113,7 @@ int main(int argc, char *argv[])
 #endif
             fprintf(stderr,
                     "        -s suppress command editing\n");
-            exit(-1);
+            exit(EXIT_FAILURE);
             break;
         }
     }
@@ -521,7 +518,7 @@ static void croak(void)
  *  him, so we need game.oldlc2, which is the last place he was
  *  safe.) */
 
-static bool playermove(token_t verb, int motion)
+static bool playermove( int motion)
 {
     int scratchloc, travel_entry = tkey[game.loc];
     game.newloc = game.loc;
@@ -598,23 +595,20 @@ static bool playermove(token_t verb, int motion)
         if (T_TERMINATE(travel[travel_entry]) || travel[travel_entry].motion == motion)
             break;
         if (travel[travel_entry].stop) {
-            /* FIXME: Magic numbers! */
             /*  Couldn't find an entry matching the motion word passed
              *  in.  Various messages depending on word given. */
             int spk = CANT_APPLY;
-            if (motion >= MOT_43 && motion <= MOT_50)
+            if (motion >= EAST && motion <= NW)
                 spk = BAD_DIRECTION;
-            if (motion == MOT_29 || motion == MOT_30)
+            if (motion == UP || motion == DOWN)
                 spk = BAD_DIRECTION;
-            if (motion == MOT_7 || motion == MOT_36 || motion == MOT_37)
+            if (motion == FORWARD || motion == LEFT || motion == RIGHT)
                 spk = UNSURE_FACING;
-            if (motion == MOT_11 || motion == MOT_19)
+            if (motion == OUTSIDE || motion == INSIDE)
                 spk = NO_INOUT_HERE;
-            if (verb == FIND || verb == INVENTORY)
-                spk = NEARBY;
-            if (motion == MOT_62 || motion == MOT_65)
+            if (motion == XYZZY || motion == PLUGH)
                 spk = NOTHING_HAPPENS;
-            if (motion == MOT_17)
+            if (motion == CRAWL)
                 spk = WHICH_WAY;
             rspeak(spk);
             return true;
@@ -998,7 +992,7 @@ static bool do_command()
             rspeak(TAME_BEAR);
         speak(msg);
         if (FORCED(game.loc)) {
-            if (playermove(command.verb, 1))
+            if (playermove(HERE))
                 return true;
             else
                 continue;      /* back to top of main interpreter loop */
@@ -1035,6 +1029,8 @@ L2600:
 
         /* This is where we get a new command from the user */
         char* input;
+        char inputbuf[LINESIZE];
+
         for (;;) {
             input = get_input();
             if (input == NULL)
@@ -1046,8 +1042,12 @@ L2600:
             if (strcmp(input, "") != 0)
                 break;
         }
+
+        strncpy(inputbuf, input, LINESIZE - 1);
+        linenoiseFree(input);
+
         long tokens[4];
-        tokenize(input, tokens);
+        tokenize(inputbuf, tokens);
         command.wd1 = tokens[0];
         command.wd1x = tokens[1];
         command.wd2 = tokens[2];
@@ -1124,7 +1124,7 @@ Lookup:
         defn = get_vocab_id(word1);
         if (defn == -1) {
             /* Gee, I don't understand. */
-            if (fallback_handler(input))
+            if (fallback_handler(inputbuf))
                 continue;
             rspeak(DONT_KNOW, command.wd1, command.wd1x);
             goto L2600;
@@ -1132,7 +1132,7 @@ Lookup:
         kmod = MOD(defn, 1000);
         switch (defn / 1000) {
         case 0:
-            if (playermove(command.verb, kmod))
+            if (playermove(kmod))
                 return true;
             else
                 continue;      /* back to top of main interpreter loop */
@@ -1156,7 +1156,7 @@ Laction:
         case GO_TERMINATE:
             return true;
         case GO_MOVE:
-            playermove(command.verb, NUL);
+            playermove(NUL);
             return true;
         case GO_TOP:
             continue;  /* back to top of main interpreter loop */
@@ -1187,7 +1187,6 @@ Laction:
         default:
             BUG(ACTION_RETURNED_PHASE_CODE_BEYOND_END_OF_SWITCH); // LCOV_EXCL_LINE
         }
-        linenoiseFree(input);
     }
 }