Debian Codegraph @

generic/tclCmdAH.c

raw
1 /*
2 * tclCmdAH.c --
3 *
4 * This file contains the top-level command routines for most of the Tcl
5 * built-in commands whose names begin with the letters A to H.
6 *
7 * Copyright (c) 1987-1993 The Regents of the University of California.
8 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
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 #ifdef _WIN32
16 # include "tclWinInt.h"
17 #endif
18 #include <locale.h>
19
20 /*
21 * The state structure used by [foreach]. Note that the actual structure has
22 * all its working arrays appended afterwards so they can be allocated and
23 * freed in a single step.
24 */
25
26 struct ForeachState {
27 Tcl_Obj *bodyPtr; /* The script body of the command. */
28 int bodyIdx; /* The argument index of the body. */
29 int j, maxj; /* Number of loop iterations. */
30 int numLists; /* Count of value lists. */
31 int *index; /* Array of value list indices. */
32 int *varcList; /* # loop variables per list. */
33 Tcl_Obj ***varvList; /* Array of var name lists. */
34 Tcl_Obj **vCopyList; /* Copies of var name list arguments. */
35 int *argcList; /* Array of value list sizes. */
36 Tcl_Obj ***argvList; /* Array of value lists. */
37 Tcl_Obj **aCopyList; /* Copies of value list arguments. */
38 Tcl_Obj *resultList; /* List of result values from the loop body,
39 * or NULL if we're not collecting them
40 * ([lmap] vs [foreach]). */
41 };
42
43 /*
44 * Prototypes for local procedures defined in this file:
45 */
46
47 static int CheckAccess(Tcl_Interp *interp, Tcl_Obj *pathPtr,
48 int mode);
49 static Tcl_ObjCmdProc BadEncodingSubcommand;
50 static Tcl_ObjCmdProc EncodingConvertfromObjCmd;
51 static Tcl_ObjCmdProc EncodingConverttoObjCmd;
52 static Tcl_ObjCmdProc EncodingDirsObjCmd;
53 static Tcl_ObjCmdProc EncodingNamesObjCmd;
54 static Tcl_ObjCmdProc EncodingSystemObjCmd;
55 static inline int ForeachAssignments(Tcl_Interp *interp,
56 struct ForeachState *statePtr);
57 static inline void ForeachCleanup(Tcl_Interp *interp,
58 struct ForeachState *statePtr);
59 static int GetStatBuf(Tcl_Interp *interp, Tcl_Obj *pathPtr,
60 Tcl_FSStatProc *statProc, Tcl_StatBuf *statPtr);
61 static const char * GetTypeFromMode(int mode);
62 static int StoreStatData(Tcl_Interp *interp, Tcl_Obj *varName,
63 Tcl_StatBuf *statPtr);
64 static inline int EachloopCmd(Tcl_Interp *interp, int collect,
65 int objc, Tcl_Obj *const objv[]);
66 static Tcl_NRPostProc CatchObjCmdCallback;
67 static Tcl_NRPostProc ExprCallback;
68 static Tcl_NRPostProc ForSetupCallback;
69 static Tcl_NRPostProc ForCondCallback;
70 static Tcl_NRPostProc ForNextCallback;
71 static Tcl_NRPostProc ForPostNextCallback;
72 static Tcl_NRPostProc ForeachLoopStep;
73 static Tcl_NRPostProc EvalCmdErrMsg;
74
75 static Tcl_ObjCmdProc BadFileSubcommand;
76 static Tcl_ObjCmdProc FileAttrAccessTimeCmd;
77 static Tcl_ObjCmdProc FileAttrIsDirectoryCmd;
78 static Tcl_ObjCmdProc FileAttrIsExecutableCmd;
79 static Tcl_ObjCmdProc FileAttrIsExistingCmd;
80 static Tcl_ObjCmdProc FileAttrIsFileCmd;
81 static Tcl_ObjCmdProc FileAttrIsOwnedCmd;
82 static Tcl_ObjCmdProc FileAttrIsReadableCmd;
83 static Tcl_ObjCmdProc FileAttrIsWritableCmd;
84 static Tcl_ObjCmdProc FileAttrLinkStatCmd;
85 static Tcl_ObjCmdProc FileAttrModifyTimeCmd;
86 static Tcl_ObjCmdProc FileAttrSizeCmd;
87 static Tcl_ObjCmdProc FileAttrStatCmd;
88 static Tcl_ObjCmdProc FileAttrTypeCmd;
89 static Tcl_ObjCmdProc FilesystemSeparatorCmd;
90 static Tcl_ObjCmdProc FilesystemVolumesCmd;
91 static Tcl_ObjCmdProc PathDirNameCmd;
92 static Tcl_ObjCmdProc PathExtensionCmd;
93 static Tcl_ObjCmdProc PathFilesystemCmd;
94 static Tcl_ObjCmdProc PathJoinCmd;
95 static Tcl_ObjCmdProc PathNativeNameCmd;
96 static Tcl_ObjCmdProc PathNormalizeCmd;
97 static Tcl_ObjCmdProc PathRootNameCmd;
98 static Tcl_ObjCmdProc PathSplitCmd;
99 static Tcl_ObjCmdProc PathTailCmd;
100 static Tcl_ObjCmdProc PathTypeCmd;
101
102 /*
103 *----------------------------------------------------------------------
104 *
105 * Tcl_BreakObjCmd --
106 *
107 * This procedure is invoked to process the "break" Tcl command. See the
108 * user documentation for details on what it does.
109 *
110 * With the bytecode compiler, this procedure is only called when a
111 * command name is computed at runtime, and is "break" or the name to
112 * which "break" was renamed: e.g., "set z break; $z"
113 *
114 * Results:
115 * A standard Tcl result.
116 *
117 * Side effects:
118 * See the user documentation.
119 *
120 *----------------------------------------------------------------------
121 */
122
123 int
124 Tcl_BreakObjCmd(
125 ClientData dummy, /* Not used. */
126 Tcl_Interp *interp, /* Current interpreter. */
127 int objc, /* Number of arguments. */
128 Tcl_Obj *const objv[]) /* Argument objects. */
129 {
130 if (objc != 1) {
131 Tcl_WrongNumArgs(interp, 1, objv, NULL);
132 return TCL_ERROR;
133 }
134 return TCL_BREAK;
135 }
136
137 /*
138 *----------------------------------------------------------------------
139 *
140 * Tcl_CaseObjCmd --
141 *
142 * This procedure is invoked to process the "case" Tcl command. See the
143 * user documentation for details on what it does. THIS COMMAND IS
144 * OBSOLETE AND DEPRECATED. SLATED FOR REMOVAL IN TCL 9.0.
145 *
146 * Results:
147 * A standard Tcl object result.
148 *
149 * Side effects:
150 * See the user documentation.
151 *
152 *----------------------------------------------------------------------
153 */
154 int
155 Tcl_CaseObjCmd(
156 ClientData dummy, /* Not used. */
157 Tcl_Interp *interp, /* Current interpreter. */
158 int objc, /* Number of arguments. */
159 Tcl_Obj *const objv[]) /* Argument objects. */
160 {
161 int i;
162 int body, result, caseObjc;
163 const char *stringPtr, *arg;
164 Tcl_Obj *const *caseObjv;
165 Tcl_Obj *armPtr;
166
167 if (objc < 3) {
168 Tcl_WrongNumArgs(interp, 1, objv,
169 "string ?in? ?pattern body ...? ?default body?");
170 return TCL_ERROR;
171 }
172
173 stringPtr = TclGetString(objv[1]);
174 body = -1;
175
176 arg = TclGetString(objv[2]);
177 if (strcmp(arg, "in") == 0) {
178 i = 3;
179 } else {
180 i = 2;
181 }
182 caseObjc = objc - i;
183 caseObjv = objv + i;
184
185 /*
186 * If all of the pattern/command pairs are lumped into a single argument,
187 * split them out again.
188 */
189
190 if (caseObjc == 1) {
191 Tcl_Obj **newObjv;
192
193 TclListObjGetElements(interp, caseObjv[0], &caseObjc, &newObjv);
194 caseObjv = newObjv;
195 }
196
197 for (i = 0; i < caseObjc; i += 2) {
198 int patObjc, j;
199 const char **patObjv;
200 const char *pat, *p;
201
202 if (i == caseObjc-1) {
203 Tcl_ResetResult(interp);
204 Tcl_SetObjResult(interp, Tcl_NewStringObj(
205 "extra case pattern with no body", -1));
206 return TCL_ERROR;
207 }
208
209 /*
210 * Check for special case of single pattern (no list) with no
211 * backslash sequences.
212 */
213
214 pat = TclGetString(caseObjv[i]);
215 for (p = pat; *p != '\0'; p++) {
216 if (TclIsSpaceProcM(*p) || (*p == '\\')) {
217 break;
218 }
219 }
220 if (*p == '\0') {
221 if ((*pat == 'd') && (strcmp(pat, "default") == 0)) {
222 body = i + 1;
223 }
224 if (Tcl_StringMatch(stringPtr, pat)) {
225 body = i + 1;
226 goto match;
227 }
228 continue;
229 }
230
231 /*
232 * Break up pattern lists, then check each of the patterns in the
233 * list.
234 */
235
236 result = Tcl_SplitList(interp, pat, &patObjc, &patObjv);
237 if (result != TCL_OK) {
238 return result;
239 }
240 for (j = 0; j < patObjc; j++) {
241 if (Tcl_StringMatch(stringPtr, patObjv[j])) {
242 body = i + 1;
243 break;
244 }
245 }
246 ckfree(patObjv);
247 if (j < patObjc) {
248 break;
249 }
250 }
251
252 match:
253 if (body != -1) {
254 armPtr = caseObjv[body - 1];
255 result = Tcl_EvalObjEx(interp, caseObjv[body], 0);
256 if (result == TCL_ERROR) {
257 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
258 "\n (\"%.50s\" arm line %d)",
259 TclGetString(armPtr), Tcl_GetErrorLine(interp)));
260 }
261 return result;
262 }
263
264 /*
265 * Nothing matched: return nothing.
266 */
267
268 return TCL_OK;
269 }
270
271 /*
272 *----------------------------------------------------------------------
273 *
274 * Tcl_CatchObjCmd --
275 *
276 * This object-based procedure is invoked to process the "catch" Tcl
277 * command. See the user documentation for details on what it does.
278 *
279 * Results:
280 * A standard Tcl object result.
281 *
282 * Side effects:
283 * See the user documentation.
284 *
285 *----------------------------------------------------------------------
286 */
287
288 int
289 Tcl_CatchObjCmd(
290 ClientData dummy, /* Not used. */
291 Tcl_Interp *interp, /* Current interpreter. */
292 int objc, /* Number of arguments. */
293 Tcl_Obj *const objv[]) /* Argument objects. */
294 {
295 return Tcl_NRCallObjProc(interp, TclNRCatchObjCmd, dummy, objc, objv);
296 }
297
298 int
299 TclNRCatchObjCmd(
300 ClientData dummy, /* Not used. */
301 Tcl_Interp *interp, /* Current interpreter. */
302 int objc, /* Number of arguments. */
303 Tcl_Obj *const objv[]) /* Argument objects. */
304 {
305 Tcl_Obj *varNamePtr = NULL;
306 Tcl_Obj *optionVarNamePtr = NULL;
307 Interp *iPtr = (Interp *) interp;
308
309 if ((objc < 2) || (objc > 4)) {
310 Tcl_WrongNumArgs(interp, 1, objv,
311 "script ?resultVarName? ?optionVarName?");
312 return TCL_ERROR;
313 }
314
315 if (objc >= 3) {
316 varNamePtr = objv[2];
317 }
318 if (objc == 4) {
319 optionVarNamePtr = objv[3];
320 }
321
322 TclNRAddCallback(interp, CatchObjCmdCallback, INT2PTR(objc),
323 varNamePtr, optionVarNamePtr, NULL);
324
325 /*
326 * TIP #280. Make invoking context available to caught script.
327 */
328
329 return TclNREvalObjEx(interp, objv[1], 0, iPtr->cmdFramePtr, 1);
330 }
331
332 static int
333 CatchObjCmdCallback(
334 ClientData data[],
335 Tcl_Interp *interp,
336 int result)
337 {
338 Interp *iPtr = (Interp *) interp;
339 int objc = PTR2INT(data[0]);
340 Tcl_Obj *varNamePtr = (Tcl_Obj *)data[1];
341 Tcl_Obj *optionVarNamePtr = (Tcl_Obj *)data[2];
342 int rewind = iPtr->execEnvPtr->rewind;
343
344 /*
345 * We disable catch in interpreters where the limit has been exceeded.
346 */
347
348 if (rewind || Tcl_LimitExceeded(interp)) {
349 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
350 "\n (\"catch\" body line %d)", Tcl_GetErrorLine(interp)));
351 return TCL_ERROR;
352 }
353
354 if (objc >= 3) {
355 if (NULL == Tcl_ObjSetVar2(interp, varNamePtr, NULL,
356 Tcl_GetObjResult(interp), TCL_LEAVE_ERR_MSG)) {
357 return TCL_ERROR;
358 }
359 }
360 if (objc == 4) {
361 Tcl_Obj *options = Tcl_GetReturnOptions(interp, result);
362
363 if (NULL == Tcl_ObjSetVar2(interp, optionVarNamePtr, NULL,
364 options, TCL_LEAVE_ERR_MSG)) {
365 /* Do not decrRefCount 'options', it was already done by
366 * Tcl_ObjSetVar2 */
367 return TCL_ERROR;
368 }
369 }
370
371 Tcl_ResetResult(interp);
372 Tcl_SetObjResult(interp, Tcl_NewIntObj(result));
373 return TCL_OK;
374 }
375
376 /*
377 *----------------------------------------------------------------------
378 *
379 * Tcl_CdObjCmd --
380 *
381 * This procedure is invoked to process the "cd" Tcl command. See the
382 * user documentation for details on what it does.
383 *
384 * Results:
385 * A standard Tcl result.
386 *
387 * Side effects:
388 * See the user documentation.
389 *
390 *----------------------------------------------------------------------
391 */
392
393 int
394 Tcl_CdObjCmd(
395 ClientData dummy, /* Not used. */
396 Tcl_Interp *interp, /* Current interpreter. */
397 int objc, /* Number of arguments. */
398 Tcl_Obj *const objv[]) /* Argument objects. */
399 {
400 Tcl_Obj *dir;
401 int result;
402
403 if (objc > 2) {
404 Tcl_WrongNumArgs(interp, 1, objv, "?dirName?");
405 return TCL_ERROR;
406 }
407
408 if (objc == 2) {
409 dir = objv[1];
410 } else {
411 TclNewLiteralStringObj(dir, "~");
412 Tcl_IncrRefCount(dir);
413 }
414 if (Tcl_FSConvertToPathType(interp, dir) != TCL_OK) {
415 result = TCL_ERROR;
416 } else {
417 result = Tcl_FSChdir(dir);
418 if (result != TCL_OK) {
419 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
420 "couldn't change working directory to \"%s\": %s",
421 TclGetString(dir), Tcl_PosixError(interp)));
422 result = TCL_ERROR;
423 }
424 }
425 if (objc != 2) {
426 Tcl_DecrRefCount(dir);
427 }
428 return result;
429 }
430
431 /*
432 *----------------------------------------------------------------------
433 *
434 * Tcl_ConcatObjCmd --
435 *
436 * This object-based procedure is invoked to process the "concat" Tcl
437 * command. See the user documentation for details on what it does.
438 *
439 * Results:
440 * A standard Tcl object result.
441 *
442 * Side effects:
443 * See the user documentation.
444 *
445 *----------------------------------------------------------------------
446 */
447
448 int
449 Tcl_ConcatObjCmd(
450 ClientData dummy, /* Not used. */
451 Tcl_Interp *interp, /* Current interpreter. */
452 int objc, /* Number of arguments. */
453 Tcl_Obj *const objv[]) /* Argument objects. */
454 {
455 if (objc >= 2) {
456 Tcl_SetObjResult(interp, Tcl_ConcatObj(objc-1, objv+1));
457 }
458 return TCL_OK;
459 }
460
461 /*
462 *----------------------------------------------------------------------
463 *
464 * Tcl_ContinueObjCmd --
465 *
466 * This procedure is invoked to process the "continue" Tcl command. See
467 * the user documentation for details on what it does.
468 *
469 * With the bytecode compiler, this procedure is only called when a
470 * command name is computed at runtime, and is "continue" or the name to
471 * which "continue" was renamed: e.g., "set z continue; $z"
472 *
473 * Results:
474 * A standard Tcl result.
475 *
476 * Side effects:
477 * See the user documentation.
478 *
479 *----------------------------------------------------------------------
480 */
481
482 int
483 Tcl_ContinueObjCmd(
484 ClientData dummy, /* Not used. */
485 Tcl_Interp *interp, /* Current interpreter. */
486 int objc, /* Number of arguments. */
487 Tcl_Obj *const objv[]) /* Argument objects. */
488 {
489 if (objc != 1) {
490 Tcl_WrongNumArgs(interp, 1, objv, NULL);
491 return TCL_ERROR;
492 }
493 return TCL_CONTINUE;
494 }
495
496 /*
497 *-----------------------------------------------------------------------------
498 *
499 * TclInitEncodingCmd --
500 *
501 * This function creates the 'encoding' ensemble.
502 *
503 * Results:
504 * Returns the Tcl_Command so created.
505 *
506 * Side effects:
507 * The ensemble is initialized.
508 *
509 * This command is hidden in a safe interpreter.
510 */
511
512 Tcl_Command
513 TclInitEncodingCmd(
514 Tcl_Interp* interp) /* Tcl interpreter */
515 {
516 static const EnsembleImplMap encodingImplMap[] = {
517 {"convertfrom", EncodingConvertfromObjCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0},
518 {"convertto", EncodingConverttoObjCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0},
519 {"dirs", EncodingDirsObjCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
520 {"names", EncodingNamesObjCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0},
521 {"system", EncodingSystemObjCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
522 {NULL, NULL, NULL, NULL, NULL, 0}
523 };
524
525 return TclMakeEnsemble(interp, "encoding", encodingImplMap);
526 }
527
528 /*
529 *-----------------------------------------------------------------------------
530 *
531 * TclMakeEncodingCommandSafe --
532 *
533 * This function hides the unsafe 'dirs' and 'system' subcommands of
534 * the "encoding" Tcl command ensemble. It must be called only from
535 * TclHideUnsafeCommands.
536 *
537 * Results:
538 * A standard Tcl result
539 *
540 * Side effects:
541 * Adds commands to the table of hidden commands.
542 *
543 *-----------------------------------------------------------------------------
544 */
545
546 int
547 TclMakeEncodingCommandSafe(
548 Tcl_Interp* interp) /* Tcl interpreter */
549 {
550 static const struct {
551 const char *cmdName;
552 int unsafe;
553 } unsafeInfo[] = {
554 {"convertfrom", 0},
555 {"convertto", 0},
556 {"dirs", 1},
557 {"names", 0},
558 {"system", 0},
559 {NULL, 0}
560 };
561
562 int i;
563 Tcl_DString oldBuf, newBuf;
564
565 Tcl_DStringInit(&oldBuf);
566 TclDStringAppendLiteral(&oldBuf, "::tcl::encoding::");
567 Tcl_DStringInit(&newBuf);
568 TclDStringAppendLiteral(&newBuf, "tcl:encoding:");
569 for (i=0 ; unsafeInfo[i].cmdName != NULL ; i++) {
570 if (unsafeInfo[i].unsafe) {
571 const char *oldName, *newName;
572
573 Tcl_DStringSetLength(&oldBuf, 17);
574 oldName = Tcl_DStringAppend(&oldBuf, unsafeInfo[i].cmdName, -1);
575 Tcl_DStringSetLength(&newBuf, 13);
576 newName = Tcl_DStringAppend(&newBuf, unsafeInfo[i].cmdName, -1);
577 if (TclRenameCommand(interp, oldName, "___tmp") != TCL_OK
578 || Tcl_HideCommand(interp, "___tmp", newName) != TCL_OK) {
579 Tcl_Panic("problem making 'encoding %s' safe: %s",
580 unsafeInfo[i].cmdName,
581 Tcl_GetString(Tcl_GetObjResult(interp)));
582 }
583 Tcl_CreateObjCommand(interp, oldName, BadEncodingSubcommand,
584 (ClientData) unsafeInfo[i].cmdName, NULL);
585 }
586 }
587 Tcl_DStringFree(&oldBuf);
588 Tcl_DStringFree(&newBuf);
589
590 /*
591 * Ugh. The [encoding] command is now actually safe, but it is assumed by
592 * scripts that it is not, which messes up security policies.
593 */
594
595 if (Tcl_HideCommand(interp, "encoding", "encoding") != TCL_OK) {
596 Tcl_Panic("problem making 'encoding' safe: %s",
597 Tcl_GetString(Tcl_GetObjResult(interp)));
598 }
599 return TCL_OK;
600 }
601
602 /*
603 *----------------------------------------------------------------------
604 *
605 * BadEncodingSubcommand --
606 *
607 * Command used to act as a backstop implementation when subcommands of
608 * "encoding" are unsafe (the real implementations of the subcommands are
609 * hidden). The clientData is always the full official subcommand name.
610 *
611 * Results:
612 * A standard Tcl result (always a TCL_ERROR).
613 *
614 * Side effects:
615 * None.
616 *
617 *----------------------------------------------------------------------
618 */
619
620 static int
621 BadEncodingSubcommand(
622 ClientData clientData,
623 Tcl_Interp *interp,
624 int objc,
625 Tcl_Obj *const objv[])
626 {
627 const char *subcommandName = (const char *) clientData;
628
629 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
630 "not allowed to invoke subcommand %s of encoding", subcommandName));
631 Tcl_SetErrorCode(interp, "TCL", "SAFE", "SUBCOMMAND", (char *)NULL);
632 return TCL_ERROR;
633 }
634
635 /*
636 *----------------------------------------------------------------------
637 *
638 * EncodingConvertfromObjCmd --
639 *
640 * This command converts a byte array in an external encoding into a
641 * Tcl string
642 *
643 * Results:
644 * A standard Tcl result.
645 *
646 *----------------------------------------------------------------------
647 */
648
649 int
650 EncodingConvertfromObjCmd(
651 ClientData dummy, /* Not used. */
652 Tcl_Interp *interp, /* Current interpreter. */
653 int objc, /* Number of arguments. */
654 Tcl_Obj *const objv[]) /* Argument objects. */
655 {
656 Tcl_Obj *data; /* Byte array to convert */
657 Tcl_DString ds; /* Buffer to hold the string */
658 Tcl_Encoding encoding; /* Encoding to use */
659 int length; /* Length of the byte array being converted */
660 const char *bytesPtr; /* Pointer to the first byte of the array */
661
662 if (objc == 2) {
663 encoding = Tcl_GetEncoding(interp, NULL);
664 data = objv[1];
665 } else if (objc == 3) {
666 if (Tcl_GetEncodingFromObj(interp, objv[1], &encoding) != TCL_OK) {
667 return TCL_ERROR;
668 }
669 data = objv[2];
670 } else {
671 Tcl_WrongNumArgs(interp, 1, objv, "?encoding? data");
672 return TCL_ERROR;
673 }
674
675 /*
676 * Convert the string into a byte array in 'ds'
677 */
678 bytesPtr = (char *) Tcl_GetByteArrayFromObj(data, &length);
679 Tcl_ExternalToUtfDString(encoding, bytesPtr, length, &ds);
680
681 /*
682 * Note that we cannot use Tcl_DStringResult here because it will
683 * truncate the string at the first null byte.
684 */
685
686 Tcl_SetObjResult(interp, TclDStringToObj(&ds));
687
688 /*
689 * We're done with the encoding
690 */
691
692 Tcl_FreeEncoding(encoding);
693 return TCL_OK;
694
695 }
696
697 /*
698 *----------------------------------------------------------------------
699 *
700 * EncodingConverttoObjCmd --
701 *
702 * This command converts a Tcl string into a byte array that
703 * encodes the string according to some encoding.
704 *
705 * Results:
706 * A standard Tcl result.
707 *
708 *----------------------------------------------------------------------
709 */
710
711 int
712 EncodingConverttoObjCmd(
713 ClientData dummy, /* Not used. */
714 Tcl_Interp *interp, /* Current interpreter. */
715 int objc, /* Number of arguments. */
716 Tcl_Obj *const objv[]) /* Argument objects. */
717 {
718 Tcl_Obj *data; /* String to convert */
719 Tcl_DString ds; /* Buffer to hold the byte array */
720 Tcl_Encoding encoding; /* Encoding to use */
721 int length; /* Length of the string being converted */
722 const char *stringPtr; /* Pointer to the first byte of the string */
723
724 if (objc == 2) {
725 encoding = Tcl_GetEncoding(interp, NULL);
726 data = objv[1];
727 } else if (objc == 3) {
728 if (Tcl_GetEncodingFromObj(interp, objv[1], &encoding) != TCL_OK) {
729 return TCL_ERROR;
730 }
731 data = objv[2];
732 } else {
733 Tcl_WrongNumArgs(interp, 1, objv, "?encoding? data");
734 return TCL_ERROR;
735 }
736
737 /*
738 * Convert the string to a byte array in 'ds'
739 */
740
741 stringPtr = TclGetStringFromObj(data, &length);
742 Tcl_UtfToExternalDString(encoding, stringPtr, length, &ds);
743 Tcl_SetObjResult(interp,
744 Tcl_NewByteArrayObj((unsigned char*) Tcl_DStringValue(&ds),
745 Tcl_DStringLength(&ds)));
746 Tcl_DStringFree(&ds);
747
748 /*
749 * We're done with the encoding
750 */
751
752 Tcl_FreeEncoding(encoding);
753 return TCL_OK;
754
755 }
756
757 /*
758 *----------------------------------------------------------------------
759 *
760 * EncodingDirsObjCmd --
761 *
762 * This command manipulates the encoding search path.
763 *
764 * Results:
765 * A standard Tcl result.
766 *
767 * Side effects:
768 * Can set the encoding search path.
769 *
770 *----------------------------------------------------------------------
771 */
772
773 int
774 EncodingDirsObjCmd(
775 ClientData dummy, /* Not used. */
776 Tcl_Interp *interp, /* Current interpreter. */
777 int objc, /* Number of arguments. */
778 Tcl_Obj *const objv[]) /* Argument objects. */
779 {
780 Tcl_Obj *dirListObj;
781
782 if (objc > 2) {
783 Tcl_WrongNumArgs(interp, 1, objv, "?dirList?");
784 return TCL_ERROR;
785 }
786 if (objc == 1) {
787 Tcl_SetObjResult(interp, Tcl_GetEncodingSearchPath());
788 return TCL_OK;
789 }
790
791 dirListObj = objv[1];
792 if (Tcl_SetEncodingSearchPath(dirListObj) == TCL_ERROR) {
793 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
794 "expected directory list but got \"%s\"",
795 TclGetString(dirListObj)));
796 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "ENCODING", "BADPATH",
797 (char *)NULL);
798 return TCL_ERROR;
799 }
800 Tcl_SetObjResult(interp, dirListObj);
801 return TCL_OK;
802 }
803
804 /*
805 *-----------------------------------------------------------------------------
806 *
807 * EncodingNamesObjCmd --
808 *
809 * This command returns a list of the available encoding names
810 *
811 * Results:
812 * Returns a standard Tcl result
813 *
814 *-----------------------------------------------------------------------------
815 */
816
817 int
818 EncodingNamesObjCmd(
819 ClientData dummy, /* Unused */
820 Tcl_Interp* interp, /* Tcl interpreter */
821 int objc, /* Number of command line args */
822 Tcl_Obj* const objv[]) /* Vector of command line args */
823 {
824 if (objc > 1) {
825 Tcl_WrongNumArgs(interp, 1, objv, NULL);
826 return TCL_ERROR;
827 }
828 Tcl_GetEncodingNames(interp);
829 return TCL_OK;
830 }
831
832 /*
833 *-----------------------------------------------------------------------------
834 *
835 * EncodingSystemObjCmd --
836 *
837 * This command retrieves or changes the system encoding
838 *
839 * Results:
840 * Returns a standard Tcl result
841 *
842 * Side effects:
843 * May change the system encoding.
844 *
845 *-----------------------------------------------------------------------------
846 */
847
848 int
849 EncodingSystemObjCmd(
850 ClientData dummy, /* Unused */
851 Tcl_Interp* interp, /* Tcl interpreter */
852 int objc, /* Number of command line args */
853 Tcl_Obj* const objv[]) /* Vector of command line args */
854 {
855 if (objc > 2) {
856 Tcl_WrongNumArgs(interp, 1, objv, "?encoding?");
857 return TCL_ERROR;
858 }
859 if (objc == 1) {
860 Tcl_SetObjResult(interp,
861 Tcl_NewStringObj(Tcl_GetEncodingName(NULL), -1));
862 } else {
863 return Tcl_SetSystemEncoding(interp, TclGetString(objv[1]));
864 }
865 return TCL_OK;
866 }
867
868 /*
869 *----------------------------------------------------------------------
870 *
871 * Tcl_ErrorObjCmd --
872 *
873 * This procedure is invoked to process the "error" Tcl command. See the
874 * user documentation for details on what it does.
875 *
876 * Results:
877 * A standard Tcl object result.
878 *
879 * Side effects:
880 * See the user documentation.
881 *
882 *----------------------------------------------------------------------
883 */
884
885 int
886 Tcl_ErrorObjCmd(
887 ClientData dummy, /* Not used. */
888 Tcl_Interp *interp, /* Current interpreter. */
889 int objc, /* Number of arguments. */
890 Tcl_Obj *const objv[]) /* Argument objects. */
891 {
892 Tcl_Obj *options, *optName;
893
894 if ((objc < 2) || (objc > 4)) {
895 Tcl_WrongNumArgs(interp, 1, objv, "message ?errorInfo? ?errorCode?");
896 return TCL_ERROR;
897 }
898
899 TclNewLiteralStringObj(options, "-code error -level 0");
900
901 if (objc >= 3) { /* Process the optional info argument */
902 TclNewLiteralStringObj(optName, "-errorinfo");
903 Tcl_ListObjAppendElement(NULL, options, optName);
904 Tcl_ListObjAppendElement(NULL, options, objv[2]);
905 }
906
907 if (objc >= 4) { /* Process the optional code argument */
908 TclNewLiteralStringObj(optName, "-errorcode");
909 Tcl_ListObjAppendElement(NULL, options, optName);
910 Tcl_ListObjAppendElement(NULL, options, objv[3]);
911 }
912
913 Tcl_SetObjResult(interp, objv[1]);
914 return Tcl_SetReturnOptions(interp, options);
915 }
916
917 /*
918 *----------------------------------------------------------------------
919 *
920 * Tcl_EvalObjCmd --
921 *
922 * This object-based procedure is invoked to process the "eval" Tcl
923 * command. See the user documentation for details on what it does.
924 *
925 * Results:
926 * A standard Tcl object result.
927 *
928 * Side effects:
929 * See the user documentation.
930 *
931 *----------------------------------------------------------------------
932 */
933
934 static int
935 EvalCmdErrMsg(
936 ClientData data[],
937 Tcl_Interp *interp,
938 int result)
939 {
940 if (result == TCL_ERROR) {
941 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
942 "\n (\"eval\" body line %d)", Tcl_GetErrorLine(interp)));
943 }
944 return result;
945 }
946
947 int
948 Tcl_EvalObjCmd(
949 ClientData dummy, /* Not used. */
950 Tcl_Interp *interp, /* Current interpreter. */
951 int objc, /* Number of arguments. */
952 Tcl_Obj *const objv[]) /* Argument objects. */
953 {
954 return Tcl_NRCallObjProc(interp, TclNREvalObjCmd, dummy, objc, objv);
955 }
956
957 int
958 TclNREvalObjCmd(
959 ClientData dummy, /* Not used. */
960 Tcl_Interp *interp, /* Current interpreter. */
961 int objc, /* Number of arguments. */
962 Tcl_Obj *const objv[]) /* Argument objects. */
963 {
964 Tcl_Obj *objPtr;
965 Interp *iPtr = (Interp *) interp;
966 CmdFrame *invoker = NULL;
967 int word = 0;
968
969 if (objc < 2) {
970 Tcl_WrongNumArgs(interp, 1, objv, "arg ?arg ...?");
971 return TCL_ERROR;
972 }
973
974 if (objc == 2) {
975 /*
976 * TIP #280. Make argument location available to eval'd script.
977 */
978
979 invoker = iPtr->cmdFramePtr;
980 word = 1;
981 objPtr = objv[1];
982 TclArgumentGet(interp, objPtr, &invoker, &word);
983 } else {
984 /*
985 * More than one argument: concatenate them together with spaces
986 * between, then evaluate the result. Tcl_EvalObjEx will delete the
987 * object when it decrements its refcount after eval'ing it.
988 *
989 * TIP #280. Make invoking context available to eval'd script, done
990 * with the default values.
991 */
992
993 objPtr = Tcl_ConcatObj(objc-1, objv+1);
994 }
995 TclNRAddCallback(interp, EvalCmdErrMsg, NULL, NULL, NULL, NULL);
996 return TclNREvalObjEx(interp, objPtr, 0, invoker, word);
997 }
998
999 /*
1000 *----------------------------------------------------------------------
1001 *
1002 * Tcl_ExitObjCmd --
1003 *
1004 * This procedure is invoked to process the "exit" Tcl command. See the
1005 * user documentation for details on what it does.
1006 *
1007 * Results:
1008 * A standard Tcl object result.
1009 *
1010 * Side effects:
1011 * See the user documentation.
1012 *
1013 *----------------------------------------------------------------------
1014 */
1015
1016 int
1017 Tcl_ExitObjCmd(
1018 ClientData dummy, /* Not used. */
1019 Tcl_Interp *interp, /* Current interpreter. */
1020 int objc, /* Number of arguments. */
1021 Tcl_Obj *const objv[]) /* Argument objects. */
1022 {
1023 int value;
1024
1025 if ((objc != 1) && (objc != 2)) {
1026 Tcl_WrongNumArgs(interp, 1, objv, "?returnCode?");
1027 return TCL_ERROR;
1028 }
1029
1030 if (objc == 1) {
1031 value = 0;
1032 } else if (Tcl_GetIntFromObj(interp, objv[1], &value) != TCL_OK) {
1033 return TCL_ERROR;
1034 }
1035 Tcl_Exit(value);
1036 return TCL_OK; /* Better not ever reach this! */
1037 }
1038
1039 /*
1040 *----------------------------------------------------------------------
1041 *
1042 * Tcl_ExprObjCmd --
1043 *
1044 * This object-based procedure is invoked to process the "expr" Tcl
1045 * command. See the user documentation for details on what it does.
1046 *
1047 * With the bytecode compiler, this procedure is called in two
1048 * circumstances: 1) to execute expr commands that are too complicated or
1049 * too unsafe to try compiling directly into an inline sequence of
1050 * instructions, and 2) to execute commands where the command name is
1051 * computed at runtime and is "expr" or the name to which "expr" was
1052 * renamed (e.g., "set z expr; $z 2+3")
1053 *
1054 * Results:
1055 * A standard Tcl object result.
1056 *
1057 * Side effects:
1058 * See the user documentation.
1059 *
1060 *----------------------------------------------------------------------
1061 */
1062
1063 int
1064 Tcl_ExprObjCmd(
1065 ClientData dummy, /* Not used. */
1066 Tcl_Interp *interp, /* Current interpreter. */
1067 int objc, /* Number of arguments. */
1068 Tcl_Obj *const objv[]) /* Argument objects. */
1069 {
1070 return Tcl_NRCallObjProc(interp, TclNRExprObjCmd, dummy, objc, objv);
1071 }
1072
1073 int
1074 TclNRExprObjCmd(
1075 ClientData dummy, /* Not used. */
1076 Tcl_Interp *interp, /* Current interpreter. */
1077 int objc, /* Number of arguments. */
1078 Tcl_Obj *const objv[]) /* Argument objects. */
1079 {
1080 Tcl_Obj *resultPtr, *objPtr;
1081
1082 if (objc < 2) {
1083 Tcl_WrongNumArgs(interp, 1, objv, "arg ?arg ...?");
1084 return TCL_ERROR;
1085 }
1086
1087 TclNewObj(resultPtr);
1088 Tcl_IncrRefCount(resultPtr);
1089 if (objc == 2) {
1090 objPtr = objv[1];
1091 TclNRAddCallback(interp, ExprCallback, resultPtr, NULL, NULL, NULL);
1092 } else {
1093 objPtr = Tcl_ConcatObj(objc-1, objv+1);
1094 TclNRAddCallback(interp, ExprCallback, resultPtr, objPtr, NULL, NULL);
1095 }
1096
1097 return Tcl_NRExprObj(interp, objPtr, resultPtr);
1098 }
1099
1100 static int
1101 ExprCallback(
1102 ClientData data[],
1103 Tcl_Interp *interp,
1104 int result)
1105 {
1106 Tcl_Obj *resultPtr = (Tcl_Obj *)data[0];
1107 Tcl_Obj *objPtr = (Tcl_Obj *)data[1];
1108
1109 if (objPtr != NULL) {
1110 Tcl_DecrRefCount(objPtr);
1111 }
1112
1113 if (result == TCL_OK) {
1114 Tcl_SetObjResult(interp, resultPtr);
1115 }
1116 Tcl_DecrRefCount(resultPtr);
1117 return result;
1118 }
1119
1120 /*
1121 *----------------------------------------------------------------------
1122 *
1123 * TclInitFileCmd --
1124 *
1125 * This function builds the "file" Tcl command ensemble. See the user
1126 * documentation for details on what that ensemble does.
1127 *
1128 * PLEASE NOTE THAT THIS FAILS WITH FILENAMES AND PATHS WITH EMBEDDED
1129 * NULLS. With the object-based Tcl_FS APIs, the above NOTE may no longer
1130 * be true. In any case this assertion should be tested.
1131 *
1132 * Results:
1133 * A standard Tcl result.
1134 *
1135 * Side effects:
1136 * See the user documentation.
1137 *
1138 *----------------------------------------------------------------------
1139 */
1140
1141 Tcl_Command
1142 TclInitFileCmd(
1143 Tcl_Interp *interp)
1144 {
1145 /*
1146 * Note that most subcommands are unsafe because either they manipulate
1147 * the native filesystem or because they reveal information about the
1148 * native filesystem.
1149 */
1150
1151 static const EnsembleImplMap initMap[] = {
1152 {"atime", FileAttrAccessTimeCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0},
1153 {"attributes", TclFileAttrsCmd, NULL, NULL, NULL, 0},
1154 {"channels", TclChannelNamesCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
1155 {"copy", TclFileCopyCmd, NULL, NULL, NULL, 0},
1156 {"delete", TclFileDeleteCmd, TclCompileBasicMin0ArgCmd, NULL, NULL, 0},
1157 {"dirname", PathDirNameCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1158 {"executable", FileAttrIsExecutableCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1159 {"exists", FileAttrIsExistingCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1160 {"extension", PathExtensionCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1161 {"isdirectory", FileAttrIsDirectoryCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1162 {"isfile", FileAttrIsFileCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1163 {"join", PathJoinCmd, TclCompileBasicMin1ArgCmd, NULL, NULL, 0},
1164 {"link", TclFileLinkCmd, TclCompileBasic1To3ArgCmd, NULL, NULL, 0},
1165 {"lstat", FileAttrLinkStatCmd, TclCompileBasic2ArgCmd, NULL, NULL, 0},
1166 {"mtime", FileAttrModifyTimeCmd, TclCompileBasic1Or2ArgCmd, NULL, NULL, 0},
1167 {"mkdir", TclFileMakeDirsCmd, TclCompileBasicMin0ArgCmd, NULL, NULL, 0},
1168 {"nativename", PathNativeNameCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1169 {"normalize", PathNormalizeCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1170 {"owned", FileAttrIsOwnedCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1171 {"pathtype", PathTypeCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1172 {"readable", FileAttrIsReadableCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1173 {"readlink", TclFileReadLinkCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1174 {"rename", TclFileRenameCmd, NULL, NULL, NULL, 0},
1175 {"rootname", PathRootNameCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1176 {"separator", FilesystemSeparatorCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
1177 {"size", FileAttrSizeCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1178 {"split", PathSplitCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1179 {"stat", FileAttrStatCmd, TclCompileBasic2ArgCmd, NULL, NULL, 0},
1180 {"system", PathFilesystemCmd, TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
1181 {"tail", PathTailCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1182 {"tempfile", TclFileTemporaryCmd, TclCompileBasic0To2ArgCmd, NULL, NULL, 0},
1183 {"type", FileAttrTypeCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1184 {"volumes", FilesystemVolumesCmd, TclCompileBasic0ArgCmd, NULL, NULL, 0},
1185 {"writable", FileAttrIsWritableCmd, TclCompileBasic1ArgCmd, NULL, NULL, 0},
1186 {NULL, NULL, NULL, NULL, NULL, 0}
1187 };
1188 return TclMakeEnsemble(interp, "file", initMap);
1189 }
1190
1191 /*
1192 *----------------------------------------------------------------------
1193 *
1194 * TclMakeFileCommandSafe --
1195 *
1196 * This function hides the unsafe subcommands of the "file" Tcl command
1197 * ensemble. It must only be called from TclHideUnsafeCommands.
1198 *
1199 * Results:
1200 * A standard Tcl result.
1201 *
1202 * Side effects:
1203 * Adds commands to the table of hidden commands.
1204 *
1205 *----------------------------------------------------------------------
1206 */
1207
1208 int
1209 TclMakeFileCommandSafe(
1210 Tcl_Interp *interp)
1211 {
1212 static const struct {
1213 const char *cmdName;
1214 int unsafe;
1215 } unsafeInfo[] = {
1216 {"atime", 1},
1217 {"attributes", 1},
1218 {"channels", 0},
1219 {"copy", 1},
1220 {"delete", 1},
1221 {"dirname", 1},
1222 {"executable", 1},
1223 {"exists", 1},
1224 {"extension", 1},
1225 {"isdirectory", 1},
1226 {"isfile", 1},
1227 {"join", 0},
1228 {"link", 1},
1229 {"lstat", 1},
1230 {"mtime", 1},
1231 {"mkdir", 1},
1232 {"nativename", 1},
1233 {"normalize", 1},
1234 {"owned", 1},
1235 {"pathtype", 0},
1236 {"readable", 1},
1237 {"readlink", 1},
1238 {"rename", 1},
1239 {"rootname", 1},
1240 {"separator", 0},
1241 {"size", 1},
1242 {"split", 0},
1243 {"stat", 1},
1244 {"system", 0},
1245 {"tail", 1},
1246 {"tempfile", 1},
1247 {"type", 1},
1248 {"volumes", 1},
1249 {"writable", 1},
1250 {NULL, 0}
1251 };
1252 int i;
1253 Tcl_DString oldBuf, newBuf;
1254
1255 Tcl_DStringInit(&oldBuf);
1256 TclDStringAppendLiteral(&oldBuf, "::tcl::file::");
1257 Tcl_DStringInit(&newBuf);
1258 TclDStringAppendLiteral(&newBuf, "tcl:file:");
1259 for (i=0 ; unsafeInfo[i].cmdName != NULL ; i++) {
1260 if (unsafeInfo[i].unsafe) {
1261 const char *oldName, *newName;
1262
1263 Tcl_DStringSetLength(&oldBuf, 13);
1264 oldName = Tcl_DStringAppend(&oldBuf, unsafeInfo[i].cmdName, -1);
1265 Tcl_DStringSetLength(&newBuf, 9);
1266 newName = Tcl_DStringAppend(&newBuf, unsafeInfo[i].cmdName, -1);
1267 if (TclRenameCommand(interp, oldName, "___tmp") != TCL_OK
1268 || Tcl_HideCommand(interp, "___tmp", newName) != TCL_OK) {
1269 Tcl_Panic("problem making 'file %s' safe: %s",
1270 unsafeInfo[i].cmdName,
1271 Tcl_GetString(Tcl_GetObjResult(interp)));
1272 }
1273 Tcl_CreateObjCommand(interp, oldName, BadFileSubcommand,
1274 (ClientData) unsafeInfo[i].cmdName, NULL);
1275 }
1276 }
1277 Tcl_DStringFree(&oldBuf);
1278 Tcl_DStringFree(&newBuf);
1279
1280 /*
1281 * Ugh. The [file] command is now actually safe, but it is assumed by
1282 * scripts that it is not, which messes up security policies. [Bug
1283 * 3211758]
1284 */
1285
1286 if (Tcl_HideCommand(interp, "file", "file") != TCL_OK) {
1287 Tcl_Panic("problem making 'file' safe: %s",
1288 Tcl_GetString(Tcl_GetObjResult(interp)));
1289 }
1290 return TCL_OK;
1291 }
1292
1293 /*
1294 *----------------------------------------------------------------------
1295 *
1296 * BadFileSubcommand --
1297 *
1298 * Command used to act as a backstop implementation when subcommands of
1299 * "file" are unsafe (the real implementations of the subcommands are
1300 * hidden). The clientData is always the full official subcommand name.
1301 *
1302 * Results:
1303 * A standard Tcl result (always a TCL_ERROR).
1304 *
1305 * Side effects:
1306 * None.
1307 *
1308 *----------------------------------------------------------------------
1309 */
1310
1311 static int
1312 BadFileSubcommand(
1313 ClientData clientData,
1314 Tcl_Interp *interp,
1315 int objc,
1316 Tcl_Obj *const objv[])
1317 {
1318 const char *subcommandName = (const char *) clientData;
1319
1320 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1321 "not allowed to invoke subcommand %s of file", subcommandName));
1322 Tcl_SetErrorCode(interp, "TCL", "SAFE", "SUBCOMMAND", (char *)NULL);
1323 return TCL_ERROR;
1324 }
1325
1326 /*
1327 *----------------------------------------------------------------------
1328 *
1329 * FileAttrAccessTimeCmd --
1330 *
1331 * This function is invoked to process the "file atime" Tcl command. See
1332 * the user documentation for details on what it does.
1333 *
1334 * Results:
1335 * A standard Tcl result.
1336 *
1337 * Side effects:
1338 * May update the access time on the file, if requested by the user.
1339 *
1340 *----------------------------------------------------------------------
1341 */
1342
1343 static int
1344 FileAttrAccessTimeCmd(
1345 ClientData clientData,
1346 Tcl_Interp *interp,
1347 int objc,
1348 Tcl_Obj *const objv[])
1349 {
1350 Tcl_StatBuf buf;
1351 struct utimbuf tval;
1352
1353 if (objc < 2 || objc > 3) {
1354 Tcl_WrongNumArgs(interp, 1, objv, "name ?time?");
1355 return TCL_ERROR;
1356 }
1357 if (GetStatBuf(interp, objv[1], Tcl_FSStat, &buf) != TCL_OK) {
1358 return TCL_ERROR;
1359 }
1360 #if defined(_WIN32)
1361 /* We use a value of 0 to indicate the access time not available */
1362 if (Tcl_GetAccessTimeFromStat(&buf) == 0) {
1363 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1364 "could not get access time for file \"%s\"",
1365 TclGetString(objv[1])));
1366 return TCL_ERROR;
1367 }
1368 #endif
1369
1370 if (objc == 3) {
1371 Tcl_WideInt newTime;
1372
1373 if (Tcl_GetWideIntFromObj(interp, objv[2], &newTime) != TCL_OK) {
1374 return TCL_ERROR;
1375 }
1376
1377 tval.actime = newTime;
1378 tval.modtime = Tcl_GetModificationTimeFromStat(&buf);
1379
1380 if (Tcl_FSUtime(objv[1], &tval) != 0) {
1381 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1382 "could not set access time for file \"%s\": %s",
1383 TclGetString(objv[1]), Tcl_PosixError(interp)));
1384 return TCL_ERROR;
1385 }
1386
1387 /*
1388 * Do another stat to ensure that the we return the new recognized
1389 * atime - hopefully the same as the one we sent in. However, fs's
1390 * like FAT don't even know what atime is.
1391 */
1392
1393 if (GetStatBuf(interp, objv[1], Tcl_FSStat, &buf) != TCL_OK) {
1394 return TCL_ERROR;
1395 }
1396 }
1397
1398 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(Tcl_GetAccessTimeFromStat(&buf)));
1399 return TCL_OK;
1400 }
1401
1402 /*
1403 *----------------------------------------------------------------------
1404 *
1405 * FileAttrModifyTimeCmd --
1406 *
1407 * This function is invoked to process the "file mtime" Tcl command. See
1408 * the user documentation for details on what it does.
1409 *
1410 * Results:
1411 * A standard Tcl result.
1412 *
1413 * Side effects:
1414 * May update the modification time on the file, if requested by the
1415 * user.
1416 *
1417 *----------------------------------------------------------------------
1418 */
1419
1420 static int
1421 FileAttrModifyTimeCmd(
1422 ClientData clientData,
1423 Tcl_Interp *interp,
1424 int objc,
1425 Tcl_Obj *const objv[])
1426 {
1427 Tcl_StatBuf buf;
1428 struct utimbuf tval;
1429
1430 if (objc < 2 || objc > 3) {
1431 Tcl_WrongNumArgs(interp, 1, objv, "name ?time?");
1432 return TCL_ERROR;
1433 }
1434 if (GetStatBuf(interp, objv[1], Tcl_FSStat, &buf) != TCL_OK) {
1435 return TCL_ERROR;
1436 }
1437 #if defined(_WIN32)
1438 /* We use a value of 0 to indicate the modification time not available */
1439 if (Tcl_GetModificationTimeFromStat(&buf) == 0) {
1440 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1441 "could not get modification time for file \"%s\"",
1442 TclGetString(objv[1])));
1443 return TCL_ERROR;
1444 }
1445 #endif
1446 if (objc == 3) {
1447 /*
1448 * Need separate variable for reading longs from an object on 64-bit
1449 * platforms. [Bug 698146]
1450 */
1451
1452 Tcl_WideInt newTime;
1453
1454 if (Tcl_GetWideIntFromObj(interp, objv[2], &newTime) != TCL_OK) {
1455 return TCL_ERROR;
1456 }
1457
1458 tval.actime = Tcl_GetAccessTimeFromStat(&buf);
1459 tval.modtime = newTime;
1460
1461 if (Tcl_FSUtime(objv[1], &tval) != 0) {
1462 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1463 "could not set modification time for file \"%s\": %s",
1464 TclGetString(objv[1]), Tcl_PosixError(interp)));
1465 return TCL_ERROR;
1466 }
1467
1468 /*
1469 * Do another stat to ensure that the we return the new recognized
1470 * mtime - hopefully the same as the one we sent in.
1471 */
1472
1473 if (GetStatBuf(interp, objv[1], Tcl_FSStat, &buf) != TCL_OK) {
1474 return TCL_ERROR;
1475 }
1476 }
1477
1478 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(Tcl_GetModificationTimeFromStat(&buf)));
1479 return TCL_OK;
1480 }
1481
1482 /*
1483 *----------------------------------------------------------------------
1484 *
1485 * FileAttrLinkStatCmd --
1486 *
1487 * This function is invoked to process the "file lstat" Tcl command. See
1488 * the user documentation for details on what it does.
1489 *
1490 * Results:
1491 * A standard Tcl result.
1492 *
1493 * Side effects:
1494 * Writes to an array named by the user.
1495 *
1496 *----------------------------------------------------------------------
1497 */
1498
1499 static int
1500 FileAttrLinkStatCmd(
1501 ClientData clientData,
1502 Tcl_Interp *interp,
1503 int objc,
1504 Tcl_Obj *const objv[])
1505 {
1506 Tcl_StatBuf buf;
1507
1508 if (objc != 3) {
1509 Tcl_WrongNumArgs(interp, 1, objv, "name varName");
1510 return TCL_ERROR;
1511 }
1512 if (GetStatBuf(interp, objv[1], Tcl_FSLstat, &buf) != TCL_OK) {
1513 return TCL_ERROR;
1514 }
1515 return StoreStatData(interp, objv[2], &buf);
1516 }
1517
1518 /*
1519 *----------------------------------------------------------------------
1520 *
1521 * FileAttrStatCmd --
1522 *
1523 * This function is invoked to process the "file stat" Tcl command. See
1524 * the user documentation for details on what it does.
1525 *
1526 * Results:
1527 * A standard Tcl result.
1528 *
1529 * Side effects:
1530 * Writes to an array named by the user.
1531 *
1532 *----------------------------------------------------------------------
1533 */
1534
1535 static int
1536 FileAttrStatCmd(
1537 ClientData clientData,
1538 Tcl_Interp *interp,
1539 int objc,
1540 Tcl_Obj *const objv[])
1541 {
1542 Tcl_StatBuf buf;
1543
1544 if (objc != 3) {
1545 Tcl_WrongNumArgs(interp, 1, objv, "name varName");
1546 return TCL_ERROR;
1547 }
1548 if (GetStatBuf(interp, objv[1], Tcl_FSStat, &buf) != TCL_OK) {
1549 return TCL_ERROR;
1550 }
1551 return StoreStatData(interp, objv[2], &buf);
1552 }
1553
1554 /*
1555 *----------------------------------------------------------------------
1556 *
1557 * FileAttrTypeCmd --
1558 *
1559 * This function is invoked to process the "file type" Tcl command. See
1560 * the user documentation for details on what it does.
1561 *
1562 * Results:
1563 * A standard Tcl result.
1564 *
1565 * Side effects:
1566 * None.
1567 *
1568 *----------------------------------------------------------------------
1569 */
1570
1571 static int
1572 FileAttrTypeCmd(
1573 ClientData clientData,
1574 Tcl_Interp *interp,
1575 int objc,
1576 Tcl_Obj *const objv[])
1577 {
1578 Tcl_StatBuf buf;
1579
1580 if (objc != 2) {
1581 Tcl_WrongNumArgs(interp, 1, objv, "name");
1582 return TCL_ERROR;
1583 }
1584 if (GetStatBuf(interp, objv[1], Tcl_FSLstat, &buf) != TCL_OK) {
1585 return TCL_ERROR;
1586 }
1587 Tcl_SetObjResult(interp, Tcl_NewStringObj(
1588 GetTypeFromMode((unsigned short) buf.st_mode), -1));
1589 return TCL_OK;
1590 }
1591
1592 /*
1593 *----------------------------------------------------------------------
1594 *
1595 * FileAttrSizeCmd --
1596 *
1597 * This function is invoked to process the "file size" Tcl command. See
1598 * the user documentation for details on what it does.
1599 *
1600 * Results:
1601 * A standard Tcl result.
1602 *
1603 * Side effects:
1604 * None.
1605 *
1606 *----------------------------------------------------------------------
1607 */
1608
1609 static int
1610 FileAttrSizeCmd(
1611 ClientData clientData,
1612 Tcl_Interp *interp,
1613 int objc,
1614 Tcl_Obj *const objv[])
1615 {
1616 Tcl_StatBuf buf;
1617
1618 if (objc != 2) {
1619 Tcl_WrongNumArgs(interp, 1, objv, "name");
1620 return TCL_ERROR;
1621 }
1622 if (GetStatBuf(interp, objv[1], Tcl_FSStat, &buf) != TCL_OK) {
1623 return TCL_ERROR;
1624 }
1625 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(buf.st_size));
1626 return TCL_OK;
1627 }
1628
1629 /*
1630 *----------------------------------------------------------------------
1631 *
1632 * FileAttrIsDirectoryCmd --
1633 *
1634 * This function is invoked to process the "file isdirectory" Tcl
1635 * command. See the user documentation for details on what it does.
1636 *
1637 * Results:
1638 * A standard Tcl result.
1639 *
1640 * Side effects:
1641 * None.
1642 *
1643 *----------------------------------------------------------------------
1644 */
1645
1646 static int
1647 FileAttrIsDirectoryCmd(
1648 ClientData clientData,
1649 Tcl_Interp *interp,
1650 int objc,
1651 Tcl_Obj *const objv[])
1652 {
1653 Tcl_StatBuf buf;
1654 int value = 0;
1655
1656 if (objc != 2) {
1657 Tcl_WrongNumArgs(interp, 1, objv, "name");
1658 return TCL_ERROR;
1659 }
1660 if (GetStatBuf(NULL, objv[1], Tcl_FSStat, &buf) == TCL_OK) {
1661 value = S_ISDIR(buf.st_mode);
1662 }
1663 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(value));
1664 return TCL_OK;
1665 }
1666
1667 /*
1668 *----------------------------------------------------------------------
1669 *
1670 * FileAttrIsExecutableCmd --
1671 *
1672 * This function is invoked to process the "file executable" Tcl command.
1673 * See the user documentation for details on what it does.
1674 *
1675 * Results:
1676 * A standard Tcl result.
1677 *
1678 * Side effects:
1679 * None.
1680 *
1681 *----------------------------------------------------------------------
1682 */
1683
1684 static int
1685 FileAttrIsExecutableCmd(
1686 ClientData clientData,
1687 Tcl_Interp *interp,
1688 int objc,
1689 Tcl_Obj *const objv[])
1690 {
1691 if (objc != 2) {
1692 Tcl_WrongNumArgs(interp, 1, objv, "name");
1693 return TCL_ERROR;
1694 }
1695 return CheckAccess(interp, objv[1], X_OK);
1696 }
1697
1698 /*
1699 *----------------------------------------------------------------------
1700 *
1701 * FileAttrIsExistingCmd --
1702 *
1703 * This function is invoked to process the "file exists" Tcl command. See
1704 * the user documentation for details on what it does.
1705 *
1706 * Results:
1707 * A standard Tcl result.
1708 *
1709 * Side effects:
1710 * None.
1711 *
1712 *----------------------------------------------------------------------
1713 */
1714
1715 static int
1716 FileAttrIsExistingCmd(
1717 ClientData clientData,
1718 Tcl_Interp *interp,
1719 int objc,
1720 Tcl_Obj *const objv[])
1721 {
1722 if (objc != 2) {
1723 Tcl_WrongNumArgs(interp, 1, objv, "name");
1724 return TCL_ERROR;
1725 }
1726 return CheckAccess(interp, objv[1], F_OK);
1727 }
1728
1729 /*
1730 *----------------------------------------------------------------------
1731 *
1732 * FileAttrIsFileCmd --
1733 *
1734 * This function is invoked to process the "file isfile" Tcl command. See
1735 * the user documentation for details on what it does.
1736 *
1737 * Results:
1738 * A standard Tcl result.
1739 *
1740 * Side effects:
1741 * None.
1742 *
1743 *----------------------------------------------------------------------
1744 */
1745
1746 static int
1747 FileAttrIsFileCmd(
1748 ClientData clientData,
1749 Tcl_Interp *interp,
1750 int objc,
1751 Tcl_Obj *const objv[])
1752 {
1753 Tcl_StatBuf buf;
1754 int value = 0;
1755
1756 if (objc != 2) {
1757 Tcl_WrongNumArgs(interp, 1, objv, "name");
1758 return TCL_ERROR;
1759 }
1760 if (GetStatBuf(NULL, objv[1], Tcl_FSStat, &buf) == TCL_OK) {
1761 value = S_ISREG(buf.st_mode);
1762 }
1763 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(value));
1764 return TCL_OK;
1765 }
1766
1767 /*
1768 *----------------------------------------------------------------------
1769 *
1770 * FileAttrIsOwnedCmd --
1771 *
1772 * This function is invoked to process the "file owned" Tcl command. See
1773 * the user documentation for details on what it does.
1774 *
1775 * Results:
1776 * A standard Tcl result.
1777 *
1778 * Side effects:
1779 * None.
1780 *
1781 *----------------------------------------------------------------------
1782 */
1783
1784 static int
1785 FileAttrIsOwnedCmd(
1786 ClientData clientData,
1787 Tcl_Interp *interp,
1788 int objc,
1789 Tcl_Obj *const objv[])
1790 {
1791 #ifdef __CYGWIN__
1792 #define geteuid() (short)(geteuid)()
1793 #endif
1794 #if !defined(_WIN32)
1795 Tcl_StatBuf buf;
1796 #endif
1797 int value = 0;
1798
1799 if (objc != 2) {
1800 Tcl_WrongNumArgs(interp, 1, objv, "name");
1801 return TCL_ERROR;
1802 }
1803 #if defined(_WIN32)
1804 value = TclWinFileOwned(objv[1]);
1805 #else
1806 if (GetStatBuf(NULL, objv[1], Tcl_FSStat, &buf) == TCL_OK) {
1807 value = (geteuid() == buf.st_uid);
1808 }
1809 #endif
1810 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(value));
1811 return TCL_OK;
1812 }
1813
1814 /*
1815 *----------------------------------------------------------------------
1816 *
1817 * FileAttrIsReadableCmd --
1818 *
1819 * This function is invoked to process the "file readable" Tcl command.
1820 * See the user documentation for details on what it does.
1821 *
1822 * Results:
1823 * A standard Tcl result.
1824 *
1825 * Side effects:
1826 * None.
1827 *
1828 *----------------------------------------------------------------------
1829 */
1830
1831 static int
1832 FileAttrIsReadableCmd(
1833 ClientData clientData,
1834 Tcl_Interp *interp,
1835 int objc,
1836 Tcl_Obj *const objv[])
1837 {
1838 if (objc != 2) {
1839 Tcl_WrongNumArgs(interp, 1, objv, "name");
1840 return TCL_ERROR;
1841 }
1842 return CheckAccess(interp, objv[1], R_OK);
1843 }
1844
1845 /*
1846 *----------------------------------------------------------------------
1847 *
1848 * FileAttrIsWritableCmd --
1849 *
1850 * This function is invoked to process the "file writable" Tcl command.
1851 * See the user documentation for details on what it does.
1852 *
1853 * Results:
1854 * A standard Tcl result.
1855 *
1856 * Side effects:
1857 * None.
1858 *
1859 *----------------------------------------------------------------------
1860 */
1861
1862 static int
1863 FileAttrIsWritableCmd(
1864 ClientData clientData,
1865 Tcl_Interp *interp,
1866 int objc,
1867 Tcl_Obj *const objv[])
1868 {
1869 if (objc != 2) {
1870 Tcl_WrongNumArgs(interp, 1, objv, "name");
1871 return TCL_ERROR;
1872 }
1873 return CheckAccess(interp, objv[1], W_OK);
1874 }
1875
1876 /*
1877 *----------------------------------------------------------------------
1878 *
1879 * PathDirNameCmd --
1880 *
1881 * This function is invoked to process the "file dirname" Tcl command.
1882 * See the user documentation for details on what it does.
1883 *
1884 * Results:
1885 * A standard Tcl result.
1886 *
1887 * Side effects:
1888 * None.
1889 *
1890 *----------------------------------------------------------------------
1891 */
1892
1893 static int
1894 PathDirNameCmd(
1895 ClientData clientData,
1896 Tcl_Interp *interp,
1897 int objc,
1898 Tcl_Obj *const objv[])
1899 {
1900 Tcl_Obj *dirPtr;
1901
1902 if (objc != 2) {
1903 Tcl_WrongNumArgs(interp, 1, objv, "name");
1904 return TCL_ERROR;
1905 }
1906 dirPtr = TclPathPart(interp, objv[1], TCL_PATH_DIRNAME);
1907 if (dirPtr == NULL) {
1908 return TCL_ERROR;
1909 }
1910 Tcl_SetObjResult(interp, dirPtr);
1911 Tcl_DecrRefCount(dirPtr);
1912 return TCL_OK;
1913 }
1914
1915 /*
1916 *----------------------------------------------------------------------
1917 *
1918 * PathExtensionCmd --
1919 *
1920 * This function is invoked to process the "file extension" Tcl command.
1921 * See the user documentation for details on what it does.
1922 *
1923 * Results:
1924 * A standard Tcl result.
1925 *
1926 * Side effects:
1927 * None.
1928 *
1929 *----------------------------------------------------------------------
1930 */
1931
1932 static int
1933 PathExtensionCmd(
1934 ClientData clientData,
1935 Tcl_Interp *interp,
1936 int objc,
1937 Tcl_Obj *const objv[])
1938 {
1939 Tcl_Obj *dirPtr;
1940
1941 if (objc != 2) {
1942 Tcl_WrongNumArgs(interp, 1, objv, "name");
1943 return TCL_ERROR;
1944 }
1945 dirPtr = TclPathPart(interp, objv[1], TCL_PATH_EXTENSION);
1946 if (dirPtr == NULL) {
1947 return TCL_ERROR;
1948 }
1949 Tcl_SetObjResult(interp, dirPtr);
1950 Tcl_DecrRefCount(dirPtr);
1951 return TCL_OK;
1952 }
1953
1954 /*
1955 *----------------------------------------------------------------------
1956 *
1957 * PathRootNameCmd --
1958 *
1959 * This function is invoked to process the "file root" Tcl command. See
1960 * the user documentation for details on what it does.
1961 *
1962 * Results:
1963 * A standard Tcl result.
1964 *
1965 * Side effects:
1966 * None.
1967 *
1968 *----------------------------------------------------------------------
1969 */
1970
1971 static int
1972 PathRootNameCmd(
1973 ClientData clientData,
1974 Tcl_Interp *interp,
1975 int objc,
1976 Tcl_Obj *const objv[])
1977 {
1978 Tcl_Obj *dirPtr;
1979
1980 if (objc != 2) {
1981 Tcl_WrongNumArgs(interp, 1, objv, "name");
1982 return TCL_ERROR;
1983 }
1984 dirPtr = TclPathPart(interp, objv[1], TCL_PATH_ROOT);
1985 if (dirPtr == NULL) {
1986 return TCL_ERROR;
1987 }
1988 Tcl_SetObjResult(interp, dirPtr);
1989 Tcl_DecrRefCount(dirPtr);
1990 return TCL_OK;
1991 }
1992
1993 /*
1994 *----------------------------------------------------------------------
1995 *
1996 * PathTailCmd --
1997 *
1998 * This function is invoked to process the "file tail" Tcl command. See
1999 * the user documentation for details on what it does.
2000 *
2001 * Results:
2002 * A standard Tcl result.
2003 *
2004 * Side effects:
2005 * None.
2006 *
2007 *----------------------------------------------------------------------
2008 */
2009
2010 static int
2011 PathTailCmd(
2012 ClientData clientData,
2013 Tcl_Interp *interp,
2014 int objc,
2015 Tcl_Obj *const objv[])
2016 {
2017 Tcl_Obj *dirPtr;
2018
2019 if (objc != 2) {
2020 Tcl_WrongNumArgs(interp, 1, objv, "name");
2021 return TCL_ERROR;
2022 }
2023 dirPtr = TclPathPart(interp, objv[1], TCL_PATH_TAIL);
2024 if (dirPtr == NULL) {
2025 return TCL_ERROR;
2026 }
2027 Tcl_SetObjResult(interp, dirPtr);
2028 Tcl_DecrRefCount(dirPtr);
2029 return TCL_OK;
2030 }
2031
2032 /*
2033 *----------------------------------------------------------------------
2034 *
2035 * PathFilesystemCmd --
2036 *
2037 * This function is invoked to process the "file system" Tcl command. See
2038 * the user documentation for details on what it does.
2039 *
2040 * Results:
2041 * A standard Tcl result.
2042 *
2043 * Side effects:
2044 * None.
2045 *
2046 *----------------------------------------------------------------------
2047 */
2048
2049 static int
2050 PathFilesystemCmd(
2051 ClientData clientData,
2052 Tcl_Interp *interp,
2053 int objc,
2054 Tcl_Obj *const objv[])
2055 {
2056 Tcl_Obj *fsInfo;
2057
2058 if (objc != 2) {
2059 Tcl_WrongNumArgs(interp, 1, objv, "name");
2060 return TCL_ERROR;
2061 }
2062 fsInfo = Tcl_FSFileSystemInfo(objv[1]);
2063 if (fsInfo == NULL) {
2064 Tcl_SetObjResult(interp, Tcl_NewStringObj("unrecognised path", -1));
2065 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "FILESYSTEM",
2066 TclGetString(objv[1]), (char *)NULL);
2067 return TCL_ERROR;
2068 }
2069 Tcl_SetObjResult(interp, fsInfo);
2070 return TCL_OK;
2071 }
2072
2073 /*
2074 *----------------------------------------------------------------------
2075 *
2076 * PathJoinCmd --
2077 *
2078 * This function is invoked to process the "file join" Tcl command. See
2079 * the user documentation for details on what it does.
2080 *
2081 * Results:
2082 * A standard Tcl result.
2083 *
2084 * Side effects:
2085 * None.
2086 *
2087 *----------------------------------------------------------------------
2088 */
2089
2090 static int
2091 PathJoinCmd(
2092 ClientData clientData,
2093 Tcl_Interp *interp,
2094 int objc,
2095 Tcl_Obj *const objv[])
2096 {
2097 if (objc < 2) {
2098 Tcl_WrongNumArgs(interp, 1, objv, "name ?name ...?");
2099 return TCL_ERROR;
2100 }
2101 Tcl_SetObjResult(interp, TclJoinPath(objc - 1, objv + 1, 0));
2102 return TCL_OK;
2103 }
2104
2105 /*
2106 *----------------------------------------------------------------------
2107 *
2108 * PathNativeNameCmd --
2109 *
2110 * This function is invoked to process the "file nativename" Tcl command.
2111 * See the user documentation for details on what it does.
2112 *
2113 * Results:
2114 * A standard Tcl result.
2115 *
2116 * Side effects:
2117 * None.
2118 *
2119 *----------------------------------------------------------------------
2120 */
2121
2122 static int
2123 PathNativeNameCmd(
2124 ClientData clientData,
2125 Tcl_Interp *interp,
2126 int objc,
2127 Tcl_Obj *const objv[])
2128 {
2129 Tcl_DString ds;
2130
2131 if (objc != 2) {
2132 Tcl_WrongNumArgs(interp, 1, objv, "name");
2133 return TCL_ERROR;
2134 }
2135 if (Tcl_TranslateFileName(interp, TclGetString(objv[1]), &ds) == NULL) {
2136 return TCL_ERROR;
2137 }
2138 Tcl_SetObjResult(interp, TclDStringToObj(&ds));
2139 return TCL_OK;
2140 }
2141
2142 /*
2143 *----------------------------------------------------------------------
2144 *
2145 * PathNormalizeCmd --
2146 *
2147 * This function is invoked to process the "file normalize" Tcl command.
2148 * See the user documentation for details on what it does.
2149 *
2150 * Results:
2151 * A standard Tcl result.
2152 *
2153 * Side effects:
2154 * None.
2155 *
2156 *----------------------------------------------------------------------
2157 */
2158
2159 static int
2160 PathNormalizeCmd(
2161 ClientData clientData,
2162 Tcl_Interp *interp,
2163 int objc,
2164 Tcl_Obj *const objv[])
2165 {
2166 Tcl_Obj *fileName;
2167
2168 if (objc != 2) {
2169 Tcl_WrongNumArgs(interp, 1, objv, "name");
2170 return TCL_ERROR;
2171 }
2172 fileName = Tcl_FSGetNormalizedPath(interp, objv[1]);
2173 if (fileName == NULL) {
2174 return TCL_ERROR;
2175 }
2176 Tcl_SetObjResult(interp, fileName);
2177 return TCL_OK;
2178 }
2179
2180 /*
2181 *----------------------------------------------------------------------
2182 *
2183 * PathSplitCmd --
2184 *
2185 * This function is invoked to process the "file split" Tcl command. See
2186 * the user documentation for details on what it does.
2187 *
2188 * Results:
2189 * A standard Tcl result.
2190 *
2191 * Side effects:
2192 * None.
2193 *
2194 *----------------------------------------------------------------------
2195 */
2196
2197 static int
2198 PathSplitCmd(
2199 ClientData clientData,
2200 Tcl_Interp *interp,
2201 int objc,
2202 Tcl_Obj *const objv[])
2203 {
2204 Tcl_Obj *res;
2205
2206 if (objc != 2) {
2207 Tcl_WrongNumArgs(interp, 1, objv, "name");
2208 return TCL_ERROR;
2209 }
2210 res = Tcl_FSSplitPath(objv[1], NULL);
2211 if (res == NULL) {
2212 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2213 "could not read \"%s\": no such file or directory",
2214 TclGetString(objv[1])));
2215 Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PATHSPLIT", "NONESUCH",
2216 (char *)NULL);
2217 return TCL_ERROR;
2218 }
2219 Tcl_SetObjResult(interp, res);
2220 return TCL_OK;
2221 }
2222
2223 /*
2224 *----------------------------------------------------------------------
2225 *
2226 * PathTypeCmd --
2227 *
2228 * This function is invoked to process the "file pathtype" Tcl command.
2229 * See the user documentation for details on what it does.
2230 *
2231 * Results:
2232 * A standard Tcl result.
2233 *
2234 * Side effects:
2235 * None.
2236 *
2237 *----------------------------------------------------------------------
2238 */
2239
2240 static int
2241 PathTypeCmd(
2242 ClientData clientData,
2243 Tcl_Interp *interp,
2244 int objc,
2245 Tcl_Obj *const objv[])
2246 {
2247 Tcl_Obj *typeName;
2248
2249 if (objc != 2) {
2250 Tcl_WrongNumArgs(interp, 1, objv, "name");
2251 return TCL_ERROR;
2252 }
2253 switch (Tcl_FSGetPathType(objv[1])) {
2254 case TCL_PATH_ABSOLUTE:
2255 TclNewLiteralStringObj(typeName, "absolute");
2256 break;
2257 case TCL_PATH_RELATIVE:
2258 TclNewLiteralStringObj(typeName, "relative");
2259 break;
2260 case TCL_PATH_VOLUME_RELATIVE:
2261 TclNewLiteralStringObj(typeName, "volumerelative");
2262 break;
2263 default:
2264 /* Should be unreachable */
2265 return TCL_OK;
2266 }
2267 Tcl_SetObjResult(interp, typeName);
2268 return TCL_OK;
2269 }
2270
2271 /*
2272 *----------------------------------------------------------------------
2273 *
2274 * FilesystemSeparatorCmd --
2275 *
2276 * This function is invoked to process the "file separator" Tcl command.
2277 * See the user documentation for details on what it does.
2278 *
2279 * Results:
2280 * A standard Tcl result.
2281 *
2282 * Side effects:
2283 * None.
2284 *
2285 *----------------------------------------------------------------------
2286 */
2287
2288 static int
2289 FilesystemSeparatorCmd(
2290 ClientData clientData,
2291 Tcl_Interp *interp,
2292 int objc,
2293 Tcl_Obj *const objv[])
2294 {
2295 if (objc < 1 || objc > 2) {
2296 Tcl_WrongNumArgs(interp, 1, objv, "?name?");
2297 return TCL_ERROR;
2298 }
2299 if (objc == 1) {
2300 const char *separator = NULL;
2301
2302 switch (tclPlatform) {
2303 case TCL_PLATFORM_UNIX:
2304 separator = "/";
2305 break;
2306 case TCL_PLATFORM_WINDOWS:
2307 separator = "\\";
2308 break;
2309 }
2310 Tcl_SetObjResult(interp, Tcl_NewStringObj(separator, 1));
2311 } else {
2312 Tcl_Obj *separatorObj = Tcl_FSPathSeparator(objv[1]);
2313
2314 if (separatorObj == NULL) {
2315 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2316 "unrecognised path", -1));
2317 Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "FILESYSTEM",
2318 TclGetString(objv[1]), (char *)NULL);
2319 return TCL_ERROR;
2320 }
2321 Tcl_SetObjResult(interp, separatorObj);
2322 }
2323 return TCL_OK;
2324 }
2325
2326 /*
2327 *----------------------------------------------------------------------
2328 *
2329 * FilesystemVolumesCmd --
2330 *
2331 * This function is invoked to process the "file volumes" Tcl command.
2332 * See the user documentation for details on what it does.
2333 *
2334 * Results:
2335 * A standard Tcl result.
2336 *
2337 * Side effects:
2338 * None.
2339 *
2340 *----------------------------------------------------------------------
2341 */
2342
2343 static int
2344 FilesystemVolumesCmd(
2345 ClientData clientData,
2346 Tcl_Interp *interp,
2347 int objc,
2348 Tcl_Obj *const objv[])
2349 {
2350 if (objc != 1) {
2351 Tcl_WrongNumArgs(interp, 1, objv, NULL);
2352 return TCL_ERROR;
2353 }
2354 Tcl_SetObjResult(interp, Tcl_FSListVolumes());
2355 return TCL_OK;
2356 }
2357
2358 /*
2359 *---------------------------------------------------------------------------
2360 *
2361 * CheckAccess --
2362 *
2363 * Utility procedure used by Tcl_FileObjCmd() to query file attributes
2364 * available through the access() system call.
2365 *
2366 * Results:
2367 * Always returns TCL_OK. Sets interp's result to boolean true or false
2368 * depending on whether the file has the specified attribute.
2369 *
2370 * Side effects:
2371 * None.
2372 *
2373 *---------------------------------------------------------------------------
2374 */
2375
2376 static int
2377 CheckAccess(
2378 Tcl_Interp *interp, /* Interp for status return. Must not be
2379 * NULL. */
2380 Tcl_Obj *pathPtr, /* Name of file to check. */
2381 int mode) /* Attribute to check; passed as argument to
2382 * access(). */
2383 {
2384 int value;
2385
2386 if (Tcl_FSConvertToPathType(interp, pathPtr) != TCL_OK) {
2387 value = 0;
2388 } else {
2389 value = (Tcl_FSAccess(pathPtr, mode) == 0);
2390 }
2391 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(value));
2392
2393 return TCL_OK;
2394 }
2395
2396 /*
2397 *---------------------------------------------------------------------------
2398 *
2399 * GetStatBuf --
2400 *
2401 * Utility procedure used by Tcl_FileObjCmd() to query file attributes
2402 * available through the stat() or lstat() system call.
2403 *
2404 * Results:
2405 * The return value is TCL_OK if the specified file exists and can be
2406 * stat'ed, TCL_ERROR otherwise. If TCL_ERROR is returned, an error
2407 * message is left in interp's result. If TCL_OK is returned, *statPtr is
2408 * filled with information about the specified file.
2409 *
2410 * Side effects:
2411 * None.
2412 *
2413 *---------------------------------------------------------------------------
2414 */
2415
2416 static int
2417 GetStatBuf(
2418 Tcl_Interp *interp, /* Interp for error return. May be NULL. */
2419 Tcl_Obj *pathPtr, /* Path name to examine. */
2420 Tcl_FSStatProc *statProc, /* Either stat() or lstat() depending on
2421 * desired behavior. */
2422 Tcl_StatBuf *statPtr) /* Filled with info about file obtained by
2423 * calling (*statProc)(). */
2424 {
2425 int status;
2426
2427 if (Tcl_FSConvertToPathType(interp, pathPtr) != TCL_OK) {
2428 return TCL_ERROR;
2429 }
2430
2431 status = statProc(pathPtr, statPtr);
2432
2433 if (status < 0) {
2434 if (interp != NULL) {
2435 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2436 "could not read \"%s\": %s",
2437 TclGetString(pathPtr), Tcl_PosixError(interp)));
2438 }
2439 return TCL_ERROR;
2440 }
2441 return TCL_OK;
2442 }
2443
2444 /*
2445 *----------------------------------------------------------------------
2446 *
2447 * StoreStatData --
2448 *
2449 * This is a utility procedure that breaks out the fields of a "stat"
2450 * structure and stores them in textual form into the elements of an
2451 * associative array.
2452 *
2453 * Results:
2454 * Returns a standard Tcl return value. If an error occurs then a message
2455 * is left in interp's result.
2456 *
2457 * Side effects:
2458 * Elements of the associative array given by "varName" are modified.
2459 *
2460 *----------------------------------------------------------------------
2461 */
2462
2463 static int
2464 StoreStatData(
2465 Tcl_Interp *interp, /* Interpreter for error reports. */
2466 Tcl_Obj *varName, /* Name of associative array variable in which
2467 * to store stat results. */
2468 Tcl_StatBuf *statPtr) /* Pointer to buffer containing stat data to
2469 * store in varName. */
2470 {
2471 Tcl_Obj *field, *value;
2472 unsigned short mode;
2473
2474 /*
2475 * Assume Tcl_ObjSetVar2() does not keep a copy of the field name!
2476 *
2477 * Might be a better idea to call Tcl_SetVar2Ex() instead, except we want
2478 * to have an object (i.e. possibly cached) array variable name but a
2479 * string element name, so no API exists. Messy.
2480 */
2481
2482 #define STORE_ARY(fieldName, object) \
2483 TclNewLiteralStringObj(field, fieldName); \
2484 Tcl_IncrRefCount(field); \
2485 value = (object); \
2486 if (Tcl_ObjSetVar2(interp,varName,field,value,TCL_LEAVE_ERR_MSG)==NULL) { \
2487 TclDecrRefCount(field); \
2488 return TCL_ERROR; \
2489 } \
2490 TclDecrRefCount(field);
2491
2492 /*
2493 * Watch out porters; the inode is meant to be an *unsigned* value, so the
2494 * cast might fail when there isn't a real arithmetic 'long long' type...
2495 */
2496
2497 STORE_ARY("dev", Tcl_NewLongObj((long)statPtr->st_dev));
2498 STORE_ARY("ino", Tcl_NewWideIntObj((Tcl_WideInt)statPtr->st_ino));
2499 STORE_ARY("nlink", Tcl_NewLongObj((long)statPtr->st_nlink));
2500 STORE_ARY("uid", Tcl_NewLongObj((long)statPtr->st_uid));
2501 STORE_ARY("gid", Tcl_NewLongObj((long)statPtr->st_gid));
2502 STORE_ARY("size", Tcl_NewWideIntObj((Tcl_WideInt)statPtr->st_size));
2503 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
2504 STORE_ARY("blocks", Tcl_NewWideIntObj((Tcl_WideInt)statPtr->st_blocks));
2505 #endif
2506 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2507 STORE_ARY("blksize", Tcl_NewLongObj((long)statPtr->st_blksize));
2508 #endif
2509 STORE_ARY("atime", Tcl_NewWideIntObj(Tcl_GetAccessTimeFromStat(statPtr)));
2510 STORE_ARY("mtime", Tcl_NewWideIntObj(Tcl_GetModificationTimeFromStat(statPtr)));
2511 STORE_ARY("ctime", Tcl_NewWideIntObj(Tcl_GetChangeTimeFromStat(statPtr)));
2512 mode = (unsigned short) statPtr->st_mode;
2513 STORE_ARY("mode", Tcl_NewIntObj(mode));
2514 STORE_ARY("type", Tcl_NewStringObj(GetTypeFromMode(mode), -1));
2515 #undef STORE_ARY
2516
2517 return TCL_OK;
2518 }
2519
2520 /*
2521 *----------------------------------------------------------------------
2522 *
2523 * GetTypeFromMode --
2524 *
2525 * Given a mode word, returns a string identifying the type of a file.
2526 *
2527 * Results:
2528 * A static text string giving the file type from mode.
2529 *
2530 * Side effects:
2531 * None.
2532 *
2533 *----------------------------------------------------------------------
2534 */
2535
2536 static const char *
2537 GetTypeFromMode(
2538 int mode)
2539 {
2540 if (S_ISREG(mode)) {
2541 return "file";
2542 } else if (S_ISDIR(mode)) {
2543 return "directory";
2544 } else if (S_ISCHR(mode)) {
2545 return "characterSpecial";
2546 } else if (S_ISBLK(mode)) {
2547 return "blockSpecial";
2548 } else if (S_ISFIFO(mode)) {
2549 return "fifo";
2550 #ifdef S_ISLNK
2551 } else if (S_ISLNK(mode)) {
2552 return "link";
2553 #endif
2554 #ifdef S_ISSOCK
2555 } else if (S_ISSOCK(mode)) {
2556 return "socket";
2557 #endif
2558 }
2559 return "unknown";
2560 }
2561
2562 /*
2563 *----------------------------------------------------------------------
2564 *
2565 * Tcl_ForObjCmd --
2566 *
2567 * This procedure is invoked to process the "for" Tcl command. See the
2568 * user documentation for details on what it does.
2569 *
2570 * With the bytecode compiler, this procedure is only called when a
2571 * command name is computed at runtime, and is "for" or the name to which
2572 * "for" was renamed: e.g.,
2573 * "set z for; $z {set i 0} {$i<100} {incr i} {puts $i}"
2574 *
2575 * Results:
2576 * A standard Tcl result.
2577 *
2578 * Side effects:
2579 * See the user documentation.
2580 *
2581 * Notes:
2582 * This command is split into a lot of pieces so that it can avoid doing
2583 * reentrant TEBC calls. This makes things rather hard to follow, but
2584 * here's the plan:
2585 *
2586 * NR: ---------------_\
2587 * Direct: Tcl_ForObjCmd -> TclNRForObjCmd
2588 * |
2589 * ForSetupCallback
2590 * |
2591 * [while] ------------> TclNRForIterCallback <---------.
2592 * | |
2593 * ForCondCallback |
2594 * | |
2595 * ForNextCallback ------------|
2596 * | |
2597 * ForPostNextCallback |
2598 * |____________________|
2599 *
2600 *----------------------------------------------------------------------
2601 */
2602
2603 int
2604 Tcl_ForObjCmd(
2605 ClientData dummy, /* Not used. */
2606 Tcl_Interp *interp, /* Current interpreter. */
2607 int objc, /* Number of arguments. */
2608 Tcl_Obj *const objv[]) /* Argument objects. */
2609 {
2610 return Tcl_NRCallObjProc(interp, TclNRForObjCmd, dummy, objc, objv);
2611 }
2612
2613 int
2614 TclNRForObjCmd(
2615 ClientData dummy, /* Not used. */
2616 Tcl_Interp *interp, /* Current interpreter. */
2617 int objc, /* Number of arguments. */
2618 Tcl_Obj *const objv[]) /* Argument objects. */
2619 {
2620 Interp *iPtr = (Interp *) interp;
2621 ForIterData *iterPtr;
2622
2623 if (objc != 5) {
2624 Tcl_WrongNumArgs(interp, 1, objv, "start test next command");
2625 return TCL_ERROR;
2626 }
2627
2628 TclSmallAllocEx(interp, sizeof(ForIterData), iterPtr);
2629 iterPtr->cond = objv[2];
2630 iterPtr->body = objv[4];
2631 iterPtr->next = objv[3];
2632 iterPtr->msg = "\n (\"for\" body line %d)";
2633 iterPtr->word = 4;
2634
2635 TclNRAddCallback(interp, ForSetupCallback, iterPtr, NULL, NULL, NULL);
2636
2637 /*
2638 * TIP #280. Make invoking context available to initial script.
2639 */
2640
2641 return TclNREvalObjEx(interp, objv[1], 0, iPtr->cmdFramePtr, 1);
2642 }
2643
2644 static int
2645 ForSetupCallback(
2646 ClientData data[],
2647 Tcl_Interp *interp,
2648 int result)
2649 {
2650 ForIterData *iterPtr = (ForIterData *)data[0];
2651
2652 if (result != TCL_OK) {
2653 if (result == TCL_ERROR) {
2654 Tcl_AddErrorInfo(interp, "\n (\"for\" initial command)");
2655 }
2656 TclSmallFreeEx(interp, iterPtr);
2657 return result;
2658 }
2659 TclNRAddCallback(interp, TclNRForIterCallback, iterPtr, NULL, NULL, NULL);
2660 return TCL_OK;
2661 }
2662
2663 int
2664 TclNRForIterCallback(
2665 ClientData data[],
2666 Tcl_Interp *interp,
2667 int result)
2668 {
2669 ForIterData *iterPtr = (ForIterData *)data[0];
2670 Tcl_Obj *boolObj;
2671
2672 switch (result) {
2673 case TCL_OK:
2674 case TCL_CONTINUE:
2675 /*
2676 * We need to reset the result before evaluating the expression.
2677 * Otherwise, any error message will be appended to the result of the
2678 * last evaluation.
2679 */
2680
2681 Tcl_ResetResult(interp);
2682 TclNewObj(boolObj);
2683 TclNRAddCallback(interp, ForCondCallback, iterPtr, boolObj, NULL,
2684 (char *)NULL);
2685 return Tcl_NRExprObj(interp, iterPtr->cond, boolObj);
2686 case TCL_BREAK:
2687 result = TCL_OK;
2688 Tcl_ResetResult(interp);
2689 break;
2690 case TCL_ERROR:
2691 Tcl_AppendObjToErrorInfo(interp,
2692 Tcl_ObjPrintf(iterPtr->msg, Tcl_GetErrorLine(interp)));
2693 }
2694 TclSmallFreeEx(interp, iterPtr);
2695 return result;
2696 }
2697
2698 static int
2699 ForCondCallback(
2700 ClientData data[],
2701 Tcl_Interp *interp,
2702 int result)
2703 {
2704 Interp *iPtr = (Interp *) interp;
2705 ForIterData *iterPtr = (ForIterData *)data[0];
2706 Tcl_Obj *boolObj = (Tcl_Obj *)data[1];
2707 int value;
2708
2709 if (result != TCL_OK) {
2710 Tcl_DecrRefCount(boolObj);
2711 TclSmallFreeEx(interp, iterPtr);
2712 return result;
2713 } else if (Tcl_GetBooleanFromObj(interp, boolObj, &value) != TCL_OK) {
2714 Tcl_DecrRefCount(boolObj);
2715 TclSmallFreeEx(interp, iterPtr);
2716 return TCL_ERROR;
2717 }
2718 Tcl_DecrRefCount(boolObj);
2719
2720 if (value) {
2721 /* TIP #280. */
2722 if (iterPtr->next) {
2723 TclNRAddCallback(interp, ForNextCallback, iterPtr, NULL, NULL,
2724 NULL);
2725 } else {
2726 TclNRAddCallback(interp, TclNRForIterCallback, iterPtr, NULL,
2727 NULL, NULL);
2728 }
2729 return TclNREvalObjEx(interp, iterPtr->body, 0, iPtr->cmdFramePtr,
2730 iterPtr->word);
2731 }
2732 TclSmallFreeEx(interp, iterPtr);
2733 return result;
2734 }
2735
2736 static int
2737 ForNextCallback(
2738 ClientData data[],
2739 Tcl_Interp *interp,
2740 int result)
2741 {
2742 Interp *iPtr = (Interp *) interp;
2743 ForIterData *iterPtr = (ForIterData *)data[0];
2744 Tcl_Obj *next = iterPtr->next;
2745
2746 if ((result == TCL_OK) || (result == TCL_CONTINUE)) {
2747 TclNRAddCallback(interp, ForPostNextCallback, iterPtr, NULL, NULL,
2748 NULL);
2749
2750 /*
2751 * TIP #280. Make invoking context available to next script.
2752 */
2753
2754 return TclNREvalObjEx(interp, next, 0, iPtr->cmdFramePtr, 3);
2755 }
2756
2757 TclNRAddCallback(interp, TclNRForIterCallback, iterPtr, NULL, NULL, NULL);
2758 return result;
2759 }
2760
2761 static int
2762 ForPostNextCallback(
2763 ClientData data[],
2764 Tcl_Interp *interp,
2765 int result)
2766 {
2767 ForIterData *iterPtr = (ForIterData *)data[0];
2768
2769 if ((result != TCL_BREAK) && (result != TCL_OK)) {
2770 if (result == TCL_ERROR) {
2771 Tcl_AddErrorInfo(interp, "\n (\"for\" loop-end command)");
2772 TclSmallFreeEx(interp, iterPtr);
2773 }
2774 return result;
2775 }
2776 TclNRAddCallback(interp, TclNRForIterCallback, iterPtr, NULL, NULL, NULL);
2777 return result;
2778 }
2779
2780 /*
2781 *----------------------------------------------------------------------
2782 *
2783 * Tcl_ForeachObjCmd, TclNRForeachCmd, EachloopCmd --
2784 *
2785 * This object-based procedure is invoked to process the "foreach" Tcl
2786 * command. See the user documentation for details on what it does.
2787 *
2788 * Results:
2789 * A standard Tcl object result.
2790 *
2791 * Side effects:
2792 * See the user documentation.
2793 *
2794 *----------------------------------------------------------------------
2795 */
2796
2797 int
2798 Tcl_ForeachObjCmd(
2799 ClientData dummy, /* Not used. */
2800 Tcl_Interp *interp, /* Current interpreter. */
2801 int objc, /* Number of arguments. */
2802 Tcl_Obj *const objv[]) /* Argument objects. */
2803 {
2804 return Tcl_NRCallObjProc(interp, TclNRForeachCmd, dummy, objc, objv);
2805 }
2806
2807 int
2808 TclNRForeachCmd(
2809 ClientData dummy,
2810 Tcl_Interp *interp,
2811 int objc,
2812 Tcl_Obj *const objv[])
2813 {
2814 return EachloopCmd(interp, TCL_EACH_KEEP_NONE, objc, objv);
2815 }
2816
2817 int
2818 Tcl_LmapObjCmd(
2819 ClientData dummy, /* Not used. */
2820 Tcl_Interp *interp, /* Current interpreter. */
2821 int objc, /* Number of arguments. */
2822 Tcl_Obj *const objv[]) /* Argument objects. */
2823 {
2824 return Tcl_NRCallObjProc(interp, TclNRLmapCmd, dummy, objc, objv);
2825 }
2826
2827 int
2828 TclNRLmapCmd(
2829 ClientData dummy,
2830 Tcl_Interp *interp,
2831 int objc,
2832 Tcl_Obj *const objv[])
2833 {
2834 return EachloopCmd(interp, TCL_EACH_COLLECT, objc, objv);
2835 }
2836
2837 static inline int
2838 EachloopCmd(
2839 Tcl_Interp *interp, /* Our context for variables and script
2840 * evaluation. */
2841 int collect, /* Select collecting or accumulating mode
2842 * (TCL_EACH_*) */
2843 int objc, /* The arguments being passed in... */
2844 Tcl_Obj *const objv[])
2845 {
2846 int numLists = (objc-2) / 2;
2847 struct ForeachState *statePtr;
2848 int i, j, result;
2849
2850 if (objc < 4 || (objc%2 != 0)) {
2851 Tcl_WrongNumArgs(interp, 1, objv,
2852 "varList list ?varList list ...? command");
2853 return TCL_ERROR;
2854 }
2855
2856 /*
2857 * Manage numList parallel value lists.
2858 * statePtr->argvList[i] is a value list counted by statePtr->argcList[i];
2859 * statePtr->varvList[i] is the list of variables associated with the
2860 * value list;
2861 * statePtr->varcList[i] is the number of variables associated with the
2862 * value list;
2863 * statePtr->index[i] is the current pointer into the value list
2864 * statePtr->argvList[i].
2865 *
2866 * The setting up of all of these pointers is moderately messy, but allows
2867 * the rest of this code to be simple and for us to use a single memory
2868 * allocation for better performance.
2869 */
2870
2871 statePtr = (struct ForeachState *)TclStackAlloc(interp,
2872 sizeof(struct ForeachState) + 3 * numLists * sizeof(int)
2873 + 2 * numLists * (sizeof(Tcl_Obj **) + sizeof(Tcl_Obj *)));
2874 memset(statePtr, 0,
2875 sizeof(struct ForeachState) + 3 * numLists * sizeof(int)
2876 + 2 * numLists * (sizeof(Tcl_Obj **) + sizeof(Tcl_Obj *)));
2877 statePtr->varvList = (Tcl_Obj ***) (statePtr + 1);
2878 statePtr->argvList = statePtr->varvList + numLists;
2879 statePtr->vCopyList = (Tcl_Obj **) (statePtr->argvList + numLists);
2880 statePtr->aCopyList = statePtr->vCopyList + numLists;
2881 statePtr->index = (int *) (statePtr->aCopyList + numLists);
2882 statePtr->varcList = statePtr->index + numLists;
2883 statePtr->argcList = statePtr->varcList + numLists;
2884
2885 statePtr->numLists = numLists;
2886 statePtr->bodyPtr = objv[objc - 1];
2887 statePtr->bodyIdx = objc - 1;
2888
2889 if (collect == TCL_EACH_COLLECT) {
2890 statePtr->resultList = Tcl_NewListObj(0, NULL);
2891 } else {
2892 statePtr->resultList = NULL;
2893 }
2894
2895 /*
2896 * Break up the value lists and variable lists into elements.
2897 */
2898
2899 for (i=0 ; i<numLists ; i++) {
2900 statePtr->vCopyList[i] = TclListObjCopy(interp, objv[1+i*2]);
2901 if (statePtr->vCopyList[i] == NULL) {
2902 result = TCL_ERROR;
2903 goto done;
2904 }
2905 TclListObjGetElements(NULL, statePtr->vCopyList[i],
2906 &statePtr->varcList[i], &statePtr->varvList[i]);
2907 if (statePtr->varcList[i] < 1) {
2908 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2909 "%s varlist is empty",
2910 (statePtr->resultList != NULL ? "lmap" : "foreach")));
2911 Tcl_SetErrorCode(interp, "TCL", "OPERATION",
2912 (statePtr->resultList != NULL ? "LMAP" : "FOREACH"),
2913 "NEEDVARS", (char *)NULL);
2914 result = TCL_ERROR;
2915 goto done;
2916 }
2917
2918 statePtr->aCopyList[i] = TclListObjCopy(interp, objv[2+i*2]);
2919 if (statePtr->aCopyList[i] == NULL) {
2920 result = TCL_ERROR;
2921 goto done;
2922 }
2923 TclListObjGetElements(NULL, statePtr->aCopyList[i],
2924 &statePtr->argcList[i], &statePtr->argvList[i]);
2925
2926 j = statePtr->argcList[i] / statePtr->varcList[i];
2927 if ((statePtr->argcList[i] % statePtr->varcList[i]) != 0) {
2928 j++;
2929 }
2930 if (j > statePtr->maxj) {
2931 statePtr->maxj = j;
2932 }
2933 }
2934
2935 /*
2936 * If there is any work to do, assign the variables and set things going
2937 * non-recursively.
2938 */
2939
2940 if (statePtr->maxj > 0) {
2941 result = ForeachAssignments(interp, statePtr);
2942 if (result == TCL_ERROR) {
2943 goto done;
2944 }
2945
2946 TclNRAddCallback(interp, ForeachLoopStep, statePtr, NULL, NULL, NULL);
2947 return TclNREvalObjEx(interp, objv[objc-1], 0,
2948 ((Interp *) interp)->cmdFramePtr, objc-1);
2949 }
2950
2951 /*
2952 * This cleanup stage is only used when an error occurs during setup or if
2953 * there is no work to do.
2954 */
2955
2956 result = TCL_OK;
2957 done:
2958 ForeachCleanup(interp, statePtr);
2959 return result;
2960 }
2961
2962 /*
2963 * Post-body processing handler.
2964 */
2965
2966 static int
2967 ForeachLoopStep(
2968 ClientData data[],
2969 Tcl_Interp *interp,
2970 int result)
2971 {
2972 struct ForeachState *statePtr = (struct ForeachState *)data[0];
2973
2974 /*
2975 * Process the result code from this run of the [foreach] body. Note that
2976 * this switch uses fallthroughs in several places. Maintainer aware!
2977 */
2978
2979 switch (result) {
2980 case TCL_CONTINUE:
2981 result = TCL_OK;
2982 break;
2983 case TCL_OK:
2984 if (statePtr->resultList != NULL) {
2985 Tcl_ListObjAppendElement(interp, statePtr->resultList,
2986 Tcl_GetObjResult(interp));
2987 }
2988 break;
2989 case TCL_BREAK:
2990 result = TCL_OK;
2991 goto finish;
2992 case TCL_ERROR:
2993 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
2994 "\n (\"%s\" body line %d)",
2995 (statePtr->resultList != NULL ? "lmap" : "foreach"),
2996 Tcl_GetErrorLine(interp)));
2997 default:
2998 goto done;
2999 }
3000
3001 /*
3002 * Test if there is work still to be done. If so, do the next round of
3003 * variable assignments, reschedule ourselves and run the body again.
3004 */
3005
3006 if (statePtr->maxj > ++statePtr->j) {
3007 result = ForeachAssignments(interp, statePtr);
3008 if (result == TCL_ERROR) {
3009 goto done;
3010 }
3011
3012 TclNRAddCallback(interp, ForeachLoopStep, statePtr, NULL, NULL, NULL);
3013 return TclNREvalObjEx(interp, statePtr->bodyPtr, 0,
3014 ((Interp *) interp)->cmdFramePtr, statePtr->bodyIdx);
3015 }
3016
3017 /*
3018 * We're done. Tidy up our work space and finish off.
3019 */
3020
3021 finish:
3022 if (statePtr->resultList == NULL) {
3023 Tcl_ResetResult(interp);
3024 } else {
3025 Tcl_SetObjResult(interp, statePtr->resultList);
3026 statePtr->resultList = NULL; /* Don't clean it up */
3027 }
3028
3029 done:
3030 ForeachCleanup(interp, statePtr);
3031 return result;
3032 }
3033
3034 /*
3035 * Factored out code to do the assignments in [foreach].
3036 */
3037
3038 static inline int
3039 ForeachAssignments(
3040 Tcl_Interp *interp,
3041 struct ForeachState *statePtr)
3042 {
3043 int i, v, k;
3044 Tcl_Obj *valuePtr, *varValuePtr;
3045
3046 for (i=0 ; i<statePtr->numLists ; i++) {
3047 for (v=0 ; v<statePtr->varcList[i] ; v++) {
3048 k = statePtr->index[i]++;
3049 if (k < statePtr->argcList[i]) {
3050 valuePtr = statePtr->argvList[i][k];
3051 } else {
3052 TclNewObj(valuePtr); /* Empty string */
3053 }
3054
3055 varValuePtr = Tcl_ObjSetVar2(interp, statePtr->varvList[i][v],
3056 NULL, valuePtr, TCL_LEAVE_ERR_MSG);
3057
3058 if (varValuePtr == NULL) {
3059 Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
3060 "\n (setting %s loop variable \"%s\")",
3061 (statePtr->resultList != NULL ? "lmap" : "foreach"),
3062 TclGetString(statePtr->varvList[i][v])));
3063 return TCL_ERROR;
3064 }
3065 }
3066 }
3067
3068 return TCL_OK;
3069 }
3070
3071 /*
3072 * Factored out code for cleaning up the state of the foreach.
3073 */
3074
3075 static inline void
3076 ForeachCleanup(
3077 Tcl_Interp *interp,
3078 struct ForeachState *statePtr)
3079 {
3080 int i;
3081
3082 for (i=0 ; i<statePtr->numLists ; i++) {
3083 if (statePtr->vCopyList[i]) {
3084 TclDecrRefCount(statePtr->vCopyList[i]);
3085 }
3086 if (statePtr->aCopyList[i]) {
3087 TclDecrRefCount(statePtr->aCopyList[i]);
3088 }
3089 }
3090 if (statePtr->resultList != NULL) {
3091 TclDecrRefCount(statePtr->resultList);
3092 }
3093 TclStackFree(interp, statePtr);
3094 }
3095
3096 /*
3097 *----------------------------------------------------------------------
3098 *
3099 * Tcl_FormatObjCmd --
3100 *
3101 * This procedure is invoked to process the "format" Tcl command. See
3102 * the user documentation for details on what it does.
3103 *
3104 * Results:
3105 * A standard Tcl result.
3106 *
3107 * Side effects:
3108 * See the user documentation.
3109 *
3110 *----------------------------------------------------------------------
3111 */
3112
3113 int
3114 Tcl_FormatObjCmd(
3115 ClientData dummy, /* Not used. */
3116 Tcl_Interp *interp, /* Current interpreter. */
3117 int objc, /* Number of arguments. */
3118 Tcl_Obj *const objv[]) /* Argument objects. */
3119 {
3120 Tcl_Obj *resultPtr; /* Where result is stored finally. */
3121
3122 if (objc < 2) {
3123 Tcl_WrongNumArgs(interp, 1, objv, "formatString ?arg ...?");
3124 return TCL_ERROR;
3125 }
3126
3127 resultPtr = Tcl_Format(interp, TclGetString(objv[1]), objc-2, objv+2);
3128 if (resultPtr == NULL) {
3129 return TCL_ERROR;
3130 }
3131 Tcl_SetObjResult(interp, resultPtr);
3132 return TCL_OK;
3133 }
3134
3135 /*
3136 * Local Variables:
3137 * mode: c
3138 * c-basic-offset: 4
3139 * fill-column: 78
3140 * End:
3141 */