22181b1e78566be2e6359c1f098fd1c2789f82dd
[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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22 $PRODUCES help.c
23
24 $BUILTIN help
25 $FUNCTION help_builtin
26 $DEPENDS_ON HELP_BUILTIN
27 $SHORT_DOC help [-s] [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.  The -s option
31 restricts the output for each builtin command matching PATTERN to
32 a short usage synopsis.
33 $END
34
35 #include <config.h>
36
37 #if defined (HELP_BUILTIN)
38 #include <stdio.h>
39
40 #if defined (HAVE_UNISTD_H)
41 #  ifdef _MINIX
42 #    include <sys/types.h>
43 #  endif
44 #  include <unistd.h>
45 #endif
46
47 #include "../shell.h"
48 #include "../builtins.h"
49 #include "../pathexp.h"
50 #include "common.h"
51 #include "bashgetopt.h"
52
53 #include <glob/strmatch.h>
54 #include <glob/glob.h>
55
56 static  void show_builtin_command_help __P((void));
57
58 /* Print out a list of the known functions in the shell, and what they do.
59    If LIST is supplied, print out the list which matches for each pattern
60    specified. */
61 int
62 help_builtin (list)
63      WORD_LIST *list;
64 {
65   register int i, j;
66   char *pattern, *name;
67   int plen, match_found, sflag;
68
69   sflag = 0;
70   reset_internal_getopt ();
71   while ((i = internal_getopt (list, "s")) != -1)
72     {
73       switch (i)
74         {
75         case 's':
76           sflag = 1;
77           break;
78         default:
79           builtin_usage ();
80           return (EX_USAGE);
81         }
82     }
83   list = loptend;
84
85   if (list == 0)
86     {
87       show_shell_version (0);
88       show_builtin_command_help ();
89       return (EXECUTION_SUCCESS);
90     }
91
92   /* We should consider making `help bash' do something. */
93
94   if (glob_pattern_p (list->word->word))
95     {
96       printf ("Shell commands matching keyword%s `", list->next ? "s" : "");
97       print_word_list (list, ", ");
98       printf ("'\n\n");
99     }
100
101   for (match_found = 0, pattern = ""; list; list = list->next)
102     {
103       pattern = list->word->word;
104       plen = strlen (pattern);
105
106       for (i = 0; name = shell_builtins[i].name; i++)
107         {
108           QUIT;
109           if ((strncmp (pattern, name, plen) == 0) ||
110               (strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH))
111             {
112               printf ("%s: %s\n", name, shell_builtins[i].short_doc);
113
114               if (sflag == 0)
115                 for (j = 0; shell_builtins[i].long_doc[j]; j++)
116                   printf ("    %s\n", shell_builtins[i].long_doc[j]);
117
118               match_found++;
119             }
120         }
121     }
122
123   if (match_found == 0)
124     {
125       builtin_error ("no help topics match `%s'.  Try `help help'.", pattern);
126       return (EXECUTION_FAILURE);
127     }
128
129   fflush (stdout);
130   return (EXECUTION_SUCCESS);
131 }
132
133 static void
134 show_builtin_command_help ()
135 {
136   int i, j;
137   char blurb[36];
138
139   printf (
140 "These shell commands are defined internally.  Type `help' to see this list.\n\
141 Type `help name' to find out more about the function `name'.\n\
142 Use `info bash' to find out more about the shell in general.\n\
143 \n\
144 A star (*) next to a name means that the command is disabled.\n\
145 \n");
146
147   for (i = 0; i < num_shell_builtins; i++)
148     {
149       QUIT;
150       blurb[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*';
151       strncpy (blurb + 1, shell_builtins[i].short_doc, 34);
152       blurb[35] = '\0';
153       printf ("%s", blurb);
154
155       if (i % 2)
156         printf ("\n");
157       else
158         for (j = strlen (blurb); j < 35; j++)
159           putc (' ', stdout);
160     }
161   if (i % 2)
162     printf ("\n");
163 }
164 #endif /* HELP_BUILTIN */