Bash-4.3 distribution sources and documentation
[platform/upstream/bash.git] / builtins / mkbuiltins.c
index 139a46f..15bb80f 100644 (file)
@@ -1,25 +1,34 @@
 /* mkbuiltins.c - Create builtins.c, builtext.h, and builtdoc.c from
    a single source file called builtins.def. */
 
-/* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2011 Free Software Foundation, Inc.
 
-This file is part of GNU Bash, the Bourne Again SHell.
+   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 2, or (at your option) any later
-version.
+   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 3 of the License, or
+   (at your option) any later version.
 
-Bash 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.
+   Bash 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 Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
-#include <config.h>
+#if !defined (CROSS_COMPILING) 
+#  include <config.h>
+#else  /* CROSS_COMPILING */
+/* A conservative set of defines based on POSIX/SUS3/XPG6 */
+#  define HAVE_UNISTD_H
+#  define HAVE_STRING_H
+#  define HAVE_STDLIB_H
+
+#  define HAVE_RENAME
+#endif /* CROSS_COMPILING */
 
 #if defined (HAVE_UNISTD_H)
 #  ifdef _MINIX
@@ -29,8 +38,10 @@ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 #endif
 
 #ifndef _MINIX
-#include "../bashtypes.h"
-#include <sys/file.h>
+#  include "../bashtypes.h"
+#  if defined (HAVE_SYS_FILE_H)
+#    include <sys/file.h>
+#  endif
 #endif
 
 #include "posixstat.h"
@@ -38,9 +49,16 @@ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
 
 #include "../bashansi.h"
 #include <stdio.h>
+#include <errno.h>
+
+#include "stdc.h"
 
 #define DOCFILE "builtins.texi"
 
+#ifndef errno
+extern int errno;
+#endif
+
 static char *xmalloc (), *xrealloc ();
 
 #if !defined (__STDC__) && !defined (strcpy)
@@ -53,6 +71,9 @@ extern char *strcpy ();
 /* Flag values that builtins can have. */
 #define BUILTIN_FLAG_SPECIAL   0x01
 #define BUILTIN_FLAG_ASSIGNMENT 0x02
+#define BUILTIN_FLAG_POSIX_BUILTIN 0x04
+
+#define BASE_INDENT    4
 
 /* If this stream descriptor is non-zero, then write
    texinfo documentation to it. */
@@ -64,9 +85,21 @@ int only_documentation = 0;
 /* Non-zero means to not do any productions. */
 int inhibit_production = 0;
 
-#if !defined (OLDCODE)
-int no_long_document = 0;
-#endif /* !OLDCODE */
+/* Non-zero means to not add functions (xxx_builtin) to the members of the
+   produced `struct builtin []' */
+int inhibit_functions = 0;
+
+/* Non-zero means to produce separate help files for each builtin, named by
+   the builtin name, in `./helpfiles'. */
+int separate_helpfiles = 0;
+
+/* Non-zero means to create single C strings for each `longdoc', with
+   embedded newlines, for ease of translation. */
+int single_longdoc_strings = 1;
+
+/* The name of a directory into which the separate external help files will
+   eventually be installed. */
+char *helpfile_directory;
 
 /* The name of a directory to precede the filename when reporting
    errors. */
@@ -115,7 +148,7 @@ ARRAY *saved_builtins = (ARRAY *)NULL;
 char *special_builtins[] =
 {
   ":", ".", "source", "break", "continue", "eval", "exec", "exit",
-  "export", "readonly", "return", "set", "shift", "trap", "unset",
+  "export", "readonly", "return", "set", "shift", "times", "trap", "unset",
   (char *)NULL
 };
 
@@ -126,9 +159,18 @@ char *assignment_builtins[] =
   (char *)NULL
 };
 
+/* The builtin commands that are special to the POSIX search order. */
+char *posix_builtins[] =
+{
+  "alias", "bg", "cd", "command", "false", "fc", "fg", "getopts", "jobs",
+  "kill", "newgrp", "pwd", "read", "true", "umask", "unalias", "wait",
+  (char *)NULL
+};
+
 /* Forward declarations. */
 static int is_special_builtin ();
 static int is_assignment_builtin ();
+static int is_posix_builtin ();
 
 #if !defined (HAVE_RENAME)
 static int rename ();
@@ -147,15 +189,20 @@ void write_documentation ();
 void write_longdocs ();
 void write_builtins ();
 
+int write_helpfiles ();
+
 void free_defs ();
 void add_documentation ();
 
 void must_be_building ();
 void remove_trailing_whitespace ();
+
+#define document_name(b)       ((b)->docname ? (b)->docname : (b)->name)
+
 \f
 /* For each file mentioned on the command line, process it and
    write the information to STRUCTFILE and EXTERNFILE, while
-   creating the production file if neccessary. */
+   creating the production file if necessary. */
 int
 main (argc, argv)
      int argc;
@@ -179,6 +226,8 @@ main (argc, argv)
        struct_filename = argv[arg_index++];
       else if (strcmp (arg, "-noproduction") == 0)
        inhibit_production = 1;
+      else if (strcmp (arg, "-nofunctions") == 0)
+       inhibit_functions = 1;
       else if (strcmp (arg, "-document") == 0)
        documentation_file = fopen (documentation_filename, "w");
       else if (strcmp (arg, "-D") == 0)
@@ -202,10 +251,13 @@ main (argc, argv)
          only_documentation = 1;
          documentation_file = fopen (documentation_filename, "w");
        }
-#if !defined (OLDCODE)
-      else if (strcmp (arg, "-nodocument") == 0)
-       no_long_document = 1;
-#endif /* !OLDCODE */  
+      else if (strcmp (arg, "-H") == 0)
+        {
+         separate_helpfiles = 1;
+         helpfile_directory = argv[arg_index++];
+        }
+      else if (strcmp (arg, "-S") == 0)
+       single_longdoc_strings = 0;
       else
        {
          fprintf (stderr, "%s: Unknown flag %s.\n", argv[0], arg);
@@ -223,7 +275,7 @@ main (argc, argv)
       if (struct_filename)
        {
          temp_struct_filename = xmalloc (15);
-         sprintf (temp_struct_filename, "mk-%d", (int) getpid ());
+         sprintf (temp_struct_filename, "mk-%ld", (long) getpid ());
          structfile = fopen (temp_struct_filename, "w");
 
          if (!structfile)
@@ -276,6 +328,14 @@ main (argc, argv)
        fclose (externfile);
     }
 
+#if 0
+  /* This is now done by a different program */
+  if (separate_helpfiles)
+    {
+      write_helpfiles (saved_builtins);
+    }
+#endif
+
   if (documentation_file)
     {
       fprintf (documentation_file, "@end ftable\n");
@@ -339,7 +399,7 @@ copy_string_array (array)
   return (copy);
 }
 
-/* Add ELEMENT to ARRAY, growing the array if neccessary. */
+/* Add ELEMENT to ARRAY, growing the array if necessary. */
 void
 array_add (element, array)
      char *element;
@@ -349,14 +409,8 @@ array_add (element, array)
     array->array = (char **)xrealloc
       (array->array, (array->size += array->growth_rate) * array->width);
 
-#if defined (HAVE_BCOPY)
-  bcopy (&element, (char *) &(array->array[array->sindex]), array->width);
-  array->sindex++;
-  bzero ((char *) &(array->array[array->sindex]), array->width);
-#else
   array->array[array->sindex++] = element;
   array->array[array->sindex] = (char *)NULL;
-#endif /* !HAVE_BCOPY */
 }
 
 /* Free an allocated array and data pointer. */
@@ -378,17 +432,22 @@ array_free (array)
 
 /* The definition of a function. */
 typedef int Function ();
+typedef int mk_handler_func_t __P((char *, DEF_FILE *, char *));
 
 /* Structure handles processor directives. */
 typedef struct {
   char *directive;
-  Function *function;
+  mk_handler_func_t *function;
 } HANDLER_ENTRY;
 
-extern int
-  builtin_handler (), function_handler (), short_doc_handler (),
-  comment_handler (), depends_on_handler (), produces_handler (),
-  end_handler (), docname_handler ();
+extern int builtin_handler __P((char *, DEF_FILE *, char *));
+extern int function_handler __P((char *, DEF_FILE *, char *));
+extern int short_doc_handler __P((char *, DEF_FILE *, char *));
+extern int comment_handler __P((char *, DEF_FILE *, char *));
+extern int depends_on_handler __P((char *, DEF_FILE *, char *));
+extern int produces_handler __P((char *, DEF_FILE *, char *));
+extern int end_handler __P((char *, DEF_FILE *, char *));
+extern int docname_handler __P((char *, DEF_FILE *, char *));
 
 HANDLER_ENTRY handlers[] = {
   { "BUILTIN", builtin_handler },
@@ -400,7 +459,7 @@ HANDLER_ENTRY handlers[] = {
   { "DEPENDS_ON", depends_on_handler },
   { "PRODUCES", produces_handler },
   { "END", end_handler },
-  { (char *)NULL, (Function *)NULL }
+  { (char *)NULL, (mk_handler_func_t *)NULL }
 };
 
 /* Return the entry in the table of handlers for NAME. */
@@ -469,6 +528,7 @@ extract_info (filename, structfile, externfile)
   if (nr == 0)
     {
       fprintf (stderr, "mkbuiltins: %s: skipping zero-length file\n", filename);
+      free (buffer);
       return;
     }
 
@@ -487,7 +547,7 @@ extract_info (filename, structfile, externfile)
     {
       array_add (&buffer[i], defs->lines);
 
-      while (buffer[i] != '\n' && i < file_size)
+      while (i < file_size && buffer[i] != '\n')
        i++;
       buffer[i++] = '\0';
     }
@@ -724,8 +784,9 @@ add_documentation (defs, line)
 /* How to handle the $BUILTIN directive. */
 int
 builtin_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   BUILTIN_DESC *new;
   char *name;
@@ -759,6 +820,8 @@ builtin_handler (self, defs, arg)
     new->flags |= BUILTIN_FLAG_SPECIAL;
   if (is_assignment_builtin (name))
     new->flags |= BUILTIN_FLAG_ASSIGNMENT;
+  if (is_posix_builtin (name))
+    new->flags |= BUILTIN_FLAG_POSIX_BUILTIN;
 
   array_add ((char *)new, defs->builtins);
   building_builtin = 1;
@@ -769,8 +832,9 @@ builtin_handler (self, defs, arg)
 /* How to handle the $FUNCTION directive. */
 int
 function_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   register BUILTIN_DESC *builtin;
 
@@ -793,8 +857,9 @@ function_handler (self, defs, arg)
 /* How to handle the $DOCNAME directive. */
 int
 docname_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   register BUILTIN_DESC *builtin;
 
@@ -812,8 +877,9 @@ docname_handler (self, defs, arg)
 /* How to handle the $SHORT_DOC directive. */
 int
 short_doc_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   register BUILTIN_DESC *builtin;
 
@@ -830,9 +896,10 @@ short_doc_handler (self, defs, arg)
 
 /* How to handle the $COMMENT directive. */
 int
-comment_handler (self, defs)
+comment_handler (self, defs, arg)
      char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   return (0);
 }
@@ -840,8 +907,9 @@ comment_handler (self, defs)
 /* How to handle the $DEPENDS_ON directive. */
 int
 depends_on_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   register BUILTIN_DESC *builtin;
   char *dependent;
@@ -860,8 +928,9 @@ depends_on_handler (self, defs, arg)
 /* How to handle the $PRODUCES directive. */
 int
 produces_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   /* If just hacking documentation, don't change any of the production
      files. */
@@ -893,8 +962,9 @@ produces_handler (self, defs, arg)
 /* How to handle the $END directive. */
 int
 end_handler (self, defs, arg)
-     char *self, *arg;
+     char *self;
      DEF_FILE *defs;
+     char *arg;
 {
   must_be_building (self, defs);
   building_builtin = 0;
@@ -1021,8 +1091,10 @@ save_builtin (builtin)
 }
 
 /* Flags that mean something to write_documentation (). */
-#define STRING_ARRAY 1
-#define TEXINFO 2
+#define STRING_ARRAY   0x01
+#define TEXINFO                0x02
+#define PLAINTEXT      0x04
+#define HELPFILE       0x08
 
 char *structfile_header[] = {
   "/* builtins.c -- the built in shell commands. */",
@@ -1030,23 +1102,23 @@ char *structfile_header[] = {
   "/* This file is manufactured by ./mkbuiltins, and should not be",
   "   edited by hand.  See the source to mkbuiltins for details. */",
   "",
-  "/* Copyright (C) 1987, 1991, 1992 Free Software Foundation, Inc.",
+  "/* Copyright (C) 1987-2012 Free Software Foundation, Inc.",
   "",
   "   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 2, or (at your option)",
-  "   any later version.",
+  "   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 3 of the License, or",
+  "   (at your option) any later version.",
   "",
-  "   Bash 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.",
+  "   Bash 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 Bash; see the file COPYING.  If not, write to the Free",
-  "   Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */",
+  "   along with Bash.  If not, see <http://www.gnu.org/licenses/>.",
+  "*/",
   "",
   "/* 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",
@@ -1056,7 +1128,7 @@ char *structfile_header[] = {
   "",
   "   Functions which need to look at only the simple commands (e.g.",
   "   the enable_builtin ()), should ignore entries where",
-  "   (array[i].function == (Function *)NULL).  Such entries are for",
+  "   (array[i].function == (sh_builtin_func_t *)NULL).  Such entries are for",
   "   the list of shell reserved control structures, like `if' and `while'.",
   "   The end of the list is denoted with a NULL name field. */",
   "",
@@ -1065,7 +1137,7 @@ char *structfile_header[] = {
   };
 
 char *structfile_footer[] = {
-  "  { (char *)0x0, (Function *)0x0, 0, (char **)0x0, (char *)0x0 }",
+  "  { (char *)0x0, (sh_builtin_func_t *)0x0, 0, (char **)0x0, (char *)0x0, (char *)0x0 }",
   "};",
   "",
   "struct builtin *shell_builtins = static_shell_builtins;",
@@ -1076,7 +1148,7 @@ char *structfile_footer[] = {
   (char *)NULL
 };
 
-/* Write out any neccessary opening information for
+/* Write out any necessary opening information for
    STRUCTFILE and EXTERNFILE. */
 void
 write_file_headers (structfile, externfile)
@@ -1091,6 +1163,9 @@ write_file_headers (structfile, externfile)
 
       fprintf (structfile, "#include \"%s\"\n",
               extern_filename ? extern_filename : "builtext.h");
+
+      fprintf (structfile, "#include \"bashintl.h\"\n");
+
       fprintf (structfile, "\nstruct builtin static_shell_builtins[] = {\n");
     }
 
@@ -1147,11 +1222,11 @@ write_builtins (defs, structfile, externfile)
              if (externfile)
                {
                  if (builtin->function)
-                   fprintf (externfile, "extern int %s ();\n",
+                   fprintf (externfile, "extern int %s __P((WORD_LIST *));\n",
                             builtin->function);
 
-                 fprintf (externfile, "extern char *%s_doc[];\n",
-                          builtin->docname ? builtin->docname : builtin->name);
+                 fprintf (externfile, "extern char * const %s_doc[];\n",
+                          document_name (builtin));
                }
 
              /* Write the structure definition. */
@@ -1159,27 +1234,36 @@ write_builtins (defs, structfile, externfile)
                {
                  fprintf (structfile, "  { \"%s\", ", builtin->name);
 
-                 if (builtin->function)
+                 if (builtin->function && inhibit_functions == 0)
                    fprintf (structfile, "%s, ", builtin->function);
                  else
-                   fprintf (structfile, "(Function *)0x0, ");
+                   fprintf (structfile, "(sh_builtin_func_t *)0x0, ");
 
-                 fprintf (structfile, "%s%s%s, %s_doc,\n",
+                 fprintf (structfile, "%s%s%s%s, %s_doc,\n",
                    "BUILTIN_ENABLED | STATIC_BUILTIN",
                    (builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
                    (builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
-                   builtin->docname ? builtin->docname : builtin->name);
-
-                 fprintf
-                   (structfile, "     \"%s\", (char *)NULL },\n",
-                    builtin->shortdoc ? builtin->shortdoc : builtin->name);
+                   (builtin->flags & BUILTIN_FLAG_POSIX_BUILTIN) ? " | POSIX_BUILTIN" : "",
+                   document_name (builtin));
+
+                 if (inhibit_functions)
+                   fprintf
+                     (structfile, "     N_(\"%s\"), \"%s\" },\n",
+                      builtin->shortdoc ? builtin->shortdoc : builtin->name,
+                      document_name (builtin));
+                 else
+                   fprintf
+                     (structfile, "     N_(\"%s\"), (char *)NULL },\n",
+                      builtin->shortdoc ? builtin->shortdoc : builtin->name);
 
-                 /* Save away this builtin for later writing of the
-                    long documentation strings. */
-                 save_builtin (builtin);
                }
 
-             /* Write out the matching #endif, if neccessary. */
+             if (structfile || separate_helpfiles)
+               /* Save away this builtin for later writing of the
+                  long documentation strings. */
+               save_builtin (builtin);
+
+             /* Write out the matching #endif, if necessary. */
              if (builtin->dependencies)
                {
                  if (externfile)
@@ -1208,6 +1292,8 @@ write_longdocs (stream, builtins)
 {
   register int i;
   register BUILTIN_DESC *builtin;
+  char *dname;
+  char *sarray[2];
 
   for (i = 0; i < builtins->sindex; i++)
     {
@@ -1217,9 +1303,20 @@ write_longdocs (stream, builtins)
        write_ifdefs (stream, builtin->dependencies->array);
 
       /* Write the long documentation strings. */
-      fprintf (stream, "char *%s_doc[] =",
-              builtin->docname ? builtin->docname : builtin->name);
-      write_documentation (stream, builtin->longdoc->array, 0, STRING_ARRAY);
+      dname = document_name (builtin);
+      fprintf (stream, "char * const %s_doc[] =", dname);
+
+      if (separate_helpfiles)
+       {
+         int l = strlen (helpfile_directory) + strlen (dname) + 1;
+         sarray[0] = (char *)xmalloc (l + 1);
+         sprintf (sarray[0], "%s/%s", helpfile_directory, dname);
+         sarray[1] = (char *)NULL;
+         write_documentation (stream, sarray, 0, STRING_ARRAY|HELPFILE);
+         free (sarray[0]);
+       }
+      else
+       write_documentation (stream, builtin->longdoc->array, 0, STRING_ARRAY);
 
       if (builtin->dependencies)
        write_endifs (stream, builtin->dependencies->array);
@@ -1227,6 +1324,26 @@ write_longdocs (stream, builtins)
     }
 }
 
+void
+write_dummy_declarations (stream, builtins)
+     FILE *stream;
+     ARRAY *builtins;
+{
+  register int i;
+  BUILTIN_DESC *builtin;
+
+  for (i = 0; structfile_header[i]; i++)
+    fprintf (stream, "%s\n", structfile_header[i]);
+
+  for (i = 0; i < builtins->sindex; i++)
+    {
+      builtin = (BUILTIN_DESC *)builtins->array[i];
+
+      /* How to guarantee that no builtin is written more than once? */
+      fprintf (stream, "int %s () { return (0); }\n", builtin->function);
+    }
+}
+
 /* Write an #ifdef string saying what needs to be defined (or not defined)
    in order to allow compilation of the code that will follow.
    STREAM is the stream to write the information to,
@@ -1287,8 +1404,10 @@ write_endifs (stream, defines)
   fprintf (stream, " */\n");
 }
 
-/* Write DOCUMENTAION to STREAM, perhaps surrounding it with double-quotes
-   and quoting special characters in the string. */
+/* Write DOCUMENTATION to STREAM, perhaps surrounding it with double-quotes
+   and quoting special characters in the string.  Handle special things for
+   internationalization (gettext) and the single-string vs. multiple-strings
+   issues. */
 void
 write_documentation (stream, documentation, indentation, flags)
      FILE *stream;
@@ -1297,38 +1416,66 @@ write_documentation (stream, documentation, indentation, flags)
 {
   register int i, j;
   register char *line;
-  int string_array, texinfo;
+  int string_array, texinfo, base_indent, filename_p;
 
-  if (!stream)
+  if (stream == 0)
     return;
 
   string_array = flags & STRING_ARRAY;
+  filename_p = flags & HELPFILE;
+
   if (string_array)
-    fprintf (stream, " {\n#if defined (HELP_BUILTIN)\n");
+    {
+      fprintf (stream, " {\n#if defined (HELP_BUILTIN)\n");    /* } */
+      if (single_longdoc_strings)
+       {
+         if (filename_p == 0)
+           {
+             if (documentation && documentation[0] && documentation[0][0])
+               fprintf (stream,  "N_(\"");
+             else
+               fprintf (stream, "N_(\" ");             /* the empty string translates specially. */
+           }
+         else
+           fprintf (stream, "\"");
+       }
+    }
 
-#if !defined (OLDCODE)
-  /* XXX -- clean me up; for experiment only */
-  if (no_long_document)
-    goto end_of_document;
-#endif /* !OLDCODE */
+  base_indent = (string_array && single_longdoc_strings && filename_p == 0) ? BASE_INDENT : 0;
 
-  for (i = 0, texinfo = (flags & TEXINFO); line = documentation[i]; i++)
+  for (i = 0, texinfo = (flags & TEXINFO); documentation && (line = documentation[i]); i++)
     {
-      /* Allow #ifdef's to be written out verbatim. */
+      /* Allow #ifdef's to be written out verbatim, but don't put them into
+        separate help files. */
       if (*line == '#')
        {
-         if (string_array)
+         if (string_array && filename_p == 0 && single_longdoc_strings == 0)
            fprintf (stream, "%s\n", line);
          continue;
        }
 
-      if (string_array)
-       fprintf (stream, "  \"");
+      /* prefix with N_( for gettext */
+      if (string_array && single_longdoc_strings == 0)
+       {
+         if (filename_p == 0)
+           {
+             if (line[0])            
+               fprintf (stream, "  N_(\"");
+             else
+               fprintf (stream, "  N_(\" ");           /* the empty string translates specially. */
+           }
+         else
+           fprintf (stream, "  \"");
+       }
 
       if (indentation)
        for (j = 0; j < indentation; j++)
          fprintf (stream, " ");
 
+      /* Don't indent the first line, because of how the help builtin works. */
+      if (i == 0)
+       indentation += base_indent;
+
       if (string_array)
        {
          for (j = 0; line[j]; j++)
@@ -1345,7 +1492,17 @@ write_documentation (stream, documentation, indentation, flags)
                }
            }
 
-         fprintf (stream, "\",\n");
+         /* closing right paren for gettext */
+         if (single_longdoc_strings == 0)
+           {
+             if (filename_p == 0)
+               fprintf (stream, "\"),\n");
+             else
+               fprintf (stream, "\",\n");
+           }
+         else if (documentation[i+1])
+           /* don't add extra newline after last line */
+           fprintf (stream, "\\n\\\n");
        }
       else if (texinfo)
        {
@@ -1369,14 +1526,61 @@ write_documentation (stream, documentation, indentation, flags)
        fprintf (stream, "%s\n", line);
     }
 
-#if !defined (OLDCODE)
-end_of_document:
-#endif /* !OLDCODE */
+  /* closing right paren for gettext */
+  if (string_array && single_longdoc_strings)
+    {
+      if (filename_p == 0)
+       fprintf (stream, "\"),\n");
+      else
+       fprintf (stream, "\",\n");
+    }
 
   if (string_array)
     fprintf (stream, "#endif /* HELP_BUILTIN */\n  (char *)NULL\n};\n");
 }
 
+int
+write_helpfiles (builtins)
+     ARRAY *builtins;
+{
+  char *helpfile, *bname;
+  FILE *helpfp;
+  int i, hdlen;
+  BUILTIN_DESC *builtin;       
+
+  i = mkdir ("helpfiles", 0777);
+  if (i < 0 && errno != EEXIST)
+    {
+      fprintf (stderr, "write_helpfiles: helpfiles: cannot create directory\n");
+      return -1;
+    }
+
+  hdlen = strlen ("helpfiles/");
+  for (i = 0; i < builtins->sindex; i++)
+    {
+      builtin = (BUILTIN_DESC *)builtins->array[i];
+
+      bname = document_name (builtin);
+      helpfile = (char *)xmalloc (hdlen + strlen (bname) + 1);
+      sprintf (helpfile, "helpfiles/%s", bname);
+
+      helpfp = fopen (helpfile, "w");
+      if (helpfp == 0)
+       {
+         fprintf (stderr, "write_helpfiles: cannot open %s\n", helpfile);
+         free (helpfile);
+         continue;
+       }
+
+      write_documentation (helpfp, builtin->longdoc->array, 4, PLAINTEXT);
+
+      fflush (helpfp);
+      fclose (helpfp);
+      free (helpfile);
+    }
+  return 0;
+}      
+               
 static int
 _find_in_table (name, name_table)
      char *name, *name_table[];
@@ -1403,6 +1607,13 @@ is_assignment_builtin (name)
   return (_find_in_table (name, assignment_builtins));
 }
 
+static int
+is_posix_builtin (name)
+     char *name;
+{
+  return (_find_in_table (name, posix_builtins));
+}
+
 #if !defined (HAVE_RENAME)
 static int
 rename (from, to)