2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / commands / echo.c
blob69aa3be3c1424a5242f745bac31656c4e906224d
1 /* echo.c - Command to display a line of text */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
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 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/dl.h>
21 #include <grub/misc.h>
22 #include <grub/extcmd.h>
24 static const struct grub_arg_option options[] =
26 {0, 'n', 0, "do not output the trailing newline", 0, 0},
27 {0, 'e', 0, "enable interpretation of backslash escapes", 0, 0},
28 {0, 0, 0, 0, 0, 0}
31 static grub_err_t
32 grub_cmd_echo (grub_extcmd_t cmd, int argc, char **args)
34 struct grub_arg_list *state = cmd->state;
35 int newline = 1;
36 int i;
38 /* Check if `-n' was used. */
39 if (state[0].set)
40 newline = 0;
42 for (i = 0; i < argc; i++)
44 char *arg = *args;
45 args++;
47 while (*arg)
49 /* In case `-e' is used, parse backslashes. */
50 if (*arg == '\\' && state[1].set)
52 arg++;
53 if (*arg == '\0')
54 break;
56 switch (*arg)
58 case '\\':
59 grub_printf ("\\");
60 break;
62 case 'a':
63 grub_printf ("\a");
64 break;
66 case 'c':
67 newline = 0;
68 break;
70 case 'f':
71 grub_printf ("\f");
72 break;
74 case 'n':
75 grub_printf ("\n");
76 break;
78 case 'r':
79 grub_printf ("\r");
80 break;
82 case 't':
83 grub_printf ("\t");
84 break;
86 case 'v':
87 grub_printf ("\v");
88 break;
90 arg++;
91 continue;
94 /* This was not an escaped character, or escaping is not
95 enabled. */
96 grub_printf ("%c", *arg);
97 arg++;
100 /* If another argument follows, insert a space. */
101 if (i != argc - 1)
102 grub_printf (" " );
105 if (newline)
106 grub_printf ("\n");
108 return 0;
111 static grub_extcmd_t cmd;
113 GRUB_MOD_INIT(echo)
115 cmd = grub_register_extcmd ("echo", grub_cmd_echo, GRUB_COMMAND_FLAG_BOTH,
116 "echo [-e|-n] STRING", "Display a line of text.",
117 options);
120 GRUB_MOD_FINI(echo)
122 grub_unregister_extcmd (cmd);