@@ -108,7 +108,22 @@ static int kdb_handle_escape(char *buf, size_t sz)
108108 return -1 ;
109109}
110110
111- static int kdb_read_get_key (char * buffer , size_t bufsize )
111+ /**
112+ * kdb_getchar() - Read a single character from a kdb console (or consoles).
113+ *
114+ * Other than polling the various consoles that are currently enabled,
115+ * most of the work done in this function is dealing with escape sequences.
116+ *
117+ * An escape key could be the start of a vt100 control sequence such as \e[D
118+ * (left arrow) or it could be a character in its own right. The standard
119+ * method for detecting the difference is to wait for 2 seconds to see if there
120+ * are any other characters. kdb is complicated by the lack of a timer service
121+ * (interrupts are off), by multiple input sources. Escape sequence processing
122+ * has to be done as states in the polling loop.
123+ *
124+ * Return: The key pressed or a control code derived from an escape sequence.
125+ */
126+ char kdb_getchar (void )
112127{
113128#define ESCAPE_UDELAY 1000
114129#define ESCAPE_DELAY (2*1000000/ESCAPE_UDELAY) /* 2 seconds worth of udelays */
@@ -126,7 +141,6 @@ static int kdb_read_get_key(char *buffer, size_t bufsize)
126141 }
127142
128143 key = (* f )();
129-
130144 if (key == -1 ) {
131145 if (escape_delay ) {
132146 udelay (ESCAPE_UDELAY );
@@ -136,14 +150,6 @@ static int kdb_read_get_key(char *buffer, size_t bufsize)
136150 continue ;
137151 }
138152
139- if (bufsize <= 2 ) {
140- if (key == '\r' )
141- key = '\n' ;
142- * buffer ++ = key ;
143- * buffer = '\0' ;
144- return -1 ;
145- }
146-
147153 if (escape_delay == 0 && key == '\e' ) {
148154 escape_delay = ESCAPE_DELAY ;
149155 ped = escape_data ;
@@ -184,17 +190,7 @@ static int kdb_read_get_key(char *buffer, size_t bufsize)
184190 * function. It is not reentrant - it relies on the fact
185191 * that while kdb is running on only one "master debug" cpu.
186192 * Remarks:
187- *
188- * The buffer size must be >= 2. A buffer size of 2 means that the caller only
189- * wants a single key.
190- *
191- * An escape key could be the start of a vt100 control sequence such as \e[D
192- * (left arrow) or it could be a character in its own right. The standard
193- * method for detecting the difference is to wait for 2 seconds to see if there
194- * are any other characters. kdb is complicated by the lack of a timer service
195- * (interrupts are off), by multiple input sources and by the need to sometimes
196- * return after just one key. Escape sequence processing has to be done as
197- * states in the polling loop.
193+ * The buffer size must be >= 2.
198194 */
199195
200196static char * kdb_read (char * buffer , size_t bufsize )
@@ -229,9 +225,7 @@ static char *kdb_read(char *buffer, size_t bufsize)
229225 * cp = '\0' ;
230226 kdb_printf ("%s" , buffer );
231227poll_again :
232- key = kdb_read_get_key (buffer , bufsize );
233- if (key == -1 )
234- return buffer ;
228+ key = kdb_getchar ();
235229 if (key != 9 )
236230 tab = 0 ;
237231 switch (key ) {
@@ -742,7 +736,7 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
742736
743737 /* check for having reached the LINES number of printed lines */
744738 if (kdb_nextline >= linecount ) {
745- char buf1 [ 16 ] = "" ;
739+ char ch ;
746740
747741 /* Watch out for recursion here. Any routine that calls
748742 * kdb_printf will come back through here. And kdb_read
@@ -777,39 +771,38 @@ int vkdb_printf(enum kdb_msgsrc src, const char *fmt, va_list ap)
777771 if (logging )
778772 printk ("%s" , moreprompt );
779773
780- kdb_read (buf1 , 2 ); /* '2' indicates to return
781- * immediately after getting one key. */
774+ ch = kdb_getchar ();
782775 kdb_nextline = 1 ; /* Really set output line 1 */
783776
784777 /* empty and reset the buffer: */
785778 kdb_buffer [0 ] = '\0' ;
786779 next_avail = kdb_buffer ;
787780 size_avail = sizeof (kdb_buffer );
788- if ((buf1 [ 0 ] == 'q' ) || (buf1 [ 0 ] == 'Q' )) {
781+ if ((ch == 'q' ) || (ch == 'Q' )) {
789782 /* user hit q or Q */
790783 KDB_FLAG_SET (CMD_INTERRUPT ); /* command interrupted */
791784 KDB_STATE_CLEAR (PAGER );
792785 /* end of command output; back to normal mode */
793786 kdb_grepping_flag = 0 ;
794787 kdb_printf ("\n" );
795- } else if (buf1 [ 0 ] == ' ' ) {
788+ } else if (ch == ' ' ) {
796789 kdb_printf ("\r" );
797790 suspend_grep = 1 ; /* for this recursion */
798- } else if (buf1 [ 0 ] == '\n' ) {
791+ } else if (ch == '\n' || ch == '\r ' ) {
799792 kdb_nextline = linecount - 1 ;
800793 kdb_printf ("\r" );
801794 suspend_grep = 1 ; /* for this recursion */
802- } else if (buf1 [ 0 ] == '/' && !kdb_grepping_flag ) {
795+ } else if (ch == '/' && !kdb_grepping_flag ) {
803796 kdb_printf ("\r" );
804797 kdb_getstr (kdb_grep_string , KDB_GREP_STRLEN ,
805798 kdbgetenv ("SEARCHPROMPT" ) ?: "search> " );
806799 * strchrnul (kdb_grep_string , '\n' ) = '\0' ;
807800 kdb_grepping_flag += KDB_GREPPING_FLAG_SEARCH ;
808801 suspend_grep = 1 ; /* for this recursion */
809- } else if (buf1 [ 0 ] && buf1 [ 0 ] != '\n' ) {
810- /* user hit something other than enter */
802+ } else if (ch ) {
803+ /* user hit something unexpected */
811804 suspend_grep = 1 ; /* for this recursion */
812- if (buf1 [ 0 ] != '/' )
805+ if (ch != '/' )
813806 kdb_printf (
814807 "\nOnly 'q', 'Q' or '/' are processed at "
815808 "more prompt, input ignored\n" );
0 commit comments