Re: [RFC] Let "gcore" command accept a suffix argument

Hui Zhu
2009-12-12T08:33:57Z

[-- Attachment #1: Type: text/plain, Size: 377 bytes --]

Hi guys,

I change it to:
eval echo "++$a"

Thanks,
Hui

On Sat, Dec 12, 2009 at 02:09, Tom Tromey <tromey@redhat.com> wrote:
>>>>>> "Joel" == Joel Brobecker <brobecker@adacore.com> writes:
>
> Joel> I don't think it seems weird. I find this pretty nice, and since
> Joel> we already have printf, I wonder how hard it would be to implement
> Joel> this?
>
> Easy :-)
>
> Tom
>

[-- Attachment #2: eval.txt --]
[-- Type: text/plain, Size: 2680 bytes --]

---
printcmd.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 87 insertions(+)

--- a/printcmd.c
+++ b/printcmd.c
@@ -50,6 +50,8 @@
#include "parser-defs.h"
#include "charset.h"

+#include <ctype.h>
+
#ifdef TUI
#include "tui/tui.h" /* For tui_active et.al. */
#endif
@@ -2625,6 +2627,88 @@ printf_command (char *arg, int from_tty)
do_cleanups (old_cleanups);
}

+static void
+eval_command (char *exp, int from_tty)
+{
+#define CMDSIZE 1024
+ char *cmd;
+ char *cmdp;
+ int is_eval = 0;
+ char *eval_begin;
+
+ if (!exp)
+ return;
+
+ cmd = xmalloc (CMDSIZE + 1);
+ make_cleanup (xfree, cmd);
+ cmdp = cmd;
+
+ while (cmdp - cmd < CMDSIZE)
+ {
+ if (is_eval)
+ {
+ if (!exp[0] || exp[0]== '"')
+ {
+ struct value *value;
+ gdb_byte *buffer;
+ int length = -1;
+ struct type *char_type = NULL;
+ const char *la_encoding = NULL;
+ char tmp = exp[0];
+
+ exp[0] = '\0';
+ value = parse_and_eval (eval_begin);
+
+ switch (TYPE_CODE (value_type (value)))
+ {
+ case TYPE_CODE_ARRAY:
+ LA_GET_STRING (value, &buffer, &length,
+ &char_type, &la_encoding);
+ break;
+ case TYPE_CODE_INT:
+ buffer = plongest (value_as_long (value));
+ length = strlen (buffer);
+ break;
+ default:
+ buffer = eval_begin;
+ length = exp - eval_begin;
+ break;
+ }
+
+ if (length > CMDSIZE - (cmdp - cmd))
+ length = CMDSIZE - (cmdp - cmd);
+ memcpy (cmdp, buffer, length);
+ cmdp += length;
+
+ exp[0] = tmp;
+ is_eval = 0;
+ }
+
+ exp ++;
+ }
+ else
+ {
+ if (!exp[0])
+ break;
+
+ if (exp[0] == '"')
+ {
+ is_eval = 1;
+ eval_begin = exp + 1;
+ }
+ else
+ {
+ cmdp[0] = exp[0];
+ cmdp ++;
+ }
+ exp ++;
+ }
+ }
+ cmdp[0] = '\0';
+
+ execute_command (cmd, from_tty);
+}
+
void
_initialize_printcmd (void)
{
@@ -2788,4 +2872,7 @@ Show printing of source filename and lin
NULL,
show_print_symbol_filename,
&setprintlist, &showprintlist);
+
+ add_com ("eval", no_class, eval_command, _("\
+Call command with variable."));
}