[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.6.7 for-each: perform function on each argument

This produces a main procedure that invokes a procedure once for each operand on the command line (non-option arguments), OR once for each non-blank, non-comment stdin input line. Leading and trailing white space is trimmed from the input line and comment lines are lines that begin with a hash ('#') character. (If anyone ever asks, I could make it specifiable, as the code to do this is emitted into the output and is not part of libopts.)

NB: The argument program attribute (see section 7.3.1 Program Description Attributes) must either begin with the [ character, or be omitted and default to "[ [...]]".

The name of the procedure must be specified with the handler-proc attribute. That procedure is presumed to be external, but if you provide the code for it, then the procedure is emitted as a static procedure in the generated code. The profile of the procedure must be:

 
int my_handler( const char *pz_operand );

If you provide the code, then your main() procedure specification will look something like this:

 
main = {
  main-type    = for-each;
  handler-proc = my_handler;
  my_handler-code = <<- EndOfMyCode
	/* whatever you want to do */
	EndOfMyCode;
};

and the emitted procedure will look like this:

 
static int
my_handler( const char* pz_entry )
{
    int res = 0;
    <<my_handler-code goes here>>
    return res;
}

Exit code reporting is cumulative. If the called procedure needs to cause a fail-exit, it is expected to call exit(3) directly. If you want to cause a warning exit code, then this handler function should return a non-zero status. That value will be OR-ed into a result integer for computing the final exit code. E.g., here is part of the emitted code:

 
    int res = 0;
    if (argc > 0) {
        do  {
            res |= my_handler( *(argv++) );
        } while (--argc > 0);
    } else { ...

You may also specify some main procedure start up code with the main-init attribute. Any code specified by that attribute will be inserted immediately after the option processing call.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by Bruce Korb on October 24, 2004 using texi2html

Viewable With Any Browser   AutoGen Home