Changeset 96729 in vbox for trunk/src/VBox/Runtime/common/misc
- Timestamp:
- Sep 14, 2022 9:14:53 AM (2 years ago)
- svn:sync-xref-src-repo-rev:
- 153615
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/getopt.cpp
r96407 r96729 102 102 { 103 103 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK)); 104 Assert( !(paOptions[i].fFlags & (RTGETOPT_FLAG_INDEX_DEF_MASK | RTGETOPT_FLAG_INDEX_DEF_DASH)) 105 || (paOptions[i].fFlags & RTGETOPT_FLAG_INDEX) ); 104 106 Assert(paOptions[i].iShort > 0); 105 107 Assert(paOptions[i].iShort != VINF_GETOPT_NOT_OPTION); … … 175 177 if (pOpt->pszLong) 176 178 { 177 if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) 179 uint32_t const fOptFlags = pOpt->fFlags; 180 if ((fOptFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) 178 181 { 179 182 /* … … 189 192 size_t cchLong = strlen(pOpt->pszLong); 190 193 if ( !strncmp(pszOption, pOpt->pszLong, cchLong) 191 || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE194 || ( (fOptFlags & RTGETOPT_FLAG_ICASE) 192 195 && !RTStrNICmp(pszOption, pOpt->pszLong, cchLong))) 193 196 { 194 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 197 if ( (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH) 198 && pszOption[cchLong] == '-' 199 && RT_C_IS_DIGIT(pszOption[cchLong + 1])) /* given "--long" we match "--long-1" but not "--long-". */ 200 cchLong++; 201 if (fOptFlags & RTGETOPT_FLAG_INDEX) 195 202 while (RT_C_IS_DIGIT(pszOption[cchLong])) 196 203 cchLong++; … … 201 208 } 202 209 } 203 else if ( pOpt->fFlags & RTGETOPT_FLAG_INDEX)210 else if (fOptFlags & RTGETOPT_FLAG_INDEX) 204 211 { 205 212 /* … … 209 216 size_t cchLong = strlen(pOpt->pszLong); 210 217 if ( !strncmp(pszOption, pOpt->pszLong, cchLong) 211 || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE218 || ( (fOptFlags & RTGETOPT_FLAG_ICASE) 212 219 && !RTStrNICmp(pszOption, pOpt->pszLong, cchLong))) 213 220 { 221 if ( (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH) 222 && pszOption[cchLong] == '-' 223 && RT_C_IS_DIGIT(pszOption[cchLong + 1])) 224 cchLong++; 214 225 while (RT_C_IS_DIGIT(pszOption[cchLong])) 215 226 cchLong++; … … 219 230 } 220 231 else if ( !strcmp(pszOption, pOpt->pszLong) 221 || ( pOpt->fFlags & RTGETOPT_FLAG_ICASE232 || ( (fOptFlags & RTGETOPT_FLAG_ICASE) 222 233 && !RTStrICmp(pszOption, pOpt->pszLong))) 223 234 return pOpt; … … 660 671 pValueUnion->pDef = pOpt; /* in case of no value or error. */ 661 672 662 if ((pOpt->fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) 673 uint32_t const fOptFlags = pOpt->fFlags; 674 if ((fOptFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) 663 675 { 664 676 /* … … 692 704 { 693 705 size_t cchLong = strlen(pOpt->pszLong); 694 if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 695 { 696 697 if (pszArgThis[cchLong] == '\0') 698 return VERR_GETOPT_INDEX_MISSING; 699 700 uint32_t uIndex; 701 char *pszRet = NULL; 702 int rc = RTStrToUInt32Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex); 703 if (rc == VWRN_TRAILING_CHARS) 706 if (fOptFlags & RTGETOPT_FLAG_INDEX) 707 { 708 if ( pszArgThis[cchLong] != '\0' 709 || (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK)) 704 710 { 705 if ( pszRet[0] != ':' 706 && pszRet[0] != '=') 707 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; 708 pState->uIndex = uIndex; 709 pszValue = pszRet + 1; 710 } 711 else if (rc == VINF_SUCCESS) 712 { 713 if (iThis + 1 + pState->cNonOptions >= pState->argc) 714 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 715 pState->uIndex = uIndex; 716 pszValue = pState->argv[iThis + pState->cNonOptions + 1]; 717 rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]); 718 pState->iNext++; 711 if ( (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH) 712 && pszArgThis[cchLong] == '-') 713 cchLong++; 714 715 uint32_t uIndex; 716 char *pszRet = NULL; 717 int rc = RTStrToUInt32Ex(&pszArgThis[cchLong], &pszRet, 10, &uIndex); 718 if ( rc == VERR_NO_DIGITS 719 && (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK)) 720 { 721 uIndex = ((fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK) >> RTGETOPT_FLAG_INDEX_DEF_SHIFT) - 1; 722 rc = pszRet[0] == '\0' ? VINF_SUCCESS : VWRN_TRAILING_CHARS; 723 } 724 if (rc == VWRN_TRAILING_CHARS) 725 { 726 if ( pszRet[0] != ':' 727 && pszRet[0] != '=') 728 return VERR_GETOPT_INVALID_ARGUMENT_FORMAT; 729 pState->uIndex = uIndex; 730 pszValue = pszRet + 1; 731 } 732 else if (rc == VINF_SUCCESS) 733 { 734 if (iThis + 1 + pState->cNonOptions >= pState->argc) 735 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 736 pState->uIndex = uIndex; 737 pszValue = pState->argv[iThis + pState->cNonOptions + 1]; 738 rtGetOptMoveArgvEntries(&pState->argv[iThis + 1], &pState->argv[iThis + pState->cNonOptions + 1]); 739 pState->iNext++; 740 } 741 else 742 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */ 719 743 } 720 744 else 721 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */745 return VERR_GETOPT_INDEX_MISSING; 722 746 } 723 747 else … … 739 763 * Set up the ValueUnion. 740 764 */ 741 int rc = rtGetOptProcessValue( pOpt->fFlags, pszValue, pValueUnion);765 int rc = rtGetOptProcessValue(fOptFlags, pszValue, pValueUnion); 742 766 if (RT_FAILURE(rc)) 743 767 return rc; … … 765 789 } 766 790 } 767 else if (pOpt->fFlags & RTGETOPT_FLAG_INDEX) 768 { 769 size_t cchLong = strlen(pOpt->pszLong); 770 if (pszArgThis[cchLong] == '\0') 791 else if (fOptFlags & RTGETOPT_FLAG_INDEX) 792 { 793 size_t cchLong = strlen(pOpt->pszLong); 794 uint32_t uIndex; 795 if (pszArgThis[cchLong] != '\0') 796 { 797 if ( (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_DASH) 798 && pszArgThis[cchLong] == '-') 799 cchLong++; 800 if (RTStrToUInt32Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS) 801 pState->uIndex = uIndex; 802 else 803 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */ 804 } 805 else if (fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK) 806 uIndex = ((fOptFlags & RTGETOPT_FLAG_INDEX_DEF_MASK) >> RTGETOPT_FLAG_INDEX_DEF_SHIFT) - 1; 807 else 771 808 return VERR_GETOPT_INDEX_MISSING; 772 773 uint32_t uIndex;774 if (RTStrToUInt32Full(&pszArgThis[cchLong], 10, &uIndex) == VINF_SUCCESS)775 pState->uIndex = uIndex;776 else777 AssertMsgFailedReturn(("%s\n", pszArgThis), VERR_GETOPT_INVALID_ARGUMENT_FORMAT); /* search bug */778 809 } 779 810
Note:
See TracChangeset
for help on using the changeset viewer.