mirror of
https://github.com/eclipse/wakaama.git
synced 2025-05-08 23:31:37 +08:00
examples: use getline() to read commands
The previous approach works only if the terminal is operating in cannonical mode (input available line-by-line), which is not always true. getline() ensures that commands are always read correctly.
This commit is contained in:
parent
d45b4e3085
commit
bf81d0827b
@ -1407,15 +1407,18 @@ int main(int argc, char *argv[])
|
||||
*/
|
||||
else if (FD_ISSET(STDIN_FILENO, &readfds))
|
||||
{
|
||||
numBytes = read(STDIN_FILENO, buffer, MAX_PACKET_SIZE - 1);
|
||||
char *line = NULL;
|
||||
size_t bufLen = 0;
|
||||
|
||||
numBytes = getline(&line, &bufLen, stdin);
|
||||
|
||||
if (numBytes > 1)
|
||||
{
|
||||
buffer[numBytes] = 0;
|
||||
line[numBytes] = 0;
|
||||
/*
|
||||
* We call the corresponding callback of the typed command passing it the buffer for further arguments
|
||||
*/
|
||||
handle_command(lwm2mH, commands, (char*)buffer);
|
||||
handle_command(lwm2mH, commands, line);
|
||||
}
|
||||
if (g_quit == 0)
|
||||
{
|
||||
@ -1426,6 +1429,8 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
fprintf(stdout, "\r\n");
|
||||
}
|
||||
|
||||
lwm2m_free(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1245,12 +1245,15 @@ int main(int argc, char *argv[])
|
||||
}
|
||||
else if (FD_ISSET(STDIN_FILENO, &readfds))
|
||||
{
|
||||
numBytes = read(STDIN_FILENO, buffer, MAX_PACKET_SIZE - 1);
|
||||
char *line = NULL;
|
||||
size_t bufLen = 0;
|
||||
|
||||
numBytes = getline(&line, &bufLen, stdin);
|
||||
|
||||
if (numBytes > 1)
|
||||
{
|
||||
buffer[numBytes] = 0;
|
||||
handle_command(lwm2mH, commands, (char*)buffer);
|
||||
line[numBytes] = 0;
|
||||
handle_command(lwm2mH, commands, line);
|
||||
fprintf(stdout, "\r\n");
|
||||
}
|
||||
if (g_quit == 0)
|
||||
@ -1262,6 +1265,8 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
fprintf(stdout, "\r\n");
|
||||
}
|
||||
|
||||
lwm2m_free(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user