pmc: Allow commands on command line.

Add a batch mode, where the commands are taken from the command line
instead of the standard input.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
master
Miroslav Lichvar 2013-02-05 17:36:06 +01:00 committed by Richard Cochran
parent ecbbf4cdb1
commit 7f760ceac8
2 changed files with 35 additions and 12 deletions

8
pmc.8
View File

@ -22,14 +22,14 @@ pmc \- PTP management client
.BI \-t " transport-specific-field" .BI \-t " transport-specific-field"
] [ ] [
.B \-v .B \-v
] ] [ command ] ...
.SH DESCRIPTION .SH DESCRIPTION
.B pmc .B pmc
is a program which implements a PTP management client according to IEEE is a program which implements a PTP management client according to IEEE
standard 1588. The program reads from the standard input actions specified by standard 1588. The program reads from the standard input or from the command
name and management ID, sends them over the selected transport and prints any line actions specified by name and management ID, sends them over the selected
received replies. There are three actions supported: transport and prints any received replies. There are three actions supported:
.B GET .B GET
retrieves the specified information, retrieves the specified information,
.B SET .B SET

39
pmc.c
View File

@ -381,7 +381,7 @@ static int do_command(char *str)
static void usage(char *progname) static void usage(char *progname)
{ {
fprintf(stderr, fprintf(stderr,
"\nusage: %s [options]\n\n" "\nusage: %s [options] [commands]\n\n"
" Network Transport\n\n" " Network Transport\n\n"
" -2 IEEE 802.3\n" " -2 IEEE 802.3\n"
" -4 UDP IPV4 (default)\n" " -4 UDP IPV4 (default)\n"
@ -402,8 +402,8 @@ static void usage(char *progname)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *iface_name = NULL, *progname; char *iface_name = NULL, *progname;
int c, cnt, length, tmo = -1; int c, cnt, length, tmo = -1, batch_mode = 0;
char line[1024]; char line[1024], *command = NULL;
enum transport_type transport_type = TRANS_UDP_IPV4; enum transport_type transport_type = TRANS_UDP_IPV4;
UInteger8 boundary_hops = 1, domain_number = 0, transport_specific = 0; UInteger8 boundary_hops = 1, domain_number = 0, transport_specific = 0;
struct ptp_message *msg; struct ptp_message *msg;
@ -458,6 +458,9 @@ int main(int argc, char *argv[])
if (!iface_name) { if (!iface_name) {
iface_name = transport_type == TRANS_UDS ? "/var/run/pmc" : "eth0"; iface_name = transport_type == TRANS_UDS ? "/var/run/pmc" : "eth0";
} }
if (optind < argc) {
batch_mode = 1;
}
print_set_progname(progname); print_set_progname(progname);
print_set_syslog(1); print_set_syslog(1);
@ -469,12 +472,28 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
pollfd[0].fd = STDIN_FILENO; pollfd[0].fd = batch_mode ? -1 : STDIN_FILENO;
pollfd[0].events = POLLIN|POLLPRI;
pollfd[1].fd = pmc_get_transport_fd(pmc); pollfd[1].fd = pmc_get_transport_fd(pmc);
pollfd[1].events = POLLIN|POLLPRI;
while (1) { while (1) {
if (batch_mode && !command) {
if (optind < argc) {
command = argv[optind++];
} else {
/* No more commands, wait a bit for
any outstanding replies and exit. */
tmo = 100;
}
}
pollfd[0].events = 0;
pollfd[1].events = POLLIN | POLLPRI;
if (!batch_mode && !command)
pollfd[0].events |= POLLIN | POLLPRI;
if (command)
pollfd[1].events |= POLLOUT;
cnt = poll(pollfd, N_FD, tmo); cnt = poll(pollfd, N_FD, tmo);
if (cnt < 0) { if (cnt < 0) {
if (EINTR == errno) { if (EINTR == errno) {
@ -505,9 +524,13 @@ int main(int argc, char *argv[])
continue; continue;
} }
line[length - 1] = 0; line[length - 1] = 0;
if (do_command(line)) { command = line;
fprintf(stderr, "bad command: %s\n", line);
} }
if (pollfd[1].revents & POLLOUT) {
if (do_command(command)) {
fprintf(stderr, "bad command: %s\n", command);
}
command = NULL;
} }
if (pollfd[1].revents & (POLLIN|POLLPRI)) { if (pollfd[1].revents & (POLLIN|POLLPRI)) {
msg = pmc_recv(pmc); msg = pmc_recv(pmc);