i-bash/builtins/help.def

203 lines
4.8 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is help.def, from which is created help.c.
It implements the builtin "help" in Bash.
2002-07-17 14:10:11 +00:00
Copyright (C) 1987-2002 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
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
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
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.
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
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$PRODUCES help.c
$BUILTIN help
$FUNCTION help_builtin
1996-12-23 17:02:34 +00:00
$DEPENDS_ON HELP_BUILTIN
2000-03-17 21:46:59 +00:00
$SHORT_DOC help [-s] [pattern ...]
1996-08-26 18:22:31 +00:00
Display helpful information about builtin commands. If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
2000-03-17 21:46:59 +00:00
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.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
#if defined (HELP_BUILTIN)
1996-08-26 18:22:31 +00:00
#include <stdio.h>
1996-12-23 17:02:34 +00:00
#if defined (HAVE_UNISTD_H)
1998-04-17 19:52:44 +00:00
# ifdef _MINIX
# include <sys/types.h>
# endif
1996-12-23 17:02:34 +00:00
# include <unistd.h>
#endif
2002-07-17 14:10:11 +00:00
#include <errno.h>
#include <filecntl.h>
1996-08-26 18:22:31 +00:00
#include "../shell.h"
#include "../builtins.h"
1998-04-17 19:52:44 +00:00
#include "../pathexp.h"
2001-11-13 17:56:06 +00:00
#include "common.h"
1996-12-23 17:02:34 +00:00
#include "bashgetopt.h"
2001-11-13 17:56:06 +00:00
#include <glob/strmatch.h>
1996-12-23 17:02:34 +00:00
#include <glob/glob.h>
1996-08-26 18:22:31 +00:00
2002-07-17 14:10:11 +00:00
#ifndef errno
extern int errno;
#endif
static void show_builtin_command_help __P((void));
static void show_longdoc __P((int));
1996-08-26 18:22:31 +00:00
/* Print out a list of the known functions in the shell, and what they do.
If LIST is supplied, print out the list which matches for each pattern
specified. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
help_builtin (list)
WORD_LIST *list;
{
2002-07-17 14:10:11 +00:00
register int i;
1996-12-23 17:02:34 +00:00
char *pattern, *name;
2000-03-17 21:46:59 +00:00
int plen, match_found, sflag;
1996-08-26 18:22:31 +00:00
2000-03-17 21:46:59 +00:00
sflag = 0;
1996-12-23 17:02:34 +00:00
reset_internal_getopt ();
2000-03-17 21:46:59 +00:00
while ((i = internal_getopt (list, "s")) != -1)
1996-12-23 17:02:34 +00:00
{
switch (i)
1996-08-26 18:22:31 +00:00
{
2000-03-17 21:46:59 +00:00
case 's':
sflag = 1;
break;
1996-12-23 17:02:34 +00:00
default:
builtin_usage ();
return (EX_USAGE);
1996-08-26 18:22:31 +00:00
}
}
1996-12-23 17:02:34 +00:00
list = loptend;
1996-08-26 18:22:31 +00:00
1997-06-05 14:59:13 +00:00
if (list == 0)
{
show_shell_version (0);
show_builtin_command_help ();
return (EXECUTION_SUCCESS);
}
1996-12-23 17:02:34 +00:00
/* We should consider making `help bash' do something. */
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (glob_pattern_p (list->word->word))
{
printf ("Shell commands matching keyword%s `", list->next ? "s" : "");
print_word_list (list, ", ");
printf ("'\n\n");
}
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
for (match_found = 0, pattern = ""; list; list = list->next)
{
pattern = list->word->word;
plen = strlen (pattern);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
for (i = 0; name = shell_builtins[i].name; i++)
{
QUIT;
if ((strncmp (pattern, name, plen) == 0) ||
2001-11-13 17:56:06 +00:00
(strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH))
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
printf ("%s: %s\n", name, shell_builtins[i].short_doc);
1996-08-26 18:22:31 +00:00
2000-03-17 21:46:59 +00:00
if (sflag == 0)
2002-07-17 14:10:11 +00:00
show_longdoc (i);
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
match_found++;
1996-08-26 18:22:31 +00:00
}
}
1996-12-23 17:02:34 +00:00
}
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (match_found == 0)
{
2002-07-17 14:10:11 +00:00
builtin_error ("no help topics match `%s'. Try `help help' or `man -k %s' or `info %s'.", pattern, pattern, pattern);
1996-12-23 17:02:34 +00:00
return (EXECUTION_FAILURE);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
1996-08-26 18:22:31 +00:00
fflush (stdout);
return (EXECUTION_SUCCESS);
}
1996-12-23 17:02:34 +00:00
2002-07-17 14:10:11 +00:00
/* 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. */
static void
show_longdoc (i)
int i;
{
register int j;
char * const *doc;
int fd;
doc = shell_builtins[i].long_doc;
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;
}
zcatfd (fd, 1, doc[0]);
close (fd);
}
else
for (j = 0; doc[j]; j++)
printf (" %s\n", doc[j]);
}
1996-12-23 17:02:34 +00:00
static void
show_builtin_command_help ()
{
int i, j;
char blurb[36];
printf (
"These shell commands are defined internally. Type `help' to see this list.\n\
Type `help name' to find out more about the function `name'.\n\
Use `info bash' to find out more about the shell in general.\n\
2002-07-17 14:10:11 +00:00
Use `man -k' or `info' to find out more about commands not in this list.\n\
1996-12-23 17:02:34 +00:00
\n\
A star (*) next to a name means that the command is disabled.\n\
\n");
for (i = 0; i < num_shell_builtins; i++)
{
QUIT;
blurb[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*';
strncpy (blurb + 1, shell_builtins[i].short_doc, 34);
blurb[35] = '\0';
printf ("%s", blurb);
if (i % 2)
printf ("\n");
else
for (j = strlen (blurb); j < 35; j++)
putc (' ', stdout);
}
if (i % 2)
printf ("\n");
}
#endif /* HELP_BUILTIN */