Changeset 17078 in vbox for trunk/src/VBox/Runtime/common
- Timestamp:
- Feb 24, 2009 4:59:16 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/misc/getopt.cpp
r8927 r17078 53 53 const char *pszArgThis = argv[iThis]; 54 54 55 if (*pszArgThis == '-')55 for (size_t i = 0; i < cOptions; i++) 56 56 { 57 /** @todo implement '--'. */ 58 for (size_t i = 0; i < cOptions; i++) 57 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK)); 58 59 bool fShort = ( *pszArgThis == '-' 60 && (uint32_t)pszArgThis[1] == paOptions[i].uShort 61 ); 62 63 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING) 59 64 { 60 Assert(!(paOptions[i].fFlags & ~RTGETOPT_VALID_MASK)); 61 Assert(paOptions[i].iShort > 0); 65 /* 66 * A value is required with the argument. We're trying to very 67 * understanding here and will permit any of the following: 68 * -svalue, -s:value, -s=value, 69 * -s value, -s: value, -s= value 70 * (Ditto for long options.) 71 */ 72 bool fShort = false; 73 size_t cchLong = 2; 74 if ( ( paOptions[i].pszLong 75 && !strncmp(pszArgThis, paOptions[i].pszLong, (cchLong = strlen(paOptions[i].pszLong))) 76 && ( pszArgThis[cchLong] == '\0' 77 || pszArgThis[cchLong] == ':' 78 || pszArgThis[cchLong] == '=') 79 ) 80 || fShort 81 ) 82 { 83 pValueUnion->pDef = &paOptions[i]; /* in case of error. */ 62 84 63 if ((paOptions[i].fFlags & RTGETOPT_REQ_MASK) != RTGETOPT_REQ_NOTHING)64 {65 85 /* 66 * A value is required with the argument. We're trying to very 67 * understanding here and will permit any of the following: 68 * -svalue, -s:value, -s=value, 69 * -s value, -s: value, -s= value 70 * (Ditto for long options.) 71 */ 72 bool fShort = false; 73 size_t cchLong = 2; 74 if ( ( paOptions[i].pszLong 75 && !strncmp(pszArgThis, paOptions[i].pszLong, (cchLong = strlen(paOptions[i].pszLong))) 76 && ( pszArgThis[cchLong] == '\0' 77 || pszArgThis[cchLong] == ':' 78 || pszArgThis[cchLong] == '=') 79 ) 80 || (fShort = (pszArgThis[1] == paOptions[i].iShort)) 81 ) 86 * Find the argument value 87 */ 88 const char *pszValue; 89 if ( fShort 90 ? pszArgThis[2] == '\0' 91 || ((pszArgThis[2] == ':' || pszArgThis[2] == '=') && pszArgThis[3] == '\0') 92 : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0') 82 93 { 83 pValueUnion->pDef = &paOptions[i]; /* in case of error. */ 94 if (iThis + 1 >= argc) 95 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 96 pszValue = argv[iThis + 1]; 97 (*piThis)++; 98 } 99 else /* same argument. */ 100 pszValue = fShort 101 ? &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')] 102 : &pszArgThis[cchLong + 1]; 84 103 85 /* 86 * Find the argument value 87 */ 88 const char *pszValue; 89 if ( fShort 90 ? pszArgThis[2] == '\0' 91 || ((pszArgThis[2] == ':' || pszArgThis[2] == '=') && pszArgThis[3] == '\0') 92 : pszArgThis[cchLong] == '\0' || pszArgThis[cchLong + 1] == '\0') 93 { 94 if (iThis + 1 >= argc) 95 return VERR_GETOPT_REQUIRED_ARGUMENT_MISSING; 96 pszValue = argv[iThis + 1]; 97 (*piThis)++; 98 } 99 else /* same argument. */ 100 pszValue = fShort 101 ? &pszArgThis[2 + (pszArgThis[2] == ':' || pszArgThis[2] == '=')] 102 : &pszArgThis[cchLong + 1]; 103 104 /* 105 * Transform into a option value as requested. 106 * If decimal conversion fails, we'll check for "0x<xdigit>" and 107 * try a 16 based conversion. We will not interpret any of the 108 * generic ints as octals. 109 */ 110 switch (paOptions[i].fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC)) 111 { 112 case RTGETOPT_REQ_STRING: 113 pValueUnion->psz = pszValue; 114 break; 104 /* 105 * Transform into a option value as requested. 106 * If decimal conversion fails, we'll check for "0x<xdigit>" and 107 * try a 16 based conversion. We will not interpret any of the 108 * generic ints as octals. 109 */ 110 switch (paOptions[i].fFlags & (RTGETOPT_REQ_MASK | RTGETOPT_FLAG_HEX | RTGETOPT_FLAG_OCT | RTGETOPT_FLAG_DEC)) 111 { 112 case RTGETOPT_REQ_STRING: 113 pValueUnion->psz = pszValue; 114 break; 115 115 116 116 #define MY_INT_CASE(req,type,memb,convfn) \ … … 137 137 } 138 138 139 140 141 142 143 144 145 146 139 MY_INT_CASE(RTGETOPT_REQ_INT8, int8_t, i, RTStrToInt8Full) 140 MY_INT_CASE(RTGETOPT_REQ_INT16, int16_t, i, RTStrToInt16Full) 141 MY_INT_CASE(RTGETOPT_REQ_INT32, int32_t, i, RTStrToInt32Full) 142 MY_INT_CASE(RTGETOPT_REQ_INT64, int64_t, i, RTStrToInt64Full) 143 MY_INT_CASE(RTGETOPT_REQ_UINT8, uint8_t, u, RTStrToUInt8Full) 144 MY_INT_CASE(RTGETOPT_REQ_UINT16, uint16_t, u, RTStrToUInt16Full) 145 MY_INT_CASE(RTGETOPT_REQ_UINT32, uint32_t, u, RTStrToUInt32Full) 146 MY_INT_CASE(RTGETOPT_REQ_UINT64, uint64_t, u, RTStrToUInt64Full) 147 147 148 149 150 151 152 153 154 155 148 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_HEX, int8_t, i, RTStrToInt8Full, 16) 149 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_HEX, int16_t, i, RTStrToInt16Full, 16) 150 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_HEX, int32_t, i, RTStrToInt32Full, 16) 151 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_HEX, int64_t, i, RTStrToInt64Full, 16) 152 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_HEX, uint8_t, u, RTStrToUInt8Full, 16) 153 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_HEX, uint16_t, u, RTStrToUInt16Full, 16) 154 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_HEX, uint32_t, u, RTStrToUInt32Full, 16) 155 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_HEX, uint64_t, u, RTStrToUInt64Full, 16) 156 156 157 158 159 160 161 162 163 164 157 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_DEC, int8_t, i, RTStrToInt8Full, 10) 158 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_DEC, int16_t, i, RTStrToInt16Full, 10) 159 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_DEC, int32_t, i, RTStrToInt32Full, 10) 160 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_DEC, int64_t, i, RTStrToInt64Full, 10) 161 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_DEC, uint8_t, u, RTStrToUInt8Full, 10) 162 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_DEC, uint16_t, u, RTStrToUInt16Full, 10) 163 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_DEC, uint32_t, u, RTStrToUInt32Full, 10) 164 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_DEC, uint64_t, u, RTStrToUInt64Full, 10) 165 165 166 167 168 169 170 171 172 173 166 MY_BASE_INT_CASE(RTGETOPT_REQ_INT8 | RTGETOPT_FLAG_OCT, int8_t, i, RTStrToInt8Full, 8) 167 MY_BASE_INT_CASE(RTGETOPT_REQ_INT16 | RTGETOPT_FLAG_OCT, int16_t, i, RTStrToInt16Full, 8) 168 MY_BASE_INT_CASE(RTGETOPT_REQ_INT32 | RTGETOPT_FLAG_OCT, int32_t, i, RTStrToInt32Full, 8) 169 MY_BASE_INT_CASE(RTGETOPT_REQ_INT64 | RTGETOPT_FLAG_OCT, int64_t, i, RTStrToInt64Full, 8) 170 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT8 | RTGETOPT_FLAG_OCT, uint8_t, u, RTStrToUInt8Full, 8) 171 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT16 | RTGETOPT_FLAG_OCT, uint16_t, u, RTStrToUInt16Full, 8) 172 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_OCT, uint32_t, u, RTStrToUInt32Full, 8) 173 MY_BASE_INT_CASE(RTGETOPT_REQ_UINT64 | RTGETOPT_FLAG_OCT, uint64_t, u, RTStrToUInt64Full, 8) 174 174 #undef MY_INT_CASE 175 175 #undef MY_BASE_INT_CASE 176 176 177 default: 178 AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags)); 179 return VERR_INTERNAL_ERROR; 180 } 181 return paOptions[i].iShort; 177 default: 178 AssertMsgFailed(("i=%d f=%#x\n", i, paOptions[i].fFlags)); 179 return VERR_INTERNAL_ERROR; 182 180 } 183 } 184 else if ( ( paOptions[i].pszLong 185 && !strcmp(pszArgThis, paOptions[i].pszLong)) 186 || ( pszArgThis[1] == paOptions[i].iShort 187 && pszArgThis[2] == '\0') /** @todo implement support for ls -lsR like stuff? */ 188 ) 189 { 190 pValueUnion->pDef = &paOptions[i]; 191 return paOptions[i].iShort; 181 return paOptions[i].uShort; 192 182 } 193 183 } 194 195 196 return VERR_GETOPT_UNKNOWN_OPTION; 184 else if ( ( paOptions[i].pszLong 185 && !strcmp(pszArgThis, paOptions[i].pszLong)) 186 || ( fShort 187 && pszArgThis[2] == '\0') /** @todo implement support for ls -lsR like stuff? */ 188 ) 189 { 190 pValueUnion->pDef = &paOptions[i]; 191 return (int)paOptions[i].uShort; 192 } 197 193 } 198 194 199 /* 200 * Not an option. 201 */ 202 (*piThis)--; 203 /** @todo Sort options and arguments (i.e. stuff that doesn't start with '-'), stop when 204 * encountering the first argument. */ 205 206 return 0; 195 return VERR_GETOPT_UNKNOWN_OPTION; 207 196 } 208 197
Note:
See TracChangeset
for help on using the changeset viewer.