From 5f36beb44a3ecbd77861a5d255f2f809f2358a5f Mon Sep 17 00:00:00 2001 From: Noah Date: Mon, 28 Sep 2026 11:10:53 -0600 Subject: [PATCH 1/2] luckfox-sdk-patches: capture the SDK deltas that lived only in one tree A clean vendor checkout plus this series could not build WardenOS: the workstation SDK that CI copies carried tracked changes and two untracked kernel fragments that no patch recorded (#4). - board/0009: sysdrv/Makefile selects buildroot-2025.02.8 and relaxes the vendor unpack fallback's cp lines. - board/0010: removes the five 2023-era busybox/hcitool package patches. - board/0011: flare_minimize strips camera/ISP payload from /oem. - board/0012: removes the generic S50sshd init script. - board/0013: e2fsprogs BUILD_CFLAGS for gcc 14 host helpers. - kernel/configs: flare-wireguard.config and rv1106-usb-host-ethernet.config, which the 86Panel board .mk names for the vendor 5.10 kernel. Every hunk is git diff output against 824b817f8, byte for byte. Refs #4 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg --- luckfox-sdk-patches/README.md | 42 +- .../patches/0009-sysdrv-buildroot-2025.patch | 65 + ...-drop-2023-buildroot-package-patches.patch | 3148 +++++++++++++++++ .../patches/0011-oem-flare-minimize.patch | 55 + .../board/patches/0012-drop-s50sshd.patch | 65 + .../patches/0013-e2fsprogs-build-cflags.patch | 21 + .../kernel/configs/flare-wireguard.config | 10 + .../configs/rv1106-usb-host-ethernet.config | 20 + 8 files changed, 3425 insertions(+), 1 deletion(-) create mode 100644 luckfox-sdk-patches/board/patches/0009-sysdrv-buildroot-2025.patch create mode 100644 luckfox-sdk-patches/board/patches/0010-drop-2023-buildroot-package-patches.patch create mode 100644 luckfox-sdk-patches/board/patches/0011-oem-flare-minimize.patch create mode 100644 luckfox-sdk-patches/board/patches/0012-drop-s50sshd.patch create mode 100644 luckfox-sdk-patches/board/patches/0013-e2fsprogs-build-cflags.patch create mode 100644 luckfox-sdk-patches/kernel/configs/flare-wireguard.config create mode 100644 luckfox-sdk-patches/kernel/configs/rv1106-usb-host-ethernet.config diff --git a/luckfox-sdk-patches/README.md b/luckfox-sdk-patches/README.md index a0a449b..7a37651 100644 --- a/luckfox-sdk-patches/README.md +++ b/luckfox-sdk-patches/README.md @@ -57,7 +57,9 @@ luckfox-sdk-patches/uboot/include/ new source files luckfox-sdk-patches/kernel/patches/ diffs against sysdrv/source/kernel/ luckfox-sdk-patches/kernel/configs/ new kernel config fragments luckfox-sdk-patches/wifi/patches/ diffs against sysdrv/drv_ko/wifi/ (out-of-tree modules) -luckfox-sdk-patches/board/patches/ diffs against project/ (board .mk + the build driver) +luckfox-sdk-patches/board/patches/ diffs against project/ and sysdrv/ build files (board .mk, + build drivers, Makefiles, init overlays); may delete files +luckfox-sdk-patches/buildroot/ our delta on the Buildroot tree (see "Buildroot" below) ``` Paths inside each patch are relative to the vendor SDK root, so apply with @@ -87,6 +89,29 @@ skipped; else if `git apply --check` succeeds it is applied; else it is a hand- patched SDK carrying an equivalent (or a genuine conflict) and the script warns and continues rather than aborting the build. +## Buildroot + +The vendor checkout ships buildroot-2023.02.6; WardenOS builds against +buildroot-2025.02.8, which is not in the vendor checkout. +`board/patches/0006-buildroot-2025.patch` (project/build.sh) and +`board/patches/0009-sysdrv-buildroot-2025.patch` (sysdrv/Makefile) select it, +and `../build/unpack-buildroot.sh` puts the pristine tree at +`sysdrv/source/buildroot/buildroot-2025.02.8`: it fetches the tarball with +`../build/fetch-buildroot-tarball.sh` (sha256-pinned) and leaves an existing +tree alone. flare-edge's `tools/build-firmware.sh` runs it right after +applying the patches, then stages `buildroot/files/` and applies +`buildroot/patches/` onto the tree (issue #4). + +## Completeness check + +flare-edge's `tools/verify-sdk-patches.sh` replays this series onto the vendor +base and compares the result with an SDK checkout. It also fails when the SDK +has a tracked change (edit or deletion) that no patch, no fragment and no +entry in flare-edge's `tools/sdk-delta-allowlist.txt` accounts for, and when +the 86Panel board config names a kernel or U-Boot config fragment that +neither the vendor base nor this directory provides. Before that check, five +real deltas lived only in copies of one SDK tree (issue #4). + ## What each patch does ### `uboot/patches/0001-display-and-boot-timing.patch` @@ -269,6 +294,19 @@ had masked it (the reproducibility gap `tools/build-firmware.sh` exists to close `sysdrv/source/uboot/u-boot/configs/` (it is a whole new file, not a diff); the board `.mk` patch is what references it by name. +### `board/patches/0009` to `0013` (issue #4) + +Deltas that lived only in the workstation SDK until issue #4. Each patch +header explains its change. + +| patch | file | what it does | +|---|---|---| +| `0009-sysdrv-buildroot-2025.patch` | `sysdrv/Makefile` | `BUILDROOT_VER` to 2025.02.8 (the tree the `buildroot` target builds); relaxes the vendor unpack fallback's `cp` lines | +| `0010-drop-2023-buildroot-package-patches.patch` | `sysdrv/tools/board/buildroot/{busybox,hcitool}_patch/` | deletes five 2023-era package patches the fallback would copy into Buildroot | +| `0011-oem-flare-minimize.patch` | `project/cfg/BoardConfig_IPC/luckfox-buildroot-oem-pre.sh` | `flare_minimize`: strips camera/ISP files and modules from /oem | +| `0012-drop-s50sshd.patch` | `overlay-luckfox-buildroot-init/etc/init.d/S50sshd` | deletes the generic sshd starter; dropbear provides SSH | +| `0013-e2fsprogs-build-cflags.patch` | `sysdrv/tools/board/e2fsprogs/Makefile` | `BUILD_CFLAGS` so the host helpers build with gcc 14 | + ## Config fragments | fragment | why | @@ -278,6 +316,8 @@ had masked it (the reproducibility gap `tools/build-firmware.sh` exists to close | `uboot/flare-bootcount.config` | boot-failure fallback to `altbootcmd=download` -> MaskRom | | `uboot/flare-spl-brom.config` | builds `spl_bootrom.o`; does *not* set `SPL_ROCKCHIP_BACK_TO_BROM`, which would flip `SPL_MMC_SUPPORT` off | | `kernel/flare-backlight.config` | `CONFIG_BACKLIGHT_PWM=y`. As `=m` the backlight class device does not exist until userspace `insmod`s it at ~3.3-3.7 s, which is when the logo is being replaced anyway | +| `kernel/flare-wireguard.config` | `CONFIG_WIREGUARD=y` for the vendor 5.10 kernel, which builds the recovery image. Named by the 86Panel board `.mk`; untracked in the workstation SDK until issue #4 | +| `kernel/rv1106-usb-host-ethernet.config` | USB-Ethernet class drivers (with the `USB_NET_DRIVERS` menu gate) for the vendor 5.10 kernel. Same history as `flare-wireguard.config` | | `kernel/flare-fbcon.config` | `# CONFIG_LOGO is not set` (the SDK ships the LuckFox fox-head over our logo) plus `FRAMEBUFFER_CONSOLE_DEFERRED_TAKEOVER=y` (fbcon seizing the fb destroyed the `drm_logo` image U-Boot handed over) | ## Measured boot timeline diff --git a/luckfox-sdk-patches/board/patches/0009-sysdrv-buildroot-2025.patch b/luckfox-sdk-patches/board/patches/0009-sysdrv-buildroot-2025.patch new file mode 100644 index 0000000..0e38d57 --- /dev/null +++ b/luckfox-sdk-patches/board/patches/0009-sysdrv-buildroot-2025.patch @@ -0,0 +1,65 @@ +Build buildroot-2025.02.8 from sysdrv/Makefile too. + +0006-buildroot-2025.patch points project/build.sh at buildroot-2025.02.8, but +sysdrv/Makefile has its own default, BUILDROOT_VER, and that is the one the +`buildroot` target actually builds. Left at the vendor's 2023.02.6, a clean +checkout configures and builds the old tree. + +The same Makefile carries the vendor's fallback that unpacks +tools/board/buildroot/$(BUILDROOT_VER).tar.gz when the tree is missing and +copies the vendor defconfigs and package patches into it. Two relaxations +keep that fallback usable with 2025.02.8: + +- `*_defconfig` instead of only luckfox_pico_w_defconfig, so any defconfig + staged beside the vendor ones is copied too. +- `|| true` on the busybox and hcitool patch copies, because + 0010-drop-2023-buildroot-package-patches.patch empties those directories. + +The WardenOS build does not use the fallback: flare-edge's +tools/build-firmware.sh unpacks the pinned, sha256-verified tarball with +build/unpack-buildroot.sh before the SDK build runs, so the Makefile's +`test -d` finds the tree and skips its own unpack and copies. + +diff --git a/sysdrv/Makefile b/sysdrv/Makefile +index 5edb8f875..c173c219e 100644 +--- a/sysdrv/Makefile ++++ b/sysdrv/Makefile +@@ -281,7 +281,7 @@ BUSYBOX_DIR := $(SYSDRV_DIR)/source/busybox + SYSDRV_BUSYBOX_OBJS_OUTPUT_DIR ?= $(BUSYBOX_DIR)/objs_$(BUSYBOX_CFG) + + BUILDROOT_CFG ?= config_normal +-BUILDROOT_VER ?= buildroot-2023.02.6 ++BUILDROOT_VER ?= buildroot-2025.02.8 + BUILDROOT_DIR := $(SYSDRV_DIR)/source/buildroot + + CUSTOM_VER ?= custom +@@ -599,11 +599,11 @@ buildroot_create: + mkdir -p $(BUILDROOT_DIR) + tar xzf $(SYSDRV_DIR)/tools/board/buildroot/$(BUILDROOT_VER).tar.gz -C $(BUILDROOT_DIR) + cp $(SYSDRV_DIR)/tools/board/buildroot/luckfox_pico_defconfig $(BUILDROOT_DIR)/$(BUILDROOT_VER)/configs/ +- cp $(SYSDRV_DIR)/tools/board/buildroot/luckfox_pico_w_defconfig $(BUILDROOT_DIR)/$(BUILDROOT_VER)/configs/ ++ cp $(SYSDRV_DIR)/tools/board/buildroot/*_defconfig $(BUILDROOT_DIR)/$(BUILDROOT_VER)/configs/ + cp $(SYSDRV_DIR)/tools/board/buildroot/busybox.config $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/busybox/ +- cp ${HCITOOL_TOOL_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/bluez5_utils/ ++ cp ${HCITOOL_TOOL_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/bluez5_utils/ || true + cp ${MPV_PATCH_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/mpv/ +- cp ${BUSYBOX_PATCH_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/busybox/ ++ cp ${BUSYBOX_PATCH_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/busybox/ || true + + buildroot: prepare + @echo -e "$(C_GREEN) ==sysdrv== build buildroot $(C_NORMAL)" +@@ -611,11 +611,11 @@ buildroot: prepare + mkdir -p $(BUILDROOT_DIR) ;\ + tar xzf $(SYSDRV_DIR)/tools/board/buildroot/$(BUILDROOT_VER).tar.gz -C $(BUILDROOT_DIR) ;\ + cp $(SYSDRV_DIR)/tools/board/buildroot/luckfox_pico_defconfig $(BUILDROOT_DIR)/$(BUILDROOT_VER)/configs/ ;\ +- cp $(SYSDRV_DIR)/tools/board/buildroot/luckfox_pico_w_defconfig $(BUILDROOT_DIR)/$(BUILDROOT_VER)/configs/ ;\ ++ cp $(SYSDRV_DIR)/tools/board/buildroot/*_defconfig $(BUILDROOT_DIR)/$(BUILDROOT_VER)/configs/ ;\ + cp $(SYSDRV_DIR)/tools/board/buildroot/busybox.config $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/busybox/ ;\ +- cp ${HCITOOL_TOOL_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/bluez5_utils/ ;\ ++ cp ${HCITOOL_TOOL_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/bluez5_utils/ || true ;\ + cp ${MPV_PATCH_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/mpv/ ;\ +- cp ${BUSYBOX_PATCH_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/busybox/ ;\ ++ cp ${BUSYBOX_PATCH_PATH}/*.patch $(BUILDROOT_DIR)/$(BUILDROOT_VER)/package/busybox/ || true ;\ + ); + + $(MAKE) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) $(BUILDROOT_DEFCONFIG) -C $(BUILDROOT_DIR)/$(BUILDROOT_VER) diff --git a/luckfox-sdk-patches/board/patches/0010-drop-2023-buildroot-package-patches.patch b/luckfox-sdk-patches/board/patches/0010-drop-2023-buildroot-package-patches.patch new file mode 100644 index 0000000..5a804f6 --- /dev/null +++ b/luckfox-sdk-patches/board/patches/0010-drop-2023-buildroot-package-patches.patch @@ -0,0 +1,3148 @@ +Remove the 2023-era busybox and hcitool package patches. + +sysdrv/Makefile's buildroot fallback copies every patch in +tools/board/buildroot/busybox_patch/ and hcitool_patch/ into the unpacked +Buildroot tree. These five were written for buildroot-2023.02.6: + +- hcitool 0001 and 0002 no longer apply to bluez 5.79, the bluez5_utils + version buildroot-2025.02.8 ships. Our replacement, the uClibc wordexp + shim, is luckfox-sdk-patches/buildroot/files/package/bluez5_utils/. +- The two busybox patches (UTF-8 terminal display, reboot with an argument) + still apply to busybox 1.37.0 but were set aside in the move to + 2025.02.8. The busybox WardenOS ships is built without them. + +Removing them keeps the fallback consistent with the tree the firmware +build uses. The deleted files are vendor content, reproduced byte for byte +below. + +diff --git a/sysdrv/tools/board/buildroot/busybox_patch/0009-busybox-support-chinese-display-in-terminal.patch b/sysdrv/tools/board/buildroot/busybox_patch/0009-busybox-support-chinese-display-in-terminal.patch +deleted file mode 100755 +index 02c57b5f1..000000000 +--- a/sysdrv/tools/board/buildroot/busybox_patch/0009-busybox-support-chinese-display-in-terminal.patch ++++ /dev/null +@@ -1,58 +0,0 @@ +-From 095248c1d232c147f53d4c8666b80b4c1cbb2d2a Mon Sep 17 00:00:00 2001 +-From: "hero.huang" +-Date: Wed, 23 Aug 2017 11:33:47 +0800 +-Subject: [PATCH] busybox: support chinese display in terminal +- +-Signed-off-by: hero.huang +---- +- libbb/printable_string.c | 4 +--- +- libbb/unicode.c | 4 ++-- +- 2 files changed, 3 insertions(+), 5 deletions(-) +- +-diff --git a/libbb/printable_string.c b/libbb/printable_string.c +-index 077d58d..e7399ae 100644 +---- a/libbb/printable_string.c +-+++ b/libbb/printable_string.c +-@@ -28,8 +28,6 @@ const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str) +- } +- if (c < ' ') +- break; +-- if (c >= 0x7f) +-- break; +- s++; +- } +- +-@@ -42,7 +40,7 @@ const char* FAST_FUNC printable_string(uni_stat_t *stats, const char *str) +- unsigned char c = *d; +- if (c == '\0') +- break; +-- if (c < ' ' || c >= 0x7f) +-+ if (c < ' ') +- *d = '?'; +- d++; +- } +-diff --git a/libbb/unicode.c b/libbb/unicode.c +-index 9c4da50..c7eba06 100644 +---- a/libbb/unicode.c +-+++ b/libbb/unicode.c +-@@ -1019,7 +1019,7 @@ static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char +- while ((int)--width >= 0); +- break; +- } +-- *d++ = (c >= ' ' && c < 0x7f) ? c : '?'; +-+ *d++ = (c >= ' ') ? c : '?'; +- src++; +- } +- *d = '\0'; +-@@ -1027,7 +1027,7 @@ static char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char +- d = dst = xstrndup(src, width); +- while (*d) { +- unsigned char c = *d; +-- if (c < ' ' || c >= 0x7f) +-+ if (c < ' ') +- *d = '?'; +- d++; +- } +--- +-1.9.1 +- +diff --git a/sysdrv/tools/board/buildroot/busybox_patch/0010-halt-Support-rebooting-with-arg.patch b/sysdrv/tools/board/buildroot/busybox_patch/0010-halt-Support-rebooting-with-arg.patch +deleted file mode 100755 +index a7f85d93c..000000000 +--- a/sysdrv/tools/board/buildroot/busybox_patch/0010-halt-Support-rebooting-with-arg.patch ++++ /dev/null +@@ -1,73 +0,0 @@ +---- a/init/halt.c +-+++ b/init/halt.c +-@@ -93,6 +93,8 @@ +- +- #include "libbb.h" +- #include "reboot.h" +-+#include +-+#include +- +- #if ENABLE_FEATURE_WTMP +- #include +-@@ -119,6 +121,49 @@ +- #define write_wtmp() ((void)0) +- #endif +- +-+ +-+static volatile int caught_sigterm = FALSE; +-+static void signal_handler(int sig) +-+{ +-+ bb_error_msg("Caught signal %d", sig); +-+ +-+ if (sig == SIGTERM) +-+ caught_sigterm = TRUE; +-+} +-+ +-+static int reboot_with_arg(const char *arg) +-+{ +-+ struct sigaction sa; +-+ int pid; +-+ +-+ /* Fork new thread to handle reboot */ +-+ if ((pid = fork())) +-+ return pid < 0 ? pid : 0; +-+ +-+ /* Handle signal and reboot in child thread */ +-+ sigemptyset(&sa.sa_mask); +-+ sa.sa_flags = 0; +-+ sa.sa_handler = signal_handler; +-+ sigaction_set(SIGTERM, &sa); +-+ +-+ bb_error_msg("Waiting for SIGTERM"); +-+ +-+ /* The init will send SIGTERM to us after SHUTDOWN actions */ +-+ while (!caught_sigterm) +-+ usleep(50000); +-+ +-+ bb_error_msg("Ready to reboot"); +-+ +-+ /* Wait 200ms for other processes to exit */ +-+ usleep(200000); +-+ sync(); +-+ +-+ bb_error_msg("Rebooting with arg(%s)", arg); +-+ return syscall(__NR_reboot, LINUX_REBOOT_MAGIC1, +-+ LINUX_REBOOT_MAGIC2, +-+ LINUX_REBOOT_CMD_RESTART2, arg); +-+} +-+ +- #if ENABLE_FEATURE_WAIT_FOR_INIT +- /* In Linux, "poweroff" may be spawned even before init. +- * For example, with ACPI: +-@@ -239,6 +284,11 @@ +- CONFIG_TELINIT_PATH); +- } +- } +-+ +-+ /* Handle rebooting with arg */ +-+ if (signals[which] == SIGTERM && argc > 1 && argv[1][0] != '-') +-+ rc = reboot_with_arg(argv[1]); +-+ +- } else { +- rc = reboot(magic[which]); +- } +diff --git a/sysdrv/tools/board/buildroot/hcitool_patch/0001-fixed-header-file-errors.patch b/sysdrv/tools/board/buildroot/hcitool_patch/0001-fixed-header-file-errors.patch +deleted file mode 100755 +index 115dc4b6d..000000000 +--- a/sysdrv/tools/board/buildroot/hcitool_patch/0001-fixed-header-file-errors.patch ++++ /dev/null +@@ -1,81 +0,0 @@ +-From 82ecf5badb818c40fba1e7ada16d53189dfe816b Mon Sep 17 00:00:00 2001 +-From: eng33 +-Date: Tue, 30 Jan 2024 16:25:34 +0800 +-Subject: [PATCH 1/2] Fixed header file errors +- +---- +- client/main.c | 2 +- +- client/player.c | 2 +- +- src/shared/shell.c | 2 +- +- tools/btmgmt.c | 2 +- +- tools/obexctl.c | 2 +- +- 5 files changed, 5 insertions(+), 5 deletions(-) +- +-diff --git a/client/main.c b/client/main.c +-index 1df94f0..2e19538 100644 +---- a/client/main.c +-+++ b/client/main.c +-@@ -18,7 +18,7 @@ +- #include +- #include +- #include +--#include +-+#include +- +- #include +- +-diff --git a/client/player.c b/client/player.c +-index 0c59db6..e807ae0 100644 +---- a/client/player.c +-+++ b/client/player.c +-@@ -23,7 +23,7 @@ +- #include +- #include +- #include +--#include +-+#include +- +- #include +- +-diff --git a/src/shared/shell.c b/src/shared/shell.c +-index dfda812..86eda9d 100644 +---- a/src/shared/shell.c +-+++ b/src/shared/shell.c +-@@ -22,7 +22,7 @@ +- #include +- #include +- #include +--#include +-+#include +- #include +- +- #include +-diff --git a/tools/btmgmt.c b/tools/btmgmt.c +-index ebef388..0cd8700 100644 +---- a/tools/btmgmt.c +-+++ b/tools/btmgmt.c +-@@ -25,7 +25,7 @@ +- #include +- #include +- #include +--#include +-+#include +- #include +- +- #include "lib/bluetooth.h" +-diff --git a/tools/obexctl.c b/tools/obexctl.c +-index 56a7691..e8aed6a 100644 +---- a/tools/obexctl.c +-+++ b/tools/obexctl.c +-@@ -20,7 +20,7 @@ +- #include +- #include +- #include +--#include +-+#include +- #include +- +- #include +--- +-2.34.1 +- +diff --git a/sysdrv/tools/board/buildroot/hcitool_patch/0002-fixed-wordexp.patch b/sysdrv/tools/board/buildroot/hcitool_patch/0002-fixed-wordexp.patch +deleted file mode 100755 +index cfd0a0ba0..000000000 +--- a/sysdrv/tools/board/buildroot/hcitool_patch/0002-fixed-wordexp.patch ++++ /dev/null +@@ -1,2780 +0,0 @@ +-diff --git a/src/shared/shell.c b/src/shared/shell.c +-index 86eda9d..fa75b00 100644 +---- a/src/shared/shell.c +-+++ b/src/shared/shell.c +-@@ -1,3 +1,11 @@ +-+/*********************************************************** +-+ * @Author: sprink +-+ * @Date: 2025-10-28 08:00:08 +-+ * @LastEditTime: 2025-10-28 09:58:34 +-+ * @LastEditors: sprink +-+ * @Description: +-+ * +-+ ***********************************************************/ +- // SPDX-License-Identifier: LGPL-2.1-or-later +- /* +- * +-@@ -13,6 +21,9 @@ +- #endif +- +- #define _GNU_SOURCE +-+ +-+#include +-+#include +- #include +- #include +- #include +-@@ -24,6 +35,10 @@ +- #include +- #include +- #include +-+#include +-+#include +-+#include +-+#include +- +- #include +- #include +-@@ -36,1383 +51,1715 @@ +- #include "src/shared/shell.h" +- #include "src/shared/log.h" +- +--#define CMD_LENGTH 48 +-+#define CMD_LENGTH 48 +- #define print_text(color, fmt, args...) \ +-- printf(color fmt COLOR_OFF "\n", ## args) +-+ printf(color fmt COLOR_OFF "\n", ## args) +- #define print_menu(cmd, args, desc) \ +-- printf(COLOR_HIGHLIGHT "%s %-*s " COLOR_OFF "%s\n", \ +-- cmd, (int)(CMD_LENGTH - strlen(cmd)), args, desc) +-+ printf(COLOR_HIGHLIGHT "%s %-*s " COLOR_OFF "%s\n", \ +-+ cmd, (int)(CMD_LENGTH - strlen(cmd)), args, desc) +- #define print_submenu(cmd, desc) \ +-- printf(COLOR_BLUE "%s %-*s " COLOR_OFF "%s\n", \ +-- cmd, (int)(CMD_LENGTH - strlen(cmd)), "", desc) +-+ printf(COLOR_BLUE "%s %-*s " COLOR_OFF "%s\n", \ +-+ cmd, (int)(CMD_LENGTH - strlen(cmd)), "", desc) +- +--struct bt_shell_env { +-- char *name; +-- void *value; +-+/* +-+ * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX +-+ * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW +-+ */ +-+#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) +-+struct bt_shell_env +-+{ +-+ char *name; +-+ void *value; +- }; +- +- static char *cmplt = "help"; +- +--struct bt_shell_prompt_input { +-- char *str; +-- bt_shell_prompt_input_func func; +-- void *user_data; +-+struct bt_shell_prompt_input +-+{ +-+ char *str; +-+ bt_shell_prompt_input_func func; +-+ void *user_data; +- }; +- +--static struct { +-- bool init; +-- char *name; +-- char history[256]; +-- int argc; +-- char **argv; +-- bool mode; +-- bool zsh; +-- bool monitor; +-- int timeout; +-- struct io *input; +-- +-- bool saved_prompt; +-- bt_shell_prompt_input_func saved_func; +-- void *saved_user_data; +-- +-- struct queue *prompts; +-- +-- const struct bt_shell_menu *menu; +-- const struct bt_shell_menu *main; +-- struct queue *submenus; +-- const struct bt_shell_menu_entry *exec; +-- +-- struct queue *envs; +-+static struct +-+{ +-+ bool init; +-+ char *name; +-+ char history[256]; +-+ int argc; +-+ char **argv; +-+ bool mode; +-+ bool zsh; +-+ bool monitor; +-+ int timeout; +-+ struct io *input; +-+ +-+ bool saved_prompt; +-+ bt_shell_prompt_input_func saved_func; +-+ void *saved_user_data; +-+ +-+ struct queue *prompts; +-+ +-+ const struct bt_shell_menu *menu; +-+ const struct bt_shell_menu *main; +-+ struct queue *submenus; +-+ const struct bt_shell_menu_entry *exec; +-+ +-+ struct queue *envs; +- } data; +- +--static void shell_print_menu(void); +--static void shell_print_menu_zsh_complete(void); +-+static void shell_print_menu ( void ); +-+static void shell_print_menu_zsh_complete ( void ); +- +--static void cmd_version(int argc, char *argv[]) +--{ +-- bt_shell_printf("Version %s\n", VERSION); +-+static int we_askshell ( const char *, wordexp_t *, int ); +-+static int we_check ( const char * ); +- +-- return bt_shell_noninteractive_quit(EXIT_SUCCESS); +--} +-- +--static void cmd_quit(int argc, char *argv[]) +-+static void * reallocarray ( void *optr, size_t nmemb, size_t size ) +- { +-- mainloop_quit(); +--} +- +--static void print_cmds(void) +--{ +-- const struct bt_shell_menu_entry *entry; +-- const struct queue_entry *submenu; +-- +-- if (!data.menu) +-- return; +-- +-- printf("Commands:\n"); +-+ if ( ( nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW ) && +-+ nmemb > 0 && SIZE_MAX / nmemb < size ) +-+ { +-+ errno = ENOMEM; +-+ return ( NULL ); +-+ } +-+ return ( realloc ( optr, size * nmemb ) ); +-+} +- +-- for (entry = data.menu->entries; entry->cmd; entry++) { +-- printf("\t%s%s\t%s\n", entry->cmd, +-- strlen(entry->cmd) < 8 ? "\t" : "", entry->desc); +-- } +- +-- for (submenu = queue_get_entries(data.submenus); submenu; +-- submenu = submenu->next) { +-- struct bt_shell_menu *menu = submenu->data; +- +-- printf("\n\t%s.:\n", menu->name); +-+static void cmd_version ( int argc, char *argv[] ) +-+{ +-+ bt_shell_printf ( "Version %s\n", VERSION ); +- +-- for (entry = menu->entries; entry->cmd; entry++) { +-- printf("\t\t%s%s\t%s\n", entry->cmd, +-- strlen(entry->cmd) < 8 ? "\t" : "", +-- entry->desc); +-- } +-- } +-+ return bt_shell_noninteractive_quit ( EXIT_SUCCESS ); +- } +- +--static void cmd_help(int argc, char *argv[]) +-+static void cmd_quit ( int argc, char *argv[] ) +- { +-- if (argv[0] == cmplt) +-- print_cmds(); +-- else +-- shell_print_menu(); +-- +-- return bt_shell_noninteractive_quit(EXIT_SUCCESS); +-+ mainloop_quit(); +- } +- +--static const struct bt_shell_menu *find_menu(const char *name, size_t len, +-- int *index) +-+static void print_cmds ( void ) +- { +-- const struct queue_entry *entry; +-- int i; +-- +-- for (i = 0, entry = queue_get_entries(data.submenus); entry; +-- entry = entry->next, i++) { +-- struct bt_shell_menu *menu = entry->data; +-- +-- if (index) { +-- if (i < *index) +-- continue; +-- (*index)++; +-- } +-- +-- if (!strncmp(menu->name, name, len)) +-- return menu; +-- } +-- +-- return NULL; +-+ const struct bt_shell_menu_entry *entry; +-+ const struct queue_entry *submenu; +-+ +-+ if ( !data.menu ) +-+ return; +-+ +-+ printf ( "Commands:\n" ); +-+ +-+ for ( entry = data.menu->entries; entry->cmd; entry++ ) +-+ { +-+ printf ( "\t%s%s\t%s\n", entry->cmd, +-+ strlen ( entry->cmd ) < 8 ? "\t" : "", entry->desc ); +-+ } +-+ +-+ for ( submenu = queue_get_entries ( data.submenus ); submenu; +-+ submenu = submenu->next ) +-+ { +-+ struct bt_shell_menu *menu = submenu->data; +-+ +-+ printf ( "\n\t%s.:\n", menu->name ); +-+ +-+ for ( entry = menu->entries; entry->cmd; entry++ ) +-+ { +-+ printf ( "\t\t%s%s\t%s\n", entry->cmd, +-+ strlen ( entry->cmd ) < 8 ? "\t" : "", +-+ entry->desc ); +-+ } +-+ } +- } +- +--static char *menu_generator(const char *text, int state) +-+static void cmd_help ( int argc, char *argv[] ) +- { +-- static unsigned int index, len; +-- static struct queue_entry *entry; +-- +-- if (!state) { +-- index = 0; +-- len = strlen(text); +-- entry = (void *) queue_get_entries(data.submenus); +-- } +-- +-- for (; entry; entry = entry->next) { +-- struct bt_shell_menu *menu = entry->data; +-+ if ( argv[0] == cmplt ) +-+ print_cmds(); +-+ else +-+ shell_print_menu(); +- +-- index++; +-+ return bt_shell_noninteractive_quit ( EXIT_SUCCESS ); +-+} +- +-- if (!strncmp(menu->name, text, len)) { +-- entry = entry->next; +-- return strdup(menu->name); +-- } +-- } +-+static const struct bt_shell_menu *find_menu ( const char *name, size_t len, +-+ int *index ) +-+{ +-+ const struct queue_entry *entry; +-+ int i; +-+ +-+ for ( i = 0, entry = queue_get_entries ( data.submenus ); entry; +-+ entry = entry->next, i++ ) +-+ { +-+ struct bt_shell_menu *menu = entry->data; +-+ +-+ if ( index ) +-+ { +-+ if ( i < *index ) +-+ continue; +-+ ( *index )++; +-+ } +-+ +-+ if ( !strncmp ( menu->name, name, len ) ) +-+ return menu; +-+ } +-+ +-+ return NULL; +-+} +- +-- return NULL; +-+static char *menu_generator ( const char *text, int state ) +-+{ +-+ static unsigned int index, len; +-+ static struct queue_entry *entry; +-+ +-+ if ( !state ) +-+ { +-+ index = 0; +-+ len = strlen ( text ); +-+ entry = ( void * ) queue_get_entries ( data.submenus ); +-+ } +-+ +-+ for ( ; entry; entry = entry->next ) +-+ { +-+ struct bt_shell_menu *menu = entry->data; +-+ +-+ index++; +-+ +-+ if ( !strncmp ( menu->name, text, len ) ) +-+ { +-+ entry = entry->next; +-+ return strdup ( menu->name ); +-+ } +-+ } +-+ +-+ return NULL; +- } +- +--static void cmd_menu(int argc, char *argv[]) +-+static void cmd_menu ( int argc, char *argv[] ) +- { +-- const struct bt_shell_menu *menu; +-+ const struct bt_shell_menu *menu; +- +-- if (argc < 2 || !strlen(argv[1])) { +-- bt_shell_printf("Missing name argument\n"); +-- return bt_shell_noninteractive_quit(EXIT_FAILURE); +-- } +-+ if ( argc < 2 || !strlen ( argv[1] ) ) +-+ { +-+ bt_shell_printf ( "Missing name argument\n" ); +-+ return bt_shell_noninteractive_quit ( EXIT_FAILURE ); +-+ } +- +-- menu = find_menu(argv[1], strlen(argv[1]), NULL); +-- if (!menu) { +-- bt_shell_printf("Unable find menu with name: %s\n", argv[1]); +-- return bt_shell_noninteractive_quit(EXIT_FAILURE); +-- } +-+ menu = find_menu ( argv[1], strlen ( argv[1] ), NULL ); +-+ if ( !menu ) +-+ { +-+ bt_shell_printf ( "Unable find menu with name: %s\n", argv[1] ); +-+ return bt_shell_noninteractive_quit ( EXIT_FAILURE ); +-+ } +- +-- bt_shell_set_menu(menu); +-+ bt_shell_set_menu ( menu ); +- +-- shell_print_menu(); +-+ shell_print_menu(); +- +-- return bt_shell_noninteractive_quit(EXIT_SUCCESS); +-+ return bt_shell_noninteractive_quit ( EXIT_SUCCESS ); +- } +- +--static bool cmd_menu_exists(const struct bt_shell_menu *menu) +-+static bool cmd_menu_exists ( const struct bt_shell_menu *menu ) +- { +-- /* Skip menu command if not on main menu or if there are no +-- * submenus. +-- */ +-- if (menu != data.main || queue_isempty(data.submenus)) +-- return false; +-+ /* Skip menu command if not on main menu or if there are no +-+ * submenus. +-+ */ +-+ if ( menu != data.main || queue_isempty ( data.submenus ) ) +-+ return false; +- +-- return true; +-+ return true; +- } +- +--static void cmd_back(int argc, char *argv[]) +-+static void cmd_back ( int argc, char *argv[] ) +- { +-- if (data.menu == data.main) { +-- bt_shell_printf("Already on main menu\n"); +-- return; +-- } +-+ if ( data.menu == data.main ) +-+ { +-+ bt_shell_printf ( "Already on main menu\n" ); +-+ return; +-+ } +- +-- bt_shell_set_menu(data.main); +-+ bt_shell_set_menu ( data.main ); +- +-- shell_print_menu(); +-+ shell_print_menu(); +- } +- +--static bool cmd_back_exists(const struct bt_shell_menu *menu) +-+static bool cmd_back_exists ( const struct bt_shell_menu *menu ) +- { +-- /* Skip back command if on main menu */ +-- if (menu == data.main) +-- return false; +-+ /* Skip back command if on main menu */ +-+ if ( menu == data.main ) +-+ return false; +- +-- return true; +-+ return true; +- } +- +--static void cmd_export(int argc, char *argv[]) +-+static void cmd_export ( int argc, char *argv[] ) +- { +-- const struct queue_entry *entry; +-+ const struct queue_entry *entry; +- +-- for (entry = queue_get_entries(data.envs); entry; entry = entry->next) { +-- struct bt_shell_env *env = entry->data; +-+ for ( entry = queue_get_entries ( data.envs ); entry; entry = entry->next ) +-+ { +-+ struct bt_shell_env *env = entry->data; +- +-- print_text(COLOR_HIGHLIGHT, "%s=%p", env->name, env->value); +-- } +-+ print_text ( COLOR_HIGHLIGHT, "%s=%p", env->name, env->value ); +-+ } +- } +- +--static const struct bt_shell_menu_entry default_menu[] = { +-- { "back", NULL, cmd_back, "Return to main menu", NULL, +-- NULL, cmd_back_exists }, +-- { "menu", "", cmd_menu, "Select submenu", +-- menu_generator, NULL, +-- cmd_menu_exists}, +-- { "version", NULL, cmd_version, "Display version" }, +-- { "quit", NULL, cmd_quit, "Quit program" }, +-- { "exit", NULL, cmd_quit, "Quit program" }, +-- { "help", NULL, cmd_help, +-- "Display help about this program" }, +-- { "export", NULL, cmd_export, +-- "Print environment variables" }, +-- { } +-+static const struct bt_shell_menu_entry default_menu[] = +-+{ +-+ { +-+ "back", NULL, cmd_back, "Return to main menu", NULL, +-+ NULL, cmd_back_exists +-+ }, +-+ { +-+ "menu", "", cmd_menu, "Select submenu", +-+ menu_generator, NULL, +-+ cmd_menu_exists +-+ }, +-+ { "version", NULL, cmd_version, "Display version" }, +-+ { "quit", NULL, cmd_quit, "Quit program" }, +-+ { "exit", NULL, cmd_quit, "Quit program" }, +-+ { +-+ "help", NULL, cmd_help, +-+ "Display help about this program" +-+ }, +-+ { +-+ "export", NULL, cmd_export, +-+ "Print environment variables" +-+ }, +-+ { } +- }; +- +--static void shell_print_help(void) +-+static void shell_print_help ( void ) +- { +-- print_text(COLOR_HIGHLIGHT, +-- "\n" +-- "Use \"help\" for a list of available commands in a menu.\n" +-- "Use \"menu \" if you want to enter any submenu.\n" +-- "Use \"back\" if you want to return to menu main."); +-+ print_text ( COLOR_HIGHLIGHT, +-+ "\n" +-+ "Use \"help\" for a list of available commands in a menu.\n" +-+ "Use \"menu \" if you want to enter any submenu.\n" +-+ "Use \"back\" if you want to return to menu main." ); +- } +- +--static void shell_print_menu(void) +-+static void shell_print_menu ( void ) +- { +-- const struct bt_shell_menu_entry *entry; +-- const struct queue_entry *submenu; +-- +-- if (!data.menu) +-- return; +-- +-- if (data.zsh) { +-- shell_print_menu_zsh_complete(); +-- return; +-- } +-- +-- print_text(COLOR_HIGHLIGHT, "Menu %s:", data.menu->name); +-- print_text(COLOR_HIGHLIGHT, "Available commands:"); +-- print_text(COLOR_HIGHLIGHT, "-------------------"); +-- +-- if (data.menu == data.main) { +-- for (submenu = queue_get_entries(data.submenus); submenu; +-- submenu = submenu->next) { +-- struct bt_shell_menu *menu = submenu->data; +-- +-- print_submenu(menu->name, menu->desc ? menu->desc : +-- "Submenu"); +-- } +-- } +-- +-- for (entry = data.menu->entries; entry->cmd; entry++) { +-- print_menu(entry->cmd, entry->arg ? : "", entry->desc ? : ""); +-- } +-- +-- for (entry = default_menu; entry->cmd; entry++) { +-- if (entry->exists && !entry->exists(data.menu)) +-- continue; +-- +-- print_menu(entry->cmd, entry->arg ? : "", entry->desc ? : ""); +-- } +-+ const struct bt_shell_menu_entry *entry; +-+ const struct queue_entry *submenu; +-+ +-+ if ( !data.menu ) +-+ return; +-+ +-+ if ( data.zsh ) +-+ { +-+ shell_print_menu_zsh_complete(); +-+ return; +-+ } +-+ +-+ print_text ( COLOR_HIGHLIGHT, "Menu %s:", data.menu->name ); +-+ print_text ( COLOR_HIGHLIGHT, "Available commands:" ); +-+ print_text ( COLOR_HIGHLIGHT, "-------------------" ); +-+ +-+ if ( data.menu == data.main ) +-+ { +-+ for ( submenu = queue_get_entries ( data.submenus ); submenu; +-+ submenu = submenu->next ) +-+ { +-+ struct bt_shell_menu *menu = submenu->data; +-+ +-+ print_submenu ( menu->name, menu->desc ? menu->desc : +-+ "Submenu" ); +-+ } +-+ } +-+ +-+ for ( entry = data.menu->entries; entry->cmd; entry++ ) +-+ { +-+ print_menu ( entry->cmd, entry->arg ? : "", entry->desc ? : "" ); +-+ } +-+ +-+ for ( entry = default_menu; entry->cmd; entry++ ) +-+ { +-+ if ( entry->exists && !entry->exists ( data.menu ) ) +-+ continue; +-+ +-+ print_menu ( entry->cmd, entry->arg ? : "", entry->desc ? : "" ); +-+ } +- } +- +--static void shell_print_menu_zsh_complete(void) +-+static void shell_print_menu_zsh_complete ( void ) +- { +-- const struct bt_shell_menu_entry *entry; +-+ const struct bt_shell_menu_entry *entry; +- +-- for (entry = data.menu->entries; entry->cmd; entry++) +-- printf("%s:%s\n", entry->cmd, entry->desc ? : ""); +-+ for ( entry = data.menu->entries; entry->cmd; entry++ ) +-+ printf ( "%s:%s\n", entry->cmd, entry->desc ? : "" ); +- +-- for (entry = default_menu; entry->cmd; entry++) { +-- if (entry->exists && !entry->exists(data.menu)) +-- continue; +-+ for ( entry = default_menu; entry->cmd; entry++ ) +-+ { +-+ if ( entry->exists && !entry->exists ( data.menu ) ) +-+ continue; +- +-- printf("%s:%s\n", entry->cmd, entry->desc ? : ""); +-- } +-+ printf ( "%s:%s\n", entry->cmd, entry->desc ? : "" ); +-+ } +- } +- +--static int parse_args(char *arg, wordexp_t *w, char *del, int flags) +-+static int parse_args ( char *arg, wordexp_t *w, char *del, int flags ) +- { +-- char *str; +-+ char *str; +- +-- str = strdelimit(arg, del, '"'); +-+ str = strdelimit ( arg, del, '"' ); +- +-- if (wordexp(str, w, flags)) { +-- free(str); +-- return -EINVAL; +-- } +-+ if ( wordexp ( str, w, flags ) ) +-+ { +-+ free ( str ); +-+ return -EINVAL; +-+ } +- +-- /* If argument ends with ... set we_offs bypass strict checks */ +-- if (w->we_wordc && !strsuffix(w->we_wordv[w->we_wordc -1], "...")) +-- w->we_offs = 1; +-+ /* If argument ends with ... set we_offs bypass strict checks */ +-+ if ( w->we_wordc && !strsuffix ( w->we_wordv[w->we_wordc - 1], "..." ) ) +-+ w->we_offs = 1; +- +-- free(str); +-+ free ( str ); +- +-- return 0; +-+ return 0; +- } +- +--static int cmd_exec(const struct bt_shell_menu_entry *entry, +-- int argc, char *argv[]) +--{ +-- wordexp_t w; +-- size_t len; +-- char *man, *opt; +-- int flags = WRDE_NOCMD; +-- bool optargs = false; +-- +-- if (argc == 2 && (!memcmp(argv[1], "help", 4) || +-- !memcmp(argv[1], "--help", 6))) { +-- printf("%s\n", entry->desc); +-- printf(COLOR_HIGHLIGHT "Usage:" COLOR_OFF "\n"); +-- printf("\t %s %-*s\n", entry->cmd, +-- (int)(CMD_LENGTH - strlen(entry->cmd)), +-- !entry->arg ? "" : entry->arg); +-- return 0; +-- } +-- +-- if (!entry->arg || entry->arg[0] == '\0') { +-- if (argc > 1) { +-- print_text(COLOR_HIGHLIGHT, "Too many arguments"); +-- return -EINVAL; +-- } +-- goto exec; +-- } +-- +-- /* Find last mandatory arguments */ +-- man = strrchr(entry->arg, '>'); +-- if (!man) { +-- opt = strdup(entry->arg); +-- goto optional; +-- } +-- +-- len = man - entry->arg; +-- if (entry->arg[0] == '<') +-- man = strndup(entry->arg, len + 1); +-- else { +-- /* Find where mandatory arguments start */ +-- opt = strrchr(entry->arg, '<'); +-- /* Skip if mandatory arguments are not in the right format */ +-- if (!opt || opt > man) { +-- opt = strdup(entry->arg); +-- goto optional; +-- } +-- man = strndup(opt, man - opt + 1); +-- optargs = true; +-- } +-- +-- if (parse_args(man, &w, "<>", flags) < 0) { +-- print_text(COLOR_HIGHLIGHT, +-- "Unable to parse mandatory command arguments: %s", man ); +-- free(man); +-- return -EINVAL; +-- } +-- +-- free(man); +-- +-- /* Check if there are enough arguments */ +-- if ((unsigned) argc - 1 < w.we_wordc) { +-- print_text(COLOR_HIGHLIGHT, "Missing %s argument", +-- w.we_wordv[argc - 1]); +-- goto fail; +-- } +-- +-- flags |= WRDE_APPEND; +-- opt = strdup(entry->arg + len + 1); +-+static int cmd_exec ( const struct bt_shell_menu_entry *entry, +-+ int argc, char *argv[] ) +-+{ +-+ wordexp_t w; +-+ size_t len; +-+ char *man, *opt; +-+ int flags = WRDE_NOCMD; +-+ bool optargs = false; +-+ +-+ if ( argc == 2 && ( !memcmp ( argv[1], "help", 4 ) || +-+ !memcmp ( argv[1], "--help", 6 ) ) ) +-+ { +-+ printf ( "%s\n", entry->desc ); +-+ printf ( COLOR_HIGHLIGHT "Usage:" COLOR_OFF "\n" ); +-+ printf ( "\t %s %-*s\n", entry->cmd, +-+ ( int ) ( CMD_LENGTH - strlen ( entry->cmd ) ), +-+ !entry->arg ? "" : entry->arg ); +-+ return 0; +-+ } +-+ +-+ if ( !entry->arg || entry->arg[0] == '\0' ) +-+ { +-+ if ( argc > 1 ) +-+ { +-+ print_text ( COLOR_HIGHLIGHT, "Too many arguments" ); +-+ return -EINVAL; +-+ } +-+ goto exec; +-+ } +-+ +-+ /* Find last mandatory arguments */ +-+ man = strrchr ( entry->arg, '>' ); +-+ if ( !man ) +-+ { +-+ opt = strdup ( entry->arg ); +-+ goto optional; +-+ } +-+ +-+ len = man - entry->arg; +-+ if ( entry->arg[0] == '<' ) +-+ man = strndup ( entry->arg, len + 1 ); +-+ else +-+ { +-+ /* Find where mandatory arguments start */ +-+ opt = strrchr ( entry->arg, '<' ); +-+ /* Skip if mandatory arguments are not in the right format */ +-+ if ( !opt || opt > man ) +-+ { +-+ opt = strdup ( entry->arg ); +-+ goto optional; +-+ } +-+ man = strndup ( opt, man - opt + 1 ); +-+ optargs = true; +-+ } +-+ +-+ if ( parse_args ( man, &w, "<>", flags ) < 0 ) +-+ { +-+ print_text ( COLOR_HIGHLIGHT, +-+ "Unable to parse mandatory command arguments: %s", man ); +-+ free ( man ); +-+ return -EINVAL; +-+ } +-+ +-+ free ( man ); +-+ +-+ /* Check if there are enough arguments */ +-+ if ( ( unsigned ) argc - 1 < w.we_wordc ) +-+ { +-+ print_text ( COLOR_HIGHLIGHT, "Missing %s argument", +-+ w.we_wordv[argc - 1] ); +-+ goto fail; +-+ } +-+ +-+ flags |= WRDE_APPEND; +-+ opt = strdup ( entry->arg + len + 1 ); +- +- optional: +-- if (parse_args(opt, &w, "[]", flags) < 0) { +-- print_text(COLOR_HIGHLIGHT, +-- "Unable to parse optional command arguments: %s", opt); +-- free(opt); +-- return -EINVAL; +-- } +-- +-- free(opt); +-- +-- /* Check if there are too many arguments */ +-- if (!optargs && ((unsigned int) argc - 1 > w.we_wordc && !w.we_offs)) { +-- print_text(COLOR_HIGHLIGHT, "Too many arguments: %d > %zu", +-- argc - 1, w.we_wordc); +-- goto fail; +-- } +-- +-- w.we_offs = 0; +-- wordfree(&w); +-+ if ( parse_args ( opt, &w, "[]", flags ) < 0 ) +-+ { +-+ print_text ( COLOR_HIGHLIGHT, +-+ "Unable to parse optional command arguments: %s", opt ); +-+ free ( opt ); +-+ return -EINVAL; +-+ } +-+ +-+ free ( opt ); +-+ +-+ /* Check if there are too many arguments */ +-+ if ( !optargs && ( ( unsigned int ) argc - 1 > w.we_wordc && !w.we_offs ) ) +-+ { +-+ print_text ( COLOR_HIGHLIGHT, "Too many arguments: %d > %zu", +-+ argc - 1, w.we_wordc ); +-+ goto fail; +-+ } +-+ +-+ w.we_offs = 0; +-+ wordfree ( &w ); +- +- exec: +-- data.exec = entry; +-+ data.exec = entry; +- +-- if (entry->func) +-- entry->func(argc, argv); +-+ if ( entry->func ) +-+ entry->func ( argc, argv ); +- +-- data.exec = NULL; +-+ data.exec = NULL; +- +-- return 0; +-+ return 0; +- +- fail: +-- w.we_offs = 0; +-- wordfree(&w); +-- return -EINVAL; +-+ w.we_offs = 0; +-+ wordfree ( &w ); +-+ return -EINVAL; +- } +- +--static int menu_exec(const struct bt_shell_menu_entry *entry, +-- int argc, char *argv[]) +-+static int menu_exec ( const struct bt_shell_menu_entry *entry, +-+ int argc, char *argv[] ) +- { +-- for (; entry->cmd; entry++) { +-- if (strcmp(argv[0], entry->cmd)) +-- continue; +-+ for ( ; entry->cmd; entry++ ) +-+ { +-+ if ( strcmp ( argv[0], entry->cmd ) ) +-+ continue; +- +-- /* Skip menu command if not on main menu */ +-- if (data.menu != data.main && !strcmp(entry->cmd, "menu")) +-- continue; +-+ /* Skip menu command if not on main menu */ +-+ if ( data.menu != data.main && !strcmp ( entry->cmd, "menu" ) ) +-+ continue; +- +-- /* Skip back command if on main menu */ +-- if (data.menu == data.main && !strcmp(entry->cmd, "back")) +-- continue; +-+ /* Skip back command if on main menu */ +-+ if ( data.menu == data.main && !strcmp ( entry->cmd, "back" ) ) +-+ continue; +- +-- return cmd_exec(entry, argc, argv); +-- } +-+ return cmd_exec ( entry, argc, argv ); +-+ } +- +-- return -ENOENT; +-+ return -ENOENT; +- } +- +--static int submenu_exec(int argc, char *argv[]) +-+static int submenu_exec ( int argc, char *argv[] ) +- { +-- char *name; +-- int len, tlen; +-- const struct bt_shell_menu *submenu; +-+ char *name; +-+ int len, tlen; +-+ const struct bt_shell_menu *submenu; +- +-- if (data.menu != data.main) +-- return -ENOENT; +-+ if ( data.menu != data.main ) +-+ return -ENOENT; +- +-- name = strchr(argv[0], '.'); +-- if (!name) +-- return -ENOENT; +-+ name = strchr ( argv[0], '.' ); +-+ if ( !name ) +-+ return -ENOENT; +- +-- tlen = strlen(argv[0]); +-- len = name - argv[0]; +-- name[0] = '\0'; +-+ tlen = strlen ( argv[0] ); +-+ len = name - argv[0]; +-+ name[0] = '\0'; +- +-- submenu = find_menu(argv[0], strlen(argv[0]), NULL); +-- if (!submenu) +-- return -ENOENT; +-+ submenu = find_menu ( argv[0], strlen ( argv[0] ), NULL ); +-+ if ( !submenu ) +-+ return -ENOENT; +- +-- /* Replace submenu.command with command */ +-- memmove(argv[0], argv[0] + len + 1, tlen - len - 1); +-- memset(argv[0] + tlen - len - 1, 0, len + 1); +-+ /* Replace submenu.command with command */ +-+ memmove ( argv[0], argv[0] + len + 1, tlen - len - 1 ); +-+ memset ( argv[0] + tlen - len - 1, 0, len + 1 ); +- +-- return menu_exec(submenu->entries, argc, argv); +-+ return menu_exec ( submenu->entries, argc, argv ); +- } +- +--static int shell_exec(int argc, char *argv[]) +-+static int shell_exec ( int argc, char *argv[] ) +- { +-- int err; +-- +-- if (!data.menu || !argv[0]) +-- return -EINVAL; +-- +-- err = menu_exec(default_menu, argc, argv); +-- if (err == -ENOENT) { +-- err = menu_exec(data.menu->entries, argc, argv); +-- if (err == -ENOENT) { +-- err = submenu_exec(argc, argv); +-- if (err == -ENOENT) { +-- print_text(COLOR_HIGHLIGHT, +-- "Invalid command in menu %s: %s", +-- data.menu->name , argv[0]); +-- shell_print_help(); +-- } +-- } +-- } +-- +-- return err; +-+ int err; +-+ +-+ if ( !data.menu || !argv[0] ) +-+ return -EINVAL; +-+ +-+ err = menu_exec ( default_menu, argc, argv ); +-+ if ( err == -ENOENT ) +-+ { +-+ err = menu_exec ( data.menu->entries, argc, argv ); +-+ if ( err == -ENOENT ) +-+ { +-+ err = submenu_exec ( argc, argv ); +-+ if ( err == -ENOENT ) +-+ { +-+ print_text ( COLOR_HIGHLIGHT, +-+ "Invalid command in menu %s: %s", +-+ data.menu->name, argv[0] ); +-+ shell_print_help(); +-+ } +-+ } +-+ } +-+ +-+ return err; +- } +- +--void bt_shell_printf(const char *fmt, ...) +--{ +-- va_list args; +-- bool save_input; +-- char *saved_line; +-- int saved_point; +-- +-- if (!data.input) +-- return; +-- +-- if (data.mode) { +-- va_start(args, fmt); +-- vprintf(fmt, args); +-- va_end(args); +-- return; +-- } +-- +-- save_input = !RL_ISSTATE(RL_STATE_DONE); +-- +-- if (save_input) { +-- saved_point = rl_point; +-- saved_line = rl_copy_text(0, rl_end); +-- if (!data.saved_prompt) +-- rl_save_prompt(); +-- rl_replace_line("", 0); +-- rl_redisplay(); +-- } +-- +-- va_start(args, fmt); +-- vprintf(fmt, args); +-- va_end(args); +-- +-- if (data.monitor) { +-- va_start(args, fmt); +-- bt_log_vprintf(0xffff, data.name, LOG_INFO, fmt, args); +-- va_end(args); +-- } +-- +-- if (save_input) { +-- if (!data.saved_prompt) +-- rl_restore_prompt(); +-- rl_replace_line(saved_line, 0); +-- rl_point = saved_point; +-- rl_forced_update_display(); +-- free(saved_line); +-- } +-+void bt_shell_printf ( const char *fmt, ... ) +-+{ +-+ va_list args; +-+ bool save_input; +-+ char *saved_line; +-+ int saved_point; +-+ +-+ if ( !data.input ) +-+ return; +-+ +-+ if ( data.mode ) +-+ { +-+ va_start ( args, fmt ); +-+ vprintf ( fmt, args ); +-+ va_end ( args ); +-+ return; +-+ } +-+ +-+ save_input = !RL_ISSTATE ( RL_STATE_DONE ); +-+ +-+ if ( save_input ) +-+ { +-+ saved_point = rl_point; +-+ saved_line = rl_copy_text ( 0, rl_end ); +-+ if ( !data.saved_prompt ) +-+ rl_save_prompt(); +-+ rl_replace_line ( "", 0 ); +-+ rl_redisplay(); +-+ } +-+ +-+ va_start ( args, fmt ); +-+ vprintf ( fmt, args ); +-+ va_end ( args ); +-+ +-+ if ( data.monitor ) +-+ { +-+ va_start ( args, fmt ); +-+ bt_log_vprintf ( 0xffff, data.name, LOG_INFO, fmt, args ); +-+ va_end ( args ); +-+ } +-+ +-+ if ( save_input ) +-+ { +-+ if ( !data.saved_prompt ) +-+ rl_restore_prompt(); +-+ rl_replace_line ( saved_line, 0 ); +-+ rl_point = saved_point; +-+ rl_forced_update_display(); +-+ free ( saved_line ); +-+ } +- } +- +--static void print_string(const char *str, void *user_data) +-+static void print_string ( const char *str, void *user_data ) +- { +-- bt_shell_printf("%s\n", str); +-+ bt_shell_printf ( "%s\n", str ); +- } +- +--void bt_shell_hexdump(const unsigned char *buf, size_t len) +-+void bt_shell_hexdump ( const unsigned char *buf, size_t len ) +- { +-- util_hexdump(' ', buf, len, print_string, NULL); +-+ util_hexdump ( ' ', buf, len, print_string, NULL ); +- } +- +- void bt_shell_usage() +- { +-- if (!data.exec) +-- return; +-+ if ( !data.exec ) +-+ return; +- +-- bt_shell_printf("Usage: %s %s\n", data.exec->cmd, +-- data.exec->arg ? data.exec->arg : ""); +-+ bt_shell_printf ( "Usage: %s %s\n", data.exec->cmd, +-+ data.exec->arg ? data.exec->arg : "" ); +- } +- +--static void prompt_input(const char *str, bt_shell_prompt_input_func func, +-- void *user_data) +-+static void prompt_input ( const char *str, bt_shell_prompt_input_func func, +-+ void *user_data ) +- { +-- data.saved_prompt = true; +-- data.saved_func = func; +-- data.saved_user_data = user_data; +-+ data.saved_prompt = true; +-+ data.saved_func = func; +-+ data.saved_user_data = user_data; +- +-- rl_save_prompt(); +-- bt_shell_set_prompt(str); +-+ rl_save_prompt(); +-+ bt_shell_set_prompt ( str ); +- } +- +--void bt_shell_prompt_input(const char *label, const char *msg, +-- bt_shell_prompt_input_func func, void *user_data) +-+void bt_shell_prompt_input ( const char *label, const char *msg, +-+ bt_shell_prompt_input_func func, void *user_data ) +- { +-- char *str; +-+ char *str; +- +-- if (!data.init || data.mode) +-- return; +-+ if ( !data.init || data.mode ) +-+ return; +- +-- if (data.saved_prompt) { +-- struct bt_shell_prompt_input *prompt; +-+ if ( data.saved_prompt ) +-+ { +-+ struct bt_shell_prompt_input *prompt; +- +-- prompt = new0(struct bt_shell_prompt_input, 1); +-+ prompt = new0 ( struct bt_shell_prompt_input, 1 ); +- +-- if (asprintf(&str, COLOR_HIGHLIGHT "[%s] %s " COLOR_OFF, label, +-- msg) < 0) { +-- free(prompt); +-- return; +-- } +-+ if ( asprintf ( &str, COLOR_HIGHLIGHT "[%s] %s " COLOR_OFF, label, +-+ msg ) < 0 ) +-+ { +-+ free ( prompt ); +-+ return; +-+ } +- +-- prompt->func = func; +-- prompt->user_data = user_data; +-+ prompt->func = func; +-+ prompt->user_data = user_data; +- +-- queue_push_tail(data.prompts, prompt); +-- free(str); +-+ queue_push_tail ( data.prompts, prompt ); +-+ free ( str ); +- +-- return; +-- } +-+ return; +-+ } +- +-- if (asprintf(&str, COLOR_HIGHLIGHT "[%s] %s " COLOR_OFF, label, +-- msg) < 0) +-- return; +-+ if ( asprintf ( &str, COLOR_HIGHLIGHT "[%s] %s " COLOR_OFF, label, +-+ msg ) < 0 ) +-+ return; +- +-- prompt_input(str, func, user_data); +-+ prompt_input ( str, func, user_data ); +- +-- free(str); +-+ free ( str ); +- } +- +--static void prompt_free(void *data) +-+static void prompt_free ( void *data ) +- { +-- struct bt_shell_prompt_input *prompt = data; +-+ struct bt_shell_prompt_input *prompt = data; +- +-- free(prompt->str); +-- free(prompt); +-+ free ( prompt->str ); +-+ free ( prompt ); +- } +- +--int bt_shell_release_prompt(const char *input) +-+int bt_shell_release_prompt ( const char *input ) +- { +-- struct bt_shell_prompt_input *prompt; +-- bt_shell_prompt_input_func func; +-- void *user_data; +-+ struct bt_shell_prompt_input *prompt; +-+ bt_shell_prompt_input_func func; +-+ void *user_data; +- +-- if (!data.saved_prompt) +-- return -1; +-+ if ( !data.saved_prompt ) +-+ return -1; +- +-- data.saved_prompt = false; +-+ data.saved_prompt = false; +- +-- rl_restore_prompt(); +-+ rl_restore_prompt(); +- +-- func = data.saved_func; +-- user_data = data.saved_user_data; +-+ func = data.saved_func; +-+ user_data = data.saved_user_data; +- +-- prompt = queue_pop_head(data.prompts); +-- if (prompt) +-- data.saved_prompt = true; +-+ prompt = queue_pop_head ( data.prompts ); +-+ if ( prompt ) +-+ data.saved_prompt = true; +- +-- data.saved_func = NULL; +-- data.saved_user_data = NULL; +-+ data.saved_func = NULL; +-+ data.saved_user_data = NULL; +- +-- func(input, user_data); +-+ func ( input, user_data ); +- +-- if (prompt) { +-- prompt_input(prompt->str, prompt->func, prompt->user_data); +-- prompt_free(prompt); +-- } +-+ if ( prompt ) +-+ { +-+ prompt_input ( prompt->str, prompt->func, prompt->user_data ); +-+ prompt_free ( prompt ); +-+ } +- +-- return 0; +-+ return 0; +- } +- +--static void rl_handler(char *input) +-+static void rl_handler ( char *input ) +- { +-- HIST_ENTRY *last; +-+ HIST_ENTRY *last; +- +-- if (!input) { +-- rl_insert_text("quit"); +-- rl_redisplay(); +-- rl_crlf(); +-- mainloop_quit(); +-- return; +-- } +-+ if ( !input ) +-+ { +-+ rl_insert_text ( "quit" ); +-+ rl_redisplay(); +-+ rl_crlf(); +-+ mainloop_quit(); +-+ return; +-+ } +- +-- if (!strlen(input)) +-- goto done; +-+ if ( !strlen ( input ) ) +-+ goto done; +- +-- if (!bt_shell_release_prompt(input)) +-- goto done; +-+ if ( !bt_shell_release_prompt ( input ) ) +-+ goto done; +- +-- last = history_get(history_length + history_base - 1); +-- /* append only if input is different from previous command */ +-- if (!last || strcmp(input, last->line)) +-- add_history(input); +-+ last = history_get ( history_length + history_base - 1 ); +-+ /* append only if input is different from previous command */ +-+ if ( !last || strcmp ( input, last->line ) ) +-+ add_history ( input ); +- +-- if (data.monitor) +-- bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input); +-+ if ( data.monitor ) +-+ bt_log_printf ( 0xffff, data.name, LOG_INFO, "%s", input ); +- +-- bt_shell_exec(input); +-+ bt_shell_exec ( input ); +- +- done: +-- free(input); +-+ free ( input ); +- } +- +--static char *find_cmd(const char *text, +-- const struct bt_shell_menu_entry *entry, int *index) +-+static char *find_cmd ( const char *text, +-+ const struct bt_shell_menu_entry *entry, int *index ) +- { +-- const struct bt_shell_menu_entry *tmp; +-- int len; +-+ const struct bt_shell_menu_entry *tmp; +-+ int len; +- +-- len = strlen(text); +-+ len = strlen ( text ); +- +-- while ((tmp = &entry[*index])) { +-- (*index)++; +-+ while ( ( tmp = &entry[*index] ) ) +-+ { +-+ ( *index )++; +- +-- if (!tmp->cmd) +-- break; +-+ if ( !tmp->cmd ) +-+ break; +- +-- if (tmp->exists && !tmp->exists(data.menu)) +-- continue; +-+ if ( tmp->exists && !tmp->exists ( data.menu ) ) +-+ continue; +- +-- if (!strncmp(tmp->cmd, text, len)) +-- return strdup(tmp->cmd); +-- } +-+ if ( !strncmp ( tmp->cmd, text, len ) ) +-+ return strdup ( tmp->cmd ); +-+ } +- +-- return NULL; +-+ return NULL; +- } +- +--static char *cmd_generator(const char *text, int state) +--{ +-- static int index; +-- static bool default_menu_enabled, menu_enabled, submenu_enabled; +-- static const struct bt_shell_menu *menu; +-- char *cmd; +-- +-- if (!state) { +-- index = 0; +-- menu = NULL; +-- default_menu_enabled = true; +-- submenu_enabled = false; +-- } +-- +-- if (default_menu_enabled) { +-- cmd = find_cmd(text, default_menu, &index); +-- if (cmd) { +-- return cmd; +-- } else { +-- index = 0; +-- menu = data.menu; +-- default_menu_enabled = false; +-- +-- if (data.main == data.menu) +-- menu_enabled = true; +-- } +-- } +-- +-- if (menu_enabled) { +-- menu = find_menu(text, strlen(text), &index); +-- if (menu) +-- return strdup(menu->name); +-- +-- index = 0; +-- menu = data.menu; +-- menu_enabled = false; +-- } +-- +-- if (!submenu_enabled) { +-- cmd = find_cmd(text, menu->entries, &index); +-- if (cmd || menu != data.main) +-- return cmd; +-- +-- cmd = strrchr(text, '.'); +-- if (!cmd) +-- return NULL; +-- +-- menu = find_menu(text, cmd - text, NULL); +-- if (!menu) +-- return NULL; +-- +-- index = 0; +-- submenu_enabled = true; +-- } +-- +-- cmd = find_cmd(text + strlen(menu->name) + 1, menu->entries, &index); +-- if (cmd) { +-- int err; +-- char *tmp; +-- +-- err = asprintf(&tmp, "%s.%s", menu->name, cmd); +-- +-- free(cmd); +-- +-- if (err < 0) +-- return NULL; +-- +-- cmd = tmp; +-- } +-- +-- return cmd; +-+static char *cmd_generator ( const char *text, int state ) +-+{ +-+ static int index; +-+ static bool default_menu_enabled, menu_enabled, submenu_enabled; +-+ static const struct bt_shell_menu *menu; +-+ char *cmd; +-+ +-+ if ( !state ) +-+ { +-+ index = 0; +-+ menu = NULL; +-+ default_menu_enabled = true; +-+ submenu_enabled = false; +-+ } +-+ +-+ if ( default_menu_enabled ) +-+ { +-+ cmd = find_cmd ( text, default_menu, &index ); +-+ if ( cmd ) +-+ { +-+ return cmd; +-+ } +-+ else +-+ { +-+ index = 0; +-+ menu = data.menu; +-+ default_menu_enabled = false; +-+ +-+ if ( data.main == data.menu ) +-+ menu_enabled = true; +-+ } +-+ } +-+ +-+ if ( menu_enabled ) +-+ { +-+ menu = find_menu ( text, strlen ( text ), &index ); +-+ if ( menu ) +-+ return strdup ( menu->name ); +-+ +-+ index = 0; +-+ menu = data.menu; +-+ menu_enabled = false; +-+ } +-+ +-+ if ( !submenu_enabled ) +-+ { +-+ cmd = find_cmd ( text, menu->entries, &index ); +-+ if ( cmd || menu != data.main ) +-+ return cmd; +-+ +-+ cmd = strrchr ( text, '.' ); +-+ if ( !cmd ) +-+ return NULL; +-+ +-+ menu = find_menu ( text, cmd - text, NULL ); +-+ if ( !menu ) +-+ return NULL; +-+ +-+ index = 0; +-+ submenu_enabled = true; +-+ } +-+ +-+ cmd = find_cmd ( text + strlen ( menu->name ) + 1, menu->entries, &index ); +-+ if ( cmd ) +-+ { +-+ int err; +-+ char *tmp; +-+ +-+ err = asprintf ( &tmp, "%s.%s", menu->name, cmd ); +-+ +-+ free ( cmd ); +-+ +-+ if ( err < 0 ) +-+ return NULL; +-+ +-+ cmd = tmp; +-+ } +-+ +-+ return cmd; +- } +- +- static wordexp_t args; +- +--static char *arg_generator(const char *text, int state) +-+static char *arg_generator ( const char *text, int state ) +- { +-- static unsigned int index, len; +-- const char *arg; +-+ static unsigned int index, len; +-+ const char *arg; +- +-- if (!state) { +-- index = 0; +-- len = strlen(text); +-- } +-+ if ( !state ) +-+ { +-+ index = 0; +-+ len = strlen ( text ); +-+ } +- +-- while (index < args.we_wordc) { +-- arg = args.we_wordv[index]; +-- index++; +-+ while ( index < args.we_wordc ) +-+ { +-+ arg = args.we_wordv[index]; +-+ index++; +- +-- if (!strncmp(arg, text, len)) +-- return strdup(arg); +-- } +-+ if ( !strncmp ( arg, text, len ) ) +-+ return strdup ( arg ); +-+ } +- +-- return NULL; +-+ return NULL; +- } +- +--static char **args_completion(const struct bt_shell_menu_entry *entry, int argc, +-- const char *text) +-+static char **args_completion ( const struct bt_shell_menu_entry *entry, int argc, +-+ const char *text ) +- { +-- char **matches = NULL; +-- char *str; +-- int index; +-+ char **matches = NULL; +-+ char *str; +-+ int index; +- +-- index = text[0] == '\0' ? argc - 1 : argc - 2; +-- if (index < 0) +-- return NULL; +-+ index = text[0] == '\0' ? argc - 1 : argc - 2; +-+ if ( index < 0 ) +-+ return NULL; +- +-- if (!entry->arg) +-- goto end; +-+ if ( !entry->arg ) +-+ goto end; +- +-- str = strdup(entry->arg); +-+ str = strdup ( entry->arg ); +- +-- if (parse_args(str, &args, "<>[]", WRDE_NOCMD)) +-- goto done; +-+ if ( parse_args ( str, &args, "<>[]", WRDE_NOCMD ) ) +-+ goto done; +- +-- /* Check if argument is valid */ +-- if ((unsigned) index > args.we_wordc - 1) +-- goto done; +-+ /* Check if argument is valid */ +-+ if ( ( unsigned ) index > args.we_wordc - 1 ) +-+ goto done; +- +-- /* Check if there are multiple values */ +-- if (!strrchr(entry->arg, '/')) +-- goto done; +-+ /* Check if there are multiple values */ +-+ if ( !strrchr ( entry->arg, '/' ) ) +-+ goto done; +- +-- free(str); +-+ free ( str ); +- +-- /* Split values separated by / */ +-- str = strdelimit(args.we_wordv[index], "/", ' '); +-+ /* Split values separated by / */ +-+ str = strdelimit ( args.we_wordv[index], "/", ' ' ); +- +-- args.we_offs = 0; +-- wordfree(&args); +-+ args.we_offs = 0; +-+ wordfree ( &args ); +- +-- if (wordexp(str, &args, WRDE_NOCMD)) +-- goto done; +-+ if ( wordexp ( str, &args, WRDE_NOCMD ) ) +-+ goto done; +- +-- rl_completion_display_matches_hook = NULL; +-- matches = rl_completion_matches(text, arg_generator); +-+ rl_completion_display_matches_hook = NULL; +-+ matches = rl_completion_matches ( text, arg_generator ); +- +- done: +-- free(str); +-+ free ( str ); +- end: +-- if (!matches && text[0] == '\0') +-- bt_shell_printf("Usage: %s %s\n", entry->cmd, +-- entry->arg ? entry->arg : ""); +-+ if ( !matches && text[0] == '\0' ) +-+ bt_shell_printf ( "Usage: %s %s\n", entry->cmd, +-+ entry->arg ? entry->arg : "" ); +- +-- args.we_offs = 0; +-- wordfree(&args); +-- return matches; +-+ args.we_offs = 0; +-+ wordfree ( &args ); +-+ return matches; +- } +- +--static char **menu_completion(const struct bt_shell_menu_entry *entry, +-- const char *text, int argc, char *input_cmd) +-+static char **menu_completion ( const struct bt_shell_menu_entry *entry, +-+ const char *text, int argc, char *input_cmd ) +- { +-- char **matches = NULL; +-+ char **matches = NULL; +- +-- for (; entry->cmd; entry++) { +-- if (strcmp(entry->cmd, input_cmd)) +-- continue; +-+ for ( ; entry->cmd; entry++ ) +-+ { +-+ if ( strcmp ( entry->cmd, input_cmd ) ) +-+ continue; +- +-- if (!entry->gen) { +-- matches = args_completion(entry, argc, text); +-- break; +-- } +-+ if ( !entry->gen ) +-+ { +-+ matches = args_completion ( entry, argc, text ); +-+ break; +-+ } +- +-- rl_completion_display_matches_hook = entry->disp; +-- matches = rl_completion_matches(text, entry->gen); +-- break; +-- } +-+ rl_completion_display_matches_hook = entry->disp; +-+ matches = rl_completion_matches ( text, entry->gen ); +-+ break; +-+ } +- +-- return matches; +-+ return matches; +- } +- +--static char **submenu_completion(const char *text, int argc, char *input_cmd) +-+static char **submenu_completion ( const char *text, int argc, char *input_cmd ) +- { +-- const struct bt_shell_menu *menu; +-- char *cmd; +-+ const struct bt_shell_menu *menu; +-+ char *cmd; +- +-- if (data.main != data.menu) +-- return NULL; +-+ if ( data.main != data.menu ) +-+ return NULL; +- +-- cmd = strrchr(input_cmd, '.'); +-- if (!cmd) +-- return NULL; +-+ cmd = strrchr ( input_cmd, '.' ); +-+ if ( !cmd ) +-+ return NULL; +- +-- menu = find_menu(input_cmd, cmd - input_cmd, NULL); +-- if (!menu) +-- return NULL; +-+ menu = find_menu ( input_cmd, cmd - input_cmd, NULL ); +-+ if ( !menu ) +-+ return NULL; +- +-- return menu_completion(menu->entries, text, argc, +-- input_cmd + strlen(menu->name) + 1); +-+ return menu_completion ( menu->entries, text, argc, +-+ input_cmd + strlen ( menu->name ) + 1 ); +- } +- +--static char **shell_completion(const char *text, int start, int end) +-+static char **shell_completion ( const char *text, int start, int end ) +- { +-- char **matches = NULL; +-- +-- rl_attempted_completion_over = 1; +-- +-- if (!data.menu) +-- return NULL; +-- +-- if (start > 0) { +-- wordexp_t w; +-- +-- if (wordexp(rl_line_buffer, &w, WRDE_NOCMD)) +-- return NULL; +-- +-- matches = menu_completion(default_menu, text, w.we_wordc, +-- w.we_wordv[0]); +-- if (!matches) { +-- matches = menu_completion(data.menu->entries, text, +-- w.we_wordc, +-- w.we_wordv[0]); +-- if (!matches) +-- matches = submenu_completion(text, w.we_wordc, +-- w.we_wordv[0]); +-- } +-- +-- wordfree(&w); +-- } else { +-- rl_completion_display_matches_hook = NULL; +-- matches = rl_completion_matches(text, cmd_generator); +-- } +-- +-- return matches; +-+ char **matches = NULL; +-+ +-+ rl_attempted_completion_over = 1; +-+ +-+ if ( !data.menu ) +-+ return NULL; +-+ +-+ if ( start > 0 ) +-+ { +-+ wordexp_t w; +-+ +-+ if ( wordexp ( rl_line_buffer, &w, WRDE_NOCMD ) ) +-+ return NULL; +-+ +-+ matches = menu_completion ( default_menu, text, w.we_wordc, +-+ w.we_wordv[0] ); +-+ if ( !matches ) +-+ { +-+ matches = menu_completion ( data.menu->entries, text, +-+ w.we_wordc, +-+ w.we_wordv[0] ); +-+ if ( !matches ) +-+ matches = submenu_completion ( text, w.we_wordc, +-+ w.we_wordv[0] ); +-+ } +-+ +-+ wordfree ( &w ); +-+ } +-+ else +-+ { +-+ rl_completion_display_matches_hook = NULL; +-+ matches = rl_completion_matches ( text, cmd_generator ); +-+ } +-+ +-+ return matches; +- } +- +--static bool io_hup(struct io *io, void *user_data) +-+static bool io_hup ( struct io *io, void *user_data ) +- { +-- mainloop_quit(); +-+ mainloop_quit(); +- +-- return false; +-+ return false; +- } +- +--static void signal_callback(int signum, void *user_data) +--{ +-- static bool terminated = false; +-- +-- switch (signum) { +-- case SIGINT: +-- if (data.input && !data.mode) { +-- rl_replace_line("", 0); +-- rl_crlf(); +-- rl_on_new_line(); +-- rl_redisplay(); +-- return; +-- } +-- +-- /* +-- * If input was not yet setup up that means signal was received +-- * while daemon was not yet running. Since user is not able +-- * to terminate client by CTRL-D or typing exit treat this as +-- * exit and fall through. +-- */ +-- +-- /* fall through */ +-- case SIGTERM: +-- if (!terminated) { +-- if (!data.mode) { +-- rl_replace_line("", 0); +-- rl_crlf(); +-- } +-- mainloop_quit(); +-- } +-- +-- terminated = true; +-- break; +-- } +-+static void signal_callback ( int signum, void *user_data ) +-+{ +-+ static bool terminated = false; +-+ +-+ switch ( signum ) +-+ { +-+ case SIGINT: +-+ if ( data.input && !data.mode ) +-+ { +-+ rl_replace_line ( "", 0 ); +-+ rl_crlf(); +-+ rl_on_new_line(); +-+ rl_redisplay(); +-+ return; +-+ } +-+ +-+ /* +-+ * If input was not yet setup up that means signal was received +-+ * while daemon was not yet running. Since user is not able +-+ * to terminate client by CTRL-D or typing exit treat this as +-+ * exit and fall through. +-+ */ +-+ +-+ /* fall through */ +-+ case SIGTERM: +-+ if ( !terminated ) +-+ { +-+ if ( !data.mode ) +-+ { +-+ rl_replace_line ( "", 0 ); +-+ rl_crlf(); +-+ } +-+ mainloop_quit(); +-+ } +-+ +-+ terminated = true; +-+ break; +-+ } +- } +- +--static void rl_init_history(void) +-+static void rl_init_history ( void ) +- { +-- const char *name; +-- char *dir; +-- +-- memset(data.history, 0, sizeof(data.history)); +-- +-- name = strrchr(data.name, '/'); +-- if (!name) +-- name = data.name; +-- else +-- name++; +-- +-- dir = getenv("XDG_CACHE_HOME"); +-- if (dir) { +-- snprintf(data.history, sizeof(data.history), "%s/.%s_history", +-- dir, name); +-- goto done; +-- } +-- +-- dir = getenv("HOME"); +-- if (dir) { +-- snprintf(data.history, sizeof(data.history), +-- "%s/.cache/.%s_history", dir, name); +-- goto done; +-- } +-- +-- dir = getenv("PWD"); +-- if (dir) { +-- snprintf(data.history, sizeof(data.history), "%s/.%s_history", +-- dir, name); +-- goto done; +-- } +-- +-- return; +-+ const char *name; +-+ char *dir; +-+ +-+ memset ( data.history, 0, sizeof ( data.history ) ); +-+ +-+ name = strrchr ( data.name, '/' ); +-+ if ( !name ) +-+ name = data.name; +-+ else +-+ name++; +-+ +-+ dir = getenv ( "XDG_CACHE_HOME" ); +-+ if ( dir ) +-+ { +-+ snprintf ( data.history, sizeof ( data.history ), "%s/.%s_history", +-+ dir, name ); +-+ goto done; +-+ } +-+ +-+ dir = getenv ( "HOME" ); +-+ if ( dir ) +-+ { +-+ snprintf ( data.history, sizeof ( data.history ), +-+ "%s/.cache/.%s_history", dir, name ); +-+ goto done; +-+ } +-+ +-+ dir = getenv ( "PWD" ); +-+ if ( dir ) +-+ { +-+ snprintf ( data.history, sizeof ( data.history ), "%s/.%s_history", +-+ dir, name ); +-+ goto done; +-+ } +-+ +-+ return; +- +- done: +-- read_history(data.history); +-- using_history(); +-- bt_shell_set_env("HISTORY", data.history); +-+ read_history ( data.history ); +-+ using_history(); +-+ bt_shell_set_env ( "HISTORY", data.history ); +- } +- +--static void rl_init(void) +-+static void rl_init ( void ) +- { +-- if (data.mode) +-- return; +-+ if ( data.mode ) +-+ return; +- +-- /* Allow conditional parsing of the ~/.inputrc file. */ +-- rl_readline_name = data.name; +-+ /* Allow conditional parsing of the ~/.inputrc file. */ +-+ rl_readline_name = data.name; +- +-- setlinebuf(stdout); +-- rl_attempted_completion_function = shell_completion; +-+ setlinebuf ( stdout ); +-+ rl_attempted_completion_function = shell_completion; +- +-- rl_erase_empty_line = 1; +-- rl_callback_handler_install(NULL, rl_handler); +-+ rl_erase_empty_line = 1; +-+ rl_callback_handler_install ( NULL, rl_handler ); +- +-- rl_init_history(); +-+ rl_init_history(); +- } +- +--static const struct option main_options[] = { +-- { "version", no_argument, 0, 'v' }, +-- { "help", no_argument, 0, 'h' }, +-- { "timeout", required_argument, 0, 't' }, +-- { "monitor", no_argument, 0, 'm' }, +-- { "zsh-complete", no_argument, 0, 'z' }, +-+static const struct option main_options[] = +-+{ +-+ { "version", no_argument, 0, 'v' }, +-+ { "help", no_argument, 0, 'h' }, +-+ { "timeout", required_argument, 0, 't' }, +-+ { "monitor", no_argument, 0, 'm' }, +-+ { "zsh-complete", no_argument, 0, 'z' }, +- }; +- +--static void usage(int argc, char **argv, const struct bt_shell_opt *opt) +-+static void usage ( int argc, char **argv, const struct bt_shell_opt *opt ) +- { +-- unsigned int i; +-+ unsigned int i; +- +-- printf("%s ver %s\n", data.name, VERSION); +-- printf("Usage:\n" +-- "\t%s [--options] [commands]\n", data.name); +-+ printf ( "%s ver %s\n", data.name, VERSION ); +-+ printf ( "Usage:\n" +-+ "\t%s [--options] [commands]\n", data.name ); +- +-- printf("Options:\n"); +-+ printf ( "Options:\n" ); +- +-- for (i = 0; opt && opt->options[i].name; i++) +-- printf("\t--%s \t%s\n", opt->options[i].name, opt->help[i]); +-+ for ( i = 0; opt && opt->options[i].name; i++ ) +-+ printf ( "\t--%s \t%s\n", opt->options[i].name, opt->help[i] ); +- +-- printf("\t--monitor \tEnable monitor output\n" +-- "\t--timeout \tTimeout in seconds for non-interactive mode\n" +-- "\t--version \tDisplay version\n" +-- "\t--help \t\tDisplay help\n"); +-+ printf ( "\t--monitor \tEnable monitor output\n" +-+ "\t--timeout \tTimeout in seconds for non-interactive mode\n" +-+ "\t--version \tDisplay version\n" +-+ "\t--help \t\tDisplay help\n" ); +- } +- +--void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt) +--{ +-- int c, index = -1; +-- struct option options[256]; +-- char optstr[256]; +-- size_t offset; +-- +-- offset = sizeof(main_options) / sizeof(struct option); +-- +-- memcpy(options, main_options, sizeof(struct option) * offset); +-- +-- if (opt) { +-- memcpy(options + offset, opt->options, +-- sizeof(struct option) * opt->optno); +-- snprintf(optstr, sizeof(optstr), "+mhvt:%s", opt->optstr); +-- } else +-- snprintf(optstr, sizeof(optstr), "+mhvt:"); +-- +-- data.name = strrchr(argv[0], '/'); +-- if (!data.name) +-- data.name = strdup(argv[0]); +-- else +-- data.name = strdup(++data.name); +-- +-- while ((c = getopt_long(argc, argv, optstr, options, &index)) != -1) { +-- switch (c) { +-- case 'v': +-- printf("%s: %s\n", data.name, VERSION); +-- exit(EXIT_SUCCESS); +-- return; +-- case 'h': +-- usage(argc, argv, opt); +-- data.argc = 1; +-- data.argv = &cmplt; +-- data.mode = 1; +-- goto done; +-- case 't': +-- data.timeout = atoi(optarg); +-- break; +-- case 'z': +-- data.zsh = 1; +-- break; +-- case 'm': +-- data.monitor = true; +-- if (bt_log_open() < 0) { +-- data.monitor = false; +-- printf("Unable to open logging channel\n"); +-- } +-- break; +-- default: +-- if (index < 0) { +-- for (index = 0; options[index].val; index++) { +-- if (c == options[index].val) +-- break; +-- } +-- } +-- +-- if (c != opt->options[index - offset].val) { +-- usage(argc, argv, opt); +-- exit(EXIT_SUCCESS); +-- return; +-- } +-- +-- *opt->optarg[index - offset] = optarg; +-- } +-- +-- index = -1; +-- } +-- +-- bt_shell_set_env("SHELL", data.name); +-- +-- data.argc = argc - optind; +-- data.argv = argv + optind; +-- optind = 0; +-- data.mode = (data.argc > 0); +-+void bt_shell_init ( int argc, char **argv, const struct bt_shell_opt *opt ) +-+{ +-+ int c, index = -1; +-+ struct option options[256]; +-+ char optstr[256]; +-+ size_t offset; +-+ +-+ offset = sizeof ( main_options ) / sizeof ( struct option ); +-+ +-+ memcpy ( options, main_options, sizeof ( struct option ) * offset ); +-+ +-+ if ( opt ) +-+ { +-+ memcpy ( options + offset, opt->options, +-+ sizeof ( struct option ) * opt->optno ); +-+ snprintf ( optstr, sizeof ( optstr ), "+mhvt:%s", opt->optstr ); +-+ } +-+ else +-+ snprintf ( optstr, sizeof ( optstr ), "+mhvt:" ); +-+ +-+ data.name = strrchr ( argv[0], '/' ); +-+ if ( !data.name ) +-+ data.name = strdup ( argv[0] ); +-+ else +-+ data.name = strdup ( ++data.name ); +-+ +-+ while ( ( c = getopt_long ( argc, argv, optstr, options, &index ) ) != -1 ) +-+ { +-+ switch ( c ) +-+ { +-+ case 'v': +-+ printf ( "%s: %s\n", data.name, VERSION ); +-+ exit ( EXIT_SUCCESS ); +-+ return; +-+ case 'h': +-+ usage ( argc, argv, opt ); +-+ data.argc = 1; +-+ data.argv = &cmplt; +-+ data.mode = 1; +-+ goto done; +-+ case 't': +-+ data.timeout = atoi ( optarg ); +-+ break; +-+ case 'z': +-+ data.zsh = 1; +-+ break; +-+ case 'm': +-+ data.monitor = true; +-+ if ( bt_log_open() < 0 ) +-+ { +-+ data.monitor = false; +-+ printf ( "Unable to open logging channel\n" ); +-+ } +-+ break; +-+ default: +-+ if ( index < 0 ) +-+ { +-+ for ( index = 0; options[index].val; index++ ) +-+ { +-+ if ( c == options[index].val ) +-+ break; +-+ } +-+ } +-+ +-+ if ( c != opt->options[index - offset].val ) +-+ { +-+ usage ( argc, argv, opt ); +-+ exit ( EXIT_SUCCESS ); +-+ return; +-+ } +-+ +-+ *opt->optarg[index - offset] = optarg; +-+ } +-+ +-+ index = -1; +-+ } +-+ +-+ bt_shell_set_env ( "SHELL", data.name ); +-+ +-+ data.argc = argc - optind; +-+ data.argv = argv + optind; +-+ optind = 0; +-+ data.mode = ( data.argc > 0 ); +- +- done: +-- if (data.mode) +-- bt_shell_set_env("NON_INTERACTIVE", &data.mode); +-+ if ( data.mode ) +-+ bt_shell_set_env ( "NON_INTERACTIVE", &data.mode ); +- +-- mainloop_init(); +-+ mainloop_init(); +- +-- rl_init(); +-+ rl_init(); +- +-- data.init = true; +-- data.prompts = queue_new(); +-+ data.init = true; +-+ data.prompts = queue_new(); +- } +- +--static void rl_cleanup(void) +-+static void rl_cleanup ( void ) +- { +-- if (data.mode) +-- return; +-+ if ( data.mode ) +-+ return; +- +-- if (data.history[0] != '\0') +-- write_history(data.history); +-+ if ( data.history[0] != '\0' ) +-+ write_history ( data.history ); +- +-- rl_message(""); +-- rl_callback_handler_remove(); +-+ rl_message ( "" ); +-+ rl_callback_handler_remove(); +- } +- +--static void env_destroy(void *data) +-+static void env_destroy ( void *data ) +- { +-- struct bt_shell_env *env = data; +-+ struct bt_shell_env *env = data; +- +-- free(env->name); +-- free(env); +-+ free ( env->name ); +-+ free ( env ); +- } +- +--int bt_shell_run(void) +-+int bt_shell_run ( void ) +- { +-- int status; +-+ int status; +- +-- status = mainloop_run_with_signal(signal_callback, NULL); +-+ status = mainloop_run_with_signal ( signal_callback, NULL ); +- +-- bt_shell_cleanup(); +-+ bt_shell_cleanup(); +- +-- return status; +-+ return status; +- } +- +--int bt_shell_exec(const char *input) +-+int bt_shell_exec ( const char *input ) +- { +-- wordexp_t w; +-- int err; +-+ wordexp_t w; +-+ int err; +- +-- if (!input) +-- return 0; +-+ if ( !input ) +-+ return 0; +- +-- if (wordexp(input, &w, WRDE_NOCMD)) +-- return -ENOEXEC; +-+ if ( wordexp ( input, &w, WRDE_NOCMD ) ) +-+ return -ENOEXEC; +- +-- if (w.we_wordc == 0) { +-- wordfree(&w); +-- return -ENOEXEC; +-- } +-+ if ( w.we_wordc == 0 ) +-+ { +-+ wordfree ( &w ); +-+ return -ENOEXEC; +-+ } +- +-- err = shell_exec(w.we_wordc, w.we_wordv); +-+ err = shell_exec ( w.we_wordc, w.we_wordv ); +- +-- wordfree(&w); +-+ wordfree ( &w ); +- +-- return err; +-+ return err; +- } +- +--void bt_shell_cleanup(void) +-+void bt_shell_cleanup ( void ) +- { +-- bt_shell_release_prompt(""); +-- bt_shell_detach(); +-+ bt_shell_release_prompt ( "" ); +-+ bt_shell_detach(); +- +-- if (data.envs) { +-- queue_destroy(data.envs, env_destroy); +-- data.envs = NULL; +-- } +-+ if ( data.envs ) +-+ { +-+ queue_destroy ( data.envs, env_destroy ); +-+ data.envs = NULL; +-+ } +- +-- if (data.monitor) +-- bt_log_close(); +-+ if ( data.monitor ) +-+ bt_log_close(); +- +-- rl_cleanup(); +-+ rl_cleanup(); +- +-- queue_destroy(data.prompts, prompt_free); +-- data.prompts = NULL; +-+ queue_destroy ( data.prompts, prompt_free ); +-+ data.prompts = NULL; +- +-- data.init = false; +-- free(data.name); +-+ data.init = false; +-+ free ( data.name ); +- } +- +--void bt_shell_quit(int status) +-+void bt_shell_quit ( int status ) +- { +-- if (status == EXIT_SUCCESS) +-- mainloop_exit_success(); +-- else +-- mainloop_exit_failure(); +-+ if ( status == EXIT_SUCCESS ) +-+ mainloop_exit_success(); +-+ else +-+ mainloop_exit_failure(); +- } +- +--void bt_shell_noninteractive_quit(int status) +-+void bt_shell_noninteractive_quit ( int status ) +- { +-- if (!data.mode || data.timeout) +-- return; +-+ if ( !data.mode || data.timeout ) +-+ return; +- +-- bt_shell_quit(status); +-+ bt_shell_quit ( status ); +- } +- +--bool bt_shell_set_menu(const struct bt_shell_menu *menu) +-+bool bt_shell_set_menu ( const struct bt_shell_menu *menu ) +- { +-- if (!menu) +-- return false; +-+ if ( !menu ) +-+ return false; +- +-- data.menu = menu; +-+ data.menu = menu; +- +-- if (!data.main) +-- data.main = menu; +-+ if ( !data.main ) +-+ data.main = menu; +- +-- return true; +-+ return true; +- } +- +--bool bt_shell_add_submenu(const struct bt_shell_menu *menu) +-+bool bt_shell_add_submenu ( const struct bt_shell_menu *menu ) +- { +-- if (!menu) +-- return false; +-+ if ( !menu ) +-+ return false; +- +-- if (!data.main) +-- return bt_shell_set_menu(menu); +-+ if ( !data.main ) +-+ return bt_shell_set_menu ( menu ); +- +-- if (!data.submenus) +-- data.submenus = queue_new(); +-+ if ( !data.submenus ) +-+ data.submenus = queue_new(); +- +-- queue_push_tail(data.submenus, (void *) menu); +-+ queue_push_tail ( data.submenus, ( void * ) menu ); +- +-- return true; +-+ return true; +- } +- +--void bt_shell_set_prompt(const char *string) +-+void bt_shell_set_prompt ( const char *string ) +- { +-- if (!data.init || data.mode) +-- return; +-+ if ( !data.init || data.mode ) +-+ return; +- +-- rl_set_prompt(string); +-- rl_redisplay(); +-+ rl_set_prompt ( string ); +-+ rl_redisplay(); +- } +- +--static bool input_read(struct io *io, void *user_data) +-+static bool input_read ( struct io *io, void *user_data ) +- { +-- rl_callback_read_char(); +-- return true; +-+ rl_callback_read_char(); +-+ return true; +- } +- +--static bool shell_quit(void *data) +-+static bool shell_quit ( void *data ) +- { +-- mainloop_quit(); +-+ mainloop_quit(); +- +-- return false; +-+ return false; +- } +- +--bool bt_shell_attach(int fd) +-+bool bt_shell_attach ( int fd ) +- { +-- struct io *io; +-+ struct io *io; +- +-- /* TODO: Allow more than one input? */ +-- if (data.input) +-- return false; +-+ /* TODO: Allow more than one input? */ +-+ if ( data.input ) +-+ return false; +- +-- io = io_new(fd); +-+ io = io_new ( fd ); +- +-- if (!data.mode) { +-- io_set_read_handler(io, input_read, NULL, NULL); +-- io_set_disconnect_handler(io, io_hup, NULL, NULL); +-- } +-+ if ( !data.mode ) +-+ { +-+ io_set_read_handler ( io, input_read, NULL, NULL ); +-+ io_set_disconnect_handler ( io, io_hup, NULL, NULL ); +-+ } +- +-- data.input = io; +-+ data.input = io; +- +-- if (data.mode) { +-- if (shell_exec(data.argc, data.argv) < 0) { +-- bt_shell_noninteractive_quit(EXIT_FAILURE); +-- return true; +-- } +-+ if ( data.mode ) +-+ { +-+ if ( shell_exec ( data.argc, data.argv ) < 0 ) +-+ { +-+ bt_shell_noninteractive_quit ( EXIT_FAILURE ); +-+ return true; +-+ } +- +-- if (data.timeout) +-- timeout_add(data.timeout * 1000, shell_quit, NULL, +-- NULL); +-- } +-+ if ( data.timeout ) +-+ timeout_add ( data.timeout * 1000, shell_quit, NULL, +-+ NULL ); +-+ } +- +-- return true; +-+ return true; +- } +- +--bool bt_shell_detach(void) +-+bool bt_shell_detach ( void ) +- { +-- if (!data.input) +-- return false; +-+ if ( !data.input ) +-+ return false; +- +-- io_destroy(data.input); +-- data.input = NULL; +-+ io_destroy ( data.input ); +-+ data.input = NULL; +- +-- return true; +-+ return true; +- } +- +--static bool match_env(const void *data, const void *user_data) +-+static bool match_env ( const void *data, const void *user_data ) +- { +-- const struct bt_shell_env *env = data; +-- const char *name = user_data; +-+ const struct bt_shell_env *env = data; +-+ const char *name = user_data; +- +-- return !strcmp(env->name, name); +-+ return !strcmp ( env->name, name ); +- } +- +--void bt_shell_set_env(const char *name, void *value) +-+void bt_shell_set_env ( const char *name, void *value ) +- { +-- struct bt_shell_env *env; +-+ struct bt_shell_env *env; +- +-- if (!data.envs) { +-- if (!value) +-- return; +-- data.envs = queue_new(); +-- goto done; +-- } +-+ if ( !data.envs ) +-+ { +-+ if ( !value ) +-+ return; +-+ data.envs = queue_new(); +-+ goto done; +-+ } +- +-- env = queue_remove_if(data.envs, match_env, (void *) name); +-- if (env) +-- env_destroy(env); +-+ env = queue_remove_if ( data.envs, match_env, ( void * ) name ); +-+ if ( env ) +-+ env_destroy ( env ); +- +-- /* Don't create an env if value is not set */ +-- if (!value) +-- return; +-+ /* Don't create an env if value is not set */ +-+ if ( !value ) +-+ return; +- +- done: +-- env = new0(struct bt_shell_env, 1); +-- env->name = strdup(name); +-- env->value = value; +-+ env = new0 ( struct bt_shell_env, 1 ); +-+ env->name = strdup ( name ); +-+ env->value = value; +-+ +-+ queue_push_tail ( data.envs, env ); +-+} +-+ +-+void *bt_shell_get_env ( const char *name ) +-+{ +-+ const struct bt_shell_env *env; +-+ +-+ if ( !data.envs ) +-+ return NULL; +-+ +-+ env = queue_find ( data.envs, match_env, name ); +-+ if ( !env ) +-+ return NULL; +- +-- queue_push_tail(data.envs, env); +-+ return env->value; +- } +- +--void *bt_shell_get_env(const char *name) +-+ +-+static int we_check(const char *words) +- { +-- const struct bt_shell_env *env; +-+ if (!words) { +-+ return WRDE_SYNTAX; +-+ } +-+ +-+ /* 允许空字符串 - 这在参数解析中是合法的 */ +-+ if (*words == '\0') { +-+ return WRDE_SUCCESS; +-+ } +-+ +-+ return WRDE_SUCCESS; +-+} +- +-- if (!data.envs) +-- return NULL; +-+static char **bluetooth_tokenize(const char *input, int *count) +-+{ +-+ if (!input || !count) return NULL; +-+ +-+ if (*input == '\0') { +-+ *count = 0; +-+ char **tokens = malloc(sizeof(char *)); +-+ if (tokens) tokens[0] = NULL; +-+ return tokens; +-+ } +-+ +-+ char *str = strdup(input); +-+ if (!str) return NULL; +-+ +-+ int max_tokens = 10; +-+ char **tokens = malloc(max_tokens * sizeof(char *)); +-+ if (!tokens) { +-+ free(str); +-+ return NULL; +-+ } +-+ +-+ int token_count = 0; +-+ char *p = str; +-+ +-+ while (*p) { +-+ while (*p && isspace((unsigned char)*p)) p++; +-+ if (!*p) break; +-+ +-+ char *start = p; +-+ +-+ while (*p && !isspace((unsigned char)*p)) p++; +-+ +-+ if (p > start) { +-+ int token_len = p - start; +-+ tokens[token_count] = malloc(token_len + 1); +-+ if (!tokens[token_count]) { +-+ for (int i = 0; i < token_count; i++) free(tokens[i]); +-+ free(tokens); +-+ free(str); +-+ return NULL; +-+ } +-+ memcpy(tokens[token_count], start, token_len); +-+ tokens[token_count][token_len] = '\0'; +-+ token_count++; +-+ +-+ if (token_count >= max_tokens) { +-+ max_tokens *= 2; +-+ char **new_tokens = realloc(tokens, max_tokens * sizeof(char *)); +-+ if (!new_tokens) { +-+ for (int i = 0; i < token_count; i++) free(tokens[i]); +-+ free(tokens); +-+ free(str); +-+ return NULL; +-+ } +-+ tokens = new_tokens; +-+ } +-+ } +-+ } +-+ +-+ tokens[token_count] = NULL; +-+ *count = token_count; +-+ free(str); +-+ return tokens; +-+} +- +-- env = queue_find(data.envs, match_env, name); +-- if (!env) +-- return NULL; +-+static size_t calculate_total_bytes(char **tokens, int count) +-+{ +-+ size_t total = 0; +-+ for (int i = 0; i < count; i++) { +-+ total += strlen(tokens[i]) + 1; +-+ } +-+ return total; +-+} +- +-- return env->value; +-+int wordexp(const char *__restrict words, wordexp_t *__restrict we, int flags) +-+{ +-+ int error; +-+ +-+ if (flags & WRDE_REUSE) { +-+ wordfree(we); +-+ } +-+ +-+ if ((flags & WRDE_APPEND) == 0) { +-+ we->we_wordc = 0; +-+ we->we_wordv = NULL; +-+ we->we_strings = NULL; +-+ we->we_nbytes = 0; +-+ } +-+ +-+ error = we_check(words); +-+ if (error != WRDE_SUCCESS) { +-+ wordfree(we); +-+ return error; +-+ } +-+ +-+ /* 特殊处理:空字符串 */ +-+ if (!words || *words == '\0') { +-+ /* 对于空输入,确保有一个空的 wordv 数组 */ +-+ if (!we->we_wordv) { +-+ we->we_wordv = malloc(sizeof(char *)); +-+ if (!we->we_wordv) { +-+ return WRDE_NOSPACE; +-+ } +-+ } +-+ /* 设置第一个元素为 NULL,表示结束 */ +-+ we->we_wordv[0] = NULL; +-+ we->we_wordc = 0; /* 空输入有0个单词 */ +-+ return WRDE_SUCCESS; +-+ } +-+ +-+ int token_count = 0; +-+ char **tokens = bluetooth_tokenize(words, &token_count); +-+ if (!tokens) { +-+ wordfree(we); +-+ return WRDE_NOSPACE; +-+ } +-+ +-+ if (token_count == 0) { +-+ free(tokens); +-+ we->we_wordv = malloc(sizeof(char *)); +-+ if (!we->we_wordv) { +-+ return WRDE_NOSPACE; +-+ } +-+ we->we_wordv[0] = NULL; +-+ we->we_wordc = 0; +-+ we->we_nbytes = 0; +-+ return WRDE_SUCCESS; +-+ } +-+ +-+ size_t total_bytes = calculate_total_bytes(tokens, token_count); +-+ +-+ char **new_wordv; +-+ if (flags & WRDE_APPEND) { +-+ new_wordv = reallocarray(we->we_wordv, we->we_wordc + token_count + 1, sizeof(char *)); +-+ } else { +-+ new_wordv = reallocarray(NULL, token_count + 1, sizeof(char *)); +-+ } +-+ +-+ if (!new_wordv) { +-+ for (int i = 0; i < token_count; i++) free(tokens[i]); +-+ free(tokens); +-+ wordfree(we); +-+ return WRDE_NOSPACE; +-+ } +-+ we->we_wordv = new_wordv; +-+ +-+ char *new_strings; +-+ if (flags & WRDE_APPEND) { +-+ new_strings = realloc(we->we_strings, we->we_nbytes + total_bytes); +-+ } else { +-+ new_strings = malloc(total_bytes); +-+ } +-+ +-+ if (!new_strings) { +-+ for (int i = 0; i < token_count; i++) free(tokens[i]); +-+ free(tokens); +-+ free(we->we_wordv); +-+ we->we_wordv = NULL; +-+ return WRDE_NOSPACE; +-+ } +-+ we->we_strings = new_strings; +-+ +-+ char *current; +-+ if (flags & WRDE_APPEND) { +-+ current = we->we_strings + we->we_nbytes; +-+ } else { +-+ current = we->we_strings; +-+ we->we_nbytes = 0; +-+ } +-+ +-+ int start_index = we->we_wordc; +-+ for (int i = 0; i < token_count; i++) { +-+ size_t len = strlen(tokens[i]); +-+ memcpy(current, tokens[i], len); +-+ current[len] = '\0'; +-+ we->we_wordv[start_index + i] = current; +-+ current += len + 1; +-+ +-+ free(tokens[i]); +-+ } +-+ free(tokens); +-+ +-+ we->we_wordv[start_index + token_count] = NULL; +-+ we->we_wordc += token_count; +-+ we->we_nbytes += total_bytes; +-+ +-+ return WRDE_SUCCESS; +- } +-+ +-+void wordfree(wordexp_t *we) +-+{ +-+ if (!we) +-+ return; +-+ +-+ free(we->we_wordv); +-+ free(we->we_strings); +-+ +-+ we->we_wordv = NULL; +-+ we->we_strings = NULL; +-+ we->we_nbytes = 0; +-+ we->we_wordc = 0; +-+} +-\ No newline at end of file +diff --git a/sysdrv/tools/board/buildroot/hcitool_patch/0003-fixed-compat-wordexp.patch b/sysdrv/tools/board/buildroot/hcitool_patch/0003-fixed-compat-wordexp.patch +deleted file mode 100755 +index 8d54120a4..000000000 +--- a/sysdrv/tools/board/buildroot/hcitool_patch/0003-fixed-compat-wordexp.patch ++++ /dev/null +@@ -1,109 +0,0 @@ +-diff --git a/android/compat/wordexp.h b/android/compat/wordexp.h +-index 08c59ee..b88b502 100644 +---- a/android/compat/wordexp.h +-+++ b/android/compat/wordexp.h +-@@ -1,31 +1,88 @@ +--/* SPDX-License-Identifier: LGPL-2.1-or-later */ +--/* +-+/*********************************************************** +-+ * @Author: sprink +-+ * @Date: 2025-10-28 07:51:24 +-+ * @LastEditTime: 2025-10-28 08:06:53 +-+ * @LastEditors: sprink +-+ * @Description: +-+ * +-+ ***********************************************************/ +-+/*- +-+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD +- * +-- * BlueZ - Bluetooth protocol stack for Linux +-+ * Copyright (c) 2002 Tim J. Robbins. +-+ * All rights reserved. +- * +-- * Copyright (C) 1991-2013 Free Software Foundation, Inc. +-+ * Redistribution and use in source and binary forms, with or without +-+ * modification, are permitted provided that the following conditions +-+ * are met: +-+ * 1. Redistributions of source code must retain the above copyright +-+ * notice, this list of conditions and the following disclaimer. +-+ * 2. Redistributions in binary form must reproduce the above copyright +-+ * notice, this list of conditions and the following disclaimer in the +-+ * documentation and/or other materials provided with the distribution. +- * +-+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +-+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +-+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +-+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +-+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +-+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +-+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +-+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +-+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +-+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +-+ * SUCH DAMAGE. +- * +-+ * $FreeBSD$ +- */ +- +- #ifndef _WORDEXP_H_ +--#define _WORDEXP_H_ +-+#define _WORDEXP_H_ +- +--#define WRDE_NOCMD 0 +-+#include +-+#include +- +- typedef struct { +-- size_t we_wordc; +-- char **we_wordv; +-- size_t we_offs; +-+ size_t we_wordc; /* count of words matched */ +-+ char **we_wordv; /* pointer to list of words */ +-+ size_t we_offs; /* slots to reserve in we_wordv */ +-+ char *we_strings; /* storage for wordv strings */ +-+ size_t we_nbytes; /* size of we_strings */ +- } wordexp_t; +- +--static inline int wordexp(const char *c, wordexp_t *w, int _i) +--{ +-- return -1; +--} +-+/* +-+ * Flags for wordexp(). +-+ */ +- +--static inline void wordfree(wordexp_t *__wordexp) +--{ +--} +-+#define WRDE_APPEND 0x1 /* append to previously generated */ +-+#define WRDE_DOOFFS 0x2 /* we_offs member is valid */ +-+#define WRDE_NOCMD 0x4 /* disallow command substitution */ +-+#define WRDE_REUSE 0x8 /* reuse wordexp_t */ +-+#define WRDE_SHOWERR 0x10 /* don't redirect stderr to /dev/null */ +-+#define WRDE_UNDEF 0x20 /* disallow undefined shell vars */ +-+ +-+/* +-+ * Return values from wordexp(). +-+ */ +-+#define WRDE_SUCCESS 0 /* 成功 */ +-+#define WRDE_BADCHAR 1 /* unquoted special character */ +-+#define WRDE_BADVAL 2 /* undefined variable */ +-+#define WRDE_CMDSUB 3 /* command substitution not allowed */ +-+#define WRDE_NOSPACE 4 /* no memory for result */ +-+#if __XSI_VISIBLE +-+#define WRDE_NOSYS 5 /* obsolete, reserved */ +-+#endif +-+#define WRDE_SYNTAX 6 /* shell syntax error */ +- +-+#ifdef __cplusplus +-+extern "C" { +- #endif +-+ +-+int wordexp(const char * __restrict, wordexp_t * __restrict, int); +-+void wordfree(wordexp_t *); +-+ +-+#ifdef __cplusplus +-+} +-+#endif +-+ +-+#endif /* !_WORDEXP_H_ */ diff --git a/luckfox-sdk-patches/board/patches/0011-oem-flare-minimize.patch b/luckfox-sdk-patches/board/patches/0011-oem-flare-minimize.patch new file mode 100644 index 0000000..478618e --- /dev/null +++ b/luckfox-sdk-patches/board/patches/0011-oem-flare-minimize.patch @@ -0,0 +1,55 @@ +Strip camera and ISP payload from /oem. + +luckfox-buildroot-oem-pre.sh runs over the OEM staging tree before it is +packed. flare_minimize() removes what WardenOS never uses because no image +sensor is fitted: the ISP tuning files (about 32 MB), the rkaiq 3A library +and servers, the camera capture kernel modules, and the camera and MPI +sample binaries. + +NPU, video codec, RGA and rockit are kept on purpose. WiFi/BT, touch and the +insmod scripts are not touched. + +diff --git a/project/cfg/BoardConfig_IPC/luckfox-buildroot-oem-pre.sh b/project/cfg/BoardConfig_IPC/luckfox-buildroot-oem-pre.sh +index 4187457d4..de2bf74e6 100755 +--- a/project/cfg/BoardConfig_IPC/luckfox-buildroot-oem-pre.sh ++++ b/project/cfg/BoardConfig_IPC/luckfox-buildroot-oem-pre.sh +@@ -46,4 +46,37 @@ function remove_data() + #========================= + # run + #========================= +-remove_data +\ No newline at end of file ++remove_data ++#========================= ++# flare-edge: drop camera / ISP from /oem ++# ++# Camera is out of scope for the Warden gateway role (no sensor attached). ++# NPU, video codec, RGA and rockit are KEPT deliberately - they are onboard ++# hardware we chose to retain. WiFi/BT (aic8800*), touch (goodix) and the ++# insmod scripts are never touched here. ++#========================= ++function flare_minimize() ++{ ++ KO=$RK_PROJECT_PACKAGE_OEM_DIR/usr/ko ++ ++ # ISP tuning data - by far the biggest item in /oem (~32MB) ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/share/iqfiles ++ ++ # ISP 3A library and its servers ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/lib/librkaiq* ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/bin/rkaiq_3A_server ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/bin/rkaiq_tool_server ++ ++ # camera capture path: ISP, CIF, MIPI CSI-2 D-PHY, and the sensor drivers ++ lf_rm $KO/video_rkisp.ko $KO/video_rkcif.ko ++ lf_rm $KO/phy-rockchip-csi2-dphy.ko $KO/phy-rockchip-csi2-dphy-hw.ko ++ lf_rm $KO/sc3336.ko $KO/mis5001.ko $KO/gspca_main.ko ++ ++ # camera/ISP demo + sample binaries (keep luckfox_*_test and modetest: ++ # those are the GPIO/UART/I2C/SPI/PWM/display bring-up tools we want) ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/bin/sample_* ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/bin/rk_mpi_* ++ lf_rm $RK_PROJECT_PACKAGE_OEM_DIR/usr/bin/simple_* ++} ++ ++flare_minimize diff --git a/luckfox-sdk-patches/board/patches/0012-drop-s50sshd.patch b/luckfox-sdk-patches/board/patches/0012-drop-s50sshd.patch new file mode 100644 index 0000000..a65f1cf --- /dev/null +++ b/luckfox-sdk-patches/board/patches/0012-drop-s50sshd.patch @@ -0,0 +1,65 @@ +Remove the generic S50sshd init script. + +overlay-luckfox-buildroot-init is in the 86Panel board's RK_POST_OVERLAY. +Its S50sshd runs `ssh-keygen -A` inline. With no hardware RNG and no saved +seed on a freshly written rootfs, getrandom() blocks, and rcS is sequential, +so every init script sorting after it (S50usbdevice among them) never ran +and the board came up with no reachable interface. + +WardenOS gets its SSH server from Buildroot's dropbear package, selected by +flare_warden_defconfig, which installs its own init script. + +diff --git a/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-buildroot-init/etc/init.d/S50sshd b/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-buildroot-init/etc/init.d/S50sshd +deleted file mode 100755 +index e5b9c4451..000000000 +--- a/project/cfg/BoardConfig_IPC/overlay/overlay-luckfox-buildroot-init/etc/init.d/S50sshd ++++ /dev/null +@@ -1,48 +0,0 @@ +-#!/bin/sh +-# +-# sshd Starts sshd. +-# +- +-# Make sure the ssh-keygen progam exists +-[ -f /usr/bin/ssh-keygen ] || exit 0 +- +-umask 077 +- +-start() { +- chown root:root /var/empty/ +- # Create any missing keys +- /usr/bin/ssh-keygen -A +- +- printf "Starting sshd: " +- /usr/sbin/sshd +- touch /var/lock/sshd +- echo "OK" +-} +-stop() { +- printf "Stopping sshd: " +- killall sshd +- rm -f /var/lock/sshd +- echo "OK" +-} +-restart() { +- stop +- start +-} +- +-case "$1" in +- start) +- start +- ;; +- stop) +- stop +- ;; +- restart|reload) +- restart +- ;; +- *) +- echo "Usage: $0 {start|stop|restart}" +- exit 1 +-esac +- +-exit $? +- diff --git a/luckfox-sdk-patches/board/patches/0013-e2fsprogs-build-cflags.patch b/luckfox-sdk-patches/board/patches/0013-e2fsprogs-build-cflags.patch new file mode 100644 index 0000000..65eae60 --- /dev/null +++ b/luckfox-sdk-patches/board/patches/0013-e2fsprogs-build-cflags.patch @@ -0,0 +1,21 @@ +Build e2fsprogs host helpers with gcc 14. + +The vendor's e2fsprogs 1.43.9 compiles a few helper programs for the build +host (BUILD_CC) while cross-building the target tools. That source relies on +implicit function declarations and implicit int, which gcc 14 turns into +errors, so the host helper build fails. BUILD_CFLAGS demotes those two back +to warnings for the host helpers only; the target CFLAGS are unchanged. + +diff --git a/sysdrv/tools/board/e2fsprogs/Makefile b/sysdrv/tools/board/e2fsprogs/Makefile +index 14ab9e503..baa541093 100644 +--- a/sysdrv/tools/board/e2fsprogs/Makefile ++++ b/sysdrv/tools/board/e2fsprogs/Makefile +@@ -20,7 +20,7 @@ all: + mkdir -p $(CURRENT_DIR)/$(PKG_BIN)/usr/; \ + pushd $(CURRENT_DIR)/$(PKG_NAME)/; \ + ./configure --host=$(SYSDRV_CROSS) --target=$(SYSDRV_CROSS) CFLAGS="$(SYSDRV_CROSS_CFLAGS)" \ +- LDFLAGS="$(SYSDRV_CROSS_CFLAGS)" --prefix=$(CURRENT_DIR)/$(PKG_BIN) \ ++ LDFLAGS="$(SYSDRV_CROSS_CFLAGS)" BUILD_CFLAGS="-Wno-implicit-function-declaration -Wno-implicit-int" --prefix=$(CURRENT_DIR)/$(PKG_BIN) \ + --disable-nls \ + --disable-debugfs --disable-imager --disable-defrag --enable-fsck --enable-resizer \ + --disable-testio-debug --disable-rpath --enable-symlink-install --disable-fuse2fs; \ diff --git a/luckfox-sdk-patches/kernel/configs/flare-wireguard.config b/luckfox-sdk-patches/kernel/configs/flare-wireguard.config new file mode 100644 index 0000000..9680b75 --- /dev/null +++ b/luckfox-sdk-patches/kernel/configs/flare-wireguard.config @@ -0,0 +1,10 @@ +# WireGuard, built in rather than as a module. +# +# =y not =m deliberately: modules live on the oem partition and must be +# reflashed in lockstep with the kernel, and this one is ~40KB. Paying that in +# boot.img is cheaper than another matched-pair failure mode. +# +# Everything WireGuard needs it "select"s for itself (NET_UDP_TUNNEL, +# DST_CACHE, CRYPTO_LIB_CURVE25519, CRYPTO_LIB_CHACHA20POLY1305), so unlike the +# USB-Ethernet fragment there is no menu gate to open first. Verify anyway. +CONFIG_WIREGUARD=y diff --git a/luckfox-sdk-patches/kernel/configs/rv1106-usb-host-ethernet.config b/luckfox-sdk-patches/kernel/configs/rv1106-usb-host-ethernet.config new file mode 100644 index 0000000..6a90b22 --- /dev/null +++ b/luckfox-sdk-patches/kernel/configs/rv1106-usb-host-ethernet.config @@ -0,0 +1,20 @@ +# USB host + USB-Ethernet class drivers (flare-edge). +# +# The first symbol is the MENU GATE. Every driver below lives inside the +# USB_NET_DRIVERS menuconfig, so without it kconfig discards the lot in +# silence -- no warning, and not even the usual "not in final .config" from +# merge_config.sh. +# +# Do not begin a comment line here with a hash followed by CONFIG_. That is +# exactly the "is not set" syntax, so merge_config.sh parses the prose as a +# directive and reports it as a redefinition of the symbol it names. +CONFIG_USB_NET_DRIVERS=y +CONFIG_USB_USBNET=y +CONFIG_USB_NET_CDCETHER=y +CONFIG_USB_NET_CDC_NCM=y +CONFIG_USB_NET_CDC_EEM=y +CONFIG_USB_RTL8152=y +CONFIG_USB_NET_AX8817X=y +CONFIG_USB_NET_AX88179_178A=y +CONFIG_USB_NET_SMSC95XX=y +CONFIG_USB_NET_MCS7830=y -- 2.54.0 From 67da1141a0f23202f28ce46fa46340d0d6f0430c Mon Sep 17 00:00:00 2001 From: Noah Date: Mon, 28 Sep 2026 11:10:53 -0600 Subject: [PATCH 2/2] build: unpack the pinned Buildroot tree into the vendor SDK build/unpack-buildroot.sh fetches buildroot-2025.02.8 through fetch-buildroot-tarball.sh (sha256-pinned) and unpacks it to sysdrv/source/buildroot/buildroot-2025.02.8, the tree the patched project/build.sh and sysdrv/Makefile select. It refuses an SDK whose selectors are not patched, leaves an existing tree alone, and never leaves a partial tree at the final path. flare-edge's build-firmware.sh calls it right after applying luckfox-sdk-patches/. Offline regression tests cover the fresh unpack, the existing-tree no-op and every refusal path; the qemu-tools job runs and shellchecks them. Refs #4 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01441BzdGcvQmXkiN2qDjfZg --- .github/workflows/ci.yml | 8 +- build/fetch-buildroot-tarball.sh | 10 +- build/unpack-buildroot.sh | 81 ++++++++++ build/vendor.manifest | 3 +- docs/ci-cd.md | 2 +- tests/mk-bootimg/run-ci-wiring-tests.sh | 1 + .../run-unpack-buildroot-tests.sh | 138 ++++++++++++++++++ 7 files changed, 233 insertions(+), 10 deletions(-) create mode 100755 build/unpack-buildroot.sh create mode 100755 tests/unpack-buildroot/run-unpack-buildroot-tests.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c68e5f6..0d6efc6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -111,7 +111,8 @@ jobs: shellcheck -x qemu/*.sh qemu/tests/*.sh build/*.sh \ qemu/rootfs/etc/warden-lib.sh qemu/rootfs/etc/rc \ qemu/rootfs/sbin/init qemu/rootfs/init tests/mk-bootimg/*.sh \ - tests/fetch-vendor/*.sh tests/fetch-buildroot-tarball/*.sh + tests/fetch-vendor/*.sh tests/fetch-buildroot-tarball/*.sh \ + tests/unpack-buildroot/*.sh - name: cache apt archives (python3-pil) # Same cost class as the busybox binary cached below: a system package # plus its libjpeg/libpng transitive deps, downloaded fresh on every @@ -166,6 +167,11 @@ jobs: # Retry-on-mismatch, cleanup, and the already-verified # short-circuit, against a fake curl on PATH: no network. run: bash tests/fetch-buildroot-tarball/run-fetch-buildroot-tarball-tests.sh + - name: unpack-buildroot regression tests + # Fresh unpack, leave-existing-tree-alone, and every refusal path + # (unpatched selectors, bad tarball, half-present tree, failed + # fetch), against a stub fetch script: no network (issue #4). + run: bash tests/unpack-buildroot/run-unpack-buildroot-tests.sh - name: mk-bootimg boot.img validation regression tests # Guards issue #22 (a missing/erroring fdtget silently skipping the # data-position check) plus the FIT metadata and per-image diff --git a/build/fetch-buildroot-tarball.sh b/build/fetch-buildroot-tarball.sh index 21e37da..92bd1ff 100755 --- a/build/fetch-buildroot-tarball.sh +++ b/build/fetch-buildroot-tarball.sh @@ -12,13 +12,9 @@ # Same shape as fetch-kernel-tarball.sh, deliberately: a version bump edits this # file and the pin beside it, nothing else. FAILS CLOSED on a missing pin. # -# Not wired into build-firmware.sh or CI yet -- tools/build-firmware.sh stages -# sdk-patches/buildroot/ onto whatever buildroot tree the vendor SDK already -# has, it does not yet extract this pinned tarball over it. Exercised today by -# tests/fetch-buildroot-tarball/run-fetch-buildroot-tarball-tests.sh and by -# running the script by hand; wiring it into the real build path is a separate -# change (it has to reconcile the pristine tree with the existing SDK buildroot -# checkout first). +# Called by build/unpack-buildroot.sh, which flare-edge's +# tools/build-firmware.sh runs after applying luckfox-sdk-patches/ (issue #4). +# Tested by tests/fetch-buildroot-tarball/run-fetch-buildroot-tarball-tests.sh. # # Usage: fetch-buildroot-tarball.sh set -euo pipefail diff --git a/build/unpack-buildroot.sh b/build/unpack-buildroot.sh new file mode 100755 index 0000000..76bd069 --- /dev/null +++ b/build/unpack-buildroot.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# Unpack the pinned Buildroot tree where the vendor SDK builds it. +# +# The vendor SDK ships buildroot-2023.02.6. WardenOS builds against +# buildroot-2025.02.8, which is not in the vendor checkout, so a clean checkout +# plus luckfox-sdk-patches/ had nothing to build (blueflare/bfe-core1106-sdk#4). +# This script closes that gap: it fetches the tarball with +# fetch-buildroot-tarball.sh (sha256-pinned, fails closed) and unpacks it to +# sysdrv/source/buildroot/buildroot-, the path the patched project/build.sh +# and sysdrv/Makefile select. +# +# Idempotent. An SDK that already has the tree is left alone, so the +# persistent CI SDK and the workstation SDK are untouched. A partial unpack +# never lands at the final path: the tarball is unpacked into a scratch +# directory beside it and moved into place only when complete. +# +# Our own Buildroot delta (defconfigs, package patches, the busybox applet +# patch) is NOT applied here. flare-edge's tools/build-firmware.sh stages it +# after this script runs, the same way it applies the rest of the series. +# +# Usage: unpack-buildroot.sh +# +# The tarball is cached at sysdrv/source/buildroot/buildroot-.tar.xz +# (inside a directory the vendor .gitignore already ignores); set +# WARDEN_BUILDROOT_TARBALL to use another path. +set -euo pipefail + +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # build/ +FETCH="$HERE/fetch-buildroot-tarball.sh" +SDK="${1:?usage: unpack-buildroot.sh }" + +die() { echo "unpack-buildroot: FATAL: $*" >&2; exit 1; } + +[ -f "$FETCH" ] || die "missing $FETCH" +BRVER="$(sed -n 's/^BRVER=//p' "$FETCH" | head -1)" +case "$BRVER" in + ''|*[!0-9.]*) die "cannot read BRVER from $FETCH" ;; +esac + +# The SDK must already select this version. Both selectors come from +# luckfox-sdk-patches/board/ (0006 for project/build.sh, 0009 for +# sysdrv/Makefile); if either still names another tree the series has not +# been applied, and unpacking would only hide that. +BUILD_SH="$SDK/project/build.sh" +MAKEFILE="$SDK/sysdrv/Makefile" +[ -f "$BUILD_SH" ] || die "no vendor SDK at $SDK (missing project/build.sh)" +[ -f "$MAKEFILE" ] || die "no vendor SDK at $SDK (missing sysdrv/Makefile)" +# shellcheck disable=SC2016 # the literal ${SDK_SYSDRV_DIR} text in build.sh +sel_build="$(sed -n 's|^BUILDROOT_PATH=${SDK_SYSDRV_DIR}/source/buildroot/||p' "$BUILD_SH" | head -1)" +sel_make="$(sed -n 's/^BUILDROOT_VER[[:space:]]*?=[[:space:]]*//p' "$MAKEFILE" | head -1)" +[ "$sel_build" = "buildroot-$BRVER" ] || + die "project/build.sh selects '${sel_build:-nothing}', not buildroot-$BRVER (apply luckfox-sdk-patches/board first)" +[ "$sel_make" = "buildroot-$BRVER" ] || + die "sysdrv/Makefile selects '${sel_make:-nothing}', not buildroot-$BRVER (apply luckfox-sdk-patches/board first)" + +PARENT="$SDK/sysdrv/source/buildroot" +TREE="$PARENT/buildroot-$BRVER" +TB="${WARDEN_BUILDROOT_TARBALL:-$PARENT/buildroot-$BRVER.tar.xz}" + +if [ -f "$TREE/Makefile" ] && [ -d "$TREE/configs" ] && [ -d "$TREE/package" ]; then + echo "buildroot-$BRVER: tree already present at ${TREE#"$SDK"/}" + exit 0 +fi +# Something is at the path but it is not a Buildroot tree. Deleting it here +# could destroy build output in a shared SDK, so say so and stop. +[ ! -e "$TREE" ] || die "$TREE exists but is not a complete Buildroot tree; remove it and rerun" + +mkdir -p "$PARENT" +bash "$FETCH" "$TB" + +SCRATCH="$(mktemp -d "$PARENT/.unpack-buildroot.XXXXXX")" +[ -d "$SCRATCH" ] || die "mktemp failed under $PARENT" +cleanup() { rm -rf "$SCRATCH"; } +trap cleanup EXIT + +echo "== unpacking buildroot-$BRVER into ${TREE#"$SDK"/}" +tar -xJf "$TB" -C "$SCRATCH" +[ -f "$SCRATCH/buildroot-$BRVER/Makefile" ] || + die "tarball did not contain buildroot-$BRVER/Makefile" +mv "$SCRATCH/buildroot-$BRVER" "$TREE" +echo "buildroot-$BRVER: unpacked" diff --git a/build/vendor.manifest b/build/vendor.manifest index b0922ed..bae7d57 100644 --- a/build/vendor.manifest +++ b/build/vendor.manifest @@ -17,7 +17,8 @@ # release tarball has no commit to name: # # linux-6.18.46 build/fetch-kernel-tarball.sh -# buildroot-2025.02.8 build/fetch-buildroot-tarball.sh +# buildroot-2025.02.8 build/fetch-buildroot-tarball.sh (unpacked into the +# vendor SDK by build/unpack-buildroot.sh) # # The buildroot one matters more than it looks. The firmware builds against # sysdrv/source/buildroot/buildroot-2025.02.8 inside the vendor SDK, and the diff --git a/docs/ci-cd.md b/docs/ci-cd.md index 7eb5b6f..d51a024 100644 --- a/docs/ci-cd.md +++ b/docs/ci-cd.md @@ -13,7 +13,7 @@ execute code on private infrastructure (ADR-0007). | `mcdc` | ubuntu-latest | 100% MC/DC enforced on every `drivers/*/test` (gcc-14 `-fcondition-coverage`). | | `bench` | ubuntu-latest | Smoke-runs the sim + rs485-bridge micro-benchmarks; emits ns/op trend JSON. | | `patches-apply` | ubuntu-latest | Fetches pristine linux-6.18.46 (cached, sha256-verified) and applies `patches/*` in order. | -| `qemu-tools` | ubuntu-latest | shellcheck on `qemu/**.sh`, `tests/mk-bootimg/*.sh`, `tests/fetch-vendor/*.sh`, `tests/fetch-buildroot-tarball/*.sh`; runs the offline regression tests for the mk-bootimg probe, boot.img validation, run.sh argv ordering, mkimage's SEED_DIR hook, staged rootfs permissions, fetch-vendor, fetch-buildroot-tarball, and its own CI wiring; builds the initramfs (pinned busybox) and the A/B disk image. | +| `qemu-tools` | ubuntu-latest | shellcheck on `qemu/**.sh`, `tests/mk-bootimg/*.sh`, `tests/fetch-vendor/*.sh`, `tests/fetch-buildroot-tarball/*.sh`, `tests/unpack-buildroot/*.sh`; runs the offline regression tests for the mk-bootimg probe, boot.img validation, run.sh argv ordering, mkimage's SEED_DIR hook, staged rootfs permissions, fetch-vendor, fetch-buildroot-tarball, unpack-buildroot, and its own CI wiring; builds the initramfs (pinned busybox) and the A/B disk image. | | `quality` | ubuntu-latest | Codacy-style grade computed in-pipeline: clippy, cppcheck, shellcheck, ruff, lizard, jscpd, cargo-audit feed `tools/quality/score.py` (SQALE debt ratio + a separate worst-of security axis; SonarQube's published thresholds). Uploads `quality.json`; fails if the security grade is worse than C. | | `kernel-build` | ubuntu-latest, **dispatch-only** | apt-installs the cross toolchain + qemu, `build/build-kernel.sh` -> `zImage` + `rv1106-warden.dtb`, QEMU `-M virt` boot smoke (fail-closed), artifact upload (best-effort). Trigger: `gh workflow run ci.yml`. | | `prune-artifacts` | ubuntu-latest, dispatch-only | Deletes `kernel-rv1106` artifacts beyond the newest 3. | diff --git a/tests/mk-bootimg/run-ci-wiring-tests.sh b/tests/mk-bootimg/run-ci-wiring-tests.sh index 0d71f80..489cac1 100755 --- a/tests/mk-bootimg/run-ci-wiring-tests.sh +++ b/tests/mk-bootimg/run-ci-wiring-tests.sh @@ -37,6 +37,7 @@ scripts=( "qemu/tests/stage-rootfs-perms.sh" "tests/fetch-vendor/run-fetch-vendor-tests.sh" "tests/fetch-buildroot-tarball/run-fetch-buildroot-tarball-tests.sh" + "tests/unpack-buildroot/run-unpack-buildroot-tests.sh" "tests/mk-bootimg/run-boot-img-validate-tests.sh" "tests/mk-bootimg/run-help-tests.sh" "tests/mk-bootimg/run-ci-wiring-tests.sh" diff --git a/tests/unpack-buildroot/run-unpack-buildroot-tests.sh b/tests/unpack-buildroot/run-unpack-buildroot-tests.sh new file mode 100755 index 0000000..7611fc0 --- /dev/null +++ b/tests/unpack-buildroot/run-unpack-buildroot-tests.sh @@ -0,0 +1,138 @@ +#!/usr/bin/env bash +# Regression tests for build/unpack-buildroot.sh (blueflare/bfe-core1106-sdk#4). +# +# A clean vendor checkout plus luckfox-sdk-patches/ could not build WardenOS +# because nothing put buildroot-2025.02.8 where the SDK builds it. The unpack +# script is now the thing that does, and the persistent CI SDK depends on it +# leaving an existing tree alone. These cases pin both halves: a fresh SDK +# gets a complete tree, an existing tree is never touched or re-fetched, and +# every refusal (unpatched selectors, a bad tarball, a half-present tree, a +# failed fetch) leaves no tree and no scratch directory behind. +# +# Runs the real script, copied beside a stub fetch-buildroot-tarball.sh that +# copies a local fixture tarball instead of downloading. No network, no SDK. +set -uo pipefail + +HERE="$(cd "$(dirname "$0")" && pwd)" +SCRIPT="$HERE/../../build/unpack-buildroot.sh" +[ -f "$SCRIPT" ] || { echo "FAIL: script not found at $SCRIPT"; exit 1; } + +FAIL=0 +ok() { printf '[PASS] %s\n' "$1"; } +bad() { printf '[FAIL] %s\n' "$1"; FAIL=1; } + +TMP="$(mktemp -d)" || { echo "FAIL: mktemp -d failed"; exit 1; } +[ -d "$TMP" ] || { echo "FAIL: mktemp -d failed"; exit 1; } +trap 'rm -rf "$TMP"' EXIT + +VER=2025.02.8 + +# Fixture tarballs: a minimal Buildroot-shaped tree, and one without Makefile. +mk_tarball() { # mk_tarball + local out="$1" mf="$2" src + src="$(mktemp -d "$TMP/src.XXXXXX")" + mkdir -p "$src/buildroot-$VER/configs" "$src/buildroot-$VER/package/busybox" + printf 'CONFIG\n' > "$src/buildroot-$VER/package/busybox/busybox.config" + [ "$mf" = 1 ] && printf 'all:\n' > "$src/buildroot-$VER/Makefile" + tar -cJf "$out" -C "$src" "buildroot-$VER" +} +mk_tarball "$TMP/good.tar.xz" 1 +mk_tarball "$TMP/nomakefile.tar.xz" 0 + +# setup -> fixture dir holding build/ and sdk/ +setup() { + local dir="$TMP/$1" src="$2" + mkdir -p "$dir/build" "$dir/sdk/project" "$dir/sdk/sysdrv/source/buildroot" + cp "$SCRIPT" "$dir/build/unpack-buildroot.sh" + { + echo '#!/usr/bin/env bash' + echo "BRVER=$VER" + echo "echo fetch-called >> \"$dir/fetch-calls\"" + if [ "$src" = FAIL ]; then echo 'exit 1'; else echo "cp \"$src\" \"\$1\""; fi + } > "$dir/build/fetch-buildroot-tarball.sh" + # shellcheck disable=SC2016 # build.sh carries the literal text + printf 'BUILDROOT_PATH=${SDK_SYSDRV_DIR}/source/buildroot/buildroot-%s\n' "$VER" \ + > "$dir/sdk/project/build.sh" + printf 'BUILDROOT_VER ?= buildroot-%s\n' "$VER" > "$dir/sdk/sysdrv/Makefile" + printf '%s' "$dir" +} + +run() { # run -> $OUT, $RC + OUT="$(bash "$1/build/unpack-buildroot.sh" "$1/sdk" 2>&1)"; RC=$? +} +tree() { printf '%s' "$1/sdk/sysdrv/source/buildroot/buildroot-$VER"; } +fetches() { [ -f "$1/fetch-calls" ] && wc -l < "$1/fetch-calls" || echo 0; } +no_scratch() { ! compgen -G "$1/sdk/sysdrv/source/buildroot/.unpack-buildroot.*" >/dev/null; } + +# A. Fresh SDK: fetched once, unpacked to the selected path, no scratch left. +d="$(setup fresh "$TMP/good.tar.xz")"; run "$d" +if [ "$RC" -eq 0 ] && [ -f "$(tree "$d")/Makefile" ] && [ -f "$(tree "$d")/package/busybox/busybox.config" ] \ + && [ "$(fetches "$d")" -eq 1 ] && no_scratch "$d"; then + ok "fresh SDK: tree unpacked, one fetch, no scratch left" +else + bad "fresh SDK: tree unpacked, one fetch, no scratch left (rc=$RC, out=$OUT)" +fi + +# B. Rerun on that SDK: already present, no second fetch, content untouched. +printf 'LOCAL\n' > "$(tree "$d")/package/busybox/busybox.config" +run "$d" +if [ "$RC" -eq 0 ] && printf '%s' "$OUT" | grep -q 'tree already present' \ + && [ "$(fetches "$d")" -eq 1 ] && grep -qx LOCAL "$(tree "$d")/package/busybox/busybox.config"; then + ok "existing tree: left alone, not re-fetched" +else + bad "existing tree: left alone, not re-fetched (rc=$RC, out=$OUT)" +fi + +# C. project/build.sh still selects the vendor tree: refuse, fetch nothing. +d="$(setup unpatched-build "$TMP/good.tar.xz")" +# shellcheck disable=SC2016 # build.sh carries the literal text +printf 'BUILDROOT_PATH=${SDK_SYSDRV_DIR}/source/buildroot/buildroot-2023.02.6\n' > "$d/sdk/project/build.sh" +run "$d" +if [ "$RC" -ne 0 ] && printf '%s' "$OUT" | grep -q 'project/build.sh selects' \ + && [ "$(fetches "$d")" -eq 0 ] && [ ! -e "$(tree "$d")" ]; then + ok "unpatched project/build.sh: refused before fetching" +else + bad "unpatched project/build.sh: refused before fetching (rc=$RC, out=$OUT)" +fi + +# D. sysdrv/Makefile still selects the vendor tree: refuse, fetch nothing. +d="$(setup unpatched-make "$TMP/good.tar.xz")" +printf 'BUILDROOT_VER ?= buildroot-2023.02.6\n' > "$d/sdk/sysdrv/Makefile" +run "$d" +if [ "$RC" -ne 0 ] && printf '%s' "$OUT" | grep -q 'sysdrv/Makefile selects' \ + && [ "$(fetches "$d")" -eq 0 ] && [ ! -e "$(tree "$d")" ]; then + ok "unpatched sysdrv/Makefile: refused before fetching" +else + bad "unpatched sysdrv/Makefile: refused before fetching (rc=$RC, out=$OUT)" +fi + +# E. Tarball without a Buildroot Makefile: refuse, no tree, no scratch. +d="$(setup bad-tarball "$TMP/nomakefile.tar.xz")"; run "$d" +if [ "$RC" -ne 0 ] && printf '%s' "$OUT" | grep -q 'did not contain' \ + && [ ! -e "$(tree "$d")" ] && no_scratch "$d"; then + ok "tarball without Makefile: refused, nothing left behind" +else + bad "tarball without Makefile: refused, nothing left behind (rc=$RC, out=$OUT)" +fi + +# F. A half-present tree (an interrupted hand unpack): refuse, keep it as is. +d="$(setup half-tree "$TMP/good.tar.xz")" +mkdir -p "$(tree "$d")/output"; printf 'keep\n' > "$(tree "$d")/output/marker" +run "$d" +if [ "$RC" -ne 0 ] && printf '%s' "$OUT" | grep -q 'not a complete Buildroot tree' \ + && [ -f "$(tree "$d")/output/marker" ] && [ "$(fetches "$d")" -eq 0 ]; then + ok "half-present tree: refused and left untouched" +else + bad "half-present tree: refused and left untouched (rc=$RC, out=$OUT)" +fi + +# G. Fetch fails (network down, sha256 mismatch): nonzero, no tree. +d="$(setup fetch-fails FAIL)"; run "$d" +if [ "$RC" -ne 0 ] && [ ! -e "$(tree "$d")" ] && no_scratch "$d"; then + ok "failed fetch: nonzero, no tree" +else + bad "failed fetch: nonzero, no tree (rc=$RC, out=$OUT)" +fi + +if [ "$FAIL" -eq 0 ]; then echo "ALL UNPACK-BUILDROOT TESTS PASSED"; else echo "UNPACK-BUILDROOT TESTS FAILED"; fi +exit "$FAIL" -- 2.54.0