1
0

msh_file.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * script for RT-Thread module shell
  3. *
  4. * COPYRIGHT (C) 2013-2015, Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * This file is part of RT-Thread (http://www.rt-thread.org)
  7. * Maintainer: bernard.xiong <bernard.xiong at gmail.com>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. *
  25. * Change Logs:
  26. * Date Author Notes
  27. * 2015-09-25 Bernard the first verion for FinSH
  28. */
  29. #include <rtthread.h>
  30. #include <finsh.h>
  31. #include "msh.h"
  32. #if defined(FINSH_USING_MSH) && defined(RT_USING_DFS)
  33. #include <dfs_posix.h>
  34. static int msh_readline(int fd, char *line_buf, int size)
  35. {
  36. char ch;
  37. int index = 0;
  38. do
  39. {
  40. if (read(fd, &ch, 1) != 1)
  41. {
  42. /* nothing in this file */
  43. return 0;
  44. }
  45. }
  46. while (ch == '\n' || ch == '\r');
  47. /* set the first character */
  48. line_buf[index ++] = ch;
  49. while (index < size)
  50. {
  51. if (read(fd, &ch, 1) == 1)
  52. {
  53. if (ch == '\n' || ch == '\r')
  54. {
  55. line_buf[index] = '\0';
  56. break;
  57. }
  58. line_buf[index++] = ch;
  59. }
  60. else
  61. {
  62. line_buf[index] = '\0';
  63. break;
  64. }
  65. }
  66. return index;
  67. }
  68. int msh_exec_script(const char *cmd_line, int size)
  69. {
  70. int ret;
  71. int fd = -1;
  72. char *pg_name;
  73. int length, cmd_length = 0;
  74. if (size == 0) return -RT_ERROR;
  75. /* get the length of command0 */
  76. while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
  77. cmd_length ++;
  78. /* get name length */
  79. length = cmd_length + 32;
  80. /* allocate program name memory */
  81. pg_name = (char *) rt_malloc(length);
  82. if (pg_name == RT_NULL) return -RT_ENOMEM;
  83. /* copy command0 */
  84. memcpy(pg_name, cmd_line, cmd_length);
  85. pg_name[cmd_length] = '\0';
  86. if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL)
  87. {
  88. /* try to open program */
  89. fd = open(pg_name, O_RDONLY, 0);
  90. /* search in /bin path */
  91. if (fd < 0)
  92. {
  93. rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
  94. fd = open(pg_name, O_RDONLY, 0);
  95. }
  96. }
  97. rt_free(pg_name);
  98. if (fd >= 0)
  99. {
  100. /* found script */
  101. char *line_buf;
  102. int length;
  103. line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
  104. /* read line by line and then exec it */
  105. do
  106. {
  107. length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
  108. if (length > 0)
  109. {
  110. char ch = '\0';
  111. int index;
  112. for (index = 0; index < length; index ++)
  113. {
  114. ch = line_buf[index];
  115. if (ch == ' ' || ch == '\t') continue;
  116. else break;
  117. }
  118. if (ch != '#') /* not a comment */
  119. msh_exec(line_buf, length);
  120. }
  121. }
  122. while (length > 0);
  123. close(fd);
  124. rt_free(line_buf);
  125. ret = 0;
  126. }
  127. else
  128. {
  129. ret = -1;
  130. }
  131. return ret;
  132. }
  133. #endif