| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is help.def, from which is created help.c. |
| 2 | It implements the builtin "help" in Bash. |
| 3 | |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2020 Free Software Foundation, Inc. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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/>. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES help.c |
| 22 | |
| 23 | $BUILTIN help |
| 24 | $FUNCTION help_builtin |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 25 | $DEPENDS_ON HELP_BUILTIN |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 26 | $SHORT_DOC help [-dms] [pattern ...] |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 27 | Display information about builtin commands. |
| 28 | |
| 29 | Displays brief summaries of builtin commands. If PATTERN is |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 30 | specified, gives detailed help on all commands matching PATTERN, |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 31 | otherwise the list of help topics is printed. |
| 32 | |
| 33 | Options: |
| 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 |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 37 | PATTERN |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 38 | |
| 39 | Arguments: |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 40 | PATTERN Pattern specifying a help topic |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 41 | |
| 42 | Exit Status: |
| 43 | Returns success unless PATTERN is not found or an invalid option is given. |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 44 | $END |
| 45 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 46 | #include <config.h> |
| 47 | |
| 48 | #if defined (HELP_BUILTIN) |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | |
| 51 | #if defined (HAVE_UNISTD_H) |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 52 | # ifdef _MINIX |
| 53 | # include <sys/types.h> |
| 54 | # endif |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 55 | # include <unistd.h> |
| 56 | #endif |
| 57 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 58 | #include <errno.h> |
| 59 | |
| 60 | #include <filecntl.h> |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 61 | #include <stddef.h> |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 62 | |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 63 | #include "../bashintl.h" |
| 64 | |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 65 | #include "../shell.h" |
| 66 | #include "../builtins.h" |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 67 | #include "../execute_cmd.h" |
| Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 68 | #include "../pathexp.h" |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 69 | #include "common.h" |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 70 | #include "bashgetopt.h" |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 71 | |
| Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 72 | #include <glob/strmatch.h> |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 73 | #include <glob/glob.h> |
| 74 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 75 | #ifndef errno |
| 76 | extern int errno; |
| 77 | #endif |
| 78 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 79 | extern const char * const bash_copyright; |
| 80 | extern const char * const bash_license; |
| 81 | |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 82 | static void show_builtin_command_help PARAMS((void)); |
| 83 | static int open_helpfile PARAMS((char *)); |
| 84 | static void show_desc PARAMS((char *, int)); |
| 85 | static void show_manpage PARAMS((char *, int)); |
| 86 | static void show_longdoc PARAMS((int)); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 87 | |
| 88 | /* Print out a list of the known functions in the shell, and what they do. |
| 89 | If LIST is supplied, print out the list which matches for each pattern |
| 90 | specified. */ |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 91 | int |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 92 | help_builtin (list) |
| 93 | WORD_LIST *list; |
| 94 | { |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 95 | register int i; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 96 | char *pattern, *name; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 97 | int plen, match_found, sflag, dflag, mflag, m, pass, this_found; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 98 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 99 | dflag = sflag = mflag = 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 100 | reset_internal_getopt (); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 101 | while ((i = internal_getopt (list, "dms")) != -1) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 102 | { |
| 103 | switch (i) |
| 104 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 105 | case 'd': |
| 106 | dflag = 1; |
| 107 | break; |
| 108 | case 'm': |
| 109 | mflag = 1; |
| 110 | break; |
| Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 111 | case 's': |
| 112 | sflag = 1; |
| 113 | break; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 114 | CASE_HELPOPT; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 115 | default: |
| 116 | builtin_usage (); |
| 117 | return (EX_USAGE); |
| 118 | } |
| 119 | } |
| 120 | list = loptend; |
| 121 | |
| Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 122 | if (list == 0) |
| 123 | { |
| 124 | show_shell_version (0); |
| 125 | show_builtin_command_help (); |
| 126 | return (EXECUTION_SUCCESS); |
| 127 | } |
| 128 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 129 | /* We should consider making `help bash' do something. */ |
| 130 | |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 131 | if (glob_pattern_p (list->word->word) == 1) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 132 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 133 | printf ("%s", ngettext ("Shell commands matching keyword `", "Shell commands matching keywords `", (list->next ? 2 : 1))); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 134 | print_word_list (list, ", "); |
| Chet Ramey | 8868eda | 2020-12-06 15:51:17 -0500 | [diff] [blame] | 135 | printf ("%s", _("'\n\n")); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | for (match_found = 0, pattern = ""; list; list = list->next) |
| 139 | { |
| 140 | pattern = list->word->word; |
| 141 | plen = strlen (pattern); |
| 142 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 143 | for (pass = 1, this_found = 0; pass < 3; pass++) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 144 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 145 | for (i = 0; name = shell_builtins[i].name; i++) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 146 | { |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 147 | QUIT; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 148 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 149 | /* First pass: look for exact string or pattern matches. |
| 150 | Second pass: look for prefix matches like bash-4.2 */ |
| 151 | if (pass == 1) |
| 152 | m = (strcmp (pattern, name) == 0) || |
| 153 | (strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH); |
| 154 | else |
| 155 | m = strncmp (pattern, name, plen) == 0; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 156 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 157 | if (m) |
| 158 | { |
| 159 | this_found = 1; |
| 160 | match_found++; |
| 161 | if (dflag) |
| 162 | { |
| 163 | show_desc (name, i); |
| 164 | continue; |
| 165 | } |
| 166 | else if (mflag) |
| 167 | { |
| 168 | show_manpage (name, i); |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | printf ("%s: %s\n", name, _(shell_builtins[i].short_doc)); |
| 173 | |
| 174 | if (sflag == 0) |
| 175 | show_longdoc (i); |
| 176 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 177 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 178 | if (pass == 1 && this_found == 1) |
| 179 | break; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | if (match_found == 0) |
| 184 | { |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 185 | builtin_error (_("no help topics match `%s'. Try `help help' or `man -k %s' or `info %s'."), pattern, pattern, pattern); |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 186 | return (EXECUTION_FAILURE); |
| 187 | } |
| 188 | |
| 189 | fflush (stdout); |
| 190 | return (EXECUTION_SUCCESS); |
| 191 | } |
| 192 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 193 | void |
| 194 | builtin_help () |
| 195 | { |
| 196 | int ind; |
| 197 | ptrdiff_t d; |
| 198 | |
| 199 | current_builtin = builtin_address_internal (this_command_name, 0); |
| 200 | if (current_builtin == 0) |
| 201 | return; |
| 202 | |
| 203 | d = current_builtin - shell_builtins; |
| 204 | |
| 205 | #if defined (__STDC__) |
| 206 | ind = (int)d; |
| 207 | #else |
| 208 | ind = (int)d / sizeof (struct builtin); |
| 209 | #endif |
| 210 | |
| 211 | printf ("%s: %s\n", this_command_name, _(shell_builtins[ind].short_doc)); |
| 212 | show_longdoc (ind); |
| 213 | } |
| 214 | |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 215 | static int |
| 216 | open_helpfile (name) |
| 217 | char *name; |
| 218 | { |
| 219 | int fd; |
| 220 | |
| 221 | fd = open (name, O_RDONLY); |
| 222 | if (fd == -1) |
| 223 | { |
| 224 | builtin_error (_("%s: cannot open: %s"), name, strerror (errno)); |
| 225 | return -1; |
| 226 | } |
| 227 | return fd; |
| 228 | } |
| 229 | |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 230 | /* By convention, enforced by mkbuiltins.c, if separate help files are being |
| 231 | used, the long_doc array contains one string -- the full pathname of the |
| 232 | help file for this builtin. */ |
| 233 | static void |
| 234 | show_longdoc (i) |
| 235 | int i; |
| 236 | { |
| 237 | register int j; |
| 238 | char * const *doc; |
| 239 | int fd; |
| 240 | |
| 241 | doc = shell_builtins[i].long_doc; |
| 242 | |
| 243 | if (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL) |
| 244 | { |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 245 | fd = open_helpfile (doc[0]); |
| 246 | if (fd < 0) |
| 247 | return; |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 248 | zcatfd (fd, 1, doc[0]); |
| 249 | close (fd); |
| 250 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 251 | else if (doc) |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 252 | for (j = 0; doc[j]; j++) |
| Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 253 | printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j])); |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 256 | static void |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 257 | show_desc (name, i) |
| 258 | char *name; |
| 259 | int i; |
| 260 | { |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 261 | register int j, r; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 262 | char **doc, *line; |
| 263 | int fd, usefile; |
| 264 | |
| 265 | doc = (char **)shell_builtins[i].long_doc; |
| 266 | |
| 267 | usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL); |
| 268 | if (usefile) |
| 269 | { |
| 270 | fd = open_helpfile (doc[0]); |
| 271 | if (fd < 0) |
| 272 | return; |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 273 | r = zmapfd (fd, &line, doc[0]); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 274 | close (fd); |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 275 | /* XXX - handle errors if zmapfd returns < 0 */ |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 276 | } |
| 277 | else |
| 278 | line = doc ? doc[0] : (char *)NULL; |
| 279 | |
| 280 | printf ("%s - ", name); |
| 281 | for (j = 0; line && line[j]; j++) |
| 282 | { |
| 283 | putchar (line[j]); |
| 284 | if (line[j] == '\n') |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | fflush (stdout); |
| 289 | |
| 290 | if (usefile) |
| 291 | free (line); |
| 292 | } |
| 293 | |
| 294 | /* Print builtin help in pseudo-manpage format. */ |
| 295 | static void |
| 296 | show_manpage (name, i) |
| 297 | char *name; |
| 298 | int i; |
| 299 | { |
| 300 | register int j; |
| 301 | char **doc, *line; |
| 302 | int fd, usefile; |
| 303 | |
| 304 | doc = (char **)shell_builtins[i].long_doc; |
| 305 | |
| 306 | usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL); |
| 307 | if (usefile) |
| 308 | { |
| 309 | fd = open_helpfile (doc[0]); |
| 310 | if (fd < 0) |
| 311 | return; |
| 312 | zmapfd (fd, &line, doc[0]); |
| 313 | close (fd); |
| 314 | } |
| 315 | else |
| 316 | line = doc ? _(doc[0]) : (char *)NULL; |
| 317 | |
| 318 | /* NAME */ |
| 319 | printf ("NAME\n"); |
| 320 | printf ("%*s%s - ", BASE_INDENT, " ", name); |
| 321 | for (j = 0; line && line[j]; j++) |
| 322 | { |
| 323 | putchar (line[j]); |
| 324 | if (line[j] == '\n') |
| 325 | break; |
| 326 | } |
| 327 | printf ("\n"); |
| 328 | |
| 329 | /* SYNOPSIS */ |
| 330 | printf ("SYNOPSIS\n"); |
| Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 331 | printf ("%*s%s\n\n", BASE_INDENT, " ", _(shell_builtins[i].short_doc)); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 332 | |
| 333 | /* DESCRIPTION */ |
| 334 | printf ("DESCRIPTION\n"); |
| 335 | if (usefile == 0) |
| 336 | { |
| 337 | for (j = 0; doc[j]; j++) |
| 338 | printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j])); |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | for (j = 0; line && line[j]; j++) |
| 343 | { |
| 344 | putchar (line[j]); |
| 345 | if (line[j] == '\n') |
| 346 | printf ("%*s", BASE_INDENT, " "); |
| 347 | } |
| 348 | } |
| 349 | putchar ('\n'); |
| 350 | |
| 351 | /* SEE ALSO */ |
| 352 | printf ("SEE ALSO\n"); |
| 353 | printf ("%*sbash(1)\n\n", BASE_INDENT, " "); |
| 354 | |
| 355 | /* IMPLEMENTATION */ |
| 356 | printf ("IMPLEMENTATION\n"); |
| 357 | printf ("%*s", BASE_INDENT, " "); |
| 358 | show_shell_version (0); |
| 359 | printf ("%*s", BASE_INDENT, " "); |
| 360 | printf ("%s\n", _(bash_copyright)); |
| 361 | printf ("%*s", BASE_INDENT, " "); |
| 362 | printf ("%s\n", _(bash_license)); |
| 363 | |
| 364 | fflush (stdout); |
| 365 | if (usefile) |
| 366 | free (line); |
| 367 | } |
| 368 | |
| 369 | static void |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 370 | dispcolumn (i, buf, bufsize, width, height) |
| 371 | int i; |
| 372 | char *buf; |
| 373 | size_t bufsize; |
| 374 | int width, height; |
| 375 | { |
| 376 | int j; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 377 | int dispcols; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 378 | char *helpdoc; |
| 379 | |
| 380 | /* first column */ |
| 381 | helpdoc = _(shell_builtins[i].short_doc); |
| 382 | |
| 383 | buf[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*'; |
| 384 | strncpy (buf + 1, helpdoc, width - 2); |
| 385 | buf[width - 2] = '>'; /* indicate truncation */ |
| 386 | buf[width - 1] = '\0'; |
| 387 | printf ("%s", buf); |
| 388 | if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins)) |
| 389 | { |
| 390 | printf ("\n"); |
| 391 | return; |
| 392 | } |
| 393 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 394 | dispcols = strlen (buf); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 395 | /* two spaces */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 396 | for (j = dispcols; j < width; j++) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 397 | putc (' ', stdout); |
| 398 | |
| 399 | /* second column */ |
| 400 | helpdoc = _(shell_builtins[i+height].short_doc); |
| 401 | |
| 402 | buf[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*'; |
| 403 | strncpy (buf + 1, helpdoc, width - 3); |
| 404 | buf[width - 3] = '>'; /* indicate truncation */ |
| 405 | buf[width - 2] = '\0'; |
| 406 | |
| 407 | printf ("%s\n", buf); |
| 408 | } |
| 409 | |
| 410 | #if defined (HANDLE_MULTIBYTE) |
| 411 | static void |
| 412 | wdispcolumn (i, buf, bufsize, width, height) |
| 413 | int i; |
| 414 | char *buf; |
| 415 | size_t bufsize; |
| 416 | int width, height; |
| 417 | { |
| 418 | int j; |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 419 | int dispcols, dispchars; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 420 | char *helpdoc; |
| 421 | wchar_t *wcstr; |
| 422 | size_t slen, n; |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 423 | |
| 424 | /* first column */ |
| 425 | helpdoc = _(shell_builtins[i].short_doc); |
| 426 | |
| 427 | wcstr = 0; |
| 428 | slen = mbstowcs ((wchar_t *)0, helpdoc, 0); |
| 429 | if (slen == -1) |
| 430 | { |
| 431 | dispcolumn (i, buf, bufsize, width, height); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | /* No bigger than the passed max width */ |
| 436 | if (slen >= width) |
| 437 | slen = width - 2; |
| 438 | wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (width + 2)); |
| 439 | n = mbstowcs (wcstr+1, helpdoc, slen + 1); |
| 440 | wcstr[n+1] = L'\0'; |
| 441 | |
| 442 | /* Turn tabs and newlines into spaces for column display, since wcwidth |
| 443 | returns -1 for them */ |
| 444 | for (j = 1; j < n; j++) |
| 445 | if (wcstr[j] == L'\n' || wcstr[j] == L'\t') |
| 446 | wcstr[j] = L' '; |
| 447 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 448 | /* dispchars == number of characters that will be displayed */ |
| 449 | dispchars = wcsnwidth (wcstr+1, slen, width - 2); |
| 450 | /* dispcols == number of columns required to display DISPCHARS */ |
| 451 | dispcols = wcswidth (wcstr+1, dispchars) + 1; /* +1 for ' ' or '*' */ |
| 452 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 453 | wcstr[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? L' ' : L'*'; |
| 454 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 455 | if (dispcols >= width-2) |
| 456 | { |
| 457 | wcstr[dispchars] = L'>'; /* indicate truncation */ |
| 458 | wcstr[dispchars+1] = L'\0'; |
| 459 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 460 | |
| 461 | printf ("%ls", wcstr); |
| 462 | if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins)) |
| 463 | { |
| 464 | printf ("\n"); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 465 | free (wcstr); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
| 469 | /* at least one space */ |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 470 | for (j = dispcols; j < width; j++) |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 471 | putc (' ', stdout); |
| 472 | |
| 473 | /* second column */ |
| 474 | helpdoc = _(shell_builtins[i+height].short_doc); |
| 475 | slen = mbstowcs ((wchar_t *)0, helpdoc, 0); |
| 476 | if (slen == -1) |
| 477 | { |
| 478 | /* for now */ |
| 479 | printf ("%c%s\n", (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*', helpdoc); |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 480 | free (wcstr); |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 481 | return; |
| 482 | } |
| 483 | |
| 484 | /* Reuse wcstr since it is already width wide chars long */ |
| 485 | if (slen >= width) |
| 486 | slen = width - 2; |
| 487 | n = mbstowcs (wcstr+1, helpdoc, slen + 1); |
| 488 | wcstr[n+1] = L'\0'; /* make sure null-terminated */ |
| 489 | |
| 490 | /* Turn tabs and newlines into spaces for column display */ |
| 491 | for (j = 1; j < n; j++) |
| 492 | if (wcstr[j] == L'\n' || wcstr[j] == L'\t') |
| 493 | wcstr[j] = L' '; |
| 494 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 495 | /* dispchars == number of characters that will be displayed */ |
| 496 | dispchars = wcsnwidth (wcstr+1, slen, width - 2); |
| 497 | dispcols = wcswidth (wcstr+1, dispchars) + 1; /* +1 for ' ' or '*' */ |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 498 | |
| 499 | wcstr[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? L' ' : L'*'; |
| 500 | |
| Chet Ramey | a0c0a00 | 2016-09-15 16:59:08 -0400 | [diff] [blame] | 501 | /* The dispchars-1 is there for terminals that behave strangely when you |
| 502 | have \n in the nth column for terminal width n; this is what bash-4.3 |
| 503 | did. */ |
| 504 | if (dispcols >= width - 2) |
| 505 | { |
| 506 | wcstr[dispchars-1] = L'>'; /* indicate truncation */ |
| 507 | wcstr[dispchars] = L'\0'; |
| 508 | } |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 509 | |
| 510 | printf ("%ls\n", wcstr); |
| 511 | |
| 512 | free (wcstr); |
| 513 | } |
| 514 | #endif /* HANDLE_MULTIBYTE */ |
| 515 | |
| 516 | static void |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 517 | show_builtin_command_help () |
| 518 | { |
| 519 | int i, j; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 520 | int height, width; |
| 521 | char *t, blurb[128]; |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 522 | |
| 523 | printf ( |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 524 | _("These shell commands are defined internally. Type `help' to see this list.\n\ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 525 | Type `help name' to find out more about the function `name'.\n\ |
| 526 | Use `info bash' to find out more about the shell in general.\n\ |
| Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 527 | Use `man -k' or `info' to find out more about commands not in this list.\n\ |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 528 | \n\ |
| 529 | A star (*) next to a name means that the command is disabled.\n\ |
| Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 530 | \n")); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 531 | |
| Chet Ramey | d233b48 | 2019-01-07 09:27:52 -0500 | [diff] [blame] | 532 | width = default_columns (); |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 533 | |
| 534 | width /= 2; |
| 535 | if (width > sizeof (blurb)) |
| 536 | width = sizeof (blurb); |
| Chet Ramey | 0001803 | 2011-11-21 20:51:19 -0500 | [diff] [blame] | 537 | if (width <= 3) |
| 538 | width = 40; |
| Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 539 | height = (num_shell_builtins + 1) / 2; /* number of rows */ |
| 540 | |
| 541 | for (i = 0; i < height; i++) |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 542 | { |
| 543 | QUIT; |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 544 | |
| Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 545 | #if defined (HANDLE_MULTIBYTE) |
| 546 | if (MB_CUR_MAX > 1) |
| 547 | wdispcolumn (i, blurb, sizeof (blurb), width, height); |
| 548 | else |
| 549 | #endif |
| 550 | dispcolumn (i, blurb, sizeof (blurb), width, height); |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 551 | } |
| Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 552 | } |
| Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 553 | #endif /* HELP_BUILTIN */ |