]> 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 6d67b126a8f056887060d7cd6a453e6e2f2ba964..b3be7fa9cdad97590a754db0b0d828c5025474b3 100644 (file)
@@ -1,24 +1,23 @@
 /* chew
-   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 2000, 2001,
-   2002, 2003, 2005
-   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.
+   This file is part of BFD, the Binary File Descriptor library.
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
 /* Yet another way of extracting documentation from source.
    No, I haven't finished it yet, but I hope you people like it better
@@ -86,6 +85,8 @@ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
 #include <assert.h>
 #include <stdio.h>
 #include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
 
 #define DEF_SIZE 5000
 #define STACK 50
@@ -117,6 +118,7 @@ static void overwrite_string (string_type *, string_type *);
 static void catbuf (string_type *, char *, unsigned int);
 static void cattext (string_type *, char *);
 static void catstr (string_type *, string_type *);
+static void die (char *);
 #endif
 
 static void
@@ -126,7 +128,7 @@ init_string_with_size (buffer, size)
 {
   buffer->write_idx = 0;
   buffer->size = size;
-  buffer->ptr = malloc (size);
+  buffer->ptr = (char *) malloc (size);
 }
 
 static void
@@ -159,7 +161,9 @@ write_buffer (buffer, f)
      string_type *buffer;
      FILE *f;
 {
-  fwrite (buffer->ptr, buffer->write_idx, 1, f);
+  if (buffer->write_idx != 0
+      && fwrite (buffer->ptr, buffer->write_idx, 1, f) != 1)
+    die ("cannot write output");
 }
 
 static void
@@ -167,6 +171,7 @@ delete_string (buffer)
      string_type *buffer;
 {
   free (buffer->ptr);
+  buffer->ptr = NULL;
 }
 
 static char *
@@ -195,7 +200,7 @@ catchar (buffer, ch)
   if (buffer->write_idx == buffer->size)
     {
       buffer->size *= 2;
-      buffer->ptr = realloc (buffer->ptr, buffer->size);
+      buffer->ptr = (char *) realloc (buffer->ptr, buffer->size);
     }
 
   buffer->ptr[buffer->write_idx++] = ch;
@@ -222,7 +227,7 @@ catbuf (buffer, buf, len)
     {
       while (buffer->write_idx + len >= buffer->size)
        buffer->size *= 2;
-      buffer->ptr = realloc (buffer->ptr, buffer->size);
+      buffer->ptr = (char *) realloc (buffer->ptr, buffer->size);
     }
   memcpy (buffer->ptr + buffer->write_idx, buf, len);
   buffer->write_idx += len;
@@ -261,6 +266,19 @@ skip_white_and_stars (src, idx)
   return idx;
 }
 
+static unsigned int
+skip_past_newline_1 (ptr, idx)
+     string_type *ptr;
+     unsigned int idx;
+{
+  while (at (ptr, idx)
+        && at (ptr, idx) != '\n')
+    idx++;
+  if (at (ptr, idx) == '\n')
+    return idx + 1;
+  return idx;
+}
+
 /***********************************************************************/
 
 string_type stack[STACK];
@@ -470,8 +488,10 @@ remove_noncomments (src, dst)
 static void
 print_stack_level ()
 {
-  fprintf (stderr, "current string stack depth = %d, ", tos - stack);
-  fprintf (stderr, "current integer stack depth = %d\n", isp - istack);
+  fprintf (stderr, "current string stack depth = %ld, ",
+          (long) (tos - stack));
+  fprintf (stderr, "current integer stack depth = %ld\n",
+          (long) (isp - istack));
   pc++;
 }
 
@@ -597,10 +617,12 @@ outputdots ()
 
   while (at (tos, idx))
     {
-      if (at (tos, idx) == '\n' && at (tos, idx + 1) == '.')
+      /* Every iteration begins at the start of a line.  */
+      if (at (tos, idx) == '.')
        {
          char c;
-         idx += 2;
+
+         idx++;
 
          while ((c = at (tos, idx)) && c != '\n')
            {
@@ -620,11 +642,13 @@ outputdots ()
                  idx++;
                }
            }
+         if (c == '\n')
+           idx++;
          catchar (&out, '\n');
        }
       else
        {
-         idx++;
+         idx = skip_past_newline_1 (tos, idx);
        }
     }
 
@@ -1065,6 +1089,7 @@ drop ()
 {
   tos--;
   check_range ();
+  delete_string (tos + 1);
   pc++;
 }
 
@@ -1089,10 +1114,7 @@ icatstr ()
 static void
 skip_past_newline ()
 {
-  while (at (ptr, idx)
-        && at (ptr, idx) != '\n')
-    idx++;
-  idx++;
+  idx = skip_past_newline_1 (ptr, idx);
   pc++;
 }
 
@@ -1144,7 +1166,10 @@ nextword (string, word)
        }
     }
   if (!*string)
-    return 0;
+    {
+      *word = NULL;
+      return NULL;
+    }
 
   word_start = string;
   if (*string == '"')
@@ -1171,7 +1196,7 @@ nextword (string, word)
        }
     }
 
-  *word = malloc (length + 1);
+  *word = (char *) malloc (length + 1);
 
   dst = *word;
   src = word_start;
@@ -1202,7 +1227,7 @@ nextword (string, word)
   if (*string)
     return string + 1;
   else
-    return 0;
+    return NULL;
 }
 
 dict_type *root;
@@ -1220,11 +1245,39 @@ lookup_word (word)
     }
   if (warning)
     fprintf (stderr, "Can't find %s\n", word);
-  return 0;
+  return NULL;
+}
+
+static void
+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 ()
+perform (void)
 {
   tos = stack;
 
@@ -1250,7 +1303,7 @@ perform ()
                fprintf (stderr, "warning, %s is not recognised\n", next);
              skip_past_newline ();
            }
-
+         free (next);
        }
       else
        skip_past_newline ();
@@ -1261,14 +1314,14 @@ dict_type *
 newentry (word)
      char *word;
 {
-  dict_type *new = (dict_type *) malloc (sizeof (dict_type));
-  new->word = word;
-  new->next = root;
-  root = new;
-  new->code = (stinst_type *) malloc (sizeof (stinst_type));
-  new->code_length = 1;
-  new->code_end = 0;
-  return new;
+  dict_type *new_d = (dict_type *) malloc (sizeof (dict_type));
+  new_d->word = word;
+  new_d->next = root;
+  root = new_d;
+  new_d->code = (stinst_type *) malloc (sizeof (stinst_type));
+  new_d->code_length = 1;
+  new_d->code_end = 0;
+  return new_d;
 }
 
 unsigned int
@@ -1281,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;
 
@@ -1293,19 +1346,19 @@ add_intrinsic (name, func)
      char *name;
      void (*func) ();
 {
-  dict_type *new = newentry (name);
-  add_to_definition (new, func);
-  add_to_definition (new, 0);
+  dict_type *new_d = newentry (strdup (name));
+  add_to_definition (new_d, func);
+  add_to_definition (new_d, 0);
 }
 
 void
 add_var (name)
      char *name;
 {
-  dict_type *new = newentry (name);
-  add_to_definition (new, push_number);
-  add_to_definition (new, (stinst_type) (&(new->var)));
-  add_to_definition (new, 0);
+  dict_type *new_d = newentry (name);
+  add_to_definition (new_d, push_number);
+  add_to_definition (new_d, (stinst_type) (&(new_d->var)));
+  add_to_definition (new_d, 0);
 }
 
 void
@@ -1314,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])
@@ -1356,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
@@ -1372,6 +1441,7 @@ compile (string)
          fprintf (stderr, "syntax error at %s\n", string - 1);
        }
     }
+  free (word);
 }
 
 static void
@@ -1541,6 +1611,7 @@ main (ac, av)
              read_in (&b, f);
              compile (b.ptr);
              perform ();
+             delete_string (&b);
            }
          else if (av[i][1] == 'i')
            {
@@ -1555,10 +1626,13 @@ 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 %d\n",
-              tos - stack);
+      fprintf (stderr, "finishing with current stack level %ld\n",
+              (long) (tos - stack));
       return 1;
     }
   return 0;