Warn when a table module reads from an interactive terminal - #9175
Warn when a table module reads from an interactive terminal#9175Esteban82 wants to merge 4 commits into
Conversation
…minal gmtapi_init_import() (called by GMT_Init_IO, used by every table-reading module) registered stdin as the input source whenever no files were given, without checking whether stdin is an interactive terminal. When a module was run with no input file and no piped/redirected data, it would block forever waiting for keyboard input. Now we detect an interactive stdin via isatty()/_isatty() and fail fast with a clear error message instead.
Follow the existing gmt_notposix.h pattern used for access/fileno rather than an ad-hoc #ifdef _WIN32 block in gmt_api.c: CMake now probes for isatty/_isatty, and gmt_notposix.h maps _isatty to isatty on Windows. The <unistd.h>/<io.h> includes added earlier were redundant, since gmt_notposix.h (via gmt_dev.h) already provides both. Also test the stream actually being registered, API->GMT->session.std[GMT_IN], instead of the global stdin.
|
gmtinfo is a particular module. All other mentioned here (blockmean, etc) print the online help when only the program names is typed. I think this would introduce unnecessary complication for very little gain to distinguish when options were passed but not the file name |
|
Fair point, Tested with
|
|
I'm also against such changes. If I understand it correctly, the error message only appears in interactive terminals. But in the real world, it's more common to run GMT commands from scripts, so the changes add little to GMT. |
When a module that reads a data table (
blockmean, gmtinfo, psxy, triangulate, surface, mapproject, etc.) is run with no input file and no piped/redirected data, GMT falls back to reading from standard input. If you run it directly in a terminal — often just because you forgot the input file — the command appears to hang, with no indication of what it's waiting for.Now a message appears in that case:
It's currently a warning, so the command keeps waiting on stdin as before (you can still type data and finish with Ctrl-D).
Question:
Would a hard error instead be preferred, so it exits immediately instead of continuing to wait?
Assisted by Claude Opus 5