1 This file is help.def, from which is created help.c.
2 It implements the builtin "help" in Bash.
4 Copyright (C) 1987-2015 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Bash is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Bash. If not, see <http://www.gnu.org/licenses/>.
24 $FUNCTION help_builtin
25 $DEPENDS_ON HELP_BUILTIN
26 $SHORT_DOC help [-dms] [pattern ...]
27 Display information about builtin commands.
29 Displays brief summaries of builtin commands. If PATTERN is
30 specified, gives detailed help on all commands matching PATTERN,
31 otherwise the list of help topics is printed.
34 -d output short description for each topic
35 -m display usage in pseudo-manpage format
36 -s output only a short usage synopsis for each topic matching
40 PATTERN Pattern specifiying a help topic
43 Returns success unless PATTERN is not found or an invalid option is given.
48 #if defined (HELP_BUILTIN)
51 #if defined (HAVE_UNISTD_H)
53 # include <sys/types.h>
63 #include "../bashintl.h"
66 #include "../builtins.h"
67 #include "../pathexp.h"
69 #include "bashgetopt.h"
71 #include <glob/strmatch.h>
72 #include <glob/glob.h>
78 extern const char * const bash_copyright;
79 extern const char * const bash_license;
81 extern char *this_command_name;
82 extern struct builtin *current_builtin;
84 static void show_builtin_command_help __P((void));
85 static int open_helpfile __P((char *));
86 static void show_desc __P((char *, int));
87 static void show_manpage __P((char *, int));
88 static void show_longdoc __P((int));
90 /* Print out a list of the known functions in the shell, and what they do.
91 If LIST is supplied, print out the list which matches for each pattern
99 int plen, match_found, sflag, dflag, mflag, m, pass, this_found;
101 dflag = sflag = mflag = 0;
102 reset_internal_getopt ();
103 while ((i = internal_getopt (list, "dms")) != -1)
126 show_shell_version (0);
127 show_builtin_command_help ();
128 return (EXECUTION_SUCCESS);
131 /* We should consider making `help bash' do something. */
133 if (glob_pattern_p (list->word->word))
135 printf (ngettext ("Shell commands matching keyword `", "Shell commands matching keywords `", (list->next ? 2 : 1)));
136 print_word_list (list, ", ");
140 for (match_found = 0, pattern = ""; list; list = list->next)
142 pattern = list->word->word;
143 plen = strlen (pattern);
145 for (pass = 1, this_found = 0; pass < 3; pass++)
147 for (i = 0; name = shell_builtins[i].name; i++)
151 /* First pass: look for exact string or pattern matches.
152 Second pass: look for prefix matches like bash-4.2 */
154 m = (strcmp (pattern, name) == 0) ||
155 (strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH);
157 m = strncmp (pattern, name, plen) == 0;
170 show_manpage (name, i);
174 printf ("%s: %s\n", name, _(shell_builtins[i].short_doc));
180 if (pass == 1 && this_found == 1)
185 if (match_found == 0)
187 builtin_error (_("no help topics match `%s'. Try `help help' or `man -k %s' or `info %s'."), pattern, pattern, pattern);
188 return (EXECUTION_FAILURE);
192 return (EXECUTION_SUCCESS);
201 current_builtin = builtin_address_internal (this_command_name, 0);
202 if (current_builtin == 0)
205 d = current_builtin - shell_builtins;
207 #if defined (__STDC__)
210 ind = (int)d / sizeof (struct builtin);
213 printf ("%s: %s\n", this_command_name, _(shell_builtins[ind].short_doc));
223 fd = open (name, O_RDONLY);
226 builtin_error (_("%s: cannot open: %s"), name, strerror (errno));
232 /* By convention, enforced by mkbuiltins.c, if separate help files are being
233 used, the long_doc array contains one string -- the full pathname of the
234 help file for this builtin. */
243 doc = shell_builtins[i].long_doc;
245 if (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL)
247 fd = open_helpfile (doc[0]);
250 zcatfd (fd, 1, doc[0]);
254 for (j = 0; doc[j]; j++)
255 printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j]));
267 doc = (char **)shell_builtins[i].long_doc;
269 usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL);
272 fd = open_helpfile (doc[0]);
275 zmapfd (fd, &line, doc[0]);
279 line = doc ? doc[0] : (char *)NULL;
281 printf ("%s - ", name);
282 for (j = 0; line && line[j]; j++)
295 /* Print builtin help in pseudo-manpage format. */
297 show_manpage (name, i)
305 doc = (char **)shell_builtins[i].long_doc;
307 usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL);
310 fd = open_helpfile (doc[0]);
313 zmapfd (fd, &line, doc[0]);
317 line = doc ? _(doc[0]) : (char *)NULL;
321 printf ("%*s%s - ", BASE_INDENT, " ", name);
322 for (j = 0; line && line[j]; j++)
331 printf ("SYNOPSIS\n");
332 printf ("%*s%s\n\n", BASE_INDENT, " ", _(shell_builtins[i].short_doc));
335 printf ("DESCRIPTION\n");
338 for (j = 0; doc[j]; j++)
339 printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j]));
343 for (j = 0; line && line[j]; j++)
347 printf ("%*s", BASE_INDENT, " ");
353 printf ("SEE ALSO\n");
354 printf ("%*sbash(1)\n\n", BASE_INDENT, " ");
357 printf ("IMPLEMENTATION\n");
358 printf ("%*s", BASE_INDENT, " ");
359 show_shell_version (0);
360 printf ("%*s", BASE_INDENT, " ");
361 printf ("%s\n", _(bash_copyright));
362 printf ("%*s", BASE_INDENT, " ");
363 printf ("%s\n", _(bash_license));
371 dispcolumn (i, buf, bufsize, width, height)
382 helpdoc = _(shell_builtins[i].short_doc);
384 buf[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*';
385 strncpy (buf + 1, helpdoc, width - 2);
386 buf[width - 2] = '>'; /* indicate truncation */
387 buf[width - 1] = '\0';
389 if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins))
395 dispcols = strlen (buf);
397 for (j = dispcols; j < width; j++)
401 helpdoc = _(shell_builtins[i+height].short_doc);
403 buf[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*';
404 strncpy (buf + 1, helpdoc, width - 3);
405 buf[width - 3] = '>'; /* indicate truncation */
406 buf[width - 2] = '\0';
408 printf ("%s\n", buf);
411 #if defined (HANDLE_MULTIBYTE)
413 wdispcolumn (i, buf, bufsize, width, height)
420 int dispcols, dispchars;
427 helpdoc = _(shell_builtins[i].short_doc);
430 slen = mbstowcs ((wchar_t *)0, helpdoc, 0);
433 dispcolumn (i, buf, bufsize, width, height);
437 /* No bigger than the passed max width */
440 wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (width + 2));
441 n = mbstowcs (wcstr+1, helpdoc, slen + 1);
444 /* Turn tabs and newlines into spaces for column display, since wcwidth
445 returns -1 for them */
446 for (j = 1; j < n; j++)
447 if (wcstr[j] == L'\n' || wcstr[j] == L'\t')
450 /* dispchars == number of characters that will be displayed */
451 dispchars = wcsnwidth (wcstr+1, slen, width - 2);
452 /* dispcols == number of columns required to display DISPCHARS */
453 dispcols = wcswidth (wcstr+1, dispchars) + 1; /* +1 for ' ' or '*' */
455 wcstr[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? L' ' : L'*';
457 if (dispcols >= width-2)
459 wcstr[dispchars] = L'>'; /* indicate truncation */
460 wcstr[dispchars+1] = L'\0';
463 printf ("%ls", wcstr);
464 if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins))
471 /* at least one space */
472 for (j = dispcols; j < width; j++)
476 helpdoc = _(shell_builtins[i+height].short_doc);
477 slen = mbstowcs ((wchar_t *)0, helpdoc, 0);
481 printf ("%c%s\n", (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*', helpdoc);
486 /* Reuse wcstr since it is already width wide chars long */
489 n = mbstowcs (wcstr+1, helpdoc, slen + 1);
490 wcstr[n+1] = L'\0'; /* make sure null-terminated */
492 /* Turn tabs and newlines into spaces for column display */
493 for (j = 1; j < n; j++)
494 if (wcstr[j] == L'\n' || wcstr[j] == L'\t')
497 /* dispchars == number of characters that will be displayed */
498 dispchars = wcsnwidth (wcstr+1, slen, width - 2);
499 dispcols = wcswidth (wcstr+1, dispchars) + 1; /* +1 for ' ' or '*' */
501 wcstr[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? L' ' : L'*';
503 /* The dispchars-1 is there for terminals that behave strangely when you
504 have \n in the nth column for terminal width n; this is what bash-4.3
506 if (dispcols >= width - 2)
508 wcstr[dispchars-1] = L'>'; /* indicate truncation */
509 wcstr[dispchars] = L'\0';
512 printf ("%ls\n", wcstr);
516 #endif /* HANDLE_MULTIBYTE */
519 show_builtin_command_help ()
526 _("These shell commands are defined internally. Type `help' to see this list.\n\
527 Type `help name' to find out more about the function `name'.\n\
528 Use `info bash' to find out more about the shell in general.\n\
529 Use `man -k' or `info' to find out more about commands not in this list.\n\
531 A star (*) next to a name means that the command is disabled.\n\
534 t = get_string_value ("COLUMNS");
535 width = (t && *t) ? atoi (t) : 80;
540 if (width > sizeof (blurb))
541 width = sizeof (blurb);
544 height = (num_shell_builtins + 1) / 2; /* number of rows */
546 for (i = 0; i < height; i++)
550 #if defined (HANDLE_MULTIBYTE)
552 wdispcolumn (i, blurb, sizeof (blurb), width, height);
555 dispcolumn (i, blurb, sizeof (blurb), width, height);
558 #endif /* HELP_BUILTIN */