[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] cmd: set: Introduce array-cutoff parameter
From: |
Carlo Caione |
Subject: |
[PATCH] cmd: set: Introduce array-cutoff parameter |
Date: |
Thu, 13 Feb 2020 12:24:55 +0100 |
Displaying large arrays is cumbersome, this patch introduces a new set
command (.set array-cutoff N) setting a maximum allowed number of
elements that are displayed when printing the array as part of a struct.
Signed-off-by: Carlo Caione <address@hidden>
* doc/poke.texi: add documentation
* src/pk-set.c: new set array-cutoff command
* testsuite/poke.cmd/set-array-cutoff.pk: new test file
* src/pvm.c: helpers to retrieve array-cutoff flag
* src/pvm.h: likewise
* src/pvm.jitter: new flag in the PVM state struct
* src/pvm-val.c: support new array cutoff parameter
---
doc/poke.texi | 6 ++++++
src/pk-set.c | 26 ++++++++++++++++++++++++++
src/pvm-val.c | 12 +++++++++++-
src/pvm.c | 14 ++++++++++++++
src/pvm.h | 3 +++
src/pvm.jitter | 2 ++
testsuite/poke.cmd/set-array-cutoff.pk | 16 ++++++++++++++++
7 files changed, 78 insertions(+), 1 deletion(-)
create mode 100644 testsuite/poke.cmd/set-array-cutoff.pk
diff --git a/doc/poke.texi b/doc/poke.texi
index ab4ae8d4..407debc6 100644
--- a/doc/poke.texi
+++ b/doc/poke.texi
@@ -694,6 +694,12 @@ up to the @code{depth}-th level. The default value
@code{0} means no limit.
@cindex indent, as number of spaces
Number defining the number of spaces used for indentation for each level. Only
values >=1 and <= 10 are valid. Default value is '2'.
+@item array-cutoff
+@cindex array cutoff
+When displaying an array as struct field, display only the elements up to the
+@code{cutoff} index and display @code{...} after that. Value of @code{0}
+means no limit. This cutoff value is not used when directly displaying arrays
+content.
@end table
@node vm command
diff --git a/src/pk-set.c b/src/pk-set.c
index f1df36bd..e5cf4be2 100644
--- a/src/pk-set.c
+++ b/src/pk-set.c
@@ -230,6 +230,28 @@ pk_cmd_set_pretty_print (int argc, struct pk_cmd_arg
argv[], uint64_t uflags)
return 1;
}
+static int
+pk_cmd_set_array_cutoff (int argc, struct pk_cmd_arg argv[], uint64_t uflags)
+{
+ /* set array-cutoff [CUTOFF] */
+
+ assert(argc == 1);
+
+ int cutoff = PK_CMD_ARG_INT (argv[0]);
+
+ if (cutoff < 0)
+ {
+ pk_term_class ("error");
+ pk_puts ("error: ");
+ pk_term_end_class ("error");
+ pk_puts (_(" cutoff should be >=0.\n"));
+ return 0;
+ }
+
+ pvm_set_array_cutoff (poke_vm, cutoff);
+ return 1;
+}
+
static int
pk_cmd_set_odepth (int argc, struct pk_cmd_arg argv[], uint64_t uflags)
{
@@ -369,6 +391,9 @@ pk_cmd_set_error_on_warning (int argc, struct pk_cmd_arg
argv[],
extern struct pk_cmd null_cmd; /* pk-cmd.c */
+struct pk_cmd set_array_cutoff_cmd =
+ {"array-cutoff", "i", "", 0, NULL, pk_cmd_set_array_cutoff, "set
array-cutoff [CUTOFF]"};
+
struct pk_cmd set_oindent_cmd =
{"oindent", "i", "", 0, NULL, pk_cmd_set_oindent, "set oindent [INDENT]"};
@@ -397,6 +422,7 @@ struct pk_cmd set_error_on_warning_cmd =
struct pk_cmd *set_cmds[] =
{
+ &set_array_cutoff_cmd,
&set_obase_cmd,
&set_omode_cmd,
&set_odepth_cmd,
diff --git a/src/pvm-val.c b/src/pvm-val.c
index 1d727102..e35f52d9 100644
--- a/src/pvm-val.c
+++ b/src/pvm-val.c
@@ -30,6 +30,8 @@
#define STREQ(a, b) (strcmp (a, b) == 0)
+unsigned int pk_odepth;
+
pvm_val
pvm_make_int (int32_t value, int size)
{
@@ -794,6 +796,15 @@ pvm_print_val (pvm_val val, int base, int flags)
if (idx != 0)
pk_puts (",");
+
+ if ((pvm_array_cutoff (poke_vm) != 0) &&
+ ((pvm_array_cutoff (poke_vm) <= idx) &&
+ (pk_odepth != 0)))
+ {
+ pk_puts("...");
+ break;
+ }
+
pvm_print_val (elem_value, base, flags);
if (flags & PVM_PRINT_F_MAPS && elem_offset != PVM_NULL)
@@ -818,7 +829,6 @@ pvm_print_val (pvm_val val, int base, int flags)
pvm_val struct_type = PVM_VAL_SCT_TYPE (val);
pvm_val struct_type_name = PVM_VAL_TYP_S_NAME (struct_type);
pvm_val pretty_printer = pvm_get_struct_method (val, "_print");
- static unsigned int pk_odepth;
/* If the struct has a pretty printing method (called _print)
then use it, unless the PVM is configured to not do so.
diff --git a/src/pvm.c b/src/pvm.c
index bb041f15..6b31a768 100644
--- a/src/pvm.c
+++ b/src/pvm.c
@@ -45,6 +45,8 @@
((PVM)->pvm_state.pvm_state_runtime.odepth)
#define PVM_STATE_OINDENT(PVM) \
((PVM)->pvm_state.pvm_state_runtime.oindent)
+#define PVM_STATE_ARRAY_CUTOFF(PVM) \
+ ((PVM)->pvm_state.pvm_state_runtime.array_cutoff)
struct pvm
{
@@ -210,6 +212,18 @@ pvm_set_odepth (pvm apvm, unsigned int odepth)
PVM_STATE_ODEPTH (apvm) = odepth;
}
+unsigned int
+pvm_array_cutoff (pvm apvm)
+{
+ return PVM_STATE_ARRAY_CUTOFF (apvm);
+}
+
+void
+pvm_set_array_cutoff (pvm apvm, unsigned int cutoff)
+{
+ PVM_STATE_ARRAY_CUTOFF (apvm) = cutoff;
+}
+
void
pvm_assert (int expression)
{
diff --git a/src/pvm.h b/src/pvm.h
index f0df8ff9..e1cf780e 100644
--- a/src/pvm.h
+++ b/src/pvm.h
@@ -111,6 +111,9 @@ void pvm_set_oindent (pvm apvm, unsigned int oindent);
unsigned int pvm_odepth (pvm apvm);
void pvm_set_odepth (pvm apvm, unsigned int odepth);
+unsigned int pvm_array_cutoff (pvm apvm);
+void pvm_set_array_cutoff (pvm apvm, unsigned int cutoff);
+
/* Set the current negative encoding for PVM. NENC should be one of
* the IOS_NENC_* values defined in ios.h */
diff --git a/src/pvm.jitter b/src/pvm.jitter
index 96c48d01..4cdb829d 100644
--- a/src/pvm.jitter
+++ b/src/pvm.jitter
@@ -570,6 +570,7 @@ state-struct-runtime-c
enum pvm_omode omode;
uint32_t odepth;
uint32_t oindent;
+ uint32_t array_cutoff;
end
end
@@ -585,6 +586,7 @@ state-initialization-c
jitter_state_runtime->omode = PVM_PRINT_FLAT;
jitter_state_runtime->odepth = 0;
jitter_state_runtime->oindent = 2;
+ jitter_state_runtime->array_cutoff = 0;
end
end
diff --git a/testsuite/poke.cmd/set-array-cutoff.pk
b/testsuite/poke.cmd/set-array-cutoff.pk
new file mode 100644
index 00000000..3ea33a90
--- /dev/null
+++ b/testsuite/poke.cmd/set-array-cutoff.pk
@@ -0,0 +1,16 @@
+/* { dg-do run } */
+/* { dg-data {c*} {0x10 0x20 0x30 0x40 0x50 0x60 0x70 0x80} } */
+
+/* { dg-command { .set obase 16 } } */
+
+/* { dg-command { deftype Foo = struct { byte[4] b; } } } */
+/* { dg-command { Foo @ 0#B } } */
+/* { dg-output "Foo \\{b=\\\[0x10UB,0x20UB,0x30UB,0x40UB\\\]\\}" } */
+
+/* { dg-command { .set array-cutoff 1 } } */
+/* { dg-command { Foo @ 0#B } } */
+/* { dg-output "\nFoo \\{b=\\\[0x10UB,...\\\]\\}" } */
+
+/* { dg-command { defvar bar = Foo @ 0#B } } */
+/* { dg-command { bar.b } } */
+/* { dg-output "\n\\\[0x10UB,0x20UB,0x30UB,0x40UB\\\]" } */
--
2.20.1
- [PATCH] cmd: set: Introduce array-cutoff parameter,
Carlo Caione <=