Simon Glass | 3035497 | 2014-04-10 20:01:29 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * (C) Copyright 2000 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * Add to readline cmdline-editing by |
| 6 | * (C) Copyright 2005 |
| 7 | * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <cli.h> |
| 14 | #include <cli_hush.h> |
| 15 | #include <malloc.h> |
| 16 | |
| 17 | /* |
| 18 | * Run a command using the selected parser. |
| 19 | * |
| 20 | * @param cmd Command to run |
| 21 | * @param flag Execution flags (CMD_FLAG_...) |
| 22 | * @return 0 on success, or != 0 on error. |
| 23 | */ |
| 24 | int run_command(const char *cmd, int flag) |
| 25 | { |
| 26 | #ifndef CONFIG_SYS_HUSH_PARSER |
| 27 | /* |
| 28 | * cli_run_command can return 0 or 1 for success, so clean up |
| 29 | * its result. |
| 30 | */ |
| 31 | if (cli_simple_run_command(cmd, flag) == -1) |
| 32 | return 1; |
| 33 | |
| 34 | return 0; |
| 35 | #else |
| 36 | return parse_string_outer(cmd, |
| 37 | FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP); |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | int run_command_list(const char *cmd, int len, int flag) |
| 42 | { |
| 43 | int need_buff = 1; |
| 44 | char *buff = (char *)cmd; /* cast away const */ |
| 45 | int rcode = 0; |
| 46 | |
| 47 | if (len == -1) { |
| 48 | len = strlen(cmd); |
| 49 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 50 | /* hush will never change our string */ |
| 51 | need_buff = 0; |
| 52 | #else |
| 53 | /* the built-in parser will change our string if it sees \n */ |
| 54 | need_buff = strchr(cmd, '\n') != NULL; |
| 55 | #endif |
| 56 | } |
| 57 | if (need_buff) { |
| 58 | buff = malloc(len + 1); |
| 59 | if (!buff) |
| 60 | return 1; |
| 61 | memcpy(buff, cmd, len); |
| 62 | buff[len] = '\0'; |
| 63 | } |
| 64 | #ifdef CONFIG_SYS_HUSH_PARSER |
| 65 | rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON); |
| 66 | #else |
| 67 | /* |
| 68 | * This function will overwrite any \n it sees with a \0, which |
| 69 | * is why it can't work with a const char *. Here we are making |
| 70 | * using of internal knowledge of this function, to avoid always |
| 71 | * doing a malloc() which is actually required only in a case that |
| 72 | * is pretty rare. |
| 73 | */ |
| 74 | rcode = cli_simple_run_command_list(buff, flag); |
| 75 | if (need_buff) |
| 76 | free(buff); |
| 77 | #endif |
| 78 | |
| 79 | return rcode; |
| 80 | } |
| 81 | |
| 82 | /****************************************************************************/ |
| 83 | |
| 84 | #if defined(CONFIG_CMD_RUN) |
| 85 | int do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 86 | { |
| 87 | int i; |
| 88 | |
| 89 | if (argc < 2) |
| 90 | return CMD_RET_USAGE; |
| 91 | |
| 92 | for (i = 1; i < argc; ++i) { |
| 93 | char *arg; |
| 94 | |
| 95 | arg = getenv(argv[i]); |
| 96 | if (arg == NULL) { |
| 97 | printf("## Error: \"%s\" not defined\n", argv[i]); |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | if (run_command(arg, flag) != 0) |
| 102 | return 1; |
| 103 | } |
| 104 | return 0; |
| 105 | } |
| 106 | #endif |