echo.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.
  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. static const struct grub_arg_option options[] =
  24. {
  25. {0, 'n', 0, N_("Do not output the trailing newline."), 0, 0},
  26. {0, 'e', 0, N_("Enable interpretation of backslash escapes."), 0, 0},
  27. {0, 0, 0, 0, 0, 0}
  28. };
  29. static grub_err_t
  30. grub_cmd_echo (grub_extcmd_t cmd, int argc, char **args)
  31. {
  32. struct grub_arg_list *state = cmd->state;
  33. int newline = 1;
  34. int i;
  35. /* Check if `-n' was used. */
  36. if (state[0].set)
  37. newline = 0;
  38. for (i = 0; i < argc; i++)
  39. {
  40. char *arg = *args;
  41. args++;
  42. while (*arg)
  43. {
  44. /* In case `-e' is used, parse backslashes. */
  45. if (*arg == '\\' && state[1].set)
  46. {
  47. arg++;
  48. if (*arg == '\0')
  49. break;
  50. switch (*arg)
  51. {
  52. case '\\':
  53. grub_printf ("\\");
  54. break;
  55. case 'a':
  56. grub_printf ("\a");
  57. break;
  58. case 'c':
  59. newline = 0;
  60. break;
  61. case 'f':
  62. grub_printf ("\f");
  63. break;
  64. case 'n':
  65. grub_printf ("\n");
  66. break;
  67. case 'r':
  68. grub_printf ("\r");
  69. break;
  70. case 't':
  71. grub_printf ("\t");
  72. break;
  73. case 'v':
  74. grub_printf ("\v");
  75. break;
  76. }
  77. arg++;
  78. continue;
  79. }
  80. /* This was not an escaped character, or escaping is not
  81. enabled. */
  82. grub_printf ("%c", *arg);
  83. arg++;
  84. }
  85. /* If another argument follows, insert a space. */
  86. if (i != argc - 1)
  87. grub_printf (" " );
  88. }
  89. if (newline)
  90. grub_printf ("\n");
  91. return 0;
  92. }
  93. static grub_extcmd_t cmd;
  94. GRUB_MOD_INIT(echo)
  95. {
  96. cmd = grub_register_extcmd ("echo", grub_cmd_echo, GRUB_COMMAND_FLAG_BOTH,
  97. N_("[-e|-n] STRING"), N_("Display a line of text."),
  98. options);
  99. }
  100. GRUB_MOD_FINI(echo)
  101. {
  102. grub_unregister_extcmd (cmd);
  103. }