Imported from ../bash-2.05.tar.gz.
[platform/upstream/bash.git] / builtins / mkbuiltins.c
index 983bada..139a46f 100644 (file)
@@ -7,7 +7,7 @@ This file is part of GNU Bash, the Bourne Again SHell.
 
 Bash 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 1, or (at your option) any later
+Software Foundation; either version 2, or (at your option) any later
 version.
 
 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
@@ -17,22 +17,24 @@ for more details.
 
 You should have received a copy of the GNU General Public License along
 with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 #include <config.h>
 
 #if defined (HAVE_UNISTD_H)
+#  ifdef _MINIX
+#    include <sys/types.h>
+#  endif
 #  include <unistd.h>
 #endif
 
+#ifndef _MINIX
 #include "../bashtypes.h"
 #include <sys/file.h>
-#include "../posixstat.h"
-#include "../filecntl.h"
+#endif
 
-#if defined (HAVE_UNISTD_H)
-#  include <unistd.h>
-#endif /* HAVE_UNISTD_H */
+#include "posixstat.h"
+#include "filecntl.h"
 
 #include "../bashansi.h"
 #include <stdio.h>
@@ -128,6 +130,10 @@ char *assignment_builtins[] =
 static int is_special_builtin ();
 static int is_assignment_builtin ();
 
+#if !defined (HAVE_RENAME)
+static int rename ();
+#endif
+
 void extract_info ();
 
 void file_error ();
@@ -199,7 +205,7 @@ main (argc, argv)
 #if !defined (OLDCODE)
       else if (strcmp (arg, "-nodocument") == 0)
        no_long_document = 1;
-#endif /* !OLDCODE */        
+#endif /* !OLDCODE */  
       else
        {
          fprintf (stderr, "%s: Unknown flag %s.\n", argv[0], arg);
@@ -263,8 +269,7 @@ main (argc, argv)
        {
          write_longdocs (structfile, saved_builtins);
          fclose (structfile);
-         link (temp_struct_filename, struct_filename);
-         unlink (temp_struct_filename);
+         rename (temp_struct_filename, struct_filename);
        }
 
       if (externfile)
@@ -437,8 +442,9 @@ extract_info (filename, structfile, externfile)
   register int i;
   DEF_FILE *defs;
   struct stat finfo;
+  size_t file_size;
   char *buffer, *line;
-  int fd;
+  int fd, nr;
 
   if (stat (filename, &finfo) == -1)
     file_error (filename);
@@ -448,13 +454,24 @@ extract_info (filename, structfile, externfile)
   if (fd == -1)
     file_error (filename);
 
-  buffer = xmalloc (1 + (int)finfo.st_size);
+  file_size = (size_t)finfo.st_size;
+  buffer = xmalloc (1 + file_size);
 
-  if (read (fd, buffer, finfo.st_size) != finfo.st_size)
+  if ((nr = read (fd, buffer, file_size)) < 0)
     file_error (filename);
 
+  /* This is needed on WIN32, and does not hurt on Unix. */
+  if (nr < file_size)
+    file_size = nr;
+
   close (fd);
 
+  if (nr == 0)
+    {
+      fprintf (stderr, "mkbuiltins: %s: skipping zero-length file\n", filename);
+      return;
+    }
+
   /* Create and fill in the initial structure describing this file. */
   defs = (DEF_FILE *)xmalloc (sizeof (DEF_FILE));
   defs->filename = filename;
@@ -466,11 +483,11 @@ extract_info (filename, structfile, externfile)
 
   /* Build the array of lines. */
   i = 0;
-  while (i < finfo.st_size)
+  while (i < file_size)
     {
       array_add (&buffer[i], defs->lines);
 
-      while (buffer[i] != '\n' && i < finfo.st_size)
+      while (buffer[i] != '\n' && i < file_size)
        i++;
       buffer[i++] = '\0';
     }
@@ -597,10 +614,10 @@ free_defs (defs)
   if (defs->builtins)
     {
       for (i = 0; builtin = (BUILTIN_DESC *)defs->builtins->array[i]; i++)
-        {
+       {
          free_builtin (builtin);
          free (builtin);
-        }
+       }
       array_free (defs->builtins);
     }
   free (defs);
@@ -676,7 +693,10 @@ current_builtin (directive, defs)
      DEF_FILE *defs;
 {
   must_be_building (directive, defs);
-  return ((BUILTIN_DESC *)defs->builtins->array[defs->builtins->sindex - 1]);
+  if (defs->builtins)
+    return ((BUILTIN_DESC *)defs->builtins->array[defs->builtins->sindex - 1]);
+  else
+    return ((BUILTIN_DESC *)NULL);
 }
 
 /* Add LINE to the long documentation for the current builtin.
@@ -756,6 +776,11 @@ function_handler (self, defs, arg)
 
   builtin = current_builtin (self, defs);
 
+  if (builtin == 0)
+    {
+      line_error (defs, "syntax error: no current builtin for $FUNCTION directive");
+      exit (1);
+    }
   if (builtin->function)
     line_error (defs, "%s already has a function (%s)",
                builtin->name, builtin->function);
@@ -965,9 +990,9 @@ copy_builtin (builtin)
 
   new = (BUILTIN_DESC *)xmalloc (sizeof (BUILTIN_DESC));
 
-  new->name         = savestring (builtin->name);
-  new->shortdoc     = savestring (builtin->shortdoc);
-  new->longdoc      = copy_string_array (builtin->longdoc);
+  new->name = savestring (builtin->name);
+  new->shortdoc = savestring (builtin->shortdoc);
+  new->longdoc = copy_string_array (builtin->longdoc);
   new->dependencies = copy_string_array (builtin->dependencies);
 
   new->function =
@@ -1011,7 +1036,7 @@ char *structfile_header[] = {
   "",
   "   Bash 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 1, or (at your option)",
+  "   the Free Software Foundation; either version 2, or (at your option)",
   "   any later version.",
   "",
   "   Bash is distributed in the hope that it will be useful, but WITHOUT",
@@ -1021,7 +1046,7 @@ char *structfile_header[] = {
   "",
   "   You should have received a copy of the GNU General Public License",
   "   along with Bash; see the file COPYING.  If not, write to the Free",
-  "   Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */",
+  "   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */",
   "",
   "/* The list of shell builtins.  Each element is name, function, flags,",
   "   long-doc, short-doc.  The long-doc field contains a pointer to an array",
@@ -1126,7 +1151,7 @@ write_builtins (defs, structfile, externfile)
                             builtin->function);
 
                  fprintf (externfile, "extern char *%s_doc[];\n",
-                          builtin->docname ?builtin->docname : builtin->name);
+                          builtin->docname ? builtin->docname : builtin->name);
                }
 
              /* Write the structure definition. */
@@ -1377,3 +1402,16 @@ is_assignment_builtin (name)
 {
   return (_find_in_table (name, assignment_builtins));
 }
+
+#if !defined (HAVE_RENAME)
+static int
+rename (from, to)
+     char *from, *to;
+{
+  unlink (to);
+  if (link (from, to) < 0)
+    return (-1);
+  unlink (from);
+  return (0);
+}
+#endif /* !HAVE_RENAME */