From: dagibbs@quantum.on.ca (David Gibbs)
Subject: Re: Galaxy Tool
Date: Sat, 21 Nov 92 09:24:08 GMT
Organization: Quantum Software Systems
Keywords: galaxy fleets variables

Here is an awk script to do what andre's program does.  Take your
pick of which you prefer to use.  I only created it for the fun
of figuring out how to do it in awk.  (Call it educational,
I just figured it was the sort of thing that awk was designed for.)

In article <ktnqxmt@quantum.on.ca> dagibbs@quantum.on.ca (David Gibbs) writes:
>In article <andre.722207434@galaxy> andre@ph.tn.tudelft.nl (Andre Verweij) writes:
>>Some time ago someone asked if it was possible to join groups into
>>a fleet. As this is still not possible in Russell's game, we all have
>>to type a command for every group seperately. This brought me on the
>>idea to use variables. The program I include below can handle 
>>variables. A variable is defined as follows :
>>
>>varname = {one,two,three,four}
>>
>>I am open for suggestions to improve this peace of software.
>
>I haven't looked at it in detail, but from what it is doing, it
>strikes me that the way to do this is in either Sed or Awk,
>rather than in C, since this is almost purely linear processing
>of a file.  (ie we are applying a filter to a text file.)
>
>Maybe I'll try writing it in awk or sed (probably awk) for fun
>this weekend.
>
>Of course, if you don't have awk, then c code is probably more
>useful.

What follows is the awk script to implement exactly the
same variable handling that Andre did.  It has no explicit
limits to the number groups in each fleet, but this is
of course limitted by the amount of memory you have 
available and the overhead that awk imposes.

To run: type:  awk -f fleet.awk orders_file > output_file
(or cat orders_file | awk -f fleet.awk - > output_file )

Our programs behaviour will be slightly different for a line of
the form:

asdf jkl = {elem1, elem2, elem3}

My program will name the variable asdf, and look for $asdf,
Andre's will look for $jkl.

-David 
(dagibbs@quantum.qnx.com)
-----------cut here---------------
BEGIN {
FS = "[ \t]+"
ind = 0
system("rm FLEET_ERRORS >/dev/null 2>/dev/null")
}

function outline( input, no_tok, tok_ar, which, i, j ) {
  no_tok = split( input, tok_ar, "[ \t]+" )
  which = 0
  for (i=1; i <= no_tok; i++) if (index(tok_ar[i], "$") == 1) which = i
  if (!which) {
    # badly positioned $ sign, just output line and flag error
    print "badly positioned dollar sign processing following line:" FNR >> "FLEET_ERRORS"
    print $0 >> "FLEET_ERRORS"
    print $0
    next
  }
  
  temp = tok_ar[which]
  n = sub( "\$", "", temp )
  for (i=1; i <= ind; i++ ) if( var[i] == temp ) break 
  if (i > ind) { 
    print "variable with no previous declaration found processing following line:" FNR >> "FLEET_ERRORS"
    print $0 >> "FLEET_ERRORS"
    print $0
    next
  }
  for (j = 1; j <= num[i]; j++) {
    tok_ar[which] = flist[i,j]
    out = ""
    for(l=1; l <= no_tok; l++ ) out = out tok_ar[l] " "
    if (index(out, "$")) outline( out ); else print out
  }  
}

/\=/ {
  #handle anything with equal sign in it
  ind++
  var[ind] = $1
  x = index( $0, "{" ) +1
  temp = substr( $0, x, (index( $0, "}" )-x) )
  sub( "^[ \t]+", "", temp )   # remove leading blanks
  sub( "[ \t]+$", "", temp )	# remove trailing blanks
  num[ind] = split( temp, temp1, "[ \t]*,[ \t]*" )
  for (i=1; i <= num[ind]; i++ ) flist[ind,i] = temp1[i]  #copy into storage
}

(($0 ~ /\$/) && ($0 !~ /\=/)) {
  #handle anything with a $ in it that doesn't have an =
  outline( $0 )
}

$0 !~ /\$|=/ { print $0 }  #handle the default case