GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/unrrdu/env.c Lines: 128 156 82.1 %
Date: 2017-05-26 Branches: 17 34 50.0 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "unrrdu.h"
25
#include "privateUnrrdu.h"
26
27
#define INFO "List relevant environment variables and their values"
28
static const char *_unrrdu_envInfoL =
29
  (INFO
30
   ". These environment variables provide a way of "
31
   "setting global variables that can affect"
32
   " the way Nrrd (and unu) operates.\n "
33
   "* Uses nrrdGetenvBool, nrrdGetenvEnum, "
34
   "nrrdGetenvInt, and nrrdGetenvUInt");
35
36
void
37
_unrrdu_envBool(FILE *file, const char *envKey, int currVal,
38
                const char *varName, const char *desc, int columns) {
39
21
  int val, ret;
40
14
  char *envVal;
41
42
14
  fprintf(file, "%s (bool): ", envKey);
43
14
  ret = nrrdGetenvBool(&val, &envVal, envKey);
44

14
  switch(ret) {
45
  case -1:
46
7
    fprintf(file, "not set\n");
47
7
    break;
48
  case AIR_TRUE:
49
    fprintf(file, "set to \"%s\"\n", envVal);
50
    break;
51
  case AIR_FALSE:
52
    fprintf(file, "set to \"%s\"? (invalid) \n", envVal);
53
    break;
54
  }
55

14
  switch(ret) {
56
  case -1:
57
  case AIR_FALSE:
58
7
    fprintf(file, "  (%s == %s; unchanged)\n",
59
7
            varName, airEnumStr(airBool, currVal));
60
7
    break;
61
  case AIR_TRUE:
62
    fprintf(file, "  ==> %s = %s   **********************\n",
63
            varName, airEnumStr(airBool, currVal));
64
    break;
65
  }
66
7
  _hestPrintStr(file, 0, 0, columns, desc, AIR_FALSE);
67
7
  fprintf(file, "\n");
68
7
}
69
70
void
71
_unrrdu_envEnum(FILE *file, const airEnum *enm, const char *envKey,
72
                int currVal, const char *varName,
73
                const char *desc, int columns) {
74
12
  int val, ret;
75
8
  char *envVal;
76
77
  /* !!! HEY: CUT + PASTE !!! */
78
8
  fprintf(file, "%s (%s enum): ", envKey, enm->name);
79
8
  ret = nrrdGetenvEnum(&val, &envVal, enm, envKey);
80

8
  switch(ret) {
81
  case -1:
82
4
    fprintf(file, "not set\n");
83
4
    break;
84
  case AIR_TRUE:
85
    fprintf(file, "set to \"%s\"\n", envVal);
86
    break;
87
  case AIR_FALSE:
88
    fprintf(file, "set to \"%s\"? (invalid) \n", envVal);
89
    break;
90
  }
91

8
  switch(ret) {
92
  case -1:
93
  case AIR_FALSE:
94
4
    fprintf(file, "  (%s == %s; unchanged)\n",
95
4
            varName, airEnumStr(enm, currVal));
96
4
    break;
97
  case AIR_TRUE:
98
    fprintf(file, "  ==> %s = %s   **********************\n",
99
            varName, airEnumStr(enm, currVal));
100
    break;
101
  }
102
4
  _hestPrintStr(file, 0, 0, columns, desc, AIR_FALSE);
103
4
  fprintf(file, "\n");
104
  /* !!! HEY: CUT + PASTE !!! */
105
4
}
106
107
void
108
_unrrdu_envInt(FILE *file, const char *envKey,
109
               int currVal, const char *varName,
110
               const char *desc, int columns) {
111
6
  int val, ret;
112
4
  char *envVal;
113
114
  /* !!! HEY: CUT + PASTE !!! */
115
4
  fprintf(file, "%s (int): ", envKey);
116
4
  ret = nrrdGetenvInt(&val, &envVal, envKey);
117

4
  switch(ret) {
118
  case -1:
119
2
    fprintf(file, "not set\n");
120
2
    break;
121
  case AIR_TRUE:
122
    fprintf(file, "set to \"%s\"\n", envVal);
123
    break;
124
  case AIR_FALSE:
125
    fprintf(file, "set to \"%s\"? (invalid) \n", envVal);
126
    break;
127
  }
128

4
  switch(ret) {
129
  case -1:
130
  case AIR_FALSE:
131
2
    fprintf(file, "  (%s == %d; unchanged)\n",
132
            varName, currVal);
133
2
    break;
134
  case AIR_TRUE:
135
    fprintf(file, "  ==> %s = %d   **********************\n",
136
            varName, currVal);
137
    break;
138
  }
139
2
  _hestPrintStr(file, 0, 0, columns, desc, AIR_FALSE);
140
2
  fprintf(file, "\n");
141
  /* !!! HEY: CUT + PASTE !!! */
142
2
}
143
144
void
145
_unrrdu_envUInt(FILE *file, const char *envKey,
146
                unsigned int currVal, const char *varName,
147
                const char *desc, int columns) {
148
  int ret;
149
6
  unsigned int val;
150
4
  char *envVal;
151
152
  /* !!! HEY: CUT + PASTE !!! */
153
4
  fprintf(file, "%s (unsigned int): ", envKey);
154
4
  ret = nrrdGetenvUInt(&val, &envVal, envKey);
155

4
  switch(ret) {
156
  case -1:
157
2
    fprintf(file, "not set\n");
158
2
    break;
159
  case AIR_TRUE:
160
    fprintf(file, "set to \"%s\"\n", envVal);
161
    break;
162
  case AIR_FALSE:
163
    fprintf(file, "set to \"%s\"? (invalid) \n", envVal);
164
    break;
165
  }
166

4
  switch(ret) {
167
  case -1:
168
  case AIR_FALSE:
169
2
    fprintf(file, "  (%s == %d; unchanged)\n",
170
            varName, currVal);
171
2
    break;
172
  case AIR_TRUE:
173
    fprintf(file, "  ==> %s = %u   **********************\n",
174
            varName, currVal);
175
    break;
176
  }
177
2
  _hestPrintStr(file, 0, 0, columns, desc, AIR_FALSE);
178
2
  fprintf(file, "\n");
179
  /* !!! HEY: CUT + PASTE !!! */
180
2
}
181
182
int
183
unrrdu_envMain(int argc, const char **argv, const char *me,
184
               hestParm *hparm) {
185
  FILE *out;
186
187
  AIR_UNUSED(argc);
188
  AIR_UNUSED(argv);
189
  AIR_UNUSED(me);
190
2
  out = stdout;
191
192
1
  hestInfo(out, me, _unrrdu_envInfoL, hparm);
193
1
  fprintf(out, "\n");
194
195
1
  _hestPrintStr(out, 0, 0, hparm->columns,
196
                ("Each variable in the listing below starts with the name of "
197
                 "the environment variable (\"NRRD_...\"), what type of value "
198
                 "it represents (e.g. \"int\", \"bool\"), what the "
199
                 "environment variable is currently set to, what the "
200
                 "corresponding Nrrd global variable is set to, and a "
201
                 "description of the variable."),
202
                AIR_FALSE);
203
1
  fprintf(out, "\n");
204
205
1
  _hestPrintStr(out, 0, 0, hparm->columns,
206
                ("Bool variables may be set to true simply by setting the "
207
                 "environment variable; setting the value to \"true\" or "
208
                 "\"false\" sets the bool accordingly.  Enum variables may "
209
                 "be set by setting the environment variable to any string "
210
                 "that parses as one of the enum values.  Int and unsigned "
211
                 "int variables are set via a string parse-able as a numeric "
212
                 "value."),
213
                AIR_FALSE);
214
1
  fprintf(out, "\n");
215
216
  /* UNRRDU_QUIET_QUIT functionality implemented in privateUnrrdu.h */
217
1
  _hestPrintStr(out, 0, 0, hparm->columns,
218
                ("In addition to the the \"NRRD_\" environment variables, "
219
                 "there is this one, " UNRRDU_QUIET_QUIT_ENV ", which "
220
                 "determines whether unu exits "
221
                 "quietly (without error and usage info) when it fails "
222
                 "because an input nrrd read immediately hit EOF (as "
223
                 "happens when many unu invocations are piped together). "
224
                 "This is currently detected by seeing if the error message "
225
                 "ends with \n \"" UNRRDU_QUIET_QUIT_STR "\"."),
226
                AIR_FALSE);
227
1
  fprintf(out, "\n");
228
229
1
  fprintf(out, "%s: ", UNRRDU_QUIET_QUIT_ENV);
230
1
  if (getenv(UNRRDU_QUIET_QUIT_ENV)) {
231
    fprintf(out, "is set (to what doesn't matter); quiet-quit enabled\n");
232
  } else {
233
1
    fprintf(out, "is NOT set; quiet-quit NOT enabled\n");
234
  }
235
1
  fprintf(out, "\n");
236
237
1
  _unrrdu_envBool(out,
238
1
                  nrrdEnvVarStateKeyValuePairsPropagate,
239
1
                  nrrdStateKeyValuePairsPropagate,
240
                  "nrrdStateKeyValuePairsPropagate",
241
                  "When true, key/value pairs are copied from input "
242
                  "nrrd to output nrrd just like other basic info that hasn't "
243
                  "just been modified (e.g. type, dimension, block size).",
244
1
                  hparm->columns);
245
1
  _unrrdu_envEnum(out,
246
1
                  nrrdCenter, nrrdEnvVarDefaultCenter,
247
1
                  nrrdDefaultCenter,
248
                  "nrrdDefaultCenter",
249
                  "The type of sample centering to use when none has been "
250
                  "set but one has to be chosen for some operation "
251
                  "(e.g. resampling).",
252
1
                  hparm->columns);
253
1
  _unrrdu_envEnum(out,
254
1
                  nrrdEncodingType, nrrdEnvVarDefaultWriteEncodingType,
255
1
                  nrrdDefaultWriteEncodingType,
256
                  "nrrdDefaultWriteEncodingType",
257
                  "When writing nrrds, what encoding to use. Only "
258
                  "\"unu save\" affords explicit control of output encoding.",
259
1
                  hparm->columns);
260
1
  _unrrdu_envBool(out,
261
1
                  nrrdEnvVarStateKindNoop,
262
1
                  nrrdStateKindNoop,
263
                  "nrrdStateKindNoop",
264
                  "When true, Nrrd makes not even the slightest effort to be "
265
                  "smart about setting the \"kind\" field of an axis after "
266
                  "some operation that modified its samples.",
267
1
                  hparm->columns);
268
1
  _unrrdu_envInt(out,
269
1
                 nrrdEnvVarStateVerboseIO,
270
1
                 nrrdStateVerboseIO,
271
                 "nrrdStateVerboseIO",
272
                 "The verbosity level of Nrrd input/output operations.",
273
1
                  hparm->columns);
274
1
  _unrrdu_envBool(out,
275
1
                  nrrdEnvVarStateBlind8BitRange,
276
1
                  nrrdStateBlind8BitRange,
277
                  "nrrdStateBlind8BitRange",
278
                  "When true, the determined range of 8-bit data will always "
279
                  "be [0,255] (for uchar) or [-128,127] (for signed char), "
280
                  "instead of actually looking into the data to find its "
281
                  "range.",
282
1
                  hparm->columns);
283
1
  _unrrdu_envBool(out,
284
1
                  nrrdEnvVarDefaultWriteBareText,
285
1
                  nrrdDefaultWriteBareText,
286
                  "nrrdDefaultWriteBareText",
287
                  "When false, text files used for saving nrrds start with "
288
                  "comment (\"# ...\") lines containing nrrd fields.",
289
1
                  hparm->columns);
290
1
  _unrrdu_envEnum(out,
291
1
                  nrrdType, nrrdEnvVarStateMeasureType,
292
1
                  nrrdStateMeasureType,
293
                  "nrrdStateMeasureType",
294
                  "For measurements (\"unu project\") like sum and product, "
295
                  "the type of the output result, when one hasn't been "
296
                  "explicitly requested.",
297
1
                  hparm->columns);
298
1
  _unrrdu_envInt(out,
299
1
                 nrrdEnvVarStateMeasureModeBins,
300
1
                 nrrdStateMeasureModeBins,
301
                 "nrrdStateMeasureModeBins",
302
                 "When measuring mode but without a given histogram, how many "
303
                 "bins to use in the temporary internal histogram.",
304
1
                  hparm->columns);
305
1
  _unrrdu_envEnum(out,
306
1
                  nrrdType, nrrdEnvVarStateMeasureHistoType,
307
1
                  nrrdStateMeasureHistoType,
308
                  "nrrdStateMeasureHistoType",
309
                  "Output type for most measurements of histograms, when one "
310
                  "hasn't been explicitly requested",
311
1
                  hparm->columns);
312
1
  _unrrdu_envBool(out,
313
1
                  nrrdEnvVarStateAlwaysSetContent,
314
1
                  nrrdStateAlwaysSetContent,
315
                  "nrrdStateAlwaysSetContent",
316
                  "If true, the output content string is set even when the "
317
                  "input content string is not set.",
318
1
                  hparm->columns);
319
1
  _unrrdu_envBool(out,
320
1
                  nrrdEnvVarStateDisableContent,
321
1
                  nrrdStateDisableContent,
322
                  "nrrdStateDisableContent",
323
                  "If true, output content is never set.",
324
1
                  hparm->columns);
325
1
  _unrrdu_envUInt(out,
326
1
                  nrrdEnvVarDefaultWriteCharsPerLine,
327
1
                  nrrdDefaultWriteCharsPerLine,
328
                  "nrrdDefaultWriteCharsPerLine",
329
                  "When using text encoding, maximum # characters allowed "
330
                  "per line.",
331
1
                  hparm->columns);
332
1
  _unrrdu_envUInt(out,
333
1
                  nrrdEnvVarDefaultWriteValsPerLine,
334
1
                  nrrdDefaultWriteValsPerLine,
335
                  "nrrdDefaultWriteValsPerLine",
336
                  "When using text encoding, maximum # values allowed "
337
                  "per line",
338
1
                  hparm->columns);
339
1
  _unrrdu_envBool(out,
340
1
                  nrrdEnvVarStateGrayscaleImage3D,
341
1
                  nrrdStateGrayscaleImage3D,
342
                  "nrrdStateGrayscaleImage3D",
343
                  "If true, reading a 2-D grayscale image results in a "
344
                  "3-D image with a single sample (size=1) on the first "
345
                  "(fastest) axis.",
346
1
                  hparm->columns);
347
348
#if 0
349
  /* GLK is ambivalent about the continued existence of these ... */
350
  nrrdGetenvDouble(/**/ &nrrdDefaultKernelParm0,
351
                   nrrdEnvVarDefaultKernelParm0);
352
  nrrdGetenvDouble(/**/ &nrrdDefaultSpacing,
353
                   nrrdEnvVarDefaultSpacing);
354
#endif
355
356
  /* NOTE: this is an exceptional unu command that doesn't rely on
357
     privateUnrrdu.h USAGE() macro; so we determine our own return value */
358
1
  return 0;
359
}
360
361
UNRRDU_CMD(env, INFO);