GURGLE Tutorial: Making a Command

We don't want users to have to know to run gurgle every time they want a phone list so we'll make the file self executable. We also want to hide the version and banner messages that gurgle prints by default when it runs. Finally we want to send the output directly to a terminal rather than into a file. All these changes are included in the new definition file below. Copy it into a file in the same directory as the data source file and call it phones4 or download phones4. Note that we don't have the .grg extension now. You may have to explicitly set execute permission on the file after downloading. You may also have to change the path given at the top where your gurgle executable lives if its not the default.

#!/usr/bin/gurgle
%%database "phonedir.txt"
%%define DELIM |
%%define NAMCOL
%%define TEXEXT .lst
%%sorton %LASTNAME %FIRSTNAME
%%equate eq_init
  0 >> _eq_verbose
  0 >> _eq_version
  "-" >> _eq_outfile
%%header
PHONE DIRECTORY
%%record
 
  NAME:     %FIRSTNAME %LASTNAME
  JOB:      %JOB
  CONTACT:  %ROOM, x#isprivate(%EXTENSION)
%%end
%%equate isprivate(extn)
  /* if the first character of the extension is the number 4 (in ascii)
     then return "PRIVATE" else return the extension */
  if extn'0 = 52 then outputs("PRIVATE")
  else outputs(extn)
  endif

We have added the definition for a special equate called a trigger equate above. Trigger equates are a number of pre-defined functions that are called at various points during processing. They start off with an empty definition but they can be redefined in the definition file. In fact any equate can be defined multiple times, the used definition is always the last one loaded. Here we use the eq_init trigger equate which is called before any processing. We assign zero to a few internal (system) variables to affect the basic behaviour. The first one turns off the output file line message. The second turns off the startup banner message. The third defines the output file to be standard output (the terminal) rather than the default file (named after the definition file).

The above can now be run without explicitly calling gurgle if it has been made executable.

./phones4

If everything worked this will write to the screen the same output as phones3.grg generated into the file phones3.lst when gurgled.

Exercises

Try removing some of the stuff in eq_init to see the behaviour change.

Now lets format the phone directory for the web.

NEXT