diff options
Diffstat (limited to 'builtins/help.def')
-rw-r--r-- | builtins/help.def | 264 |
1 files changed, 221 insertions, 43 deletions
diff --git a/builtins/help.def b/builtins/help.def index 35a6de9..f857af9 100644 --- a/builtins/help.def +++ b/builtins/help.def @@ -1,35 +1,46 @@ This file is help.def, from which is created help.c. It implements the builtin "help" in Bash. -Copyright (C) 1987-2003 Free Software Foundation, Inc. +Copyright (C) 1987-2009 Free Software Foundation, Inc. This file is part of GNU Bash, the Bourne Again SHell. -Bash is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free -Software Foundation; either version 2, or (at your option) any later -version. +Bash is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. -Bash is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or -FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -for more details. +Bash is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. -You should have received a copy of the GNU General Public License along -with Bash; see the file COPYING. If not, write to the Free Software -Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. +You should have received a copy of the GNU General Public License +along with Bash. If not, see <http://www.gnu.org/licenses/>. $PRODUCES help.c $BUILTIN help $FUNCTION help_builtin $DEPENDS_ON HELP_BUILTIN -$SHORT_DOC help [-s] [pattern ...] -Display helpful information about builtin commands. If PATTERN is +$SHORT_DOC help [-dms] [pattern ...] +Display information about builtin commands. + +Displays brief summaries of builtin commands. If PATTERN is specified, gives detailed help on all commands matching PATTERN, -otherwise a list of the builtins is printed. The -s option -restricts the output for each builtin command matching PATTERN to -a short usage synopsis. +otherwise the list of help topics is printed. + +Options: + -d output short description for each topic + -m display usage in pseudo-manpage format + -s output only a short usage synopsis for each topic matching + PATTERN + +Arguments: + PATTERN Pattern specifiying a help topic + +Exit Status: +Returns success unless PATTERN is not found or an invalid option is given. $END #include <config.h> @@ -63,7 +74,13 @@ $END extern int errno; #endif +extern const char * const bash_copyright; +extern const char * const bash_license; + static void show_builtin_command_help __P((void)); +static int open_helpfile __P((char *)); +static void show_desc __P((char *, int)); +static void show_manpage __P((char *, int)); static void show_longdoc __P((int)); /* Print out a list of the known functions in the shell, and what they do. @@ -75,14 +92,20 @@ help_builtin (list) { register int i; char *pattern, *name; - int plen, match_found, sflag; + int plen, match_found, sflag, dflag, mflag; - sflag = 0; + dflag = sflag = mflag = 0; reset_internal_getopt (); - while ((i = internal_getopt (list, "s")) != -1) + while ((i = internal_getopt (list, "dms")) != -1) { switch (i) { + case 'd': + dflag = 1; + break; + case 'm': + mflag = 1; + break; case 's': sflag = 1; break; @@ -104,10 +127,7 @@ help_builtin (list) if (glob_pattern_p (list->word->word)) { - if (list->next) - printf (_("Shell commands matching keywords `")); - else - printf (_("Shell commands matching keyword `")); + printf (ngettext ("Shell commands matching keyword `", "Shell commands matching keywords `", (list->next ? 2 : 1))); print_word_list (list, ", "); printf ("'\n\n"); } @@ -123,12 +143,22 @@ help_builtin (list) if ((strncmp (pattern, name, plen) == 0) || (strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH)) { + match_found++; + if (dflag) + { + show_desc (name, i); + continue; + } + else if (mflag) + { + show_manpage (name, i); + continue; + } + printf ("%s: %s\n", name, shell_builtins[i].short_doc); if (sflag == 0) show_longdoc (i); - - match_found++; } } } @@ -143,6 +173,21 @@ help_builtin (list) return (EXECUTION_SUCCESS); } +static int +open_helpfile (name) + char *name; +{ + int fd; + + fd = open (name, O_RDONLY); + if (fd == -1) + { + builtin_error (_("%s: cannot open: %s"), name, strerror (errno)); + return -1; + } + return fd; +} + /* By convention, enforced by mkbuiltins.c, if separate help files are being used, the long_doc array contains one string -- the full pathname of the help file for this builtin. */ @@ -158,12 +203,9 @@ show_longdoc (i) if (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL) { - fd = open (doc[0], O_RDONLY); - if (fd == -1) - { - builtin_error (_("%s: cannot open: %s"), doc[0], strerror (errno)); - return; - } + fd = open_helpfile (doc[0]); + if (fd < 0) + return; zcatfd (fd, 1, doc[0]); close (fd); } @@ -173,10 +215,123 @@ show_longdoc (i) } static void +show_desc (name, i) + char *name; + int i; +{ + register int j; + char **doc, *line; + int fd, usefile; + + doc = (char **)shell_builtins[i].long_doc; + + usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL); + if (usefile) + { + fd = open_helpfile (doc[0]); + if (fd < 0) + return; + zmapfd (fd, &line, doc[0]); + close (fd); + } + else + line = doc ? doc[0] : (char *)NULL; + + printf ("%s - ", name); + for (j = 0; line && line[j]; j++) + { + putchar (line[j]); + if (line[j] == '\n') + break; + } + + fflush (stdout); + + if (usefile) + free (line); +} + +/* Print builtin help in pseudo-manpage format. */ +static void +show_manpage (name, i) + char *name; + int i; +{ + register int j; + char **doc, *line; + int fd, usefile; + + doc = (char **)shell_builtins[i].long_doc; + + usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL); + if (usefile) + { + fd = open_helpfile (doc[0]); + if (fd < 0) + return; + zmapfd (fd, &line, doc[0]); + close (fd); + } + else + line = doc ? _(doc[0]) : (char *)NULL; + + /* NAME */ + printf ("NAME\n"); + printf ("%*s%s - ", BASE_INDENT, " ", name); + for (j = 0; line && line[j]; j++) + { + putchar (line[j]); + if (line[j] == '\n') + break; + } + printf ("\n"); + + /* SYNOPSIS */ + printf ("SYNOPSIS\n"); + printf ("%*s%s\n\n", BASE_INDENT, " ", shell_builtins[i].short_doc); + + /* DESCRIPTION */ + printf ("DESCRIPTION\n"); + if (usefile == 0) + { + for (j = 0; doc[j]; j++) + printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j])); + } + else + { + for (j = 0; line && line[j]; j++) + { + putchar (line[j]); + if (line[j] == '\n') + printf ("%*s", BASE_INDENT, " "); + } + } + putchar ('\n'); + + /* SEE ALSO */ + printf ("SEE ALSO\n"); + printf ("%*sbash(1)\n\n", BASE_INDENT, " "); + + /* IMPLEMENTATION */ + printf ("IMPLEMENTATION\n"); + printf ("%*s", BASE_INDENT, " "); + show_shell_version (0); + printf ("%*s", BASE_INDENT, " "); + printf ("%s\n", _(bash_copyright)); + printf ("%*s", BASE_INDENT, " "); + printf ("%s\n", _(bash_license)); + + fflush (stdout); + if (usefile) + free (line); +} + +static void show_builtin_command_help () { int i, j; - char blurb[36]; + int height, width; + char *t, blurb[128]; printf ( _("These shell commands are defined internally. Type `help' to see this list.\n\ @@ -187,21 +342,44 @@ Use `man -k' or `info' to find out more about commands not in this list.\n\ A star (*) next to a name means that the command is disabled.\n\ \n")); - for (i = 0; i < num_shell_builtins; i++) + t = get_string_value ("COLUMNS"); + width = (t && *t) ? atoi (t) : 80; + if (width <= 0) + width = 80; + + width /= 2; + if (width > sizeof (blurb)) + width = sizeof (blurb); + if (width <= 3) + width = 40; + height = (num_shell_builtins + 1) / 2; /* number of rows */ + + for (i = 0; i < height; i++) { QUIT; + + /* first column */ blurb[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*'; - strncpy (blurb + 1, shell_builtins[i].short_doc, 34); - blurb[35] = '\0'; + strncpy (blurb + 1, shell_builtins[i].short_doc, width - 2); + blurb[width - 2] = '>'; /* indicate truncation */ + blurb[width - 1] = '\0'; printf ("%s", blurb); + if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins)) + { + printf ("\n"); + break; + } + + /* two spaces */ + for (j = strlen (blurb); j < width; j++) + putc (' ', stdout); - if (i % 2) - printf ("\n"); - else - for (j = strlen (blurb); j < 35; j++) - putc (' ', stdout); + /* second column */ + blurb[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*'; + strncpy (blurb + 1, shell_builtins[i+height].short_doc, width - 3); + blurb[width - 3] = '>'; /* indicate truncation */ + blurb[width - 2] = '\0'; + printf ("%s\n", blurb); } - if (i % 2) - printf ("\n"); } #endif /* HELP_BUILTIN */ |