Beispiel #1
0
static char *
read_next_line (void)
{
  struct ui *ui = current_ui;
  char *prompt_ptr, control_prompt[256];
  int i = 0;
  int from_tty = ui->instream == ui->stdin_stream;

  if (control_level >= 254)
    error (_("Control nesting too deep!"));

  /* Set a prompt based on the nesting of the control commands.  */
  if (from_tty
      || (ui->instream == 0 && deprecated_readline_hook != NULL))
    {
      for (i = 0; i < control_level; i++)
	control_prompt[i] = ' ';
      control_prompt[i] = '>';
      control_prompt[i + 1] = '\0';
      prompt_ptr = (char *) &control_prompt[0];
    }
  else
    prompt_ptr = NULL;

  return command_line_input (prompt_ptr, from_tty, "commands");
}
Beispiel #2
0
static char *
gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
			char *prompt)
{
  int n;
  char *p = NULL, *p_start, *p_end, *q;
  volatile struct gdb_exception except;

  TRY_CATCH (except, RETURN_MASK_ALL)
    p = command_line_input (prompt, 0, "python");

  /* Detect user interrupt (Ctrl-C).  */
  if (except.reason == RETURN_QUIT)
    return NULL;

  /* Handle errors by raising Python exceptions.  */
  if (except.reason < 0)
    {
      /* The thread state is nulled during gdbpy_readline_wrapper,
	 with the original value saved in the following undocumented
	 variable (see Python's Parser/myreadline.c and
	 Modules/readline.c).  */
      PyEval_RestoreThread (_PyOS_ReadlineTState);
      gdbpy_convert_exception (except);
      PyEval_SaveThread ();
      return NULL;
    }

  /* Detect EOF (Ctrl-D).  */
  if (p == NULL)
    {
      q = PyMem_Malloc (1);
      if (q != NULL)
	q[0] = '\0';
      return q;
    }

  n = strlen (p);

  /* Copy the line to Python and return.  */
  q = PyMem_Malloc (n + 2);
  if (q != NULL)
    {
      strncpy (q, p, n);
      q[n] = '\n';
      q[n + 1] = '\0';
    }
  return q;
}
Beispiel #3
0
static char *
read_next_line (void)
{
  char *prompt_ptr, control_prompt[256];
  int i = 0;

  if (control_level >= 254)
    error (_("Control nesting too deep!"));

  /* Set a prompt based on the nesting of the control commands.  */
  if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
    {
      for (i = 0; i < control_level; i++)
	control_prompt[i] = ' ';
      control_prompt[i] = '>';
      control_prompt[i + 1] = '\0';
      prompt_ptr = (char *) &control_prompt[0];
    }
  else
    prompt_ptr = NULL;

  return command_line_input (prompt_ptr, instream == stdin, "commands");
}
Beispiel #4
0
/*
 * CLI, returns results as CMD_xxx (such as CMD_EXIT)
 * If argc is supplied, then this is one shot cli, ie run the command
 */
static int
do_cli(const struct cmd_tbl_entry *cmd_tbl, const char *prompt, int argc, char **argv)
{
	/* Build up argc/argv */
	const struct cmd_tbl_entry *ctp;
	int cmd_argc;
	char *cmd_argv[20];
	char *input = NULL;
	int rv, done;
	int i;

	char promptbuf[PROMPTBUFSIZE];	/* Was 1024, who needs that long a prompt? (the part before user input up to '>') */
	static char nullstr[2]={0,0};

#ifdef HAVE_LIBREADLINE
	//set the current command level for command completion
	current_cmd_level = cmd_tbl;
#endif

	rv = 0, done = 0;
	snprintf(promptbuf, PROMPTBUFSIZE, "%s> ", prompt);
	while (!done) {
		char *inptr, *s;

		if (argc == 0) {
			/* Get Input */
			if (input)
				free(input);
			input = command_line_input(promptbuf);
			if (!input) {
					if (instream == stdin)
						printf("\n");
					break;
			}

			/* Parse it */
			inptr = input;
			if (*inptr == '@') 	//printing comment
			{
				printf("%s\n", inptr);
				continue;
			}
			if (*inptr == '#')		//non-printing comment
			{
				continue;
			}
			cmd_argc = 0;
			while ( (s = strtok(inptr, " \t")) != NULL ) {
				cmd_argv[cmd_argc] = s;
				inptr = NULL;
				if (cmd_argc == (ARRAY_SIZE(cmd_argv)-1))
					break;
				cmd_argc++;
			}
			cmd_argv[cmd_argc] = nullstr;
		} else {
			/* Use supplied argc */
			cmd_argc = argc;
			for (i=0; i<=argc; i++)
				cmd_argv[i] = argv[i];
		}

		if (cmd_argc != 0) {
			ctp = cmd_tbl;
			while (ctp->command) {
				if (strcasecmp(ctp->command, cmd_argv[0]) == 0) {
					if (ctp->sub_cmd_tbl) {
						log_command(1, cmd_argv);
						snprintf(promptbuf, PROMPTBUFSIZE,"%s/%s",
							prompt, ctp->command);
						/* Sub menu */
						rv = do_cli(ctp->sub_cmd_tbl,
							promptbuf,
							cmd_argc-1,
							&cmd_argv[1]);
#ifdef HAVE_LIBREADLINE
						//went out of the sub-menu, so update the command level for command completion
						current_cmd_level = cmd_tbl;
#endif
						if (rv==CMD_EXIT)	//allow exiting prog. from a submenu
							done=1;
						snprintf(promptbuf, PROMPTBUFSIZE, "%s> ", prompt);
					} else {
						/* Found command */
						log_command(cmd_argc, cmd_argv);
						rv = ctp->routine(cmd_argc, cmd_argv);
						switch (rv) {
							case CMD_USAGE:
								printf("Usage: %s\n", ctp->usage);
								break;
							case CMD_EXIT:
								rv = CMD_EXIT;
								done = 1;
								break;
							case CMD_UP:
								rv = CMD_UP;
								done = 1;
								break;
						}
					}
					break;
				}
				if (!done)
					ctp++;
			}
			if (ctp->command == NULL) {
				printf("Huh? Try \"help\"\n");
			}
			if (argc) {
				/* Single command */
				done = 1;
				break;
			}
		}
		if (done)
			break;
	}	//while !done
	if (input)
			free(input);
	if (rv == CMD_UP)
		return CMD_OK;
	if (rv == CMD_EXIT) {
		char *disco="disconnect";
		if (global_logfp != NULL)
			cmd_stoplog(0, NULL);
		if (global_state > STATE_IDLE) {
			do_cli(diag_cmd_table, "", 1, &disco);	//XXX should be called recursively in case there are >1 active L3 conns...
		}
		rv=diag_end();
		if (rv)
			fprintf(stderr, FLFMT "diag_end failed !?\n", FL);
		rv = CMD_EXIT;
	}
	return rv;
}
Beispiel #5
0
   command_line_input is used instead.  */

static char *
gdbpy_readline_wrapper (FILE *sys_stdin, FILE *sys_stdout,
#if PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 4
			const char *prompt)
#else
			char *prompt)
#endif
{
  int n;
  char *p = NULL, *q;
  volatile struct gdb_exception except;

  TRY_CATCH (except, RETURN_MASK_ALL)
    p = command_line_input (prompt, 0, "python");

  /* Detect user interrupt (Ctrl-C).  */
  if (except.reason == RETURN_QUIT)
    return NULL;

  /* Handle errors by raising Python exceptions.  */
  if (except.reason < 0)
    {
      /* The thread state is nulled during gdbpy_readline_wrapper,
	 with the original value saved in the following undocumented
	 variable (see Python's Parser/myreadline.c and
	 Modules/readline.c).  */
      PyEval_RestoreThread (_PyOS_ReadlineTState);
      gdbpy_convert_exception (except);
      PyEval_SaveThread ();