| 635 | */ |
| 636 | |
| 637 | static int32_t raw_command_length( char const * command ) |
| 638 | { |
| 639 | char const * p; |
| 640 | char const * escape = 0; |
| 641 | char inquote = 0; |
| 642 | char const * newline = 0; |
| 643 | |
| 644 | /* Skip leading whitespace. */ |
| 645 | while ( isspace( *command ) ) |
| 646 | ++command; |
| 647 | |
| 648 | p = command; |
| 649 | |
| 650 | /* Look for newlines and unquoted I/O redirection. */ |
| 651 | do |
| 652 | { |
| 653 | p += strcspn( p, "\n\"'<>|\\" ); |
| 654 | switch ( *p ) |
| 655 | { |
| 656 | case '\n': |
| 657 | /* If our command contains non-whitespace content split over |
| 658 | * multiple lines we can not execute it directly. |
| 659 | */ |
| 660 | newline = p; |
| 661 | while ( isspace( *++p ) ); |
| 662 | if ( *p ) return -1; |
| 663 | break; |
| 664 | |
| 665 | case '\\': |
| 666 | escape = escape && escape == p - 1 ? 0 : p; |
| 667 | ++p; |
| 668 | break; |
| 669 | |
| 670 | case '"': |
| 671 | case '\'': |
| 672 | if ( escape && escape == p - 1 ) |
| 673 | escape = 0; |
| 674 | else if ( inquote == *p ) |
| 675 | inquote = 0; |
| 676 | else if ( !inquote ) |
| 677 | inquote = *p; |
| 678 | ++p; |
| 679 | break; |
| 680 | |
| 681 | case '<': |
| 682 | case '>': |
| 683 | case '|': |
| 684 | if ( !inquote ) |
| 685 | return -1; |
| 686 | ++p; |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | while ( *p ); |
| 691 | |
| 692 | /* Return the number of characters the command will occupy. */ |
| 693 | return int32_t(( newline ? newline : p ) - command); |
| 694 | } |
no test coverage detected