2N/A/* cat.c - command to show the contents of a file */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003,2005,2007,2008 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/file.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/term.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/extcmd.h>
2N/A#include <grub/i18n.h>
2N/A
2N/AGRUB_MOD_LICENSE ("GPLv3+");
2N/A
2N/Astatic const struct grub_arg_option options[] =
2N/A {
2N/A {"dos", -1, 0, N_("Accept DOS-style CR/NL line endings."), 0, 0},
2N/A {0, 0, 0, 0, 0, 0}
2N/A };
2N/A
2N/Astatic grub_err_t
2N/Agrub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)
2N/A{
2N/A struct grub_arg_list *state = ctxt->state;
2N/A int dos = 0;
2N/A grub_file_t file;
2N/A char buf[GRUB_DISK_SECTOR_SIZE];
2N/A grub_ssize_t size;
2N/A int key = 0;
2N/A
2N/A if (state[0].set)
2N/A dos = 1;
2N/A
2N/A if (argc != 1)
2N/A return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
2N/A
2N/A file = grub_file_open (args[0]);
2N/A if (! file)
2N/A return grub_errno;
2N/A
2N/A while ((size = grub_file_read (file, buf, sizeof (buf))) > 0
2N/A && key != GRUB_TERM_ESC)
2N/A {
2N/A int i;
2N/A
2N/A for (i = 0; i < size; i++)
2N/A {
2N/A unsigned char c = buf[i];
2N/A
2N/A if ((grub_isprint (c) || grub_isspace (c)) && c != '\r')
2N/A grub_printf ("%c", c);
2N/A else if (dos && c == '\r' && i + 1 < size && buf[i + 1] == '\n')
2N/A {
2N/A grub_printf ("\n");
2N/A i++;
2N/A }
2N/A else
2N/A {
2N/A grub_setcolorstate (GRUB_TERM_COLOR_HIGHLIGHT);
2N/A grub_printf ("<%x>", (int) c);
2N/A grub_setcolorstate (GRUB_TERM_COLOR_STANDARD);
2N/A }
2N/A }
2N/A
2N/A while (grub_checkkey () >= 0 &&
2N/A (key = grub_getkey ()) != GRUB_TERM_ESC)
2N/A ;
2N/A }
2N/A
2N/A grub_xputs ("\n");
2N/A grub_refresh ();
2N/A grub_file_close (file);
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_extcmd_t cmd;
2N/A
2N/AGRUB_MOD_INIT(cat)
2N/A{
2N/A cmd = grub_register_extcmd ("cat", grub_cmd_cat, 0,
2N/A N_("FILE"), N_("Show the contents of a file."),
2N/A options);
2N/A}
2N/A
2N/AGRUB_MOD_FINI(cat)
2N/A{
2N/A grub_unregister_extcmd (cmd);
2N/A}