"Fossies" - the Fresh Open Source Software Archive

Member "multirun-1.0.1/multirun" (25 May 1999, 7253 Bytes) of package /linux/misc/old/multirun-1.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/bin/bash
    2 #
    3 # 25 May 1999 - Andrew Wood <andrew.wood@poboxes.com>
    4 
    5 pn=multirun
    6 vn=1.0.1
    7 rn=$0
    8 
    9 help_text="Usage: $rn [OPTIONS] PROGRAM [[OPTIONS] PROGRAM]...
   10 Run all PROGRAMs simultaneously, each with their own portion of the screen.
   11 
   12 Options apply to the program immediately afterwards ONLY.  Default values are
   13 shown in [brackets].  To set the defaults for all subsequent programs, prefix
   14 the option name with \`default-', eg \`--default-lines'.
   15 
   16   --minlines=NUM          allow at least NUM lines for text [2]
   17   --maxlines=NUM          allow at most NUM lines for text  [0]
   18   --foreground=COL        PROGRAM output foreground colour  [white]
   19   --background=COL        PROGRAM output background colour  [black]
   20   --title=TITLE           PROGRAM title text                [PROGRAM]
   21   --title-foreground=COL  title text foreground colour      [white]
   22   --title-background=COL  title text background colour      [black]
   23   --title-attribute=ATTR  title text attribute              [reversed]
   24   --log=FILE              also append output to FILE        [/dev/null]
   25 
   26   --help                  display this help
   27   --version               show version and warranty information
   28 
   29 Colours can be any of \`black', \`red', \`green', \`yellow', \`blue', \`magenta',
   30 \`cyan', or \`white', or the ANSI colour code (0-7).  Attributes can be any of
   31 \`normal', \`bold', \`underlined', or \`reversed', or the code (0,1,4,7)."
   32 
   33 version_text="$pn version $vn
   34 Copyright (C) 1999 Andrew Wood
   35 
   36     This program is free software; you can redistribute it and/or modify
   37     it under the terms of the GNU General Public License as published by
   38     the Free Software Foundation; either version 2 of the License, or
   39     (at your option) any later version.
   40 
   41     This program is distributed in the hope that it will be useful,
   42     but WITHOUT ANY WARRANTY; without even the implied warranty of
   43     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   44     GNU General Public License for more details.
   45 
   46     You should have received a copy of the GNU General Public License
   47     along with this program.  If not, write to the Free Software
   48     Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
   49 
   50 Please report bugs to Andrew Wood <andrew.wood@poboxes.com>."
   51 
   52 
   53 # Variables that can be set for each program
   54 #
   55 allvars="minlines maxlines foreground background title
   56          title_foreground title_background title_attribute log"
   57 
   58 # Kludge
   59 #
   60 title_foreground=title-foreground
   61 title_background=title-background
   62 title_attribute=title-attribute
   63 
   64 
   65 # Set up evaluation script to deal with all options above
   66 #
   67 opt_eval="case \$i in"
   68 for i in $allvars; do
   69   case $i in
   70     *_*) eval o=\$$i ;;
   71     *) o=$i ;;
   72   esac
   73   opt_eval="$opt_eval
   74     --$o) store_next=$i ;;
   75     --$o=*) $i=\`echo \$i|sed 's/^[^=]*=//'\` ;;
   76     --default-$o) store_next=default_$i ;;
   77     --default-$o=*) default_$i=\`echo \$i|sed 's/^[^=]*=//'\` ;;"
   78 done
   79 opt_eval="$opt_eval
   80     --*) errexit \"unknown option \\\`\$i'.\" ;;
   81   esac"
   82 
   83 
   84 # Program defaults
   85 #
   86 default_minlines=2
   87 default_maxlines=0
   88 default_foreground=white
   89 default_background=black
   90 default_title=PROGRAM
   91 default_title_foreground=white
   92 default_title_background=black
   93 default_title_attribute=reversed
   94 default_log=/dev/null
   95 
   96 
   97 # Abort with error $1.
   98 #
   99 function errexit () {
  100   echo "$rn: $1" >&2
  101   echo "Try \`$rn --help' for more information." >&2
  102   exit 1
  103 }
  104 
  105 
  106 # Convert the colour values in variables $* to numbers and store them back.
  107 #
  108 function decode_colour () {
  109   local var val
  110   for var in $*; do
  111     eval val="\$$var"
  112     case $val in
  113       [bB][lL][aA]*) val=0 ;;
  114       [rR]*) val=1 ;;
  115       [gG]*) val=2 ;;
  116       [yY]*|[oO]*) val=3 ;;
  117       [bB][lL][uU]*) val=4 ;;
  118       [mM][aA]*|[pP][uU]*) val=5 ;;
  119       [cC]*) val=6 ;;
  120       [wW]*) val=7 ;;
  121       0|1|2|3|4|5|6|7) ;;
  122       *) errexit "invalid colour \`$val'" ;;
  123     esac
  124     eval $var=$val
  125   done
  126 }
  127 
  128 
  129 # Convert the attribute value in $1 to a number and store it back.
  130 #
  131 function decode_attribute () {
  132   local var val
  133   var=$1
  134   eval val="\$$1"
  135   case $val in
  136     [nN]*) val=0 ;;
  137     [bB]*) val=1 ;;
  138     [uU]*) val=4 ;;
  139     [rR]*) val=7 ;;
  140     0|1|4|7) ;;
  141     *) errexit "invalid attribute \`$val'" ;;
  142   esac
  143   eval $var=$val
  144 }
  145 
  146 
  147 # Add program $1 to the list of programs to be executed.
  148 #
  149 function add_program () {
  150   progs="${progs} <($1 2>&1|awk '{print\"$n:\"\$0;\
  151 print>>\"$log\";fflush(\"\")}'>&3)"
  152   if [ "$title" = "PROGRAM" ]; then title="$1"; fi
  153   decode_colour foreground background title_foreground title_background
  154   decode_attribute title_attribute
  155   if [ "$minlines" -lt 2 ]; then
  156     errexit "must use at least 2 lines - one for title, one for text"
  157   fi
  158   eval _tt_$n=\""$title"\"
  159   eval _te_$n=\""\033[3$title_foreground;\
  160 4$title_background;${title_attribute}m"\"
  161   eval _be_$n=\""\033[3$foreground;4${background}m"\"
  162   eval _lsize_$n=$minlines
  163   eval _lmax_$n=$maxlines
  164   n=$[1+$n]
  165   lines_used=$[$lines_used+$minlines]
  166   for v in $allvars; do eval $v="\$default_$v"; done
  167 }
  168 
  169 
  170 # Process command line options
  171 #
  172 for v in $allvars; do eval $v="\$default_$v"; done
  173 store_next=""
  174 progs=""
  175 lines_used=0
  176 n=1
  177 for i; do
  178   if [ -n "$store_next" ]; then eval $store_next="$i"; store_next=""; else
  179     o=$i
  180     case $i in
  181       -[hH?]|--help) echo "$help_text"; exit 0 ;;
  182       -[vV]|--version) echo "$version_text"; exit 0 ;;
  183       --*) eval "$opt_eval" ;;
  184       *) add_program "$i" ;;
  185     esac
  186   fi
  187 done
  188 
  189 if [ -n "$store_next" ]; then errexit "\`$o' option requires an argument"; fi
  190 if [ $n = 1 ]; then errexit "no programs specified"; fi
  191 
  192 
  193 # Check output is connected to a terminal
  194 #
  195 if [ ! -t 1 ] && [ -t 2 ]; then exec >&2; fi
  196 if [ ! -t 1 ] || [ ! -t 0 ]; then
  197   errexit "must be connected to a terminal"
  198 fi
  199 
  200 
  201 # Find the current terminal size
  202 #
  203 ROWS=`stty -a | sed -n 's/^.* rows \([0-9]\+\);.*$/\1/p'`
  204 
  205 
  206 # Make sure we can fit everything on the screen
  207 #
  208 if [ $lines_used -gt $ROWS ]; then
  209   errexit "need at least $lines_used lines, terminal only has $ROWS"
  210 fi
  211 
  212 
  213 # Expand windows to fill the screen
  214 #
  215 atmax=1
  216 j=1
  217 while [ $lines_used -lt $ROWS ] && [ $atmax -lt $n ]; do
  218   if [ $j -ge $n ]; then
  219     j=1
  220     atmax=1
  221   fi
  222   eval size=\$_lsize_$j
  223   eval max=\$_lmax_$j
  224   if [ $max -gt 0 ] && [ $size -ge $max ]; then
  225     atmax=$[1+$atmax]
  226     j=$[1+$j]
  227   else
  228     eval _lsize_$j=$[1+$size]
  229     lines_used=$[1+$lines_used]
  230     j=$[1+$j]
  231   fi
  232 done
  233 
  234 
  235 # Clear the screen and output the titles, also working out Y co-ordinates of
  236 # each window and storing them for use in the AWK script
  237 #
  238 echo -n -e "\033[H\033[J"
  239 j=1
  240 ypos=1
  241 awkprg='BEGIN{'
  242 while [ $j -lt $n ]; do
  243   eval text=\""\$_tt_$j"\"
  244   eval trib=\""\$_te_$j"\"
  245   eval lines=\$_lsize_$j
  246   eval body=\""\$_be_$j"\"
  247   echo -n -e "\033[${ypos}H${trib}${text}\033[0;37;40m"
  248   awkprg="${awkprg};yt[$j]=$[1+$ypos];yb[$j]=$[$ypos+$lines-1]"
  249   awkprg="${awkprg};be[$j]=\"$body\""
  250   ypos=$[$ypos+$lines]
  251   j=$[1+$j]
  252 done
  253 
  254 
  255 # Finish off the AWK output program
  256 #
  257 awkprg="${awkprg}}{
  258   t=substr(\$0,3)
  259   n=substr(\$0,1,1)
  260   printf \"\033[%d;%dr\033[%dH\n\",yt[n],yb[n],yb[n]
  261   printf \"%s%s\033[0;37;40m\",be[n],t
  262   fflush(\"\")
  263 }"
  264 
  265 
  266 # Make sure screen is reset to normal on exit
  267 #
  268 trap "echo -n -e '\033[0;37;40m\033[1;${ROWS}r\033[${ROWS};1H\n'" 0
  269 
  270 
  271 # Run the pipeline
  272 #
  273 eval "{ exec /bin/true $progs >/dev/null; } 3>&1 | awk '$awkprg'"
  274 
  275 # EOF