chiark / gitweb /
doc/runtime.tex, lib/keyword.3: Explain the other benefit of `NO_KWARGS'.
[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
630d9305
MW
405If there are no keyword arguments,
406use the argument-less macro
9e91c8e7 407.B NO_KWARGS
9e91c8e7 408instead.
630d9305
MW
409There are two reasons for this.
410.hP \*o
411C89 doesn't permit empty macro arguments for some reason,
412so
413.B NO_KWARGS
414is necessary when using a C89 compiler.
415.hP \*o
416Omitting the null terminator is a common mistake,
417so
418.B <keyword.h>
419tries to get the compiler to warn if you miss it.
420However, the
421.B KWTAIL
422macro introduces an extra real argument
423.BR kwfirst_ ,
424because it's not possible to scan a variable-length argument tail
425if there are no mandatory arguments.
426If you use
427.BR KWARGS() ,
428with an empty argument list,
429then the null terminator is passed as
430.B kwfirst_
431and the variable-length tail ends up empty,
432which might trigger a compiler warning
433about the missing terminator.
434.B NO_KWARGS
435passes
436.I two
437null terminators:
438a real one to indicate that there are no keyword arguments,
439and a dummy one to placate the compiler.
9e91c8e7
MW
440.PP
441The following keyword-argument macros can be used
442within
443.BR KWARGS 's
444.I body
445argument.
446.TP
447.BI "K(" name ", " value ")"
448Passes a keyword name and its corresponding value,
449as a pair of arguments.
450The
451.I name
452should be a single identifier
453(not a quoted string).
454The
455.I value
456may be any C expression
457of the appropriate type.
458.TP
459.BI "K_VALIST(" ap ")"
460Passes an indirect variable-length argument tail.
461The argument
462.I ap
463should be an lvalue of type
464.B va_list
465which will be passed by reference.
466.TP
467.BI "K_TAB(" v ", " n ")"
468Passes a vector of keyword arguments.
469The argument
470.I v
471should be the base address of the vector, and
472.I n
473should be the number of elements in the vector.
474.
475.SS Defining functions with keyword arguments
476A
477.I "keyword set"
478defines the collection of keyword arguments
479accepted by a particular function.
480The same keyword set may be used by several functions.
481(If your function currently accepts no keyword arguments,
482but you plan to add some later,
483do not define a keyword set,
484and use the
485.B KWPARSE_EMPTY
486macro described below.)
487.PP
488Each keyword set has a name,
489which is a C identifier.
490It's good to choose meaningful and distinctive names for keyword sets.
491Keyword set names are meaningful at runtime:
492they are used as part of the
493.B kw_unknown
494protocol (described below),
495and may be examined by handler functions,
496or reported to a user in error messages.
497For a keyword set which is used only by a single function,
498it is recommended that the set be given the same name as the function.
499.PP
500The keyword arguments for a keyword set named
501.I set
502are described by a `list macro' named
503.IB set _KWSET \fR.
504This macro takes a single argument,
505conventionally named
506.RB ` _ '.
507It should expand to a sequence of one or more list items of the form
508.IP
509.BI "_(" type ", " name ", " default ")"
510.PP
511with no separation between them.
512.PP
513For example:
514.IP
515.nf
516.ft B
517#define example_KWSET(_) \e
518.in +4m
519_(int, x, 0) \e
520_(const char *, y, NULL)
521.fi
522.ft P
523.PP
524Each
525.I name
526should be a distinct C identifier;
527they will be used to name structure members.
528An argument
529.I name
530should not end with the suffix
531.RB ` _suppliedp '
532(for reasons which will soon become apparent).
533.PP
534Each
535.I type
536should be a C
537.I type-name
538such that
539.IP
540.IB type " " name ;
541.PP
542is a valid declaration:
543so it may consist of declaration specifiers and
544(possibly qualified) pointer declarator markers,
545but not array or function markers
546(since they must be placed after the
547.IR name ).
548This is the same requirement made by the standard
549.BR va_arg (3)
550macro.
551.PP
552Each
553.I default
554should be an initializer expression
555or brace-enclosed list,
556suitable for use in an aggregate initializer
557for a variable with automatic storage duration.
558(In C89, aggregate initializers may contain only constant expressions;
559this restriction was lifted in C99.)
560.PP
561The macro
562.B KWTAIL
563is expected to be used at the end of function parameter type list
564to indicate that the function accepts keyword arguments;
565if there are preceding mandatory arguments
566then the
567.B KWTAIL
568marker should be separated from them with a comma
569.RB ` , '.
570(It is permitted for a function parameter type list to contain
571only a
572.B KWTAIL
573marker.)
574.PP
575Specifically,
576the macro declares a mandatory argument
577.B const char *kwfirst_
578(to collect the first keyword name),
579and a variable-length argument tail.
580.PP
581The macro
582.B KWPARSE
583(described below)
584assumes that the enclosing function's argument list ends with a
585.B KWTAIL
586marker.
587The marker should be included both in the function's definition and
588in any declarations, e.g., in the corresponding header file.
589.PP
590The
591.B KWCALL
592macro acts as a declaration specifier for
593functions which accept keyword arguments.
594Its effect is to arrange for the compiler to check,
595as far as is possible,
596that calls to the function are well-formed
597according to the keyword-argument rules.
598The exact checking performed depends on the compiler's abilities
599(and how well supported the compiler is):
600it may check that every other argument is a string;
601it may check that the list is terminated with a null pointer;
602it may not do anything at all.
603Again, this marker should be included in a function's definition and
604in any declarations.
605.PP
606The
607.B KWSET_STRUCT
608macro defines a
609.IR "keyword structure" .
610If
611.I set
612is a keyword-set name then
613.IP
614.BI "KWSET_STRUCT(" set ");"
615.PP
616declares a structure
617.B struct
618.IB set _kwargs \fR.
619For each argument defined in the keyword set,
620this structure contains two members:
621one has exactly the
622.I name
623and
624.I type
625listed in the keyword set definition;
626the other is a 1-bit-wide bitfield of type
627.B "unsigned int"
628named
629.IB name _suppliedp \fR.
630.PP
631The macro
632.B KWDECL
633declares and initializes a keyword argument structure variable.
634If
635.I set
636is a keyword-set name then
637.IP
638.I declaration-specifiers
639.BI "KWDECL(" set ", " kw ");"
640.PP
641declares a variable of type
642.B struct
643.IB set _kwargs
644named
645.IR kw .
646The optional
647.I declaration-specifiers
648may provide additional storage-class,
649qualifiers,
650or other declaration specifiers.
651The
652.RB ` _suppliedp '
653flags are initialized to zero;
654the other members are initialized with the corresponding defaults
655from the keyword-set definition.
656.PP
657The macro
658.B KWSET_PARSEFN
659defines a keyword argument
660.IR "parser function" .
661If
662.I set
663is a keyword-set name then
664.IP
665.I declaration-specifiers
666.BI "KWSET_PARSEFN(" set ")"
667.PP
668(no trailing semicolon!)
669defines a function
670.IP
671.B void
672.IB set _kwparse( \c
673.t(
674.BI "struct " set "_kwargs *" kw ","
675.br
676.BI "const char *" kwfirst ", va_list *" ap ","
677.br
678.BI "const struct kwval *" v ", size_t " n ");"
679.t)
680.PP
681The macro call can
682(and usually will)
683be preceded by storage class specifiers such as
684.BR static ,
685for example to adjust the linkage of the name.
686(I don't recommend declaring parser functions
687.BR inline :
688parser functions are somewhat large, and
689modern compilers are pretty good at
690figuring out whether to inline static functions.)
691.PP
692The function's behaviour is as follows.
693It parses keyword arguments from
694a variable-length argument tail, and/or
695a vector of
696.B kwval
697structures.
698When a keyword argument is recognized,
699for some keyword
700.IR name ,
701the keyword argument structure pointed to by
702.I kw
703is updated:
704the flag
705.IB name _suppliedp
706is set to 1;
707and the argument value is stored (by simple assignment) in the
708.I name
709member.
710Hence, if the
711.RB ` _suppliedp '
712members are initialized to zero,
713the caller can determine which keyword arguments were supplied.
714It is not possible to discover whether two or more arguments
715have the same keyword:
716in this case,
717the value from the last such argument is left
718in the keyword argument structure,
719and any values from earlier arguments are lost.
720(For this purpose,
721the argument vector
722.I v
723is scanned
724.I after
725the variable-length argument tail captured in
726.IR ap .)
727.PP
728The variable-argument tail is read from the list described by
729.BI * ap \fR.
730The argument tail is expected to consist of alternating
731keyword strings (as ordinary null-terminated strings)
732and the corresponding values,
733terminated by a null pointer of type
734.B "const char *"
735in place of a keyword;
736except that the first keyword
737(or terminating null pointer, if no arguments are provided)
738is expected to have been extracted already
739and provided as the
740.I kwfirst
741argument;
742the first argument retrieved using the
743.B va_list
744cursor object should then be the value
745corresponding to the keyword named by
746.IR kwfirst .
747(This slightly unusual convention makes it possible for a function to
748collect the first keyword as a separate mandatory argument,
749which is essential if there are no other mandatory arguments.
750It also means that the compiler will emit a diagnostic
751if you attempt to call a function which expects keyword arguments,
752but don't supply any and
753forget the null pointer which terminates the (empty) list.)
754If
755.I kwfirst
756is a null pointer,
757then
758.I ap
759need not be a valid pointer;
760otherwise, the cursor object
761.BI * ap
762will be modified as the function extracts
763successive arguments from the tail.
764.PP
765The keyword vector is read from the vector of
766.B kwval
767structures starting at address
768.I v
769and containing the following
770.I n
771items.
772If
773.I n
774is zero then
775.I v
776need not be a valid pointer.
777.PP
778The function also handles the special
779.B kw.valist
780and
781.B kw.tab
782arguments described above.
783If an unrecognized keyword argument is encountered,
784then
785.B kw_unknown
786is called:
787see below for details.
788.PP
789The
790.B KW_PARSE
791macro invokes a keyword argument parsing function.
792If
793.I set
794is a keyword-set name,
795.I kw
796names a keyword argument structure variable of type
797.B struct
798.IB set _kwargs \fR,
799and
800.I kwfirst
801is the name of the enclosing function's last mandatory argument,
802which must have type
803.BR "const char *" ,
804then
805.IP
806.BI "KW_PARSE(" set ", " kw ", " kwfirst ");"
807.PP
808calls the function
809.IB set _kwparse
810with five arguments:
811the address of the keyword argument structure
812.IR kw ;
813the string pointer
814.IR kwfirst ;
815the address of a temporary argument-tail cursor object of type
816.BR va_list ,
817constructed on the assumption that
818.I kwfirst
819is the enclosing function's final keyword argument;
820a null pointer; and
821the value zero (signifying an empty keyword-argument vector).
822If the variable
823.I kw
824was declared using
825.B KWDECL
826and the function
827.IB set _kwparse
828has been defined using
829.B KWSET_PARSEFN
830then the effect is to parse the keyword arguments passed to the function
831and set the members of
832.I kw
833appropriately.
834.PP
835The macro
836.B KWPARSE
837(note the lack of underscore)
838combines
839.B KWDECL
840and
841.BR KW_PARSE .
842If
843.I set
844is a keyword-set name then
845.IP
846.BI "KWPARSE(" set ");"
847.PP
848declares and initializes a keyword argument structure variable
849with the fixed name
850.BR kw ,
851and parses the keyword arguments provided to the enclosing function,
852storing the results in
853.BR kw .
854It assumes that the first keyword name
855is in an argument named
856.BR kwfirst_ ,
65f822bc 857as set up by the
5d3d5970
MW
858.B KWTAIL
859marker described above.
9e91c8e7
MW
860.PP
861The macro expands both to a variable declaration and a statement:
862in C89, declarations must precede statements,
863so under C89 rules this macro must appear exactly between
864the declarations at the head of a brace-enclosed block
865(typically the function body)
866and the statements at the end.
867This restriction was lifted in C99,
868so the macro may appear anywhere in the function body.
869However, it is recommended that callers avoid taking actions
870which might require cleanup
871before attempting to parse their keyword arguments,
872since keyword argument parsing functions invoke the
873.B kw_unknown
874handler if they encounter an unknown keyword,
875and the calling function will not get a chance
876to tidy up after itself if this happens.
877.PP
878As mentioned above,
879it is not permitted to define an empty keyword set.
880(Specifically, invoking
881.B KWSET_STRUCT
882for an empty keyword set would result in attempting to define
883a structure with no members, which C doesn't allow.)
884On the other hand, keyword arguments are a useful extension mechanism,
885and it's useful to be able to define a function which doesn't
886currently accept any keywords,
887but which might in the future be extended to allow keyword arguments.
888The macros
889.B KW_PARSE_EMPTY
890and
891.B KWPARSE_EMPTY
892are analogues of
893.B KW_PARSE
894and
895.B KWPARSE
896respectively,
897and handle this case.
898These macros take a keyword-set name as an argument,
899but this name is used only in diagnostic messages
900(e.g., if an unknown keyword name is encountered)
901and need not
902(and probably should not)
903correspond to a defined keyword set.
904.PP
905If
906.I set
907is an identifier then
908.IP
909.BI "KW_PARSE_EMPTY(" set ", " kwfirst ");"
910.PP
911calls the function
912.B kw_parseempty
913with five arguments:
914the
915.I set
916name, as a string;
917the string pointer
918.IR kwfirst ;
919the address of a temporary argument-tail cursor object of type
920.BR va_list ,
921constructed on the assumption that
922.I kwfirst
923is the enclosing function's final keyword argument;
924a null pointer; and
925the value zero (signifying an empty keyword-argument vector).
926The effect is to check that the argument tail contains
927no keyword arguments other than the special predefined ones.
928.PP
929If
930.I set
931is an identifier then
932.IP
5d3d5970 933.BI "KWPARSE_EMPTY(" set ");"
9e91c8e7
MW
934.PP
935(note the lack of underscore)
936checks that the enclosing function has been passed
937no keyword arguments other than the special predefined ones.
938It assumes that the function's parameter type list ends with the
939.B KWTAIL
940marker described above.
941.PP
942The
943.B kw_parseempty
944function checks an keyword argument list
945to make sure that contains no keyword arguments
946(other than the special ones described above).
947.PP
948The
949.I set
950argument should point to a null-terminated string:
951this will be reported as the keyword set name to
952.BR kw_unknown ,
953though it need not
954(and likely will not)
955refer to any defined keyword set.
956The remaining arguments are as for
957the keyword parsing functions
958defined by the
959.B KWSET_PARSEFN
960macro.
961.
962.SS "Wrapper functions"
963Most users will not need the hairy machinery involving argument vectors.
964Their main use is in defining
965.IR "wrapper functions" .
966Suppose there is a function
967.I f
968which accepts some keyword arguments,
969and we want to write a function
970.I g
971which accepts the same keywords recognized by
972.I f
973and some additional ones.
974Unfortunately
975.I f
976may behave differently depending on whether or not
977a particular keyword argument is supplied at all, but
978it's not possible to synthesize a valid
979.B va_list
980other than by simply capturing a live argument tail,
981and it's not possible to decide at runtime
982whether or not to include some arguments in a function call.
983It's still possible to write
984.IR g ,
985by building a vector of keyword arguments,
986collected one-by-one depending on the corresponding
987.RB ` _suppliedp '
988flags (see below).
989A few macros are provided to make this task easier.
990.PP
991The macro
992.B KW_COUNT
993returns the number of keywords defined in a keyword set.
994If
995.I set
996is a keyword-set name, then
997.IP
998.BI "KW_COUNT(" set ")"
999.PP
1000returns the number of keywords defined by
1001.IR set ,
1002as a constant expression of type
1003.BR "unsigned int" .
1004.PP
1005The macro
1006.B KW_COPY
1007populates a vector of
1008.B kwval
1009structures from a keyword-argument structure.
1010If
1011.I fromset
1012and
1013.I toset
1014are two keyword-set names then
1015.IP
1016.BI "KW_COPY(" fromset ", " toset ", " kw ", " v ", " n ");"
1017.PP
1018will populate the vector
1019.IR v ,
1020taking argument values from
1021.IR kw .
1022The
1023.I toset
1024must be a subset of
1025.IR fromset :
1026i.e., for every keyword defined in
1027.I toset
1028there is a keyword defined in
1029.I fromset
1030with the same name and type.
1031The remaining arguments are as follows:
1032.I kw
1033is a pointer to a
1034.BI "struct " fromset "_kwset"
1035keyword-argument structure which has been filled in,
1036e.g., by the keyword-argument parsing function
1037.IB fromset _kwparse \fR;
1038.I v
1039is a pointer to a sufficiently large vector of
1040.B "struct kwval"
1041objects;
1042and
1043.I n
1044is an lvalue designating an object of integer type.
1045Successive elements of
1046.IR v ,
1047starting at index
1048.IR n ,
1049are filled in to refer to
1050the keyword arguments defined in
1051.I toset
1052whose
1053.RB ` _suppliedp '
1054flag is set in the argument structure pointed to by
1055.IR kw ;
1056for each such argument,
1057a pointer to the keyword name is stored in
1058the corresponding vector element's
1059.B kw
1060member, and
1061a pointer to the argument value,
1062held in the keyword argument structure,
1063is stored in the vector element's
1064.B val
1065member.
1066At the end of this,
1067the index
1068.I n
1069is advanced so as to contain the index of the first unused element of
1070.IR v .
1071Hence, at most
1072.BI KW_COUNT( toset )
1073elements of
1074.I v
1075will be used.
1076.
1077.SS Handling unknown-keyword errors
1078When parsing a variable-length argument tail,
1079it is not possible to continue after
1080encountering an unknown keyword name.
1081This is because it is necessary
1082to know the (promoted) type of the following argument value
1083in order to skip past it;
1084but the only clue provided as to the type is the keyword name,
1085which in this case is meaningless.
1086.PP
1087In this situation,
1088the parser functions generated by
1089.B KW_PARSEFN
1090(and the
1091.B kw_parseempty
1092function)
1093call
1094.BR kw_unknown .
1095This is a function of two arguments:
1096.I set
1097points to the name of the keyword set expected by the caller,
1098as a null-terminated string; and
1099.I kw
1100is the unknown keyword which was encountered.
1101All that
1102.B kw_unknown
1103does is invoke the function whose address is stored in
1104the global variable
1105.B kw_unkhook
1106with the same arguments.
1107The
1108.B kw_unknown
1109function never returns to its caller:
1110if the
1111.B kw_unkhook
1112function returns (which it shouldn't)
1113then
1114.B kw_unknown
1115writes a fatal error message to standard error
1116and calls
1117.BR abort (3).
1118.PP
1119By default
1120.B kw_unkhook
1121points to the function
1122.BR kw_defunknown ,
1123which just writes an error message
1124quoting the keyword set name
1125and offending keyword
1126to standard error
1127and calls
1128.BR abort (3).
1129.PP
1130(In freestanding environments,
1131the behaviour may be somewhat different:
1132porting the library to such environments involves
1133choosing appropriate behaviour for the target platform.)
1134.PP
1135As an example of the kind of special effect
1136which can be achieved using this hook,
1137the following hacking answers whether
1138a function recognizes a particular keyword argument.
1139.IP
1140.nf
1141.ft B
1142#define KWARGS_TEST(k, val) KWARGS(K(k, val) K(kw.unknown, 0))
1143
1144static jmp_buf kw_test_jmp;
1145
1146static void kw_test_unknown(const char *set, const char *kw)
1147{
1148 if (strcmp(kw, "kw.unknown")) longjmp(kw_test_jmp, 1);
1149 else longjmp(kw_test_jmp, 2);
1150}
1151
1152#define KW_TEST(flag, set, call) do { \e
1153 kw_unkhookfn *oldunk = kw_unkhook; \e
1154 kw_unkhook = kw_test_unknown; \e
1155 switch (setjmp(kw_test_jmp)) { \e
1156 case 0: call; abort(); \e
1157 case 1: flag = 1; break; \e
1158 case 2: flag = 0; break; \e
1159 default: abort(); \e
1160 } \e
1161 kw_unkhook = oldunk; \e
1162} while (0)
1163
1164/* Example of use */
1165int f;
1166KW_TEST(f, somefunc(1, "two", 3, KWARGS_TEST("shiny", 68.7)));
1167/* now f is nonzero if `somefunc' accepts the `shiny' keyword
1168 * (which we hope wants a double argument)
1169 */
1170.ft P
1171.fi
1172.
1173.\"--------------------------------------------------------------------------
1174.SH BUGS
1175.
1176The unknown-keyword hook is inadequate for a modern library,
1177but dealing with multiple threads isn't currently possible
1178without writing (moderately complex) system-specific code.
1179The author's intention is that the hook variable
1180.B kw_unkhook
1181be `owned' by some external library
1182which can make its functionality available to client programs
1183in a safer and more convenient way.
1184On Unix-like platforms
1185(including Cygwin)
1186that library will be (a later version) of
1187.BR mLib ;
1188other platforms will likely need different arrangements.
1189The author is willing to coordinate any such efforts.
1190.PP
1191The whole interface is rather clunky.
1192Working with keyword-argument vectors is especially unpleasant.
1193The remarkable thing is not that it's done well,
1194but that it can be done at all.
1195.
1196.\"--------------------------------------------------------------------------
1197.SH SEE ALSO
1198.
1199.BR va_start (3),
1200.BR va_arg (3),
1201.BR va_end (3).
1202.
1203.\"--------------------------------------------------------------------------
1204.SH AUTHOR
1205.
1206Mark Wooding,
1207<mdw@distorted.org.uk>
1208.
1209.\"----- That's all, folks --------------------------------------------------