YARP
Yet Another Robot Platform
Terminal.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
8
9#include <yarp/os/Bottle.h>
10#include <yarp/os/Port.h>
11#include <yarp/os/Vocab.h>
14
15#include <cstdio>
16#include <cstring>
17
18#ifdef YARP_HAS_Libedit
19# include <editline/readline.h>
20char* szLine = (char*)nullptr;
21bool readlineEOF = false;
22#endif // YARP_HAS_Libedit
23
25{
26#ifdef YARP_HAS_Libedit
27 if (yarp::os::impl::isatty(yarp::os::impl::fileno(stdin)) != 0) {
28 return readlineEOF;
29 }
30#endif // YARP_HAS_Libedit
31 return feof(stdin) != 0;
32}
33
35{
36 std::string txt;
37
38#ifdef YARP_HAS_Libedit
39 if (yarp::os::impl::isatty(yarp::os::impl::fileno(stdin)) != 0) {
40 if (szLine != nullptr) {
41 free(szLine);
42 szLine = (char*)nullptr;
43 }
44
45 szLine = readline(">>");
46 if ((szLine != nullptr) && (*szLine != 0)) {
47 txt = szLine;
48 add_history(szLine);
49 } else if (szLine == nullptr) {
50 readlineEOF = true;
51 }
52 return txt;
53 }
54#endif // YARP_HAS_Libedit
55
56 bool done = false;
57 char buf[2048];
58 while (!done) {
59 char* result = fgets(buf, sizeof(buf), stdin);
60 if (result != nullptr) {
61 for (unsigned int i = 0; i < strlen(buf); i++) {
62 if (buf[i] == '\n') {
63 buf[i] = '\0';
64 done = true;
65 break;
66 }
67 }
68 txt += buf;
69 } else {
70 done = true;
71 }
72 }
73 return txt;
74}
75
77{
78 bool end = false;
79
80 std::string txt;
81 if (!EOFreached()) {
82 txt = getStdin();
83 }
84
85 if (EOFreached() || (!txt.empty() && txt[0] < 32 && txt[0] != '\n' && txt[0] != '\r')) {
86 end = true;
87 }
88
89 if (end) {
90 txt = "";
91 }
92
93 if (eof != nullptr) {
94 *eof = end;
95 }
96 return txt;
97}
std::string readString(bool *eof)
Definition: Terminal.cpp:76
std::string getStdin()
Definition: Terminal.cpp:34