| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <ctype.h> |
| 20 |
#include <stdio.h> |
| 21 |
#include <string.h> |
| 22 |
#include <tcl.h> |
| 23 |
#include <tclInt.h> |
| 24 |
#include <tk.h> |
| 25 |
#include "tkInt.h" |
| 26 |
#ifdef NO_STDLIB_H |
| 27 |
# include "../compat/stdlib.h" |
| 28 |
#else |
| 29 |
# include <stdlib.h> |
| 30 |
#endif |
| 31 |
#ifdef __WIN32__ |
| 32 |
#include "tkWinInt.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
typedef struct ThreadSpecificData { |
| 37 |
Tcl_Interp *interp; |
| 38 |
Tcl_DString command; |
| 39 |
|
| 40 |
Tcl_DString line; |
| 41 |
|
| 42 |
int tty; |
| 43 |
|
| 44 |
|
| 45 |
} ThreadSpecificData; |
| 46 |
static Tcl_ThreadDataKey dataKey; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#if !defined(__WIN32__) && !defined(_WIN32) |
| 58 |
#if !defined(MAC_TCL) |
| 59 |
extern int isatty _ANSI_ARGS_((int fd)); |
| 60 |
#else |
| 61 |
#include <unistd.h> |
| 62 |
#endif |
| 63 |
extern char * strrchr _ANSI_ARGS_((CONST char *string, int c)); |
| 64 |
#endif |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static void Prompt _ANSI_ARGS_((Tcl_Interp *interp, int partial)); |
| 71 |
static void StdinProc _ANSI_ARGS_((ClientData clientData, |
| 72 |
int mask)); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
void |
| 93 |
Tk_MainEx(argc, argv, appInitProc, interp) |
| 94 |
int argc; |
| 95 |
char **argv; |
| 96 |
Tcl_AppInitProc *appInitProc; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
Tcl_Interp *interp; |
| 101 |
{ |
| 102 |
char *args; |
| 103 |
char buf[TCL_INTEGER_SPACE]; |
| 104 |
int code; |
| 105 |
size_t length; |
| 106 |
Tcl_Channel inChannel, outChannel; |
| 107 |
Tcl_DString argString; |
| 108 |
ThreadSpecificData *tsdPtr; |
| 109 |
#ifdef __WIN32__ |
| 110 |
HANDLE handle; |
| 111 |
#endif |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
if (Tcl_InitStubs(interp, TCL_VERSION, 1) == NULL) { |
| 119 |
abort(); |
| 120 |
} |
| 121 |
|
| 122 |
tsdPtr = (ThreadSpecificData *) |
| 123 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 124 |
|
| 125 |
Tcl_FindExecutable(argv[0]); |
| 126 |
tsdPtr->interp = interp; |
| 127 |
|
| 128 |
#if (defined(__WIN32__) || defined(MAC_TCL)) |
| 129 |
Tk_InitConsoleChannels(interp); |
| 130 |
#endif |
| 131 |
|
| 132 |
#ifdef TCL_MEM_DEBUG |
| 133 |
Tcl_InitMemory(interp); |
| 134 |
#endif |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (argc > 1) { |
| 144 |
length = strlen(argv[1]); |
| 145 |
if ((length >= 2) && (strncmp(argv[1], "-file", length) == 0)) { |
| 146 |
argc--; |
| 147 |
argv++; |
| 148 |
} |
| 149 |
} |
| 150 |
if (TclGetStartupScriptFileName() == NULL) { |
| 151 |
if ((argc > 1) && (argv[1][0] != '-')) { |
| 152 |
TclSetStartupScriptFileName(argv[1]); |
| 153 |
argc--; |
| 154 |
argv++; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
args = Tcl_Merge(argc-1, (CONST char **)argv+1); |
| 164 |
Tcl_ExternalToUtfDString(NULL, args, -1, &argString); |
| 165 |
Tcl_SetVar(interp, "argv", Tcl_DStringValue(&argString), TCL_GLOBAL_ONLY); |
| 166 |
Tcl_DStringFree(&argString); |
| 167 |
ckfree(args); |
| 168 |
sprintf(buf, "%d", argc-1); |
| 169 |
|
| 170 |
if (TclGetStartupScriptFileName() == NULL) { |
| 171 |
Tcl_ExternalToUtfDString(NULL, argv[0], -1, &argString); |
| 172 |
} else { |
| 173 |
TclSetStartupScriptFileName(Tcl_ExternalToUtfDString(NULL, |
| 174 |
TclGetStartupScriptFileName(), -1, &argString)); |
| 175 |
} |
| 176 |
Tcl_SetVar(interp, "argc", buf, TCL_GLOBAL_ONLY); |
| 177 |
Tcl_SetVar(interp, "argv0", Tcl_DStringValue(&argString), TCL_GLOBAL_ONLY); |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#ifdef __WIN32__ |
| 191 |
handle = GetStdHandle(STD_INPUT_HANDLE); |
| 192 |
|
| 193 |
if ((handle == INVALID_HANDLE_VALUE) || (handle == 0) |
| 194 |
|| (GetFileType(handle) == FILE_TYPE_UNKNOWN)) { |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
tsdPtr->tty = 1; |
| 201 |
} else if (GetFileType(handle) == FILE_TYPE_CHAR) { |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
tsdPtr->tty = 1; |
| 207 |
} else { |
| 208 |
tsdPtr->tty = 0; |
| 209 |
} |
| 210 |
|
| 211 |
#else |
| 212 |
tsdPtr->tty = isatty(0); |
| 213 |
#endif |
| 214 |
Tcl_SetVar(interp, "tcl_interactive", |
| 215 |
((TclGetStartupScriptFileName() == NULL) |
| 216 |
&& tsdPtr->tty) ? "1" : "0", TCL_GLOBAL_ONLY); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
if ((*appInitProc)(interp) != TCL_OK) { |
| 223 |
TkpDisplayWarning(Tcl_GetStringResult(interp), |
| 224 |
"Application initialization failed"); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if (TclGetStartupScriptFileName() != NULL) { |
| 232 |
Tcl_ResetResult(interp); |
| 233 |
code = Tcl_EvalFile(interp, TclGetStartupScriptFileName()); |
| 234 |
if (code != TCL_OK) { |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
Tcl_AddErrorInfo(interp, ""); |
| 241 |
TkpDisplayWarning(Tcl_GetVar(interp, "errorInfo", |
| 242 |
TCL_GLOBAL_ONLY), "Error in startup script"); |
| 243 |
Tcl_DeleteInterp(interp); |
| 244 |
Tcl_Exit(1); |
| 245 |
} |
| 246 |
tsdPtr->tty = 0; |
| 247 |
} else { |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
Tcl_SourceRCFile(interp); |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
inChannel = Tcl_GetStdChannel(TCL_STDIN); |
| 260 |
if (inChannel) { |
| 261 |
Tcl_CreateChannelHandler(inChannel, TCL_READABLE, StdinProc, |
| 262 |
(ClientData) inChannel); |
| 263 |
} |
| 264 |
if (tsdPtr->tty) { |
| 265 |
Prompt(interp, 0); |
| 266 |
} |
| 267 |
} |
| 268 |
Tcl_DStringFree(&argString); |
| 269 |
|
| 270 |
outChannel = Tcl_GetStdChannel(TCL_STDOUT); |
| 271 |
if (outChannel) { |
| 272 |
Tcl_Flush(outChannel); |
| 273 |
} |
| 274 |
Tcl_DStringInit(&tsdPtr->command); |
| 275 |
Tcl_DStringInit(&tsdPtr->line); |
| 276 |
Tcl_ResetResult(interp); |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
Tk_MainLoop(); |
| 284 |
Tcl_DeleteInterp(interp); |
| 285 |
Tcl_Exit(0); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
static void |
| 310 |
StdinProc(clientData, mask) |
| 311 |
ClientData clientData; |
| 312 |
int mask; |
| 313 |
{ |
| 314 |
static int gotPartial = 0; |
| 315 |
char *cmd; |
| 316 |
int code, count; |
| 317 |
Tcl_Channel chan = (Tcl_Channel) clientData; |
| 318 |
ThreadSpecificData *tsdPtr = (ThreadSpecificData *) |
| 319 |
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData)); |
| 320 |
Tcl_Interp *interp = tsdPtr->interp; |
| 321 |
|
| 322 |
count = Tcl_Gets(chan, &tsdPtr->line); |
| 323 |
|
| 324 |
if (count < 0) { |
| 325 |
if (!gotPartial) { |
| 326 |
if (tsdPtr->tty) { |
| 327 |
Tcl_Exit(0); |
| 328 |
} else { |
| 329 |
Tcl_DeleteChannelHandler(chan, StdinProc, (ClientData) chan); |
| 330 |
} |
| 331 |
return; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
(void) Tcl_DStringAppend(&tsdPtr->command, Tcl_DStringValue( |
| 336 |
&tsdPtr->line), -1); |
| 337 |
cmd = Tcl_DStringAppend(&tsdPtr->command, "\n", -1); |
| 338 |
Tcl_DStringFree(&tsdPtr->line); |
| 339 |
if (!Tcl_CommandComplete(cmd)) { |
| 340 |
gotPartial = 1; |
| 341 |
goto prompt; |
| 342 |
} |
| 343 |
gotPartial = 0; |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
Tcl_CreateChannelHandler(chan, 0, StdinProc, (ClientData) chan); |
| 354 |
code = Tcl_RecordAndEval(interp, cmd, TCL_EVAL_GLOBAL); |
| 355 |
|
| 356 |
chan = Tcl_GetStdChannel(TCL_STDIN); |
| 357 |
if (chan) { |
| 358 |
Tcl_CreateChannelHandler(chan, TCL_READABLE, StdinProc, |
| 359 |
(ClientData) chan); |
| 360 |
} |
| 361 |
Tcl_DStringFree(&tsdPtr->command); |
| 362 |
if (Tcl_GetStringResult(interp)[0] != '\0') { |
| 363 |
if ((code != TCL_OK) || (tsdPtr->tty)) { |
| 364 |
chan = Tcl_GetStdChannel(TCL_STDOUT); |
| 365 |
if (chan) { |
| 366 |
Tcl_WriteObj(chan, Tcl_GetObjResult(interp)); |
| 367 |
Tcl_WriteChars(chan, "\n", 1); |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
prompt: |
| 377 |
if (tsdPtr->tty) { |
| 378 |
Prompt(interp, gotPartial); |
| 379 |
} |
| 380 |
Tcl_ResetResult(interp); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
static void |
| 402 |
Prompt(interp, partial) |
| 403 |
Tcl_Interp *interp; |
| 404 |
int partial; |
| 405 |
|
| 406 |
|
| 407 |
{ |
| 408 |
Tcl_Obj *promptCmd; |
| 409 |
int code; |
| 410 |
Tcl_Channel outChannel, errChannel; |
| 411 |
|
| 412 |
promptCmd = Tcl_GetVar2Ex(interp, |
| 413 |
partial ? "tcl_prompt2" : "tcl_prompt1", NULL, TCL_GLOBAL_ONLY); |
| 414 |
if (promptCmd == NULL) { |
| 415 |
defaultPrompt: |
| 416 |
if (!partial) { |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
outChannel = Tcl_GetChannel(interp, "stdout", NULL); |
| 425 |
if (outChannel != (Tcl_Channel) NULL) { |
| 426 |
Tcl_WriteChars(outChannel, "% ", 2); |
| 427 |
} |
| 428 |
} |
| 429 |
} else { |
| 430 |
code = Tcl_EvalObjEx(interp, promptCmd, TCL_EVAL_GLOBAL); |
| 431 |
if (code != TCL_OK) { |
| 432 |
Tcl_AddErrorInfo(interp, |
| 433 |
"\n (script that generates prompt)"); |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
errChannel = Tcl_GetChannel(interp, "stderr", NULL); |
| 441 |
if (errChannel != (Tcl_Channel) NULL) { |
| 442 |
Tcl_WriteObj(errChannel, Tcl_GetObjResult(interp)); |
| 443 |
Tcl_WriteChars(errChannel, "\n", 1); |
| 444 |
} |
| 445 |
goto defaultPrompt; |
| 446 |
} |
| 447 |
} |
| 448 |
outChannel = Tcl_GetChannel(interp, "stdout", NULL); |
| 449 |
if (outChannel != (Tcl_Channel) NULL) { |
| 450 |
Tcl_Flush(outChannel); |
| 451 |
} |
| 452 |
} |