Support abort of 'cat' even if in paging mode
Hello,
we needed a way for GRUB to show a rather large text file in pager mode (set pager=1) while still allowing the user to abort without scrolling all the way to the bottom of the file.
From the original commit message:
[...] The reason is that "cat" scans for key strokes but does not block waiting for them. This results in a race between the pager that eats all the keystrokes and "cat" that scans for them in non-blocking mode.
To fix this, we let the pager report if a keypress was ESC, 'q' or 'Q'. The caller can check for this and stop the printing of data. The behaviour of the pager itself is not affected by this.
Please note that this is a rather quick fix and there might be a better solution (possibly entirely inside the term layer?). If anyone has a better solution, please let me know. Otherwise I could submit a proper patch using this fix.
Original patch by Christian Ehrhardt <[email protected]>.
Thanks
Valentin
From dd878255a7d5a071e7fb8843ce104d27f7404867 Mon Sep 17 00:00:00 2001
From: Valentin Dornauer <[email protected]>
Date: Wed, 14 Jan 2015 11:44:22 +0100
Subject: [PATCH] Support abort of cat even if in paging mode
--- grub-core/commands/cat.c | 5 ++++- grub-core/normal/term.c | 19 ++++++++++++++++++- include/grub/normal.h | 2 ++ 3 files changed, 24 insertions(+), 2 deletions(-)
grub-core/commands/cat.c +4 -1
@@ -25,6 +25,7 @@#include <grub/extcmd.h> #include <grub/i18n.h> #include <grub/charset.h> +#include <grub/normal.h> GRUB_MOD_LICENSE ("GPLv3+" );
@@ -50,6 +51,8 @@ grub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)int is_0d =0 ;int j; +grub_set_more_abort (0 ); +if (state[0 ].set) dos =1 ;
@@ -61,7 +64,7 @@ grub_cmd_cat (grub_extcmd_context_t ctxt, int argc, char **args)return grub_errno;while ((size =grub_file_read (file, buf,sizeof (buf))) >0 - && key !=GRUB_TERM_ESC ) + && key !=GRUB_TERM_ESC && !grub_get_more_abort ()) {int i;
grub-core/normal/term.c +18 -1
@@ -57,6 +57,7 @@ static struct term_state *term_states = NULL;/* If the more pager is active. */ static int grub_more; +static int grub_more_abort;static void putcode_real (grub_uint32_t code,struct grub_term_output *term,int fixed_tab);
@@ -118,7 +119,11 @@ print_more (void) state->num_lines--; }else -grub_normal_reset_more (); + { +if (key ==GRUB_TERM_ESC || key =='q' || key =='Q' ) +grub_set_more_abort (1 ); +grub_normal_reset_more (); + } }void
@@ -128,6 +133,18 @@ grub_set_more (int onoff)grub_normal_reset_more (); } +void +grub_set_more_abort (int val) +{ + grub_more_abort = !!val; +} + +int +grub_get_more_abort (void ) +{ +return !!grub_more_abort; +} +enum {GRUB_CP437_UPDOWNARROW =0x12 ,
include/grub/normal.h +2 -0
@@ -133,6 +133,8 @@ void read_crypto_list (const char *prefix);void read_terminal_list (const char *prefix);void grub_set_more (int onoff); +void grub_set_more_abort (int onoff); +int grub_get_more_abort (void );void grub_normal_reset_more (void );
Signature · 1 line
1.7.10.4
В Wed, 14 Jan 2015 11:49:57 +0100
Valentin Dornauer <[email protected]> пишет:
Quoted 11 lines
Hello,
we needed a way for GRUB to show a rather large text file in pager mode (set pager=1) while still allowing the user to abort without scrolling all the way to the bottom of the file.From the original commit message:
[...] The reason is that "cat" scans for key strokes but does not block waiting for them. This results in a race between the pager that eats all the keystrokes and "cat" that scans for them in non-blocking mode.
Quoted 9 lines
To fix this, we let the pager report if a keypress was ESC, 'q' or 'Q'. The caller can check for this and stop the printing of data. The behaviour of the pager itself is not affected by this.
Please note that this is a rather quick fix and there might be a better solution (possibly entirely inside the term layer?). If anyone has a better solution, please let me know. Otherwise I could submit a proper patch using this fix.
It is not limited to cat; just try to run lsefi on any real system (or even VM). Even simple "ls -l" could be quite time consuming. For this reasons I would suggest having common grub_output_interrupted() function that could be used in all commands doing long term output. The we could start adding it to each command.
The idea is to minimize impact on existing commands and to abstract away how we actually signal termination of output.
Quoted 4 lines
Original patch by Christian Ehrhardt <[email protected]>.
Thanks
Valentin