Imported from ../bash-1.14.7.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 $SHORT_DOC help [pattern ...]
27 Display helpful information about builtin commands.  If PATTERN is
28 specified, gives detailed help on all commands matching PATTERN,
29 otherwise a list of the builtins is printed.
30 $END
31
32 #include <stdio.h>
33 #include "../shell.h"
34 #include "../builtins.h"
35
36 #if defined (USE_GLOB_LIBRARY)
37 #  include <glob/glob.h>
38 #else
39 #  define FNM_NOMATCH 1
40 #endif /* USE_GLOB_LIBRARY */
41
42 /* Print out a list of the known functions in the shell, and what they do.
43    If LIST is supplied, print out the list which matches for each pattern
44    specified. */
45 help_builtin (list)
46      WORD_LIST *list;
47 {
48   if (!list)
49     {
50       register int i, j;
51       char blurb[256];
52
53       show_shell_version ();
54       printf (
55 "Shell commands that are defined internally.  Type `help' to see this list.\n\
56 Type `help name' to find out more about the function `name'.\n\
57 Use `info bash' to find out more about the shell in general.\n\
58 \n\
59 A star (*) next to a name means that the command is disabled.\n\
60 \n");
61
62       for (i = 0; i < num_shell_builtins; i++)
63         {
64           QUIT;
65           sprintf (blurb, "%c%s",
66                    (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*',
67                    shell_builtins[i].short_doc);
68
69           blurb[35] = '\0';
70           printf ("%s", blurb);
71
72           if (i % 2)
73             printf ("\n");
74           else
75             for (j = strlen (blurb); j < 35; j++)
76               putc (' ', stdout);
77
78         }
79       if (i % 2)
80         printf ("\n");
81     }
82   else
83     {
84       int match_found = 0;
85       char *pattern = "";
86
87       if (glob_pattern_p (list->word->word))
88         {
89           printf ("Shell commands matching keyword%s `",
90                   list->next ? "s" : "");
91           print_word_list (list, ", ");
92           printf ("'\n\n");
93         }
94
95       while (list)
96         {
97           register int i = 0, plen;
98           char *name;
99
100           pattern = list->word->word;
101           plen = strlen (pattern);
102
103           while (name = shell_builtins[i].name)
104             {
105               int doc_index;
106
107               QUIT;
108               if ((strncmp (pattern, name, plen) == 0) ||
109                   (fnmatch (pattern, name, 0) != FNM_NOMATCH))
110                 {
111                   printf ("%s: %s\n", name, shell_builtins[i].short_doc);
112
113                   for (doc_index = 0;
114                        shell_builtins[i].long_doc[doc_index]; doc_index++)
115                     printf ("    %s\n", shell_builtins[i].long_doc[doc_index]);
116
117                   match_found++;
118                 }
119               i++;
120             }
121           list = list->next;
122         }
123
124       if (!match_found)
125         {
126           fprintf (stderr, "No help topics match `%s'.  Try `help help'.\n",
127                    pattern);
128           fflush (stderr);
129           return (EXECUTION_FAILURE);
130         }
131     }
132   fflush (stdout);
133   return (EXECUTION_SUCCESS);
134 }