echo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* echo.c - Command to display a line of text */
  2. /*
  3. * 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. * (at your option) any later version.
  10. *
  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.
  15. *
  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/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/extcmd.h>
  22. #include <grub/i18n.h>
  23. #include <grub/term.h>
  24. GRUB_MOD_LICENSE ("GPLv3+");
  25. static const struct grub_arg_option options[] =
  26. {
  27. {0, 'n', 0, N_("Do not output the trailing newline."), 0, 0},
  28. {0, 'e', 0, N_("Enable interpretation of backslash escapes."), 0, 0},
  29. {0, 0, 0, 0, 0, 0}
  30. };
  31. static grub_err_t
  32. grub_cmd_echo (grub_extcmd_context_t ctxt, int argc, char **args)
  33. {
  34. struct grub_arg_list *state = ctxt->state;
  35. int newline = 1;
  36. int i;
  37. /* Check if `-n' was used. */
  38. if (state[0].set)
  39. newline = 0;
  40. for (i = 0; i < argc; i++)
  41. {
  42. char *arg = *args;
  43. /* Unescaping results in a string no longer than the original. */
  44. char *unescaped = grub_malloc (grub_strlen (arg) + 1);
  45. char *p = unescaped;
  46. args++;
  47. if (!unescaped)
  48. return grub_errno;
  49. while (*arg)
  50. {
  51. /* In case `-e' is used, parse backslashes. */
  52. if (*arg == '\\' && state[1].set)
  53. {
  54. arg++;
  55. if (*arg == '\0')
  56. break;
  57. switch (*arg)
  58. {
  59. case '\\':
  60. *p++ = '\\';
  61. break;
  62. case 'a':
  63. *p++ = '\a';
  64. break;
  65. case 'c':
  66. newline = 0;
  67. break;
  68. case 'f':
  69. *p++ = '\f';
  70. break;
  71. case 'n':
  72. *p++ = '\n';
  73. break;
  74. case 'r':
  75. *p++ = '\r';
  76. break;
  77. case 't':
  78. *p++ = '\t';
  79. break;
  80. case 'v':
  81. *p++ = '\v';
  82. break;
  83. }
  84. arg++;
  85. continue;
  86. }
  87. /* This was not an escaped character, or escaping is not
  88. enabled. */
  89. *p++ = *arg;
  90. arg++;
  91. }
  92. *p = '\0';
  93. grub_xputs (unescaped);
  94. grub_free (unescaped);
  95. /* If another argument follows, insert a space. */
  96. if (i != argc - 1)
  97. grub_printf (" " );
  98. }
  99. if (newline)
  100. grub_printf ("\n");
  101. grub_refresh ();
  102. return 0;
  103. }
  104. static grub_extcmd_t cmd;
  105. GRUB_MOD_INIT(echo)
  106. {
  107. cmd = grub_register_extcmd ("echo", grub_cmd_echo,
  108. GRUB_COMMAND_ACCEPT_DASH
  109. | GRUB_COMMAND_OPTIONS_AT_START,
  110. N_("[-e|-n] STRING"), N_("Display a line of text."),
  111. options);
  112. }
  113. GRUB_MOD_FINI(echo)
  114. {
  115. grub_unregister_extcmd (cmd);
  116. }