* @brief Load and process interpreter script for light-weight process * * @param[in,out] ai Pointer to the lwp_args_info structure to store script arguments * @param[in] filename Path to the script file to load * * @return rt_err_t Returns RT_EOK (0) on success, negative error code on failure: * - -1: General error (file open/read failure) * - Other errors from args_init() o
| 709 | * It handles script verification, argument parsing, and proper cleanup on failure. |
| 710 | */ |
| 711 | rt_err_t lwp_args_load_script(struct lwp_args_info *ai, const char *filename) |
| 712 | { |
| 713 | rt_err_t error = -1; |
| 714 | int fd = -RT_ERROR; |
| 715 | int len; |
| 716 | int rf_stat; |
| 717 | char interp[INTERP_BUF_SIZE]; |
| 718 | char *cp, *nextword; |
| 719 | char script_magic[2]; |
| 720 | struct lwp_args_info ow_ai = {0}; |
| 721 | |
| 722 | fd = open(filename, O_BINARY | O_RDONLY, 0); |
| 723 | if (fd < 0) |
| 724 | { |
| 725 | goto quit; |
| 726 | } |
| 727 | |
| 728 | /** |
| 729 | * verify an interpreter script by matching script file magic |
| 730 | * eg: #!/bin/sh |
| 731 | */ |
| 732 | len = read(fd, script_magic, sizeof(script_magic)); |
| 733 | if (len != 2 || memcmp(script_magic, "#!", sizeof(script_magic))) |
| 734 | { |
| 735 | goto quit; |
| 736 | } |
| 737 | |
| 738 | /* setup a new args struct to save script arguments */ |
| 739 | if (args_init(&ow_ai, INTERP_BUF_SIZE)) |
| 740 | { |
| 741 | goto quit; |
| 742 | } |
| 743 | |
| 744 | while (1) |
| 745 | { |
| 746 | /* read file to buffer (avoid any truncated words in buffer) */ |
| 747 | rf_stat = _readfile(fd, INTERP_BUF_SIZE, interp, &len); |
| 748 | if (len <= 0) |
| 749 | { |
| 750 | goto quit; |
| 751 | } |
| 752 | |
| 753 | /* find first word until reaching nil */ |
| 754 | cp = _find_word(interp); |
| 755 | if (*cp == '\0') |
| 756 | { |
| 757 | if (READFILE_STAT_CAN_READMORE(rf_stat)) |
| 758 | continue; |
| 759 | else |
| 760 | break; |
| 761 | } |
| 762 | |
| 763 | do |
| 764 | { |
| 765 | nextword = _seperate_and_get_nextword(cp); |
| 766 | args_append(&ow_ai, cp, strlen(cp), LWP_ARGS_TYPE_KARG); |
| 767 | cp = nextword; |
| 768 | } |
no test coverage detected