171 lines
4.4 KiB
C
171 lines
4.4 KiB
C
/* cat.c - command to show the contents of a file */
|
||
/*
|
||
* VAS_EBOOT -- GRand Unified Bootloader
|
||
* Copyright (C) 2003,2005,2007,2008 Free Software Foundation, Inc.
|
||
*
|
||
* VAS_EBOOT is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* VAS_EBOOT is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU General Public License
|
||
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include <VasEBoot/dl.h>
|
||
#include <VasEBoot/file.h>
|
||
#include <VasEBoot/disk.h>
|
||
#include <VasEBoot/term.h>
|
||
#include <VasEBoot/misc.h>
|
||
#include <VasEBoot/extcmd.h>
|
||
#include <VasEBoot/i18n.h>
|
||
#include <VasEBoot/charset.h>
|
||
|
||
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
||
|
||
static const struct VasEBoot_arg_option options[] =
|
||
{
|
||
{"dos", -1, 0, N_("Accept DOS-style CR/NL line endings."), 0, 0},
|
||
{0, 0, 0, 0, 0, 0}
|
||
};
|
||
|
||
static VasEBoot_err_t
|
||
VasEBoot_cmd_cat (VasEBoot_extcmd_context_t ctxt, int argc, char **args)
|
||
{
|
||
struct VasEBoot_arg_list *state = ctxt->state;
|
||
int dos = 0;
|
||
VasEBoot_file_t file;
|
||
unsigned char buf[VAS_EBOOT_DISK_SECTOR_SIZE];
|
||
VasEBoot_ssize_t size;
|
||
int key = VAS_EBOOT_TERM_NO_KEY;
|
||
VasEBoot_uint32_t code = 0;
|
||
int count = 0;
|
||
unsigned char utbuf[VAS_EBOOT_MAX_UTF8_PER_CODEPOINT + 1];
|
||
int utcount = 0;
|
||
int is_0d = 0;
|
||
int j;
|
||
|
||
if (state[0].set)
|
||
dos = 1;
|
||
|
||
if (argc != 1)
|
||
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, N_("filename expected"));
|
||
|
||
file = VasEBoot_file_open (args[0], VAS_EBOOT_FILE_TYPE_CAT);
|
||
if (! file)
|
||
return VasEBoot_errno;
|
||
|
||
while ((size = VasEBoot_file_read (file, buf, sizeof (buf))) > 0
|
||
&& key != VAS_EBOOT_TERM_ESC)
|
||
{
|
||
int i;
|
||
|
||
for (i = 0; i < size; i++)
|
||
{
|
||
utbuf[utcount++] = buf[i];
|
||
|
||
if (is_0d && buf[i] != '\n')
|
||
{
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_HIGHLIGHT);
|
||
VasEBoot_printf ("<%x>", (int) '\r');
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_STANDARD);
|
||
}
|
||
|
||
is_0d = 0;
|
||
|
||
if (!VasEBoot_utf8_process (buf[i], &code, &count))
|
||
{
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_HIGHLIGHT);
|
||
for (j = 0; j < utcount - 1; j++)
|
||
VasEBoot_printf ("<%x>", (unsigned int) utbuf[j]);
|
||
code = 0;
|
||
count = 0;
|
||
if (utcount == 1 || !VasEBoot_utf8_process (buf[i], &code, &count))
|
||
{
|
||
VasEBoot_printf ("<%x>", (unsigned int) buf[i]);
|
||
code = 0;
|
||
count = 0;
|
||
utcount = 0;
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_STANDARD);
|
||
continue;
|
||
}
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_STANDARD);
|
||
utcount = 1;
|
||
}
|
||
if (count)
|
||
continue;
|
||
|
||
if ((code >= 0xa1 || VasEBoot_isprint (code)
|
||
|| VasEBoot_isspace (code)) && code != '\r')
|
||
{
|
||
VasEBoot_printf ("%C", code);
|
||
count = 0;
|
||
code = 0;
|
||
utcount = 0;
|
||
continue;
|
||
}
|
||
|
||
if (dos && code == '\r')
|
||
{
|
||
is_0d = 1;
|
||
count = 0;
|
||
code = 0;
|
||
utcount = 0;
|
||
continue;
|
||
}
|
||
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_HIGHLIGHT);
|
||
for (j = 0; j < utcount; j++)
|
||
VasEBoot_printf ("<%x>", (unsigned int) utbuf[j]);
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_STANDARD);
|
||
count = 0;
|
||
code = 0;
|
||
utcount = 0;
|
||
}
|
||
|
||
do
|
||
key = VasEBoot_getkey_noblock ();
|
||
while (key != VAS_EBOOT_TERM_ESC && key != VAS_EBOOT_TERM_NO_KEY);
|
||
}
|
||
|
||
if (is_0d)
|
||
{
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_HIGHLIGHT);
|
||
VasEBoot_printf ("<%x>", (unsigned int) '\r');
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_STANDARD);
|
||
}
|
||
|
||
if (utcount)
|
||
{
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_HIGHLIGHT);
|
||
for (j = 0; j < utcount; j++)
|
||
VasEBoot_printf ("<%x>", (unsigned int) utbuf[j]);
|
||
VasEBoot_setcolorstate (VAS_EBOOT_TERM_COLOR_STANDARD);
|
||
}
|
||
|
||
VasEBoot_xputs ("\n");
|
||
VasEBoot_refresh ();
|
||
VasEBoot_file_close (file);
|
||
|
||
return 0;
|
||
}
|
||
|
||
static VasEBoot_extcmd_t cmd;
|
||
|
||
VAS_EBOOT_MOD_INIT(cat)
|
||
{
|
||
cmd = VasEBoot_register_extcmd ("cat", VasEBoot_cmd_cat, 0,
|
||
N_("FILE"), N_("Show the contents of a file."),
|
||
options);
|
||
}
|
||
|
||
VAS_EBOOT_MOD_FINI(cat)
|
||
{
|
||
VasEBoot_unregister_extcmd (cmd);
|
||
}
|