Update year range in copyright notice of binutils files
[deliverable/binutils-gdb.git] / bfd / doc / chew.c
index 52de92e01e00e6306226d9e59b78f331fcef287d..da229df202c8a2b5d5ef2789630a62435c6b49f7 100644 (file)
@@ -1,23 +1,23 @@
 /* chew
-   Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 1998, 2000
-   Free Software Foundation, Inc.
+   Copyright (C) 1990-2018 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
@@ -81,11 +81,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
    Foo.  */
 
-#include <ansidecl.h>
-#include "sysdep.h"
+#include "ansidecl.h"
 #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,14 +161,18 @@ 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
 delete_string (buffer)
      string_type *buffer;
 {
-  free (buffer->ptr);
+  if (buffer->ptr)
+    free (buffer->ptr);
+  buffer->ptr = NULL;
 }
 
 static char *
@@ -195,7 +201,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 +228,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 +267,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];
@@ -289,8 +308,6 @@ struct dict_struct
 
 typedef struct dict_struct dict_type;
 
-#define WORD(x) static void x()
-
 static void
 die (msg)
      char *msg;
@@ -362,7 +379,8 @@ exec (word)
     (*pc) ();
 }
 
-WORD (call)
+static void
+call ()
 {
   stinst_type *oldpc = pc;
   dict_type *e;
@@ -371,7 +389,8 @@ WORD (call)
   pc = oldpc + 2;
 }
 
-WORD (remchar)
+static void
+remchar ()
 {
   if (tos->write_idx)
     tos->write_idx--;
@@ -388,7 +407,8 @@ strip_trailing_newlines ()
   pc++;
 }
 
-WORD (push_number)
+static void
+push_number ()
 {
   isp++;
   icheck_range ();
@@ -397,7 +417,8 @@ WORD (push_number)
   pc++;
 }
 
-WORD (push_text)
+static void
+push_text ()
 {
   tos++;
   check_range ();
@@ -468,8 +489,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++;
 }
 
@@ -482,16 +505,20 @@ print_stack_level ()
  */
 
 static void
-paramstuff (void)
+paramstuff ()
 {
   unsigned int openp;
   unsigned int fname;
   unsigned int idx;
+  unsigned int len;
   string_type out;
   init_string (&out);
 
+#define NO_PARAMS 1
+
   /* Make sure that it's not already param'd or proto'd.  */
-  if (find (tos, "PARAMS") || find (tos, "PROTO") || !find (tos, "("))
+  if (NO_PARAMS
+      || find (tos, "PARAMS") || find (tos, "PROTO") || !find (tos, "("))
     {
       catstr (&out, tos);
     }
@@ -513,25 +540,33 @@ paramstuff (void)
 
       fname++;
 
-      for (idx = 0; idx < fname; idx++)        /* Output type */
+      /* Output type, omitting trailing whitespace character(s), if
+         any.  */
+      for (len = fname; 0 < len; len--)
        {
-         catchar (&out, at (tos, idx));
+         if (!isspace ((unsigned char) at (tos, len - 1)))
+           break;
        }
+      for (idx = 0; idx < len; idx++)
+       catchar (&out, at (tos, idx));
 
       cattext (&out, "\n");    /* Insert a newline between type and fnname */
 
-      for (idx = fname; idx < openp; idx++)            /* Output fnname */
+      /* Output function name, omitting trailing whitespace
+         character(s), if any.  */
+      for (len = openp; 0 < len; len--)
        {
-         catchar (&out, at (tos, idx));
+         if (!isspace ((unsigned char) at (tos, len - 1)))
+           break;
        }
+      for (idx = fname; idx < len; idx++)
+       catchar (&out, at (tos, idx));
 
       cattext (&out, " PARAMS (");
 
-      while (at (tos, idx) && at (tos, idx) != ';')
-       {
-         catchar (&out, at (tos, idx));
-         idx++;
-       }
+      for (idx = openp; at (tos, idx) && at (tos, idx) != ';'; idx++)
+       catchar (&out, at (tos, idx));
+
       cattext (&out, ");\n\n");
     }
   overwrite_string (tos, &out);
@@ -542,7 +577,8 @@ paramstuff (void)
 /* turn {*
    and *} into comments */
 
-WORD (translatecomments)
+static void
+translatecomments ()
 {
   unsigned int idx = 0;
   string_type out;
@@ -572,47 +608,9 @@ WORD (translatecomments)
   pc++;
 }
 
-#if 0
-
-/* This is not currently used.  */
-
-/* turn everything not starting with a . into a comment */
-
-WORD (manglecomments)
-{
-  unsigned int idx = 0;
-  string_type out;
-  init_string (&out);
-
-  while (at (tos, idx))
-    {
-      if (at (tos, idx) == '\n' && at (tos, idx + 1) == '*')
-       {
-         cattext (&out, "      /*");
-         idx += 2;
-       }
-      else if (at (tos, idx) == '*' && at (tos, idx + 1) == '}')
-       {
-         cattext (&out, "*/");
-         idx += 2;
-       }
-      else
-       {
-         catchar (&out, at (tos, idx));
-         idx++;
-       }
-    }
-
-  overwrite_string (tos, &out);
-
-  pc++;
-}
-
-#endif
-
 /* Mod tos so that only lines with leading dots remain */
 static void
-outputdots (void)
+outputdots ()
 {
   unsigned int idx = 0;
   string_type out;
@@ -620,10 +618,12 @@ outputdots (void)
 
   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')
            {
@@ -643,11 +643,13 @@ outputdots (void)
                  idx++;
                }
            }
+         if (c == '\n')
+           idx++;
          catchar (&out, '\n');
        }
       else
        {
-         idx++;
+         idx = skip_past_newline_1 (tos, idx);
        }
     }
 
@@ -656,7 +658,8 @@ outputdots (void)
 }
 
 /* Find lines starting with . and | and put example around them on tos */
-WORD (courierize)
+static void
+courierize ()
 {
   string_type out;
   unsigned int idx = 0;
@@ -677,37 +680,47 @@ WORD (courierize)
 
              while (at (tos, idx) && at (tos, idx) != '\n')
                {
-                 if (at (tos, idx) == '{' && at (tos, idx + 1) == '*')
+                 if (command > 1)
+                   {
+                     /* We are inside {} parameters of some command;
+                        Just pass through until matching brace.  */
+                     if (at (tos, idx) == '{')
+                       ++command;
+                     else if (at (tos, idx) == '}')
+                       --command;
+                   }
+                 else if (command != 0)
+                   {
+                     if (at (tos, idx) == '{')
+                       ++command;
+                     else if (!islower ((unsigned char) at (tos, idx)))
+                       --command;
+                   }
+                 else if (at (tos, idx) == '@'
+                          && islower ((unsigned char) at (tos, idx + 1)))
+                   {
+                     ++command;
+                   }
+                 else if (at (tos, idx) == '{' && at (tos, idx + 1) == '*')
                    {
                      cattext (&out, "/*");
                      idx += 2;
+                     continue;
                    }
                  else if (at (tos, idx) == '*' && at (tos, idx + 1) == '}')
                    {
                      cattext (&out, "*/");
                      idx += 2;
+                     continue;
                    }
-                 else if (at (tos, idx) == '{' && !command)
+                 else if (at (tos, idx) == '{'
+                          || at (tos, idx) == '}')
                    {
-                     cattext (&out, "@{");
-                     idx++;
-                   }
-                 else if (at (tos, idx) == '}' && !command)
-                   {
-                     cattext (&out, "@}");
-                     idx++;
-                   }
-                 else
-                   {
-                     if (at (tos, idx) == '@')
-                       command = 1;
-                     else if (isspace ((unsigned char) at (tos, idx))
-                              || at (tos, idx) == '}')
-                       command = 0;
-                     catchar (&out, at (tos, idx));
-                     idx++;
+                     catchar (&out, '@');
                    }
 
+                 catchar (&out, at (tos, idx));
+                 idx++;
                }
              catchar (&out, '\n');
            }
@@ -732,7 +745,8 @@ WORD (courierize)
    on @itemize @bullet, and @items each of them. Then ends with @end
    itemize, inplace at TOS*/
 
-WORD (bulletize)
+static void
+bulletize ()
 {
   unsigned int idx = 0;
   int on = 0;
@@ -786,7 +800,8 @@ WORD (bulletize)
 
 /* Turn <<foo>> into @code{foo} in place at TOS*/
 
-WORD (do_fancy_stuff)
+static void
+do_fancy_stuff ()
 {
   unsigned int idx = 0;
   string_type out;
@@ -882,7 +897,8 @@ copy_past_newline (ptr, idx, dst)
 
 }
 
-WORD (icopy_past_newline)
+static void
+icopy_past_newline ()
 {
   tos++;
   check_range ();
@@ -894,7 +910,8 @@ WORD (icopy_past_newline)
 /* indent
    Take the string at the top of the stack, do some prettying.  */
 
-WORD (kill_bogus_lines)
+static void
+kill_bogus_lines ()
 {
   int sl;
 
@@ -980,7 +997,8 @@ WORD (kill_bogus_lines)
 
 }
 
-WORD (indent)
+static void
+indent ()
 {
   string_type out;
   int tab = 0;
@@ -1030,7 +1048,8 @@ WORD (indent)
 
 }
 
-WORD (get_stuff_in_command)
+static void
+get_stuff_in_command ()
 {
   tos++;
   check_range ();
@@ -1045,7 +1064,8 @@ WORD (get_stuff_in_command)
   pc++;
 }
 
-WORD (swap)
+static void
+swap ()
 {
   string_type t;
 
@@ -1055,7 +1075,8 @@ WORD (swap)
   pc++;
 }
 
-WORD (other_dup)
+static void
+other_dup ()
 {
   tos++;
   check_range ();
@@ -1064,21 +1085,25 @@ WORD (other_dup)
   pc++;
 }
 
-WORD (drop)
+static void
+drop ()
 {
   tos--;
   check_range ();
+  delete_string (tos + 1);
   pc++;
 }
 
-WORD (idrop)
+static void
+idrop ()
 {
   isp--;
   icheck_range ();
   pc++;
 }
 
-WORD (icatstr)
+static void
+icatstr ()
 {
   tos--;
   check_range ();
@@ -1087,16 +1112,15 @@ WORD (icatstr)
   pc++;
 }
 
-WORD (skip_past_newline)
+static void
+skip_past_newline ()
 {
-  while (at (ptr, idx)
-        && at (ptr, idx) != '\n')
-    idx++;
-  idx++;
+  idx = skip_past_newline_1 (ptr, idx);
   pc++;
 }
 
-WORD (internalmode)
+static void
+internalmode ()
 {
   internal_mode = *(isp);
   isp--;
@@ -1104,7 +1128,8 @@ WORD (internalmode)
   pc++;
 }
 
-WORD (maybecatstr)
+static void
+maybecatstr ()
 {
   if (internal_wanted == internal_mode)
     {
@@ -1169,7 +1194,7 @@ nextword (string, word)
        }
     }
 
-  *word = malloc (length + 1);
+  *word = (char *) malloc (length + 1);
 
   dst = *word;
   src = word_start;
@@ -1222,7 +1247,36 @@ lookup_word (word)
 }
 
 static void
-perform (void)
+free_words (void)
+{
+  dict_type *ptr = root;
+
+  while (ptr)
+    {
+      dict_type *next;
+
+      if (ptr->word)
+       free (ptr->word);
+      if (ptr->code)
+       {
+         int i;
+         for (i = 0; i < ptr->code_length; i ++)
+           if (ptr->code[i] == push_text
+               && ptr->code[i + 1])
+             {
+               free (ptr->code[i + 1] - 1);
+               ++ i;
+             }
+         free (ptr->code);
+       }
+      next = ptr->next;
+      free (ptr);
+      ptr = next;
+    }
+}
+
+static void
+perform ()
 {
   tos = stack;
 
@@ -1248,7 +1302,7 @@ perform (void)
                fprintf (stderr, "warning, %s is not recognised\n", next);
              skip_past_newline ();
            }
-
+         free (next);
        }
       else
        skip_past_newline ();
@@ -1259,14 +1313,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
@@ -1291,19 +1345,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
@@ -1312,24 +1366,27 @@ 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);
-
          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);
-
          ptr = newentry (word);
          string = nextword (string, &word);
+         
          while (word[0] != ';')
            {
              switch (word[0])
@@ -1354,15 +1411,19 @@ 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);
+         word = NULL;
          string = nextword (string, &word);
        }
       else
@@ -1370,10 +1431,12 @@ compile (string)
          fprintf (stderr, "syntax error at %s\n", string - 1);
        }
     }
+  if (word)
+    free (word);
 }
 
 static void
-bang (void)
+bang ()
 {
   *(long *) ((isp[0])) = isp[-1];
   isp -= 2;
@@ -1381,19 +1444,22 @@ bang (void)
   pc++;
 }
 
-WORD (atsign)
+static void
+atsign ()
 {
   isp[0] = *(long *) (isp[0]);
   pc++;
 }
 
-WORD (hello)
+static void
+hello ()
 {
   printf ("hello\n");
   pc++;
 }
 
-WORD (stdout_)
+static void
+stdout_ ()
 {
   isp++;
   icheck_range ();
@@ -1401,7 +1467,8 @@ WORD (stdout_)
   pc++;
 }
 
-WORD (stderr_)
+static void
+stderr_ ()
 {
   isp++;
   icheck_range ();
@@ -1409,7 +1476,8 @@ WORD (stderr_)
   pc++;
 }
 
-WORD (print)
+static void
+print ()
 {
   if (*isp == 1)
     write_buffer (tos, stdout);
@@ -1443,7 +1511,7 @@ read_in (str, file)
 }
 
 static void
-usage (void)
+usage ()
 {
   fprintf (stderr, "usage: -[d|i|g] <file >file\n");
   exit (33);
@@ -1534,6 +1602,7 @@ main (ac, av)
              read_in (&b, f);
              compile (b.ptr);
              perform ();
+             delete_string (&b);
            }
          else if (av[i][1] == 'i')
            {
@@ -1548,10 +1617,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;
This page took 0.033372 seconds and 4 git commands to generate.