groff
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Groff] getopt() as gtroff macro ?


From: Keith MARSHALL
Subject: Re: [Groff] getopt() as gtroff macro ?
Date: Mon, 20 Sep 2004 11:09:59 +0100

Heinz-Jürgen Oertel wrote:
> More and more gtroff macros needs to handle arguments
> in a generic way.  Is there something like getopt(3)
> available, may be already implemented in some other
> macro packages, ms, me, mm, or the more modern ones
> like mom?

FWIW, here's the technique I've adopted in my PDF macro
set, (which I plan to contribute, but it isn't quite stable
enough yet, for public consumption).

The macro I have provided to place an active reference
has the syntax:

  .pdfhref L [-D <dest-name>] [-P <prefix-text>] \
     [-A <affix-text>] [-X] [--] <description>

where the L tells pdfhref to create a link, <description>
is user defined text which appears in the "hot-spot" region
of the link, and all other arguments are optional, (-D, -P
and -A each having one additional argument associated with
them, which becomes mandatory when the option is specified).

The pdfhref macro itself is quite simple, delegating the
actual work load to another macro, called pdfhref-L:

  .de pdfhref
  .   ie d\\$0-\\$1 \{\
  .      als pdfhref.exec \\$0-\\$1
  .      shift \" discard the reference class code
  .      pdfhref.exec \\$@ \" do the business
  .      pdfhref.cleanup   \" remove temporary strings
  .                        \" and registers
  .      \}
  .   .el .complain "\\$0: unknown reference class \\$1"
  ..

Now, to capture the optional arguments, I define something
along the lines of:

  .de pdfhref.opt
  .   ds pdfhref.opt\\$1 \\$2
  .   nr pdfhref.argc 2
  ..

  .de pdfhref.flag
  .   nr pdfhref.flag\\$1 1
  .   nr pdfhref.argc 1
  ..

  .als pdfhref.getopt-D pdfhref.opt
  .als pdfhref.getopt-P pdfhref.opt
  .als pdfhref.getopt-A pdfhref.opt
  .als pdfhref.getopt-X pdfhref.flag

and, at the beginning of pdfhref-L, I have an option
initialisation and capture sequence, something like:

  .de pdfhref-L
  .   ds pdfhref.opt-A    \" initialise optargs to NULL
  .   ds pdfhref.opt-D
  .   ds pdfhref.opt-P
  .   nr pdfhref.opt-X 0  \" and flag option to OFF
  .
  .   while dpdfhref.getopt\\$1 \{\
  .      pdfhref.getopt\\$1 \\$@
  .      shift \\n[pdfhref.argc
  .      \}
  .
  .   if '\\$1'--' shift   \" discard option terminator
  .                        \" (if present)
  .
  .

HTH.

Best regards,
Keith.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]