Beispiel #1
0
static char *
grub_getline (void)
{
  int i;
  char *line;
  char *tmp;
  char c;

  i = 0;
  line = grub_malloc (1 + i + sizeof('\0'));
  if (! line)
    return NULL;

  while (1)
    {
      c = grub_getkey ();
      if ((c == '\n') || (c == '\r'))
	break;

      line[i] = c;
      if (grub_isprint (c))
	grub_putchar (c);
      i++;
      tmp = grub_realloc (line, 1 + i + sizeof('\0'));
      if (! tmp)
	{
	  grub_free (line);
	  return NULL;
	}
      line = tmp;
    }
  line[i] = '\0';

  return line;
}
Beispiel #2
0
static grub_err_t
grub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)
{
  struct grub_arg_list *state = ctxt->state;
  int dos = 0;
  grub_file_t file;
  char buf[GRUB_DISK_SECTOR_SIZE];
  grub_ssize_t size;
  int key = 0;

  if (state[0].set)
    dos = 1;

  if (argc != 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");

  file = grub_file_open (args[0]);
  if (! file)
    return grub_errno;

  while ((size = grub_file_read (file, buf, sizeof (buf))) > 0
	 && key != GRUB_TERM_ESC)
    {
      int i;

      for (i = 0; i < size; i++)
	{
	  unsigned char c = buf[i];

	  if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
	    grub_printf ("%c", c);
	  else if (dos && c == '\r' && i + 1 < size && buf[i + 1] == '\n')
	    {
	      grub_printf ("\n");
	      i++;
	    }
	  else
	    {
	      grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
	      grub_printf ("<%x>", (int) c);
	      grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
	    }
	}

      while (grub_checkkey () >= 0 &&
	     (key = grub_getkey ()) != GRUB_TERM_ESC)
	;
    }

  grub_xputs ("\n");
  grub_refresh ();
  grub_file_close (file);

  return 0;
}
Beispiel #3
0
/* Prompt to input a command and read the line.  */
static grub_err_t
grub_rescue_read_line (char **line, int cont)
{
  int c;
  int pos = 0;
  char str[4];

  grub_printf ((cont) ? "> " : "grub rescue> ");
  grub_memset (linebuf, 0, GRUB_RESCUE_BUF_SIZE);

  while ((c = grub_getkey ()) != '\n' && c != '\r')
    {
      if (grub_isprint (c))
	{
	  if (pos < GRUB_RESCUE_BUF_SIZE - 1)
	    {
	      str[0] = c;
	      str[1] = 0;
	      linebuf[pos++] = c;
	      grub_xputs (str);
	    }
	}
      else if (c == '\b')
	{
	  if (pos > 0)
	    {
	      str[0] = c;
	      str[1] = ' ';
	      str[2] = c;
	      str[3] = 0;
	      linebuf[--pos] = 0;
	      grub_xputs (str);
	    }
	}
      grub_refresh ();
    }

  grub_xputs ("\n");
  grub_refresh ();

  *line = grub_strdup (linebuf);

  return 0;
}
Beispiel #4
0
static grub_err_t
grub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)
{
  struct grub_arg_list *state = ctxt->state;
  int dos = 0;
  grub_file_t file;
  unsigned char buf[GRUB_DISK_SECTOR_SIZE];
  grub_ssize_t size;
  int key = GRUB_TERM_NO_KEY;
  grub_uint32_t code = 0;
  int count = 0;
  unsigned char utbuf[GRUB_MAX_UTF8_PER_CODEPOINT + 1];
  int utcount = 0;
  int is_0d = 0;
  int j;

  if (state[0].set)
    dos = 1;

  if (argc != 1)
    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));

  file = grub_file_open (args[0], GRUB_FILE_TYPE_CAT);
  if (! file)
    return grub_errno;

  while ((size = grub_file_read (file, buf, sizeof (buf))) > 0
	 && key != GRUB_TERM_ESC)
    {
      int i;

      for (i = 0; i < size; i++)
	{
	  utbuf[utcount++] = buf[i];

	  if (is_0d && buf[i] != '\n')
	    {
	      grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
	      grub_printf ("<%x>", (int) '\r');
	      grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
	    }

	  is_0d = 0;

	  if (!grub_utf8_process (buf[i], &code, &count))
	    {
	      grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
	      for (j = 0; j < utcount - 1; j++)
		grub_printf ("<%x>", (unsigned int) utbuf[j]);
	      code = 0;
	      count = 0;
	      if (utcount == 1 || !grub_utf8_process (buf[i], &code, &count))
		{
		  grub_printf ("<%x>", (unsigned int) buf[i]);
		  code = 0;
		  count = 0;
		  utcount = 0;
		  grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
		  continue;
		}
	      grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
	      utcount = 1;
	    }
	  if (count)
	    continue;

	  if ((code >= 0xa1 || grub_isprint (code)
	       || grub_isspace (code)) && code != '\r')
	    {
	      grub_printf ("%C", code);
	      count = 0; 
	      code = 0;
	      utcount = 0;
	      continue;
	    }

	  if (dos && code == '\r')
	    {
	      is_0d = 1;
	      count = 0; 
	      code = 0;
	      utcount = 0;
	      continue;
	    }

	  grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
	  for (j = 0; j < utcount; j++)
	    grub_printf ("<%x>", (unsigned int) utbuf[j]);
	  grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
	  count = 0; 
	  code = 0;
	  utcount = 0;
	}

      do
	key = grub_getkey_noblock ();
      while (key != GRUB_TERM_ESC && key != GRUB_TERM_NO_KEY);
    }

  if (is_0d)
    {
      grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
      grub_printf ("<%x>", (unsigned int) '\r');
      grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
    }

  if (utcount)
    {
      grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
      for (j = 0; j < utcount; j++)
	grub_printf ("<%x>", (unsigned int) utbuf[j]);
      grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
    }

  grub_xputs ("\n");
  grub_refresh ();
  grub_file_close (file);

  return 0;
}