Imported from ../bash-2.01.tar.gz.
[platform/upstream/bash.git] / builtins / help.def
1 This file is help.def, from which is created help.c.
2 It implements the builtin "help" in Bash.
3
4 Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 1, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING.  If not, write to the Free Software
20 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 $PRODUCES help.c
23
24 $BUILTIN help
25 $FUNCTION help_builtin
26 $DEPENDS_ON HELP_BUILTIN
27 $SHORT_DOC help [pattern ...]
28 Display helpful information about builtin commands.  If PATTERN is
29 specified, gives detailed help on all commands matching PATTERN,
30 otherwise a list of the builtins is printed.
31 $END
32
33 #include <config.h>
34
35 #if defined (HELP_BUILTIN)
36 #include <stdio.h>
37
38 #if defined (HAVE_UNISTD_H)
39 #  include <unistd.h>
40 #endif
41
42 #include "../shell.h"
43 #include "../builtins.h"
44 #include "bashgetopt.h"
45
46 #include <glob/fnmatch.h>
47 #include <glob/glob.h>
48
49 extern void builtin_error ();
50 extern void builtin_usage ();
51
52 static  void show_builtin_command_help ();
53
54 /* Print out a list of the known functions in the shell, and what they do.
55    If LIST is supplied, print out the list which matches for each pattern
56    specified. */
57 int
58 help_builtin (list)
59      WORD_LIST *list;
60 {
61   register int i, j;
62   char *pattern, *name;
63   int plen, match_found;
64
65   /* Placeholder for future options. */
66   reset_internal_getopt ();
67   while ((i = internal_getopt (list, "")) != -1)
68     {
69       switch (i)
70         {
71         default:
72           builtin_usage ();
73           return (EX_USAGE);
74         }
75     }
76   list = loptend;
77
78   if (list == 0)
79     {
80       show_shell_version (0);
81       show_builtin_command_help ();
82       return (EXECUTION_SUCCESS);
83     }
84
85   /* We should consider making `help bash' do something. */
86
87   if (glob_pattern_p (list->word->word))
88     {
89       printf ("Shell commands matching keyword%s `", list->next ? "s" : "");
90       print_word_list (list, ", ");
91       printf ("'\n\n");
92     }
93
94   for (match_found = 0, pattern = ""; list; list = list->next)
95     {
96       pattern = list->word->word;
97       plen = strlen (pattern);
98
99       for (i = 0; name = shell_builtins[i].name; i++)
100         {
101           QUIT;
102           if ((strncmp (pattern, name, plen) == 0) ||
103               (fnmatch (pattern, name, 0) != FNM_NOMATCH))
104             {
105               printf ("%s: %s\n", name, shell_builtins[i].short_doc);
106
107               for (j = 0; shell_builtins[i].long_doc[j]; j++)
108                 printf ("    %s\n", shell_builtins[i].long_doc[j]);
109
110               match_found++;
111             }
112         }
113     }
114
115   if (match_found == 0)
116     {
117       builtin_error ("no help topics match `%s'.  Try `help help'.", pattern);
118       return (EXECUTION_FAILURE);
119     }
120
121   fflush (stdout);
122   return (EXECUTION_SUCCESS);
123 }
124
125 static void
126 show_builtin_command_help ()
127 {
128   int i, j;
129   char blurb[36];
130
131   printf (
132 "These shell commands are defined internally.  Type `help' to see this list.\n\
133 Type `help name' to find out more about the function `name'.\n\
134 Use `info bash' to find out more about the shell in general.\n\
135 \n\
136 A star (*) next to a name means that the command is disabled.\n\
137 \n");
138
139   for (i = 0; i < num_shell_builtins; i++)
140     {
141       QUIT;
142       blurb[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*';
143       strncpy (blurb + 1, shell_builtins[i].short_doc, 34);
144       blurb[35] = '\0';
145       printf ("%s", blurb);
146
147       if (i % 2)
148         printf ("\n");
149       else
150         for (j = strlen (blurb); j < 35; j++)
151           putc (' ', stdout);
152     }
153   if (i % 2)
154     printf ("\n");
155 }
156 #endif /* HELP_BUILTIN */