3 * Copyright (C) 2016 Andre Noll <maan@tuebingen.mpg.de>
5 * Licensed under the LGPL v3, see http://www.gnu.org/licenses/lgpl-3.0.html
18 «Opaque structure which describes a lopsub suite.»,
20 This structure is defined in the .c file which is generated by
21 lopsubgen(1). The corresponding header file exposes a const pointer
22 to a this structure for use in the application.
24 Applications can not de-reference this pointer or access its content
25 directly. They must call one of the accessor functions described below.
30 «Represents one command of a suite.»,
32 A command is identified by a suite and a command number. The symbolic
33 names of all commands defined in a suite are exposed in the enumeration
34 defined in the header file which is generated by lopsubgen(1),
36 Applications can obtain an opaque pointer to a command by calling
37 lls_cmd(), providing the command number and the suite pointer as
43 «Represents one option of a command.»,
45 Similar to a command, an option is identified by a command and an
46 option number. The header file created by the lopsubgen(1) utility
47 provides an enumeration for all options of each command.
49 The lls_opt() function returns an opaque pointer to an option, given
50 a command pointer and an option number.
54 «struct lls_parse_result»,
55 «An argument vector, fully parsed according to a lopsub command.»,
57 A pointer to an opaque structure of this type is returned by
58 lls_parse() if the argument vector was found valid for the given
61 Several functions (described below) take a pointer to such a
62 structure. This enables applications to obtain details about the
63 options and arguments that were given in the argument vector, for
64 example whether an option was specified and how many non-options
65 (aka inputs) were given.
69 «struct lls_opt_result»,
70 «The part of a parse result related to a specific option»,
72 Given an option and a parse result, the lls_opt_result() function
73 returns an opaque pointer to a structure of this type which contains
74 information about the option in the argument vector that was used to
75 create the parse result.
77 A pointer to a structure of this type can be passed to the various
78 accessor functions described below. These functions return information
79 about the option in the argument vector, for example how many times
85 «A strerror-like function for lopsub error codes.»,
87 This works just like strerror(3).
89 «int lss_errno», «positive error code returned from a lopsub library function»
92 «const char *», «points to static memory, must not be freed by the caller»
97 «Parse an argument vector according to a lopsub command.»,
99 This function turns a broken-up command line into a parse result,
100 completely parsing all arguments according to the options defined
101 in the given a lopsub command. As usual, options may be given in any
102 order and the special argument "--" forces an end of option-scanning.
104 For each option defined in the suite, if the multiple flag is set
105 for the option, the parse result contains an array of values, with
106 one value for each time the option was given. Conversely, if the
107 multiple flag is not set, only a single value is stored in the parse
108 result. Those options may still be given multiple times, but only
109 the last given argument is stored while all previous arguments are
112 For options which take an integer value, conversion is performed in
113 a way that recognizes an optional base prefix like "0x". The empty
114 string and strings with trailing non-digit characters result in a
115 parse error. Range violations are detected and also cause the function
118 «int argc», «Usual argument counter.»,
119 «char **argv», «Usual argument vector to parse.»,
120 «const struct lls_command *cmd», «Contains names and characteristics of all allowed options.»,
121 «struct lls_parse_result **lprp», «Result pointer.»,
122 «char **errctx», «Optional error context string, only set on failure»
124 The parse_result pointer returned through lprp can be passed to several
125 accessor functions described below in order to obtain information
126 about the options and arguments in argv[].
128 «int», «Standard (negative error code on failure, non-negative on success).»,
130 On success lprp is initialized according to the options that have been
131 parsed successfully. In this case either errctx or *errctx is NULL,
132 so no cleanup needs to be performed in the caller. However, when the
133 caller is no longer interested in the parse result, it should call
134 lls_free_parse_result() to release the memory that was allocated
135 during the call to lls_parse().
137 On errors, *lprp is set to NULL and the function returns a negative
138 error code. This can happen for various reasons, for example if an
139 invalid option or argument was given. Another possible reason is worth
140 mentioning: when the non-opts-name directive was not specified in the
141 suite, the subcommand is assumed to take no non-option arguments. In
142 this case, lls_parse() fails if the argument vector does contain any
143 non-option arguments.
145 In the error case, if errctx is not NULL, *errctx points to a
146 zero-terminated string which describes the context of the error
147 condition, for example the problematic element of argv[]. The only
148 exception is when an out of memory condition occurs. In this case
149 *errctx may be NULL because the function was unable to allocate
150 the memory needed for the error context. If *errctx is not NULL,
151 the memory it points to should be freed by the caller. However,
152 lls_free_parse_result() need not be called in this case.
156 «lls_free_parse_result»,
157 «Deallocate a parse result.»,
159 This frees the memory space allocated by lls_parse().
161 «struct lls_parse_result *lpr», «As returned by lls_parse().»,
162 «const struct lls_command *cmd»,
163 «This must match the pointer passed earlier to lls_parse().»
165 The parse result pointer must have been returned by a previous
166 call to lls_parse() or lls_serialize_parse_result(). Otherwise, or
167 if lls_free_parse_result has already been called before, undefined
168 behavior occurs. It's OK to pass a NULL pointer though. In this case
169 no action is performed.
176 «Return the long help text of a command.»,
178 The long help text contains the synopsis, the purpose and the help
179 text of the command, followed by the option list including descriptions
180 and help for each option.
182 «const struct lls_command *cmd», «As returned from lls_cmd().»
185 «char *», «A dynamically allocated string that must be freed by the caller.»
190 «Return the short help text of a command.»,
192 This is similar to lls_long_help() but help texts are omitted from
195 «const struct lls_command *cmd», «See lls_long_help().»
198 «char *», «See lls_long_help().»
203 «Tell whether the given string is the name of a subcommand.»,
205 This tries to match the given string against the subcommands of the
206 suite. Exact matches and unique partial matches count as success.
208 «const char *string», «The name to look up.»,
209 «const struct lls_suite *suite», «Contains the command list.»,
210 «char **errctx», «Contains lookup string and the name of the suite.»
213 «int», «The command number on success, negative error code on failure.»,
215 The lookup fails if (a) the given string pointer is NULL, or (b) if
216 the string is no prefix of any subcommand of the suite, or (c) if it
217 is a proper prefix of more than one subcommand.
219 On success the error context pointer is set to NULL. In the error case,
220 if errctx is not NULL, *errctx is pointed to a string that must be
226 «Return a pointer to a command structure.»,
228 Applications usually call this at the beginning of each function that
229 implements a lopsub command (aka command handler). The returned
230 pointer serves as an abstract reference to the command. This
231 reference is needed to call other functions of the lopsub library,
234 «unsigned cmd_num», «Appropriate enum value from the header file.»,
235 «const struct lls_suite *suite», «Also declared in the header file.»
237 The suite pointer and all valid command numbers are defined in the
238 header file which is generated by lopsubgen(1). Hence this header
239 file must be included from the application to get the name of the
240 suite pointer variable and the command numbers.
242 «const struct lls_command *», «Never returns NULL.»,
244 This function always succeeds if both arguments are valid. That is,
245 the command number is a symbolic constant from the LSG_XXX_COMMANDS
246 enumeration of the header file generated by lopsubgen(1), and the suite
247 pointer equals the pointer that is declared in the same header file.
249 If at least one of the arguments are invalid, the behavior is
255 «Obtain the name of the command, given a command pointer.»,
257 Even in situations where the application knows the name of the command,
258 it is less error-prone to call this function rather than to duplicate
259 the command name in the application.
261 «const struct lls_command *cmd», «As obtained from lls_cmd().»
264 «const char *», «Never returns NULL.»,
266 This function succeeds unless the given command pointer is invalid
267 (was not obtained through an earlier call to lls_cmd() or is NULL),
268 in which case the behavior is undefined. The return pointer refers
269 to static storage that must not be freed by the caller.
274 «Obtain the application-specific data pointer.»,
276 Some applications need to store further information for each subcommand,
277 for example a function pointer which refers to the implementation of
278 the subcommand. The optional user data feature allows the application
279 to define a pointer that can be retrieved by calling this function.
281 Of course storing one function pointer per command could simply be
282 done by defining a suitable array in the application which contains
283 one pointer per (sub)command. However, this approach has the disadvantage
284 that it effectively creates two command lists (one in the suite
285 file and one for the array) that need to be maintained and kept in
286 sync. Moreover, functions can not be declared as static if they are
287 defined in a different source file than the one that defines the array.
289 Therefore, lopsub offers an alternative: The .c file generated by
290 lopsubgen(1) declares one const void * pointer per command. These
291 pointers are marked with the "weak" attribute (a gcc extension, but
292 also available for clang). This instructs the compiler to store the
293 declaration as a weak symbol in the object file. Since the linker
294 does not require weak symbols to be defined, linking succeeds even
295 if the application chooses to not employ the user data feature.
297 To make use of the user data feature, the application needs to define
298 one pointer for each command called lsg_xxx_com_yyy_user_data, where
299 xxx is the name of the suite and yyy the name of the command. A
300 suitable preprocessor macro can make this as simple as EXPORT_CMD(foo).
302 «const struct lls_command *cmd», «As obtained from lls_cmd().»
305 «const void *», «The user data pointer defined in the application.»,
307 If the application did not define a user data pointer for the given
308 command, the function returns NULL.
313 «Extract information about one option from a parse result.»,
315 The returned pointer can be passed to the accessor functions described
316 below. Those functions let the applications tell how many times the
317 option was given and retrieve any argument(s) for the option.
319 «unsigned opt_num», «As declared in the header file.»,
320 «const struct lls_parse_result *lpr», «As returned from lls_parse().»
322 The header file generated by lopsubgen(1) generates for each command
323 an enumeration which declares one option number per option as a
326 «const struct lls_opt_result *», «Never returns NULL.»,
328 If the parse result pointer is invalid (was not returned by
329 lls_parse(), or is NULL), or the option number does not correspond to
330 the command that was used to create the parse result, the behaviour
331 is undefined. Otherwise this function succeeds.
336 «Get a reference to an option, given a command and an option number.»,
338 While an opt_result pointer described above is used to obtain
339 information in an argument vector, the pointer returned by this
340 function allows to obtain information about the option itself.
342 Applications rarely need to care about the option pointer. It
343 is required to get the possible values of an enumeration option
344 though. See lls_enum_string_val().
346 «unsigned opt_num», «See lls_opt_result()»,
347 «const struct lls_command *cmd», «Obtained from lls_cmd().»
350 «const struct lls_option *», «Never returns NULL.»,
352 This function always succeeds if both arguments are
353 valid. Otherwise the behavior is undefined.
358 «Return how many times an option was given.»,
360 This is employed as follows. Applications first call lls_parse() to
361 initialize the parse result, followed by lls_opt_result() to obtain a
362 reference to those parts of the parse result that are related to one
363 specific option. The reference can then be passed to this function
364 to find out how many times the option was given.
366 «const struct lls_opt_result *r», «As returned from lls_opt_result().»
369 «unsigned», «Zero means: Not given at all.»,
371 Even if the multiple flag was not set for the option, the returned
372 value may be greater than one because this flag only affects how many
373 arguments are stored in the parse result.
375 This function succeeds unless the opt_result pointer is invalid
376 (was not returned by lls_opt_result(), or is NULL), in which case
377 the behaviour is undefined.
382 «Retrieve one argument to a string option.»,
384 This function may only be called for options which take an optional or
385 required argument of string type. Enum options (which take as their
386 argument one of a fixed, finite set of possible strings), however,
387 are treated as if the option took an argument of uint32 type. Hence
388 this function must not be called for these options.
390 «unsigned idx», «The index in the array of values.»,
391 «const struct lls_opt_result *r», «As returned from lls_opt_result.»
393 The first argument must be zero if the multiple flag is not set for
394 the option. Otherwise any number between zero and n - 1 (inclusively)
395 may be passed, where n is the number of times the option was given,
396 that is, the return value of lls_opt_given().
398 As as special case, if the option was not given at all (i.e., n == 0),
399 it is still OK to call this function with an index value of zero. In
400 this case, the default value for the option will be returned, or NULL
401 if no default value was specified in the suite.
403 «const char *», «The argument that corresponds to the given index.»,
405 The memory referenced by the return pointer is part of the parse
406 result and must not be freed by the caller. It will be freed when
407 lls_free_parse_result() is called.
409 Undefined behaviour occurs in all of the following cases: (a) the
410 index is out of range, (b) the opt_result pointer is NULL or was
411 not obtained through a previous call to lls_opt_result(), (c) the
412 opt_result pointer corresponds to an option which takes an argument
413 of different type or no argument at all. If none of these conditions
414 apply, the function is guaranteed to succeed.
419 «Retrieve one argument to an option that takes an int32 argument.»,
421 This is like lls_string_val(), but for options which take an optional
422 or required argument of type int32.
424 «unsigned idx», «See lls_string_val()»,
425 «const struct lls_opt_result *r», «See lls_string_val().»
427 As for lls_string_val(), a zero index value is considered a valid
428 input even if the option was not given at all. In this case. the
429 default value is returned, or zero if the option has no default value.
431 «int32_t», «The argument, converted to a 32 bit signed integer.»,
433 Since conversion of the argument to int32_t takes place earlier during
434 lls_parse(), no errors are possible unless the index parameter or the
435 the opt result pointer (or both) are invalid. See above for details.
440 «Retrieve one argument to an option that takes an uint32 argument.»,
442 Identical to lls_int32_val(), except the argument type of the option
443 and the return value are different.
445 For enum options, this is the correct function to call in order
446 to obtain the index into the array of possible values, see
447 lls_enum_string_val() below.
449 «unsigned idx», «See lls_int32_val().»,
450 «const struct lls_opt_result *r», «See lls_int32_val().»
453 «uint32_t», «See lls_int32_val().»
458 «Retrieve one argument to an option that takes an int64 argument.»,
460 Identical to lls_int32_val(), except that this function must be used
461 for options which take a 64 bit signed integer argument.
463 «unsigned idx», «See lls_int32_val().»,
464 «const struct lls_opt_result *r», «See lls_int32_val().»
467 «int64_t», «See lls_int32_val().»
472 «Retrieve one argument to an option that takes an uint64 argument.»,
474 Identical to lls_int32_val(), except that this function must be used
475 for options which take a 64 bit unsigned integer argument.
477 «unsigned idx», «See lls_int32_val().»,
478 «const struct lls_opt_result *r», «See lls_int32_val().»
481 «uint64_t», «See lls_int32_val().»
485 «lls_enum_string_val»,
486 «Get one possible argument value for an option.»,
488 This function must only be called for enum options. That is, options
489 for which the set of possible arguments was defined through the values
490 directive in the suite.
492 «unsigned idx», «Determines which of the possible values to get.»,
493 «const struct lls_option *opt», «As returned by lls_opt().»
495 The possible values of an enum option are a property of the option
496 itself and are thus independent of the command line. Therefore this
497 function expects an option pointer rather than a pointer to an opt
500 The index parameter must be a value between zero and the number of
501 possible values minus one, inclusively. This number is declared as
502 the last member of the enumeration for the option, which is defined
503 of the generated header file.
505 «const char *», «Static memory, must not be freed.»,
507 Behavior is undefined if the given option is not an enum option, a
508 NULL pointer is passed, or if the index value is out of range.
514 «Get the number of non-option arguments.»,
516 In addition to options and their arguments, subcommands may accept
517 any number of additional arguments which are not related to any
518 particular option. For example, file names are frequently passed
519 as such non-option arguments (aka inputs).
521 «const struct lls_parse_result *lpr», «As returned from lls_parse().»
523 Passing a NULL pointer to this function results in undefined behaviour.
525 «unsigned», «Number of non-option arguments.»,
527 This function never fails. See also lls_input(), lls_check_arg_count().
532 «Get a reference to one non-option argument.»,
534 If the argument vector passed to lls_parse() contained non-option
535 arguments, the value of each of them can be obtained by calling
538 «unsigned idx», «The index into the array of non-option arguments.»,
539 «const struct lls_parse_result *lpr», «As returned from lls_parse().»
541 The index must be between zero and n-1, inclusively, where n is the
542 number returned by lls_num_inputs(). The parse_result pointer must have
543 been obtained by an earlier call to lls_parse().
545 «const char *», «Pointer to the corresponding non-option argument.»,
547 If the conditions described above are met, the function is guaranteed
548 to succeed. It will never return a NULL pointer in this case.
554 «Get the version string of the lopsub library.»,
556 The version string is determined at build time from the sha1 of the
557 HEAD git commit or from the name of the top level directory for compiling
558 from a gitweb snapshot.
563 «const char *», «Static storage, must not be freed by the caller.»,
565 The returned string is of the form <tag>-<d>-g<sha1>, where <tag>
566 is the name of the last tagged commit contained in the HEAD commit,
567 <d> is the number of commits between <tag> and HEAD, and <sha1> is the
568 first four hex digits of the hash of the HEAD commit. If the working
569 tree was dirty at compile time, the string "-dirty" is appended to
572 This function always succeeds.
578 «Get the line which describes the purpose of a command.»,
580 This function is useful for applications which need to print
581 their own command summary rather than employ lls_short_help() and
582 lls_long_help(). One reason for this could be that the application
583 has additional per-command informations which should be included in
586 «const struct lls_command *cmd», «Obtained from lls_cmd().»
589 «const char *», «Static storage, must not be freed.»,
591 The returned string is the content of the corresponding directive of
592 the suite file, with leading and trailing whitespace removed.
597 «lls_convert_config»,
598 «Transform the contents of a config file into an argument vector.»,
600 This function scans the given input buffer to compute an (argc,
601 argv) pair which is suitable to be fed to lls_parse(). The function
602 is config-agnostic. That is, it does not know anything about option
603 names and their type.
605 Arguments are separated from the option by whitespace and an optional
606 '=' character. Arguments to string options should be enclosed in double
607 quotes and must not spawn multiple lines. Newline or tab characters
608 may be embedded into the argument string with '\n' and '\t'. To embed
609 a backslash, double it. To embed a quote, prefix it with a backslash.
611 «const char *buf», «Input buffer (content of the config file).»,
612 «size_t nbytes», «The buffer size.»,
613 «const char *subcmd», «NULL means supercommand.»,
614 «char ***result», «Argument vector is returned here.»,
615 «char **errctx», «Error context, see lls_parse().»
617 If a subcommand is specified, only the part of the input buffer which
618 is started by a [subcmd] marker is taken into account. Conversely,
619 if a NULL pointer is passed, only the beginning part until the first
620 section marker will be considered. This allows config files to contain
621 options for the supercommand and subcommands.
623 «int», «Length of the argument vector.»,
625 On success, the number of elements in the computed argument vector
626 is returned. Slot zero of the argument vector is initialized to a
627 dummy value while the remaining values correspond to the options and
628 arguments found in the input buffer. The argument vector should be
629 freed with lls_free_argv() when it is no longer needed.
631 On failure a negative error code is returned and *result is set to
632 NULL. Several types of failure are possible, including allocation
633 failure, errors from the lexer and various syntax errors.
639 «Deallocate an argument vector.»,
641 lls_convert_config() dynamically allocates memory for the argument
642 and for each of its elements. This function frees this memory.
644 «char **argv», «The argument vector to free.»
646 It's OK to pass a NULL pointer, in which case the function does
653 «lls_check_arg_count»,
654 «Check the number of non-option arguments.»,
656 This helper verifies that the number of non-option arguments lies
657 within the specified range. Although this function is kind of trivial,
658 it can help applications to provide nice and consistent error messages.
660 «const struct lls_parse_result *lpr», «As obtained from lls_parse().»,
661 «int min_argc», «Lower bound on the number of non-option arguments.»,
662 «int max_argc», «Upper bound on the number of non-option arguments.»,
663 «char **errctx», «Describes the range violation, only set on failure.»
665 For the function to succeed, the number of non-option arguments (as
666 returned by lls_num_inputs()) must be greater or equal to min_argc
667 and less or equal to max_argc.
669 Both min_argc and max_argc may be zero (but not negative), and min_argc
670 must be less or equal to max_argc. The value INT_MAX for max_argc
671 indicates that the number of allowed non-option arguments is unbounded.
673 «int», «Standard. The only possible error is -LLS_E_BAD_ARG_COUNT.»,
677 min_argc = 0, max_argc = 0: no non-option argument may be given.
679 min_argc = 0, max_argc = INT_MAX: any number of non-option arguments OK.
681 min_argc = 1, max_argc = 2: either one or two non-option arguments OK.
683 Behaviour is undefined if min_argc or max_argc is negative, if min_argc
684 is greater than max_argc, or if lpr is invalid.
689 «lls_serialize_parse_result»,
690 «Create a buffer from a parse result.»,
692 This function is handy for passing the result from lls_parse() to a
695 «const struct lls_parse_result *lpr», «The parse result to serialize.»,
696 «const struct lls_command *cmd», «Must point to the command used to create the parse result.»,
697 «char **result», «The serialized parse result.»,
698 «size_t *nbytes», «The size of the serialized buffer.»
700 Depending on the initial value of the result pointer, the function
703 (a) If result is NULL, the size required to store the serialized
704 buffer is computed and returned through the nbytes argument, but no
705 serialization takes place.
707 (b) If result is not NULL, but *result is NULL, a suitable buffer is
708 allocated with malloc() and *result is pointed at this buffer. The
709 caller is responsible for freeing this buffer when it is no longer
712 (c) If *result is not NULL, the buffer pointed at by *result is assumed
713 be be large enough for the serialized parse result, and this buffer
714 is used to store the result.
718 See also: lls_deserialize_parse_result().
723 «lls_deserialize_parse_result»,
724 «Initialize a parse result from a buffer.»,
726 This is the counterpart to lls_serialize_parse_result().
728 «const char *buf», «The buffer to de-serialize.»,
729 «const struct lls_command *cmd», «Must match the pointer used for serializing.»,
730 «struct lls_parse_result **lprp», «Result pointer.»
732 The input buffer should have been obtained through an earlier call
733 to lls_serialize_parse_result().
737 On success all fields of lpr match the original values. After the
738 call, no fields of *lprp contain references to buf, so buf may safely
745 «Combine two parse results, creating an effective configuration.»,
747 This is useful for applications which receive options from the command
748 line and the configuration file.
750 «const struct lls_parse_result *primary», «From command line options.»,
751 «const struct lls_parse_result *secondary», «From config file.»,
752 «const struct lls_command *cmd», «Common command for both parse results.»,
753 «struct lls_parse_result **lprp», «Effective configuration is returned here.»,
754 «char **errctx», «Error context, see lls_parse().»
756 Merging works on a per-option basis as follows. If the multiple flag
757 is set for the option, the argument arrays of the primary and the
758 secondary parse result are concatenated and the concatenation is the
759 argument array for the merge result. It the multiple flag is not set,
760 the value of the primary parse result becomes the argument for the
763 The two non-option argument arrays are concatenated in the same way
764 as the arguments to options with the multiple flag set.
766 All arguments are copied from the two input parse results. It is safe
767 to free them after the function returns. The merge result should be
768 freed with lls_parse_result() when it is no longer needed.
772 The only possible error is an out of memory condition. However,
773 behaviour is undefined if the primary or secondary parse result is
774 NULL, or was not obtained from the given command.
779 «lls_dump_parse_result»,
780 «Create contents of a configuration file from a parse result.»,
782 The subcommand marker ([subcommand]) is not included in the output.
784 «const struct lls_parse_result *lpr», «As obtained from lls_parse() or lls_merge().»,
785 «const struct lls_command *cmd», «Subcommand or supercommand.»,
786 «bool non_default_only», «Only include option values that differ from the default.»
788 If non_default_only is false, options are included in the dump even
789 if they are not given in the parse result. However, flag options are
790 excluded in this case as well as options which take an argument for
791 which no default value has been defined.
793 «char *», «Must be freed by the caller.»,
795 If no options are given, or if every option argument of the parse
796 result matches the default value of the option and non_default_only
797 is true, the function returns the empty string.
799 The only possible error is an out of memory condition, in which case
800 the NULL pointer is returned. Behaviour is undefined if any of the
801 pointer arguments is NULL, or if the parse result does not match the
805 VERBATIM_C(«#endif /* _LOPSUB_H */»)