| 1 |
/* |
| 2 |
* tclFileName.c -- |
| 3 |
* |
| 4 |
* This file contains routines for converting file names betwen native |
| 5 |
* and network form. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1998 Sun Microsystems, Inc. |
| 8 |
* Copyright (c) 1998-1999 Scriptics Corporation. |
| 9 |
* |
| 10 |
* See the file "license.terms" for information on usage and redistribution of |
| 11 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include "tclInt.h" |
| 15 |
#include "tclRegexp.h" |
| 16 |
#include "tclFileSystem.h" /* For TclGetPathType() */ |
| 17 |
|
| 18 |
/* |
| 19 |
* The following variable is set in the TclPlatformInit call to one of: |
| 20 |
* TCL_PLATFORM_UNIX or TCL_PLATFORM_WINDOWS. |
| 21 |
*/ |
| 22 |
|
| 23 |
TclPlatformType tclPlatform = TCL_PLATFORM_UNIX; |
| 24 |
|
| 25 |
/* |
| 26 |
* Prototypes for local procedures defined in this file: |
| 27 |
*/ |
| 28 |
|
| 29 |
static const char * DoTildeSubst(Tcl_Interp *interp, |
| 30 |
const char *user, Tcl_DString *resultPtr); |
| 31 |
static const char * ExtractWinRoot(const char *path, |
| 32 |
Tcl_DString *resultPtr, int offset, |
| 33 |
Tcl_PathType *typePtr); |
| 34 |
static int SkipToChar(char **stringPtr, int match); |
| 35 |
static Tcl_Obj * SplitWinPath(const char *path); |
| 36 |
static Tcl_Obj * SplitUnixPath(const char *path); |
| 37 |
static int DoGlob(Tcl_Interp *interp, Tcl_Obj *resultPtr, |
| 38 |
const char *separators, Tcl_Obj *pathPtr, int flags, |
| 39 |
char *pattern, Tcl_GlobTypeData *types); |
| 40 |
static int TclGlob(Tcl_Interp *interp, char *pattern, |
| 41 |
Tcl_Obj *pathPrefix, int globFlags, |
| 42 |
Tcl_GlobTypeData *types); |
| 43 |
|
| 44 |
/* |
| 45 |
* When there is no support for getting the block size of a file in a stat() |
| 46 |
* call, use this as a guess. Allow it to be overridden in the platform- |
| 47 |
* specific files. |
| 48 |
*/ |
| 49 |
|
| 50 |
#if (!defined(HAVE_STRUCT_STAT_ST_BLKSIZE) && !defined(GUESSED_BLOCK_SIZE)) |
| 51 |
#define GUESSED_BLOCK_SIZE 1024 |
| 52 |
#endif |
| 53 |
|
| 54 |
/* |
| 55 |
*---------------------------------------------------------------------- |
| 56 |
* |
| 57 |
* SetResultLength -- |
| 58 |
* |
| 59 |
* Resets the result DString for ExtractWinRoot to accommodate |
| 60 |
* any NT extended path prefixes. |
| 61 |
* |
| 62 |
* Results: |
| 63 |
* None. |
| 64 |
* |
| 65 |
* Side effects: |
| 66 |
* May modify the Tcl_DString. |
| 67 |
*---------------------------------------------------------------------- |
| 68 |
*/ |
| 69 |
|
| 70 |
static void |
| 71 |
SetResultLength( |
| 72 |
Tcl_DString *resultPtr, |
| 73 |
int offset, |
| 74 |
int extended) |
| 75 |
{ |
| 76 |
Tcl_DStringSetLength(resultPtr, offset); |
| 77 |
if (extended == 2) { |
| 78 |
TclDStringAppendLiteral(resultPtr, "//?/UNC/"); |
| 79 |
} else if (extended == 1) { |
| 80 |
TclDStringAppendLiteral(resultPtr, "//?/"); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
*---------------------------------------------------------------------- |
| 86 |
* |
| 87 |
* ExtractWinRoot -- |
| 88 |
* |
| 89 |
* Matches the root portion of a Windows path and appends it to the |
| 90 |
* specified Tcl_DString. |
| 91 |
* |
| 92 |
* Results: |
| 93 |
* Returns the position in the path immediately after the root including |
| 94 |
* any trailing slashes. Appends a cleaned up version of the root to the |
| 95 |
* Tcl_DString at the specified offset. |
| 96 |
* |
| 97 |
* Side effects: |
| 98 |
* Modifies the specified Tcl_DString. |
| 99 |
* |
| 100 |
*---------------------------------------------------------------------- |
| 101 |
*/ |
| 102 |
|
| 103 |
static const char * |
| 104 |
ExtractWinRoot( |
| 105 |
const char *path, /* Path to parse. */ |
| 106 |
Tcl_DString *resultPtr, /* Buffer to hold result. */ |
| 107 |
int offset, /* Offset in buffer where result should be |
| 108 |
* stored. */ |
| 109 |
Tcl_PathType *typePtr) /* Where to store pathType result */ |
| 110 |
{ |
| 111 |
int extended = 0; |
| 112 |
|
| 113 |
if ( (path[0] == '/' || path[0] == '\\') |
| 114 |
&& (path[1] == '/' || path[1] == '\\') |
| 115 |
&& (path[2] == '?') |
| 116 |
&& (path[3] == '/' || path[3] == '\\')) { |
| 117 |
extended = 1; |
| 118 |
path = path + 4; |
| 119 |
if (path[0] == 'U' && path[1] == 'N' && path[2] == 'C' |
| 120 |
&& (path[3] == '/' || path[3] == '\\')) { |
| 121 |
extended = 2; |
| 122 |
path = path + 4; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if (path[0] == '/' || path[0] == '\\') { |
| 127 |
/* |
| 128 |
* Might be a UNC or Vol-Relative path. |
| 129 |
*/ |
| 130 |
|
| 131 |
const char *host, *share, *tail; |
| 132 |
int hlen, slen; |
| 133 |
|
| 134 |
if (path[1] != '/' && path[1] != '\\') { |
| 135 |
SetResultLength(resultPtr, offset, extended); |
| 136 |
*typePtr = TCL_PATH_VOLUME_RELATIVE; |
| 137 |
TclDStringAppendLiteral(resultPtr, "/"); |
| 138 |
return &path[1]; |
| 139 |
} |
| 140 |
host = &path[2]; |
| 141 |
|
| 142 |
/* |
| 143 |
* Skip separators. |
| 144 |
*/ |
| 145 |
|
| 146 |
while (host[0] == '/' || host[0] == '\\') { |
| 147 |
host++; |
| 148 |
} |
| 149 |
|
| 150 |
for (hlen = 0; host[hlen];hlen++) { |
| 151 |
if (host[hlen] == '/' || host[hlen] == '\\') { |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
if (host[hlen] == 0 || host[hlen+1] == 0) { |
| 156 |
/* |
| 157 |
* The path given is simply of the form '/foo', '//foo', |
| 158 |
* '/////foo' or the same with backslashes. If there is exactly |
| 159 |
* one leading '/' the path is volume relative (see filename man |
| 160 |
* page). If there are more than one, we are simply assuming they |
| 161 |
* are superfluous and we trim them away. (An alternative |
| 162 |
* interpretation would be that it is a host name, but we have |
| 163 |
* been documented that that is not the case). |
| 164 |
*/ |
| 165 |
|
| 166 |
*typePtr = TCL_PATH_VOLUME_RELATIVE; |
| 167 |
TclDStringAppendLiteral(resultPtr, "/"); |
| 168 |
return &path[2]; |
| 169 |
} |
| 170 |
SetResultLength(resultPtr, offset, extended); |
| 171 |
share = &host[hlen]; |
| 172 |
|
| 173 |
/* |
| 174 |
* Skip separators. |
| 175 |
*/ |
| 176 |
|
| 177 |
while (share[0] == '/' || share[0] == '\\') { |
| 178 |
share++; |
| 179 |
} |
| 180 |
|
| 181 |
for (slen=0; share[slen]; slen++) { |
| 182 |
if (share[slen] == '/' || share[slen] == '\\') { |
| 183 |
break; |
| 184 |
} |
| 185 |
} |
| 186 |
TclDStringAppendLiteral(resultPtr, "//"); |
| 187 |
Tcl_DStringAppend(resultPtr, host, hlen); |
| 188 |
TclDStringAppendLiteral(resultPtr, "/"); |
| 189 |
Tcl_DStringAppend(resultPtr, share, slen); |
| 190 |
|
| 191 |
tail = &share[slen]; |
| 192 |
|
| 193 |
/* |
| 194 |
* Skip separators. |
| 195 |
*/ |
| 196 |
|
| 197 |
while (tail[0] == '/' || tail[0] == '\\') { |
| 198 |
tail++; |
| 199 |
} |
| 200 |
|
| 201 |
*typePtr = TCL_PATH_ABSOLUTE; |
| 202 |
return tail; |
| 203 |
} else if (*path && path[1] == ':') { |
| 204 |
/* |
| 205 |
* Might be a drive separator. |
| 206 |
*/ |
| 207 |
|
| 208 |
SetResultLength(resultPtr, offset, extended); |
| 209 |
|
| 210 |
if (path[2] != '/' && path[2] != '\\') { |
| 211 |
*typePtr = TCL_PATH_VOLUME_RELATIVE; |
| 212 |
Tcl_DStringAppend(resultPtr, path, 2); |
| 213 |
return &path[2]; |
| 214 |
} else { |
| 215 |
const char *tail = &path[3]; |
| 216 |
|
| 217 |
/* |
| 218 |
* Skip separators. |
| 219 |
*/ |
| 220 |
|
| 221 |
while (*tail && (tail[0] == '/' || tail[0] == '\\')) { |
| 222 |
tail++; |
| 223 |
} |
| 224 |
|
| 225 |
*typePtr = TCL_PATH_ABSOLUTE; |
| 226 |
Tcl_DStringAppend(resultPtr, path, 2); |
| 227 |
TclDStringAppendLiteral(resultPtr, "/"); |
| 228 |
|
| 229 |
return tail; |
| 230 |
} |
| 231 |
} else { |
| 232 |
int abs = 0; |
| 233 |
|
| 234 |
/* |
| 235 |
* Check for Windows devices. |
| 236 |
*/ |
| 237 |
|
| 238 |
if ((path[0] == 'c' || path[0] == 'C') |
| 239 |
&& (path[1] == 'o' || path[1] == 'O')) { |
| 240 |
if ((path[2] == 'm' || path[2] == 'M') |
| 241 |
&& path[3] >= '1' && path[3] <= '9') { |
| 242 |
/* |
| 243 |
* May have match for 'com[1-9]:?', which is a serial port. |
| 244 |
*/ |
| 245 |
|
| 246 |
if (path[4] == '\0') { |
| 247 |
abs = 4; |
| 248 |
} else if (path[4] == ':' && path[5] == '\0') { |
| 249 |
abs = 5; |
| 250 |
} |
| 251 |
|
| 252 |
} else if ((path[2] == 'n' || path[2] == 'N') && path[3] == '\0') { |
| 253 |
/* |
| 254 |
* Have match for 'con'. |
| 255 |
*/ |
| 256 |
|
| 257 |
abs = 3; |
| 258 |
} |
| 259 |
|
| 260 |
} else if ((path[0] == 'l' || path[0] == 'L') |
| 261 |
&& (path[1] == 'p' || path[1] == 'P') |
| 262 |
&& (path[2] == 't' || path[2] == 'T')) { |
| 263 |
if (path[3] >= '1' && path[3] <= '9') { |
| 264 |
/* |
| 265 |
* May have match for 'lpt[1-9]:?' |
| 266 |
*/ |
| 267 |
|
| 268 |
if (path[4] == '\0') { |
| 269 |
abs = 4; |
| 270 |
} else if (path[4] == ':' && path[5] == '\0') { |
| 271 |
abs = 5; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
} else if ((path[0] == 'p' || path[0] == 'P') |
| 276 |
&& (path[1] == 'r' || path[1] == 'R') |
| 277 |
&& (path[2] == 'n' || path[2] == 'N') |
| 278 |
&& path[3] == '\0') { |
| 279 |
/* |
| 280 |
* Have match for 'prn'. |
| 281 |
*/ |
| 282 |
abs = 3; |
| 283 |
|
| 284 |
} else if ((path[0] == 'n' || path[0] == 'N') |
| 285 |
&& (path[1] == 'u' || path[1] == 'U') |
| 286 |
&& (path[2] == 'l' || path[2] == 'L') |
| 287 |
&& path[3] == '\0') { |
| 288 |
/* |
| 289 |
* Have match for 'nul'. |
| 290 |
*/ |
| 291 |
|
| 292 |
abs = 3; |
| 293 |
|
| 294 |
} else if ((path[0] == 'a' || path[0] == 'A') |
| 295 |
&& (path[1] == 'u' || path[1] == 'U') |
| 296 |
&& (path[2] == 'x' || path[2] == 'X') |
| 297 |
&& path[3] == '\0') { |
| 298 |
/* |
| 299 |
* Have match for 'aux'. |
| 300 |
*/ |
| 301 |
|
| 302 |
abs = 3; |
| 303 |
} |
| 304 |
|
| 305 |
if (abs != 0) { |
| 306 |
*typePtr = TCL_PATH_ABSOLUTE; |
| 307 |
SetResultLength(resultPtr, offset, extended); |
| 308 |
Tcl_DStringAppend(resultPtr, path, abs); |
| 309 |
return path + abs; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
/* |
| 314 |
* Anything else is treated as relative. |
| 315 |
*/ |
| 316 |
|
| 317 |
*typePtr = TCL_PATH_RELATIVE; |
| 318 |
return path; |
| 319 |
} |
| 320 |
|
| 321 |
/* |
| 322 |
*---------------------------------------------------------------------- |
| 323 |
* |
| 324 |
* Tcl_GetPathType -- |
| 325 |
* |
| 326 |
* Determines whether a given path is relative to the current directory, |
| 327 |
* relative to the current volume, or absolute. |
| 328 |
* |
| 329 |
* The objectified Tcl_FSGetPathType should be used in preference to this |
| 330 |
* function (as you can see below, this is just a wrapper around that |
| 331 |
* other function). |
| 332 |
* |
| 333 |
* Results: |
| 334 |
* Returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or |
| 335 |
* TCL_PATH_VOLUME_RELATIVE. |
| 336 |
* |
| 337 |
* Side effects: |
| 338 |
* None. |
| 339 |
* |
| 340 |
*---------------------------------------------------------------------- |
| 341 |
*/ |
| 342 |
|
| 343 |
Tcl_PathType |
| 344 |
Tcl_GetPathType( |
| 345 |
const char *path) |
| 346 |
{ |
| 347 |
Tcl_PathType type; |
| 348 |
Tcl_Obj *tempObj = Tcl_NewStringObj(path,-1); |
| 349 |
|
| 350 |
Tcl_IncrRefCount(tempObj); |
| 351 |
type = Tcl_FSGetPathType(tempObj); |
| 352 |
Tcl_DecrRefCount(tempObj); |
| 353 |
return type; |
| 354 |
} |
| 355 |
|
| 356 |
/* |
| 357 |
*---------------------------------------------------------------------- |
| 358 |
* |
| 359 |
* TclpGetNativePathType -- |
| 360 |
* |
| 361 |
* Determines whether a given path is relative to the current directory, |
| 362 |
* relative to the current volume, or absolute, but ONLY FOR THE NATIVE |
| 363 |
* FILESYSTEM. This function is called from tclIOUtil.c (but needs to be |
| 364 |
* here due to its dependence on static variables/functions in this |
| 365 |
* file). The exported function Tcl_FSGetPathType should be used by |
| 366 |
* extensions. |
| 367 |
* |
| 368 |
* Note that '~' paths are always considered TCL_PATH_ABSOLUTE, even |
| 369 |
* though expanding the '~' could lead to any possible path type. This |
| 370 |
* function should therefore be considered a low-level, string |
| 371 |
* manipulation function only -- it doesn't actually do any expansion in |
| 372 |
* making its determination. |
| 373 |
* |
| 374 |
* Results: |
| 375 |
* Returns one of TCL_PATH_ABSOLUTE, TCL_PATH_RELATIVE, or |
| 376 |
* TCL_PATH_VOLUME_RELATIVE. |
| 377 |
* |
| 378 |
* Side effects: |
| 379 |
* None. |
| 380 |
* |
| 381 |
*---------------------------------------------------------------------- |
| 382 |
*/ |
| 383 |
|
| 384 |
Tcl_PathType |
| 385 |
TclpGetNativePathType( |
| 386 |
Tcl_Obj *pathPtr, /* Native path of interest */ |
| 387 |
int *driveNameLengthPtr, /* Returns length of drive, if non-NULL and |
| 388 |
* path was absolute */ |
| 389 |
Tcl_Obj **driveNameRef) |
| 390 |
{ |
| 391 |
Tcl_PathType type = TCL_PATH_ABSOLUTE; |
| 392 |
int pathLen; |
| 393 |
const char *path = TclGetStringFromObj(pathPtr, &pathLen); |
| 394 |
|
| 395 |
if (path[0] == '~') { |
| 396 |
/* |
| 397 |
* This case is common to all platforms. Paths that begin with ~ are |
| 398 |
* absolute. |
| 399 |
*/ |
| 400 |
|
| 401 |
if (driveNameLengthPtr != NULL) { |
| 402 |
const char *end = path + 1; |
| 403 |
while ((*end != '\0') && (*end != '/')) { |
| 404 |
end++; |
| 405 |
} |
| 406 |
*driveNameLengthPtr = end - path; |
| 407 |
} |
| 408 |
} else { |
| 409 |
switch (tclPlatform) { |
| 410 |
case TCL_PLATFORM_UNIX: { |
| 411 |
const char *origPath = path; |
| 412 |
|
| 413 |
/* |
| 414 |
* Paths that begin with / are absolute. |
| 415 |
*/ |
| 416 |
|
| 417 |
if (path[0] == '/') { |
| 418 |
++path; |
| 419 |
#if defined(__CYGWIN__) || defined(__QNX__) |
| 420 |
/* |
| 421 |
* Check for "//" network path prefix |
| 422 |
*/ |
| 423 |
if ((*path == '/') && path[1] && (path[1] != '/')) { |
| 424 |
path += 2; |
| 425 |
while (*path && *path != '/') { |
| 426 |
++path; |
| 427 |
} |
| 428 |
} |
| 429 |
#endif |
| 430 |
if (driveNameLengthPtr != NULL) { |
| 431 |
/* |
| 432 |
* We need this addition in case the "//" code was used. |
| 433 |
*/ |
| 434 |
|
| 435 |
*driveNameLengthPtr = (path - origPath); |
| 436 |
} |
| 437 |
} else { |
| 438 |
type = TCL_PATH_RELATIVE; |
| 439 |
} |
| 440 |
break; |
| 441 |
} |
| 442 |
case TCL_PLATFORM_WINDOWS: { |
| 443 |
Tcl_DString ds; |
| 444 |
const char *rootEnd; |
| 445 |
|
| 446 |
Tcl_DStringInit(&ds); |
| 447 |
rootEnd = ExtractWinRoot(path, &ds, 0, &type); |
| 448 |
if ((rootEnd != path) && (driveNameLengthPtr != NULL)) { |
| 449 |
*driveNameLengthPtr = rootEnd - path; |
| 450 |
if (driveNameRef != NULL) { |
| 451 |
*driveNameRef = TclDStringToObj(&ds); |
| 452 |
Tcl_IncrRefCount(*driveNameRef); |
| 453 |
} |
| 454 |
} |
| 455 |
Tcl_DStringFree(&ds); |
| 456 |
break; |
| 457 |
} |
| 458 |
} |
| 459 |
} |
| 460 |
return type; |
| 461 |
} |
| 462 |
|
| 463 |
/* |
| 464 |
*--------------------------------------------------------------------------- |
| 465 |
* |
| 466 |
* TclpNativeSplitPath -- |
| 467 |
* |
| 468 |
* This function takes the given Tcl_Obj, which should be a valid path, |
| 469 |
* and returns a Tcl List object containing each segment of that path as |
| 470 |
* an element. |
| 471 |
* |
| 472 |
* Note this function currently calls the older Split(Plat)Path |
| 473 |
* functions, which require more memory allocation than is desirable. |
| 474 |
* |
| 475 |
* Results: |
| 476 |
* Returns list object with refCount of zero. If the passed in lenPtr is |
| 477 |
* non-NULL, we use it to return the number of elements in the returned |
| 478 |
* list. |
| 479 |
* |
| 480 |
* Side effects: |
| 481 |
* None. |
| 482 |
* |
| 483 |
*--------------------------------------------------------------------------- |
| 484 |
*/ |
| 485 |
|
| 486 |
Tcl_Obj * |
| 487 |
TclpNativeSplitPath( |
| 488 |
Tcl_Obj *pathPtr, /* Path to split. */ |
| 489 |
int *lenPtr) /* int to store number of path elements. */ |
| 490 |
{ |
| 491 |
Tcl_Obj *resultPtr = NULL; /* Needed only to prevent gcc warnings. */ |
| 492 |
|
| 493 |
/* |
| 494 |
* Perform platform specific splitting. |
| 495 |
*/ |
| 496 |
|
| 497 |
switch (tclPlatform) { |
| 498 |
case TCL_PLATFORM_UNIX: |
| 499 |
resultPtr = SplitUnixPath(TclGetString(pathPtr)); |
| 500 |
break; |
| 501 |
|
| 502 |
case TCL_PLATFORM_WINDOWS: |
| 503 |
resultPtr = SplitWinPath(TclGetString(pathPtr)); |
| 504 |
break; |
| 505 |
} |
| 506 |
|
| 507 |
/* |
| 508 |
* Compute the number of elements in the result. |
| 509 |
*/ |
| 510 |
|
| 511 |
if (lenPtr != NULL) { |
| 512 |
TclListObjLength(NULL, resultPtr, lenPtr); |
| 513 |
} |
| 514 |
return resultPtr; |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
*---------------------------------------------------------------------- |
| 519 |
* |
| 520 |
* Tcl_SplitPath -- |
| 521 |
* |
| 522 |
* Split a path into a list of path components. The first element of the |
| 523 |
* list will have the same path type as the original path. |
| 524 |
* |
| 525 |
* Results: |
| 526 |
* Returns a standard Tcl result. The interpreter result contains a list |
| 527 |
* of path components. *argvPtr will be filled in with the address of an |
| 528 |
* array whose elements point to the elements of path, in order. |
| 529 |
* *argcPtr will get filled in with the number of valid elements in the |
| 530 |
* array. A single block of memory is dynamically allocated to hold both |
| 531 |
* the argv array and a copy of the path elements. The caller must |
| 532 |
* eventually free this memory by calling ckfree() on *argvPtr. Note: |
| 533 |
* *argvPtr and *argcPtr are only modified if the procedure returns |
| 534 |
* normally. |
| 535 |
* |
| 536 |
* Side effects: |
| 537 |
* Allocates memory. |
| 538 |
* |
| 539 |
*---------------------------------------------------------------------- |
| 540 |
*/ |
| 541 |
|
| 542 |
void |
| 543 |
Tcl_SplitPath( |
| 544 |
const char *path, /* Pointer to string containing a path. */ |
| 545 |
int *argcPtr, /* Pointer to location to fill in with the |
| 546 |
* number of elements in the path. */ |
| 547 |
const char ***argvPtr) /* Pointer to place to store pointer to array |
| 548 |
* of pointers to path elements. */ |
| 549 |
{ |
| 550 |
Tcl_Obj *resultPtr = NULL; /* Needed only to prevent gcc warnings. */ |
| 551 |
Tcl_Obj *tmpPtr, *eltPtr; |
| 552 |
int i, size, len; |
| 553 |
char *p; |
| 554 |
const char *str; |
| 555 |
|
| 556 |
/* |
| 557 |
* Perform the splitting, using objectified, vfs-aware code. |
| 558 |
*/ |
| 559 |
|
| 560 |
tmpPtr = Tcl_NewStringObj(path, -1); |
| 561 |
Tcl_IncrRefCount(tmpPtr); |
| 562 |
resultPtr = Tcl_FSSplitPath(tmpPtr, argcPtr); |
| 563 |
Tcl_IncrRefCount(resultPtr); |
| 564 |
Tcl_DecrRefCount(tmpPtr); |
| 565 |
|
| 566 |
/* |
| 567 |
* Calculate space required for the result. |
| 568 |
*/ |
| 569 |
|
| 570 |
size = 1; |
| 571 |
for (i = 0; i < *argcPtr; i++) { |
| 572 |
Tcl_ListObjIndex(NULL, resultPtr, i, &eltPtr); |
| 573 |
TclGetStringFromObj(eltPtr, &len); |
| 574 |
size += len + 1; |
| 575 |
} |
| 576 |
|
| 577 |
/* |
| 578 |
* Allocate a buffer large enough to hold the contents of all of the list |
| 579 |
* plus the argv pointers and the terminating NULL pointer. |
| 580 |
*/ |
| 581 |
|
| 582 |
*argvPtr = (const char **)ckalloc( |
| 583 |
((((*argcPtr) + 1) * sizeof(char *)) + size)); |
| 584 |
|
| 585 |
/* |
| 586 |
* Position p after the last argv pointer and copy the contents of the |
| 587 |
* list in, piece by piece. |
| 588 |
*/ |
| 589 |
|
| 590 |
p = (char *) &(*argvPtr)[(*argcPtr) + 1]; |
| 591 |
for (i = 0; i < *argcPtr; i++) { |
| 592 |
Tcl_ListObjIndex(NULL, resultPtr, i, &eltPtr); |
| 593 |
str = TclGetStringFromObj(eltPtr, &len); |
| 594 |
memcpy(p, str, len + 1); |
| 595 |
p += len+1; |
| 596 |
} |
| 597 |
|
| 598 |
/* |
| 599 |
* Now set up the argv pointers. |
| 600 |
*/ |
| 601 |
|
| 602 |
p = (char *) &(*argvPtr)[(*argcPtr) + 1]; |
| 603 |
|
| 604 |
for (i = 0; i < *argcPtr; i++) { |
| 605 |
(*argvPtr)[i] = p; |
| 606 |
for (; *(p++)!='\0'; ); |
| 607 |
} |
| 608 |
(*argvPtr)[i] = NULL; |
| 609 |
|
| 610 |
/* |
| 611 |
* Free the result ptr given to us by Tcl_FSSplitPath |
| 612 |
*/ |
| 613 |
|
| 614 |
Tcl_DecrRefCount(resultPtr); |
| 615 |
} |
| 616 |
|
| 617 |
/* |
| 618 |
*---------------------------------------------------------------------- |
| 619 |
* |
| 620 |
* SplitUnixPath -- |
| 621 |
* |
| 622 |
* This routine is used by Tcl_(FS)SplitPath to handle splitting Unix |
| 623 |
* paths. |
| 624 |
* |
| 625 |
* Results: |
| 626 |
* Returns a newly allocated Tcl list object. |
| 627 |
* |
| 628 |
* Side effects: |
| 629 |
* None. |
| 630 |
* |
| 631 |
*---------------------------------------------------------------------- |
| 632 |
*/ |
| 633 |
|
| 634 |
static Tcl_Obj * |
| 635 |
SplitUnixPath( |
| 636 |
const char *path) /* Pointer to string containing a path. */ |
| 637 |
{ |
| 638 |
int length; |
| 639 |
const char *origPath = path, *elementStart; |
| 640 |
Tcl_Obj *result; |
| 641 |
|
| 642 |
/* |
| 643 |
* Deal with the root directory as a special case. |
| 644 |
*/ |
| 645 |
|
| 646 |
TclNewObj(result); |
| 647 |
if (*path == '/') { |
| 648 |
Tcl_Obj *rootElt; |
| 649 |
++path; |
| 650 |
#if defined(__CYGWIN__) || defined(__QNX__) |
| 651 |
/* |
| 652 |
* Check for "//" network path prefix |
| 653 |
*/ |
| 654 |
if ((*path == '/') && path[1] && (path[1] != '/')) { |
| 655 |
path += 2; |
| 656 |
while (*path && *path != '/') { |
| 657 |
++path; |
| 658 |
} |
| 659 |
} |
| 660 |
#endif |
| 661 |
rootElt = Tcl_NewStringObj(origPath, path - origPath); |
| 662 |
Tcl_ListObjAppendElement(NULL, result, rootElt); |
| 663 |
while (*path == '/') { |
| 664 |
++path; |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
/* |
| 669 |
* Split on slashes. Embedded elements that start with tilde will be |
| 670 |
* prefixed with "./" so they are not affected by tilde substitution. |
| 671 |
*/ |
| 672 |
|
| 673 |
for (;;) { |
| 674 |
elementStart = path; |
| 675 |
while ((*path != '\0') && (*path != '/')) { |
| 676 |
path++; |
| 677 |
} |
| 678 |
length = path - elementStart; |
| 679 |
if (length > 0) { |
| 680 |
Tcl_Obj *nextElt; |
| 681 |
if ((elementStart[0] == '~') && (elementStart != origPath)) { |
| 682 |
TclNewLiteralStringObj(nextElt, "./"); |
| 683 |
Tcl_AppendToObj(nextElt, elementStart, length); |
| 684 |
} else { |
| 685 |
nextElt = Tcl_NewStringObj(elementStart, length); |
| 686 |
} |
| 687 |
Tcl_ListObjAppendElement(NULL, result, nextElt); |
| 688 |
} |
| 689 |
if (*path++ == '\0') { |
| 690 |
break; |
| 691 |
} |
| 692 |
} |
| 693 |
return result; |
| 694 |
} |
| 695 |
|
| 696 |
/* |
| 697 |
*---------------------------------------------------------------------- |
| 698 |
* |
| 699 |
* SplitWinPath -- |
| 700 |
* |
| 701 |
* This routine is used by Tcl_(FS)SplitPath to handle splitting Windows |
| 702 |
* paths. |
| 703 |
* |
| 704 |
* Results: |
| 705 |
* Returns a newly allocated Tcl list object. |
| 706 |
* |
| 707 |
* Side effects: |
| 708 |
* None. |
| 709 |
* |
| 710 |
*---------------------------------------------------------------------- |
| 711 |
*/ |
| 712 |
|
| 713 |
static Tcl_Obj * |
| 714 |
SplitWinPath( |
| 715 |
const char *path) /* Pointer to string containing a path. */ |
| 716 |
{ |
| 717 |
int length; |
| 718 |
const char *p, *elementStart; |
| 719 |
Tcl_PathType type = TCL_PATH_ABSOLUTE; |
| 720 |
Tcl_DString buf; |
| 721 |
Tcl_Obj *result; |
| 722 |
Tcl_DStringInit(&buf); |
| 723 |
|
| 724 |
TclNewObj(result); |
| 725 |
p = ExtractWinRoot(path, &buf, 0, &type); |
| 726 |
|
| 727 |
/* |
| 728 |
* Terminate the root portion, if we matched something. |
| 729 |
*/ |
| 730 |
|
| 731 |
if (p != path) { |
| 732 |
Tcl_ListObjAppendElement(NULL, result, TclDStringToObj(&buf)); |
| 733 |
} |
| 734 |
Tcl_DStringFree(&buf); |
| 735 |
|
| 736 |
/* |
| 737 |
* Split on slashes. Embedded elements that start with tilde or a drive |
| 738 |
* letter will be prefixed with "./" so they are not affected by tilde |
| 739 |
* substitution. |
| 740 |
*/ |
| 741 |
|
| 742 |
do { |
| 743 |
elementStart = p; |
| 744 |
while ((*p != '\0') && (*p != '/') && (*p != '\\')) { |
| 745 |
p++; |
| 746 |
} |
| 747 |
length = p - elementStart; |
| 748 |
if (length > 0) { |
| 749 |
Tcl_Obj *nextElt; |
| 750 |
if ((elementStart != path) && ((elementStart[0] == '~') |
| 751 |
|| (isalpha(UCHAR(elementStart[0])) |
| 752 |
&& elementStart[1] == ':'))) { |
| 753 |
TclNewLiteralStringObj(nextElt, "./"); |
| 754 |
Tcl_AppendToObj(nextElt, elementStart, length); |
| 755 |
} else { |
| 756 |
nextElt = Tcl_NewStringObj(elementStart, length); |
| 757 |
} |
| 758 |
Tcl_ListObjAppendElement(NULL, result, nextElt); |
| 759 |
} |
| 760 |
} while (*p++ != '\0'); |
| 761 |
|
| 762 |
return result; |
| 763 |
} |
| 764 |
|
| 765 |
/* |
| 766 |
*--------------------------------------------------------------------------- |
| 767 |
* |
| 768 |
* Tcl_FSJoinToPath -- |
| 769 |
* |
| 770 |
* This function takes the given object, which should usually be a valid |
| 771 |
* path or NULL, and joins onto it the array of paths segments given. |
| 772 |
* |
| 773 |
* The objects in the array given will temporarily have their refCount |
| 774 |
* increased by one, and then decreased by one when this function exits |
| 775 |
* (which means if they had zero refCount when we were called, they will |
| 776 |
* be freed). |
| 777 |
* |
| 778 |
* Results: |
| 779 |
* Returns object owned by the caller (which should increment its |
| 780 |
* refCount) - typically an object with refCount of zero. |
| 781 |
* |
| 782 |
* Side effects: |
| 783 |
* None. |
| 784 |
* |
| 785 |
*--------------------------------------------------------------------------- |
| 786 |
*/ |
| 787 |
|
| 788 |
Tcl_Obj * |
| 789 |
Tcl_FSJoinToPath( |
| 790 |
Tcl_Obj *pathPtr, /* Valid path or NULL. */ |
| 791 |
int objc, /* Number of array elements to join */ |
| 792 |
Tcl_Obj *const objv[]) /* Path elements to join. */ |
| 793 |
{ |
| 794 |
if (pathPtr == NULL) { |
| 795 |
return TclJoinPath(objc, objv, 0); |
| 796 |
} |
| 797 |
if (objc == 0) { |
| 798 |
return TclJoinPath(1, &pathPtr, 0); |
| 799 |
} |
| 800 |
if (objc == 1) { |
| 801 |
Tcl_Obj *pair[2]; |
| 802 |
|
| 803 |
pair[0] = pathPtr; |
| 804 |
pair[1] = objv[0]; |
| 805 |
return TclJoinPath(2, pair, 0); |
| 806 |
} else { |
| 807 |
int elemc = objc + 1; |
| 808 |
Tcl_Obj *ret, **elemv = (Tcl_Obj**)ckalloc(elemc*sizeof(Tcl_Obj *)); |
| 809 |
|
| 810 |
elemv[0] = pathPtr; |
| 811 |
memcpy(elemv+1, objv, objc*sizeof(Tcl_Obj *)); |
| 812 |
ret = TclJoinPath(elemc, elemv, 0); |
| 813 |
ckfree(elemv); |
| 814 |
return ret; |
| 815 |
} |
| 816 |
} |
| 817 |
|
| 818 |
/* |
| 819 |
*--------------------------------------------------------------------------- |
| 820 |
* |
| 821 |
* TclpNativeJoinPath -- |
| 822 |
* |
| 823 |
* 'prefix' is absolute, 'joining' is relative to prefix. |
| 824 |
* |
| 825 |
* Results: |
| 826 |
* modifies prefix |
| 827 |
* |
| 828 |
* Side effects: |
| 829 |
* None. |
| 830 |
* |
| 831 |
*--------------------------------------------------------------------------- |
| 832 |
*/ |
| 833 |
|
| 834 |
void |
| 835 |
TclpNativeJoinPath( |
| 836 |
Tcl_Obj *prefix, |
| 837 |
const char *joining) |
| 838 |
{ |
| 839 |
int length, needsSep; |
| 840 |
char *dest; |
| 841 |
const char *p; |
| 842 |
const char *start; |
| 843 |
|
| 844 |
start = TclGetStringFromObj(prefix, &length); |
| 845 |
|
| 846 |
/* |
| 847 |
* Remove the ./ from tilde prefixed elements, and drive-letter prefixed |
| 848 |
* elements on Windows, unless it is the first component. |
| 849 |
*/ |
| 850 |
|
| 851 |
p = joining; |
| 852 |
|
| 853 |
if (length != 0) { |
| 854 |
if ((p[0] == '.') && (p[1] == '/') && ((p[2] == '~') |
| 855 |
|| (tclPlatform==TCL_PLATFORM_WINDOWS && isalpha(UCHAR(p[2])) |
| 856 |
&& (p[3] == ':')))) { |
| 857 |
p += 2; |
| 858 |
} |
| 859 |
} |
| 860 |
if (*p == '\0') { |
| 861 |
return; |
| 862 |
} |
| 863 |
|
| 864 |
switch (tclPlatform) { |
| 865 |
case TCL_PLATFORM_UNIX: |
| 866 |
/* |
| 867 |
* Append a separator if needed. |
| 868 |
*/ |
| 869 |
|
| 870 |
if (length > 0 && (start[length-1] != '/')) { |
| 871 |
Tcl_AppendToObj(prefix, "/", 1); |
| 872 |
TclGetStringFromObj(prefix, &length); |
| 873 |
} |
| 874 |
needsSep = 0; |
| 875 |
|
| 876 |
/* |
| 877 |
* Append the element, eliminating duplicate and trailing slashes. |
| 878 |
*/ |
| 879 |
|
| 880 |
Tcl_SetObjLength(prefix, length + (int) strlen(p)); |
| 881 |
|
| 882 |
dest = TclGetString(prefix) + length; |
| 883 |
for (; *p != '\0'; p++) { |
| 884 |
if (*p == '/') { |
| 885 |
while (p[1] == '/') { |
| 886 |
p++; |
| 887 |
} |
| 888 |
if (p[1] != '\0' && needsSep) { |
| 889 |
*dest++ = '/'; |
| 890 |
} |
| 891 |
} else { |
| 892 |
*dest++ = *p; |
| 893 |
needsSep = 1; |
| 894 |
} |
| 895 |
} |
| 896 |
length = dest - TclGetString(prefix); |
| 897 |
Tcl_SetObjLength(prefix, length); |
| 898 |
break; |
| 899 |
|
| 900 |
case TCL_PLATFORM_WINDOWS: |
| 901 |
/* |
| 902 |
* Check to see if we need to append a separator. |
| 903 |
*/ |
| 904 |
|
| 905 |
if ((length > 0) && |
| 906 |
(start[length-1] != '/') && (start[length-1] != ':')) { |
| 907 |
Tcl_AppendToObj(prefix, "/", 1); |
| 908 |
TclGetStringFromObj(prefix, &length); |
| 909 |
} |
| 910 |
needsSep = 0; |
| 911 |
|
| 912 |
/* |
| 913 |
* Append the element, eliminating duplicate and trailing slashes. |
| 914 |
*/ |
| 915 |
|
| 916 |
Tcl_SetObjLength(prefix, length + (int) strlen(p)); |
| 917 |
dest = TclGetString(prefix) + length; |
| 918 |
for (; *p != '\0'; p++) { |
| 919 |
if ((*p == '/') || (*p == '\\')) { |
| 920 |
while ((p[1] == '/') || (p[1] == '\\')) { |
| 921 |
p++; |
| 922 |
} |
| 923 |
if ((p[1] != '\0') && needsSep) { |
| 924 |
*dest++ = '/'; |
| 925 |
} |
| 926 |
} else { |
| 927 |
*dest++ = *p; |
| 928 |
needsSep = 1; |
| 929 |
} |
| 930 |
} |
| 931 |
length = dest - TclGetString(prefix); |
| 932 |
Tcl_SetObjLength(prefix, length); |
| 933 |
break; |
| 934 |
} |
| 935 |
return; |
| 936 |
} |
| 937 |
|
| 938 |
/* |
| 939 |
*---------------------------------------------------------------------- |
| 940 |
* |
| 941 |
* Tcl_JoinPath -- |
| 942 |
* |
| 943 |
* Combine a list of paths in a platform specific manner. The function |
| 944 |
* 'Tcl_FSJoinPath' should be used in preference where possible. |
| 945 |
* |
| 946 |
* Results: |
| 947 |
* Appends the joined path to the end of the specified Tcl_DString |
| 948 |
* returning a pointer to the resulting string. Note that the |
| 949 |
* Tcl_DString must already be initialized. |
| 950 |
* |
| 951 |
* Side effects: |
| 952 |
* Modifies the Tcl_DString. |
| 953 |
* |
| 954 |
*---------------------------------------------------------------------- |
| 955 |
*/ |
| 956 |
|
| 957 |
char * |
| 958 |
Tcl_JoinPath( |
| 959 |
int argc, |
| 960 |
const char *const *argv, |
| 961 |
Tcl_DString *resultPtr) /* Pointer to previously initialized DString */ |
| 962 |
{ |
| 963 |
int i, len; |
| 964 |
Tcl_Obj *listObj; |
| 965 |
Tcl_Obj *resultObj; |
| 966 |
const char *resultStr; |
| 967 |
|
| 968 |
/* |
| 969 |
* Build the list of paths. |
| 970 |
*/ |
| 971 |
|
| 972 |
TclNewObj(listObj); |
| 973 |
for (i = 0; i < argc; i++) { |
| 974 |
Tcl_ListObjAppendElement(NULL, listObj, |
| 975 |
Tcl_NewStringObj(argv[i], -1)); |
| 976 |
} |
| 977 |
|
| 978 |
/* |
| 979 |
* Ask the objectified code to join the paths. |
| 980 |
*/ |
| 981 |
|
| 982 |
Tcl_IncrRefCount(listObj); |
| 983 |
resultObj = Tcl_FSJoinPath(listObj, argc); |
| 984 |
Tcl_IncrRefCount(resultObj); |
| 985 |
Tcl_DecrRefCount(listObj); |
| 986 |
|
| 987 |
/* |
| 988 |
* Store the result. |
| 989 |
*/ |
| 990 |
|
| 991 |
resultStr = TclGetStringFromObj(resultObj, &len); |
| 992 |
Tcl_DStringAppend(resultPtr, resultStr, len); |
| 993 |
Tcl_DecrRefCount(resultObj); |
| 994 |
|
| 995 |
/* |
| 996 |
* Return a pointer to the result. |
| 997 |
*/ |
| 998 |
|
| 999 |
return Tcl_DStringValue(resultPtr); |
| 1000 |
} |
| 1001 |
|
| 1002 |
/* |
| 1003 |
*--------------------------------------------------------------------------- |
| 1004 |
* |
| 1005 |
* Tcl_TranslateFileName -- |
| 1006 |
* |
| 1007 |
* Converts a file name into a form usable by the native system |
| 1008 |
* interfaces. If the name starts with a tilde, it will produce a name |
| 1009 |
* where the tilde and following characters have been replaced by the |
| 1010 |
* home directory location for the named user. |
| 1011 |
* |
| 1012 |
* Results: |
| 1013 |
* The return value is a pointer to a string containing the name after |
| 1014 |
* tilde substitution. If there was no tilde substitution, the return |
| 1015 |
* value is a pointer to a copy of the original string. If there was an |
| 1016 |
* error in processing the name, then an error message is left in the |
| 1017 |
* interp's result (if interp was not NULL) and the return value is NULL. |
| 1018 |
* Space for the return value is allocated in bufferPtr; the caller must |
| 1019 |
* call Tcl_DStringFree() to free the space if the return value was not |
| 1020 |
* NULL. |
| 1021 |
* |
| 1022 |
* Side effects: |
| 1023 |
* None. |
| 1024 |
* |
| 1025 |
*---------------------------------------------------------------------- |
| 1026 |
*/ |
| 1027 |
|
| 1028 |
char * |
| 1029 |
Tcl_TranslateFileName( |
| 1030 |
Tcl_Interp *interp, /* Interpreter in which to store error message |
| 1031 |
* (if necessary). */ |
| 1032 |
const char *name, /* File name, which may begin with "~" (to |
| 1033 |
* indicate current user's home directory) or |
| 1034 |
* "~<user>" (to indicate any user's home |
| 1035 |
* directory). */ |
| 1036 |
Tcl_DString *bufferPtr) /* Uninitialized or free DString filled with |
| 1037 |
* name after tilde substitution. */ |
| 1038 |
{ |
| 1039 |
Tcl_Obj *path = Tcl_NewStringObj(name, -1); |
| 1040 |
Tcl_Obj *transPtr; |
| 1041 |
|
| 1042 |
Tcl_IncrRefCount(path); |
| 1043 |
transPtr = Tcl_FSGetTranslatedPath(interp, path); |
| 1044 |
if (transPtr == NULL) { |
| 1045 |
Tcl_DecrRefCount(path); |
| 1046 |
return NULL; |
| 1047 |
} |
| 1048 |
|
| 1049 |
Tcl_DStringInit(bufferPtr); |
| 1050 |
TclDStringAppendObj(bufferPtr, transPtr); |
| 1051 |
Tcl_DecrRefCount(path); |
| 1052 |
Tcl_DecrRefCount(transPtr); |
| 1053 |
|
| 1054 |
/* |
| 1055 |
* Convert forward slashes to backslashes in Windows paths because some |
| 1056 |
* system interfaces don't accept forward slashes. |
| 1057 |
*/ |
| 1058 |
|
| 1059 |
if (tclPlatform == TCL_PLATFORM_WINDOWS) { |
| 1060 |
char *p; |
| 1061 |
for (p = Tcl_DStringValue(bufferPtr); *p != '\0'; p++) { |
| 1062 |
if (*p == '/') { |
| 1063 |
*p = '\\'; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
|
| 1068 |
return Tcl_DStringValue(bufferPtr); |
| 1069 |
} |
| 1070 |
|
| 1071 |
/* |
| 1072 |
*---------------------------------------------------------------------- |
| 1073 |
* |
| 1074 |
* TclGetExtension -- |
| 1075 |
* |
| 1076 |
* This function returns a pointer to the beginning of the extension part |
| 1077 |
* of a file name. |
| 1078 |
* |
| 1079 |
* Results: |
| 1080 |
* Returns a pointer into name which indicates where the extension |
| 1081 |
* starts. If there is no extension, returns NULL. |
| 1082 |
* |
| 1083 |
* Side effects: |
| 1084 |
* None. |
| 1085 |
* |
| 1086 |
*---------------------------------------------------------------------- |
| 1087 |
*/ |
| 1088 |
|
| 1089 |
const char * |
| 1090 |
TclGetExtension( |
| 1091 |
const char *name) /* File name to parse. */ |
| 1092 |
{ |
| 1093 |
const char *p, *lastSep; |
| 1094 |
|
| 1095 |
/* |
| 1096 |
* First find the last directory separator. |
| 1097 |
*/ |
| 1098 |
|
| 1099 |
lastSep = NULL; /* Needed only to prevent gcc warnings. */ |
| 1100 |
switch (tclPlatform) { |
| 1101 |
case TCL_PLATFORM_UNIX: |
| 1102 |
lastSep = strrchr(name, '/'); |
| 1103 |
break; |
| 1104 |
|
| 1105 |
case TCL_PLATFORM_WINDOWS: |
| 1106 |
lastSep = NULL; |
| 1107 |
for (p = name; *p != '\0'; p++) { |
| 1108 |
if (strchr("/\\:", *p) != NULL) { |
| 1109 |
lastSep = p; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
break; |
| 1113 |
} |
| 1114 |
p = strrchr(name, '.'); |
| 1115 |
if ((p != NULL) && (lastSep != NULL) && (lastSep > p)) { |
| 1116 |
p = NULL; |
| 1117 |
} |
| 1118 |
|
| 1119 |
/* |
| 1120 |
* In earlier versions, we used to back up to the first period in a series |
| 1121 |
* so that "foo..o" would be split into "foo" and "..o". This is a |
| 1122 |
* confusing and usually incorrect behavior, so now we split at the last |
| 1123 |
* period in the name. |
| 1124 |
*/ |
| 1125 |
|
| 1126 |
return p; |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* |
| 1130 |
*---------------------------------------------------------------------- |
| 1131 |
* |
| 1132 |
* DoTildeSubst -- |
| 1133 |
* |
| 1134 |
* Given a string following a tilde, this routine returns the |
| 1135 |
* corresponding home directory. |
| 1136 |
* |
| 1137 |
* Results: |
| 1138 |
* The result is a pointer to a static string containing the home |
| 1139 |
* directory in native format. If there was an error in processing the |
| 1140 |
* substitution, then an error message is left in the interp's result and |
| 1141 |
* the return value is NULL. On success, the results are appended to |
| 1142 |
* resultPtr, and the contents of resultPtr are returned. |
| 1143 |
* |
| 1144 |
* Side effects: |
| 1145 |
* Information may be left in resultPtr. |
| 1146 |
* |
| 1147 |
*---------------------------------------------------------------------- |
| 1148 |
*/ |
| 1149 |
|
| 1150 |
static const char * |
| 1151 |
DoTildeSubst( |
| 1152 |
Tcl_Interp *interp, /* Interpreter in which to store error message |
| 1153 |
* (if necessary). */ |
| 1154 |
const char *user, /* Name of user whose home directory should be |
| 1155 |
* substituted, or "" for current user. */ |
| 1156 |
Tcl_DString *resultPtr) /* Initialized DString filled with name after |
| 1157 |
* tilde substitution. */ |
| 1158 |
{ |
| 1159 |
const char *dir; |
| 1160 |
|
| 1161 |
if (*user == '\0') { |
| 1162 |
Tcl_DString dirString; |
| 1163 |
|
| 1164 |
dir = TclGetEnv("HOME", &dirString); |
| 1165 |
if (dir == NULL) { |
| 1166 |
if (interp) { |
| 1167 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1168 |
"couldn't find HOME environment " |
| 1169 |
"variable to expand path", -1)); |
| 1170 |
Tcl_SetErrorCode(interp, "TCL", "FILENAME", "NO_HOME", (char *)NULL); |
| 1171 |
} |
| 1172 |
return NULL; |
| 1173 |
} |
| 1174 |
Tcl_JoinPath(1, &dir, resultPtr); |
| 1175 |
Tcl_DStringFree(&dirString); |
| 1176 |
} else if (TclpGetUserHome(user, resultPtr) == NULL) { |
| 1177 |
if (interp) { |
| 1178 |
Tcl_ResetResult(interp); |
| 1179 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1180 |
"user \"%s\" doesn't exist", user)); |
| 1181 |
Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "USER", user, (char *)NULL); |
| 1182 |
} |
| 1183 |
return NULL; |
| 1184 |
} |
| 1185 |
return Tcl_DStringValue(resultPtr); |
| 1186 |
} |
| 1187 |
|
| 1188 |
/* |
| 1189 |
*---------------------------------------------------------------------- |
| 1190 |
* |
| 1191 |
* Tcl_GlobObjCmd -- |
| 1192 |
* |
| 1193 |
* This procedure is invoked to process the "glob" Tcl command. See the |
| 1194 |
* user documentation for details on what it does. |
| 1195 |
* |
| 1196 |
* Results: |
| 1197 |
* A standard Tcl result. |
| 1198 |
* |
| 1199 |
* Side effects: |
| 1200 |
* See the user documentation. |
| 1201 |
* |
| 1202 |
*---------------------------------------------------------------------- |
| 1203 |
*/ |
| 1204 |
|
| 1205 |
int |
| 1206 |
Tcl_GlobObjCmd( |
| 1207 |
ClientData dummy, /* Not used. */ |
| 1208 |
Tcl_Interp *interp, /* Current interpreter. */ |
| 1209 |
int objc, /* Number of arguments. */ |
| 1210 |
Tcl_Obj *const objv[]) /* Argument objects. */ |
| 1211 |
{ |
| 1212 |
int index, i, globFlags, length, join, dir, result; |
| 1213 |
char *string; |
| 1214 |
const char *separators; |
| 1215 |
Tcl_Obj *typePtr, *look; |
| 1216 |
Tcl_Obj *pathOrDir = NULL; |
| 1217 |
Tcl_DString prefix; |
| 1218 |
static const char *const options[] = { |
| 1219 |
"-directory", "-join", "-nocomplain", "-path", "-tails", |
| 1220 |
"-types", "--", NULL |
| 1221 |
}; |
| 1222 |
enum globOptionsEnum { |
| 1223 |
GLOB_DIR, GLOB_JOIN, GLOB_NOCOMPLAIN, GLOB_PATH, GLOB_TAILS, |
| 1224 |
GLOB_TYPE, GLOB_LAST |
| 1225 |
}; |
| 1226 |
enum pathDirOptions {PATH_NONE = -1 , PATH_GENERAL = 0, PATH_DIR = 1}; |
| 1227 |
Tcl_GlobTypeData *globTypes = NULL; |
| 1228 |
(void)dummy; |
| 1229 |
|
| 1230 |
globFlags = 0; |
| 1231 |
join = 0; |
| 1232 |
dir = PATH_NONE; |
| 1233 |
typePtr = NULL; |
| 1234 |
for (i = 1; i < objc; i++) { |
| 1235 |
if (Tcl_GetIndexFromObj(interp, objv[i], options, "option", 0, |
| 1236 |
&index) != TCL_OK) { |
| 1237 |
string = TclGetStringFromObj(objv[i], &length); |
| 1238 |
if (string[0] == '-') { |
| 1239 |
/* |
| 1240 |
* It looks like the command contains an option so signal an |
| 1241 |
* error. |
| 1242 |
*/ |
| 1243 |
|
| 1244 |
return TCL_ERROR; |
| 1245 |
} else { |
| 1246 |
/* |
| 1247 |
* This clearly isn't an option; assume it's the first glob |
| 1248 |
* pattern. We must clear the error. |
| 1249 |
*/ |
| 1250 |
|
| 1251 |
Tcl_ResetResult(interp); |
| 1252 |
break; |
| 1253 |
} |
| 1254 |
} |
| 1255 |
|
| 1256 |
switch ((enum globOptionsEnum) index) { |
| 1257 |
case GLOB_NOCOMPLAIN: /* -nocomplain */ |
| 1258 |
globFlags |= TCL_GLOBMODE_NO_COMPLAIN; |
| 1259 |
break; |
| 1260 |
case GLOB_DIR: /* -dir */ |
| 1261 |
if (i == (objc-1)) { |
| 1262 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1263 |
"missing argument to \"-directory\"", -1)); |
| 1264 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 1265 |
return TCL_ERROR; |
| 1266 |
} |
| 1267 |
if (dir != PATH_NONE) { |
| 1268 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1269 |
dir == PATH_DIR |
| 1270 |
? "\"-directory\" may only be used once" |
| 1271 |
: "\"-directory\" cannot be used with \"-path\"", |
| 1272 |
-1)); |
| 1273 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "GLOB", |
| 1274 |
"BADOPTIONCOMBINATION", (char *)NULL); |
| 1275 |
return TCL_ERROR; |
| 1276 |
} |
| 1277 |
dir = PATH_DIR; |
| 1278 |
globFlags |= TCL_GLOBMODE_DIR; |
| 1279 |
pathOrDir = objv[i+1]; |
| 1280 |
i++; |
| 1281 |
break; |
| 1282 |
case GLOB_JOIN: /* -join */ |
| 1283 |
join = 1; |
| 1284 |
break; |
| 1285 |
case GLOB_TAILS: /* -tails */ |
| 1286 |
globFlags |= TCL_GLOBMODE_TAILS; |
| 1287 |
break; |
| 1288 |
case GLOB_PATH: /* -path */ |
| 1289 |
if (i == (objc-1)) { |
| 1290 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1291 |
"missing argument to \"-path\"", -1)); |
| 1292 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 1293 |
return TCL_ERROR; |
| 1294 |
} |
| 1295 |
if (dir != PATH_NONE) { |
| 1296 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1297 |
dir == PATH_GENERAL |
| 1298 |
? "\"-path\" may only be used once" |
| 1299 |
: "\"-path\" cannot be used with \"-dictionary\"", |
| 1300 |
-1)); |
| 1301 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "GLOB", |
| 1302 |
"BADOPTIONCOMBINATION", (char *)NULL); |
| 1303 |
return TCL_ERROR; |
| 1304 |
} |
| 1305 |
dir = PATH_GENERAL; |
| 1306 |
pathOrDir = objv[i+1]; |
| 1307 |
i++; |
| 1308 |
break; |
| 1309 |
case GLOB_TYPE: /* -types */ |
| 1310 |
if (i == (objc-1)) { |
| 1311 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1312 |
"missing argument to \"-types\"", -1)); |
| 1313 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "MISSING", (char *)NULL); |
| 1314 |
return TCL_ERROR; |
| 1315 |
} |
| 1316 |
typePtr = objv[i+1]; |
| 1317 |
if (TclListObjLength(interp, typePtr, &length) != TCL_OK) { |
| 1318 |
return TCL_ERROR; |
| 1319 |
} |
| 1320 |
i++; |
| 1321 |
break; |
| 1322 |
case GLOB_LAST: /* -- */ |
| 1323 |
i++; |
| 1324 |
goto endOfForLoop; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
endOfForLoop: |
| 1329 |
if ((globFlags & TCL_GLOBMODE_TAILS) && (pathOrDir == NULL)) { |
| 1330 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1331 |
"\"-tails\" must be used with either " |
| 1332 |
"\"-directory\" or \"-path\"", -1)); |
| 1333 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "GLOB", |
| 1334 |
"BADOPTIONCOMBINATION", (char *)NULL); |
| 1335 |
return TCL_ERROR; |
| 1336 |
} |
| 1337 |
|
| 1338 |
separators = NULL; |
| 1339 |
switch (tclPlatform) { |
| 1340 |
case TCL_PLATFORM_UNIX: |
| 1341 |
separators = "/"; |
| 1342 |
break; |
| 1343 |
case TCL_PLATFORM_WINDOWS: |
| 1344 |
separators = "/\\:"; |
| 1345 |
break; |
| 1346 |
} |
| 1347 |
|
| 1348 |
if (dir == PATH_GENERAL) { |
| 1349 |
int pathlength; |
| 1350 |
const char *last; |
| 1351 |
const char *first = TclGetStringFromObj(pathOrDir,&pathlength); |
| 1352 |
|
| 1353 |
/* |
| 1354 |
* Find the last path separator in the path |
| 1355 |
*/ |
| 1356 |
|
| 1357 |
last = first + pathlength; |
| 1358 |
for (; last != first; last--) { |
| 1359 |
if (strchr(separators, *(last-1)) != NULL) { |
| 1360 |
break; |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
if (last == first + pathlength) { |
| 1365 |
/* |
| 1366 |
* It's really a directory. |
| 1367 |
*/ |
| 1368 |
|
| 1369 |
dir = PATH_DIR; |
| 1370 |
|
| 1371 |
} else { |
| 1372 |
Tcl_DString pref; |
| 1373 |
char *search, *find; |
| 1374 |
Tcl_DStringInit(&pref); |
| 1375 |
if (last == first) { |
| 1376 |
/* |
| 1377 |
* The whole thing is a prefix. This means we must remove any |
| 1378 |
* 'tails' flag too, since it is irrelevant now (the same |
| 1379 |
* effect will happen without it), but in particular its use |
| 1380 |
* in TclGlob requires a non-NULL pathOrDir. |
| 1381 |
*/ |
| 1382 |
|
| 1383 |
Tcl_DStringAppend(&pref, first, -1); |
| 1384 |
globFlags &= ~TCL_GLOBMODE_TAILS; |
| 1385 |
pathOrDir = NULL; |
| 1386 |
} else { |
| 1387 |
/* |
| 1388 |
* Have to split off the end. |
| 1389 |
*/ |
| 1390 |
|
| 1391 |
Tcl_DStringAppend(&pref, last, first+pathlength-last); |
| 1392 |
pathOrDir = Tcl_NewStringObj(first, last-first-1); |
| 1393 |
|
| 1394 |
/* |
| 1395 |
* We must ensure that we haven't cut off too much, and turned |
| 1396 |
* a valid path like '/' or 'C:/' into an incorrect path like |
| 1397 |
* '' or 'C:'. The way we do this is to add a separator if |
| 1398 |
* there are none presently in the prefix. |
| 1399 |
*/ |
| 1400 |
|
| 1401 |
if (strpbrk(TclGetString(pathOrDir), "\\/") == NULL) { |
| 1402 |
Tcl_AppendToObj(pathOrDir, last-1, 1); |
| 1403 |
} |
| 1404 |
} |
| 1405 |
|
| 1406 |
/* |
| 1407 |
* Need to quote 'prefix'. |
| 1408 |
*/ |
| 1409 |
|
| 1410 |
Tcl_DStringInit(&prefix); |
| 1411 |
search = Tcl_DStringValue(&pref); |
| 1412 |
while ((find = (strpbrk(search, "\\[]*?{}"))) != NULL) { |
| 1413 |
Tcl_DStringAppend(&prefix, search, find-search); |
| 1414 |
TclDStringAppendLiteral(&prefix, "\\"); |
| 1415 |
Tcl_DStringAppend(&prefix, find, 1); |
| 1416 |
search = find+1; |
| 1417 |
if (*search == '\0') { |
| 1418 |
break; |
| 1419 |
} |
| 1420 |
} |
| 1421 |
if (*search != '\0') { |
| 1422 |
Tcl_DStringAppend(&prefix, search, -1); |
| 1423 |
} |
| 1424 |
Tcl_DStringFree(&pref); |
| 1425 |
} |
| 1426 |
} |
| 1427 |
|
| 1428 |
if (pathOrDir != NULL) { |
| 1429 |
Tcl_IncrRefCount(pathOrDir); |
| 1430 |
} |
| 1431 |
|
| 1432 |
if (typePtr != NULL) { |
| 1433 |
/* |
| 1434 |
* The rest of the possible type arguments (except 'd') are platform |
| 1435 |
* specific. We don't complain when they are used on an incompatible |
| 1436 |
* platform. |
| 1437 |
*/ |
| 1438 |
|
| 1439 |
TclListObjLength(interp, typePtr, &length); |
| 1440 |
if (length <= 0) { |
| 1441 |
goto skipTypes; |
| 1442 |
} |
| 1443 |
globTypes = (Tcl_GlobTypeData *)TclStackAlloc(interp, sizeof(Tcl_GlobTypeData)); |
| 1444 |
globTypes->type = 0; |
| 1445 |
globTypes->perm = 0; |
| 1446 |
globTypes->macType = NULL; |
| 1447 |
globTypes->macCreator = NULL; |
| 1448 |
|
| 1449 |
while (--length >= 0) { |
| 1450 |
int len; |
| 1451 |
const char *str; |
| 1452 |
|
| 1453 |
Tcl_ListObjIndex(interp, typePtr, length, &look); |
| 1454 |
str = TclGetStringFromObj(look, &len); |
| 1455 |
if (strcmp("readonly", str) == 0) { |
| 1456 |
globTypes->perm |= TCL_GLOB_PERM_RONLY; |
| 1457 |
} else if (strcmp("hidden", str) == 0) { |
| 1458 |
globTypes->perm |= TCL_GLOB_PERM_HIDDEN; |
| 1459 |
} else if (len == 1) { |
| 1460 |
switch (str[0]) { |
| 1461 |
case 'r': |
| 1462 |
globTypes->perm |= TCL_GLOB_PERM_R; |
| 1463 |
break; |
| 1464 |
case 'w': |
| 1465 |
globTypes->perm |= TCL_GLOB_PERM_W; |
| 1466 |
break; |
| 1467 |
case 'x': |
| 1468 |
globTypes->perm |= TCL_GLOB_PERM_X; |
| 1469 |
break; |
| 1470 |
case 'b': |
| 1471 |
globTypes->type |= TCL_GLOB_TYPE_BLOCK; |
| 1472 |
break; |
| 1473 |
case 'c': |
| 1474 |
globTypes->type |= TCL_GLOB_TYPE_CHAR; |
| 1475 |
break; |
| 1476 |
case 'd': |
| 1477 |
globTypes->type |= TCL_GLOB_TYPE_DIR; |
| 1478 |
break; |
| 1479 |
case 'p': |
| 1480 |
globTypes->type |= TCL_GLOB_TYPE_PIPE; |
| 1481 |
break; |
| 1482 |
case 'f': |
| 1483 |
globTypes->type |= TCL_GLOB_TYPE_FILE; |
| 1484 |
break; |
| 1485 |
case 'l': |
| 1486 |
globTypes->type |= TCL_GLOB_TYPE_LINK; |
| 1487 |
break; |
| 1488 |
case 's': |
| 1489 |
globTypes->type |= TCL_GLOB_TYPE_SOCK; |
| 1490 |
break; |
| 1491 |
default: |
| 1492 |
goto badTypesArg; |
| 1493 |
} |
| 1494 |
|
| 1495 |
} else if (len == 4) { |
| 1496 |
/* |
| 1497 |
* This is assumed to be a MacOS file type. |
| 1498 |
*/ |
| 1499 |
|
| 1500 |
if (globTypes->macType != NULL) { |
| 1501 |
goto badMacTypesArg; |
| 1502 |
} |
| 1503 |
globTypes->macType = look; |
| 1504 |
Tcl_IncrRefCount(look); |
| 1505 |
|
| 1506 |
} else { |
| 1507 |
Tcl_Obj *item; |
| 1508 |
|
| 1509 |
if ((TclListObjLength(NULL, look, &len) == TCL_OK) |
| 1510 |
&& (len == 3)) { |
| 1511 |
Tcl_ListObjIndex(interp, look, 0, &item); |
| 1512 |
if (!strcmp("macintosh", TclGetString(item))) { |
| 1513 |
Tcl_ListObjIndex(interp, look, 1, &item); |
| 1514 |
if (!strcmp("type", TclGetString(item))) { |
| 1515 |
Tcl_ListObjIndex(interp, look, 2, &item); |
| 1516 |
if (globTypes->macType != NULL) { |
| 1517 |
goto badMacTypesArg; |
| 1518 |
} |
| 1519 |
globTypes->macType = item; |
| 1520 |
Tcl_IncrRefCount(item); |
| 1521 |
continue; |
| 1522 |
} else if (!strcmp("creator", TclGetString(item))) { |
| 1523 |
Tcl_ListObjIndex(interp, look, 2, &item); |
| 1524 |
if (globTypes->macCreator != NULL) { |
| 1525 |
goto badMacTypesArg; |
| 1526 |
} |
| 1527 |
globTypes->macCreator = item; |
| 1528 |
Tcl_IncrRefCount(item); |
| 1529 |
continue; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
} |
| 1533 |
|
| 1534 |
/* |
| 1535 |
* Error cases. We reset the 'join' flag to zero, since we |
| 1536 |
* haven't yet made use of it. |
| 1537 |
*/ |
| 1538 |
|
| 1539 |
badTypesArg: |
| 1540 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1541 |
"bad argument to \"-types\": %s", |
| 1542 |
Tcl_GetString(look))); |
| 1543 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "BAD", (char *)NULL); |
| 1544 |
result = TCL_ERROR; |
| 1545 |
join = 0; |
| 1546 |
goto endOfGlob; |
| 1547 |
|
| 1548 |
badMacTypesArg: |
| 1549 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 1550 |
"only one MacOS type or creator argument" |
| 1551 |
" to \"-types\" allowed", -1)); |
| 1552 |
result = TCL_ERROR; |
| 1553 |
Tcl_SetErrorCode(interp, "TCL", "ARGUMENT", "BAD", (char *)NULL); |
| 1554 |
join = 0; |
| 1555 |
goto endOfGlob; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
skipTypes: |
| 1561 |
/* |
| 1562 |
* Now we perform the actual glob below. This may involve joining together |
| 1563 |
* the pattern arguments, dealing with particular file types etc. We use a |
| 1564 |
* 'goto' to ensure we free any memory allocated along the way. |
| 1565 |
*/ |
| 1566 |
|
| 1567 |
objc -= i; |
| 1568 |
objv += i; |
| 1569 |
result = TCL_OK; |
| 1570 |
|
| 1571 |
if (join) { |
| 1572 |
if (dir != PATH_GENERAL) { |
| 1573 |
Tcl_DStringInit(&prefix); |
| 1574 |
} |
| 1575 |
for (i = 0; i < objc; i++) { |
| 1576 |
TclDStringAppendObj(&prefix, objv[i]); |
| 1577 |
if (i != objc -1) { |
| 1578 |
Tcl_DStringAppend(&prefix, separators, 1); |
| 1579 |
} |
| 1580 |
} |
| 1581 |
if (TclGlob(interp, Tcl_DStringValue(&prefix), pathOrDir, globFlags, |
| 1582 |
globTypes) != TCL_OK) { |
| 1583 |
result = TCL_ERROR; |
| 1584 |
goto endOfGlob; |
| 1585 |
} |
| 1586 |
} else if (dir == PATH_GENERAL) { |
| 1587 |
Tcl_DString str; |
| 1588 |
|
| 1589 |
Tcl_DStringInit(&str); |
| 1590 |
for (i = 0; i < objc; i++) { |
| 1591 |
Tcl_DStringSetLength(&str, 0); |
| 1592 |
if (dir == PATH_GENERAL) { |
| 1593 |
TclDStringAppendDString(&str, &prefix); |
| 1594 |
} |
| 1595 |
TclDStringAppendObj(&str, objv[i]); |
| 1596 |
if (TclGlob(interp, Tcl_DStringValue(&str), pathOrDir, globFlags, |
| 1597 |
globTypes) != TCL_OK) { |
| 1598 |
result = TCL_ERROR; |
| 1599 |
Tcl_DStringFree(&str); |
| 1600 |
goto endOfGlob; |
| 1601 |
} |
| 1602 |
} |
| 1603 |
Tcl_DStringFree(&str); |
| 1604 |
} else { |
| 1605 |
for (i = 0; i < objc; i++) { |
| 1606 |
string = TclGetString(objv[i]); |
| 1607 |
if (TclGlob(interp, string, pathOrDir, globFlags, |
| 1608 |
globTypes) != TCL_OK) { |
| 1609 |
result = TCL_ERROR; |
| 1610 |
goto endOfGlob; |
| 1611 |
} |
| 1612 |
} |
| 1613 |
} |
| 1614 |
|
| 1615 |
if ((globFlags & TCL_GLOBMODE_NO_COMPLAIN) == 0) { |
| 1616 |
if (TclListObjLength(interp, Tcl_GetObjResult(interp), |
| 1617 |
&length) != TCL_OK) { |
| 1618 |
/* |
| 1619 |
* This should never happen. Maybe we should be more dramatic. |
| 1620 |
*/ |
| 1621 |
|
| 1622 |
result = TCL_ERROR; |
| 1623 |
goto endOfGlob; |
| 1624 |
} |
| 1625 |
|
| 1626 |
if (length == 0) { |
| 1627 |
Tcl_Obj *errorMsg = |
| 1628 |
Tcl_ObjPrintf("no files matched glob pattern%s \"", |
| 1629 |
(join || (objc == 1)) ? "" : "s"); |
| 1630 |
|
| 1631 |
if (join) { |
| 1632 |
Tcl_AppendToObj(errorMsg, Tcl_DStringValue(&prefix), -1); |
| 1633 |
} else { |
| 1634 |
const char *sep = ""; |
| 1635 |
|
| 1636 |
for (i = 0; i < objc; i++) { |
| 1637 |
Tcl_AppendPrintfToObj(errorMsg, "%s%s", |
| 1638 |
sep, TclGetString(objv[i])); |
| 1639 |
sep = " "; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
Tcl_AppendToObj(errorMsg, "\"", -1); |
| 1643 |
Tcl_SetObjResult(interp, errorMsg); |
| 1644 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "GLOB", "NOMATCH", |
| 1645 |
(char *)NULL); |
| 1646 |
result = TCL_ERROR; |
| 1647 |
} |
| 1648 |
} |
| 1649 |
|
| 1650 |
endOfGlob: |
| 1651 |
if (join || (dir == PATH_GENERAL)) { |
| 1652 |
Tcl_DStringFree(&prefix); |
| 1653 |
} |
| 1654 |
if (pathOrDir != NULL) { |
| 1655 |
Tcl_DecrRefCount(pathOrDir); |
| 1656 |
} |
| 1657 |
if (globTypes != NULL) { |
| 1658 |
if (globTypes->macType != NULL) { |
| 1659 |
Tcl_DecrRefCount(globTypes->macType); |
| 1660 |
} |
| 1661 |
if (globTypes->macCreator != NULL) { |
| 1662 |
Tcl_DecrRefCount(globTypes->macCreator); |
| 1663 |
} |
| 1664 |
TclStackFree(interp, globTypes); |
| 1665 |
} |
| 1666 |
return result; |
| 1667 |
} |
| 1668 |
|
| 1669 |
/* |
| 1670 |
*---------------------------------------------------------------------- |
| 1671 |
* |
| 1672 |
* TclGlob -- |
| 1673 |
* |
| 1674 |
* Sets the separator string based on the platform, performs tilde |
| 1675 |
* substitution, and calls DoGlob. |
| 1676 |
* |
| 1677 |
* The interpreter's result, on entry to this function, must be a valid |
| 1678 |
* Tcl list (e.g. it could be empty), since we will lappend any new |
| 1679 |
* results to that list. If it is not a valid list, this function will |
| 1680 |
* fail to do anything very meaningful. |
| 1681 |
* |
| 1682 |
* Note that if globFlags contains 'TCL_GLOBMODE_TAILS' then pathPrefix |
| 1683 |
* cannot be NULL (it is only allowed with -dir or -path). |
| 1684 |
* |
| 1685 |
* Results: |
| 1686 |
* The return value is a standard Tcl result indicating whether an error |
| 1687 |
* occurred in globbing. After a normal return the result in interp (set |
| 1688 |
* by DoGlob) holds all of the file names given by the pattern and |
| 1689 |
* pathPrefix arguments. After an error the result in interp will hold |
| 1690 |
* an error message. |
| 1691 |
* |
| 1692 |
* Side effects: |
| 1693 |
* The 'pattern' is written to. |
| 1694 |
* |
| 1695 |
*---------------------------------------------------------------------- |
| 1696 |
*/ |
| 1697 |
|
| 1698 |
static int |
| 1699 |
TclGlob( |
| 1700 |
Tcl_Interp *interp, /* Interpreter for returning error message or |
| 1701 |
* appending list of matching file names. */ |
| 1702 |
char *pattern, /* Glob pattern to match. Must not refer to a |
| 1703 |
* static string. */ |
| 1704 |
Tcl_Obj *pathPrefix, /* Path prefix to glob pattern, if non-null, |
| 1705 |
* which is considered literally. */ |
| 1706 |
int globFlags, /* Stores or'ed combination of flags */ |
| 1707 |
Tcl_GlobTypeData *types) /* Struct containing acceptable types. May be |
| 1708 |
* NULL. */ |
| 1709 |
{ |
| 1710 |
const char *separators; |
| 1711 |
const char *head; |
| 1712 |
char *tail, *start; |
| 1713 |
int result; |
| 1714 |
Tcl_Obj *filenamesObj, *savedResultObj; |
| 1715 |
|
| 1716 |
separators = NULL; |
| 1717 |
switch (tclPlatform) { |
| 1718 |
case TCL_PLATFORM_UNIX: |
| 1719 |
separators = "/"; |
| 1720 |
break; |
| 1721 |
case TCL_PLATFORM_WINDOWS: |
| 1722 |
separators = "/\\:"; |
| 1723 |
break; |
| 1724 |
} |
| 1725 |
|
| 1726 |
if (pathPrefix == NULL) { |
| 1727 |
char c; |
| 1728 |
Tcl_DString buffer; |
| 1729 |
Tcl_DStringInit(&buffer); |
| 1730 |
|
| 1731 |
start = pattern; |
| 1732 |
|
| 1733 |
/* |
| 1734 |
* Perform tilde substitution, if needed. |
| 1735 |
*/ |
| 1736 |
|
| 1737 |
if (start[0] == '~') { |
| 1738 |
/* |
| 1739 |
* Find the first path separator after the tilde. |
| 1740 |
*/ |
| 1741 |
|
| 1742 |
for (tail = start; *tail != '\0'; tail++) { |
| 1743 |
if (*tail == '\\') { |
| 1744 |
if (strchr(separators, tail[1]) != NULL) { |
| 1745 |
break; |
| 1746 |
} |
| 1747 |
} else if (strchr(separators, *tail) != NULL) { |
| 1748 |
break; |
| 1749 |
} |
| 1750 |
} |
| 1751 |
|
| 1752 |
/* |
| 1753 |
* Determine the home directory for the specified user. |
| 1754 |
*/ |
| 1755 |
|
| 1756 |
c = *tail; |
| 1757 |
*tail = '\0'; |
| 1758 |
head = DoTildeSubst(interp, start+1, &buffer); |
| 1759 |
*tail = c; |
| 1760 |
if (head == NULL) { |
| 1761 |
return TCL_ERROR; |
| 1762 |
} |
| 1763 |
if (head != Tcl_DStringValue(&buffer)) { |
| 1764 |
Tcl_DStringAppend(&buffer, head, -1); |
| 1765 |
} |
| 1766 |
pathPrefix = TclDStringToObj(&buffer); |
| 1767 |
Tcl_IncrRefCount(pathPrefix); |
| 1768 |
globFlags |= TCL_GLOBMODE_DIR; |
| 1769 |
if (c != '\0') { |
| 1770 |
tail++; |
| 1771 |
} |
| 1772 |
} else { |
| 1773 |
tail = pattern; |
| 1774 |
} |
| 1775 |
} else { |
| 1776 |
Tcl_IncrRefCount(pathPrefix); |
| 1777 |
tail = pattern; |
| 1778 |
} |
| 1779 |
|
| 1780 |
/* |
| 1781 |
* Handling empty path prefixes with glob patterns like 'C:' or |
| 1782 |
* 'c:////////' is a pain on Windows if we leave it too late, since these |
| 1783 |
* aren't really patterns at all! We therefore check the head of the |
| 1784 |
* pattern now for such cases, if we don't have an unquoted prefix yet. |
| 1785 |
* |
| 1786 |
* Similarly on Unix with '/' at the head of the pattern -- it just |
| 1787 |
* indicates the root volume, so we treat it as such. |
| 1788 |
*/ |
| 1789 |
|
| 1790 |
if (tclPlatform == TCL_PLATFORM_WINDOWS) { |
| 1791 |
if (pathPrefix == NULL && tail[0] != '\0' && tail[1] == ':') { |
| 1792 |
char *p = tail + 1; |
| 1793 |
pathPrefix = Tcl_NewStringObj(tail, 1); |
| 1794 |
while (*p != '\0') { |
| 1795 |
char c = p[1]; |
| 1796 |
if (*p == '\\') { |
| 1797 |
if (strchr(separators, c) != NULL) { |
| 1798 |
if (c == '\\') { |
| 1799 |
c = '/'; |
| 1800 |
} |
| 1801 |
Tcl_AppendToObj(pathPrefix, &c, 1); |
| 1802 |
p++; |
| 1803 |
} else { |
| 1804 |
break; |
| 1805 |
} |
| 1806 |
} else if (strchr(separators, *p) != NULL) { |
| 1807 |
Tcl_AppendToObj(pathPrefix, p, 1); |
| 1808 |
} else { |
| 1809 |
break; |
| 1810 |
} |
| 1811 |
p++; |
| 1812 |
} |
| 1813 |
tail = p; |
| 1814 |
Tcl_IncrRefCount(pathPrefix); |
| 1815 |
} else if (pathPrefix == NULL && (tail[0] == '/' |
| 1816 |
|| (tail[0] == '\\' && tail[1] == '\\'))) { |
| 1817 |
int driveNameLen; |
| 1818 |
Tcl_Obj *driveName; |
| 1819 |
Tcl_Obj *temp = Tcl_NewStringObj(tail, -1); |
| 1820 |
Tcl_IncrRefCount(temp); |
| 1821 |
|
| 1822 |
switch (TclGetPathType(temp, NULL, &driveNameLen, &driveName)) { |
| 1823 |
case TCL_PATH_VOLUME_RELATIVE: { |
| 1824 |
/* |
| 1825 |
* Volume relative path which is equivalent to a path in the |
| 1826 |
* root of the cwd's volume. We will actually return |
| 1827 |
* non-volume-relative paths here. i.e. 'glob /foo*' will |
| 1828 |
* return 'C:/foobar'. This is much the same as globbing for a |
| 1829 |
* path with '\\' will return one with '/' on Windows. |
| 1830 |
*/ |
| 1831 |
|
| 1832 |
Tcl_Obj *cwd = Tcl_FSGetCwd(interp); |
| 1833 |
|
| 1834 |
if (cwd == NULL) { |
| 1835 |
Tcl_DecrRefCount(temp); |
| 1836 |
return TCL_ERROR; |
| 1837 |
} |
| 1838 |
pathPrefix = Tcl_NewStringObj(TclGetString(cwd), 3); |
| 1839 |
Tcl_DecrRefCount(cwd); |
| 1840 |
if (tail[0] == '/') { |
| 1841 |
tail++; |
| 1842 |
} else { |
| 1843 |
tail += 2; |
| 1844 |
} |
| 1845 |
Tcl_IncrRefCount(pathPrefix); |
| 1846 |
break; |
| 1847 |
} |
| 1848 |
case TCL_PATH_ABSOLUTE: |
| 1849 |
/* |
| 1850 |
* Absolute, possibly network path //Machine/Share. Use that |
| 1851 |
* as the path prefix (it already has a refCount). |
| 1852 |
*/ |
| 1853 |
|
| 1854 |
pathPrefix = driveName; |
| 1855 |
tail += driveNameLen; |
| 1856 |
break; |
| 1857 |
case TCL_PATH_RELATIVE: |
| 1858 |
/* Do nothing */ |
| 1859 |
break; |
| 1860 |
} |
| 1861 |
Tcl_DecrRefCount(temp); |
| 1862 |
} |
| 1863 |
|
| 1864 |
/* |
| 1865 |
* ':' no longer needed as a separator. It is only relevant to the |
| 1866 |
* beginning of the path. |
| 1867 |
*/ |
| 1868 |
|
| 1869 |
separators = "/\\"; |
| 1870 |
|
| 1871 |
} else if (tclPlatform == TCL_PLATFORM_UNIX) { |
| 1872 |
if (pathPrefix == NULL && tail[0] == '/' |
| 1873 |
#if defined(__CYGWIN__) || defined(__QNX__) |
| 1874 |
&& tail[1] != '/' |
| 1875 |
#endif |
| 1876 |
) { |
| 1877 |
pathPrefix = Tcl_NewStringObj(tail, 1); |
| 1878 |
tail++; |
| 1879 |
Tcl_IncrRefCount(pathPrefix); |
| 1880 |
} |
| 1881 |
} |
| 1882 |
|
| 1883 |
/* |
| 1884 |
* Finally if we still haven't managed to generate a path prefix, check if |
| 1885 |
* the path starts with a current volume. |
| 1886 |
*/ |
| 1887 |
|
| 1888 |
if (pathPrefix == NULL) { |
| 1889 |
int driveNameLen; |
| 1890 |
Tcl_Obj *driveName; |
| 1891 |
if (TclFSNonnativePathType(tail, (int) strlen(tail), NULL, |
| 1892 |
&driveNameLen, &driveName) == TCL_PATH_ABSOLUTE) { |
| 1893 |
pathPrefix = driveName; |
| 1894 |
tail += driveNameLen; |
| 1895 |
} |
| 1896 |
} |
| 1897 |
|
| 1898 |
/* |
| 1899 |
* To process a [glob] invocation, this function may be called multiple |
| 1900 |
* times. Each time, the previously discovered filenames are in the |
| 1901 |
* interpreter result. We stash that away here so the result is free for |
| 1902 |
* error messages. |
| 1903 |
*/ |
| 1904 |
|
| 1905 |
savedResultObj = Tcl_GetObjResult(interp); |
| 1906 |
Tcl_IncrRefCount(savedResultObj); |
| 1907 |
Tcl_ResetResult(interp); |
| 1908 |
TclNewObj(filenamesObj); |
| 1909 |
Tcl_IncrRefCount(filenamesObj); |
| 1910 |
|
| 1911 |
/* |
| 1912 |
* Now we do the actual globbing, adding filenames as we go to buffer in |
| 1913 |
* filenamesObj |
| 1914 |
*/ |
| 1915 |
|
| 1916 |
if (*tail == '\0' && pathPrefix != NULL) { |
| 1917 |
/* |
| 1918 |
* An empty pattern. This means 'pathPrefix' is actually a full path |
| 1919 |
* of a file/directory we want to simply check for existence and type. |
| 1920 |
*/ |
| 1921 |
|
| 1922 |
if (types == NULL) { |
| 1923 |
/* |
| 1924 |
* We just want to check for existence. In this case we make it |
| 1925 |
* easy on Tcl_FSMatchInDirectory and its sub-implementations by |
| 1926 |
* not bothering them (even though they should support this |
| 1927 |
* situation) and we just use the simple existence check with |
| 1928 |
* Tcl_FSAccess. |
| 1929 |
*/ |
| 1930 |
|
| 1931 |
if (Tcl_FSAccess(pathPrefix, F_OK) == 0) { |
| 1932 |
Tcl_ListObjAppendElement(interp, filenamesObj, pathPrefix); |
| 1933 |
} |
| 1934 |
result = TCL_OK; |
| 1935 |
} else { |
| 1936 |
/* |
| 1937 |
* We want to check for the correct type. Tcl_FSMatchInDirectory |
| 1938 |
* is documented to do this for us, if we give it a NULL pattern. |
| 1939 |
*/ |
| 1940 |
|
| 1941 |
result = Tcl_FSMatchInDirectory(interp, filenamesObj, pathPrefix, |
| 1942 |
NULL, types); |
| 1943 |
} |
| 1944 |
} else { |
| 1945 |
result = DoGlob(interp, filenamesObj, separators, pathPrefix, |
| 1946 |
globFlags & TCL_GLOBMODE_DIR, tail, types); |
| 1947 |
} |
| 1948 |
|
| 1949 |
/* |
| 1950 |
* Check for errors... |
| 1951 |
*/ |
| 1952 |
|
| 1953 |
if (result != TCL_OK) { |
| 1954 |
TclDecrRefCount(filenamesObj); |
| 1955 |
TclDecrRefCount(savedResultObj); |
| 1956 |
if (pathPrefix != NULL) { |
| 1957 |
Tcl_DecrRefCount(pathPrefix); |
| 1958 |
} |
| 1959 |
return result; |
| 1960 |
} |
| 1961 |
|
| 1962 |
/* |
| 1963 |
* If we only want the tails, we must strip off the prefix now. It may |
| 1964 |
* seem more efficient to pass the tails flag down into DoGlob, |
| 1965 |
* Tcl_FSMatchInDirectory, but those functions are continually adjusting |
| 1966 |
* the prefix as the various pieces of the pattern are assimilated, so |
| 1967 |
* that would add a lot of complexity to the code. This way is a little |
| 1968 |
* slower (when the -tails flag is given), but much simpler to code. |
| 1969 |
* |
| 1970 |
* We do it by rewriting the result list in-place. |
| 1971 |
*/ |
| 1972 |
|
| 1973 |
if (globFlags & TCL_GLOBMODE_TAILS) { |
| 1974 |
int objc, i; |
| 1975 |
Tcl_Obj **objv; |
| 1976 |
int prefixLen; |
| 1977 |
const char *pre; |
| 1978 |
|
| 1979 |
/* |
| 1980 |
* If this length has never been set, set it here. |
| 1981 |
*/ |
| 1982 |
|
| 1983 |
if (pathPrefix == NULL) { |
| 1984 |
Tcl_Panic("Called TclGlob with TCL_GLOBMODE_TAILS and pathPrefix==NULL"); |
| 1985 |
} |
| 1986 |
|
| 1987 |
pre = TclGetStringFromObj(pathPrefix, &prefixLen); |
| 1988 |
if (prefixLen > 0 |
| 1989 |
&& (strchr(separators, pre[prefixLen-1]) == NULL)) { |
| 1990 |
/* |
| 1991 |
* If we're on Windows and the prefix is a volume relative one |
| 1992 |
* like 'C:', then there won't be a path separator in between, so |
| 1993 |
* no need to skip it here. |
| 1994 |
*/ |
| 1995 |
|
| 1996 |
if ((tclPlatform != TCL_PLATFORM_WINDOWS) || (prefixLen != 2) |
| 1997 |
|| (pre[1] != ':')) { |
| 1998 |
prefixLen++; |
| 1999 |
} |
| 2000 |
} |
| 2001 |
|
| 2002 |
TclListObjGetElements(NULL, filenamesObj, &objc, &objv); |
| 2003 |
for (i = 0; i< objc; i++) { |
| 2004 |
int len; |
| 2005 |
const char *oldStr = TclGetStringFromObj(objv[i], &len); |
| 2006 |
Tcl_Obj *elem; |
| 2007 |
|
| 2008 |
if (len == prefixLen) { |
| 2009 |
if ((pattern[0] == '\0') |
| 2010 |
|| (strchr(separators, pattern[0]) == NULL)) { |
| 2011 |
TclNewLiteralStringObj(elem, "."); |
| 2012 |
} else { |
| 2013 |
TclNewLiteralStringObj(elem, "/"); |
| 2014 |
} |
| 2015 |
} else { |
| 2016 |
elem = Tcl_NewStringObj(oldStr+prefixLen, len-prefixLen); |
| 2017 |
} |
| 2018 |
Tcl_ListObjReplace(interp, filenamesObj, i, 1, 1, &elem); |
| 2019 |
} |
| 2020 |
} |
| 2021 |
|
| 2022 |
/* |
| 2023 |
* Now we have a list of discovered filenames in filenamesObj and a list |
| 2024 |
* of previously discovered (saved earlier from the interpreter result) in |
| 2025 |
* savedResultObj. Merge them and put them back in the interpreter result. |
| 2026 |
*/ |
| 2027 |
|
| 2028 |
if (Tcl_IsShared(savedResultObj)) { |
| 2029 |
TclDecrRefCount(savedResultObj); |
| 2030 |
savedResultObj = Tcl_DuplicateObj(savedResultObj); |
| 2031 |
Tcl_IncrRefCount(savedResultObj); |
| 2032 |
} |
| 2033 |
if (Tcl_ListObjAppendList(interp, savedResultObj, filenamesObj) != TCL_OK){ |
| 2034 |
result = TCL_ERROR; |
| 2035 |
} else { |
| 2036 |
Tcl_SetObjResult(interp, savedResultObj); |
| 2037 |
} |
| 2038 |
TclDecrRefCount(savedResultObj); |
| 2039 |
TclDecrRefCount(filenamesObj); |
| 2040 |
if (pathPrefix != NULL) { |
| 2041 |
Tcl_DecrRefCount(pathPrefix); |
| 2042 |
} |
| 2043 |
|
| 2044 |
return result; |
| 2045 |
} |
| 2046 |
|
| 2047 |
/* |
| 2048 |
*---------------------------------------------------------------------- |
| 2049 |
* |
| 2050 |
* SkipToChar -- |
| 2051 |
* |
| 2052 |
* This function traverses a glob pattern looking for the next unquoted |
| 2053 |
* occurrence of the specified character at the same braces nesting level. |
| 2054 |
* |
| 2055 |
* Results: |
| 2056 |
* Updates stringPtr to point to the matching character, or to the end of |
| 2057 |
* the string if nothing matched. The return value is 1 if a match was |
| 2058 |
* found at the top level, otherwise it is 0. |
| 2059 |
* |
| 2060 |
* Side effects: |
| 2061 |
* None. |
| 2062 |
* |
| 2063 |
*---------------------------------------------------------------------- |
| 2064 |
*/ |
| 2065 |
|
| 2066 |
static int |
| 2067 |
SkipToChar( |
| 2068 |
char **stringPtr, /* Pointer string to check. */ |
| 2069 |
int match) /* Character to find. */ |
| 2070 |
{ |
| 2071 |
int quoted, level; |
| 2072 |
char *p; |
| 2073 |
|
| 2074 |
quoted = 0; |
| 2075 |
level = 0; |
| 2076 |
|
| 2077 |
for (p = *stringPtr; *p != '\0'; p++) { |
| 2078 |
if (quoted) { |
| 2079 |
quoted = 0; |
| 2080 |
continue; |
| 2081 |
} |
| 2082 |
if ((level == 0) && (*p == match)) { |
| 2083 |
*stringPtr = p; |
| 2084 |
return 1; |
| 2085 |
} |
| 2086 |
if (*p == '{') { |
| 2087 |
level++; |
| 2088 |
} else if (*p == '}') { |
| 2089 |
level--; |
| 2090 |
} else if (*p == '\\') { |
| 2091 |
quoted = 1; |
| 2092 |
} |
| 2093 |
} |
| 2094 |
*stringPtr = p; |
| 2095 |
return 0; |
| 2096 |
} |
| 2097 |
|
| 2098 |
/* |
| 2099 |
*---------------------------------------------------------------------- |
| 2100 |
* |
| 2101 |
* DoGlob -- |
| 2102 |
* |
| 2103 |
* This recursive procedure forms the heart of the globbing code. It |
| 2104 |
* performs a depth-first traversal of the tree given by the path name to |
| 2105 |
* be globbed and the pattern. The directory and remainder are assumed to |
| 2106 |
* be native format paths. The prefix contained in 'pathPtr' is either a |
| 2107 |
* directory or path from which to start the search (or NULL). If pathPtr |
| 2108 |
* is NULL, then the pattern must not start with an absolute path |
| 2109 |
* specification (that case should be handled by moving the absolute path |
| 2110 |
* prefix into pathPtr before calling DoGlob). |
| 2111 |
* |
| 2112 |
* Results: |
| 2113 |
* The return value is a standard Tcl result indicating whether an error |
| 2114 |
* occurred in globbing. After a normal return the result in interp will |
| 2115 |
* be set to hold all of the file names given by the dir and remaining |
| 2116 |
* arguments. After an error the result in interp will hold an error |
| 2117 |
* message. |
| 2118 |
* |
| 2119 |
* Side effects: |
| 2120 |
* None. |
| 2121 |
* |
| 2122 |
*---------------------------------------------------------------------- |
| 2123 |
*/ |
| 2124 |
|
| 2125 |
static int |
| 2126 |
DoGlob( |
| 2127 |
Tcl_Interp *interp, /* Interpreter to use for error reporting |
| 2128 |
* (e.g. unmatched brace). */ |
| 2129 |
Tcl_Obj *matchesObj, /* Unshared list object in which to place all |
| 2130 |
* resulting filenames. Caller allocates and |
| 2131 |
* deallocates; DoGlob must not touch the |
| 2132 |
* refCount of this object. */ |
| 2133 |
const char *separators, /* String containing separator characters that |
| 2134 |
* should be used to identify globbing |
| 2135 |
* boundaries. */ |
| 2136 |
Tcl_Obj *pathPtr, /* Completely expanded prefix. */ |
| 2137 |
int flags, /* If non-zero then pathPtr is a directory */ |
| 2138 |
char *pattern, /* The pattern to match against. Must not be a |
| 2139 |
* pointer to a static string. */ |
| 2140 |
Tcl_GlobTypeData *types) /* List object containing list of acceptable |
| 2141 |
* types. May be NULL. */ |
| 2142 |
{ |
| 2143 |
int baseLength, quoted; |
| 2144 |
int result = TCL_OK; |
| 2145 |
char *name, *p, *openBrace, *closeBrace, *firstSpecialChar; |
| 2146 |
Tcl_Obj *joinedPtr; |
| 2147 |
|
| 2148 |
/* |
| 2149 |
* Consume any leading directory separators, leaving pattern pointing just |
| 2150 |
* past the last initial separator. |
| 2151 |
*/ |
| 2152 |
|
| 2153 |
name = pattern; |
| 2154 |
for (; *pattern != '\0'; pattern++) { |
| 2155 |
if (*pattern == '\\') { |
| 2156 |
/* |
| 2157 |
* If the first character is escaped, either we have a directory |
| 2158 |
* separator, or we have any other character. In the latter case |
| 2159 |
* the rest is a pattern, and we must break from the loop. This |
| 2160 |
* is particularly important on Windows where '\' is both the |
| 2161 |
* escaping character and a directory separator. |
| 2162 |
*/ |
| 2163 |
|
| 2164 |
if (strchr(separators, pattern[1]) != NULL) { |
| 2165 |
pattern++; |
| 2166 |
} else { |
| 2167 |
break; |
| 2168 |
} |
| 2169 |
} else if (strchr(separators, *pattern) == NULL) { |
| 2170 |
break; |
| 2171 |
} |
| 2172 |
} |
| 2173 |
|
| 2174 |
/* |
| 2175 |
* Look for the first matching pair of braces or the first directory |
| 2176 |
* separator that is not inside a pair of braces. |
| 2177 |
*/ |
| 2178 |
|
| 2179 |
openBrace = closeBrace = NULL; |
| 2180 |
quoted = 0; |
| 2181 |
for (p = pattern; *p != '\0'; p++) { |
| 2182 |
if (quoted) { |
| 2183 |
quoted = 0; |
| 2184 |
|
| 2185 |
} else if (*p == '\\') { |
| 2186 |
quoted = 1; |
| 2187 |
if (strchr(separators, p[1]) != NULL) { |
| 2188 |
/* |
| 2189 |
* Quoted directory separator. |
| 2190 |
*/ |
| 2191 |
break; |
| 2192 |
} |
| 2193 |
|
| 2194 |
} else if (strchr(separators, *p) != NULL) { |
| 2195 |
/* |
| 2196 |
* Unquoted directory separator. |
| 2197 |
*/ |
| 2198 |
break; |
| 2199 |
|
| 2200 |
} else if (*p == '{') { |
| 2201 |
openBrace = p; |
| 2202 |
p++; |
| 2203 |
if (SkipToChar(&p, '}')) { |
| 2204 |
/* |
| 2205 |
* Balanced braces. |
| 2206 |
*/ |
| 2207 |
|
| 2208 |
closeBrace = p; |
| 2209 |
break; |
| 2210 |
} |
| 2211 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2212 |
"unmatched open-brace in file name", -1)); |
| 2213 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "GLOB", "BALANCE", |
| 2214 |
(char *)NULL); |
| 2215 |
return TCL_ERROR; |
| 2216 |
|
| 2217 |
} else if (*p == '}') { |
| 2218 |
Tcl_SetObjResult(interp, Tcl_NewStringObj( |
| 2219 |
"unmatched close-brace in file name", -1)); |
| 2220 |
Tcl_SetErrorCode(interp, "TCL", "OPERATION", "GLOB", "BALANCE", |
| 2221 |
(char *)NULL); |
| 2222 |
return TCL_ERROR; |
| 2223 |
} |
| 2224 |
} |
| 2225 |
|
| 2226 |
/* |
| 2227 |
* Substitute the alternate patterns from the braces and recurse. |
| 2228 |
*/ |
| 2229 |
|
| 2230 |
if (openBrace != NULL) { |
| 2231 |
char *element; |
| 2232 |
Tcl_DString newName; |
| 2233 |
|
| 2234 |
Tcl_DStringInit(&newName); |
| 2235 |
|
| 2236 |
/* |
| 2237 |
* For each element within in the outermost pair of braces, append the |
| 2238 |
* element and the remainder to the fixed portion before the first |
| 2239 |
* brace and recursively call DoGlob. |
| 2240 |
*/ |
| 2241 |
|
| 2242 |
Tcl_DStringAppend(&newName, pattern, openBrace-pattern); |
| 2243 |
baseLength = Tcl_DStringLength(&newName); |
| 2244 |
*closeBrace = '\0'; |
| 2245 |
for (p = openBrace; p != closeBrace; ) { |
| 2246 |
p++; |
| 2247 |
element = p; |
| 2248 |
SkipToChar(&p, ','); |
| 2249 |
Tcl_DStringSetLength(&newName, baseLength); |
| 2250 |
Tcl_DStringAppend(&newName, element, p-element); |
| 2251 |
Tcl_DStringAppend(&newName, closeBrace+1, -1); |
| 2252 |
result = DoGlob(interp, matchesObj, separators, pathPtr, flags, |
| 2253 |
Tcl_DStringValue(&newName), types); |
| 2254 |
if (result != TCL_OK) { |
| 2255 |
break; |
| 2256 |
} |
| 2257 |
} |
| 2258 |
*closeBrace = '}'; |
| 2259 |
Tcl_DStringFree(&newName); |
| 2260 |
return result; |
| 2261 |
} |
| 2262 |
|
| 2263 |
/* |
| 2264 |
* At this point, there are no more brace substitutions to perform on this |
| 2265 |
* path component. The variable p is pointing at a quoted or unquoted |
| 2266 |
* directory separator or the end of the string. So we need to check for |
| 2267 |
* special globbing characters in the current pattern. We avoid modifying |
| 2268 |
* pattern if p is pointing at the end of the string. |
| 2269 |
* |
| 2270 |
* If we find any globbing characters, then we must call |
| 2271 |
* Tcl_FSMatchInDirectory. If we're at the end of the string, then that's |
| 2272 |
* all we need to do. If we're not at the end of the string, then we must |
| 2273 |
* recurse, so we do that below. |
| 2274 |
* |
| 2275 |
* Alternatively, if there are no globbing characters then again there are |
| 2276 |
* two cases. If we're at the end of the string, we just need to check for |
| 2277 |
* the given path's existence and type. If we're not at the end of the |
| 2278 |
* string, we recurse. |
| 2279 |
*/ |
| 2280 |
|
| 2281 |
if (*p != '\0') { |
| 2282 |
char savedChar = *p; |
| 2283 |
|
| 2284 |
/* |
| 2285 |
* Note that we are modifying the string in place. This won't work if |
| 2286 |
* the string is a static. |
| 2287 |
*/ |
| 2288 |
|
| 2289 |
*p = '\0'; |
| 2290 |
firstSpecialChar = strpbrk(pattern, "*[]?\\"); |
| 2291 |
*p = savedChar; |
| 2292 |
} else { |
| 2293 |
firstSpecialChar = strpbrk(pattern, "*[]?\\"); |
| 2294 |
} |
| 2295 |
|
| 2296 |
if (firstSpecialChar != NULL) { |
| 2297 |
/* |
| 2298 |
* Look for matching files in the given directory. The implementation |
| 2299 |
* of this function is filesystem specific. For each file that |
| 2300 |
* matches, it will add the match onto the resultPtr given. |
| 2301 |
*/ |
| 2302 |
|
| 2303 |
static Tcl_GlobTypeData dirOnly = { |
| 2304 |
TCL_GLOB_TYPE_DIR, 0, NULL, NULL |
| 2305 |
}; |
| 2306 |
char save = *p; |
| 2307 |
Tcl_Obj *subdirsPtr; |
| 2308 |
|
| 2309 |
if (*p == '\0') { |
| 2310 |
return Tcl_FSMatchInDirectory(interp, matchesObj, pathPtr, |
| 2311 |
pattern, types); |
| 2312 |
} |
| 2313 |
|
| 2314 |
/* |
| 2315 |
* We do the recursion ourselves. This makes implementing |
| 2316 |
* Tcl_FSMatchInDirectory for each filesystem much easier. |
| 2317 |
*/ |
| 2318 |
|
| 2319 |
*p = '\0'; |
| 2320 |
TclNewObj(subdirsPtr); |
| 2321 |
Tcl_IncrRefCount(subdirsPtr); |
| 2322 |
result = Tcl_FSMatchInDirectory(interp, subdirsPtr, pathPtr, |
| 2323 |
pattern, &dirOnly); |
| 2324 |
*p = save; |
| 2325 |
if (result == TCL_OK) { |
| 2326 |
int subdirc, i, repair = -1; |
| 2327 |
Tcl_Obj **subdirv; |
| 2328 |
|
| 2329 |
result = TclListObjGetElements(interp, subdirsPtr, |
| 2330 |
&subdirc, &subdirv); |
| 2331 |
for (i=0; result==TCL_OK && i<subdirc; i++) { |
| 2332 |
Tcl_Obj *copy = NULL; |
| 2333 |
|
| 2334 |
if (pathPtr == NULL && TclGetString(subdirv[i])[0] == '~') { |
| 2335 |
TclListObjLength(NULL, matchesObj, &repair); |
| 2336 |
copy = subdirv[i]; |
| 2337 |
subdirv[i] = Tcl_NewStringObj("./", 2); |
| 2338 |
Tcl_AppendObjToObj(subdirv[i], copy); |
| 2339 |
Tcl_IncrRefCount(subdirv[i]); |
| 2340 |
} |
| 2341 |
result = DoGlob(interp, matchesObj, separators, subdirv[i], |
| 2342 |
1, p+1, types); |
| 2343 |
if (copy) { |
| 2344 |
int end; |
| 2345 |
|
| 2346 |
Tcl_DecrRefCount(subdirv[i]); |
| 2347 |
subdirv[i] = copy; |
| 2348 |
TclListObjLength(NULL, matchesObj, &end); |
| 2349 |
while (repair < end) { |
| 2350 |
const char *bytes; |
| 2351 |
int numBytes; |
| 2352 |
Tcl_Obj *fixme, *newObj; |
| 2353 |
|
| 2354 |
Tcl_ListObjIndex(NULL, matchesObj, repair, &fixme); |
| 2355 |
bytes = TclGetStringFromObj(fixme, &numBytes); |
| 2356 |
newObj = Tcl_NewStringObj(bytes+2, numBytes-2); |
| 2357 |
Tcl_ListObjReplace(NULL, matchesObj, repair, 1, |
| 2358 |
1, &newObj); |
| 2359 |
repair++; |
| 2360 |
} |
| 2361 |
repair = -1; |
| 2362 |
} |
| 2363 |
} |
| 2364 |
} |
| 2365 |
TclDecrRefCount(subdirsPtr); |
| 2366 |
return result; |
| 2367 |
} |
| 2368 |
|
| 2369 |
/* |
| 2370 |
* We reach here with no pattern char in current section |
| 2371 |
*/ |
| 2372 |
|
| 2373 |
if (*p == '\0') { |
| 2374 |
int length; |
| 2375 |
Tcl_DString append; |
| 2376 |
|
| 2377 |
/* |
| 2378 |
* This is the code path reached by a command like 'glob foo'. |
| 2379 |
* |
| 2380 |
* There are no more wildcards in the pattern and no more unprocessed |
| 2381 |
* characters in the pattern, so now we can construct the path, and |
| 2382 |
* pass it to Tcl_FSMatchInDirectory with an empty pattern to verify |
| 2383 |
* the existence of the file and check it is of the correct type (if a |
| 2384 |
* 'types' flag it given -- if no such flag was given, we could just |
| 2385 |
* use 'Tcl_FSLStat', but for simplicity we keep to a common |
| 2386 |
* approach). |
| 2387 |
*/ |
| 2388 |
|
| 2389 |
Tcl_DStringInit(&append); |
| 2390 |
Tcl_DStringAppend(&append, pattern, p-pattern); |
| 2391 |
|
| 2392 |
if (pathPtr != NULL) { |
| 2393 |
(void)TclGetStringFromObj(pathPtr, &length); |
| 2394 |
} else { |
| 2395 |
length = 0; |
| 2396 |
} |
| 2397 |
|
| 2398 |
switch (tclPlatform) { |
| 2399 |
case TCL_PLATFORM_WINDOWS: |
| 2400 |
if (length == 0 && (Tcl_DStringLength(&append) == 0)) { |
| 2401 |
if (((*name == '\\') && (name[1] == '/' || |
| 2402 |
name[1] == '\\')) || (*name == '/')) { |
| 2403 |
TclDStringAppendLiteral(&append, "/"); |
| 2404 |
} else { |
| 2405 |
TclDStringAppendLiteral(&append, "."); |
| 2406 |
} |
| 2407 |
} |
| 2408 |
|
| 2409 |
break; |
| 2410 |
|
| 2411 |
case TCL_PLATFORM_UNIX: |
| 2412 |
if (length == 0 && (Tcl_DStringLength(&append) == 0)) { |
| 2413 |
if ((*name == '\\' && name[1] == '/') || (*name == '/')) { |
| 2414 |
TclDStringAppendLiteral(&append, "/"); |
| 2415 |
} else { |
| 2416 |
TclDStringAppendLiteral(&append, "."); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
break; |
| 2420 |
} |
| 2421 |
|
| 2422 |
/* |
| 2423 |
* Common for all platforms. |
| 2424 |
*/ |
| 2425 |
|
| 2426 |
if (pathPtr == NULL) { |
| 2427 |
joinedPtr = TclDStringToObj(&append); |
| 2428 |
} else if (flags) { |
| 2429 |
joinedPtr = TclNewFSPathObj(pathPtr, Tcl_DStringValue(&append), |
| 2430 |
Tcl_DStringLength(&append)); |
| 2431 |
} else { |
| 2432 |
joinedPtr = Tcl_DuplicateObj(pathPtr); |
| 2433 |
if (strchr(separators, Tcl_DStringValue(&append)[0]) == NULL) { |
| 2434 |
/* |
| 2435 |
* The current prefix must end in a separator. |
| 2436 |
*/ |
| 2437 |
|
| 2438 |
int len; |
| 2439 |
const char *joined = TclGetStringFromObj(joinedPtr, &len); |
| 2440 |
|
| 2441 |
if ((len > 0) && (strchr(separators, joined[len-1]) == NULL)) { |
| 2442 |
Tcl_AppendToObj(joinedPtr, "/", 1); |
| 2443 |
} |
| 2444 |
} |
| 2445 |
Tcl_AppendToObj(joinedPtr, Tcl_DStringValue(&append), |
| 2446 |
Tcl_DStringLength(&append)); |
| 2447 |
} |
| 2448 |
Tcl_IncrRefCount(joinedPtr); |
| 2449 |
Tcl_DStringFree(&append); |
| 2450 |
result = Tcl_FSMatchInDirectory(interp, matchesObj, joinedPtr, NULL, |
| 2451 |
types); |
| 2452 |
Tcl_DecrRefCount(joinedPtr); |
| 2453 |
return result; |
| 2454 |
} |
| 2455 |
|
| 2456 |
/* |
| 2457 |
* If it's not the end of the string, we must recurse |
| 2458 |
*/ |
| 2459 |
|
| 2460 |
if (pathPtr == NULL) { |
| 2461 |
joinedPtr = Tcl_NewStringObj(pattern, p-pattern); |
| 2462 |
} else if (flags) { |
| 2463 |
joinedPtr = TclNewFSPathObj(pathPtr, pattern, p-pattern); |
| 2464 |
} else { |
| 2465 |
joinedPtr = Tcl_DuplicateObj(pathPtr); |
| 2466 |
if (strchr(separators, pattern[0]) == NULL) { |
| 2467 |
/* |
| 2468 |
* The current prefix must end in a separator, unless this is a |
| 2469 |
* volume-relative path. In particular globbing in Windows shares, |
| 2470 |
* when not using -dir or -path, e.g. 'glob [file join |
| 2471 |
* //machine/share/subdir *]' requires adding a separator here. |
| 2472 |
* This behaviour is not currently tested for in the test suite. |
| 2473 |
*/ |
| 2474 |
|
| 2475 |
int len; |
| 2476 |
const char *joined = TclGetStringFromObj(joinedPtr,&len); |
| 2477 |
|
| 2478 |
if ((len > 0) && (strchr(separators, joined[len-1]) == NULL)) { |
| 2479 |
if (Tcl_FSGetPathType(pathPtr) != TCL_PATH_VOLUME_RELATIVE) { |
| 2480 |
Tcl_AppendToObj(joinedPtr, "/", 1); |
| 2481 |
} |
| 2482 |
} |
| 2483 |
} |
| 2484 |
Tcl_AppendToObj(joinedPtr, pattern, p-pattern); |
| 2485 |
} |
| 2486 |
|
| 2487 |
Tcl_IncrRefCount(joinedPtr); |
| 2488 |
result = DoGlob(interp, matchesObj, separators, joinedPtr, 1, p, types); |
| 2489 |
Tcl_DecrRefCount(joinedPtr); |
| 2490 |
|
| 2491 |
return result; |
| 2492 |
} |
| 2493 |
|
| 2494 |
/* |
| 2495 |
*--------------------------------------------------------------------------- |
| 2496 |
* |
| 2497 |
* Tcl_AllocStatBuf -- |
| 2498 |
* |
| 2499 |
* This procedure allocates a Tcl_StatBuf on the heap. It exists so that |
| 2500 |
* extensions may be used unchanged on systems where largefile support is |
| 2501 |
* optional. |
| 2502 |
* |
| 2503 |
* Results: |
| 2504 |
* A pointer to a Tcl_StatBuf which may be deallocated by being passed to |
| 2505 |
* ckfree(). |
| 2506 |
* |
| 2507 |
* Side effects: |
| 2508 |
* None. |
| 2509 |
* |
| 2510 |
*--------------------------------------------------------------------------- |
| 2511 |
*/ |
| 2512 |
|
| 2513 |
Tcl_StatBuf * |
| 2514 |
Tcl_AllocStatBuf(void) |
| 2515 |
{ |
| 2516 |
return (Tcl_StatBuf *)ckalloc(sizeof(Tcl_StatBuf)); |
| 2517 |
} |
| 2518 |
|
| 2519 |
/* |
| 2520 |
*--------------------------------------------------------------------------- |
| 2521 |
* |
| 2522 |
* Access functions for Tcl_StatBuf -- |
| 2523 |
* |
| 2524 |
* These functions provide portable read-only access to the portable |
| 2525 |
* fields of the Tcl_StatBuf structure (really a 'struct stat', 'struct |
| 2526 |
* stat64' or something else related). [TIP #316] |
| 2527 |
* |
| 2528 |
* Results: |
| 2529 |
* The value from the field being retrieved. |
| 2530 |
* |
| 2531 |
* Side effects: |
| 2532 |
* None. |
| 2533 |
* |
| 2534 |
*--------------------------------------------------------------------------- |
| 2535 |
*/ |
| 2536 |
|
| 2537 |
unsigned |
| 2538 |
Tcl_GetFSDeviceFromStat( |
| 2539 |
const Tcl_StatBuf *statPtr) |
| 2540 |
{ |
| 2541 |
return (unsigned)statPtr->st_dev; |
| 2542 |
} |
| 2543 |
|
| 2544 |
unsigned |
| 2545 |
Tcl_GetFSInodeFromStat( |
| 2546 |
const Tcl_StatBuf *statPtr) |
| 2547 |
{ |
| 2548 |
return (unsigned)statPtr->st_ino; |
| 2549 |
} |
| 2550 |
|
| 2551 |
unsigned |
| 2552 |
Tcl_GetModeFromStat( |
| 2553 |
const Tcl_StatBuf *statPtr) |
| 2554 |
{ |
| 2555 |
return (unsigned)statPtr->st_mode; |
| 2556 |
} |
| 2557 |
|
| 2558 |
int |
| 2559 |
Tcl_GetLinkCountFromStat( |
| 2560 |
const Tcl_StatBuf *statPtr) |
| 2561 |
{ |
| 2562 |
return (int)statPtr->st_nlink; |
| 2563 |
} |
| 2564 |
|
| 2565 |
int |
| 2566 |
Tcl_GetUserIdFromStat( |
| 2567 |
const Tcl_StatBuf *statPtr) |
| 2568 |
{ |
| 2569 |
return (int)statPtr->st_uid; |
| 2570 |
} |
| 2571 |
|
| 2572 |
int |
| 2573 |
Tcl_GetGroupIdFromStat( |
| 2574 |
const Tcl_StatBuf *statPtr) |
| 2575 |
{ |
| 2576 |
return (int) statPtr->st_gid; |
| 2577 |
} |
| 2578 |
|
| 2579 |
int |
| 2580 |
Tcl_GetDeviceTypeFromStat( |
| 2581 |
const Tcl_StatBuf *statPtr) |
| 2582 |
{ |
| 2583 |
return (int) statPtr->st_rdev; |
| 2584 |
} |
| 2585 |
|
| 2586 |
Tcl_WideInt |
| 2587 |
Tcl_GetAccessTimeFromStat( |
| 2588 |
const Tcl_StatBuf *statPtr) |
| 2589 |
{ |
| 2590 |
return statPtr->st_atime; |
| 2591 |
} |
| 2592 |
|
| 2593 |
Tcl_WideInt |
| 2594 |
Tcl_GetModificationTimeFromStat( |
| 2595 |
const Tcl_StatBuf *statPtr) |
| 2596 |
{ |
| 2597 |
return statPtr->st_mtime; |
| 2598 |
} |
| 2599 |
|
| 2600 |
Tcl_WideInt |
| 2601 |
Tcl_GetChangeTimeFromStat( |
| 2602 |
const Tcl_StatBuf *statPtr) |
| 2603 |
{ |
| 2604 |
return statPtr->st_ctime; |
| 2605 |
} |
| 2606 |
|
| 2607 |
Tcl_WideUInt |
| 2608 |
Tcl_GetSizeFromStat( |
| 2609 |
const Tcl_StatBuf *statPtr) |
| 2610 |
{ |
| 2611 |
return (Tcl_WideUInt)statPtr->st_size; |
| 2612 |
} |
| 2613 |
|
| 2614 |
Tcl_WideUInt |
| 2615 |
Tcl_GetBlocksFromStat( |
| 2616 |
const Tcl_StatBuf *statPtr) |
| 2617 |
{ |
| 2618 |
#ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
| 2619 |
return (Tcl_WideUInt)statPtr->st_blocks; |
| 2620 |
#else |
| 2621 |
unsigned blksize = Tcl_GetBlockSizeFromStat(statPtr); |
| 2622 |
|
| 2623 |
return ((Tcl_WideUInt)statPtr->st_size + blksize - 1) / blksize; |
| 2624 |
#endif |
| 2625 |
} |
| 2626 |
|
| 2627 |
unsigned |
| 2628 |
Tcl_GetBlockSizeFromStat( |
| 2629 |
const Tcl_StatBuf *statPtr) |
| 2630 |
{ |
| 2631 |
#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 2632 |
return (unsigned) statPtr->st_blksize; |
| 2633 |
#else |
| 2634 |
/* |
| 2635 |
* Not a great guess, but will do... |
| 2636 |
*/ |
| 2637 |
|
| 2638 |
return GUESSED_BLOCK_SIZE; |
| 2639 |
#endif |
| 2640 |
} |
| 2641 |
|
| 2642 |
/* |
| 2643 |
* Local Variables: |
| 2644 |
* mode: c |
| 2645 |
* c-basic-offset: 4 |
| 2646 |
* fill-column: 78 |
| 2647 |
* End: |
| 2648 |
*/ |