Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gene
xmc4500-relax-linux
Commits
fe44d127
Commit
fe44d127
authored
Sep 09, 2016
by
Christian Eichler
Browse files
Read commands from stdin instead from argv
parent
ec0722e6
Changes
1
Hide whitespace changes
Inline
Side-by-side
xmccomm.c
View file @
fe44d127
...
...
@@ -29,7 +29,7 @@ static void print_er() {
}
static
void
print_vb
(
char
*
msg
)
{
printf
(
SHELL_ESC
"[;36;02m[vb] "
SHELL_ESC
"[m%s"
,
msg
);
f
printf
(
stderr
,
SHELL_ESC
"[;36;02m[vb] "
SHELL_ESC
"[m%s"
,
msg
);
}
static
bool
recv_response
(
FILE
*
fh
,
char
*
buf
,
size_t
buf_size
)
{
...
...
@@ -66,7 +66,7 @@ static int connect_tty(char *tty) {
unsigned
retry
=
0
;
do
{
print_vb
(
""
);
printf
(
"open('%s') try %u
\n
"
,
tty
,
retry
);
f
printf
(
stderr
,
"open('%s') try %u
\n
"
,
tty
,
retry
);
++
retry
;
struct
stat
sbuf
;
...
...
@@ -86,7 +86,7 @@ static int connect_tty(char *tty) {
}
exit
(
EXIT_FAILURE
);
}
else
{
print_vb
(
""
);
printf
(
"%s successfully opened
\n
"
,
tty
);
print_vb
(
""
);
f
printf
(
stderr
,
"%s successfully opened
\n
"
,
tty
);
}
// setup control structure
...
...
@@ -222,6 +222,7 @@ static void response_loop(FILE *fh, void (*cb)(bool, char*)) {
}
printf
(
SHELL_ESC
"[m
\n
"
);
fflush
(
stdout
);
}
}
...
...
@@ -241,8 +242,8 @@ static struct command commands[] = {
static
const
size_t
commands_count
=
sizeof
(
commands
)
/
sizeof
(
struct
command
);
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
3
>
argc
)
{
fprintf
(
stderr
,
"Usage: %s <tty>
<command>...
\n
"
,
argv
[
0
]);
if
(
2
!=
argc
)
{
fprintf
(
stderr
,
"Usage: %s <tty>
\n
"
,
argv
[
0
]);
return
EXIT_FAILURE
;
}
...
...
@@ -259,24 +260,29 @@ int main(int argc, char *argv[]) {
response_loop
(
fh
,
resp_inf
);
print_vb
(
"Sending user commands...
\n
"
);
for
(
int
i
=
2
;
i
<
argc
;
++
i
)
{
char
input
[
512
];
while
(
NULL
!=
fgets
(
input
,
sizeof
(
input
),
stdin
))
{
struct
command
*
cmd
=
NULL
;
input
[
strlen
(
input
)
-
1
]
=
'\0'
;
for
(
int
j
=
0
;
j
<
commands_count
&&
NULL
==
cmd
;
++
j
)
{
if
(
!
strncmp
(
argv
[
i
]
,
commands
[
j
].
cmd
,
strlen
(
commands
[
j
].
cmd
)))
{
if
(
!
strncmp
(
input
,
commands
[
j
].
cmd
,
strlen
(
commands
[
j
].
cmd
)))
{
cmd
=
&
commands
[
j
];
}
}
if
(
NULL
==
cmd
)
{
print_er
();
printf
(
"Unknown command `%s`; ABORTING
\n
"
,
argv
[
i
]
);
printf
(
"Unknown command `%s`; ABORTING
\n
"
,
input
);
return
EXIT_FAILURE
;
}
print_vb
(
""
);
printf
(
"Sending command '%s'...
\n
"
,
argv
[
i
]
);
print_vb
(
""
);
f
printf
(
stderr
,
"Sending command '%s'...
\n
"
,
input
);
fprintf
(
fh
,
"%s
\n
"
,
argv
[
i
]
);
fprintf
(
fh
,
"%s
\n
"
,
input
);
fflush
(
fh
);
response_loop
(
fh
,
cmd
->
callback
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment