2N/A/* echo.c - Command to display a line of text */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2006,2007,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/dl.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/i18n.h>
2N/A#include <grub/term.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const struct grub_arg_option options[] =
2N/A {
2N/A {0, 'n', 0, N_("Do not output the trailing newline."), 0, 0},
2N/A {0, 'e', 0, N_("Enable interpretation of backslash escapes."), 0, 0},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_echo (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A int newline = 1;
2N/A int i;
2N/A
2N/A /* Check if `-n' was used. */
2N/A if (state[0].set)
2N/A newline = 0;
2N/A
2N/A for (i = 0; i < argc; i++)
2N/A {
2N/A char *arg = *args;
2N/A /* Unescaping results in a string no longer than the original. */
2N/A char *unescaped = grub_malloc (grub_strlen (arg) + 1);
2N/A char *p = unescaped;
2N/A args++;
2N/A
2N/A if (!unescaped)
2N/A return grub_errno;
2N/A
2N/A while (*arg)
2N/A {
2N/A /* In case `-e' is used, parse backslashes. */
2N/A if (*arg == '\\' && state[1].set)
2N/A {
2N/A arg++;
2N/A if (*arg == '\0')
2N/A break;
2N/A
2N/A switch (*arg)
2N/A {
2N/A case '\\':
2N/A *p++ = '\\';
2N/A break;
2N/A
2N/A case 'a':
2N/A *p++ = '\a';
2N/A break;
2N/A
2N/A case 'c':
2N/A newline = 0;
2N/A break;
2N/A
2N/A case 'f':
2N/A *p++ = '\f';
2N/A break;
2N/A
2N/A case 'n':
2N/A *p++ = '\n';
2N/A break;
2N/A
2N/A case 'r':
2N/A *p++ = '\r';
2N/A break;
2N/A
2N/A case 't':
2N/A *p++ = '\t';
2N/A break;
2N/A
2N/A case 'v':
2N/A *p++ = '\v';
2N/A break;
2N/A }
2N/A arg++;
2N/A continue;
2N/A }
2N/A
2N/A /* This was not an escaped character, or escaping is not
2N/A enabled. */
2N/A *p++ = *arg;
2N/A arg++;
2N/A }
2N/A
2N/A *p = '\0';
2N/A grub_xputs (unescaped);
2N/A grub_free (unescaped);
2N/A
2N/A /* If another argument follows, insert a space. */
2N/A if (i != argc - 1)
2N/A grub_printf (" " );
2N/A }
2N/A
2N/A if (newline)
2N/A grub_printf ("\n");
2N/A
2N/A grub_refresh ();
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT(echo)
2N/A{
2N/A cmd = grub_register_extcmd ("echo", grub_cmd_echo,
2N/A GRUB_COMMAND_ACCEPT_DASH
2N/A | GRUB_COMMAND_OPTIONS_AT_START,
2N/A N_("[-e|-n] STRING"), N_("Display a line of text."),
2N/A options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(echo)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}