<i>tclsh in a command pipeline: ::tcl_interactive is 0, but interpreter is in interactive mode (halfway).</i> status still Open with 3 other changes
by
.
D 2015-04-14T17:48:43.526
J comment tclsh\sis\sschizophrenic\sabout\sinteractive\smode.\s\sIn\sthe\scase\swhere\sa\sfile\sis\r\ngiven\sas\san\sargument\sto\stclsh,\sthe\scode\spath\sis\sclear,\sand\stclsh\sruns\sin\strue\r\nnon-interactive\smode,\spassing\sthe\sscript\soff\sto\sTcl_FSEvalFileEx.\s\sIf\sstdin\sis\r\na\sterminal\sand\sa\sscript\sis\snot\sprovided,\s::tcl_interactive\sis\strue,\sand\stclsh\sruns\r\nin\sinteractive\smode.\sPrompts\sand\scommand\sresults\sare\sprinted\sto\sstdout,\serror\r\nmessages\sprinted\son\sstderr,\serrors\sdo\snot\send\sthe\sREPL,\sand\swhen\sthe\send\sof\sthe\r\nfile\sis\sreached,\sthe\sexit\sstatus\sis\s0\seven\sif\sthe\slast\scommand\sresulted\sin\san\r\nerror.\r\n\r\nHere's\sthe\smultiple-personality\scase:\s\sIf\sstdin\sis\snot\sa\sterminal\sand\sa\sscript\sis\r\nnot\sprovided,\s::tcl_interactive\sis\sfalse\sbut\stclsh\sstill\soperates\smostly\sin\r\ninteractive\smode,\sexcept\sthat\sit\sdoesn't\sprint\sprompts\sand\scommand\sresults\sto\r\nstdout.\s\sWhat\sit\sdoes\sdo\sis\snot\shalt\son\serrors,\sand\sexit\swith\sa\sstatus\sof\s0\r\neven\sif\sthe\slast\scommand\sin\sthe\sscript\sreturns\san\serror.\s\sThis\smeans\sthat\sthe\r\nexit\sstatus\sof\sa\spipeline\slike\sthe\sfollowing\sis\s0:\s\r\n\r\n<code><verbatim>echo\ssome_command_that_doesnt_exist\s|\stclsh\s</verbatim></code>\r\n\r\nIt\salso\smeans\sthat\show\sa\sscript\sis\spassed\sto\sthe\sinterpreter\scan\sdetermine\sits\r\nexit\sstatus.\s\sConsider\sa\sscript\snamed\s<code>myscript</code>\swith\sthe\sfollowing\r\ncontents:\r\n\r\n<code><verbatim>puts\stoo\smany\sarguments</verbatim></code>\r\n\r\nIf\sinvoked\slike\sthis,\sthe\sexit\sstatus\sis\s1:\r\n\r\n<code><verbatim>tclsh\smyscript</verbatim></code>\r\n\r\nBut\sif\sinvoked\sin\sthese\sways,\sthe\sshort\serror\sresult\sis\sdisplayed,\sthe\sexit\sstatus\sis\s0:\r\n\r\n<code><verbatim>tclsh\s<\smyscript</verbatim></code>\r\n\r\n<code><verbatim>cat\smyscript\s|\stclsh</verbatim></code>\r\n\r\nTclsh\sin\sa\spipeline\sis\san\simportant\suse\scase\son\sUnix-like\ssystems.\s\sIt\scould\r\nbehave\sin\sa\smore\sexpected\smanner\sby\shalting\son\serrors\sand\spassing\sa\snon-zero\r\nexit\sstatus\swhen\sscript\sinput\sis\svia\sstdin\sand\sthat\schannel\sis\snot\sa\sterminal.\r\nThe\schange\sto\saccomplish\sthat\sis\strivial:\r\n\r\n<code><verbatim>\r\n---\sgeneric/tclMain.c\r\n+++\sgeneric/tclMain.c\r\n@@\s-540,10\s+540,14\s@@\r\n\s\t\s\s\s\sif\s(code\s!=\sTCL_OK)\s{\r\n\s\t\tchan\s=\sTcl_GetStdChannel(TCL_STDERR);\r\n\s\t\tif\s(chan)\s{\r\n\s\t\t\s\s\s\sTcl_WriteObj(chan,\sTcl_GetObjResult(interp));\r\n\s\t\t\s\s\s\sTcl_WriteChars(chan,\s"\\n",\s1);\r\n+\t\t}\r\n+\t\tif\s(!is.tty)\s{\r\n+\t\t\s\s\s\sexitCode\s=\s1;\r\n+\t\t\s\s\s\sgoto\sdone;\r\n\s\t\t}\r\n\s\t\s\s\s\s}\selse\sif\s(is.tty)\s{\r\n\s\t\tresultPtr\s=\sTcl_GetObjResult(interp);\r\n\s\t\tTcl_IncrRefCount(resultPtr);\r\n\s\t\tTcl_GetStringFromObj(resultPtr,\s&length);\r\n</verbatim></code>\r\n\r\nApart\sfrom\sthe\smodified\slines\sin\sthis\spage,\sit\scan\sbe\sseen\sthat\sthis\scode\spath\r\nwas\salready\smaking\sdecisions\sbased\son\sis.tty,\seven\sthough\sthe\swhole\spath\sis\r\nostensibly\salready\sthe\sinteractive\spath.\s\sWhat\sit\sreally\sis\sis\sthe\sstream\spath.\r\nOne\sadditional\sdecision\sbased\son\sis.tty\sseems\sreasonable\senough.\s\sThis\splaces\r\nthe\sburden\sof\sdeciding\swhether\sstdin\sis\sinteractive\smore\ssquarely\son\sthe\r\nplatform-specific\sparts\sof\sTcl\sthat\slive\soff\sin\stheir\sown\sdirectories.\r\n\r\nThe\spotential\sdownside\sto\sthis\schange\sis\sany\sdisruption\sit\smay\scause\sto\r\nexisting\sprocesses\sthat\sfeed\sscripts\sto\stclsh\svia\san\sstdin\sthat\sis\snot\sa\sterminal.\r\nThis\sis\sprobably\suncommon\sbecause\sthis\sstrategy\sis\splaying\swith\sfire.\s\sSuch\r\nprocesses\swould\sno\slonger\sbe\sable\sto\srely\son\sthe\sinterpreter\sto\signore\serrors.\r\nThe\ssolution\swould\sbe\sto\sexplicitly\scatch\serrors\sin\sthe\sscripts\sor\sto\suse\r\nsomething\slike\sExpect,\swhich\scould\sprovide\sa\spseudo\sterminal.\r\n\r\nOne\shint\sthat\sthis\sfix\sis\sgood\scomes\sfrom\sthe\stest\ssuite.\s\sThe\schanges\srequired\r\nto\ssupport\sthis\spatch\sare\sfor\sthe\smost\spart\sto\suse\s::tcl_interactive\sexplicitly\r\nin\sorder\sto\sget\sthe\sdesired\soutcome,\swhich\swas\sthe\sreason\sfor\sintroducing\r\n::tcl_interactive\sin\sthe\sfirst\splace.
J login pooryorick
J mimetype text/plain
K 0e7c09eb49912d9c58b1d64dea507f64b480e8b2
U pooryorick
Z 960f089d72cf872fe207a6e6d438efa7