|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Meta |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +#include <zephyr/posix/sys/utsname.h> |
| 10 | +#include <zephyr/shell/shell.h> |
| 11 | + |
| 12 | +#define UNAME_KERNEL BIT(0) |
| 13 | +#define UNAME_NODE BIT(1) |
| 14 | +#define UNAME_RELEASE BIT(2) |
| 15 | +#define UNAME_VERSION BIT(3) |
| 16 | +#define UNAME_MACHINE BIT(4) |
| 17 | +#define UNAME_PLATFORM BIT(5) |
| 18 | +#define UNAME_UNKNOWN BIT(6) |
| 19 | +#define UNAME_ALL \ |
| 20 | + (UNAME_KERNEL | UNAME_NODE | UNAME_RELEASE | UNAME_VERSION | UNAME_MACHINE | UNAME_PLATFORM) |
| 21 | + |
| 22 | +static void uname_print_usage(const struct shell *sh) |
| 23 | +{ |
| 24 | + shell_print(sh, "usage: uname [-asonrvmpi]"); |
| 25 | +} |
| 26 | + |
| 27 | +static int uname_cmd_handler(const struct shell *sh, size_t argc, char **argv) |
| 28 | +{ |
| 29 | + struct getopt_state *state = getopt_state_get(); |
| 30 | + struct utsname info; |
| 31 | + unsigned int set; |
| 32 | + int option; |
| 33 | + char badarg = 0; |
| 34 | + int ret; |
| 35 | + |
| 36 | + set = 0; |
| 37 | + |
| 38 | + /* Get the uname options */ |
| 39 | + |
| 40 | + optind = 1; |
| 41 | + while ((option = getopt(argc, argv, "asonrvmpi")) != -1) { |
| 42 | + switch (option) { |
| 43 | + case 'a': |
| 44 | + set = UNAME_ALL; |
| 45 | + break; |
| 46 | + |
| 47 | + case 'o': |
| 48 | + case 's': |
| 49 | + set |= UNAME_KERNEL; |
| 50 | + break; |
| 51 | + |
| 52 | + case 'n': |
| 53 | + set |= UNAME_NODE; |
| 54 | + break; |
| 55 | + |
| 56 | + case 'r': |
| 57 | + set |= UNAME_RELEASE; |
| 58 | + break; |
| 59 | + |
| 60 | + case 'v': |
| 61 | + set |= UNAME_VERSION; |
| 62 | + break; |
| 63 | + |
| 64 | + case 'm': |
| 65 | + set |= UNAME_MACHINE; |
| 66 | + break; |
| 67 | + |
| 68 | + case 'p': |
| 69 | + if (set != UNAME_ALL) { |
| 70 | + set |= UNAME_UNKNOWN; |
| 71 | + } |
| 72 | + break; |
| 73 | + |
| 74 | + case 'i': |
| 75 | + set |= UNAME_PLATFORM; |
| 76 | + break; |
| 77 | + |
| 78 | + case '?': |
| 79 | + default: |
| 80 | + badarg = (char)state->optopt; |
| 81 | + break; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (argc != optind) { |
| 86 | + shell_error(sh, "extra operand %s", argv[optind]); |
| 87 | + uname_print_usage(sh); |
| 88 | + return -1; |
| 89 | + } |
| 90 | + |
| 91 | + /* If a bad argument was encountered, then return without processing the |
| 92 | + * command |
| 93 | + */ |
| 94 | + |
| 95 | + if (badarg != 0) { |
| 96 | + shell_error(sh, "uname: illegal option -- %c", badarg); |
| 97 | + uname_print_usage(sh); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + |
| 101 | + /* If nothing is provided on the command line, the default is -s */ |
| 102 | + |
| 103 | + if (set == 0) { |
| 104 | + set = UNAME_KERNEL; |
| 105 | + } |
| 106 | + |
| 107 | + /* Get uname data */ |
| 108 | + |
| 109 | + ret = uname(&info); |
| 110 | + if (ret < 0) { |
| 111 | + shell_error(sh, "cannot get system name"); |
| 112 | + return -1; |
| 113 | + } |
| 114 | + |
| 115 | + /* Process each option */ |
| 116 | + |
| 117 | + /* print the kernel/operating system name */ |
| 118 | + if (set & UNAME_KERNEL) { |
| 119 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", info.sysname); |
| 120 | + } |
| 121 | + |
| 122 | + /* Print nodename */ |
| 123 | + if (set & UNAME_NODE) { |
| 124 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", info.nodename); |
| 125 | + } |
| 126 | + |
| 127 | + /* Print the kernel release */ |
| 128 | + if (set & UNAME_RELEASE) { |
| 129 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", info.release); |
| 130 | + } |
| 131 | + |
| 132 | + /* Print the kernel version */ |
| 133 | + if (set & UNAME_VERSION) { |
| 134 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", info.version); |
| 135 | + } |
| 136 | + |
| 137 | + /* Print the machine hardware name */ |
| 138 | + if (set & UNAME_MACHINE) { |
| 139 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", info.machine); |
| 140 | + } |
| 141 | + |
| 142 | + /* Print the machine platform name */ |
| 143 | + if (set & UNAME_PLATFORM) { |
| 144 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", CONFIG_BOARD); |
| 145 | + } |
| 146 | + |
| 147 | + /* Print "unknown" */ |
| 148 | + if (set & UNAME_UNKNOWN) { |
| 149 | + shell_fprintf(sh, SHELL_NORMAL, "%s ", "unknown"); |
| 150 | + } |
| 151 | + |
| 152 | + shell_fprintf(sh, SHELL_NORMAL, "\n"); |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
| 156 | + |
| 157 | +SHELL_CMD_REGISTER(uname, NULL, NULL, uname_cmd_handler); |
0 commit comments