echo.c 2.86 KB
Newer Older
1 2 3
/* echo.c - Command to display a line of text  */
/*
 *  GRUB  --  GRand Unified Bootloader
4
 *  Copyright (C) 2006,2007,2010  Free Software Foundation, Inc.
5
 *
6
 *  GRUB is free software: you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation, either version 3 of the License, or
9 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
17
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
18 19 20 21
 */

#include <grub/dl.h>
#include <grub/misc.h>
22
#include <grub/extcmd.h>
23
#include <grub/i18n.h>
Robert Millan's avatar
Robert Millan committed
24
#include <grub/term.h>
25

26 27
GRUB_MOD_LICENSE ("GPLv3+");

28 29
static const struct grub_arg_option options[] =
  {
30 31
    {0, 'n', 0, N_("Do not output the trailing newline."), 0, 0},
    {0, 'e', 0, N_("Enable interpretation of backslash escapes."), 0, 0},
32 33 34 35
    {0, 0, 0, 0, 0, 0}
  };

static grub_err_t
36
grub_cmd_echo (grub_extcmd_context_t ctxt, int argc, char **args)
37
{
38
  struct grub_arg_list *state = ctxt->state;
39 40 41 42 43 44 45 46 47 48
  int newline = 1;
  int i;

  /* Check if `-n' was used.  */
  if (state[0].set)
    newline = 0;

  for (i = 0; i < argc; i++)
    {
      char *arg = *args;
49 50 51
      /* Unescaping results in a string no longer than the original.  */
      char *unescaped = grub_malloc (grub_strlen (arg) + 1);
      char *p = unescaped;
52 53
      args++;

54 55 56
      if (!unescaped)
	return grub_errno;

57 58 59 60 61 62 63 64 65 66 67 68
      while (*arg)
	{
	  /* In case `-e' is used, parse backslashes.  */
	  if (*arg == '\\' && state[1].set)
	    {
	      arg++;
	      if (*arg == '\0')
		break;

	      switch (*arg)
		{
		case '\\':
69
		  *p++ = '\\';
70 71 72
		  break;

		case 'a':
73
		  *p++ = '\a';
74 75 76 77 78 79 80
		  break;

		case 'c':
		  newline = 0;
		  break;

		case 'f':
81
		  *p++ = '\f';
82 83 84
		  break;

		case 'n':
85
		  *p++ = '\n';
86 87 88
		  break;

		case 'r':
89
		  *p++ = '\r';
90 91 92
		  break;

		case 't':
93
		  *p++ = '\t';
94 95 96
		  break;

		case 'v':
97
		  *p++ = '\v';
98 99 100 101 102
		  break;
		}
	      arg++;
	      continue;
	    }
103

104 105
	  /* This was not an escaped character, or escaping is not
	     enabled.  */
106
	  *p++ = *arg;
107 108 109
	  arg++;
	}

110 111 112 113
      *p = '\0';
      grub_xputs (unescaped);
      grub_free (unescaped);

114 115 116 117 118 119 120 121
      /* If another argument follows, insert a space.  */
      if (i != argc - 1)
	grub_printf (" " );
    }

  if (newline)
    grub_printf ("\n");

122 123
  grub_refresh ();  

124 125 126
  return 0;
}

127
static grub_extcmd_t cmd;
128 129 130

GRUB_MOD_INIT(echo)
{
131 132 133
  cmd = grub_register_extcmd ("echo", grub_cmd_echo,
			      GRUB_COMMAND_ACCEPT_DASH
			      | GRUB_COMMAND_OPTIONS_AT_START,
134
			      N_("[-e|-n] STRING"), N_("Display a line of text."),
135
			      options);
136 137 138 139
}

GRUB_MOD_FINI(echo)
{
140
  grub_unregister_extcmd (cmd);
141
}