Skip to content

Commit

Permalink
pmc: Allow commands on command line.
Browse files Browse the repository at this point in the history
Add a batch mode, where the commands are taken from the command line
instead of the standard input.

Signed-off-by: Miroslav Lichvar <[email protected]>
  • Loading branch information
mlichvar authored and richardcochran committed Feb 6, 2013
1 parent ecbbf4c commit 7f760ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
8 changes: 4 additions & 4 deletions pmc.8
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ pmc \- PTP management client
.BI \-t " transport-specific-field"
] [
.B \-v
]
] [ command ] ...

.SH DESCRIPTION
.B pmc
is a program which implements a PTP management client according to IEEE
standard 1588. The program reads from the standard input actions specified by
name and management ID, sends them over the selected transport and prints any
received replies. There are three actions supported:
standard 1588. The program reads from the standard input or from the command
line actions specified by name and management ID, sends them over the selected
transport and prints any received replies. There are three actions supported:
.B GET
retrieves the specified information,
.B SET
Expand Down
39 changes: 31 additions & 8 deletions pmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ static int do_command(char *str)
static void usage(char *progname)
{
fprintf(stderr,
"\nusage: %s [options]\n\n"
"\nusage: %s [options] [commands]\n\n"
" Network Transport\n\n"
" -2 IEEE 802.3\n"
" -4 UDP IPV4 (default)\n"
Expand All @@ -402,8 +402,8 @@ static void usage(char *progname)
int main(int argc, char *argv[])
{
char *iface_name = NULL, *progname;
int c, cnt, length, tmo = -1;
char line[1024];
int c, cnt, length, tmo = -1, batch_mode = 0;
char line[1024], *command = NULL;
enum transport_type transport_type = TRANS_UDP_IPV4;
UInteger8 boundary_hops = 1, domain_number = 0, transport_specific = 0;
struct ptp_message *msg;
Expand Down Expand Up @@ -458,6 +458,9 @@ int main(int argc, char *argv[])
if (!iface_name) {
iface_name = transport_type == TRANS_UDS ? "/var/run/pmc" : "eth0";
}
if (optind < argc) {
batch_mode = 1;
}

print_set_progname(progname);
print_set_syslog(1);
Expand All @@ -469,12 +472,28 @@ int main(int argc, char *argv[])
return -1;
}

pollfd[0].fd = STDIN_FILENO;
pollfd[0].events = POLLIN|POLLPRI;
pollfd[0].fd = batch_mode ? -1 : STDIN_FILENO;
pollfd[1].fd = pmc_get_transport_fd(pmc);
pollfd[1].events = POLLIN|POLLPRI;

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);
if (cnt < 0) {
if (EINTR == errno) {
Expand Down Expand Up @@ -505,9 +524,13 @@ int main(int argc, char *argv[])
continue;
}
line[length - 1] = 0;
if (do_command(line)) {
fprintf(stderr, "bad command: %s\n", line);
command = 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)) {
msg = pmc_recv(pmc);
Expand Down

0 comments on commit 7f760ce

Please sign in to comment.