|
From: <kin...@us...> - 2023-08-11 06:50:49
|
Revision: 7152
http://sourceforge.net/p/teem/code/7152
Author: kindlmann
Date: 2023-08-11 06:50:47 +0000 (Fri, 11 Aug 2023)
Log Message:
-----------
adding and using hestParmColumnsIoctl() to contain in a single function smarts for using ioctl() to get terminal width, for the sake of hest usage
Modified Paths:
--------------
teem/trunk/src/bin/gprobe.c
teem/trunk/src/bin/ilk.c
teem/trunk/src/bin/tend.c
teem/trunk/src/bin/unu.c
teem/trunk/src/hest/hest.h
teem/trunk/src/hest/methodsHest.c
teem/trunk/src/unrrdu/flotsam.c
Modified: teem/trunk/src/bin/gprobe.c
===================================================================
--- teem/trunk/src/bin/gprobe.c 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/bin/gprobe.c 2023-08-11 06:50:47 UTC (rev 7152)
@@ -270,6 +270,7 @@
mop = airMopNew();
hparm = hestParmNew();
airMopAdd(mop, hparm, AIR_CAST(airMopper, hestParmFree), airMopAlways);
+ hestParmColumnsIoctl(hparm, hestDefaultColumns);
hparm->elideSingleOtherType = AIR_TRUE;
hparm->respectDashDashHelp = AIR_TRUE;
hestOptAdd_1_Other(&hopt, "i", "nin", &nin, NULL, "input volume", nrrdHestNrrd);
Modified: teem/trunk/src/bin/ilk.c
===================================================================
--- teem/trunk/src/bin/ilk.c 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/bin/ilk.c 2023-08-11 06:50:47 UTC (rev 7152)
@@ -21,7 +21,6 @@
#include <teem/unrrdu.h>
#include <teem/moss.h>
-#include <sys/ioctl.h>
static const char *ilkInfo
= ("(I)mage (L)inear Trans(X-->K)forms. Applies linear (homogenous coordinate) "
@@ -51,7 +50,6 @@
int debug[2], d, bound, ax0, size[2]; /* HEY size[] should be size_t */
unsigned int matListLen, _bkgLen, i, avgNum, bkgIdx;
double scale[4];
- struct winsize wsz;
me = argv[0];
mop = airMopNew();
@@ -62,12 +60,8 @@
hparm->elideSingleOtherDefault = AIR_FALSE;
hparm->elideMultipleNonExistFloatDefault = AIR_TRUE;
hparm->respFileEnable = AIR_TRUE;
+ hestParmColumnsIoctl(hparm, hestDefaultColumns);
- ioctl(0, TIOCGWINSZ, &wsz);
- /* -2 because else "\" for continuation can wrap when it shouldn't
- (which may be a hest bug) */
- hparm->columns = AIR_MAX(59, wsz.ws_col - 2);
-
hestOptAdd_1_Other(&hopt, "i", "image", &nin, "-", "input image", nrrdHestNrrd);
hestOptAdd_1_Other(&hopt, "0", "origin", &origInfo, "p:0,0",
"where to location (0,0) prior to applying transforms.\n "
Modified: teem/trunk/src/bin/tend.c
===================================================================
--- teem/trunk/src/bin/tend.c 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/bin/tend.c 2023-08-11 06:50:47 UTC (rev 7152)
@@ -21,16 +21,10 @@
#include <teem/ten.h>
-/* to learn # columns */
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
-
#define TEND "tend"
int
main(int argc, const char **argv) {
- struct winsize wsz;
int i, ret;
const char *me;
char *argv0 = NULL;
@@ -60,18 +54,8 @@
/* so that we look for, and know how to handle, seeing "--help" */
hparm->respectDashDashHelp = AIR_TRUE;
hparm->cleverPluralizeOtherY = AIR_TRUE;
- /* (following ~copied from unu.c) */
- /* Try to dynamically learn number of columns. Learning the terminal size will probably
- work if stdout is the terminal, but not if we're piping elsewhere (as is common with
- unu), Then try stderr, or else use an old reliable number */
- if (-1 != ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsz)) {
- hparm->columns = wsz.ws_col - 2;
- } else if (-1 != ioctl(STDERR_FILENO, TIOCGWINSZ, &wsz)) {
- hparm->columns = wsz.ws_col - 2;
- } else {
- /* old default */
- hparm->columns = 78;
- }
+ /* set hparm->columns from ioctl if possible, else use 78 */
+ hestParmColumnsIoctl(hparm, 78);
/* if there are no arguments, then we give general usage information */
if (1 >= argc) {
Modified: teem/trunk/src/bin/unu.c
===================================================================
--- teem/trunk/src/bin/unu.c 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/bin/unu.c 2023-08-11 06:50:47 UTC (rev 7152)
@@ -21,16 +21,10 @@
#include <teem/unrrdu.h>
-/* to learn # columns */
-#include <sys/types.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
-
#define UNU "unu"
int
main(int argc, const char **argv) {
- struct winsize wsz;
int i, ret, listAll;
const char *me;
char *argv0 = NULL;
@@ -76,16 +70,8 @@
hparm->elideMultipleEmptyStringDefault = AIR_TRUE;
/* say that we look for, and know how to handle, seeing "--help" */
hparm->respectDashDashHelp = AIR_TRUE;
- /* Try to dynamically learn number of columns. Learning the terminal size will probably
- work if stdout is the terminal, but not if we're piping elsewhere (as is common with
- unu), Then try stderr, or else use unrrduDefNumColumns */
- if (-1 != ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsz)) {
- hparm->columns = wsz.ws_col - 2;
- } else if (-1 != ioctl(STDERR_FILENO, TIOCGWINSZ, &wsz)) {
- hparm->columns = wsz.ws_col - 2;
- } else {
- hparm->columns = unrrduDefNumColumns;
- }
+ /* set hparm->columns from ioctl if possible, else use unrrduDefNumColumns */
+ hestParmColumnsIoctl(hparm, unrrduDefNumColumns);
hparm->greedySingleString = AIR_TRUE;
/* if there are no arguments, or "unu list" (or "unu all" shhh), then we give general
Modified: teem/trunk/src/hest/hest.h
===================================================================
--- teem/trunk/src/hest/hest.h 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/hest/hest.h 2023-08-11 06:50:47 UTC (rev 7152)
@@ -141,7 +141,7 @@
length of the hestOpt array, and in arrLen the number of hestOpts actually used and
set. This facilitates implementing something much like an airArray, but without the
burden of extra calls for the user (like airArrayLenIncr), nor new kinds of containers
- for hest and its users to manage: just same same array of hestOpt structs */
+ for hest and its users to manage: it is just the same array of hestOpt structs */
unsigned int arrAlloc, arrLen;
/* --------------------- Output
@@ -245,6 +245,7 @@
HEST_EXPORT hestParm *hestParmNew(void);
HEST_EXPORT hestParm *hestParmFree(hestParm *parm);
HEST_EXPORT void *hestParmFree_vp(void *parm);
+HEST_EXPORT int hestParmColumnsIoctl(hestParm *hparm, unsigned int nonIoctlColumns);
HEST_EXPORT void hestOptSingleSet(hestOpt *opt, const char *flag, const char *name,
int type, unsigned int min, int max, void *valueP,
const char *dflt, const char *info, unsigned int *sawP,
Modified: teem/trunk/src/hest/methodsHest.c
===================================================================
--- teem/trunk/src/hest/methodsHest.c 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/hest/methodsHest.c 2023-08-11 06:50:47 UTC (rev 7152)
@@ -24,6 +24,9 @@
#include <limits.h>
#include <assert.h>
+#include <sys/ioctl.h> /* for ioctl(), TIOCGWINSZ, struct winsize */
+#include <unistd.h> /* for STDOUT_FILENO and friends */
+
const int hestPresent = 42;
/* INCR is like airArray->incr: granularity with which we (linearly) reallocate the
@@ -83,6 +86,32 @@
return NULL;
}
+/* hestParmColumnsIoctl:
+Try to dynamically learn number of columns in the current terminal from ioctl(), and save
+it in hparm->columns. Learning the terminal size from stdin will probably work if we're
+not being piped into, else try learning it from stdout (but that won't work if we're
+piping elsewhere), else try learning the terminal size from stderr. If one of these
+works, then hparm->columns is set via ioctl(), and we return 0. If ioctl() never worked,
+then hparm->columns gets the given nonIoctlColumns, and we return 1 (but this 1 is not an
+error that needs any recovering from). */
+int
+hestParmColumnsIoctl(hestParm *hparm, unsigned int nonIoctlColumns) {
+ struct winsize wsz;
+ int ret;
+ if (-1 != ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz)
+ || -1 != ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsz)
+ || -1 != ioctl(STDERR_FILENO, TIOCGWINSZ, &wsz)) {
+ /* the "- 2" here may be the sign of a hest bug; sometimes it seems the "\" for line
+ continuation (in generated usage info) causes a line wrap when it shouldn't */
+ hparm->columns = wsz.ws_col - 2;
+ ret = 0;
+ } else {
+ hparm->columns = nonIoctlColumns;
+ ret = 1;
+ }
+ return ret;
+}
+
/* _hestMax(-1) == INT_MAX, otherwise _hestMax(m) == m */
int
_hestMax(int max) {
Modified: teem/trunk/src/unrrdu/flotsam.c
===================================================================
--- teem/trunk/src/unrrdu/flotsam.c 2023-08-05 12:09:58 UTC (rev 7151)
+++ teem/trunk/src/unrrdu/flotsam.c 2023-08-11 06:50:47 UTC (rev 7152)
@@ -89,14 +89,9 @@
hparm->elideSingleEmptyStringDefault = AIR_TRUE;
hparm->elideMultipleEmptyStringDefault = AIR_TRUE;
hparm->cleverPluralizeOtherY = AIR_TRUE;
- /* learning columns from current window; if ioctl is available
- if (1) {
- struct winsize ws;
- ioctl(1, TIOCGWINSZ, &ws);
- hparm->columns = ws.ws_col - 1;
- }
- */
- hparm->columns = 78;
+ /* it is up to caller to try hestParmColumnsIoctl if they want
+ output to vary depending on current terminal. If nothing else;
+ hestParmNew() above sets hparm->columns to hestDefaultColumns */
}
/* if there are no arguments, then we give general usage information */
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|