@@ -32,15 +32,19 @@ using chip::Shell::streamer_get;
32
32
33
33
namespace {
34
34
35
- constexpr const char kShellPrompt [] = " matterCli > " ;
35
+ constexpr const char kShellPrompt [] = " matterCli> " ;
36
+
37
+ // To track carriage returns of Windows return cases of '\r\n'
38
+ bool haveCR = false ;
36
39
37
- // max > 1
38
40
void ReadLine (char * buffer, size_t max)
39
41
{
40
42
size_t line_sz = 0 ;
43
+ size_t read = 0 ;
44
+ bool done = false ;
41
45
42
46
// Read in characters until we get a line ending or EOT.
43
- for ( bool done = false ; !done; )
47
+ while ((line_sz < max) && !done)
44
48
{
45
49
// Stop reading if we've run out of space in the buffer (still need to null-terminate).
46
50
if (line_sz >= max - 1u )
@@ -52,45 +56,72 @@ void ReadLine(char * buffer, size_t max)
52
56
#ifdef BRD4325A
53
57
// for 917 SoC board, we need to create a rx event before we wait for the shell activity
54
58
// NotifyShellProcessFromISR() is called once the buffer is filled
55
- if (streamer_read (streamer_get (), buffer + line_sz , 1 ) ! = 1 )
59
+ while (streamer_read (streamer_get (), buffer + read , 1 ) = = 1 )
56
60
{
57
- continue ;
61
+ // Count how many characters were read; usually one but could be copy/paste
62
+ read ++;
58
63
}
59
64
#endif
60
65
chip::WaitForShellActivity ();
61
66
#ifndef BRD4325A
62
67
// for EFR32 boards
63
- if (streamer_read (streamer_get (), buffer + line_sz , 1 ) ! = 1 )
68
+ while (streamer_read (streamer_get (), buffer + read , 1 ) = = 1 )
64
69
{
65
- continue ;
70
+ // Count how many characters were read; usually one but could be copy/paste
71
+ read ++;
66
72
}
67
73
#endif
68
-
69
- // Process character we just read.
70
- switch (buffer[line_sz])
74
+ // Process all characters that were read until we run out or exceed max char limit
75
+ while (line_sz < read && line_sz < max)
71
76
{
72
- case ' \r ' :
73
- case ' \n ' :
74
- streamer_printf (streamer_get (), " \r\n " );
75
- buffer[line_sz] = ' \0 ' ;
76
- line_sz++;
77
- done = true ;
78
- break ;
79
- case 0x7F :
80
- // Do not accept backspace character (i.e. don't increment line_sz) and remove 1 additional character if it exists.
81
- if (line_sz >= 1u )
77
+ switch (buffer[line_sz])
82
78
{
83
- streamer_printf (streamer_get (), " \b \b " );
84
- line_sz--;
85
- }
86
- break ;
87
- default :
88
- if (isprint (static_cast <int >(buffer[line_sz])) || buffer[line_sz] == ' \t ' )
89
- {
90
- streamer_printf (streamer_get (), " %c" , buffer[line_sz]);
79
+ case ' \r ' :
80
+ // Mac OS return case of '\r' or beginning of Windows return case '\r\n'
81
+ buffer[line_sz] = ' \0 ' ;
82
+ streamer_printf (streamer_get (), " \r\n " );
83
+ haveCR = true ;
84
+ done = true ;
91
85
line_sz++;
86
+ break ;
87
+ case ' \n ' :
88
+ // True if Windows return case of '\r\n'
89
+ if (haveCR)
90
+ {
91
+ // Do nothing - already taken care of with CR, return to loop and don't increment buffer
92
+ haveCR = false ;
93
+ read --;
94
+ }
95
+ // Linux return case of just '\n'
96
+ else
97
+ {
98
+ buffer[line_sz] = ' \0 ' ;
99
+ streamer_printf (streamer_get (), " \r\n " );
100
+ done = true ;
101
+ line_sz++;
102
+ }
103
+ break ;
104
+ case 0x7F :
105
+ // Do not accept backspace character (i.e. don't increment line_sz) and remove 1 additional character if it exists.
106
+ if (line_sz >= 1u )
107
+ {
108
+ // Delete backspace character + whatever came before it
109
+ streamer_printf (streamer_get (), " \b \b " );
110
+ line_sz--;
111
+ read --;
112
+ }
113
+ // Remove backspace character regardless
114
+ read --;
115
+
116
+ break ;
117
+ default :
118
+ if (isprint (static_cast <int >(buffer[line_sz])) || buffer[line_sz] == ' \t ' )
119
+ {
120
+ streamer_printf (streamer_get (), " %c" , buffer[line_sz]);
121
+ line_sz++;
122
+ }
123
+ break ;
92
124
}
93
- break ;
94
125
}
95
126
}
96
127
}
0 commit comments