On Thu, Aug 12, 2010 at 09:19:52PM -0400, Kris Maglione wrote:
Yes... Unfortunately awk dies on SIGPIPE in that example, so it never runs the END block. This slight variation should work:

    wimenu -c <fifo | awk '
        BEGIN {
             # Define the completion results
             cmds = "foo\nbar\nbaz\n"
             cmd["foo"] = "1\n2\n3\n"
             cmd["bar"] = "4\n5\n6\n"
             cmd["baz"] = "7\n8\n9\n"
                 # Print the first set of completions to wimenu’s fifo
             fifo = "fifo"
             print cmds >fifo; fflush(fifo)
        }

        //

        # Push out a new set of completions
        function update(str, opts) {
             print length(str) >fifo # Print the length of the preceding string
             print opts >fifo        # and the options themself
             fflush(fifo)
        }

        # Ensure correct argument count with trailing spaces
        / $/ { $0 = $0 "#"; }
            { # Process the input and provide the completions
             if (NF == 1)
                  update("", cmds)        # The first arg, command choices
             else
                  update($1 " ", cmd[$1]) # The second arg, command arguments
             # Skip the trailing part of the command
             getline rest
        }
    ' | tail -1

Oh, but I notice that this doesn't work with gawk. You'll need to replace the // line with { print; fllush() }. I'm afraid that if you use mawk it's hopeless.

--
Kris Maglione

If you think your management doesn't know what it's doing or that your
organisation turns out low-quality software crap that embarrasses you,
then leave.
        --Edward Yourdon


Reply via email to