| 67 | } |
| 68 | |
| 69 | int msh_exec_script(const char *cmd_line, int size) |
| 70 | { |
| 71 | int ret; |
| 72 | int fd = -1; |
| 73 | char *pg_name; |
| 74 | int length, cmd_length = 0; |
| 75 | |
| 76 | if (size == 0) return -RT_ERROR; |
| 77 | |
| 78 | /* get the length of command0 */ |
| 79 | while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size) |
| 80 | cmd_length ++; |
| 81 | |
| 82 | /* get name length */ |
| 83 | length = cmd_length + 32; |
| 84 | |
| 85 | /* allocate program name memory */ |
| 86 | pg_name = (char *) rt_malloc(length); |
| 87 | if (pg_name == RT_NULL) return -RT_ENOMEM; |
| 88 | |
| 89 | /* copy command0 */ |
| 90 | rt_memcpy(pg_name, cmd_line, cmd_length); |
| 91 | pg_name[cmd_length] = '\0'; |
| 92 | |
| 93 | if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL) |
| 94 | { |
| 95 | /* try to open program */ |
| 96 | fd = open(pg_name, O_RDONLY, 0); |
| 97 | |
| 98 | /* search in /bin path */ |
| 99 | if (fd < 0) |
| 100 | { |
| 101 | rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line); |
| 102 | fd = open(pg_name, O_RDONLY, 0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | rt_free(pg_name); |
| 107 | if (fd >= 0) |
| 108 | { |
| 109 | /* found script */ |
| 110 | char *line_buf; |
| 111 | int length; |
| 112 | |
| 113 | line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE); |
| 114 | if (line_buf == RT_NULL) |
| 115 | { |
| 116 | close(fd); |
| 117 | return -RT_ENOMEM; |
| 118 | } |
| 119 | |
| 120 | /* read line by line and then exec it */ |
| 121 | do |
| 122 | { |
| 123 | length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE); |
| 124 | if (length > 0) |
| 125 | { |
| 126 | char ch = '\0'; |
no test coverage detected