|
From: <kin...@us...> - 2024-09-19 20:41:56
|
Revision: 7240
http://sourceforge.net/p/teem/code/7240
Author: kindlmann
Date: 2024-09-19 20:41:54 +0000 (Thu, 19 Sep 2024)
Log Message:
-----------
API NEW: new field NrrdIoState->declineStdioOnTTY, which instructs nrrdLoad/nrrdSave to decline reading/writing to stdin/stdout if the given filename is - AND stdin/stdout is a terminal (as per isatty). Can be over-ridden by using filename -=. This is long-wanted functionality that was only half-addressed, on the command-line input side, via nrrdHestNrrdNoTTY
Modified Paths:
--------------
teem/trunk/src/nrrd/defaultsNrrd.c
teem/trunk/src/nrrd/methodsNrrd.c
teem/trunk/src/nrrd/nrrd.h
teem/trunk/src/nrrd/read.c
teem/trunk/src/nrrd/write.c
Modified: teem/trunk/src/nrrd/defaultsNrrd.c
===================================================================
--- teem/trunk/src/nrrd/defaultsNrrd.c 2024-09-19 20:36:33 UTC (rev 7239)
+++ teem/trunk/src/nrrd/defaultsNrrd.c 2024-09-19 20:41:54 UTC (rev 7240)
@@ -51,6 +51,8 @@
double nrrdDefaultResamplePadValue = 0.0;
int nrrdDefaultResampleNonExistent = nrrdResampleNonExistentNoop;
double nrrdDefaultKernelParm0 = 1.0;
+/* creating and by-default enabling this functionality is a change for Teem2 */
+int nrrdDefaultDeclineStdioOnTTY = AIR_TRUE;
/* ---- END non-NrrdIO */
int nrrdDefaultCenter = nrrdCenterCell;
double nrrdDefaultSpacing = 1.0;
Modified: teem/trunk/src/nrrd/methodsNrrd.c
===================================================================
--- teem/trunk/src/nrrd/methodsNrrd.c 2024-09-19 20:36:33 UTC (rev 7239)
+++ teem/trunk/src/nrrd/methodsNrrd.c 2024-09-19 20:41:54 UTC (rev 7240)
@@ -278,6 +278,9 @@
nio->skipData = AIR_FALSE;
nio->skipFormatURL = AIR_FALSE;
nio->keepNrrdDataFileOpen = AIR_FALSE;
+ /* ---- BEGIN non-NrrdIO */
+ nio->declineStdioOnTTY = nrrdDefaultDeclineStdioOnTTY;
+ /* ---- END non-NrrdIO */
nio->zlibLevel = -1;
nio->zlibStrategy = nrrdZlibStrategyDefault;
nio->bzip2BlockSize = -1;
Modified: teem/trunk/src/nrrd/nrrd.h
===================================================================
--- teem/trunk/src/nrrd/nrrd.h 2024-09-19 20:36:33 UTC (rev 7239)
+++ teem/trunk/src/nrrd/nrrd.h 2024-09-19 20:41:54 UTC (rev 7240)
@@ -427,15 +427,29 @@
nrrd format. Probably used in conjunction with
skipData. (currently for "unu data")
ON WRITE: no semantics */
- zlibLevel, /* zlib compression level (0-9, -1 for
- default[6], 0 for no compression). */
- zlibStrategy, /* zlib compression strategy, can be one
- of the nrrdZlibStrategy enums, default is
- nrrdZlibStrategyDefault. */
- bzip2BlockSize, /* block size used for compression,
- roughly equivalent to better but slower
- (1-9, -1 for default[9]). */
/* ---- BEGIN non-NrrdIO */
+ declineStdioOnTTY, /* ON READ and ON WRITE: If nrrdLoad is about to read from
+ filename "-" (via nrrdRead), or nrrdSave is about to write to
+ "-" (via nrrdWrite), regardless of file format (which isn't
+ known on read, though may be known on write): if this is
+ non-zero, decline to nrrdRead from stdin or nrrdWrite to stdout
+ IF stdin/stdout seems to be a terminal (as per isatty()). On
+ read, this avoids cryptic stalls as something tries to read
+ from the terminal (where a human is unlikely to be typing the
+ file contents), and on write, this avoids clobbering the
+ terminal with screens of non-printing characters. Using
+ filename "-=" is a sneaky way to name stdin/stdout while
+ over-riding this declination and force that IO to happen. */
+ /* ---- END non-NrrdIO */
+ zlibLevel, /* zlib compression level (0-9, -1 for
+ default[6], 0 for no compression). */
+ zlibStrategy, /* zlib compression strategy, can be one
+ of the nrrdZlibStrategy enums, default is
+ nrrdZlibStrategyDefault. */
+ bzip2BlockSize, /* block size used for compression,
+ roughly equivalent to better but slower
+ (1-9, -1 for default[9]). */
+ /* ---- BEGIN non-NrrdIO */
/* seems odd to have contents of NrrdIoState differ between full Teem
and NrrdIO, but these fields can't be meaningfully set or read if
the nrrdFormatPNGsRGBIntent is not available */
@@ -707,6 +721,7 @@
NRRD_EXPORT double nrrdDefaultResamplePadValue;
NRRD_EXPORT int nrrdDefaultResampleNonExistent;
NRRD_EXPORT double nrrdDefaultKernelParm0;
+NRRD_EXPORT int nrrdDefaultDeclineStdioOnTTY;
/* ---- END non-NrrdIO */
NRRD_EXPORT int nrrdDefaultCenter;
NRRD_EXPORT double nrrdDefaultSpacing;
Modified: teem/trunk/src/nrrd/read.c
===================================================================
--- teem/trunk/src/nrrd/read.c 2024-09-19 20:36:33 UTC (rev 7239)
+++ teem/trunk/src/nrrd/read.c 2024-09-19 20:41:54 UTC (rev 7240)
@@ -26,6 +26,10 @@
# include <bzlib.h>
#endif
+/* ---- BEGIN non-NrrdIO */
+#include <unistd.h> /* for isatty() and STDIN_FILENO */
+/* ---- END non-NrrdIO */
+
/* (not apparently used) const char *const _nrrdRelativePathFlag = "./"; */
const char *const _nrrdFieldSep = " \t";
static const char *const _nrrdLineSep = "\r\n";
@@ -639,6 +643,19 @@
airMopError(mop);
return 2;
}
+ /* ---- BEGIN non-NrrdIO */
+ if (nio->declineStdioOnTTY /* if we're cautious about reading from stdin */
+ && stdin == file /* and we're reading from stdin */
+ && strcmp("-=", filename) /* and filename is NOT -= (which over-rides caution) */
+ && isatty(STDIN_FILENO) /* and stdin is a tty */) {
+ biffAddf(NRRD,
+ "%s: declining to try reading Nrrd from terminal (tty) stdin "
+ "(implied by filename \"%s\"; over-ride with \"-=\")",
+ me, filename);
+ airMopError(mop);
+ return 1;
+ }
+ /* ---- END non-NrrdIO */
airMopAdd(mop, file, (airMopper)airFclose, airMopOnError);
/* non-error exiting is handled below */
Modified: teem/trunk/src/nrrd/write.c
===================================================================
--- teem/trunk/src/nrrd/write.c 2024-09-19 20:36:33 UTC (rev 7239)
+++ teem/trunk/src/nrrd/write.c 2024-09-19 20:41:54 UTC (rev 7240)
@@ -22,10 +22,9 @@
#include "nrrd.h"
#include "privateNrrd.h"
-/*
- #include <sys/types.h>
- #include <unistd.h>
-*/
+/* ---- BEGIN non-NrrdIO */
+#include <unistd.h> /* for isatty() and STDIN_FILENO */
+/* ---- END non-NrrdIO */
int /* Biff: 1 */
nrrdIoStateSet(NrrdIoState *nio, int parm, int value) {
@@ -1018,6 +1017,19 @@
airMopError(mop);
return 1;
}
+ /* ---- BEGIN non-NrrdIO */
+ if (nio->declineStdioOnTTY /* if we're cautious about writing to stdout */
+ && stdout == file /* and we're writing to stdout */
+ && strcmp("-=", filename) /* and filename is NOT -= (which over-rides caution) */
+ && isatty(STDOUT_FILENO) /* and stdout is a tty */) {
+ biffAddf(NRRD,
+ "%s: declining to try writing file to terminal (tty) stdout "
+ "(implied by filename \"%s\"; over-ride with \"-=\")",
+ me, filename);
+ airMopError(mop);
+ return 1;
+ }
+ /* ---- END non-NrrdIO */
airMopAdd(mop, file, (airMopper)airFclose, airMopAlways);
if (nrrdWrite(file, nrrd, nio)) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|