cat.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* cat.c - command to show the contents of a file */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2005,2007,2008 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/file.h>
  21. #include <grub/disk.h>
  22. #include <grub/term.h>
  23. #include <grub/misc.h>
  24. #include <grub/extcmd.h>
  25. #include <grub/i18n.h>
  26. #include <grub/charset.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. static const struct grub_arg_option options[] =
  29. {
  30. {"dos", -1, 0, N_("Accept DOS-style CR/NL line endings."), 0, 0},
  31. {0, 0, 0, 0, 0, 0}
  32. };
  33. static grub_err_t
  34. grub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)
  35. {
  36. struct grub_arg_list *state = ctxt->state;
  37. int dos = 0;
  38. grub_file_t file;
  39. unsigned char buf[GRUB_DISK_SECTOR_SIZE];
  40. grub_ssize_t size;
  41. int key = GRUB_TERM_NO_KEY;
  42. grub_uint32_t code = 0;
  43. int count = 0;
  44. unsigned char utbuf[GRUB_MAX_UTF8_PER_CODEPOINT + 1];
  45. int utcount = 0;
  46. int is_0d = 0;
  47. int j;
  48. if (state[0].set)
  49. dos = 1;
  50. if (argc != 1)
  51. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  52. file = grub_file_open (args[0]);
  53. if (! file)
  54. return grub_errno;
  55. while ((size = grub_file_read (file, buf, sizeof (buf))) > 0
  56. && key != GRUB_TERM_ESC)
  57. {
  58. int i;
  59. for (i = 0; i < size; i++)
  60. {
  61. utbuf[utcount++] = buf[i];
  62. if (is_0d && buf[i] != '\n')
  63. {
  64. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  65. grub_printf ("<%x>", (int) '\r');
  66. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  67. }
  68. is_0d = 0;
  69. if (!grub_utf8_process (buf[i], &code, &count))
  70. {
  71. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  72. for (j = 0; j < utcount - 1; j++)
  73. grub_printf ("<%x>", (unsigned int) utbuf[j]);
  74. code = 0;
  75. count = 0;
  76. if (utcount == 1 || !grub_utf8_process (buf[i], &code, &count))
  77. {
  78. grub_printf ("<%x>", (unsigned int) buf[i]);
  79. code = 0;
  80. count = 0;
  81. utcount = 0;
  82. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  83. continue;
  84. }
  85. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  86. utcount = 1;
  87. }
  88. if (count)
  89. continue;
  90. if ((code >= 0xa1 || grub_isprint (code)
  91. || grub_isspace (code)) && code != '\r')
  92. {
  93. grub_printf ("%C", code);
  94. count = 0;
  95. code = 0;
  96. utcount = 0;
  97. continue;
  98. }
  99. if (dos && code == '\r')
  100. {
  101. is_0d = 1;
  102. count = 0;
  103. code = 0;
  104. utcount = 0;
  105. continue;
  106. }
  107. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  108. for (j = 0; j < utcount; j++)
  109. grub_printf ("<%x>", (unsigned int) utbuf[j]);
  110. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  111. count = 0;
  112. code = 0;
  113. utcount = 0;
  114. }
  115. do
  116. key = grub_getkey_noblock ();
  117. while (key != GRUB_TERM_ESC && key != GRUB_TERM_NO_KEY);
  118. }
  119. if (is_0d)
  120. {
  121. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  122. grub_printf ("<%x>", (unsigned int) '\r');
  123. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  124. }
  125. if (utcount)
  126. {
  127. grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
  128. for (j = 0; j < utcount; j++)
  129. grub_printf ("<%x>", (unsigned int) utbuf[j]);
  130. grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
  131. }
  132. grub_xputs ("\n");
  133. grub_refresh ();
  134. grub_file_close (file);
  135. return 0;
  136. }
  137. static grub_extcmd_t cmd;
  138. GRUB_MOD_INIT(cat)
  139. {
  140. cmd = grub_register_extcmd ("cat", grub_cmd_cat, 0,
  141. N_("FILE"), N_("Show the contents of a file."),
  142. options);
  143. }
  144. GRUB_MOD_FINI(cat)
  145. {
  146. grub_unregister_extcmd (cmd);
  147. }