MCPcopy Index your code
hub / github.com/RT-Thread/rt-thread / msh_exec_script

Function msh_exec_script

components/finsh/msh_file.c:69–153  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67}
68
69int 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';

Callers 2

msh_execFunction · 0.85
fstab_mnt_initFunction · 0.85

Calls 8

rt_mallocFunction · 0.85
rt_memcpyFunction · 0.85
rt_snprintfFunction · 0.85
rt_freeFunction · 0.85
msh_readlineFunction · 0.85
msh_execFunction · 0.85
openFunction · 0.50
closeFunction · 0.50

Tested by

no test coverage detected