2 psh/builtins/hash.c - builtin command hash
4 Copyright 2020 Zhang Maiyun.
6 This file is part of Psh, P shell.
8 Psh 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 Psh 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 this program. If not, see <https://www.gnu.org/licenses/>.
31 #include "libpsh/util.h"
37 #define NBUILTIN (sizeof(builtin_helps) / sizeof(builtin_help_t))
38 /* Help string of all builtins */
39 #ifndef WITHOUT_BUILTIN_HELP
40 typedef char *builtin_help_t
[4];
41 const builtin_help_t builtin_helps
[63] = {
42 {".", "filename [arguments]",
43 "Execute commands from a file in the current shell.",
44 "Read and execute commands from FILENAME in the current shell. The "
45 "entries in $PATH are used to find the directory containing FILENAME. If "
46 "any ARGUMENTS are supplied, they become the positional parameters when "
47 "FILENAME is executed.\n"
49 "\tReturns the status of the last command executed in FILENAME; fails if "
50 "FILENAME cannot be read."},
52 {"alias", "", "", ""},
55 {"break", "", "", ""},
56 {"builtin", "", "", ""},
59 {"chdir", "", "", ""},
60 {"command", "", "", ""},
61 {"continue", "", "", ""},
62 {"declare", "", "", ""},
72 {"export", "", "", ""},
73 {"false", "", "", ""},
78 {"getopts", "", "", ""},
79 {"getstat", "", "", ""},
82 {"history", "", "", ""},
84 {"jobid", "", "", ""},
86 {"local", "", "", ""},
87 {"logout", "", "", ""},
89 {"pushd", "", "", ""},
93 {"readonly", "", "", ""},
94 {"return", "", "", ""},
96 {"setvar", "", "", ""},
97 {"shift", "", "", ""},
98 {"source", "", "", ""},
100 {"then", "", "", ""},
101 {"times", "", "", ""},
102 {"trap", "", "", ""},
103 {"true", "", "", ""},
104 {"type", "", "", ""},
105 {"ulimit", "", "", ""},
106 {"umask", "", "", ""},
107 {"unalias", "", "", ""},
108 {"unset", "", "", ""},
109 {"until", "", "", ""},
110 {"wait", "", "", ""},
111 {"which", "", "", ""},
112 {"while", "", "", ""}};
114 static int compare_builtin_helps(const void *key
, const void *cur
)
116 return strcmp((char *)key
, (*(builtin_help_t
*)cur
)[0]);
119 int builtin_help(int argc
, char **argv
, psh_state
*state
)
121 #ifndef WITHOUT_BUILTIN_HELP
124 /* count always points to the next possible command name */
125 while (++count
< argc
)
127 if (argv
[count
][0] != '-')
129 if (argv
[count
][1] == '-')
135 if (argv
[count
][1] == '\0')
136 /* Treat '-' as an argument. */
138 if (argv
[count
][1] == 'm')
140 else if (argv
[count
][1] == 's')
142 else if (argv
[count
][1] == 'd')
146 OUT2E("%s: %s: unrecognized argument -%c\n", state
->argv0
, argv
[0],
155 for (count2
= 0; count2
< NBUILTIN
; ++count2
)
157 printf("%s %s\n", builtin_helps
[count2
][0],
158 builtin_helps
[count2
][1]);
162 /* Go over all positional parameters, print their help */
163 for (; count
< argc
; ++count
)
165 builtin_help_t
*itm
=
166 bsearch(argv
[count
], builtin_helps
, NBUILTIN
,
167 sizeof(builtin_help_t
), &compare_builtin_helps
);
170 OUT2E("%s: %s: no help topics match `%s'. Try `help help' or `man "
171 "-k %s' or `info %s'.\n",
172 state
->argv0
, argv
[0], argv
[count
], argv
[count
], argv
[count
]);
176 /* Print help in man format. */
177 printf("NAME\n\t%s - %s\n\nSYNOPSIS\n\t%s "
178 "%s\n\nDESCRIPTION\n\t%s\n\nSEE "
179 "ALSO\n\tpsh(1)\n\nIMPLEMENTATION\n\tpsh\n",
180 (*itm
)[0], (*itm
)[2], (*itm
)[0], (*itm
)[1], (*itm
)[3]);
181 else if (type
== SHORT
)
182 printf("%s - %s\n", (*itm
)[0], (*itm
)[2]);
183 else if (type
== USAGE
)
184 printf("%s: %s %s\n", (*itm
)[0], (*itm
)[0], (*itm
)[1]);
188 OUT2E("%s: %s: Built without builtin help.\n", state
->argv0
, argv
[0]);