# HG changeset patch # User Mychaela Falconia # Date 1500270691 0 # Node ID 713749548df6811fd66671a031415fa8213865cb # Parent 0937d34fb3ba82bbd7f75e7bb1ef78fc6e8e9acc fc-rfcal-tri900 script and fc-rfcal-tee helper implemented diff -r 0937d34fb3ba -r 713749548df6 .hgignore --- a/.hgignore Mon Jul 17 01:38:00 2017 +0000 +++ b/.hgignore Mon Jul 17 05:51:31 2017 +0000 @@ -12,6 +12,8 @@ ^cmu200/fc-cmu200d$ ^cmu200/fc-serscpi$ +^scripts/fc-rfcal-tee$ + ^tsid-test/fc-tsid-shell$ ^vcxo-manual/fc-vcxo-linear$ diff -r 0937d34fb3ba -r 713749548df6 scripts/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/Makefile Mon Jul 17 05:51:31 2017 +0000 @@ -0,0 +1,17 @@ +CC= gcc +CFLAGS= -O2 +PROGS= fc-rfcal-tee +SCRIPTS=fc-rfcal-tri900 +INSTBIN=/opt/freecalypso/bin + +all: ${PROGS} + +fc-rfcal-tee: fc-rfcal-tee.c + ${CC} ${CFLAGS} -o $@ $@.c + +install: + mkdir -p ${INSTBIN} + install -c ${PROGS} ${SCRIPTS} ${INSTBIN} + +clean: + rm -f *.o *.out *errs ${PROGS} diff -r 0937d34fb3ba -r 713749548df6 scripts/fc-rfcal-tee.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/fc-rfcal-tee.c Mon Jul 17 05:51:31 2017 +0000 @@ -0,0 +1,86 @@ +/* + * This C program is a workaround for a stupidity in Bourne shell. + * I would like to have a shell script like the following: + * + * set -e + * fc-rfcal-vcxo | tee vcxo + * fc-rfcal-rxband 900 | tee rx-900 + * fc-rfcal-rxband 1800 | tee rx-1800 + * ... + * + * and if one of the fc-rfcal-* steps encounters an error and returns + * a non-zero exit code, I want the script to stop right there, hence + * the set -e at the beginning. Sounds like a very simple and reasonable + * desire, doesn't it? But oh noes - because I am piping the output + * through tee in order to save it in log files, the process return + * codes from fc-rfcal-* get *ignored* by the shell (the always successful + * exit code from tee is all that counts), and it will keep executing + * the following lines without stopping. + * + * This C program is like tee, but with invokation of the "main" command + * whose output is to be saved built in. The process forks, the "main" + * command is executed in the child, the parent performs the tee function, + * and when the child terminates, the parent propagates its exit code. + */ + +#include +#include +#include +#include +#include +#include + +char shell_pathname[] = "/bin/sh"; +int log_fd; + +tee_process(pipe_fd) +{ + char buf[1024]; + int cc; + + while ((cc = read(pipe_fd, buf, sizeof buf)) > 0) { + write(1, buf, cc); + if (log_fd > 2) + write(log_fd, buf, cc); + } +} + +main(argc, argv) + char **argv; +{ + int p[2]; + pid_t child, waitres; + int status; + + if (argc != 3) { + fprintf(stderr, "usage: %s command logfile\n", argv[0]); + exit(1); + } + if (pipe(p) < 0) { + perror("pipe"); + exit(1); + } + child = fork(); + if (child < 0) { + perror("fork"); + exit(1); + } + if (!child) { + dup2(p[1], 1); + close(p[0]); + close(p[1]); + execl(shell_pathname, "sh", "-c", argv[1], 0); + perror(shell_pathname); + exit(1); + } + close(p[1]); + log_fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0666); + if (log_fd < 0) + perror(argv[2]); + tee_process(p[0]); + waitres = waitpid(child, &status, 0); + if (waitres == child && WIFEXITED(status)) + exit(WEXITSTATUS(status)); + else + exit(1); +} diff -r 0937d34fb3ba -r 713749548df6 scripts/fc-rfcal-tri900 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scripts/fc-rfcal-tri900 Mon Jul 17 05:51:31 2017 +0000 @@ -0,0 +1,62 @@ +#!/bin/sh + +case $# in + 1) + ;; + 2) + if [ ! -d "$2" ] + then + echo "error: specified report dir $2 is bad" 1>&2 + exit 1 + fi + ;; + *) + echo "usage: $0 txlevels-profile [report-dir]" 1>&2 + exit 1 + ;; +esac + +profile_dir=/opt/freecalypso/rfcal/txlevels + +if [ ! -f "$profile_dir/$1-900" ] +then + echo "error: $profile_dir/$1-900 profile missing" 1>&2 + exit 1 +fi + +if [ ! -f "$profile_dir/$1-1800" ] +then + echo "error: $profile_dir/$1-1800 profile missing" 1>&2 + exit 1 +fi + +if [ ! -f "$profile_dir/$1-1900" ] +then + echo "error: $profile_dir/$1-1900 profile missing" 1>&2 + exit 1 +fi + +report_dir="$2" + +run_cmd() { + if [ -n "$report_dir" ] + then + fc-rfcal-tee "$1" "$report_dir/$2" + else + eval "$1" + fi + rc=$? + if [ $rc != 0 ] + then + echo "error: $1 returned $rc" 1>&2 + exit $rc + fi +} + +run_cmd fc-rfcal-vcxo vcxo +run_cmd "fc-rfcal-rxband 900" rx-900 +run_cmd "fc-rfcal-rxband 1800" rx-1800 +run_cmd "fc-rfcal-rxband 1900" rx-1900 +run_cmd "fc-rfcal-txband 900 $1" tx-900 +run_cmd "fc-rfcal-txband 1800 $1" tx-1800 +run_cmd "fc-rfcal-txband 1900 $1" tx-1900