# HG changeset patch # User Mychaela Falconia # Date 1477525907 0 # Node ID 27c41e4b21ae790ece3ae313ec3034d3bb277572 # Parent 2c6dca514a20d70d5a21209f318e79481fbd2884 fc-tmsh one-shot operation mode implemented diff -r 2c6dca514a20 -r 27c41e4b21ae rvinterf/tmsh/Makefile --- a/rvinterf/tmsh/Makefile Wed Oct 26 23:33:21 2016 +0000 +++ b/rvinterf/tmsh/Makefile Wed Oct 26 23:51:47 2016 +0000 @@ -2,7 +2,7 @@ CFLAGS= -O2 -I../include PROG= fc-tmsh OBJS= abb.o audiocmd.o etmbasic.o ffs2.o ffs2resp.o init.o main.o misc.o \ - omr.o pktsort.o tmcore.o usercmd.o + omr.o oneshot.o pktsort.o tmcore.o usercmd.o LIBS= ../libasync/libasync.a INSTBIN=/opt/freecalypso/bin diff -r 2c6dca514a20 -r 27c41e4b21ae rvinterf/tmsh/main.c --- a/rvinterf/tmsh/main.c Wed Oct 26 23:33:21 2016 +0000 +++ b/rvinterf/tmsh/main.c Wed Oct 26 23:51:47 2016 +0000 @@ -12,6 +12,7 @@ char *socket_pathname = "/tmp/rvinterf_socket"; char *rvinterf_ttyport; int ttyhacks, dflag; +int oneshot_nowait; int sock; @@ -25,7 +26,7 @@ int c, sopt = 0; fd_set fds; - while ((c = getopt(argc, argv, "B:dl:p:s:w:")) != EOF) + while ((c = getopt(argc, argv, "B:dl:np:s:w:")) != EOF) switch (c) { case 'B': rvinterf_Bopt = optarg; @@ -36,6 +37,9 @@ case 'l': rvinterf_lopt = optarg; continue; + case 'n': + oneshot_nowait++; + continue; case 'p': rvinterf_ttyport = optarg; continue; @@ -48,8 +52,7 @@ continue; case '?': default: -usage: fprintf(stderr, - "usage: %s [options] [command]\n", argv[0]); + /* error msg already printed */ exit(ERROR_USAGE); } if (rvinterf_ttyport) { @@ -70,6 +73,9 @@ connect_local_socket(); } + if (argv[optind]) + return oneshot_command(argc - optind, argv + optind); + ttyhacks = isatty(0) && !dflag; init(); tty_init(); diff -r 2c6dca514a20 -r 27c41e4b21ae rvinterf/tmsh/oneshot.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rvinterf/tmsh/oneshot.c Wed Oct 26 23:51:47 2016 +0000 @@ -0,0 +1,45 @@ +/* + * This module implements the one-shot mode of operation for fc-tmsh. + */ + +#include +#include +#include +#include +#include +#include "exitcodes.h" + +extern int oneshot_nowait; +extern int sock; +extern int got_tm_response; + +oneshot_command(argc, argv) + char **argv; +{ + fd_set fds; + int rc; + + if (!oneshot_nowait) + init(); /* to catch the response properly */ + rc = dispatch_oneshot_cmd(argc, argv); + if (rc) + exit(rc); + if (oneshot_nowait) + exit(0); + /* wait for response */ + for (;;) { + FD_ZERO(&fds); + FD_SET(sock, &fds); + rc = select(sock+1, &fds, 0, 0, 0); + if (rc < 0) { + if (errno == EINTR) + continue; + perror("select"); + exit(ERROR_UNIX); + } + if (FD_ISSET(sock, &fds)) + handle_rvinterf_input(); + if (got_tm_response) + exit(0); + } +} diff -r 2c6dca514a20 -r 27c41e4b21ae rvinterf/tmsh/pktsort.c --- a/rvinterf/tmsh/pktsort.c Wed Oct 26 23:33:21 2016 +0000 +++ b/rvinterf/tmsh/pktsort.c Wed Oct 26 23:51:47 2016 +0000 @@ -17,6 +17,8 @@ extern u_char rvi_msg[]; extern int rvi_msg_len; +int got_tm_response; + static void print_etm_trace() { @@ -63,6 +65,7 @@ return; case RVT_TM_HEADER: etm_packet_rx(); + got_tm_response = 1; return; default: tty_cleanup(); diff -r 2c6dca514a20 -r 27c41e4b21ae rvinterf/tmsh/usercmd.c --- a/rvinterf/tmsh/usercmd.c Wed Oct 26 23:33:21 2016 +0000 +++ b/rvinterf/tmsh/usercmd.c Wed Oct 26 23:51:47 2016 +0000 @@ -8,6 +8,7 @@ #include #include #include +#include "exitcodes.h" extern char usercmd[]; @@ -120,3 +121,27 @@ *ap = 0; tp->func(ap - argv, argv); } + +dispatch_oneshot_cmd(argc, argv) + char **argv; +{ + struct cmdtab *tp; + + for (tp = cmdtab; tp->cmd; tp++) + if (!strcmp(tp->cmd, argv[0])) + break; + if (!tp->func) { + fprintf(stderr, + "error: \"%s\" is not a valid command\n", argv[0]); + exit(ERROR_USAGE); + } + if (argc - 1 > tp->maxargs) { + fprintf(stderr, "%s: too many arguments\n", tp->cmd); + exit(ERROR_USAGE); + } + if (argc - 1 < tp->minargs) { + fprintf(stderr, "%s: too few arguments\n", tp->cmd); + exit(ERROR_USAGE); + } + return tp->func(argc, argv); +}