]>
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 | ||
7117c2d2 | 4 | Copyright (C) 1987-2002 Free Software Foundation, Inc. |
726f6388 JA |
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 | ||
7117c2d2 JA |
47 | #include <errno.h> |
48 | ||
49 | #include <filecntl.h> | |
50 | ||
726f6388 JA |
51 | #include "../shell.h" |
52 | #include "../builtins.h" | |
cce855bc | 53 | #include "../pathexp.h" |
f73dda09 | 54 | #include "common.h" |
ccc6cda3 JA |
55 | #include "bashgetopt.h" |
56 | ||
f73dda09 | 57 | #include <glob/strmatch.h> |
ccc6cda3 | 58 | #include <glob/glob.h> |
726f6388 | 59 | |
7117c2d2 JA |
60 | #ifndef errno |
61 | extern int errno; | |
62 | #endif | |
63 | ||
64 | static void show_builtin_command_help __P((void)); | |
65 | static void show_longdoc __P((int)); | |
726f6388 JA |
66 | |
67 | /* Print out a list of the known functions in the shell, and what they do. | |
68 | If LIST is supplied, print out the list which matches for each pattern | |
69 | specified. */ | |
ccc6cda3 | 70 | int |
726f6388 JA |
71 | help_builtin (list) |
72 | WORD_LIST *list; | |
73 | { | |
7117c2d2 | 74 | register int i; |
ccc6cda3 | 75 | char *pattern, *name; |
bb70624e | 76 | int plen, match_found, sflag; |
726f6388 | 77 | |
bb70624e | 78 | sflag = 0; |
ccc6cda3 | 79 | reset_internal_getopt (); |
bb70624e | 80 | while ((i = internal_getopt (list, "s")) != -1) |
ccc6cda3 JA |
81 | { |
82 | switch (i) | |
726f6388 | 83 | { |
bb70624e JA |
84 | case 's': |
85 | sflag = 1; | |
86 | break; | |
ccc6cda3 JA |
87 | default: |
88 | builtin_usage (); | |
89 | return (EX_USAGE); | |
726f6388 | 90 | } |
726f6388 | 91 | } |
ccc6cda3 | 92 | list = loptend; |
726f6388 | 93 | |
d166f048 JA |
94 | if (list == 0) |
95 | { | |
96 | show_shell_version (0); | |
97 | show_builtin_command_help (); | |
98 | return (EXECUTION_SUCCESS); | |
99 | } | |
100 | ||
ccc6cda3 | 101 | /* We should consider making `help bash' do something. */ |
726f6388 | 102 | |
ccc6cda3 JA |
103 | if (glob_pattern_p (list->word->word)) |
104 | { | |
105 | printf ("Shell commands matching keyword%s `", list->next ? "s" : ""); | |
106 | print_word_list (list, ", "); | |
107 | printf ("'\n\n"); | |
108 | } | |
726f6388 | 109 | |
ccc6cda3 JA |
110 | for (match_found = 0, pattern = ""; list; list = list->next) |
111 | { | |
112 | pattern = list->word->word; | |
113 | plen = strlen (pattern); | |
726f6388 | 114 | |
ccc6cda3 JA |
115 | for (i = 0; name = shell_builtins[i].name; i++) |
116 | { | |
117 | QUIT; | |
118 | if ((strncmp (pattern, name, plen) == 0) || | |
f73dda09 | 119 | (strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH)) |
726f6388 | 120 | { |
ccc6cda3 | 121 | printf ("%s: %s\n", name, shell_builtins[i].short_doc); |
726f6388 | 122 | |
bb70624e | 123 | if (sflag == 0) |
7117c2d2 | 124 | show_longdoc (i); |
726f6388 | 125 | |
ccc6cda3 | 126 | match_found++; |
726f6388 | 127 | } |
726f6388 | 128 | } |
ccc6cda3 | 129 | } |
726f6388 | 130 | |
ccc6cda3 JA |
131 | if (match_found == 0) |
132 | { | |
7117c2d2 | 133 | builtin_error ("no help topics match `%s'. Try `help help' or `man -k %s' or `info %s'.", pattern, pattern, pattern); |
ccc6cda3 | 134 | return (EXECUTION_FAILURE); |
726f6388 | 135 | } |
ccc6cda3 | 136 | |
726f6388 JA |
137 | fflush (stdout); |
138 | return (EXECUTION_SUCCESS); | |
139 | } | |
ccc6cda3 | 140 | |
7117c2d2 JA |
141 | /* By convention, enforced by mkbuiltins.c, if separate help files are being |
142 | used, the long_doc array contains one string -- the full pathname of the | |
143 | help file for this builtin. */ | |
144 | static void | |
145 | show_longdoc (i) | |
146 | int i; | |
147 | { | |
148 | register int j; | |
149 | char * const *doc; | |
150 | int fd; | |
151 | ||
152 | doc = shell_builtins[i].long_doc; | |
153 | ||
154 | if (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL) | |
155 | { | |
156 | fd = open (doc[0], O_RDONLY); | |
157 | if (fd == -1) | |
158 | { | |
159 | builtin_error ("%s: cannot open: %s", doc[0], strerror (errno)); | |
160 | return; | |
161 | } | |
162 | zcatfd (fd, 1, doc[0]); | |
163 | close (fd); | |
164 | } | |
165 | else | |
166 | for (j = 0; doc[j]; j++) | |
167 | printf (" %s\n", doc[j]); | |
168 | } | |
169 | ||
ccc6cda3 JA |
170 | static void |
171 | show_builtin_command_help () | |
172 | { | |
173 | int i, j; | |
174 | char blurb[36]; | |
175 | ||
176 | printf ( | |
177 | "These shell commands are defined internally. Type `help' to see this list.\n\ | |
178 | Type `help name' to find out more about the function `name'.\n\ | |
179 | Use `info bash' to find out more about the shell in general.\n\ | |
7117c2d2 | 180 | Use `man -k' or `info' to find out more about commands not in this list.\n\ |
ccc6cda3 JA |
181 | \n\ |
182 | A star (*) next to a name means that the command is disabled.\n\ | |
183 | \n"); | |
184 | ||
185 | for (i = 0; i < num_shell_builtins; i++) | |
186 | { | |
187 | QUIT; | |
188 | blurb[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*'; | |
189 | strncpy (blurb + 1, shell_builtins[i].short_doc, 34); | |
190 | blurb[35] = '\0'; | |
191 | printf ("%s", blurb); | |
192 | ||
193 | if (i % 2) | |
194 | printf ("\n"); | |
195 | else | |
196 | for (j = strlen (blurb); j < 35; j++) | |
197 | putc (' ', stdout); | |
198 | } | |
199 | if (i % 2) | |
200 | printf ("\n"); | |
201 | } | |
202 | #endif /* HELP_BUILTIN */ |