magic/tcltk
Darryl L. Miles 7d11b8e51b tcltk: don't discard piped stdin buffered before the stdin-channel swap
magic replaces Tcl's stdin channel with its own (TerminalInputProc) during
startup (_magic_startup).  The replacement builds a brand-new Tcl channel over
the same fd; the new channel starts with an empty buffer.  If anything had
already read() stdin bytes out of the kernel into the *old* channel's buffer
before the swap -- e.g. a script/commands piped in fast enough that Tcl buffered
them during startup -- those bytes were silently dropped, and every command
after that point vanished.  This was timing-dependent, so it surfaced only
intermittently ("piped input discarded sometimes").

Fix: before creating the replacement channel, drain any bytes still buffered in
the old channel (Tcl_InputBuffered / Tcl_ReadChars) and hand them to
TerminalInputProc via TxBuffer, which it already serves ahead of any fresh
read().  The swap is now lossless by construction rather than by luck.  On the
normal path Tcl_InputBuffered() is 0, so the new branch is inert.

How to reproduce / observe the original loss:
  Temporarily force the old channel to buffer input just before the swap, right
  after `fsOrig = Tcl_GetChannelInstanceData(oldchannel);` in _magic_startup():

      { Tcl_Obj *l = Tcl_NewObj(); Tcl_IncrRefCount(l);
        Tcl_GetsObj(oldchannel, l);       /* consume 1 line, buffering the rest */
        fprintf(stderr, "buffered=%d\n", (int)Tcl_InputBuffered(oldchannel));
        Tcl_DecrRefCount(l); }

  then pipe a burst of commands (more than one line):

      printf 'puts a\nputs b\n...\nquit -noprompt\n' | ./run_magicnull.sh

  Comment the drain out and every command after the first disappears; restore it
  and they all survive.  Measured with 218 bytes buffered: 0/20 commands survive
  without the drain, 19/20 with it (the one line the probe consumed aside).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-24 14:57:28 +00:00
..
Makefile Makefile: LD_FIRST_LIBS high-priority lib order conf control point 2026-07-23 16:09:35 -04:00
bsitools.tcl Updates from running the fix-ending-whitespace script. 2020-05-23 17:13:14 -04:00
cellmgr.tcl Modified the "tag" command to add an optional subcommand "add" or 2025-10-08 17:11:27 -04:00
console.tcl Updates from running the fix-ending-whitespace script. 2020-05-23 17:13:14 -04:00
drcmgr.tcl Quick fix to the script use of "units", which needs to expand the 2026-02-24 16:40:07 -05:00
ext2sim.sh.in Allow for bash in locations other than /bin 2023-04-22 14:13:57 -04:00
ext2spice.sh.in Allow for bash in locations other than /bin 2023-04-22 14:13:57 -04:00
libmgr.tcl Corrected the "Pick" button action in the "Library Manager" window; 2026-05-24 16:23:18 -04:00
magic.sh.in Allow for bash in locations other than /bin 2023-04-22 14:13:57 -04:00
magic.tcl.in A few minor changes to message reporting, plus some additional 2025-12-19 09:30:35 -05:00
magicdnull.c Rewrote the configuration scripts, makefiles, and some initialization 2017-10-13 10:28:07 -04:00
magicexec.c Removed the "macosx" definition from the configuration file, which 2017-10-14 18:42:24 -04:00
mazeroute.tcl Updates from running the fix-ending-whitespace script. 2020-05-23 17:13:14 -04:00
readspice.tcl Made a simple but important change to "readspice.tcl" to unexpand 2023-03-08 11:14:22 -05:00
reorderLayers.tcl EDIT: wrapper maketoolbar method refactored into a separate script, with changes to the toolbar. Reorder layers functionality added as well 2023-12-15 15:54:05 -05:00
socketcmd.tcl Updates from running the fix-ending-whitespace script. 2020-05-23 17:13:14 -04:00
strip_reflibs.tcl Updates from running the fix-ending-whitespace script. 2020-05-23 17:13:14 -04:00
tclmagic.c tcltk: don't discard piped stdin buffered before the stdin-channel swap 2026-07-24 14:57:28 +00:00
tclmagic.h I broke the CI again for the "wasm" build by adding a Tk routine 2026-06-24 09:20:06 -04:00
techbuilder.tcl Create techbuilder.tcl 2021-08-28 12:58:58 -04:00
texthelper.tcl Solved three issues: 2026-04-01 21:17:54 -04:00
tkcon.tcl Handled the case in which environment variable HOME is not set. 2026-05-13 08:51:16 -04:00
tkshell.tcl Initial commit at Tue Apr 25 08:41:48 EDT 2017 by tim on stravinsky 2017-04-25 08:41:48 -04:00
toolbar.tcl Finally got around to restoring the behavior of the toolbar with 2024-10-01 16:01:28 -04:00
toolkit.tcl Ensure all cells are loaded into memory in netlist_to_spice to avoid file corruption during save 2026-05-06 11:37:52 -04:00
toolkit_rev0.tcl Updates from running the fix-ending-whitespace script. 2020-05-23 17:13:14 -04:00
tools.tcl Corrected two errors with the crash backup mechanism, one minor, one 2026-06-22 21:52:06 -04:00
wrapper.tcl Added some support for the open PDKs in the technology manager GUI. 2026-06-08 15:10:14 -04:00