summaryrefslogtreecommitdiff
path: root/builtins/help.def
blob: 9c479103c0fa644b01b1492214ffd4b76d71c5e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
This file is help.def, from which is created help.c.
It implements the builtin "help" in Bash.

Copyright (C) 1987, 1989, 1991 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 1, 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.

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, 675 Mass Ave, Cambridge, MA 02139, USA.

$PRODUCES help.c

$BUILTIN help
$FUNCTION help_builtin
$DEPENDS_ON HELP_BUILTIN
$SHORT_DOC help [pattern ...]
Display helpful information about builtin commands.  If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
otherwise a list of the builtins is printed.
$END

#include <config.h>

#if defined (HELP_BUILTIN)
#include <stdio.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "../shell.h"
#include "../builtins.h"
#include "bashgetopt.h"

#include <glob/fnmatch.h>
#include <glob/glob.h>

extern void builtin_error ();
extern void builtin_usage ();

static  void show_builtin_command_help ();

/* 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. */
int
help_builtin (list)
     WORD_LIST *list;
{
  register int i, j;
  char *pattern, *name;
  int plen, match_found;

  /* Placeholder for future options. */
  reset_internal_getopt ();
  while ((i = internal_getopt (list, "")) != -1)
    {
      switch (i)
	{
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  if (list == 0)
    {
      show_shell_version (0);
      show_builtin_command_help ();
      return (EXECUTION_SUCCESS);
    }

  /* We should consider making `help bash' do something. */

  if (glob_pattern_p (list->word->word))
    {
      printf ("Shell commands matching keyword%s `", list->next ? "s" : "");
      print_word_list (list, ", ");
      printf ("'\n\n");
    }

  for (match_found = 0, pattern = ""; list; list = list->next)
    {
      pattern = list->word->word;
      plen = strlen (pattern);

      for (i = 0; name = shell_builtins[i].name; i++)
	{
	  QUIT;
	  if ((strncmp (pattern, name, plen) == 0) ||
	      (fnmatch (pattern, name, 0) != FNM_NOMATCH))
	    {
	      printf ("%s: %s\n", name, shell_builtins[i].short_doc);

	      for (j = 0; shell_builtins[i].long_doc[j]; j++)
		printf ("    %s\n", shell_builtins[i].long_doc[j]);

	      match_found++;
	    }
	}
    }

  if (match_found == 0)
    {
      builtin_error ("no help topics match `%s'.  Try `help help'.", pattern);
      return (EXECUTION_FAILURE);
    }

  fflush (stdout);
  return (EXECUTION_SUCCESS);
}

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\
\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 */