| 390 | } |
| 391 | |
| 392 | BAN::ErrorOr<void> Execute::source_script(BAN::StringView path) |
| 393 | { |
| 394 | BAN::Vector<BAN::String> script_lines; |
| 395 | |
| 396 | { |
| 397 | FILE* fp = fopen(path.data(), "r"); |
| 398 | if (fp == nullptr) |
| 399 | return BAN::Error::from_errno(errno); |
| 400 | |
| 401 | BAN::String current; |
| 402 | char temp_buffer[128]; |
| 403 | while (fgets(temp_buffer, sizeof(temp_buffer), fp)) |
| 404 | { |
| 405 | TRY(current.append(temp_buffer)); |
| 406 | if (current.back() != '\n') |
| 407 | continue; |
| 408 | current.pop_back(); |
| 409 | |
| 410 | if (!current.empty()) |
| 411 | TRY(script_lines.push_back(BAN::move(current))); |
| 412 | current.clear(); |
| 413 | } |
| 414 | |
| 415 | if (!current.empty()) |
| 416 | TRY(script_lines.push_back(BAN::move(current))); |
| 417 | |
| 418 | fclose(fp); |
| 419 | } |
| 420 | |
| 421 | size_t index = 0; |
| 422 | TokenParser parser( |
| 423 | [&](BAN::Optional<BAN::StringView>) -> BAN::Optional<BAN::String> |
| 424 | { |
| 425 | if (index >= script_lines.size()) |
| 426 | return {}; |
| 427 | return script_lines[index++]; |
| 428 | } |
| 429 | ); |
| 430 | if (!parser.main_loop(true)) |
| 431 | return BAN::Error::from_literal("oop"); |
| 432 | return {}; |
| 433 | } |