| 1 |
/* |
| 2 |
* tclIndexObj.c -- |
| 3 |
* |
| 4 |
* This file implements objects of type "index". This object type is used |
| 5 |
* to lookup a keyword in a table of valid values and cache the index of |
| 6 |
* the matching entry. Also provides table-based argv/argc processing. |
| 7 |
* |
| 8 |
* Copyright (c) 1990-1994 The Regents of the University of California. |
| 9 |
* Copyright (c) 1997 Sun Microsystems, Inc. |
| 10 |
* Copyright (c) 2006 Sam Bromley. |
| 11 |
* |
| 12 |
* See the file "license.terms" for information on usage and redistribution of |
| 13 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 14 |
*/ |
| 15 |
|
| 16 |
#include "tclInt.h" |
| 17 |
|
| 18 |
/* |
| 19 |
* Prototypes for functions defined later in this file: |
| 20 |
*/ |
| 21 |
|
| 22 |
static int GetIndexFromObjList(Tcl_Interp *interp, |
| 23 |
Tcl_Obj *objPtr, Tcl_Obj *tableObjPtr, |
| 24 |
const char *msg, int flags, int *indexPtr); |
| 25 |
static void UpdateStringOfIndex(Tcl_Obj *objPtr); |
| 26 |
static void DupIndex(Tcl_Obj *srcPtr, Tcl_Obj *dupPtr); |
| 27 |
static void FreeIndex(Tcl_Obj *objPtr); |
| 28 |
static Tcl_ObjCmdProc PrefixAllObjCmd; |
| 29 |
static Tcl_ObjCmdProc PrefixLongestObjCmd; |
| 30 |
static Tcl_ObjCmdProc PrefixMatchObjCmd; |
| 31 |
static void PrintUsage(Tcl_Interp *interp, |
| 32 |
const Tcl_ArgvInfo *argTable); |
| 33 |
|
| 34 |
/* |
| 35 |
* The structure below defines the index Tcl object type by means of functions |
| 36 |
* that can be invoked by generic object code. |
| 37 |
*/ |
| 38 |
|
| 39 |
static const Tcl_ObjType indexType = { |
| 40 |
"index", /* name */ |
| 41 |
FreeIndex, /* freeIntRepProc */ |
| 42 |
DupIndex, /* dupIntRepProc */ |
| 43 |
UpdateStringOfIndex, /* updateStringProc */ |
| 44 |
NULL /* setFromAnyProc */ |
| 45 |
}; |
| 46 |
|
| 47 |
/* |
| 48 |
* The definition of the internal representation of the "index" object; The |
| 49 |
* internalRep.twoPtrValue.ptr1 field of an object of "index" type will be a |
| 50 |
* pointer to one of these structures. |
| 51 |
* |
| 52 |
* Keep this structure declaration in sync with tclTestObj.c |
| 53 |
*/ |
| 54 |
|
| 55 |
typedef struct { |
| 56 |
void *tablePtr; /* Pointer to the table of strings */ |
| 57 |
int offset; /* Offset between table entries */ |
| 58 |
int index; /* Selected index into table. */ |
| 59 |
} IndexRep; |
| 60 |
|
| 61 |
/* |
| 62 |
* The following macros greatly simplify moving through a table... |
| 63 |
*/ |
| 64 |
|
| 65 |
#define STRING_AT(table, offset) \ |
| 66 |
(*((const char *const *)(((char *)(table)) + (offset)))) |
| 67 |
#define NEXT_ENTRY(table, offset) \ |
| 68 |
(&(STRING_AT(table, offset))) |
| 69 |
#define EXPAND_OF(indexRep) \ |
| 70 |
(((indexRep)->index >= 0) ? STRING_AT((indexRep)->tablePtr, (indexRep)->offset*(indexRep)->index) : "") |
| 71 |
|
| 72 |
/* |
| 73 |
*---------------------------------------------------------------------- |
| 74 |
* |
| 75 |
* Tcl_GetIndexFromObj -- |
| 76 |
* |
| 77 |
* This function looks up an object's value in a table of strings and |
| 78 |
* returns the index of the matching string, if any. |
| 79 |
* |
| 80 |
* Results: |
| 81 |
* If the value of objPtr is identical to or a unique abbreviation for |
| 82 |
* one of the entries in tablePtr, then the return value is TCL_OK and the |
| 83 |
* index of the matching entry is stored at *indexPtr. If there isn't a |
| 84 |
* proper match, then TCL_ERROR is returned and an error message is left |
| 85 |
* in interp's result (unless interp is NULL). The msg argument is used |
| 86 |
* in the error message; for example, if msg has the value "option" then |
| 87 |
* the error message will say something flag 'bad option "foo": must be |
| 88 |
* ...' |
| 89 |
* |
| 90 |
* Side effects: |
| 91 |
* The result of the lookup is cached as the internal rep of objPtr, so |
| 92 |
* that repeated lookups can be done quickly. |
| 93 |
* |
| 94 |
*---------------------------------------------------------------------- |
| 95 |
*/ |
| 96 |
|
| 97 |
#undef Tcl_GetIndexFromObj |
| 98 |
int |
| 99 |
Tcl_GetIndexFromObj( |
| 100 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 101 |
Tcl_Obj *objPtr, /* Object containing the string to lookup. */ |
| 102 |
const char *const *tablePtr, /* Array of strings to compare against the |
| 103 |
* value of objPtr; last entry must be NULL |
| 104 |
* and there must not be duplicate entries. */ |
| 105 |
const char *msg, /* Identifying word to use in error |
| 106 |
* messages. */ |
| 107 |
int flags, /* 0 or TCL_EXACT */ |
| 108 |
int *indexPtr) /* Place to store resulting integer index. */ |
| 109 |
{ |
| 110 |
|
| 111 |
/* |
| 112 |
* See if there is a valid cached result from a previous lookup (doing the |
| 113 |
* check here saves the overhead of calling Tcl_GetIndexFromObjStruct in |
| 114 |
* the common case where the result is cached). |
| 115 |
*/ |
| 116 |
|
| 117 |
if (objPtr->typePtr == &indexType) { |
| 118 |
IndexRep *indexRep = (IndexRep *)objPtr->internalRep.twoPtrValue.ptr1; |
| 119 |
|
| 120 |
/* |
| 121 |
* Here's hoping we don't get hit by unfortunate packing constraints |
| 122 |
* on odd platforms like a Cray PVP... |
| 123 |
*/ |
| 124 |
|
| 125 |
if (indexRep->tablePtr == (void *)tablePtr |
| 126 |
&& indexRep->offset == sizeof(char *)) { |
| 127 |
*indexPtr = indexRep->index; |
| 128 |
return TCL_OK; |
| 129 |
} |
| 130 |
} |
| 131 |
return Tcl_GetIndexFromObjStruct(interp, objPtr, tablePtr, sizeof(char *), |
| 132 |
msg, flags, indexPtr); |
| 133 |
} |
| 134 |
|
| 135 |
/* |
| 136 |
*---------------------------------------------------------------------- |
| 137 |
* |
| 138 |
* GetIndexFromObjList -- |
| 139 |
* |
| 140 |
* This procedure looks up an object's value in a table of strings and |
| 141 |
* returns the index of the matching string, if any. |
| 142 |
* |
| 143 |
* Results: |
| 144 |
* If the value of objPtr is identical to or a unique abbreviation for |
| 145 |
* one of the entries in tableObjPtr, then the return value is TCL_OK and |
| 146 |
* the index of the matching entry is stored at *indexPtr. If there isn't |
| 147 |
* a proper match, then TCL_ERROR is returned and an error message is |
| 148 |
* left in interp's result (unless interp is NULL). The msg argument is |
| 149 |
* used in the error message; for example, if msg has the value "option" |
| 150 |
* then the error message will say something flag 'bad option "foo": must |
| 151 |
* be ...' |
| 152 |
* |
| 153 |
* Side effects: |
| 154 |
* Removes any internal representation that the object might have. (TODO: |
| 155 |
* find a way to cache the lookup.) |
| 156 |
* |
| 157 |
*---------------------------------------------------------------------- |
| 158 |
*/ |
| 159 |
|
| 160 |
int |
| 161 |
GetIndexFromObjList( |
| 162 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 163 |
Tcl_Obj *objPtr, /* Object containing the string to lookup. */ |
| 164 |
Tcl_Obj *tableObjPtr, /* List of strings to compare against the |
| 165 |
* value of objPtr. */ |
| 166 |
const char *msg, /* Identifying word to use in error |
| 167 |
* messages. */ |
| 168 |
int flags, /* 0 or TCL_EXACT */ |
| 169 |
int *indexPtr) /* Place to store resulting index. */ |
| 170 |
{ |
| 171 |
|
| 172 |
int objc, t; |
| 173 |
int result; |
| 174 |
Tcl_Obj **objv; |
| 175 |
const char **tablePtr; |
| 176 |
|
| 177 |
/* |
| 178 |
* Use Tcl_GetIndexFromObjStruct to do the work to avoid duplicating most |
| 179 |
* of the code there. This is a bit inefficient but simpler. |
| 180 |
*/ |
| 181 |
|
| 182 |
result = TclListObjGetElements(interp, tableObjPtr, &objc, &objv); |
| 183 |
if (result != TCL_OK) { |
| 184 |
return result; |
| 185 |
} |
| 186 |
|
| 187 |
/* |
| 188 |
* Build a string table from the list. |
| 189 |
*/ |
| 190 |
|
| 191 |
tablePtr = (const char **)ckalloc((objc + 1) * sizeof(char *)); |
| 192 |
for (t = 0; t < objc; t++) { |
| 193 |
if (objv[t] == objPtr) { |
| 194 |
/* |
| 195 |
* An exact match is always chosen, so we can stop here. |
| 196 |
*/ |
| 197 |
|
| 198 |
ckfree(tablePtr); |
| 199 |
*indexPtr = t; |
| 200 |
return TCL_OK; |
| 201 |
} |
| 202 |
|
| 203 |
tablePtr[t] = TclGetString(objv[t]); |
| 204 |
} |
| 205 |
tablePtr[objc] = NULL; |
| 206 |
|
| 207 |
result = Tcl_GetIndexFromObjStruct(interp, objPtr, tablePtr, |
| 208 |
sizeof(char *), msg, flags, indexPtr); |
| 209 |
|
| 210 |
/* |
| 211 |
* The internal rep must be cleared since tablePtr will go away. |
| 212 |
*/ |
| 213 |
|
| 214 |
TclFreeIntRep(objPtr); |
| 215 |
ckfree(tablePtr); |
| 216 |
|
| 217 |
return result; |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
*---------------------------------------------------------------------- |
| 222 |
* |
| 223 |
* Tcl_GetIndexFromObjStruct -- |
| 224 |
* |
| 225 |
* This function looks up an object's value given a starting string and |
| 226 |
* an offset for the amount of space between strings. This is useful when |
| 227 |
* the strings are embedded in some other kind of array. |
| 228 |
* |
| 229 |
* Results: |
| 230 |
* If the value of objPtr is identical to or a unique abbreviation for |
| 231 |
* one of the entries in tablePtr, then the return value is TCL_OK and |
| 232 |
* the index of the matching entry is stored at *indexPtr. If there isn't |
| 233 |
* a proper match, then TCL_ERROR is returned and an error message is |
| 234 |
* left in interp's result (unless interp is NULL). The msg argument is |
| 235 |
* used in the error message; for example, if msg has the value "option" |
| 236 |
* then the error message will say something like 'bad option "foo": must |
| 237 |
* be ...' |
| 238 |
* |
| 239 |
* Side effects: |
| 240 |
* The result of the lookup is cached as the internal rep of objPtr, so |
| 241 |
* that repeated lookups can be done quickly. |
| 242 |
* |
| 243 |
*---------------------------------------------------------------------- |
| 244 |
*/ |
| 245 |
|
| 246 |
int |
| 247 |
Tcl_GetIndexFromObjStruct( |
| 248 |
Tcl_Interp *interp, /* Used for error reporting if not NULL. */ |
| 249 |
Tcl_Obj *objPtr, /* Object containing the string to lookup. */ |
| 250 |
const void *tablePtr, /* The first string in the table. The second |
| 251 |
* string will be at this address plus the |
| 252 |
* offset, the third plus the offset again, |
| 253 |
* etc. The last entry must be NULL and there |
| 254 |
* must not be duplicate entries. */ |
| 255 |
int offset, /* The number of bytes between entries */ |
| 256 |
const char *msg, /* Identifying word to use in error |
| 257 |
* messages. */ |
| 258 |
int flags, /* 0 or TCL_EXACT */ |
| 259 |
int *indexPtr) /* Place to store resulting integer index. */ |
| 260 |
{ |
| 261 |
int index, idx, numAbbrev; |
| 262 |
const char *key, *p1; |
| 263 |
const char *p2; |
| 264 |
const char *const *entryPtr; |
| 265 |
Tcl_Obj *resultPtr; |
| 266 |
IndexRep *indexRep; |
| 267 |
|
| 268 |
/* Protect against invalid values, like -1 or 0. */ |
| 269 |
if (offset < (int)sizeof(char *)) { |
| 270 |
offset = (int)sizeof(char *); |
| 271 |
} |
| 272 |
/* |
| 273 |
* See if there is a valid cached result from a previous lookup. |
| 274 |
*/ |
| 275 |
|
| 276 |
if (objPtr && (objPtr->typePtr == &indexType)) { |
| 277 |
indexRep = (IndexRep *)objPtr->internalRep.twoPtrValue.ptr1; |
| 278 |
if ((indexRep->tablePtr == tablePtr) |
| 279 |
&& (indexRep->offset == offset) |
| 280 |
&& (indexRep->index >= 0)) { |
| 281 |
*indexPtr = indexRep->index; |
| 282 |
return TCL_OK; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/* |
| 287 |
* Lookup the value of the object in the table. Accept unique |
| 288 |
* abbreviations unless TCL_EXACT is set in flags. |
| 289 |
*/ |
| 290 |
|
| 291 |
key = objPtr ? TclGetString(objPtr) : ""; |
| 292 |
index = -1; |
| 293 |
numAbbrev = 0; |
| 294 |
|
| 295 |
/* |
| 296 |
* Scan the table looking for one of: |
| 297 |
* - An exact match (always preferred) |
| 298 |
* - A single abbreviation (allowed depending on flags) |
| 299 |
* - Several abbreviations (never allowed, but overridden by exact match) |
| 300 |
*/ |
| 301 |
|
| 302 |
for (entryPtr = (const char *const *)tablePtr, idx = 0; *entryPtr != NULL; |
| 303 |
entryPtr = NEXT_ENTRY(entryPtr, offset), idx++) { |
| 304 |
for (p1 = key, p2 = *entryPtr; *p1 == *p2; p1++, p2++) { |
| 305 |
if (*p1 == '\0') { |
| 306 |
index = idx; |
| 307 |
goto done; |
| 308 |
} |
| 309 |
} |
| 310 |
if (*p1 == '\0') { |
| 311 |
/* |
| 312 |
* The value is an abbreviation for this entry. Continue checking |
| 313 |
* other entries to make sure it's unique. If we get more than one |
| 314 |
* unique abbreviation, keep searching to see if there is an exact |
| 315 |
* match, but remember the number of unique abbreviations and |
| 316 |
* don't allow either. |
| 317 |
*/ |
| 318 |
|
| 319 |
numAbbrev++; |
| 320 |
index = idx; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* Check if we were instructed to disallow abbreviations. |
| 326 |
*/ |
| 327 |
|
| 328 |
if ((flags & TCL_EXACT) || (key[0] == '\0') || (numAbbrev != 1)) { |
| 329 |
goto error; |
| 330 |
} |
| 331 |
|
| 332 |
done: |
| 333 |
/* |
| 334 |
* Cache the found representation. Note that we want to avoid allocating a |
| 335 |
* new internal-rep if at all possible since that is potentially a slow |
| 336 |
* operation. |
| 337 |
*/ |
| 338 |
|
| 339 |
if (objPtr && (index >= 0)) { |
| 340 |
if (objPtr->typePtr == &indexType) { |
| 341 |
indexRep = (IndexRep *)objPtr->internalRep.twoPtrValue.ptr1; |
| 342 |
} else { |
| 343 |
TclFreeIntRep(objPtr); |
| 344 |
indexRep = (IndexRep *)ckalloc(sizeof(IndexRep)); |
| 345 |
objPtr->internalRep.twoPtrValue.ptr1 = indexRep; |
| 346 |
objPtr->typePtr = &indexType; |
| 347 |
} |
| 348 |
indexRep->tablePtr = (void *) tablePtr; |
| 349 |
indexRep->offset = offset; |
| 350 |
indexRep->index = index; |
| 351 |
} |
| 352 |
|
| 353 |
*indexPtr = index; |
| 354 |
return TCL_OK; |
| 355 |
|
| 356 |
error: |
| 357 |
if (interp != NULL) { |
| 358 |
/* |
| 359 |
* Produce a fancy error message. |
| 360 |
*/ |
| 361 |
|
| 362 |
int count = 0; |
| 363 |
|
| 364 |
TclNewObj(resultPtr); |
| 365 |
entryPtr = (const char *const *)tablePtr; |
| 366 |
while ((*entryPtr != NULL) && !**entryPtr) { |
| 367 |
entryPtr = NEXT_ENTRY(entryPtr, offset); |
| 368 |
} |
| 369 |
Tcl_AppendStringsToObj(resultPtr, |
| 370 |
(numAbbrev>1 && !(flags & TCL_EXACT) ? "ambiguous " : "bad "), |
| 371 |
msg, " \"", key, (char *)NULL); |
| 372 |
if (*entryPtr == NULL) { |
| 373 |
Tcl_AppendStringsToObj(resultPtr, "\": no valid options", (char *)NULL); |
| 374 |
} else { |
| 375 |
Tcl_AppendStringsToObj(resultPtr, "\": must be ", |
| 376 |
*entryPtr, (char *)NULL); |
| 377 |
entryPtr = NEXT_ENTRY(entryPtr, offset); |
| 378 |
while (*entryPtr != NULL) { |
| 379 |
if (*NEXT_ENTRY(entryPtr, offset) == NULL) { |
| 380 |
Tcl_AppendStringsToObj(resultPtr, (count > 0 ? "," : ""), |
| 381 |
" or ", *entryPtr, (char *)NULL); |
| 382 |
} else if (**entryPtr) { |
| 383 |
Tcl_AppendStringsToObj(resultPtr, ", ", *entryPtr, (char *)NULL); |
| 384 |
count++; |
| 385 |
} |
| 386 |
entryPtr = NEXT_ENTRY(entryPtr, offset); |
| 387 |
} |
| 388 |
} |
| 389 |
Tcl_SetObjResult(interp, resultPtr); |
| 390 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "INDEX", msg, key, (char *)NULL); |
| 391 |
} |
| 392 |
return TCL_ERROR; |
| 393 |
} |
| 394 |
|
| 395 |
/* |
| 396 |
*---------------------------------------------------------------------- |
| 397 |
* |
| 398 |
* UpdateStringOfIndex -- |
| 399 |
* |
| 400 |
* This function is called to convert a Tcl object from index internal |
| 401 |
* form to its string form. No abbreviation is ever generated. |
| 402 |
* |
| 403 |
* Results: |
| 404 |
* None. |
| 405 |
* |
| 406 |
* Side effects: |
| 407 |
* The string representation of the object is updated. |
| 408 |
* |
| 409 |
*---------------------------------------------------------------------- |
| 410 |
*/ |
| 411 |
|
| 412 |
static void |
| 413 |
UpdateStringOfIndex( |
| 414 |
Tcl_Obj *objPtr) |
| 415 |
{ |
| 416 |
IndexRep *indexRep = (IndexRep *)objPtr->internalRep.twoPtrValue.ptr1; |
| 417 |
char *buf; |
| 418 |
size_t len; |
| 419 |
const char *indexStr = EXPAND_OF(indexRep); |
| 420 |
|
| 421 |
len = strlen(indexStr); |
| 422 |
buf = (char *)ckalloc(len + 1); |
| 423 |
memcpy(buf, indexStr, len+1); |
| 424 |
objPtr->bytes = buf; |
| 425 |
objPtr->length = len; |
| 426 |
} |
| 427 |
|
| 428 |
/* |
| 429 |
*---------------------------------------------------------------------- |
| 430 |
* |
| 431 |
* DupIndex -- |
| 432 |
* |
| 433 |
* This function is called to copy the internal rep of an index Tcl |
| 434 |
* object from to another object. |
| 435 |
* |
| 436 |
* Results: |
| 437 |
* None. |
| 438 |
* |
| 439 |
* Side effects: |
| 440 |
* The internal representation of the target object is updated and the |
| 441 |
* type is set. |
| 442 |
* |
| 443 |
*---------------------------------------------------------------------- |
| 444 |
*/ |
| 445 |
|
| 446 |
static void |
| 447 |
DupIndex( |
| 448 |
Tcl_Obj *srcPtr, |
| 449 |
Tcl_Obj *dupPtr) |
| 450 |
{ |
| 451 |
IndexRep *srcIndexRep = (IndexRep *)srcPtr->internalRep.twoPtrValue.ptr1; |
| 452 |
IndexRep *dupIndexRep = (IndexRep *)ckalloc(sizeof(IndexRep)); |
| 453 |
|
| 454 |
memcpy(dupIndexRep, srcIndexRep, sizeof(IndexRep)); |
| 455 |
dupPtr->internalRep.twoPtrValue.ptr1 = dupIndexRep; |
| 456 |
dupPtr->typePtr = &indexType; |
| 457 |
} |
| 458 |
|
| 459 |
/* |
| 460 |
*---------------------------------------------------------------------- |
| 461 |
* |
| 462 |
* FreeIndex -- |
| 463 |
* |
| 464 |
* This function is called to delete the internal rep of an index Tcl |
| 465 |
* object. |
| 466 |
* |
| 467 |
* Results: |
| 468 |
* None. |
| 469 |
* |
| 470 |
* Side effects: |
| 471 |
* The internal representation of the target object is deleted. |
| 472 |
* |
| 473 |
*---------------------------------------------------------------------- |
| 474 |
*/ |
| 475 |
|
| 476 |
static void |
| 477 |
FreeIndex( |
| 478 |
Tcl_Obj *objPtr) |
| 479 |
{ |
| 480 |
ckfree(objPtr->internalRep.twoPtrValue.ptr1); |
| 481 |
objPtr->typePtr = NULL; |
| 482 |
} |
| 483 |
|
| 484 |
/* |
| 485 |
*---------------------------------------------------------------------- |
| 486 |
* |
| 487 |
* TclInitPrefixCmd -- |
| 488 |
* |
| 489 |
* This procedure creates the "prefix" Tcl command. See the user |
| 490 |
* documentation for details on what it does. |
| 491 |
* |
| 492 |
* Results: |
| 493 |
* A standard Tcl result. |
| 494 |
* |
| 495 |
* Side effects: |
| 496 |
* See the user documentation. |
| 497 |
* |
| 498 |
*---------------------------------------------------------------------- |
| 499 |
*/ |
| 500 |
|
| 501 |
Tcl_Command |
| 502 |
TclInitPrefixCmd( |
| 503 |
Tcl_Interp *interp) /* Current interpreter. */ |
| 504 |
{ |
| 505 |
static const EnsembleImplMap prefixImplMap[] = { |
| 506 |
{"all", PrefixAllObjCmd, TclCompileBasic2ArgCmd, NULL, NULL, 0}, |
| 507 |
{"longest", PrefixLongestObjCmd,TclCompileBasic2ArgCmd, NULL, NULL, 0}, |
| 508 |
{"match", PrefixMatchObjCmd, TclCompileBasicMin2ArgCmd, NULL, NULL, 0}, |
| 509 |
{NULL, NULL, NULL, NULL, NULL, 0} |
| 510 |
}; |
| 511 |
Tcl_Command prefixCmd; |
| 512 |
|
| 513 |
prefixCmd = TclMakeEnsemble(interp, "::tcl::prefix", prefixImplMap); |
| 514 |
Tcl_Export(interp, Tcl_FindNamespace(interp, "::tcl", NULL, 0), |
| 515 |
"prefix", 0); |
| 516 |
return prefixCmd; |
| 517 |
} |
| 518 |
|
| 519 |
/*---------------------------------------------------------------------- |
| 520 |
* |
| 521 |
* PrefixMatchObjCmd -- |
| 522 |
* |
| 523 |
* This function implements the 'prefix match' Tcl command. Refer to the |
| 524 |
* user documentation for details on what it does. |
| 525 |
* |
| 526 |
* Results: |
| 527 |
* Returns a standard Tcl result. |
| 528 |
* |
| 529 |
* Side effects: |
| 530 |
* None. |
| 531 |
* |
| 532 |
*---------------------------------------------------------------------- |
| 533 |
*/ |
| 534 |
|
| 535 |
static int |
| 536 |
PrefixMatchObjCmd( |
| 537 |
ClientData clientData, /* Not used. */ |
| 538 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 539 |
int objc, /* Number of arguments. */ |
| 540 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 541 |
{ |
| 542 |
int flags = 0, result, index; |
| 543 |
int dummyLength, errorLength, i; |
| 544 |
Tcl_Obj *errorPtr = NULL; |
| 545 |
const char *message = "option"; |
| 546 |
Tcl_Obj *tablePtr, *objPtr, *resultPtr; |
| 547 |
static const char *const matchOptions[] = { |
| 548 |
"-error", "-exact", "-message", NULL |
| 549 |
}; |
| 550 |
enum matchOptionsEnum { |
| 551 |
PRFMATCH_ERROR, PRFMATCH_EXACT, PRFMATCH_MESSAGE |
| 552 |
}; |
| 553 |
|
| 554 |
if (objc < 3) { |
| 555 |
Tcl_WrongNumArgs(interp, 1, objv, "?options? table string"); |
| 556 |
return TCL_ERROR; |
| 557 |
} |
| 558 |
|
| 559 |
for (i = 1; i < (objc - 2); i++) { |
| 560 |
if (Tcl_GetIndexFromObjStruct(interp, objv[i], matchOptions, |
| 561 |
sizeof(char *), "option", 0, &index) != TCL_OK) { |
| 562 |
return TCL_ERROR; |
| 563 |
} |
| 564 |
switch ((enum matchOptionsEnum) index) { |
| 565 |
case PRFMATCH_EXACT: |
| 566 |
flags |= TCL_EXACT; |
| 567 |
break; |
| 568 |
case PRFMATCH_MESSAGE: |
| 569 |
if (i > objc-4) { |
| 570 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 571 |
"missing value for -message", -1)); |
| 572 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "NOARG", (char *)NULL); |
| 573 |
return TCL_ERROR; |
| 574 |
} |
| 575 |
i++; |
| 576 |
message = TclGetString(objv[i]); |
| 577 |
break; |
| 578 |
case PRFMATCH_ERROR: |
| 579 |
if (i > objc-4) { |
| 580 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 581 |
"missing value for -error", -1)); |
| 582 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "NOARG", (char *)NULL); |
| 583 |
return TCL_ERROR; |
| 584 |
} |
| 585 |
i++; |
| 586 |
result = TclListObjLength(interp, objv[i], &errorLength); |
| 587 |
if (result != TCL_OK) { |
| 588 |
return TCL_ERROR; |
| 589 |
} |
| 590 |
if ((errorLength % 2) != 0) { |
| 591 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 592 |
"error options must have an even number of elements", |
| 593 |
-1)); |
| 594 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "DICTIONARY", (char *)NULL); |
| 595 |
return TCL_ERROR; |
| 596 |
} |
| 597 |
errorPtr = objv[i]; |
| 598 |
break; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
tablePtr = objv[objc - 2]; |
| 603 |
objPtr = objv[objc - 1]; |
| 604 |
|
| 605 |
/* |
| 606 |
* Check that table is a valid list first, since we want to handle that |
| 607 |
* error case regardless of level. |
| 608 |
*/ |
| 609 |
|
| 610 |
result = TclListObjLength(interp, tablePtr, &dummyLength); |
| 611 |
if (result != TCL_OK) { |
| 612 |
return result; |
| 613 |
} |
| 614 |
|
| 615 |
result = GetIndexFromObjList(interp, objPtr, tablePtr, message, flags, |
| 616 |
&index); |
| 617 |
if (result != TCL_OK) { |
| 618 |
if (errorPtr != NULL && errorLength == 0) { |
| 619 |
Tcl_ResetResult(interp); |
| 620 |
return TCL_OK; |
| 621 |
} else if (errorPtr == NULL) { |
| 622 |
return TCL_ERROR; |
| 623 |
} |
| 624 |
|
| 625 |
if (Tcl_IsShared(errorPtr)) { |
| 626 |
errorPtr = Tcl_DuplicateObj(errorPtr); |
| 627 |
} |
| 628 |
Tcl_ListObjAppendElement(interp, errorPtr, |
| 629 |
Tcl_NewStringObj("-code", 5)); |
| 630 |
Tcl_ListObjAppendElement(interp, errorPtr, Tcl_NewIntObj(result)); |
| 631 |
|
| 632 |
return Tcl_SetReturnOptions(interp, errorPtr); |
| 633 |
} |
| 634 |
|
| 635 |
result = Tcl_ListObjIndex(interp, tablePtr, index, &resultPtr); |
| 636 |
if (result != TCL_OK) { |
| 637 |
return result; |
| 638 |
} |
| 639 |
Tcl_SetObjResult(interp, resultPtr); |
| 640 |
return TCL_OK; |
| 641 |
} |
| 642 |
|
| 643 |
/*---------------------------------------------------------------------- |
| 644 |
* |
| 645 |
* PrefixAllObjCmd -- |
| 646 |
* |
| 647 |
* This function implements the 'prefix all' Tcl command. Refer to the |
| 648 |
* user documentation for details on what it does. |
| 649 |
* |
| 650 |
* Results: |
| 651 |
* Returns a standard Tcl result. |
| 652 |
* |
| 653 |
* Side effects: |
| 654 |
* None. |
| 655 |
* |
| 656 |
*---------------------------------------------------------------------- |
| 657 |
*/ |
| 658 |
|
| 659 |
static int |
| 660 |
PrefixAllObjCmd( |
| 661 |
ClientData clientData, /* Not used. */ |
| 662 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 663 |
int objc, /* Number of arguments. */ |
| 664 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 665 |
{ |
| 666 |
int result; |
| 667 |
int length, elemLength, tableObjc, t; |
| 668 |
const char *string, *elemString; |
| 669 |
Tcl_Obj **tableObjv, *resultPtr; |
| 670 |
|
| 671 |
if (objc != 3) { |
| 672 |
Tcl_WrongNumArgs(interp, 1, objv, "table string"); |
| 673 |
return TCL_ERROR; |
| 674 |
} |
| 675 |
|
| 676 |
result = TclListObjGetElements(interp, objv[1], &tableObjc, &tableObjv); |
| 677 |
if (result != TCL_OK) { |
| 678 |
return result; |
| 679 |
} |
| 680 |
resultPtr = Tcl_NewListObj(0, NULL); |
| 681 |
string = TclGetStringFromObj(objv[2], &length); |
| 682 |
|
| 683 |
for (t = 0; t < tableObjc; t++) { |
| 684 |
elemString = TclGetStringFromObj(tableObjv[t], &elemLength); |
| 685 |
|
| 686 |
/* |
| 687 |
* A prefix cannot match if it is longest. |
| 688 |
*/ |
| 689 |
|
| 690 |
if (length <= elemLength) { |
| 691 |
if (TclUtfNcmp2(elemString, string, length) == 0) { |
| 692 |
Tcl_ListObjAppendElement(interp, resultPtr, tableObjv[t]); |
| 693 |
} |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
Tcl_SetObjResult(interp, resultPtr); |
| 698 |
return TCL_OK; |
| 699 |
} |
| 700 |
|
| 701 |
/*---------------------------------------------------------------------- |
| 702 |
* |
| 703 |
* PrefixLongestObjCmd -- |
| 704 |
* |
| 705 |
* This function implements the 'prefix longest' Tcl command. Refer to |
| 706 |
* the user documentation for details on what it does. |
| 707 |
* |
| 708 |
* Results: |
| 709 |
* Returns a standard Tcl result. |
| 710 |
* |
| 711 |
* Side effects: |
| 712 |
* None. |
| 713 |
* |
| 714 |
*---------------------------------------------------------------------- |
| 715 |
*/ |
| 716 |
|
| 717 |
static int |
| 718 |
PrefixLongestObjCmd( |
| 719 |
ClientData clientData, /* Not used. */ |
| 720 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 721 |
int objc, /* Number of arguments. */ |
| 722 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 723 |
{ |
| 724 |
int result; |
| 725 |
int i, length, elemLength, resultLength, tableObjc, t; |
| 726 |
const char *string, *elemString, *resultString; |
| 727 |
Tcl_Obj **tableObjv; |
| 728 |
|
| 729 |
if (objc != 3) { |
| 730 |
Tcl_WrongNumArgs(interp, 1, objv, "table string"); |
| 731 |
return TCL_ERROR; |
| 732 |
} |
| 733 |
|
| 734 |
result = TclListObjGetElements(interp, objv[1], &tableObjc, &tableObjv); |
| 735 |
if (result != TCL_OK) { |
| 736 |
return result; |
| 737 |
} |
| 738 |
string = TclGetStringFromObj(objv[2], &length); |
| 739 |
|
| 740 |
resultString = NULL; |
| 741 |
resultLength = 0; |
| 742 |
|
| 743 |
for (t = 0; t < tableObjc; t++) { |
| 744 |
elemString = TclGetStringFromObj(tableObjv[t], &elemLength); |
| 745 |
|
| 746 |
/* |
| 747 |
* First check if the prefix string matches the element. A prefix |
| 748 |
* cannot match if it is longest. |
| 749 |
*/ |
| 750 |
|
| 751 |
if ((length > elemLength) || |
| 752 |
TclUtfNcmp2(elemString, string, length) != 0) { |
| 753 |
continue; |
| 754 |
} |
| 755 |
|
| 756 |
if (resultString == NULL) { |
| 757 |
/* |
| 758 |
* If this is the first match, the longest common substring this |
| 759 |
* far is the complete string. The result is part of this string |
| 760 |
* so we only need to adjust the length later. |
| 761 |
*/ |
| 762 |
|
| 763 |
resultString = elemString; |
| 764 |
resultLength = elemLength; |
| 765 |
} else { |
| 766 |
/* |
| 767 |
* Longest common substring cannot be longer than shortest string. |
| 768 |
*/ |
| 769 |
|
| 770 |
if (elemLength < resultLength) { |
| 771 |
resultLength = elemLength; |
| 772 |
} |
| 773 |
|
| 774 |
/* |
| 775 |
* Compare strings. |
| 776 |
*/ |
| 777 |
|
| 778 |
for (i = 0; i < resultLength; i++) { |
| 779 |
if (resultString[i] != elemString[i]) { |
| 780 |
/* |
| 781 |
* Adjust in case we stopped in the middle of a UTF char. |
| 782 |
*/ |
| 783 |
|
| 784 |
resultLength = TclUtfPrev(&resultString[i+1], |
| 785 |
resultString) - resultString; |
| 786 |
break; |
| 787 |
} |
| 788 |
} |
| 789 |
} |
| 790 |
} |
| 791 |
if (resultLength > 0) { |
| 792 |
Tcl_SetObjResult(interp, |
| 793 |
Tcl_NewStringObj(resultString, resultLength)); |
| 794 |
} |
| 795 |
return TCL_OK; |
| 796 |
} |
| 797 |
|
| 798 |
/* |
| 799 |
*---------------------------------------------------------------------- |
| 800 |
* |
| 801 |
* Tcl_WrongNumArgs -- |
| 802 |
* |
| 803 |
* This function generates a "wrong # args" error message in an |
| 804 |
* interpreter. It is used as a utility function by many command |
| 805 |
* functions, including the function that implements procedures. |
| 806 |
* |
| 807 |
* Results: |
| 808 |
* None. |
| 809 |
* |
| 810 |
* Side effects: |
| 811 |
* An error message is generated in interp's result object to indicate |
| 812 |
* that a command was invoked with the wrong number of arguments. The |
| 813 |
* message has the form |
| 814 |
* wrong # args: should be "foo bar additional stuff" |
| 815 |
* where "foo" and "bar" are the initial objects in objv (objc determines |
| 816 |
* how many of these are printed) and "additional stuff" is the contents |
| 817 |
* of the message argument. |
| 818 |
* |
| 819 |
* The message printed is modified somewhat if the command is wrapped |
| 820 |
* inside an ensemble. In that case, the error message generated is |
| 821 |
* rewritten in such a way that it appears to be generated from the |
| 822 |
* user-visible command and not how that command is actually implemented, |
| 823 |
* giving a better overall user experience. |
| 824 |
* |
| 825 |
* Internally, the Tcl core may set the flag INTERP_ALTERNATE_WRONG_ARGS |
| 826 |
* in the interpreter to generate complex multi-part messages by calling |
| 827 |
* this function repeatedly. This allows the code that knows how to |
| 828 |
* handle ensemble-related error messages to be kept here while still |
| 829 |
* generating suitable error messages for commands like [read] and |
| 830 |
* [socket]. Ideally, this would be done through an extra flags argument, |
| 831 |
* but that wouldn't be source-compatible with the existing API and it's |
| 832 |
* a fairly rare requirement anyway. |
| 833 |
* |
| 834 |
*---------------------------------------------------------------------- |
| 835 |
*/ |
| 836 |
|
| 837 |
void |
| 838 |
Tcl_WrongNumArgs( |
| 839 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 840 |
int objc, /* Number of arguments to print from objv. */ |
| 841 |
Tcl_Obj *const objv[], /* Initial argument objects, which should be |
| 842 |
* included in the error message. */ |
| 843 |
const char *message) /* Error message to print after the leading |
| 844 |
* objects in objv. The message may be |
| 845 |
* NULL. */ |
| 846 |
{ |
| 847 |
Tcl_Obj *objPtr; |
| 848 |
int i, len, elemLen; |
| 849 |
char flags; |
| 850 |
Interp *iPtr = (Interp *)interp; |
| 851 |
const char *elementStr; |
| 852 |
|
| 853 |
/* |
| 854 |
* [incr Tcl] does something fairly horrific when generating error |
| 855 |
* messages for its ensembles; it passes the whole set of ensemble |
| 856 |
* arguments as a list in the first argument. This means that this code |
| 857 |
* causes a problem in iTcl if it attempts to correctly quote all |
| 858 |
* arguments, which would be the correct thing to do. We work around this |
| 859 |
* nasty behaviour for now, and hope that we can remove it all in the |
| 860 |
* future... |
| 861 |
*/ |
| 862 |
|
| 863 |
#ifndef AVOID_HACKS_FOR_ITCL |
| 864 |
int isFirst = 1; /* Special flag used to inhibit the treating |
| 865 |
* of the first word as a list element so the |
| 866 |
* hacky way Itcl generates error messages for |
| 867 |
* its ensembles will still work. [Bug |
| 868 |
* 1066837] */ |
| 869 |
# define MAY_QUOTE_WORD (!isFirst) |
| 870 |
# define AFTER_FIRST_WORD (isFirst = 0) |
| 871 |
#else /* !AVOID_HACKS_FOR_ITCL */ |
| 872 |
# define MAY_QUOTE_WORD 1 |
| 873 |
# define AFTER_FIRST_WORD (void) 0 |
| 874 |
#endif /* AVOID_HACKS_FOR_ITCL */ |
| 875 |
|
| 876 |
TclNewObj(objPtr); |
| 877 |
if (iPtr->flags & INTERP_ALTERNATE_WRONG_ARGS) { |
| 878 |
iPtr->flags &= ~INTERP_ALTERNATE_WRONG_ARGS; |
| 879 |
Tcl_AppendObjToObj(objPtr, Tcl_GetObjResult(interp)); |
| 880 |
Tcl_AppendToObj(objPtr, " or \"", -1); |
| 881 |
} else { |
| 882 |
Tcl_AppendToObj(objPtr, "wrong # args: should be \"", -1); |
| 883 |
} |
| 884 |
|
| 885 |
/* |
| 886 |
* If processing an an ensemble implementation, rewrite the results in |
| 887 |
* terms of how the ensemble was invoked. |
| 888 |
*/ |
| 889 |
|
| 890 |
if (iPtr->ensembleRewrite.sourceObjs != NULL) { |
| 891 |
int toSkip = iPtr->ensembleRewrite.numInsertedObjs; |
| 892 |
int toPrint = iPtr->ensembleRewrite.numRemovedObjs; |
| 893 |
Tcl_Obj *const *origObjv = TclEnsembleGetRewriteValues(interp); |
| 894 |
|
| 895 |
/* |
| 896 |
* Only do rewrite the command if all the replaced objects are |
| 897 |
* actually arguments (in objv) to this function. Otherwise it just |
| 898 |
* gets too complicated and it's to just give a slightly |
| 899 |
* confusing error message... |
| 900 |
*/ |
| 901 |
|
| 902 |
if (objc < toSkip) { |
| 903 |
goto addNormalArgumentsToMessage; |
| 904 |
} |
| 905 |
|
| 906 |
/* |
| 907 |
* Strip out the actual arguments that the ensemble inserted. |
| 908 |
*/ |
| 909 |
|
| 910 |
objv += toSkip; |
| 911 |
objc -= toSkip; |
| 912 |
|
| 913 |
/* |
| 914 |
* Assume no object is of index type. |
| 915 |
*/ |
| 916 |
|
| 917 |
for (i=0 ; i<toPrint ; i++) { |
| 918 |
/* |
| 919 |
* Add the element, quoting it if necessary. |
| 920 |
*/ |
| 921 |
|
| 922 |
if (origObjv[i]->typePtr == &indexType) { |
| 923 |
IndexRep *indexRep = |
| 924 |
(IndexRep *)origObjv[i]->internalRep.twoPtrValue.ptr1; |
| 925 |
|
| 926 |
elementStr = EXPAND_OF(indexRep); |
| 927 |
elemLen = strlen(elementStr); |
| 928 |
} else { |
| 929 |
elementStr = TclGetStringFromObj(origObjv[i], &elemLen); |
| 930 |
} |
| 931 |
flags = 0; |
| 932 |
len = TclScanElement(elementStr, elemLen, &flags); |
| 933 |
|
| 934 |
if (MAY_QUOTE_WORD && len != elemLen) { |
| 935 |
char *quotedElementStr = (char *)TclStackAlloc(interp, len + 1); |
| 936 |
|
| 937 |
len = TclConvertElement(elementStr, elemLen, |
| 938 |
quotedElementStr, flags); |
| 939 |
Tcl_AppendToObj(objPtr, quotedElementStr, len); |
| 940 |
TclStackFree(interp, quotedElementStr); |
| 941 |
} else { |
| 942 |
Tcl_AppendToObj(objPtr, elementStr, elemLen); |
| 943 |
} |
| 944 |
|
| 945 |
AFTER_FIRST_WORD; |
| 946 |
|
| 947 |
/* |
| 948 |
* Add a space if the word is not the last one (which has a |
| 949 |
* moderately complex condition here). |
| 950 |
*/ |
| 951 |
|
| 952 |
if (i + 1 < toPrint || objc!=0 || message!=NULL) { |
| 953 |
Tcl_AppendStringsToObj(objPtr, " ", (char *)NULL); |
| 954 |
} |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
/* |
| 959 |
* Now add the arguments (other than those rewritten) that the caller took |
| 960 |
* from its calling context. |
| 961 |
*/ |
| 962 |
|
| 963 |
addNormalArgumentsToMessage: |
| 964 |
for (i = 0; i < objc; i++) { |
| 965 |
/* |
| 966 |
* If the object is an index type, use the index table which allows for |
| 967 |
* the correct error message even if the subcommand was abbreviated. |
| 968 |
* Otherwise, just use the string rep. |
| 969 |
*/ |
| 970 |
|
| 971 |
if (objv[i]->typePtr == &indexType) { |
| 972 |
IndexRep *indexRep = (IndexRep *)objv[i]->internalRep.twoPtrValue.ptr1; |
| 973 |
|
| 974 |
Tcl_AppendStringsToObj(objPtr, EXPAND_OF(indexRep), (char *)NULL); |
| 975 |
} else { |
| 976 |
/* |
| 977 |
* Quote the argument if it contains spaces (Bug 942757). |
| 978 |
*/ |
| 979 |
|
| 980 |
elementStr = TclGetStringFromObj(objv[i], &elemLen); |
| 981 |
flags = 0; |
| 982 |
len = TclScanElement(elementStr, elemLen, &flags); |
| 983 |
|
| 984 |
if (MAY_QUOTE_WORD && len != elemLen) { |
| 985 |
char *quotedElementStr = (char *)TclStackAlloc(interp, len + 1); |
| 986 |
|
| 987 |
len = TclConvertElement(elementStr, elemLen, |
| 988 |
quotedElementStr, flags); |
| 989 |
Tcl_AppendToObj(objPtr, quotedElementStr, len); |
| 990 |
TclStackFree(interp, quotedElementStr); |
| 991 |
} else { |
| 992 |
Tcl_AppendToObj(objPtr, elementStr, elemLen); |
| 993 |
} |
| 994 |
} |
| 995 |
|
| 996 |
AFTER_FIRST_WORD; |
| 997 |
|
| 998 |
/* |
| 999 |
* Append a space character (" ") if there is more text to follow |
| 1000 |
* (either another element from objv, or the message string). |
| 1001 |
*/ |
| 1002 |
|
| 1003 |
if (i + 1 < objc || message!=NULL) { |
| 1004 |
Tcl_AppendStringsToObj(objPtr, " ", (char *)NULL); |
| 1005 |
} |
| 1006 |
} |
| 1007 |
|
| 1008 |
/* |
| 1009 |
* Add any trailing message bits and set the resulting string as the |
| 1010 |
* interpreter result. Caller is responsible for reporting this as an |
| 1011 |
* actual error. |
| 1012 |
*/ |
| 1013 |
|
| 1014 |
if (message != NULL) { |
| 1015 |
Tcl_AppendStringsToObj(objPtr, message, (char *)NULL); |
| 1016 |
} |
| 1017 |
Tcl_AppendStringsToObj(objPtr, "\"", (char *)NULL); |
| 1018 |
Tcl_SetErrorCode(interp, "TCL", "WRONGARGS", (char *)NULL); |
| 1019 |
Tcl_SetObjResult(interp, objPtr); |
| 1020 |
#undef MAY_QUOTE_WORD |
| 1021 |
#undef AFTER_FIRST_WORD |
| 1022 |
} |
| 1023 |
|
| 1024 |
/* |
| 1025 |
*---------------------------------------------------------------------- |
| 1026 |
* |
| 1027 |
* Tcl_ParseArgsObjv -- |
| 1028 |
* |
| 1029 |
* Process an objv array according to a table of expected command-line |
| 1030 |
* options. See the manual page for more details. |
| 1031 |
* |
| 1032 |
* Results: |
| 1033 |
* The return value is a standard Tcl return value. If an error occurs |
| 1034 |
* then an error message is left in the interp's result. Under normal |
| 1035 |
* conditions, both *objcPtr and *objv are modified to return the |
| 1036 |
* arguments that couldn't be processed here (they didn't match the |
| 1037 |
* option table, or followed an TCL_ARGV_REST argument). |
| 1038 |
* |
| 1039 |
* Side effects: |
| 1040 |
* Variables may be modified, or procedures may be called. It all depends |
| 1041 |
* on the arguments and their entries in argTable. See the user |
| 1042 |
* documentation for details. |
| 1043 |
* |
| 1044 |
*---------------------------------------------------------------------- |
| 1045 |
*/ |
| 1046 |
|
| 1047 |
int |
| 1048 |
Tcl_ParseArgsObjv( |
| 1049 |
Tcl_Interp *interp, /* Place to store error message. */ |
| 1050 |
const Tcl_ArgvInfo *argTable, |
| 1051 |
/* Array of option descriptions. */ |
| 1052 |
int *objcPtr, /* Number of arguments in objv. Modified to |
| 1053 |
* hold # args left in objv at end. */ |
| 1054 |
Tcl_Obj *const *objv, /* Array of arguments to be parsed. */ |
| 1055 |
Tcl_Obj ***remObjv) /* Pointer to array of arguments that were not |
| 1056 |
* processed here. Should be NULL if no return |
| 1057 |
* of arguments is desired. */ |
| 1058 |
{ |
| 1059 |
Tcl_Obj **leftovers; /* Array to write back to remObjv on |
| 1060 |
* successful exit. Will include the name of |
| 1061 |
* the command. */ |
| 1062 |
int nrem; /* Size of leftovers.*/ |
| 1063 |
const Tcl_ArgvInfo *infoPtr; |
| 1064 |
/* Pointer to the current entry in the table |
| 1065 |
* of argument descriptions. */ |
| 1066 |
const Tcl_ArgvInfo *matchPtr; |
| 1067 |
/* Descriptor that matches current argument */ |
| 1068 |
Tcl_Obj *curArg; /* Current argument */ |
| 1069 |
const char *str = NULL; |
| 1070 |
char c; /* Second character of current arg (used for |
| 1071 |
* quick check for matching; use 2nd char. |
| 1072 |
* because first char. will almost always be |
| 1073 |
* '-'). */ |
| 1074 |
int srcIndex; /* Location from which to read next argument |
| 1075 |
* from objv. */ |
| 1076 |
int dstIndex; /* Used to keep track of current arguments |
| 1077 |
* being processed, primarily for error |
| 1078 |
* reporting. */ |
| 1079 |
int objc; /* # arguments in objv still to process. */ |
| 1080 |
int length; /* Number of characters in current argument */ |
| 1081 |
int gf_ret; /* Return value from Tcl_ArgvGenFuncProc*/ |
| 1082 |
|
| 1083 |
if (remObjv != NULL) { |
| 1084 |
/* |
| 1085 |
* Then we should copy the name of the command (0th argument). The |
| 1086 |
* upper bound on the number of elements is known, and (undocumented, |
| 1087 |
* but historically true) there should be a NULL argument after the |
| 1088 |
* last result. [Bug 3413857] |
| 1089 |
*/ |
| 1090 |
|
| 1091 |
nrem = 1; |
| 1092 |
leftovers = (Tcl_Obj **)ckalloc((1 + *objcPtr) * sizeof(Tcl_Obj *)); |
| 1093 |
leftovers[0] = objv[0]; |
| 1094 |
} else { |
| 1095 |
nrem = 0; |
| 1096 |
leftovers = NULL; |
| 1097 |
} |
| 1098 |
|
| 1099 |
/* |
| 1100 |
* OK, now start processing from the second element (1st argument). |
| 1101 |
*/ |
| 1102 |
|
| 1103 |
srcIndex = dstIndex = 1; |
| 1104 |
objc = *objcPtr-1; |
| 1105 |
|
| 1106 |
while (objc > 0) { |
| 1107 |
curArg = objv[srcIndex]; |
| 1108 |
srcIndex++; |
| 1109 |
objc--; |
| 1110 |
str = TclGetStringFromObj(curArg, &length); |
| 1111 |
if (length > 0) { |
| 1112 |
c = str[1]; |
| 1113 |
} else { |
| 1114 |
c = 0; |
| 1115 |
} |
| 1116 |
|
| 1117 |
/* |
| 1118 |
* Loop through the argument descriptors searching for one with the |
| 1119 |
* matching key string. If found, leave a pointer to it in matchPtr. |
| 1120 |
*/ |
| 1121 |
|
| 1122 |
matchPtr = NULL; |
| 1123 |
infoPtr = argTable; |
| 1124 |
for (; infoPtr != NULL && infoPtr->type != TCL_ARGV_END ; infoPtr++) { |
| 1125 |
if (infoPtr->keyStr == NULL) { |
| 1126 |
continue; |
| 1127 |
} |
| 1128 |
if ((infoPtr->keyStr[1] != c) |
| 1129 |
|| (strncmp(infoPtr->keyStr, str, length) != 0)) { |
| 1130 |
continue; |
| 1131 |
} |
| 1132 |
if (infoPtr->keyStr[length] == 0) { |
| 1133 |
matchPtr = infoPtr; |
| 1134 |
goto gotMatch; |
| 1135 |
} |
| 1136 |
if (matchPtr != NULL) { |
| 1137 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1138 |
"ambiguous option \"%s\"", str)); |
| 1139 |
goto error; |
| 1140 |
} |
| 1141 |
matchPtr = infoPtr; |
| 1142 |
} |
| 1143 |
if (matchPtr == NULL) { |
| 1144 |
/* |
| 1145 |
* Unrecognized argument. Just copy it down, unless the caller |
| 1146 |
* prefers an error to be registered. |
| 1147 |
*/ |
| 1148 |
|
| 1149 |
if (remObjv == NULL) { |
| 1150 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1151 |
"unrecognized argument \"%s\"", str)); |
| 1152 |
goto error; |
| 1153 |
} |
| 1154 |
|
| 1155 |
dstIndex++; /* This argument is now handled */ |
| 1156 |
leftovers[nrem++] = curArg; |
| 1157 |
continue; |
| 1158 |
} |
| 1159 |
|
| 1160 |
/* |
| 1161 |
* Take the appropriate action based on the option type |
| 1162 |
*/ |
| 1163 |
|
| 1164 |
gotMatch: |
| 1165 |
infoPtr = matchPtr; |
| 1166 |
switch (infoPtr->type) { |
| 1167 |
case TCL_ARGV_CONSTANT: |
| 1168 |
*((int *) infoPtr->dstPtr) = PTR2INT(infoPtr->srcPtr); |
| 1169 |
break; |
| 1170 |
case TCL_ARGV_INT: |
| 1171 |
if (objc == 0) { |
| 1172 |
goto missingArg; |
| 1173 |
} |
| 1174 |
if (Tcl_GetIntFromObj(interp, objv[srcIndex], |
| 1175 |
(int *) infoPtr->dstPtr) == TCL_ERROR) { |
| 1176 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1177 |
"expected integer argument for \"%s\" but got \"%s\"", |
| 1178 |
infoPtr->keyStr, TclGetString(objv[srcIndex]))); |
| 1179 |
goto error; |
| 1180 |
} |
| 1181 |
srcIndex++; |
| 1182 |
objc--; |
| 1183 |
break; |
| 1184 |
case TCL_ARGV_STRING: |
| 1185 |
if (objc == 0) { |
| 1186 |
goto missingArg; |
| 1187 |
} |
| 1188 |
*((const char **) infoPtr->dstPtr) = |
| 1189 |
TclGetString(objv[srcIndex]); |
| 1190 |
srcIndex++; |
| 1191 |
objc--; |
| 1192 |
break; |
| 1193 |
case TCL_ARGV_REST: |
| 1194 |
/* |
| 1195 |
* Only store the point where we got to if it's not to be written |
| 1196 |
* to NULL, so that TCL_ARGV_AUTO_REST works. |
| 1197 |
*/ |
| 1198 |
|
| 1199 |
if (infoPtr->dstPtr != NULL) { |
| 1200 |
*((int *) infoPtr->dstPtr) = dstIndex; |
| 1201 |
} |
| 1202 |
goto argsDone; |
| 1203 |
case TCL_ARGV_FLOAT: |
| 1204 |
if (objc == 0) { |
| 1205 |
goto missingArg; |
| 1206 |
} |
| 1207 |
if (Tcl_GetDoubleFromObj(interp, objv[srcIndex], |
| 1208 |
(double *) infoPtr->dstPtr) == TCL_ERROR) { |
| 1209 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1210 |
"expected floating-point argument for \"%s\" but got \"%s\"", |
| 1211 |
infoPtr->keyStr, TclGetString(objv[srcIndex]))); |
| 1212 |
goto error; |
| 1213 |
} |
| 1214 |
srcIndex++; |
| 1215 |
objc--; |
| 1216 |
break; |
| 1217 |
case TCL_ARGV_FUNC: { |
| 1218 |
Tcl_ArgvFuncProc *handlerProc = (Tcl_ArgvFuncProc *) |
| 1219 |
infoPtr->srcPtr; |
| 1220 |
Tcl_Obj *argObj; |
| 1221 |
|
| 1222 |
if (objc == 0) { |
| 1223 |
argObj = NULL; |
| 1224 |
} else { |
| 1225 |
argObj = objv[srcIndex]; |
| 1226 |
} |
| 1227 |
if (handlerProc(infoPtr->clientData, argObj, infoPtr->dstPtr)) { |
| 1228 |
srcIndex++; |
| 1229 |
objc--; |
| 1230 |
} |
| 1231 |
break; |
| 1232 |
} |
| 1233 |
case TCL_ARGV_GENFUNC: { |
| 1234 |
Tcl_ArgvGenFuncProc *handlerProc = (Tcl_ArgvGenFuncProc *) |
| 1235 |
infoPtr->srcPtr; |
| 1236 |
|
| 1237 |
gf_ret = handlerProc(infoPtr->clientData, interp, objc, |
| 1238 |
&objv[srcIndex], infoPtr->dstPtr); |
| 1239 |
if (gf_ret < 0) { |
| 1240 |
goto error; |
| 1241 |
} else { |
| 1242 |
srcIndex += gf_ret; |
| 1243 |
objc -= gf_ret; |
| 1244 |
} |
| 1245 |
break; |
| 1246 |
} |
| 1247 |
case TCL_ARGV_HELP: |
| 1248 |
PrintUsage(interp, argTable); |
| 1249 |
goto error; |
| 1250 |
default: |
| 1251 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1252 |
"bad argument type %d in Tcl_ArgvInfo", infoPtr->type)); |
| 1253 |
goto error; |
| 1254 |
} |
| 1255 |
} |
| 1256 |
|
| 1257 |
/* |
| 1258 |
* If we broke out of the loop because of an OPT_REST argument, copy the |
| 1259 |
* remaining arguments down. Note that there is always at least one |
| 1260 |
* argument left over - the command name - so we always have a result if |
| 1261 |
* our caller is willing to receive it. [Bug 3413857] |
| 1262 |
*/ |
| 1263 |
|
| 1264 |
argsDone: |
| 1265 |
if (remObjv == NULL) { |
| 1266 |
/* |
| 1267 |
* Nothing to do. |
| 1268 |
*/ |
| 1269 |
|
| 1270 |
return TCL_OK; |
| 1271 |
} |
| 1272 |
|
| 1273 |
if (objc > 0) { |
| 1274 |
memcpy(leftovers+nrem, objv+srcIndex, objc*sizeof(Tcl_Obj *)); |
| 1275 |
nrem += objc; |
| 1276 |
} |
| 1277 |
leftovers[nrem] = NULL; |
| 1278 |
*objcPtr = nrem++; |
| 1279 |
*remObjv = (Tcl_Obj **)ckrealloc(leftovers, nrem * sizeof(Tcl_Obj *)); |
| 1280 |
return TCL_OK; |
| 1281 |
|
| 1282 |
/* |
| 1283 |
* Make sure to handle freeing any temporary space we've allocated on the |
| 1284 |
* way to an error. |
| 1285 |
*/ |
| 1286 |
|
| 1287 |
missingArg: |
| 1288 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1289 |
"\"%s\" option requires an additional argument", str)); |
| 1290 |
error: |
| 1291 |
if (leftovers != NULL) { |
| 1292 |
ckfree(leftovers); |
| 1293 |
} |
| 1294 |
return TCL_ERROR; |
| 1295 |
} |
| 1296 |
|
| 1297 |
/* |
| 1298 |
*---------------------------------------------------------------------- |
| 1299 |
* |
| 1300 |
* PrintUsage -- |
| 1301 |
* |
| 1302 |
* Generate a help string describing command-line options. |
| 1303 |
* |
| 1304 |
* Results: |
| 1305 |
* The interp's result will be modified to hold a help string describing |
| 1306 |
* all the options in argTable. |
| 1307 |
* |
| 1308 |
* Side effects: |
| 1309 |
* None. |
| 1310 |
* |
| 1311 |
*---------------------------------------------------------------------- |
| 1312 |
*/ |
| 1313 |
|
| 1314 |
static void |
| 1315 |
PrintUsage( |
| 1316 |
Tcl_Interp *interp, /* Place information in this interp's result |
| 1317 |
* area. */ |
| 1318 |
const Tcl_ArgvInfo *argTable) |
| 1319 |
/* Array of command-specific argument |
| 1320 |
* descriptions. */ |
| 1321 |
{ |
| 1322 |
const Tcl_ArgvInfo *infoPtr; |
| 1323 |
int width, numSpaces; |
| 1324 |
#define NUM_SPACES 20 |
| 1325 |
static const char spaces[] = " "; |
| 1326 |
Tcl_Obj *msg; |
| 1327 |
|
| 1328 |
/* |
| 1329 |
* First, compute the width of the widest option key, so that we can make |
| 1330 |
* everything line up. |
| 1331 |
*/ |
| 1332 |
|
| 1333 |
width = 4; |
| 1334 |
for (infoPtr = argTable; infoPtr->type != TCL_ARGV_END; infoPtr++) { |
| 1335 |
int length; |
| 1336 |
|
| 1337 |
if (infoPtr->keyStr == NULL) { |
| 1338 |
continue; |
| 1339 |
} |
| 1340 |
length = strlen(infoPtr->keyStr); |
| 1341 |
if (length > width) { |
| 1342 |
width = length; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
/* |
| 1347 |
* Now add the option information, with pretty-printing. |
| 1348 |
*/ |
| 1349 |
|
| 1350 |
msg = Tcl_NewStringObj("Command-specific options:", -1); |
| 1351 |
for (infoPtr = argTable; infoPtr->type != TCL_ARGV_END; infoPtr++) { |
| 1352 |
if ((infoPtr->type == TCL_ARGV_HELP) && (infoPtr->keyStr == NULL)) { |
| 1353 |
Tcl_AppendPrintfToObj(msg, "\n%s", infoPtr->helpStr); |
| 1354 |
continue; |
| 1355 |
} |
| 1356 |
Tcl_AppendPrintfToObj(msg, "\n %s:", infoPtr->keyStr); |
| 1357 |
numSpaces = width + 1 - strlen(infoPtr->keyStr); |
| 1358 |
while (numSpaces > 0) { |
| 1359 |
if (numSpaces >= NUM_SPACES) { |
| 1360 |
Tcl_AppendToObj(msg, spaces, NUM_SPACES); |
| 1361 |
} else { |
| 1362 |
Tcl_AppendToObj(msg, spaces, numSpaces); |
| 1363 |
} |
| 1364 |
numSpaces -= NUM_SPACES; |
| 1365 |
} |
| 1366 |
Tcl_AppendToObj(msg, infoPtr->helpStr, -1); |
| 1367 |
switch (infoPtr->type) { |
| 1368 |
case TCL_ARGV_INT: |
| 1369 |
Tcl_AppendPrintfToObj(msg, "\n\t\tDefault value: %d", |
| 1370 |
*((int *) infoPtr->dstPtr)); |
| 1371 |
break; |
| 1372 |
case TCL_ARGV_FLOAT: |
| 1373 |
Tcl_AppendPrintfToObj(msg, "\n\t\tDefault value: %g", |
| 1374 |
*((double *) infoPtr->dstPtr)); |
| 1375 |
break; |
| 1376 |
case TCL_ARGV_STRING: { |
| 1377 |
char *string = *((char **) infoPtr->dstPtr); |
| 1378 |
|
| 1379 |
if (string != NULL) { |
| 1380 |
Tcl_AppendPrintfToObj(msg, "\n\t\tDefault value: \"%s\"", |
| 1381 |
string); |
| 1382 |
} |
| 1383 |
break; |
| 1384 |
} |
| 1385 |
default: |
| 1386 |
break; |
| 1387 |
} |
| 1388 |
} |
| 1389 |
Tcl_SetObjResult(interp, msg); |
| 1390 |
} |
| 1391 |
|
| 1392 |
/* |
| 1393 |
*---------------------------------------------------------------------- |
| 1394 |
* |
| 1395 |
* TclGetCompletionCodeFromObj -- |
| 1396 |
* |
| 1397 |
* Parses Completion code Code |
| 1398 |
* |
| 1399 |
* Results: |
| 1400 |
* Returns TCL_ERROR if the value is an invalid completion code. |
| 1401 |
* Otherwise, returns TCL_OK, and writes the completion code to the |
| 1402 |
* pointer provided. |
| 1403 |
* |
| 1404 |
* Side effects: |
| 1405 |
* None. |
| 1406 |
* |
| 1407 |
*---------------------------------------------------------------------- |
| 1408 |
*/ |
| 1409 |
|
| 1410 |
int |
| 1411 |
TclGetCompletionCodeFromObj( |
| 1412 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1413 |
Tcl_Obj *value, |
| 1414 |
int *codePtr) /* Argument objects. */ |
| 1415 |
{ |
| 1416 |
static const char *const returnCodes[] = { |
| 1417 |
"ok", "error", "return", "break", "continue", NULL |
| 1418 |
}; |
| 1419 |
|
| 1420 |
if ((value->typePtr != &indexType) |
| 1421 |
&& TclGetIntFromObj(NULL, value, codePtr) == TCL_OK) { |
| 1422 |
return TCL_OK; |
| 1423 |
} |
| 1424 |
if (Tcl_GetIndexFromObjStruct(NULL, value, returnCodes, |
| 1425 |
sizeof(char *), NULL, TCL_EXACT, codePtr) == TCL_OK) { |
| 1426 |
return TCL_OK; |
| 1427 |
} |
| 1428 |
|
| 1429 |
/* |
| 1430 |
* Value is not a legal completion code. |
| 1431 |
*/ |
| 1432 |
|
| 1433 |
if (interp != NULL) { |
| 1434 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1435 |
"bad completion code \"%s\": must be" |
| 1436 |
" ok, error, return, break, continue, or an integer", |
| 1437 |
TclGetString(value))); |
| 1438 |
Tcl_SetErrorCode(interp, "TCL", "RESULT", "ILLEGAL_CODE", (char *)NULL); |
| 1439 |
} |
| 1440 |
return TCL_ERROR; |
| 1441 |
} |
| 1442 |
|
| 1443 |
/* |
| 1444 |
* Local Variables: |
| 1445 |
* mode: c |
| 1446 |
* c-basic-offset: 4 |
| 1447 |
* fill-column: 78 |
| 1448 |
* End: |
| 1449 |
*/ |