msh_file.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-09-25 Bernard the first verion for FinSH
  9. */
  10. #include <rtthread.h>
  11. #if defined(FINSH_USING_MSH) && defined(RT_USING_DFS)
  12. #include <finsh.h>
  13. #include "msh.h"
  14. #include <dfs_posix.h>
  15. static int msh_readline(int fd, char *line_buf, int size)
  16. {
  17. char ch;
  18. int index = 0;
  19. do
  20. {
  21. if (read(fd, &ch, 1) != 1)
  22. {
  23. /* nothing in this file */
  24. return 0;
  25. }
  26. }
  27. while (ch == '\n' || ch == '\r');
  28. /* set the first character */
  29. line_buf[index ++] = ch;
  30. while (index < size)
  31. {
  32. if (read(fd, &ch, 1) == 1)
  33. {
  34. if (ch == '\n' || ch == '\r')
  35. {
  36. line_buf[index] = '\0';
  37. break;
  38. }
  39. line_buf[index++] = ch;
  40. }
  41. else
  42. {
  43. line_buf[index] = '\0';
  44. break;
  45. }
  46. }
  47. return index;
  48. }
  49. int msh_exec_script(const char *cmd_line, int size)
  50. {
  51. int ret;
  52. int fd = -1;
  53. char *pg_name;
  54. int length, cmd_length = 0;
  55. if (size == 0) return -RT_ERROR;
  56. /* get the length of command0 */
  57. while ((cmd_line[cmd_length] != ' ' && cmd_line[cmd_length] != '\t') && cmd_length < size)
  58. cmd_length ++;
  59. /* get name length */
  60. length = cmd_length + 32;
  61. /* allocate program name memory */
  62. pg_name = (char *) rt_malloc(length);
  63. if (pg_name == RT_NULL) return -RT_ENOMEM;
  64. /* copy command0 */
  65. memcpy(pg_name, cmd_line, cmd_length);
  66. pg_name[cmd_length] = '\0';
  67. if (strstr(pg_name, ".sh") != RT_NULL || strstr(pg_name, ".SH") != RT_NULL)
  68. {
  69. /* try to open program */
  70. fd = open(pg_name, O_RDONLY, 0);
  71. /* search in /bin path */
  72. if (fd < 0)
  73. {
  74. rt_snprintf(pg_name, length - 1, "/bin/%.*s", cmd_length, cmd_line);
  75. fd = open(pg_name, O_RDONLY, 0);
  76. }
  77. }
  78. rt_free(pg_name);
  79. if (fd >= 0)
  80. {
  81. /* found script */
  82. char *line_buf;
  83. int length;
  84. line_buf = (char *) rt_malloc(RT_CONSOLEBUF_SIZE);
  85. /* read line by line and then exec it */
  86. do
  87. {
  88. length = msh_readline(fd, line_buf, RT_CONSOLEBUF_SIZE);
  89. if (length > 0)
  90. {
  91. char ch = '\0';
  92. int index;
  93. for (index = 0; index < length; index ++)
  94. {
  95. ch = line_buf[index];
  96. if (ch == ' ' || ch == '\t') continue;
  97. else break;
  98. }
  99. if (ch != '#') /* not a comment */
  100. msh_exec(line_buf, length);
  101. }
  102. }
  103. while (length > 0);
  104. close(fd);
  105. rt_free(line_buf);
  106. ret = 0;
  107. }
  108. else
  109. {
  110. ret = -1;
  111. }
  112. return ret;
  113. }
  114. #endif /* defined(FINSH_USING_MSH) && defined(RT_USING_DFS) */