Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / builtins / mkbuiltins.c
index 7532866..c911f86 100644 (file)
@@ -1,7 +1,7 @@
 /* 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-2002 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -38,11 +38,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)
@@ -66,9 +71,13 @@ 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 produce separate help files for each builtin, named by
+   the builtin name, in `./helpfiles'. */
+int separate_helpfiles = 0;
+
+/* 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. */
@@ -149,11 +158,16 @@ 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
@@ -204,10 +218,11 @@ 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
        {
          fprintf (stderr, "%s: Unknown flag %s.\n", argv[0], arg);
@@ -278,6 +293,11 @@ main (argc, argv)
        fclose (externfile);
     }
 
+  if (separate_helpfiles)
+    {
+      write_helpfiles (saved_builtins);
+    }
+
   if (documentation_file)
     {
       fprintf (documentation_file, "@end ftable\n");
@@ -1036,8 +1056,9 @@ save_builtin (builtin)
 }
 
 /* Flags that mean something to write_documentation (). */
-#define STRING_ARRAY 1
-#define TEXINFO 2
+#define STRING_ARRAY   1
+#define TEXINFO                2
+#define PLAINTEXT      4
 
 char *structfile_header[] = {
   "/* builtins.c -- the built in shell commands. */",
@@ -1045,7 +1066,7 @@ 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-2002 Free Software Foundation, Inc.",
   "",
   "   This file is part of GNU Bash, the Bourne Again SHell.",
   "",
@@ -1165,8 +1186,8 @@ write_builtins (defs, structfile, externfile)
                    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. */
@@ -1183,17 +1204,19 @@ write_builtins (defs, structfile, externfile)
                    "BUILTIN_ENABLED | STATIC_BUILTIN",
                    (builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
                    (builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
-                   builtin->docname ? builtin->docname : builtin->name);
+                   document_name (builtin));
 
                  fprintf
                    (structfile, "     \"%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);
                }
 
+             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 neccessary. */
              if (builtin->dependencies)
                {
@@ -1223,6 +1246,8 @@ write_longdocs (stream, builtins)
 {
   register int i;
   register BUILTIN_DESC *builtin;
+  char *dname;
+  char *sarray[2];
 
   for (i = 0; i < builtins->sindex; i++)
     {
@@ -1232,9 +1257,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);
+         free (sarray[0]);
+       }
+      else
+       write_documentation (stream, builtin->longdoc->array, 0, STRING_ARRAY);
 
       if (builtin->dependencies)
        write_endifs (stream, builtin->dependencies->array);
@@ -1321,12 +1357,6 @@ write_documentation (stream, documentation, indentation, flags)
   if (string_array)
     fprintf (stream, " {\n#if defined (HELP_BUILTIN)\n");
 
-#if !defined (OLDCODE)
-  /* XXX -- clean me up; for experiment only */
-  if (no_long_document)
-    goto end_of_document;
-#endif /* !OLDCODE */
-
   for (i = 0, texinfo = (flags & TEXINFO); line = documentation[i]; i++)
     {
       /* Allow #ifdef's to be written out verbatim. */
@@ -1384,14 +1414,52 @@ write_documentation (stream, documentation, indentation, flags)
        fprintf (stream, "%s\n", line);
     }
 
-#if !defined (OLDCODE)
-end_of_document:
-#endif /* !OLDCODE */
-
   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[];