Skip to contentSkip to conversation

hexcat + grub_ncurses_getwh

PATCH Vladimir Serbinenko 1 participant 1 message Aug 8, 2005

lore
Share & tools
lore
#0 lore raw

I wrote a new patch: grub_ncurses_getwh + hexcat. hexcat shows the contents of file like hex editors.

Vladimir

2005-08-08 Vladimir Serbinenko <[email protected]>
* commands/hexcat.c: new file
* conf/i386-pc.rmk (hexcat): new module
(grub_emu): hexcat added
* conf/powerpc-ieee1275.rmk: Likewise
* normal/cmdline.c: include grub/machine/console.h (grub_cmdline_get): support for special keys in grub-emu
* normal/menu.c: include grub/machine/console.h (run_menu): support for special keys in grub-emu
* util/console.c (grub_ncurses_getwh): New function
* util/grub-emu.c: call to grub_hexcat_init() and grub_hexcat_fini()

grub2_3/commands/hexcat.c renamed from grub2_2/commands/hexcat.c +111 -0
diff -urpN ./grub2_2/commands/hexcat.c ./grub2_3/commands/hexcat.c
@@ -0,0 +1,111 @@
+/* hexcat.c - command to show the contents of a file in hexadecimal  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2003,2005  Free Software Foundation, Inc.
+ *  Vladimir Serbinenko <[email protected]>
+ *
+ *  GRUB 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 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program 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 GRUB; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <grub/normal.h>
+#include <grub/dl.h>
+#include <grub/arg.h>
+#include <grub/file.h>
+#include <grub/disk.h>
+#include <grub/term.h>
+#include <grub/misc.h>
+
+static grub_err_t
+grub_cmd_cat (struct grub_arg_list *state __attribute__ ((unused)),
+	      int argc, char **args)
+
+{
+  grub_file_t file;
+  char buf[16];
+  unsigned int offset = 0;
+  grub_ssize_t size;
+
+  if (argc != 1)
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, "file name required");
+
+  file = grub_file_open (args[0]);
+  if (! file)
+    return 0;
+  
+  while ((size = grub_file_read (file, buf, sizeof (buf))) > 0)
+    {
+      int i;
+
+      grub_printf ("%08x: ", offset);
+
+      for (i = 0; i < size; i++)
+	{
+	  unsigned int c = buf[i];
+	  
+	  grub_printf ("%02x ", c & 0xFF);
+	}
+
+      for (; i < 16; i++)
+	grub_printf ("   ");
+      
+      for (i = 0; i < size; i++)
+	{
+	  unsigned char c = buf[i];
+	  
+	  if (grub_isprint (c) || c == ' ')
+	    grub_putchar (c);
+	  else
+	    grub_putchar ('.');
+	}
+      
+      grub_putchar ('\n');
+
+      offset += 16;
+    }
+
+  grub_putchar ('\n');
+  grub_refresh ();
+  grub_file_close (file);
+  
+  return 0;
+}
+
+
+#ifdef GRUB_UTIL
+void
+grub_hexcat_init (void)
+{
+  grub_register_command ("hexcat", grub_cmd_cat, GRUB_COMMAND_FLAG_BOTH,
+			 "hexcat FILE", "Show the contents of a file in hexadecimal.", 0);
+}
+
+void
+grub_hexcat_fini (void)
+{
+  grub_unregister_command ("hexcat");
+}
+#else /* ! GRUB_UTIL */
+GRUB_MOD_INIT
+{
+  (void) mod;			/* To stop warning. */
+  grub_register_command ("hexcat", grub_cmd_cat, GRUB_COMMAND_FLAG_BOTH,
+			 "hexcat FILE", "Show the contents of a file ins hexadecimal.", 0);
+}
+
+GRUB_MOD_FINI
+{
+  grub_unregister_command ("hexcat");
+}
+#endif /* ! GRUB_UTIL */
grub2_3/conf/i386-pc.mk renamed from grub2_2/conf/i386-pc.mk +67 -9

107 lines not on this page · raw message

grub2_3/conf/i386-pc.rmk renamed from grub2_2/conf/i386-pc.rmk +10 -6
diff -urpN ./grub2_2/conf/i386-pc.rmk ./grub2_3/conf/i386-pc.rmk
@@ -75,11 +75,11 @@ grub_probefs_SOURCES = util/i386/pc/grub
 	fs/hfs.c fs/jfs.c kern/fs.c kern/env.c fs/fshelp.c
 
 # For grub-emu.
-grub_emu_SOURCES = commands/boot.c commands/cat.c commands/cmp.c 	\
-	commands/configfile.c commands/default.c commands/help.c	\
-	commands/terminal.c commands/ls.c commands/timeout.c		\
-	commands/i386/pc/halt.c commands/i386/pc/reboot.c		\
-	disk/loopback.c							\
+grub_emu_SOURCES = commands/boot.c commands/cat.c commands/hexcat.c  	\
+	commands/cmp.c commands/configfile.c commands/default.c 	\
+	commands/help.c	commands/terminal.c commands/ls.c		\
+	commands/timeout.c commands/i386/pc/halt.c 			\
+	commands/i386/pc/reboot.c disk/loopback.c			\
 	fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c fs/jfs.c	\
 	fs/minix.c fs/ufs.c						\
 	kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c 	\
@@ -110,7 +110,7 @@ pkgdata_MODULES = _chain.mod _linux.mod 
 	terminal.mod fshelp.mod chain.mod multiboot.mod amiga.mod	\
 	apple.mod pc.mod sun.mod loopback.mod reboot.mod halt.mod	\
 	help.mod default.mod timeout.mod configfile.mod sendkey.mod     \
-	activate.mod parttype.mod
+	activate.mod parttype.mod hexcat.mod
 
 # For _chain.mod.
 _chain_mod_SOURCES = loader/i386/pc/chainloader.c
@@ -203,6 +203,10 @@ cmp_mod_CFLAGS = $(COMMON_CFLAGS)
 cat_mod_SOURCES = commands/cat.c
 cat_mod_CFLAGS = $(COMMON_CFLAGS)
 
+# For hexcat.mod.
+hexcat_mod_SOURCES = commands/hexcat.c
+hexcat_mod_CFLAGS = $(COMMON_CFLAGS)
+
 # For help.mod.
 help_mod_SOURCES = commands/help.c
 help_mod_CFLAGS = $(COMMON_CFLAGS)
grub2_3/conf/powerpc-ieee1275.mk renamed from grub2_2/conf/powerpc-ieee1275.mk +63 -5

99 lines not on this page · raw message

grub2_3/conf/powerpc-ieee1275.rmk renamed from grub2_2/conf/powerpc-ieee1275.rmk +6 -2
diff -urpN ./grub2_2/conf/powerpc-ieee1275.rmk ./grub2_3/conf/powerpc-ieee1275.rmk
@@ -37,7 +37,7 @@ grub_emu_SOURCES = commands/boot.c comma
 	commands/configfile.c commands/default.c commands/help.c	\
 	commands/terminal.c commands/ls.c commands/timeout.c		\
 	commands/ieee1275/halt.c commands/ieee1275/reboot.c		\
-	disk/loopback.c							\
+	disk/loopback.c	commands/hexcat.c				\
 	fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c fs/jfs.c	\
 	fs/minix.c fs/ufs.c						\
 	kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c 	\
@@ -73,7 +73,7 @@ pkgdata_MODULES = _linux.mod linux.mod f
 	hfs.mod jfs.mod normal.mod hello.mod font.mod ls.mod \
 	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod amiga.mod apple.mod \
 	pc.mod suspend.mod loopback.mod help.mod reboot.mod halt.mod sun.mod \
-	default.mod timeout.mod configfile.mod
+	default.mod timeout.mod configfile.mod hexcat.mod
 
 # For fshelp.mod.
 fshelp_mod_SOURCES = fs/fshelp.c
@@ -147,6 +147,10 @@ cmp_mod_CFLAGS = $(COMMON_CFLAGS)
 cat_mod_SOURCES = commands/cat.c
 cat_mod_CFLAGS = $(COMMON_CFLAGS)
 
+# For hexcat.mod.
+hexcat_mod_SOURCES = commands/hexcat.c
+hexcat_mod_CFLAGS = $(COMMON_CFLAGS)
+
 # For font.mod.
 font_mod_SOURCES = font/manager.c
 font_mod_CFLAGS = $(COMMON_CFLAGS)
grub2_3/normal/cmdline.c renamed from grub2_2/normal/cmdline.c +32 -2
diff -urpN ./grub2_2/normal/cmdline.c ./grub2_3/normal/cmdline.c
@@ -27,6 +27,7 @@
 #include <grub/disk.h>
 #include <grub/file.h>
 #include <grub/env.h>
+#include <grub/machine/console.h>
 
 static char *kill_buf;
 
@@ -474,7 +475,7 @@ grub_cmdline_get (const char *prompt, ch
   grub_size_t lpos, llen;
   grub_size_t plen;
   char buf[max_len];
-  int key;
+  int key, key0;
   int histpos = 0;
   auto void cl_insert (const char *str);
   auto void cl_delete (unsigned len);
@@ -564,18 +565,20 @@ grub_cmdline_get (const char *prompt, ch
   if (hist_used == 0)
     grub_history_add (buf);
 
-  while ((key = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key != '\r')
+  while ((key = GRUB_TERM_ASCII_CHAR (key0 = grub_getkey ())) != '\n' && key != '\r')
     {
       if (readline)
 	{
 	  switch (key)
 	    {
 	    case 1:	/* Ctrl-a */
+	    key_home:
 	      lpos = 0;
 	      cl_set_pos ();
 	      break;
 
 	    case 2:	/* Ctrl-b */
+	    key_left:
 	      if (lpos > 0)
 		{
 		  lpos--;
@@ -584,11 +587,13 @@ grub_cmdline_get (const char *prompt, ch
 	      break;
 
 	    case 5:	/* Ctrl-e */
+	    key_end:
 	      lpos = llen;
 	      cl_set_pos ();
 	      break;
 
 	    case 6:	/* Ctrl-f */
+	    key_right:
 	      if (lpos < llen)
 		{
 		  lpos++;
@@ -641,6 +646,7 @@ grub_cmdline_get (const char *prompt, ch
 	      break;
 
 	    case 14:	/* Ctrl-n */
+	    key_down:
 	      {
 		char *hist;
 
@@ -658,7 +664,9 @@ grub_cmdline_get (const char *prompt, ch
 
 		break;
 	      }
+
 	    case 16:	/* Ctrl-p */
+	    key_up:
 	      {
 		char *hist;
 
@@ -703,6 +711,28 @@ grub_cmdline_get (const char *prompt, ch
 	      if (kill_buf)
 		cl_insert (kill_buf);
 	      break;
+	      
+	    default:
+	      switch (key0)
+		{
+		case GRUB_CONSOLE_KEY_HOME:
+		  goto key_home;
+
+		case GRUB_CONSOLE_KEY_END:
+		  goto key_end;
+
+		case GRUB_CONSOLE_KEY_LEFT:
+		  goto key_left;
+
+		case GRUB_CONSOLE_KEY_RIGHT:
+		  goto key_right;
+
+		case GRUB_CONSOLE_KEY_UP:
+		  goto key_up;
+
+		case GRUB_CONSOLE_KEY_DOWN:
+		  goto key_down;
+		}
 	    }
 	}
 
grub2_3/normal/menu.c renamed from grub2_2/normal/menu.c +16 -2
diff -urpN ./grub2_2/normal/menu.c ./grub2_3/normal/menu.c
@@ -23,6 +23,7 @@
 #include <grub/loader.h>
 #include <grub/mm.h>
 #include <grub/machine/time.h>
+#include <grub/machine/console.h>
 
 static void
 draw_border (void)
@@ -195,7 +196,7 @@ run_menu (grub_menu_t menu, int nested)
 
   while (1)
     {
-      int c;
+      int c, c0;
 
       if (menu->timeout > 0)
 	{
@@ -226,7 +227,7 @@ run_menu (grub_menu_t menu, int nested)
       
       if (grub_checkkey () >= 0 || menu->timeout < 0)
 	{
-	  c = GRUB_TERM_ASCII_CHAR (grub_getkey ());
+	  c = GRUB_TERM_ASCII_CHAR (c0 = grub_getkey ());
 	  
 	  if (menu->timeout >= 0)
 	    {
@@ -242,6 +243,7 @@ run_menu (grub_menu_t menu, int nested)
 	    {
 	    case 16:
 	    case '^':
+	    key_up:
 	      if (offset > 0)
 		{
 		  print_entry (GRUB_TERM_FIRST_ENTRY_Y + offset, 0,
@@ -259,6 +261,7 @@ run_menu (grub_menu_t menu, int nested)
 	      
 	    case 14:
 	    case 'v':
+	    key_down:
 	      if (menu->size > first + offset + 1)
 		{
 		  if (offset < GRUB_TERM_NUM_ENTRIES - 1)
@@ -300,6 +303,17 @@ run_menu (grub_menu_t menu, int nested)
 	      goto refresh;
 	      
 	    default:
+
+	      switch (c0)
+		{
+		case GRUB_CONSOLE_KEY_DOWN:
+		  goto key_down;
+
+		case GRUB_CONSOLE_KEY_UP:
+		  goto key_up;
+
+		}
+
 	      break;
 	    }
 	  
grub2_3/util/console.c renamed from grub2_2/util/console.c +7 -0
diff -urpN ./grub2_2/util/console.c ./grub2_3/util/console.c
@@ -258,6 +258,12 @@ grub_ncurses_init (void)
   return 0;
 }
 
+static grub_uint16_t
+grub_ncurses_getwh (void)
+{
+  return (getmaxx (stdscr) << 8) | getmaxy (stdscr);
+}
+
 static grub_err_t
 grub_ncurses_fini (void)
 {
@@ -274,6 +280,7 @@ static struct grub_term grub_ncurses_ter
     .putchar = grub_ncurses_putchar,
     .checkkey = grub_ncurses_checkkey,
     .getkey = grub_ncurses_getkey,
+    .getwh = grub_ncurses_getwh,
     .getxy = grub_ncurses_getxy,
     .gotoxy = grub_ncurses_gotoxy,
     .cls = grub_ncurses_cls,
grub2_3/util/grub-emu.c renamed from grub2_2/util/grub-emu.c +2 -0
diff -urpN ./grub2_2/util/grub-emu.c ./grub2_3/util/grub-emu.c
@@ -186,6 +186,7 @@ main (int argc, char *argv[])
   grub_boot_init ();
   grub_cmp_init ();
   grub_cat_init ();
+  grub_hexcat_init ();
   grub_terminal_init ();
   grub_loop_init ();
   grub_help_init ();
@@ -219,6 +220,7 @@ main (int argc, char *argv[])
   grub_fat_fini ();
   grub_boot_fini ();
   grub_cmp_fini ();
+  grub_hexcat_fini ();
   grub_cat_fini ();
   grub_terminal_fini ();
   grub_amiga_partition_map_fini ();