11.8. Gawk-Specific Features

 < Day Day Up > 

This section describes features unique to gawk .

11.8.1. Coprocesses and Sockets

gawk allows you to open a two-way pipe to another process, called a coprocess. This is done with the |& operator used with getline and print or printf.

     print database command |& "db_server"     "db_server" |& getline response

If the command used with |& is a filename beginning with /inet/, gawk opens a TCP/IP connection. The filename should be of the following form:

     /inet/protocol/lport/hostname/rport

The parts of the filename are:


protocol

One of tcp, udp, or raw, for TCP, UDP, or raw IP sockets, respectively. Note: raw is currently reserved but unsupported.


lport

The local TCP or UPD port number to use. Use 0 to let the operating system pick a port.


hostname

The name or IP address of the remote host to connect to.


rport

The port (application) on the remote host to connect to. A service name (e.g., tftp) is looked up using the C getservbyname( ) function.

11.8.2. Profiling

When gawk is built and installed, a separate program named pgawk (profiling gawk) is built and installed with it. The two programs behave identically; however, pgawk runs more slowly since it keeps execution counts for each statement as it runs. When it is done, it automatically places an execution profile of your program in a file named awkprof.out. (You can change the filename with the --profile option.)

The execution profile is a "prettyprinted" version of your program with execution counts listed in the left margin. For example, after running this program:

     $ pgawk '/bash$/ { nusers++ }     > END { print nusers, "users use Bash." }' /etc/passwd     16 users use Bash.

The execution profile looks like this:

         # gawk profile, created Mon Nov  1 14:34:38 2004         # Rule(s)     35  /bash$/ { # 16     16          nusers++         }         # END block(s)         END {      1          print nusers, "users use Bash."         }

If sent SIGUSR1, pgawk prints the profile and an awk function call stack trace, and then keeps going. Multiple SIGUSR1 signals may be sent; the profile and trace will be printed each time. This facility is useful if your awk program appears to be looping, and you want to see if something unexpected is being executed.

If sent SIGHUP, pgawk prints the profile and stack trace, and then exits.

11.8.3. File Inclusion

The igawk program provides a file inclusion facility for gawk. You invoke it the same way you do gawk: it passes all command-line arguments on to gawk. However, igawk processes source files and command-line programs for special statements of the form:

     @include file.awk

Such files are searched for along the list of directories specified by the AWKPATH environment variable. When found, the @include line is replaced with the text of the corresponding file. Included files may themselves include other files with @include.

The combination of the AWKPATH environment variable and igawk makes it easy to have and use libraries of awk functions.

11.8.4. Internationalization

You can internationalize your programs if you use gawk. This consists of choosing a text domain for your program, marking strings that are to be translated, and if necessary, using the bindtextdomain( ), dcgettext( ), and dcngettext( ) functions.

Localizing your program consists of extracting the marked strings, creating translations, and compiling and installing the translations in the proper place. Full details are given in Effective awk Programming, cited in the Bibliography.

The internationalization features in gawk use GNU gettext. You may need to install the GNU gettext tools to create translations if your system doesn't already have them. Here is a very brief outline of the steps involved.

  1. Set TEXtdOMAIN to your text domain in a BEGIN block:

         BEGIN { TEXTDOMAIN = "whizprog" } 

  2. Mark all strings to be translated by prepending a leading underscore:

         printf(_"whizprog: can't open /dev/telepath (%s)\n",                        dcgettext(ERRNO)) > "/dev/stderr" 

  3. Extract the strings with the --gen-po option:

         $ gawk --gen-po -f whizprog.awk > whizprog.pot 

  4. Copy the file for translating, and make the translations:

         $ cp whizprog.pot esperanto.po     $ ed esperanto.po 

  5. Use the msgfmt program from GNU gettext to compile the translations. The binary format allows fast lookup of the translations at runtime. The default output is a file named messages:

         $ msgfmt esperanto.po     $ mv messages esperanto.mo 

  6. Install the file in the standard location. This is usually done at program installation. The location can vary from system to system.

That's it! gawk will automatically find and use the translated messages, if they exist.

     < Day Day Up > 


    Unix in a Nutshell
    Unix in a Nutshell, Fourth Edition
    ISBN: 0596100299
    EAN: 2147483647
    Year: 2005
    Pages: 201

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net