@@ -366,13 +366,72 @@ int main( int argc, char ** argv )
366
366
367
367
CaptureKeyboardInput ();
368
368
369
+ #if TERMINAL_INPUT_BUFFER
370
+ char pline_buf [256 ]; // Buffer that contains current line that is being printed to
371
+ char input_buf [128 ]; // Buffer that contains user input until it is sent out
372
+ memset ( pline_buf , 0 , sizeof ( pline_buf ) );
373
+ memset ( input_buf , 0 , sizeof ( input_buf ) );
374
+ uint8_t input_pos = 0 ;
375
+ uint8_t to_send = 0 ;
376
+ #endif
377
+ printf ( "Terminal started\n\n" );
369
378
uint32_t appendword = 0 ;
370
379
do
371
380
{
372
381
uint8_t buffer [256 ];
382
+ #if TERMINAL_INPUT_BUFFER
383
+ char print_buf [300 ]; // Buffer that is filled with everything and will be written to stdout (basically it's for formatting)
384
+ uint8_t update = 0 ;
385
+ #endif
373
386
if ( !IsGDBServerInShadowHaltState ( dev ) )
374
387
{
375
388
// Handle keyboard input.
389
+ #if TERMINAL_INPUT_BUFFER
390
+ if ( IsKBHit () && to_send == 0 )
391
+ {
392
+ uint8_t c = ReadKBByte ();
393
+ if ( c == 8 || c == 127 )
394
+ {
395
+ input_buf [input_pos - 1 ] = 0 ;
396
+ if ( input_pos > 0 ) input_pos -- ;
397
+ }
398
+ else if ( c > 31 && c < 127 )
399
+ {
400
+ input_buf [input_pos ] = c ;
401
+ input_pos ++ ;
402
+ }
403
+ else if ( c == '\n' || c == 10 )
404
+ {
405
+ to_send = input_pos ;
406
+ }
407
+ update = 1 ;
408
+ }
409
+ // Process incomming buffer during sending
410
+ if ( to_send > 0 && appendword == 0 )
411
+ {
412
+ for ( int i = 0 ; i < 3 ; i ++ )
413
+ {
414
+ appendword |= input_buf [input_pos - to_send ] << ( i * 8 + 8 );
415
+ to_send -- ;
416
+ if ( to_send == 0 ) break ;
417
+ }
418
+ if ( to_send == 0 )
419
+ {
420
+ strcpy ( print_buf , TERMINAL_CLEAR_CUR );
421
+ strcat ( print_buf , TERMIANL_INPUT_SENT );
422
+ strcat ( print_buf , input_buf );
423
+ strcat ( print_buf , "\n" );
424
+ strcat ( print_buf , pline_buf );
425
+ strcat ( print_buf , TERMINAL_SEND_LABEL );
426
+ fwrite ( print_buf , strlen ( print_buf ), 1 , stdout );
427
+ fflush ( stdout );
428
+ input_pos = 0 ;
429
+ input_buf [0 ] = 0 ;
430
+ // memset( input_buf, 0, sizeof( input_buf ) );
431
+ }
432
+ appendword |= i + 4 ;
433
+ }
434
+ #else
376
435
if ( appendword == 0 )
377
436
{
378
437
int i ;
@@ -383,7 +442,20 @@ int main( int argc, char ** argv )
383
442
}
384
443
appendword |= i + 4 ; // Will go into DATA0.
385
444
}
445
+ #endif
446
+
386
447
int r = MCF .PollTerminal ( dev , buffer , sizeof ( buffer ), appendword , 0 );
448
+ #if TERMINAL_INPUT_BUFFER
449
+ if ( ( r == -1 || r == 0 ) && update > 0 )
450
+ {
451
+ strcpy ( print_buf , TERMINAL_CLEAR_CUR );
452
+ if ( to_send > 0 ) strcat ( print_buf , TERMINAL_DIM );
453
+ strcat ( print_buf , TERMINAL_SEND_LABEL );
454
+ strcat ( print_buf , input_buf );
455
+ fwrite ( print_buf , strlen ( print_buf ), 1 , stdout );
456
+ fflush ( stdout );
457
+ }
458
+ #endif
387
459
if ( r < -5 )
388
460
{
389
461
fprintf ( stderr , "Terminal dead. code %d\n" , r );
@@ -396,7 +468,22 @@ int main( int argc, char ** argv )
396
468
}
397
469
else if ( r > 0 )
398
470
{
471
+ #if TERMINAL_INPUT_BUFFER
472
+ uint8_t new_line = 0 ;
473
+ if ( buffer [r - 1 ] == '\n' ) new_line = 1 ;
474
+ if ( new_line == 0 ) strcpy ( print_buf , TERMINAL_CLEAR_PREV ); // Go one line up and erase it
475
+ else strcpy ( print_buf , TERMINAL_CLEAR_CUR ); // Go to the start of the line and erase it
476
+ strncat ( pline_buf , (char * )buffer , r ); // Add newely received chars to line buffer
477
+ strcat ( print_buf , pline_buf ); // Add line to buffer
478
+ if ( to_send > 0 ) strcat ( print_buf , TERMINAL_DIM );
479
+ strcat ( print_buf , TERMINAL_SEND_LABEL ); // Print styled "Send" label
480
+ strcat ( print_buf , input_buf ); // Print current input
481
+ fwrite ( print_buf , strlen ( print_buf ), 1 , stdout );
482
+ print_buf [0 ] = 0 ;
483
+ if ( new_line == 1 ) pline_buf [0 ] = 0 ;
484
+ #else
399
485
fwrite ( buffer , r , 1 , stdout );
486
+ #endif
400
487
fflush ( stdout );
401
488
// Otherwise it's basically just an ack for appendword.
402
489
appendword = 0 ;
0 commit comments