rllib
1
Main Page
Classes
Files
File List
File Members
All
Classes
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
lib
rlcommandlineinterface.cpp
Go to the documentation of this file.
1
/***************************************************************************
2
rlcommandlineinterface.cpp - description
3
-------------------
4
begin : Sat Mar 27 2010
5
copyright : (C) 2010 by R. Lehrig
6
email : lehrig@t-online.de
7
***************************************************************************/
8
9
/***************************************************************************
10
* *
11
* This library is free software; you can redistribute it and/or modify *
12
* it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as *
13
* published by the Free Software Foundation *
14
* *
15
***************************************************************************/
16
#include "
rlcommandlineinterface.h
"
17
#include "
rlstring.h
"
18
#include "
rlcutil.h
"
19
#include "
rlthread.h
"
20
#include <unistd.h>
21
#include <stdarg.h>
22
23
rlCommandlineInterface::rlCommandlineInterface
()
24
{
25
sock
= NULL;
26
spawn
= NULL;
27
tty
= NULL;
28
}
29
30
rlCommandlineInterface::~rlCommandlineInterface
()
31
{
32
if
(
sock
!= NULL)
delete
sock
;
33
if
(
spawn
!= NULL)
delete
spawn
;
34
}
35
36
int
rlCommandlineInterface::start
(
const
char
*how,
const
char
*command)
37
{
38
if
(
sock
!= NULL)
delete
sock
;
39
sock
= NULL;
40
if
(
spawn
!= NULL)
delete
spawn
;
41
spawn
= NULL;
42
tty
= NULL;
43
44
if
(strcmp(how,
"pipe"
) == 0)
45
{
46
if
(command == NULL)
return
-1;
47
spawn
=
new
rlSpawn
();
48
int
ret =
spawn
->
spawn
(command);
49
if
(ret < 0)
50
{
51
delete
spawn
;
52
spawn
= NULL;
53
return
-1;
54
}
55
return
ret;
56
}
57
else
if
(strcmp(how,
"stdio"
) == 0)
58
{
59
return
1;
60
}
61
else
62
{
63
rlString
rlhow(how);
64
char
*cptr, *host;
65
host = rlhow.
text
();
66
cptr = strchr(host,
':'
);
67
if
(cptr == NULL)
return
-1;
68
*cptr =
'\0'
; cptr++;
69
int
port = atoi(cptr);
70
if
(strcmp(host,
"server.localhost"
) == 0)
71
{
72
sock
=
new
rlSocket
(host,port,0);
73
}
74
else
75
{
76
if
(strcmp(host,
"localhost"
) == 0 && command != NULL)
77
{
78
rlString
cmd(command);
79
#ifdef RLUNIX
80
cmd +=
" &"
;
81
#endif
82
rlsystem
(cmd.
text
());
83
}
84
sock
=
new
rlSocket
(host,port,1);
85
}
86
for
(
int
itry=0; itry<10; itry++)
87
{
88
sock
->
connect
();
89
if
(
sock
->
isConnected
())
return
sock
->
s
;
90
rlsleep
(10);
91
}
92
return
-1;
93
}
94
}
95
96
int
rlCommandlineInterface::start
(
rlSerial
*ttyinterface)
97
{
98
if
(
sock
!= NULL)
delete
sock
;
99
sock
= NULL;
100
if
(
spawn
!= NULL)
delete
spawn
;
101
spawn
= NULL;
102
tty
= ttyinterface;
103
if
(
tty
== NULL)
return
-1;
104
return
1;
105
}
106
107
int
rlCommandlineInterface::readBlock
(
void
*buf,
int
len,
int
timeout)
108
{
109
if
(
spawn
!= NULL)
110
{
111
unsigned
char
*cbuf = (
unsigned
char
*) buf;
112
int
i = 0;
113
while
(i<len)
114
{
115
if
(timeout > 0)
116
{
117
if
(
spawn
->
select
(timeout) == 0)
return
-1;
118
}
119
cbuf[i++] =
spawn
->
getchar
();
120
}
121
return
len;
122
}
123
else
if
(
sock
!= NULL)
124
{
125
if
(
sock
->
isConnected
() == 0)
return
-1;
126
int
ret =
sock
->
read
(buf,len,timeout);
127
if
(ret <= 0)
128
{
129
sock
->
disconnect
();
130
return
-1;
131
}
132
return
len;
133
}
134
else
if
(
tty
!= NULL)
135
{
136
int
timout = 0;
137
if
(timeout > 0) timout = timeout;
138
return
tty
->
readBlock
((
unsigned
char
*) buf, len, timout);
139
}
140
else
141
{
142
int
ret = read(0,buf,len);
143
return
ret;
144
}
145
}
146
147
const
char
*
rlCommandlineInterface::readLine
(
int
timeout)
148
{
149
if
(
spawn
!= NULL)
150
{
151
if
(timeout > 0)
152
{
153
if
(
spawn
->
select
(timeout) == 0)
return
NULL;
154
}
155
return
spawn
->
readLine
();
156
}
157
else
if
(
sock
!= NULL)
158
{
159
if
(
sock
->
isConnected
() == 0)
return
NULL;
160
int
ret =
sock
->
readStr
(
line
,
sizeof
(
line
)-1,timeout);
161
if
(ret <= 0)
162
{
163
sock
->
disconnect
();
164
return
NULL;
165
}
166
return
line
;
167
}
168
else
if
(
tty
!= NULL)
169
{
170
int
ret =
tty
->
readLine
((
unsigned
char
*)
line
,
sizeof
(
line
)-1, timeout);
171
if
(ret <= 0)
return
NULL;
172
return
line
;
173
}
174
else
175
{
176
return
fgets(
line
,
sizeof
(
line
)-1,stdin);
177
}
178
}
179
180
int
rlCommandlineInterface::printf
(
const
char
*format, ...)
181
{
182
va_list ap;
183
va_start(ap,format);
184
int
ret =
rlvsnprintf
(
line
,
sizeof
(
line
) - 1, format, ap);
185
va_end(ap);
186
if
(ret < 0)
return
ret;
187
188
if
(
spawn
!= NULL)
189
{
190
return
spawn
->
printf
(
"%s"
,
line
);
191
}
192
else
if
(
sock
!= NULL)
193
{
194
if
(
sock
->
isConnected
() == 0)
return
-1;
195
int
ret =
sock
->
printf
(
"%s"
,
line
);
196
return
ret;
197
}
198
else
if
(
tty
!= NULL)
199
{
200
return
tty
->
writeBlock
((
unsigned
char
*)
line
, strlen(
line
));
201
}
202
else
203
{
204
int
ret =
::printf
(
"%s"
,
line
);
205
fflush(stdout);
206
return
ret;
207
}
208
}
209
210
int
rlCommandlineInterface::writeBlock
(
void
*buf,
int
len)
211
{
212
if
(
spawn
!= NULL)
213
{
214
return
spawn
->
write
((
const
char
*) buf, len);
215
}
216
else
if
(
sock
!= NULL)
217
{
218
if
(
sock
->
isConnected
() == 0)
return
-1;
219
return
sock
->
write
(buf,len);
220
}
221
else
if
(
tty
!= NULL)
222
{
223
return
tty
->
writeBlock
((
unsigned
char
*) buf, len);
224
}
225
else
226
{
227
return
write(1,buf,len);
228
}
229
}
230
231
#ifdef TESTING
232
int
main
()
233
{
234
const
char
*line;
235
rlCommandlineInterface
cli;
236
237
// connect to a server over TCP
238
// or be a TCP server if how = "server.localhost:port"
239
printf(
"localhost:\n"
);
240
cli.
start
(
"localhost:50500"
);
241
int
i = 0;
242
while
((line = cli.
readLine
()) != NULL)
243
{
244
printf(
"line=%s"
,line);
245
if
(i<5) cli.
printf
(
"%s%d\n"
,
"hallo"
,i++);
246
else
cli.
printf
(
"%s\n"
,
"exit"
);
247
}
248
249
// run command and connect it over a pipe to us
250
printf(
"pipe:\n"
);
251
cli.
start
(
"pipe"
,
"./myecho"
);
252
i = 0;
253
while
((line = cli.
readLine
()) != NULL)
254
{
255
::printf(
"line=%s"
,line);
256
if
(i<5) cli.
printf
(
"%s%d\n"
,
"hallo"
,i++);
257
else
cli.
printf
(
"%s\n"
,
"exit"
);
258
}
259
260
// communicate via stdin / stdout
261
printf(
"stdio:\n"
);
262
cli.
start
(
"stdio"
);
263
cli.
printf
(
"Bitte tipp was ein\n"
);
264
printf(
"Du hast '%s' eingegeben\n"
, cli.
readLine
());
265
266
return
0;
267
}
268
#endif
269
Generated by
1.8.2