1 // SPDX-License-Identifier: GPL-2.0+
3 * This file defines the compilation unit for the new hush shell version. The
4 * actual implementation from upstream BusyBox can be found in
5 * `cli_hush_upstream.c` which is included at the end of this file.
7 * This "wrapper" technique is used to keep the changes to the upstream version
8 * as minmal as possible. Instead, all defines and redefines necessary are done
9 * here, outside the upstream sources. This will hopefully make upgrades to
10 * newer revisions much easier.
12 * Copyright (c) 2021, Harald Seiler, DENX Software Engineering, hws@denx.de
16 #include <malloc.h> /* malloc, free, realloc*/
17 #include <linux/ctype.h> /* isalpha, isdigit */
19 #include <bootretry.h>
22 #include <command.h> /* find_cmd */
23 #include <asm/global_data.h>
26 * BusyBox Version: UPDATE THIS WHEN PULLING NEW UPSTREAM REVISION!
28 #define BB_VER "1.35.0.git7d1c7d833785"
31 * Define hush features by the names used upstream.
33 #define ENABLE_HUSH_INTERACTIVE 1
34 #define ENABLE_FEATURE_EDITING 1
35 #define ENABLE_HUSH_IF 1
36 #define ENABLE_HUSH_LOOPS 1
37 /* No MMU in U-Boot */
39 #define USE_FOR_NOMMU(...) __VA_ARGS__
40 #define USE_FOR_MMU(...)
43 * Size-saving "small" ints (arch-dependent)
45 #if CONFIG_IS_ENABLED(X86) || CONFIG_IS_ENABLED(X86_64) || CONFIG_IS_ENABLED(MIPS)
46 /* add other arches which benefit from this... */
47 typedef signed char smallint;
48 typedef unsigned char smalluint;
50 /* for arches where byte accesses generate larger code: */
52 typedef unsigned smalluint;
56 * Alignment defines used by BusyBox.
58 #define ALIGN1 __attribute__((aligned(1)))
59 #define ALIGN2 __attribute__((aligned(2)))
60 #define ALIGN4 __attribute__((aligned(4)))
61 #define ALIGN8 __attribute__((aligned(8)))
62 #define ALIGN_PTR __attribute__((aligned(sizeof(void*))))
65 * Miscellaneous compiler/platform defines.
67 #define FAST_FUNC /* not used in U-Boot */
68 #define UNUSED_PARAM __always_unused
69 #define ALWAYS_INLINE __always_inline
70 #define NOINLINE noinline
73 * Defines to provide equivalents to what libc/BusyBox defines.
76 #define EXIT_SUCCESS 0
77 #define EXIT_FAILURE 1
80 * Stubs to provide libc/BusyBox functions based on U-Boot equivalents where it
83 #define utoa simple_itoa
85 static void __noreturn xfunc_die(void)
90 #define bb_error_msg_and_die(format, ...) do { \
91 panic("HUSH: " format, __VA_ARGS__); \
94 #define bb_simple_error_msg_and_die(msg) do { \
95 panic_str("HUSH: " msg); \
98 /* fdprintf() is used for debug output. */
99 static int __maybe_unused fdprintf(int fd, const char *format, ...)
106 va_start(args, format);
107 i = vprintf(format, args);
113 static void bb_verror_msg(const char *s, va_list p, const char* strerr)
115 /* TODO: what to do with strerr arg? */
119 static void bb_error_msg(const char *s, ...)
124 bb_verror_msg(s, p, NULL);
128 static void bb_simple_error_msg(const char *s)
130 bb_error_msg("%s", s);
133 static void *xmalloc(size_t size)
136 if (!(p = malloc(size)))
137 panic("out of memory");
141 static void *xzalloc(size_t size)
143 void *p = xmalloc(size);
148 static void *xrealloc(void *ptr, size_t size)
151 if (!(p = realloc(ptr, size)))
152 panic("out of memory");
156 static void *xmemdup(const void *s, int n)
158 return memcpy(xmalloc(n), s, n);
161 #define xstrdup strdup
162 #define xstrndup strndup
164 static void *mempcpy(void *dest, const void *src, size_t len)
166 return memcpy(dest, src, len) + len;
169 /* Like strcpy but can copy overlapping strings. */
170 static void overlapping_strcpy(char *dst, const char *src)
173 * Cheap optimization for dst == src case -
174 * better to have it here than in many callers.
177 while ((*dst = *src) != '\0') {
184 static char* skip_whitespace(const char *s)
187 * In POSIX/C locale (the only locale we care about: do we REALLY want
188 * to allow Unicode whitespace in, say, .conf files? nuts!)
189 * isspace is only these chars: "\t\n\v\f\r" and space.
190 * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
193 while (*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9))
199 static char* skip_non_whitespace(const char *s)
201 while (*s != '\0' && *s != ' ' && (unsigned char)(*s - 9) > (13 - 9))
207 #define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
208 #define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
210 static const char* endofname(const char *name)
215 if (!is_in_name(*name))
222 * list_size() - returns the number of elements in char ** before NULL.
224 * Argument must contain NULL to signalize its end.
226 * @list The list to count the number of element.
227 * @return The number of element in list.
229 static size_t list_size(char **list)
233 for (size = 0; list[size] != NULL; size++);
238 static int varcmp(const char *p, const char *q)
242 while ((c = *p) == (d = *q)) {
243 if (c == '\0' || c == '=')
257 static int u_boot_cli_readline(struct in_str *i);
260 static int u_boot_cli_readline(struct in_str *i);
263 * BusyBox globals which are needed for hush.
265 static uint8_t xfunc_error_retval;
267 static const char defifsvar[] __aligned(1) = "IFS= \t\n";
268 #define defifs (defifsvar + 4)
270 /* This define is used to check if exit command was called. */
271 #define EXIT_RET_CODE -2
274 * This define is used for changes that need be done directly in the upstream
275 * sources still. Ideally, its use should be minimized as much as possible.
281 * +-- Include of the upstream sources --+ *
284 #include "cli_hush_upstream.c"
287 * +-- Include of the upstream sources --+ *
291 int u_boot_hush_start_modern(void)
297 static int u_boot_cli_readline(struct in_str *i)
300 char __maybe_unused *ps_prompt = NULL;
303 prompt = CONFIG_SYS_PROMPT;
304 #ifdef CONFIG_SYS_PROMPT_HUSH_PS2
306 prompt = CONFIG_SYS_PROMPT_HUSH_PS2;
308 /* TODO: default value? */
309 #error "SYS_PROMPT_HUSH_PS2 is not defined!"
312 if (CONFIG_IS_ENABLED(CMDLINE_PS_SUPPORT)) {
314 ps_prompt = env_get("PS1");
316 ps_prompt = env_get("PS2");
322 return cli_readline(prompt);