]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - bfd/doc/chew.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / bfd / doc / chew.c
index af7c01b0a34f19a0fce207943a60ace15960f7ab..b3be7fa9cdad97590a754db0b0d828c5025474b3 100644 (file)
@@ -1,5 +1,5 @@
 /* chew
-   Copyright (C) 1990-2015 Free Software Foundation, Inc.
+   Copyright (C) 1990-2021 Free Software Foundation, Inc.
    Contributed by steve chamberlain @cygnus
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -171,6 +171,7 @@ delete_string (buffer)
      string_type *buffer;
 {
   free (buffer->ptr);
+  buffer->ptr = NULL;
 }
 
 static char *
@@ -1088,6 +1089,7 @@ drop ()
 {
   tos--;
   check_range ();
+  delete_string (tos + 1);
   pc++;
 }
 
@@ -1164,7 +1166,10 @@ nextword (string, word)
        }
     }
   if (!*string)
-    return 0;
+    {
+      *word = NULL;
+      return NULL;
+    }
 
   word_start = string;
   if (*string == '"')
@@ -1222,7 +1227,7 @@ nextword (string, word)
   if (*string)
     return string + 1;
   else
-    return 0;
+    return NULL;
 }
 
 dict_type *root;
@@ -1240,11 +1245,39 @@ lookup_word (word)
     }
   if (warning)
     fprintf (stderr, "Can't find %s\n", word);
-  return 0;
+  return NULL;
 }
 
 static void
-perform ()
+free_words (void)
+{
+  dict_type *ptr = root;
+
+  while (ptr)
+    {
+      dict_type *next;
+
+      free (ptr->word);
+      if (ptr->code)
+       {
+         int i;
+         for (i = 0; i < ptr->code_end - 1; i ++)
+           if (ptr->code[i] == push_text
+               && ptr->code[i + 1])
+             {
+               free ((char *) ptr->code[i + 1] - 1);
+               ++ i;
+             }
+         free (ptr->code);
+       }
+      next = ptr->next;
+      free (ptr);
+      ptr = next;
+    }
+}
+
+static void
+perform (void)
 {
   tos = stack;
 
@@ -1301,7 +1334,7 @@ add_to_definition (entry, word)
       entry->code_length += 2;
       entry->code =
        (stinst_type *) realloc ((char *) (entry->code),
-                                entry->code_length * sizeof (word_type));
+                                entry->code_length * sizeof (stinst_type));
     }
   entry->code[entry->code_end] = word;
 
@@ -1313,7 +1346,7 @@ add_intrinsic (name, func)
      char *name;
      void (*func) ();
 {
-  dict_type *new_d = newentry (name);
+  dict_type *new_d = newentry (strdup (name));
   add_to_definition (new_d, func);
   add_to_definition (new_d, 0);
 }
@@ -1334,24 +1367,37 @@ compile (string)
 {
   /* Add words to the dictionary.  */
   char *word;
+
   string = nextword (string, &word);
   while (string && *string && word[0])
     {
       if (strcmp (word, "var") == 0)
        {
+         free (word);
          string = nextword (string, &word);
-
+         if (!string)
+           continue;
          add_var (word);
          string = nextword (string, &word);
        }
       else if (word[0] == ':')
        {
          dict_type *ptr;
+
          /* Compile a word and add to dictionary.  */
+         free (word);
          string = nextword (string, &word);
-
+         if (!string)
+           continue;
          ptr = newentry (word);
          string = nextword (string, &word);
+         if (!string)
+           {
+             free (ptr->code);
+             free (ptr);
+             continue;
+           }
+         
          while (word[0] != ';')
            {
              switch (word[0])
@@ -1376,15 +1422,18 @@ compile (string)
                     function */
                  add_to_definition (ptr, push_number);
                  add_to_definition (ptr, (stinst_type) atol (word));
+                 free (word);
                  break;
                default:
                  add_to_definition (ptr, call);
                  add_to_definition (ptr, (stinst_type) lookup_word (word));
+                 free (word);
                }
 
              string = nextword (string, &word);
            }
          add_to_definition (ptr, 0);
+         free (word);
          string = nextword (string, &word);
        }
       else
@@ -1392,6 +1441,7 @@ compile (string)
          fprintf (stderr, "syntax error at %s\n", string - 1);
        }
     }
+  free (word);
 }
 
 static void
@@ -1561,6 +1611,7 @@ main (ac, av)
              read_in (&b, f);
              compile (b.ptr);
              perform ();
+             delete_string (&b);
            }
          else if (av[i][1] == 'i')
            {
@@ -1575,6 +1626,9 @@ main (ac, av)
        }
     }
   write_buffer (stack + 0, stdout);
+  free_words ();
+  delete_string (&pptr);
+  delete_string (&buffer);
   if (tos != stack)
     {
       fprintf (stderr, "finishing with current stack level %ld\n",