doc/concepts.tex: Delete `no way to discover alignment for instance' claim.
[sod] / lib / keyword.3
CommitLineData
9e91c8e7
MW
1.\" -*-nroff-*-
2.\"
3.\" Keyword argument support
4.\"
5.\" (c) 2015 Straylight/Edgeware
6.\"
7.
8.\"----- Licensing notice ---------------------------------------------------
9.\"
10.\" This file is part of the Sensible Object Design, an object system for C.
11.\"
12.\" SOD is free software; you can redistribute it and/or modify
13.\" it under the terms of the GNU Library General Public License as
14.\" published by the Free Software Foundation; either version 2 of the
15.\" License, or (at your option) any later version.
16.\"
17.\" SOD is distributed in the hope that it will be useful,
18.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20.\" GNU Library General Public License for more details.
21.\"
22.\" You should have received a copy of the GNU Library General Public
23.\" License along with SOD; if not, write to the Free
24.\" Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25.\" MA 02111-1307, USA.
26.
27.\" Highlight using terminal escapes, rather than overstriking.
28.\"\X'tty: sgr 1'
29.
30.\" String definitions and font selection.
31.ie t \{\
32. ds o \(bu
33. if \n(.g .fam P
34.\}
35.el \{\
36. ds o o
37.\}
38.
39.\" .hP TEXT -- start an indented paragraph with TEXT hanging off to the left
40.de hP
41.IP
42\h'-\w'\fB\\$1\ \fP'u'\fB\\$1\ \fP\c
43..
44.
45.de t(
46'in +\\n(.ku
47..
48.de t)
49.in
50..
51.
52.\"--------------------------------------------------------------------------
53.TH keyword 3 "16 December 2015" "Straylight/Edgeware" "Sensible Object Design"
54.
55.SH NAME
56keyword \- keyword argument support library
57.
58.\"--------------------------------------------------------------------------
59.SH SYNOPSIS
60.B #include <sod/keyword.h>
61.PP
62.B "struct kwval { const char *kw; const void *val; };"
63.br
64.B "struct kwtab { const struct kwval *v; size_t n; };"
65.br
66.BI "typedef void kw_unkhookfn(const char *" set ", const char *" kw ");"
67.PP
68.BI "#define " set "_KWSET(_) \e"
69.in +4m
70.BI "_(" name ", " type ", " default ") \e"
71.br
72\&...
73.in
74.IB declaration-specifiers " KWSET_STRUCT(" set ");"
75.br
76.IB declaration-specifiers " KWSET_PARSEFN(" set ")"
77.PP
78.B KWCALL
79.IB type0 " " func "(" type1 " " arg1 ,
80.RB ... ,
81.IB typen " " argn ,
82.B "KWTAIL);"
83.br
84.BI "KWDECL(" set ", " kw ");"
85.br
86.BI "KW_PARSE(" set ", " kw ", " kwfirst ");"
87.br
88.BI "KW_PARSE_EMPTY(" set ", " kwfirst ");"
89.br
90.BI "KWPARSE(" set ");"
91.br
92.BI "KWPARSE_EMPTY(" set ");"
93.PP
94.I val
95.B =
96.IB func "(" arg1 ,
97.RB ... ,
98.IB argn ,
99.BI "KWARGS(" \c
100.t(
101.BI "K(" name ", " value ")"
102.br
103.BI "K_VALIST(" ap ")"
104.br
105.BI "K_TAB(" v ", " n ")"
106.br
107.RB ... );
108.t)
109.br
110.I val
111.B =
112.IB func "(" arg1 ,
113.RB ... ,
114.IB argn ,
115.B "NO_KWARGS);"
116.PP
117.B unsigned
118.BI "KW_COUNT(" set ");"
119.br
120.B void
121.BI "KW_COPY(" \c
122.t(
123.IB fromset ", " toset ","
124.br
125.BI "const struct " fromset "_kwset *" kw ","
126.br
127.BI "struct kwval *" v ", size_t " n ");"
128.t)
129.PP
130.BI "void kw_unknown(const char *" set ", const char *" kw );
131.br
132.BI "void kw_parseempty(\fP" \c
133.t(
134.BI "const char *" set ,
135.BI "const char *" kwfirst ,
136.BI "va_list *" ap ,
137.br
138.BI "const struct kwval *" v ,
139.BI "size_t " n );
140.t)
141.PP
142.B "kw_unkhookfn *kw_unkhook;"
143.br
144.B "kw_unkhookfn kw_defunknown;"
145.
146.\"--------------------------------------------------------------------------
147.SH DESCRIPTION
148.
149.SS Theory
150In standard C,
151the actual arguments provided to a function
152are matched up with the formal arguments
153given in the function definition
154according to their ordering in a list.
155Unless the (rather cumbersome) machinery for dealing with
156variable-length argument tails
157.RB ( <stdarg.h> )
158is used,
159exactly the correct number of arguments must be supplied,
160and in the correct order.
161.PP
162A
163.I keyword argument
164is matched by its distinctive
165.IR name ,
166rather than by its position in a list.
167Keyword arguments may be
168.IR omitted ,
169causing some default behaviour by the function.
170A function can detect whether
171a particular keyword argument was supplied:
172so the default behaviour need not be the same as
173that caused by any specific value of the argument.
174.PP
175Keyword arguments can be provided in three ways.
176.hP 1.
177Directly, as a variable-length argument tail,
178consisting (for the most part \(en see below) of alternating
179keyword names, as pointers to null-terminated strings, and
180argument values, and
181terminated by a null pointer.
182This is somewhat error-prone,
183and the support library defines some macros
184which help ensure that keyword argument lists are well formed.
185.hP 2.
186Indirectly, through a
187.B va_list
188object capturing a variable-length argument tail
189passed to some other function.
190Such indirect argument tails have the same structure as
191the direct argument tails described above.
192Because
193.B va_list
194objects are hard to copy,
195the keyword-argument support library consistently passes
196.B va_list
197objects
198.I by reference
199throughout its programming interface.
200.hP 3.
201Indirectly, through a vector of
202.B struct kwval
203objects,
204each of which contains
205a keyword name, as a pointer to a null-terminated string, and
206the
207.I address
208of a corresponding argument value.
209(This indirection is necessary so that
210the items in the vector can be of uniform size.)
211Argument vectors are rather inconvenient to use,
212but are the only practical way in which a caller can decide at runtime
213which arguments to include in a call,
214which is useful when writing wrapper functions.
4f634d20
MW
215.PP
216Perhaps surprisingly,
217keyword arguments have a relatively small performance impact.
218On the author's aging laptop,
219a call to a simple function,
220passing two out of three keyword arguments,
221takes about 30 cycles longer than
222calling a standard function which just takes integer arguments.
223On the other hand,
224quite a lot of code is involved in decoding keyword arguments,
225so code size will naturally suffer.
9e91c8e7
MW
226.
227.SS Type definitions
228The header file defines two simple structure types.
229.PP
230.IP
231.nf
232.ft B
233struct kwval {
234 const char *kw;
235 const void *val;
236};
237.fi
238.PP
239The
240.B kwval
241structure describes a keyword argument name/value pair.
242The
243.B kw
244member points to the name,
245as a null-terminated string.
246The
247.B val
248member always contains the
249.I address
250of the value.
251(This somewhat inconvenient arrangement
252makes the size of a
253.B kwval
254object independent of the actual argument type.)
255.PP
256.IP
257.nf
258.ft B
259struct kwtab {
260 const struct kwval *v;
261 size_t n;
262};
263.fi
264.PP
265The
266.B kwtab
267structure describes a list of keyword arguments,
268represented as a vector of
269.B kwval
270structures.
271The
272.B v
273member points to the start of the vector;
274the
275.B n
276member contains the number of elements in the vector.
277.PP
278The
279.B kw_unkhookfn
280type is the type of
281unknown-keyword handler functions.
282See the descriptions of
283.B kw_unknown
284and
285.B kw_unkhook
286below.
287.
288.SS Calling functions with keyword arguments
289Functions which accept keyword arguments are ordinary C functions
290with variable-length argument tails.
291Hence, they can be called using ordinary C (of the right kind)
292and all will be well.
293However, argument lists must follow certain rules
294(which will be described in full below);
295failure to do this will result in
296.IR "undefined behaviour" .
297The header file provides integration with some C compilers
298in the form of macros which can be used to help the compiler diagnose
299errors in calls to keyword-accepting functions;
300but such support is rather limited at the moment.
301Some additional macros are provided for use in calls to such functions,
302and it is recommended that, where possible, these are used.
303In particular, it's all too easy to forget the trailing null terminator
304which marks the end of a list of keyword arguments.
305.PP
306That said, the underlying machinery is presented first,
307and the convenience macros are described later.
308.PP
309The argument tail,
310following the mandatory arguments,
311consists of a sequence of zero or more alternating
312keyword names,
313as pointers to null-terminated strings
314(with type
315.BR "const char *" ),
316and their argument values.
317This sequence is finally terminated by a null pointer
318(again with type
319.BR "const char *" )
320in place of a keyword name.
321.PP
322Each function may define for itself which keyword names it accepts,
323and what types the corresponding argument values should have.
324There are also (currently) three special keyword names.
325.TP
326.B kw.valist
327This special keyword is followed by a pointer to
328a variable-length argument tail cursor object, of type
329.BR "va_list *" .
330This cursor object will be modified as the function extracts
331successive arguments from the tail.
332The argument tail should consist of alternating
333keyword names and argument values, as described above,
334including the first keyword name.
335(This is therefore different from the convention used when calling
336keyword argument parser functions:
337see the description of the
338.B KW_PARSEFN
339macro below for more details about these.)
340The argument tail may itself contain the special keywords.
341.TP
342.B kw.tab
343This special keyword is followed by
344.I two
345argument values:
346a pointer to the base of a vector of
347.B kwval
348structures,
349and the number of elements in this vector
350(as a
351.BR size_t ).
352Each element of the vector describes a single keyword argument:
353the
354.B kw
355member points to the keyword's name, and
356the
357.B val
358member points to the value.
359The vector may contain special keywords.
360The
361.B val
362pointer for a
363.B kw.valist
364argument should contain the address of an object of type
365.B "va_list *"
366(and not point directly to the cursor object,
367since
368.B val
369is has type
370.B "const void *"
371but the cursor will be modified as its argument tail is traversed).
372The
373.B val
374pointer for a
375.B kw.tab
376argument should contain the address of a
377.B kwtab
378structure which itself contains the base address and length of
379the argument vector to be processed.
380.TP
381.B kw.unknown
382This keyword is never accepted by any function.
383If it is encountered,
384the
385.B kw_unknown
386function is called to report the situation as an error;
387see below.
388.PP
389It is possible to construct a circular structure
390of indirect argument lists
391(in a number of ways).
392Don't try to pass such a structure to a function:
393the result will be unbounded recursion
394or some other bad outcome.
395.PP
396The macro
397.BI "KWARGS(" body ")"
398wraps up a sequence of keyword arguments.
399The single
400.I body
401argument consists of a sequence of calls to
402the keyword-argument macros described below,
403one after another without any separation.
404.PP
405In C89, macro actual arguments are not permitted to be empty;
406if there are no keyword arguments to provide,
407then the argument-less macro
408.B NO_KWARGS
409should be used instead.
410If you're using C99 or later,
411it's fine to just write
412.B KWARGS()
413instead.
414.PP
415The following keyword-argument macros can be used
416within
417.BR KWARGS 's
418.I body
419argument.
420.TP
421.BI "K(" name ", " value ")"
422Passes a keyword name and its corresponding value,
423as a pair of arguments.
424The
425.I name
426should be a single identifier
427(not a quoted string).
428The
429.I value
430may be any C expression
431of the appropriate type.
432.TP
433.BI "K_VALIST(" ap ")"
434Passes an indirect variable-length argument tail.
435The argument
436.I ap
437should be an lvalue of type
438.B va_list
439which will be passed by reference.
440.TP
441.BI "K_TAB(" v ", " n ")"
442Passes a vector of keyword arguments.
443The argument
444.I v
445should be the base address of the vector, and
446.I n
447should be the number of elements in the vector.
448.
449.SS Defining functions with keyword arguments
450A
451.I "keyword set"
452defines the collection of keyword arguments
453accepted by a particular function.
454The same keyword set may be used by several functions.
455(If your function currently accepts no keyword arguments,
456but you plan to add some later,
457do not define a keyword set,
458and use the
459.B KWPARSE_EMPTY
460macro described below.)
461.PP
462Each keyword set has a name,
463which is a C identifier.
464It's good to choose meaningful and distinctive names for keyword sets.
465Keyword set names are meaningful at runtime:
466they are used as part of the
467.B kw_unknown
468protocol (described below),
469and may be examined by handler functions,
470or reported to a user in error messages.
471For a keyword set which is used only by a single function,
472it is recommended that the set be given the same name as the function.
473.PP
474The keyword arguments for a keyword set named
475.I set
476are described by a `list macro' named
477.IB set _KWSET \fR.
478This macro takes a single argument,
479conventionally named
480.RB ` _ '.
481It should expand to a sequence of one or more list items of the form
482.IP
483.BI "_(" type ", " name ", " default ")"
484.PP
485with no separation between them.
486.PP
487For example:
488.IP
489.nf
490.ft B
491#define example_KWSET(_) \e
492.in +4m
493_(int, x, 0) \e
494_(const char *, y, NULL)
495.fi
496.ft P
497.PP
498Each
499.I name
500should be a distinct C identifier;
501they will be used to name structure members.
502An argument
503.I name
504should not end with the suffix
505.RB ` _suppliedp '
506(for reasons which will soon become apparent).
507.PP
508Each
509.I type
510should be a C
511.I type-name
512such that
513.IP
514.IB type " " name ;
515.PP
516is a valid declaration:
517so it may consist of declaration specifiers and
518(possibly qualified) pointer declarator markers,
519but not array or function markers
520(since they must be placed after the
521.IR name ).
522This is the same requirement made by the standard
523.BR va_arg (3)
524macro.
525.PP
526Each
527.I default
528should be an initializer expression
529or brace-enclosed list,
530suitable for use in an aggregate initializer
531for a variable with automatic storage duration.
532(In C89, aggregate initializers may contain only constant expressions;
533this restriction was lifted in C99.)
534.PP
535The macro
536.B KWTAIL
537is expected to be used at the end of function parameter type list
538to indicate that the function accepts keyword arguments;
539if there are preceding mandatory arguments
540then the
541.B KWTAIL
542marker should be separated from them with a comma
543.RB ` , '.
544(It is permitted for a function parameter type list to contain
545only a
546.B KWTAIL
547marker.)
548.PP
549Specifically,
550the macro declares a mandatory argument
551.B const char *kwfirst_
552(to collect the first keyword name),
553and a variable-length argument tail.
554.PP
555The macro
556.B KWPARSE
557(described below)
558assumes that the enclosing function's argument list ends with a
559.B KWTAIL
560marker.
561The marker should be included both in the function's definition and
562in any declarations, e.g., in the corresponding header file.
563.PP
564The
565.B KWCALL
566macro acts as a declaration specifier for
567functions which accept keyword arguments.
568Its effect is to arrange for the compiler to check,
569as far as is possible,
570that calls to the function are well-formed
571according to the keyword-argument rules.
572The exact checking performed depends on the compiler's abilities
573(and how well supported the compiler is):
574it may check that every other argument is a string;
575it may check that the list is terminated with a null pointer;
576it may not do anything at all.
577Again, this marker should be included in a function's definition and
578in any declarations.
579.PP
580The
581.B KWSET_STRUCT
582macro defines a
583.IR "keyword structure" .
584If
585.I set
586is a keyword-set name then
587.IP
588.BI "KWSET_STRUCT(" set ");"
589.PP
590declares a structure
591.B struct
592.IB set _kwargs \fR.
593For each argument defined in the keyword set,
594this structure contains two members:
595one has exactly the
596.I name
597and
598.I type
599listed in the keyword set definition;
600the other is a 1-bit-wide bitfield of type
601.B "unsigned int"
602named
603.IB name _suppliedp \fR.
604.PP
605The macro
606.B KWDECL
607declares and initializes a keyword argument structure variable.
608If
609.I set
610is a keyword-set name then
611.IP
612.I declaration-specifiers
613.BI "KWDECL(" set ", " kw ");"
614.PP
615declares a variable of type
616.B struct
617.IB set _kwargs
618named
619.IR kw .
620The optional
621.I declaration-specifiers
622may provide additional storage-class,
623qualifiers,
624or other declaration specifiers.
625The
626.RB ` _suppliedp '
627flags are initialized to zero;
628the other members are initialized with the corresponding defaults
629from the keyword-set definition.
630.PP
631The macro
632.B KWSET_PARSEFN
633defines a keyword argument
634.IR "parser function" .
635If
636.I set
637is a keyword-set name then
638.IP
639.I declaration-specifiers
640.BI "KWSET_PARSEFN(" set ")"
641.PP
642(no trailing semicolon!)
643defines a function
644.IP
645.B void
646.IB set _kwparse( \c
647.t(
648.BI "struct " set "_kwargs *" kw ","
649.br
650.BI "const char *" kwfirst ", va_list *" ap ","
651.br
652.BI "const struct kwval *" v ", size_t " n ");"
653.t)
654.PP
655The macro call can
656(and usually will)
657be preceded by storage class specifiers such as
658.BR static ,
659for example to adjust the linkage of the name.
660(I don't recommend declaring parser functions
661.BR inline :
662parser functions are somewhat large, and
663modern compilers are pretty good at
664figuring out whether to inline static functions.)
665.PP
666The function's behaviour is as follows.
667It parses keyword arguments from
668a variable-length argument tail, and/or
669a vector of
670.B kwval
671structures.
672When a keyword argument is recognized,
673for some keyword
674.IR name ,
675the keyword argument structure pointed to by
676.I kw
677is updated:
678the flag
679.IB name _suppliedp
680is set to 1;
681and the argument value is stored (by simple assignment) in the
682.I name
683member.
684Hence, if the
685.RB ` _suppliedp '
686members are initialized to zero,
687the caller can determine which keyword arguments were supplied.
688It is not possible to discover whether two or more arguments
689have the same keyword:
690in this case,
691the value from the last such argument is left
692in the keyword argument structure,
693and any values from earlier arguments are lost.
694(For this purpose,
695the argument vector
696.I v
697is scanned
698.I after
699the variable-length argument tail captured in
700.IR ap .)
701.PP
702The variable-argument tail is read from the list described by
703.BI * ap \fR.
704The argument tail is expected to consist of alternating
705keyword strings (as ordinary null-terminated strings)
706and the corresponding values,
707terminated by a null pointer of type
708.B "const char *"
709in place of a keyword;
710except that the first keyword
711(or terminating null pointer, if no arguments are provided)
712is expected to have been extracted already
713and provided as the
714.I kwfirst
715argument;
716the first argument retrieved using the
717.B va_list
718cursor object should then be the value
719corresponding to the keyword named by
720.IR kwfirst .
721(This slightly unusual convention makes it possible for a function to
722collect the first keyword as a separate mandatory argument,
723which is essential if there are no other mandatory arguments.
724It also means that the compiler will emit a diagnostic
725if you attempt to call a function which expects keyword arguments,
726but don't supply any and
727forget the null pointer which terminates the (empty) list.)
728If
729.I kwfirst
730is a null pointer,
731then
732.I ap
733need not be a valid pointer;
734otherwise, the cursor object
735.BI * ap
736will be modified as the function extracts
737successive arguments from the tail.
738.PP
739The keyword vector is read from the vector of
740.B kwval
741structures starting at address
742.I v
743and containing the following
744.I n
745items.
746If
747.I n
748is zero then
749.I v
750need not be a valid pointer.
751.PP
752The function also handles the special
753.B kw.valist
754and
755.B kw.tab
756arguments described above.
757If an unrecognized keyword argument is encountered,
758then
759.B kw_unknown
760is called:
761see below for details.
762.PP
763The
764.B KW_PARSE
765macro invokes a keyword argument parsing function.
766If
767.I set
768is a keyword-set name,
769.I kw
770names a keyword argument structure variable of type
771.B struct
772.IB set _kwargs \fR,
773and
774.I kwfirst
775is the name of the enclosing function's last mandatory argument,
776which must have type
777.BR "const char *" ,
778then
779.IP
780.BI "KW_PARSE(" set ", " kw ", " kwfirst ");"
781.PP
782calls the function
783.IB set _kwparse
784with five arguments:
785the address of the keyword argument structure
786.IR kw ;
787the string pointer
788.IR kwfirst ;
789the address of a temporary argument-tail cursor object of type
790.BR va_list ,
791constructed on the assumption that
792.I kwfirst
793is the enclosing function's final keyword argument;
794a null pointer; and
795the value zero (signifying an empty keyword-argument vector).
796If the variable
797.I kw
798was declared using
799.B KWDECL
800and the function
801.IB set _kwparse
802has been defined using
803.B KWSET_PARSEFN
804then the effect is to parse the keyword arguments passed to the function
805and set the members of
806.I kw
807appropriately.
808.PP
809The macro
810.B KWPARSE
811(note the lack of underscore)
812combines
813.B KWDECL
814and
815.BR KW_PARSE .
816If
817.I set
818is a keyword-set name then
819.IP
820.BI "KWPARSE(" set ");"
821.PP
822declares and initializes a keyword argument structure variable
823with the fixed name
824.BR kw ,
825and parses the keyword arguments provided to the enclosing function,
826storing the results in
827.BR kw .
828It assumes that the first keyword name
829is in an argument named
830.BR kwfirst_ ,
65f822bc 831as set up by the
5d3d5970
MW
832.B KWTAIL
833marker described above.
9e91c8e7
MW
834.PP
835The macro expands both to a variable declaration and a statement:
836in C89, declarations must precede statements,
837so under C89 rules this macro must appear exactly between
838the declarations at the head of a brace-enclosed block
839(typically the function body)
840and the statements at the end.
841This restriction was lifted in C99,
842so the macro may appear anywhere in the function body.
843However, it is recommended that callers avoid taking actions
844which might require cleanup
845before attempting to parse their keyword arguments,
846since keyword argument parsing functions invoke the
847.B kw_unknown
848handler if they encounter an unknown keyword,
849and the calling function will not get a chance
850to tidy up after itself if this happens.
851.PP
852As mentioned above,
853it is not permitted to define an empty keyword set.
854(Specifically, invoking
855.B KWSET_STRUCT
856for an empty keyword set would result in attempting to define
857a structure with no members, which C doesn't allow.)
858On the other hand, keyword arguments are a useful extension mechanism,
859and it's useful to be able to define a function which doesn't
860currently accept any keywords,
861but which might in the future be extended to allow keyword arguments.
862The macros
863.B KW_PARSE_EMPTY
864and
865.B KWPARSE_EMPTY
866are analogues of
867.B KW_PARSE
868and
869.B KWPARSE
870respectively,
871and handle this case.
872These macros take a keyword-set name as an argument,
873but this name is used only in diagnostic messages
874(e.g., if an unknown keyword name is encountered)
875and need not
876(and probably should not)
877correspond to a defined keyword set.
878.PP
879If
880.I set
881is an identifier then
882.IP
883.BI "KW_PARSE_EMPTY(" set ", " kwfirst ");"
884.PP
885calls the function
886.B kw_parseempty
887with five arguments:
888the
889.I set
890name, as a string;
891the string pointer
892.IR kwfirst ;
893the address of a temporary argument-tail cursor object of type
894.BR va_list ,
895constructed on the assumption that
896.I kwfirst
897is the enclosing function's final keyword argument;
898a null pointer; and
899the value zero (signifying an empty keyword-argument vector).
900The effect is to check that the argument tail contains
901no keyword arguments other than the special predefined ones.
902.PP
903If
904.I set
905is an identifier then
906.IP
5d3d5970 907.BI "KWPARSE_EMPTY(" set ");"
9e91c8e7
MW
908.PP
909(note the lack of underscore)
910checks that the enclosing function has been passed
911no keyword arguments other than the special predefined ones.
912It assumes that the function's parameter type list ends with the
913.B KWTAIL
914marker described above.
915.PP
916The
917.B kw_parseempty
918function checks an keyword argument list
919to make sure that contains no keyword arguments
920(other than the special ones described above).
921.PP
922The
923.I set
924argument should point to a null-terminated string:
925this will be reported as the keyword set name to
926.BR kw_unknown ,
927though it need not
928(and likely will not)
929refer to any defined keyword set.
930The remaining arguments are as for
931the keyword parsing functions
932defined by the
933.B KWSET_PARSEFN
934macro.
935.
936.SS "Wrapper functions"
937Most users will not need the hairy machinery involving argument vectors.
938Their main use is in defining
939.IR "wrapper functions" .
940Suppose there is a function
941.I f
942which accepts some keyword arguments,
943and we want to write a function
944.I g
945which accepts the same keywords recognized by
946.I f
947and some additional ones.
948Unfortunately
949.I f
950may behave differently depending on whether or not
951a particular keyword argument is supplied at all, but
952it's not possible to synthesize a valid
953.B va_list
954other than by simply capturing a live argument tail,
955and it's not possible to decide at runtime
956whether or not to include some arguments in a function call.
957It's still possible to write
958.IR g ,
959by building a vector of keyword arguments,
960collected one-by-one depending on the corresponding
961.RB ` _suppliedp '
962flags (see below).
963A few macros are provided to make this task easier.
964.PP
965The macro
966.B KW_COUNT
967returns the number of keywords defined in a keyword set.
968If
969.I set
970is a keyword-set name, then
971.IP
972.BI "KW_COUNT(" set ")"
973.PP
974returns the number of keywords defined by
975.IR set ,
976as a constant expression of type
977.BR "unsigned int" .
978.PP
979The macro
980.B KW_COPY
981populates a vector of
982.B kwval
983structures from a keyword-argument structure.
984If
985.I fromset
986and
987.I toset
988are two keyword-set names then
989.IP
990.BI "KW_COPY(" fromset ", " toset ", " kw ", " v ", " n ");"
991.PP
992will populate the vector
993.IR v ,
994taking argument values from
995.IR kw .
996The
997.I toset
998must be a subset of
999.IR fromset :
1000i.e., for every keyword defined in
1001.I toset
1002there is a keyword defined in
1003.I fromset
1004with the same name and type.
1005The remaining arguments are as follows:
1006.I kw
1007is a pointer to a
1008.BI "struct " fromset "_kwset"
1009keyword-argument structure which has been filled in,
1010e.g., by the keyword-argument parsing function
1011.IB fromset _kwparse \fR;
1012.I v
1013is a pointer to a sufficiently large vector of
1014.B "struct kwval"
1015objects;
1016and
1017.I n
1018is an lvalue designating an object of integer type.
1019Successive elements of
1020.IR v ,
1021starting at index
1022.IR n ,
1023are filled in to refer to
1024the keyword arguments defined in
1025.I toset
1026whose
1027.RB ` _suppliedp '
1028flag is set in the argument structure pointed to by
1029.IR kw ;
1030for each such argument,
1031a pointer to the keyword name is stored in
1032the corresponding vector element's
1033.B kw
1034member, and
1035a pointer to the argument value,
1036held in the keyword argument structure,
1037is stored in the vector element's
1038.B val
1039member.
1040At the end of this,
1041the index
1042.I n
1043is advanced so as to contain the index of the first unused element of
1044.IR v .
1045Hence, at most
1046.BI KW_COUNT( toset )
1047elements of
1048.I v
1049will be used.
1050.
1051.SS Handling unknown-keyword errors
1052When parsing a variable-length argument tail,
1053it is not possible to continue after
1054encountering an unknown keyword name.
1055This is because it is necessary
1056to know the (promoted) type of the following argument value
1057in order to skip past it;
1058but the only clue provided as to the type is the keyword name,
1059which in this case is meaningless.
1060.PP
1061In this situation,
1062the parser functions generated by
1063.B KW_PARSEFN
1064(and the
1065.B kw_parseempty
1066function)
1067call
1068.BR kw_unknown .
1069This is a function of two arguments:
1070.I set
1071points to the name of the keyword set expected by the caller,
1072as a null-terminated string; and
1073.I kw
1074is the unknown keyword which was encountered.
1075All that
1076.B kw_unknown
1077does is invoke the function whose address is stored in
1078the global variable
1079.B kw_unkhook
1080with the same arguments.
1081The
1082.B kw_unknown
1083function never returns to its caller:
1084if the
1085.B kw_unkhook
1086function returns (which it shouldn't)
1087then
1088.B kw_unknown
1089writes a fatal error message to standard error
1090and calls
1091.BR abort (3).
1092.PP
1093By default
1094.B kw_unkhook
1095points to the function
1096.BR kw_defunknown ,
1097which just writes an error message
1098quoting the keyword set name
1099and offending keyword
1100to standard error
1101and calls
1102.BR abort (3).
1103.PP
1104(In freestanding environments,
1105the behaviour may be somewhat different:
1106porting the library to such environments involves
1107choosing appropriate behaviour for the target platform.)
1108.PP
1109As an example of the kind of special effect
1110which can be achieved using this hook,
1111the following hacking answers whether
1112a function recognizes a particular keyword argument.
1113.IP
1114.nf
1115.ft B
1116#define KWARGS_TEST(k, val) KWARGS(K(k, val) K(kw.unknown, 0))
1117
1118static jmp_buf kw_test_jmp;
1119
1120static void kw_test_unknown(const char *set, const char *kw)
1121{
1122 if (strcmp(kw, "kw.unknown")) longjmp(kw_test_jmp, 1);
1123 else longjmp(kw_test_jmp, 2);
1124}
1125
1126#define KW_TEST(flag, set, call) do { \e
1127 kw_unkhookfn *oldunk = kw_unkhook; \e
1128 kw_unkhook = kw_test_unknown; \e
1129 switch (setjmp(kw_test_jmp)) { \e
1130 case 0: call; abort(); \e
1131 case 1: flag = 1; break; \e
1132 case 2: flag = 0; break; \e
1133 default: abort(); \e
1134 } \e
1135 kw_unkhook = oldunk; \e
1136} while (0)
1137
1138/* Example of use */
1139int f;
1140KW_TEST(f, somefunc(1, "two", 3, KWARGS_TEST("shiny", 68.7)));
1141/* now f is nonzero if `somefunc' accepts the `shiny' keyword
1142 * (which we hope wants a double argument)
1143 */
1144.ft P
1145.fi
1146.
1147.\"--------------------------------------------------------------------------
1148.SH BUGS
1149.
1150The unknown-keyword hook is inadequate for a modern library,
1151but dealing with multiple threads isn't currently possible
1152without writing (moderately complex) system-specific code.
1153The author's intention is that the hook variable
1154.B kw_unkhook
1155be `owned' by some external library
1156which can make its functionality available to client programs
1157in a safer and more convenient way.
1158On Unix-like platforms
1159(including Cygwin)
1160that library will be (a later version) of
1161.BR mLib ;
1162other platforms will likely need different arrangements.
1163The author is willing to coordinate any such efforts.
1164.PP
1165The whole interface is rather clunky.
1166Working with keyword-argument vectors is especially unpleasant.
1167The remarkable thing is not that it's done well,
1168but that it can be done at all.
1169.
1170.\"--------------------------------------------------------------------------
1171.SH SEE ALSO
1172.
1173.BR va_start (3),
1174.BR va_arg (3),
1175.BR va_end (3).
1176.
1177.\"--------------------------------------------------------------------------
1178.SH AUTHOR
1179.
1180Mark Wooding,
1181<mdw@distorted.org.uk>
1182.
1183.\"----- That's all, folks --------------------------------------------------