From c8470a24739ec85403cd829e32f4a50b391bfb3b Mon Sep 17 00:00:00 2001 From: Ralph Lange Date: Tue, 15 Dec 2009 22:05:55 +0000 Subject: [PATCH] Added -l.. commandline options to catools (by Stephanie Allison) --- src/ca/CAref.html | 24 ++++++++++++++++++++++++ src/catools/caget.c | 25 +++++++++++++++++++------ src/catools/camonitor.c | 20 +++++++++++++++----- src/catools/tool_lib.c | 30 ++++++++++++++++++++++++------ src/catools/tool_lib.h | 3 ++- 5 files changed, 84 insertions(+), 18 deletions(-) diff --git a/src/ca/CAref.html b/src/ca/CAref.html index 5f1e00c3d..717d28b6b 100644 --- a/src/ca/CAref.html +++ b/src/ca/CAref.html @@ -1280,6 +1280,18 @@ the output.

-s Get value as string (may honour server-side precision) + + -lx + Round to long integer and print as hex number + + + -lo + Round to long integer and print as octal number + + + -lb + Round to long integer and print as binary number + Integer number format: @@ -1412,6 +1424,18 @@ the output.

-s Get value as string (may honour server-side precision) + + -lx + Round to long integer and print as hex number + + + -lo + Round to long integer and print as octal number + + + -lb + Round to long integer and print as binary number + Integer number format: diff --git a/src/catools/caget.c b/src/catools/caget.c index 3acef9331..bae87e7b5 100644 --- a/src/catools/caget.c +++ b/src/catools/caget.c @@ -88,6 +88,9 @@ static void usage (void) " -f : Use %%f format, with a precision of digits\n" " -g : Use %%g format, with a precision of digits\n" " -s: Get value as string (may honour server-side precision)\n" + " -lx: Round to long integer and print as hex number\n" + " -lo: Round to long integer and print as octal number\n" + " -lb: Round to long integer and print as binary number\n" "Integer number format:\n" " Default: Print as decimal number\n" " -0x: Print as hex number\n" @@ -376,6 +379,7 @@ int main (int argc, char *argv[]) int result; /* CA result */ OutputT format = plain; /* User specified format */ RequestT request = get; /* User specified request type */ + IntFormatT outType; /* Output type */ int count = 0; /* 0 = not specified by -# option */ int opt; /* getopt() current option */ @@ -387,7 +391,7 @@ int main (int argc, char *argv[]) setvbuf(stdout,NULL,_IOLBF,BUFSIZ); /* Set stdout to line buffering */ - while ((opt = getopt(argc, argv, ":taicnhsSe:f:g:#:d:0:w:p:F:")) != -1) { + while ((opt = getopt(argc, argv, ":taicnhsSe:f:g:l:#:d:0:w:p:F:")) != -1) { switch (opt) { case 'h': /* Print usage */ usage(); @@ -472,15 +476,24 @@ int main (int argc, char *argv[]) "out of range - ignored.\n", digits, opt); } break; + case 'l': /* Convert to long and use integer format */ case '0': /* Select integer format */ - type = DBR_LONG; switch ((char) *optarg) { - case 'x': outType = hex; break; /* 0x print Hex */ - case 'b': outType = bin; break; /* 0b print Binary */ - case 'o': outType = oct; break; /* 0o print Octal */ + case 'x': outType = hex; break; /* x print Hex */ + case 'b': outType = bin; break; /* b print Binary */ + case 'o': outType = oct; break; /* o print Octal */ default : + outType = dec; fprintf(stderr, "Invalid argument '%s' " - "for option '-0' - ignored.\n", optarg); + "for option '-%c' - ignored.\n", optarg, opt); + } + if (outType != dec) { + if (opt == '0') { + type = DBR_LONG; + outTypeI = outType; + } else { + outTypeF = outType; + } } break; case 'F': /* Store this for output and tool_lib formatting */ diff --git a/src/catools/camonitor.c b/src/catools/camonitor.c index 4c293df71..82ee20fa8 100644 --- a/src/catools/camonitor.c +++ b/src/catools/camonitor.c @@ -71,6 +71,9 @@ void usage (void) " -f : Use %%f format, with a precision of digits\n" " -g : Use %%g format, with a precision of digits\n" " -s: Get value as string (may honour server-side precision)\n" + " -lx: Round to long integer and print as hex number\n" + " -lo: Round to long integer and print as octal number\n" + " -lb: Round to long integer and print as binary number\n" "Integer number format:\n" " Default: Print as decimal number\n" " -0x: Print as hex number\n" @@ -204,6 +207,7 @@ int main (int argc, char *argv[]) int returncode = 0; int n = 0; int result; /* CA result */ + IntFormatT outType; /* Output type */ int opt; /* getopt() current option */ int digits = 0; /* getopt() no. of float digits */ @@ -213,7 +217,7 @@ int main (int argc, char *argv[]) setvbuf(stdout,NULL,_IOLBF,BUFSIZ); /* Set stdout to line buffering */ - while ((opt = getopt(argc, argv, ":nhm:sSe:f:g:#:d:0:w:t:p:F:")) != -1) { + while ((opt = getopt(argc, argv, ":nhm:sSe:f:g:l:#:0:w:t:p:F:")) != -1) { switch (opt) { case 'h': /* Print usage */ usage(); @@ -307,14 +311,20 @@ int main (int argc, char *argv[]) "out of range - ignored.\n", digits, opt); } break; + case 'l': /* Convert to long and use integer format */ case '0': /* Select integer format */ switch ((char) *optarg) { - case 'x': outType = hex; break; /* 0x print Hex */ - case 'b': outType = bin; break; /* 0b print Binary */ - case 'o': outType = oct; break; /* 0o print Octal */ + case 'x': outType = hex; break; /* x print Hex */ + case 'b': outType = bin; break; /* b print Binary */ + case 'o': outType = oct; break; /* o print Octal */ default : + outType = dec; fprintf(stderr, "Invalid argument '%s' " - "for option '-0' - ignored.\n", optarg); + "for option '-%c' - ignored.\n", optarg, opt); + } + if (outType != dec) { + if (opt == '0') outTypeI = outType; + else outTypeF = outType; } break; case 'F': /* Store this for output and tool_lib formatting */ diff --git a/src/catools/tool_lib.c b/src/catools/tool_lib.c index 7ffc9efdd..0bfcab3a3 100644 --- a/src/catools/tool_lib.c +++ b/src/catools/tool_lib.c @@ -45,7 +45,8 @@ static int tsInitC = 0; /* Flag: Client timestamps init'd */ TimeT tsType = absolute; /* Timestamp type flag (-t option) */ int tsSrcServer = 1; /* Timestamp source flag (-t option) */ int tsSrcClient = 0; /* Timestamp source flag (-t option) */ -IntFormatT outType = dec; /* For -0.. output format option */ +IntFormatT outTypeI = dec; /* For -0.. output format option */ +IntFormatT outTypeF = dec; /* For -l.. output format option */ char dblFormatStr[30] = "%g"; /* Format string to print doubles (-efg options) */ char timeFormatStr[30] = "%Y-%m-%d %H:%M:%S.%06f"; /* Time format string */ @@ -60,7 +61,7 @@ capri caPriority = DEFAULT_CA_PRIORITY; /* CA Priority */ -void sprint_long (char *ret, long val) +void sprint_long (char *ret, long val, IntFormatT outType) { long i, bit, skip=-1L; /* used only for printing bits */ switch (outType) { @@ -108,6 +109,7 @@ char *val2str (const void *v, unsigned type, int index) char ch; void *val_ptr; unsigned base_type; + dbr_long_t val_long; if (!dbr_type_is_valid(type)) { strcpy (str, "*** invalid type"); @@ -126,20 +128,36 @@ char *val2str (const void *v, unsigned type, int index) epicsStrnEscapedFromRaw(str, STR, ((dbr_string_t*) val_ptr)[index], strlen(((dbr_string_t*) val_ptr)[index])); break; case DBR_FLOAT: - sprintf(str, dblFormatStr, ((dbr_float_t*) val_ptr)[index]); + if (outTypeF == dec) { + sprintf(str, dblFormatStr, ((dbr_float_t*) val_ptr)[index]); + } else { + if (((dbr_float_t*) val_ptr)[index] > 0.0) + val_long = ((dbr_float_t*) val_ptr)[index] + 0.5; + else + val_long = ((dbr_float_t*) val_ptr)[index] - 0.5; + sprint_long(str, val_long, outTypeF); + } break; case DBR_DOUBLE: - sprintf(str, dblFormatStr, ((dbr_double_t*) val_ptr)[index]); + if (outTypeF == dec) { + sprintf(str, dblFormatStr, ((dbr_double_t*) val_ptr)[index]); + } else { + if (((dbr_double_t*) val_ptr)[index] > 0.0) + val_long = ((dbr_double_t*) val_ptr)[index] + 0.5; + else + val_long = ((dbr_double_t*) val_ptr)[index] - 0.5; + sprint_long(str, val_long, outTypeF); + } break; case DBR_CHAR: ch = ((dbr_char_t*) val_ptr)[index]; sprintf(str, "%d", ch); break; case DBR_INT: - sprint_long(str, ((dbr_int_t*) val_ptr)[index]); + sprint_long(str, ((dbr_int_t*) val_ptr)[index], outTypeI); break; case DBR_LONG: - sprint_long(str, ((dbr_long_t*) val_ptr)[index]); + sprint_long(str, ((dbr_long_t*) val_ptr)[index], outTypeI); break; case DBR_ENUM: { diff --git a/src/catools/tool_lib.h b/src/catools/tool_lib.h index 39185ef61..5bb6b5ddd 100644 --- a/src/catools/tool_lib.h +++ b/src/catools/tool_lib.h @@ -78,7 +78,8 @@ typedef struct extern TimeT tsType; /* Timestamp type flag (-t option) */ extern int tsSrcServer; /* Timestamp source flag (-t option) */ extern int tsSrcClient; /* Timestamp source flag (-t option) */ -extern IntFormatT outType; /* Flag used for -0.. output format option */ +extern IntFormatT outTypeI; /* Flag used for -0.. output format option */ +extern IntFormatT outTypeF; /* Flag used for -l.. output format option */ extern int enumAsNr; /* Used for -n option (get DBF_ENUM as number) */ extern int charArrAsStr; /* used for -S option - treat char array as (long) string */ extern double caTimeout; /* Wait time default (see -w option) */