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