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