gifsicle  1.92
About: Gifsicle is a UNIX command-line tool for creating, editing, and getting information about GIF images and animations.
  Fossies Dox: gifsicle-1.92.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

clp.c File Reference
#include <lcdf/clp.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <ctype.h>
#include <inttypes.h>
Include dependency graph for clp.c:

Go to the source code of this file.

Data Structures

struct  Clp_ValType
 
struct  Clp_InternOption
 
struct  Clp_Oclass
 
struct  Clp_Internal
 
struct  Clp_ParserState
 
struct  Clp_StringList
 
struct  Clp_BuildString
 

Macros

#define HAVE_STRTOUL   1
 
#define Clp_DoubledLong   (Clp_LongImplicit * 2)
 
#define Clp_InitialValType   8
 
#define MAX_AMBIGUOUS_VALUES   4
 
#define Clp_OptionCharsSize   5
 
#define Clp_OclassSize   10
 
#define U_REPLACEMENT   0xFFFD
 
#define ENSURE_BUILD_STRING(bs, space)
 

Typedefs

typedef struct Clp_Internal Clp_Internal
 
typedef struct Clp_StringList Clp_StringList
 
typedef struct Clp_BuildString Clp_BuildString
 

Functions

static int parse_string (Clp_Parser *, const char *, int, void *)
 
static int parse_int (Clp_Parser *, const char *, int, void *)
 
static int parse_bool (Clp_Parser *, const char *, int, void *)
 
static int parse_double (Clp_Parser *, const char *, int, void *)
 
static int parse_string_list (Clp_Parser *, const char *, int, void *)
 
static int ambiguity_error (Clp_Parser *, int, int *, const Clp_Option *, const Clp_InternOption *, const char *, const char *,...)
 
static char * encode_utf8 (char *s, int n, int c)
 
static int decode_utf8 (const char *s, const char **cp)
 
static int utf8_charlen (const char *s)
 
static int clp_utf8_charlen (const Clp_Internal *cli, const char *s)
 
static int min_different_chars (const char *s, const char *t)
 
static int long_as_short (const Clp_Internal *cli, const Clp_Option *o, Clp_InternOption *io, int failure)
 
static void compare_options (Clp_Parser *clp, const Clp_Option *o1, Clp_InternOption *io1, const Clp_Option *o2, Clp_InternOption *io2)
 
static void calculate_lmm (Clp_Parser *clp, const Clp_Option *opt, Clp_InternOption *iopt, int nopt)
 
Clp_ParserClp_NewParser (int argc, const char *const *argv, int nopt, const Clp_Option *opt)
 Create a new Clp_Parser. More...
 
void Clp_DeleteParser (Clp_Parser *clp)
 Destroy a Clp_Parser object. More...
 
Clp_ErrorHandler Clp_SetErrorHandler (Clp_Parser *clp, void(*errh)(Clp_Parser *, const char *))
 
int Clp_SetUTF8 (Clp_Parser *clp, int utf8)
 Set clp's UTF-8 mode. More...
 
int Clp_OptionChar (Clp_Parser *clp, int c)
 Return clp's treatment of character c. More...
 
int Clp_SetOptionChar (Clp_Parser *clp, int c, int type)
 Set clp's treatment of character c. More...
 
int Clp_SetOptions (Clp_Parser *clp, int nopt, const Clp_Option *opt)
 Set clp's option definitions. More...
 
void Clp_SetArguments (Clp_Parser *clp, int argc, const char *const *argv)
 Set clp's arguments. More...
 
int Clp_SetOptionProcessing (Clp_Parser *clp, int on)
 Set whether clp is searching for options. More...
 
static int argcmp (const char *ref, const char *arg, int min_match, int fewer_dashes)
 
static int find_prefix_opt (Clp_Parser *clp, const char *arg, int nopt, const Clp_Option *opt, const Clp_InternOption *iopt, int *ambiguous, int *ambiguous_values)
 
static int val_type_binsearch (Clp_Internal *cli, int val_type)
 
int Clp_AddType (Clp_Parser *clp, int val_type, int flags, Clp_ValParseFunc parser, void *user_data)
 Define a new value type for clp. More...
 
static int finish_string_list (Clp_Parser *clp, int val_type, int flags, Clp_Option *items, int nitems, int itemscap)
 
int Clp_AddStringListType (Clp_Parser *clp, int val_type, int flags,...)
 Define a new string list value type for clp. More...
 
int Clp_AddStringListTypeVec (Clp_Parser *clp, int val_type, int flags, int nstrs, const char *const *strs, const int *vals)
 Define a new string list value type for clp. More...
 
const char * Clp_ProgramName (Clp_Parser *clp)
 Return clp's program name. More...
 
const char * Clp_SetProgramName (Clp_Parser *clp, const char *name)
 Set clp's program name. More...
 
Clp_ParserStateClp_NewParserState (void)
 Create a new Clp_ParserState. More...
 
void Clp_DeleteParserState (Clp_ParserState *state)
 Destroy a Clp_ParserState object. More...
 
void Clp_SaveParser (const Clp_Parser *clp, Clp_ParserState *state)
 Save clp's current state in state. More...
 
void Clp_RestoreParser (Clp_Parser *clp, const Clp_ParserState *state)
 Restore parser state from state into clp. More...
 
static void set_option_text (Clp_Internal *cli, const char *text, int n_option_chars)
 
static int get_oclass (Clp_Parser *clp, const char *text, int *ocharskip)
 
static int next_argument (Clp_Parser *clp, int want_argument)
 
static void switch_to_short_argument (Clp_Parser *clp)
 
static int find_long (Clp_Parser *clp, const char *arg)
 
static int find_short (Clp_Parser *clp, const char *text)
 
int Clp_Next (Clp_Parser *clp)
 Parse and return the next argument from clp. More...
 
const char * Clp_Shift (Clp_Parser *clp, int allow_options)
 Return the next argument from clp without option parsing. More...
 
static void build_string_program_prefix (Clp_BuildString *bs, const Clp_Parser *clp)
 
static void build_string_init (Clp_BuildString *bs, Clp_Parser *clp)
 
static void build_string_cleanup (Clp_BuildString *bs)
 
static int build_string_grow (Clp_BuildString *bs, size_t want)
 
static void append_build_string (Clp_BuildString *bs, const char *s, int l)
 
static void Clp_vbsprintf (Clp_Parser *clp, Clp_BuildString *bs, const char *fmt, va_list val)
 
static const char * build_string_text (Clp_BuildString *bs, int report_oom)
 
static void do_error (Clp_Parser *clp, Clp_BuildString *bs)
 
int Clp_OptionError (Clp_Parser *clp, const char *format,...)
 Report a parser error. More...
 
int Clp_fprintf (Clp_Parser *clp, FILE *f, const char *format,...)
 Print a message. More...
 
int Clp_vfprintf (Clp_Parser *clp, FILE *f, const char *format, va_list val)
 Print a message. More...
 
int Clp_vsnprintf (Clp_Parser *clp, char *str, size_t size, const char *format, va_list val)
 Format a message. More...
 
static int copy_string (char *buf, int buflen, int bufpos, const char *what)
 
int Clp_CurOptionNameBuf (Clp_Parser *clp, char *buf, int len)
 Extract the current option as a string. More...
 
const char * Clp_CurOptionName (Clp_Parser *clp)
 Extract the current option as a string. More...
 
int Clp_IsLong (Clp_Parser *clp, const char *long_name)
 Test if the current option had long name name. More...
 
int Clp_IsShort (Clp_Parser *clp, int short_name)
 Test if the current option had short name name. More...
 

Variables

static const Clp_Option clp_option_sentinel []
 

Macro Definition Documentation

◆ Clp_DoubledLong

#define Clp_DoubledLong   (Clp_LongImplicit * 2)

Definition at line 148 of file clp.c.

◆ Clp_InitialValType

#define Clp_InitialValType   8

Definition at line 150 of file clp.c.

◆ Clp_OclassSize

#define Clp_OclassSize   10

Definition at line 182 of file clp.c.

◆ Clp_OptionCharsSize

#define Clp_OptionCharsSize   5

Definition at line 176 of file clp.c.

◆ ENSURE_BUILD_STRING

#define ENSURE_BUILD_STRING (   bs,
  space 
)
Value:
((((bs)->end_data - (bs)->pos) >= (space)) \
|| build_string_grow((bs), (bs)->pos - (bs)->data + (space)))

Definition at line 2042 of file clp.c.

◆ HAVE_STRTOUL

#define HAVE_STRTOUL   1

Definition at line 41 of file clp.c.

◆ MAX_AMBIGUOUS_VALUES

#define MAX_AMBIGUOUS_VALUES   4

Definition at line 151 of file clp.c.

◆ U_REPLACEMENT

#define U_REPLACEMENT   0xFFFD

Definition at line 273 of file clp.c.

Typedef Documentation

◆ Clp_BuildString

◆ Clp_Internal

typedef struct Clp_Internal Clp_Internal

◆ Clp_StringList

Function Documentation

◆ ambiguity_error()

static int ambiguity_error ( Clp_Parser clp,
int  ambiguous,
int *  ambiguous_values,
const Clp_Option opt,
const Clp_InternOption iopt,
const char *  prefix,
const char *  fmt,
  ... 
)
static

Definition at line 2354 of file clp.c.

Referenced by parse_string_list().

◆ append_build_string()

static void append_build_string ( Clp_BuildString bs,
const char *  s,
int  l 
)
static

Definition at line 2047 of file clp.c.

Referenced by Clp_vbsprintf().

◆ argcmp()

static int argcmp ( const char *  ref,
const char *  arg,
int  min_match,
int  fewer_dashes 
)
static

Definition at line 909 of file clp.c.

References compare().

◆ build_string_cleanup()

static void build_string_cleanup ( Clp_BuildString bs)
static

Definition at line 2016 of file clp.c.

◆ build_string_grow()

static int build_string_grow ( Clp_BuildString bs,
size_t  want 
)
static

Definition at line 2021 of file clp.c.

◆ build_string_init()

static void build_string_init ( Clp_BuildString bs,
Clp_Parser clp 
)
static

◆ build_string_program_prefix()

static void build_string_program_prefix ( Clp_BuildString bs,
const Clp_Parser clp 
)
static

Definition at line 2058 of file clp.c.

References ENSURE_BUILD_STRING, and Clp_BuildString::pos.

◆ build_string_text()

static const char* build_string_text ( Clp_BuildString bs,
int  report_oom 
)
static

Definition at line 2192 of file clp.c.

◆ calculate_lmm()

static void calculate_lmm ( Clp_Parser clp,
const Clp_Option opt,
Clp_InternOption iopt,
int  nopt 
)
static

◆ Clp_AddStringListType()

int Clp_AddStringListType ( Clp_Parser clp,
int  val_type,
int  flags,
  ... 
)

Define a new string list value type for clp.

Parameters
clpthe parser
val_typevalue type ID
flagsstring list flags
Returns
0 on success, -1 on failure

Defines argument type val_type in parser clp. The parsing function sets clp->val.i to an integer. The value string is matched against strings provided in the ellipsis arguments. For example, the Clp_AddStringListType() call below has the same effect as the Clp_AddStringListTypeVec() call:

For example:

"cattle", 2, "dog", 3, (const char *) NULL);
const char * const strs[] = { "cat", "cattle", "dog" };
const int vals[] = { 1, 2, 3 };
Note
The CLP library will not modify any of the passed-in strings. The calling program must not modify or free them either until the parser is destroyed.

Definition at line 1285 of file clp.c.

Referenced by main().

◆ Clp_AddStringListTypeVec()

int Clp_AddStringListTypeVec ( Clp_Parser clp,
int  val_type,
int  flags,
int  nstrs,
const char *const *  strs,
const int *  vals 
)

Define a new string list value type for clp.

Parameters
clpthe parser
val_typevalue type ID
flagsstring list flags
nstrsnumber of strings in list
strsarray of strings
valsarray of values
Returns
0 on success, -1 on failure

Defines argument type val_type in parser clp. The parsing function sets clp->val.i to an integer. The value string is matched against the strs. If there's a unique match, the corresponding entry from vals is returned. Unique prefix matches also work. Finally, if flags contains the Clp_AllowNumbers flag, then integers are also accepted.

For example:

const char * const strs[] = { "cat", "cattle", "dog" };
const int vals[] = { 1, 2, 3 };

Say that option –animal takes value type 100. Then:

  • –animal=cat will succeed and set clp->val.i = 1.
  • –animal=cattle will succeed and set clp->val.i = 2.
  • –animal=dog will succeed and set clp->val.i = 3.
  • –animal=d will succeed and set clp->val.i = 3.
  • –animal=c will fail, since c is ambiguous.
  • –animal=4 will succeed and set clp->val.i = 4.
Note
The CLP library will not modify the contents of strs or vals. The calling program can modify the strs array, but the actual strings (for instance, strs[0] and strs[1]) must not be modified or freed until the parser is destroyed.

Definition at line 1374 of file clp.c.

◆ Clp_AddType()

int Clp_AddType ( Clp_Parser clp,
int  val_type,
int  flags,
Clp_ValParseFunc  parser,
void *  user_data 
)

Define a new value type for clp.

Parameters
clpthe parser
val_typevalue type ID
flagsvalue type flags
parserparser function
user_datauser data for parser function
Returns
0 on success, -1 on failure

Defines argument type val_type in parser clp. The parsing function parser will be passed argument values for type val_type. It should parse the argument into values (usually in clp->val, but sometimes elsewhere), report errors if necessary, and return whether the parse was successful.

Any prior argument parser match val_type is removed. val_type must be greater than zero.

flags specifies additional parsing flags. At the moment the only relevant flag is Clp_DisallowOptions, which means that separated values must not look like options. For example, assume argument –a/-a has mandatory value type Clp_ValStringNotOption (which has Clp_DisallowOptions). Then:

  • –a=–b will parse with value –b.
  • -a–b will parse with value –b.
  • –a –b will not parse, since the mandatory value looks like an option.
  • -a –b will not parse, since the mandatory value looks like an option.

Definition at line 1041 of file clp.c.

Referenced by main().

◆ Clp_CurOptionName()

const char* Clp_CurOptionName ( Clp_Parser clp)

Extract the current option as a string.

Parameters
clpthe parser
Returns
string describing the current option

This function acts like Clp_CurOptionNameBuf(), but returns a pointer into a static buffer that will be rewritten on the next call to Clp_CurOptionName().

Note
This function is not thread safe.
See also
Clp_CurOptionName

Definition at line 2460 of file clp.c.

Referenced by main().

◆ Clp_CurOptionNameBuf()

int Clp_CurOptionNameBuf ( Clp_Parser clp,
char *  buf,
int  len 
)

Extract the current option as a string.

Parameters
clpthe parser
bufoutput buffer
lenlength of output buffer
Returns
number of characters written to the buffer, not including the terminating NUL

A string that looks like the last option parsed by clp is extracted into buf. The correct option characters are put into the string first, followed by the option text. The output buffer is null-terminated unless len == 0.

See also
Clp_CurOptionName

Definition at line 2422 of file clp.c.

◆ Clp_DeleteParser()

void Clp_DeleteParser ( Clp_Parser clp)

Destroy a Clp_Parser object.

Parameters
clpthe parser

All memory associated with clp is freed.

Definition at line 579 of file clp.c.

Referenced by main().

◆ Clp_DeleteParserState()

void Clp_DeleteParserState ( Clp_ParserState state)

Destroy a Clp_ParserState object.

Parameters
stateparser state

The memory associated with state is freed.

Definition at line 1470 of file clp.c.

◆ Clp_fprintf()

int Clp_fprintf ( Clp_Parser clp,
FILE *  f,
const char *  format,
  ... 
)

Print a message.

Parameters
clpthe parser
foutput file
formaterror format

Format an error message using format and additional arguments in the ellipsis, according to the Clp_OptionError formatting conventions. The resulting message is written to f.

See also
Clp_OptionError

Definition at line 2285 of file clp.c.

Referenced by short_usage(), and usage().

◆ Clp_IsLong()

int Clp_IsLong ( Clp_Parser clp,
const char *  long_name 
)

Test if the current option had long name name.

Definition at line 2468 of file clp.c.

◆ Clp_IsShort()

int Clp_IsShort ( Clp_Parser clp,
int  short_name 
)

Test if the current option had short name name.

Definition at line 2476 of file clp.c.

References clp, Clp_Internal::current_option, Clp_Parser::internal, Clp_Option::long_name, and Clp_Internal::opt.

◆ Clp_NewParser()

Clp_Parser* Clp_NewParser ( int  argc,
const char *const *  argv,
int  nopt,
const Clp_Option opt 
)

Create a new Clp_Parser.

Parameters
argcnumber of arguments
argvargument array
noptnumber of option definitions
optoption definition array
Returns
the parser

The new Clp_Parser that will parse the arguments in argv according to the option definitions in opt.

The Clp_Parser is created with the following characteristics:

  • The "-" character introduces short options (Clp_SetOptionChar(clp, '-', Clp_Short)).
  • Clp_ProgramName is set from the first argument in argv, if any. The first argument returned by Clp_Next() will be the second argument in argv. Note that this behavior differs from Clp_SetArguments.
  • UTF-8 support is on iff the LANG environment variable contains one of the substrings "UTF-8", "UTF8", or "utf8". Override this with Clp_SetUTF8().
  • The Clp_ValString, Clp_ValStringNotOption, Clp_ValInt, Clp_ValUnsigned, Clp_ValLong, Clp_ValUnsignedLong, Clp_ValBool, and Clp_ValDouble types are installed.
  • Errors are reported to standard error.

You may also create a Clp_Parser with no arguments or options (Clp_NewParser(0, 0, 0, 0)) and set the arguments and options later.

Returns NULL if there isn't enough memory to construct the parser.

Note
The CLP library will not modify the contents of argv or opt. The calling program must not modify opt. It may modify argv in limited cases.

Definition at line 503 of file clp.c.

Referenced by main().

◆ Clp_NewParserState()

Clp_ParserState* Clp_NewParserState ( void  )

Create a new Clp_ParserState.

Returns
the parser state

A Clp_ParserState object can store a parsing state of a Clp_Parser object. This state specifies exactly how far the Clp_Parser has gotten in parsing an argument list. The Clp_SaveParser() and Clp_RestoreParser() functions can be used to save this state and then restore it later, allowing a Clp_Parser to switch among argument lists.

The initial state is empty, in that after Clp_RestoreParser(clp, state), Clp_Next(clp) would return Clp_Done.

Parser states can be saved and restored among different parser objects.

See also
Clp_DeleteParserState, Clp_SaveParser, Clp_RestoreParser

Definition at line 1446 of file clp.c.

◆ Clp_Next()

int Clp_Next ( Clp_Parser clp)

Parse and return the next argument from clp.

Parameters
clpthe parser
Returns
option ID of next option

Parse the next argument from the argument list, store information about that argument in the fields of clp, and return the option's ID.

If an argument was successfully parsed, that option's ID is returned. Other possible return values are:

Clp_Done
There are no more arguments.
Clp_NotOption
The next argument was not an option. The argument's text is clp->vstr (and clp->val.s).
Clp_BadOption
The next argument was a bad option: either an option that wasn't understood, or an option lacking a required value, or an option whose value couldn't be parsed. The option has been skipped.
Clp_Error
There was an internal error. This should never occur unless a user messes with, for example, a Clp_Option array.

The fields of clp are set as follows.

negated
1 if the option was negated, 0 if it wasn't.
have_val
1 if the option had a value, 0 if it didn't. Note that negated options are not allowed to have values.
vstr
The value string, if any. NULL if there was no value.
val
An option's value type will parse the value string into this union.

The parsed argument is shifted off the argument list, so that sequential calls to Clp_Next() step through the arugment list.

Definition at line 1835 of file clp.c.

Referenced by main().

◆ Clp_OptionChar()

int Clp_OptionChar ( Clp_Parser clp,
int  c 
)

Return clp's treatment of character c.

Parameters
clpthe parser
ccharacter
Returns
option character treatment

Returns an integer specifying how CLP treats arguments that begin with character c. See Clp_SetOptionChar for possibilities.

Definition at line 652 of file clp.c.

◆ Clp_OptionError()

int Clp_OptionError ( Clp_Parser clp,
const char *  format,
  ... 
)

Report a parser error.

Parameters
clpthe parser
formaterror format

Format an error message from format and any additional arguments in the ellipsis. The resulting error string is then printed to standard error (or passed to the error handler specified by Clp_SetErrorHandler). Returns the number of characters printed.

The following format characters are accepted:

%c
A character (type int). Control characters are printed in caret notation. If the parser is in UTF-8 mode, the character is formatted in UTF-8. Otherwise, special characters are printed with backslashes and octal notation.
%s
A string (type const char *).
%C
The argument is a string (type const char *). The first character in this string is printed. If the parser is in UTF-8 mode, this may involve multiple bytes.
%d
An integer (type int). Printed in decimal.
%O
The current option. No values are read from the argument list; the current option is defined in the Clp_Parser object itself.
%V
Like %O, but also includes the current value, if any.
%%
Prints a percent character.
%<
Prints an open quote string. In UTF-8 mode, prints a left single quote. Otherwise prints a single quote.
%>
Prints a closing quote string. In UTF-8 mode, prints a right single quote. Otherwise prints a single quote.
%,
Prints an apostrophe. In UTF-8 mode, prints a right single quote. Otherwise prints a single quote.

Note that no flag characters, precision, or field width characters are currently supported.

See also
Clp_SetErrorHandler

Definition at line 2261 of file clp.c.

Referenced by compare_options(), main(), parse_color(), parse_dimensions(), parse_frame_spec(), parse_position(), parse_rectangle(), parse_scale_factor(), and parse_two_colors().

◆ Clp_ProgramName()

const char* Clp_ProgramName ( Clp_Parser clp)

Return clp's program name.

Definition at line 1407 of file clp.c.

References clp, Clp_Parser::internal, and Clp_Internal::program_name.

Referenced by main().

◆ Clp_RestoreParser()

void Clp_RestoreParser ( Clp_Parser clp,
const Clp_ParserState state 
)

Restore parser state from state into clp.

Parameters
clpthe parser
stateparser state

The parser state in state is restored into clp. The next call to Clp_Next() will return the same result as it would have at the time state was saved (probably by Clp_SaveParser(clp, state)).

A parser state contains information about arguments (argc and argv; see Clp_SetArguments()) and option processing (Clp_SetOptionProcessing()), but not about options (Clp_SetOptions()). Changes to options and value types are preserved across Clp_RestoreParser().

See also
Clp_NewParserState, Clp_SaveParser

Definition at line 1513 of file clp.c.

◆ Clp_SaveParser()

void Clp_SaveParser ( const Clp_Parser clp,
Clp_ParserState state 
)

Save clp's current state in state.

Parameters
clpthe parser
stateparser state
See also
Clp_NewParserState, Clp_RestoreParser

Definition at line 1480 of file clp.c.

Referenced by Clp_Shift().

◆ Clp_SetArguments()

void Clp_SetArguments ( Clp_Parser clp,
int  argc,
const char *const *  argv 
)

Set clp's arguments.

Parameters
clpthe parser
argcnumber of arguments
argvargument array

Installs the arguments in argv for parsing. Future option parsing will analyze argv.

Unlike Clp_NewParser(), this function does not treat argv[0] specially. The first subsequent call to Clp_Next() will analyze argv[0].

This function also sets option processing to on, as by Clp_SetOptionProcessing(clp, 1).

Note
The CLP library will not modify the contents of argv. The calling program should not generally modify the element of argv that CLP is currently analyzing.

Definition at line 871 of file clp.c.

◆ Clp_SetErrorHandler()

Clp_ErrorHandler Clp_SetErrorHandler ( Clp_Parser clp,
void(*)(Clp_Parser *, const char *)  errh 
)
Parameters
clpthe parser
errherror handler function
Returns
previous error handler function

The error handler function is called when CLP encounters an error while parsing the command line. It is called with the arguments "<tt>(*errh)(@a clp, s)</tt>", where s is a description of the error terminated by a newline. The s descriptions produced by CLP itself are prefixed by the program name, if any.

Definition at line 614 of file clp.c.

◆ Clp_SetOptionChar()

int Clp_SetOptionChar ( Clp_Parser clp,
int  c,
int  type 
)

Set clp's treatment of character c.

Parameters
clpthe parser
ccharacter
typeoption character treatment
Returns
previous option character treatment, or -1 on error

type specifies how CLP treats arguments that begin with character c. Possibilities are:

Clp_NotOption (or 0)
The argument cannot be an option.
Clp_Long
The argument is a long option.
Clp_Short
The argument is a set of short options.
Clp_Short|Clp_Long
The argument is either a long option or, if no matching long option is found, a set of short options.
Clp_LongNegated
The argument is a negated long option. For example, after Clp_SetOptionChar(clp, '^', Clp_LongNegated), the argument "^foo" is equivalent to "--no-foo".
Clp_ShortNegated
The argument is a set of negated short options.
Clp_ShortNegated|Clp_LongNegated
The argument is either a negated long option or, if no matching long option is found, a set of negated short options.
Clp_LongImplicit
The argument may be a long option, where the character c is actually part of the long option name. For example, after Clp_SetOptionChar(clp, 'f', Clp_LongImplicit), the argument "foo" may be equivalent to "--foo".

In UTF-8 mode, c may be any Unicode character. Otherwise, c must be an unsigned char value. The special character 0 assigns type to every character.

It is an error if c is out of range, type is illegal, or there are too many character definitions stored in clp already. The function returns -1 on error.

A double hyphen "--" always introduces a long option. This behavior cannot currently be changed with Clp_SetOptionChar().

Definition at line 710 of file clp.c.

Referenced by main().

◆ Clp_SetOptionProcessing()

int Clp_SetOptionProcessing ( Clp_Parser clp,
int  on 
)

Set whether clp is searching for options.

Parameters
clpthe parser
onwhether to search for options
Returns
previous option processing setting

When option processing is off, every call to Clp_Next() returns Clp_NotOption. By default the option "--" turns off option processing and is otherwise ignored.

Definition at line 894 of file clp.c.

◆ Clp_SetOptions()

int Clp_SetOptions ( Clp_Parser clp,
int  nopt,
const Clp_Option opt 
)

Set clp's option definitions.

Parameters
clpthe parser
noptnumber of option definitions
optoption definition array
Returns
0 on success, -1 on failure

Installs the option definitions in opt. Future option parsing will use opt to search for options.

Also checks opt's option definitions for validity. "CLP internal errors" are reported via Clp_OptionError() if:

  • An option has a negative ID.
  • Two different short options have the same name.
  • Two different long options have the same name.
  • A short and a long option are ambiguous, in that some option character might introduce either a short or a long option (e.g., Clp_SetOptionChar(clp, '-', Clp_Long|Clp_Short)), and a short name equals a long name.

If necessary memory cannot be allocated, this function returns -1 without modifying the parser.

Note
The CLP library will not modify the contents of argv or opt. The calling program must not modify opt either until another call to Clp_SetOptions() or the parser is destroyed.

Definition at line 785 of file clp.c.

◆ Clp_SetProgramName()

const char* Clp_SetProgramName ( Clp_Parser clp,
const char *  name 
)

Set clp's program name.

Parameters
clpthe parser
namenew program name
Returns
previous program name

The calling program should not modify or free name until clp itself is destroyed.

Definition at line 1419 of file clp.c.

◆ Clp_SetUTF8()

int Clp_SetUTF8 ( Clp_Parser clp,
int  utf8 
)

Set clp's UTF-8 mode.

Parameters
clpthe parser
utf8does the parser support UTF-8?
Returns
previous UTF-8 mode

In UTF-8 mode, all input strings (arguments and long names for options) are assumed to be encoded via UTF-8, and all character names (Clp_SetOptionChar() and short names for options) may cover the whole Unicode range. Out of UTF-8 mode, all input strings are treated as binary, and all character names must be unsigned char values.

Furthermore, error messages in UTF-8 mode may contain Unicode quote characters.

Definition at line 635 of file clp.c.

◆ Clp_Shift()

const char* Clp_Shift ( Clp_Parser clp,
int  allow_options 
)

Return the next argument from clp without option parsing.

Parameters
clpthe parser
allow_optionswhether options will be allowed

Remove and return the next argument from clp's argument array. If there are no arguments left, or if the next argument is an option and allow_options != 0, then returns null.

Definition at line 1983 of file clp.c.

References clp, Clp_SaveParser(), and next_argument().

Referenced by parse_two_colors().

◆ clp_utf8_charlen()

static int clp_utf8_charlen ( const Clp_Internal cli,
const char *  s 
)
static

Definition at line 357 of file clp.c.

References Clp_Internal::utf8, and utf8_charlen().

◆ Clp_vbsprintf()

static void Clp_vbsprintf ( Clp_Parser clp,
Clp_BuildString bs,
const char *  fmt,
va_list  val 
)
static

Definition at line 2069 of file clp.c.

References append_build_string(), clp, Clp_Parser::internal, and Clp_Internal::program_name.

◆ Clp_vfprintf()

int Clp_vfprintf ( Clp_Parser clp,
FILE *  f,
const char *  format,
va_list  val 
)

Print a message.

Parameters
clpthe parser
foutput file
formaterror format
valarguments

Format an error message using format and val, according to the Clp_OptionError formatting conventions. The resulting message is written to f.

See also
Clp_OptionError

Definition at line 2310 of file clp.c.

◆ Clp_vsnprintf()

int Clp_vsnprintf ( Clp_Parser clp,
char *  str,
size_t  size,
const char *  format,
va_list  val 
)

Format a message.

Parameters
clpthe parser
stroutput string
sizesize of output string
formaterror format

Format an error message from format and any additional arguments in the ellipsis, according to the Clp_OptionError formatting conventions. The resulting string is written to str. At most size characters are written to str, including a terminating null byte. The return value is the number of characters that would have been written (excluding the terminating null byte) if size were large enough to contain the entire string.

See also
Clp_OptionError

Definition at line 2336 of file clp.c.

Referenced by error(), fatal_error(), and verror().

◆ compare_options()

◆ copy_string()

static int copy_string ( char *  buf,
int  buflen,
int  bufpos,
const char *  what 
)
static

Definition at line 2400 of file clp.c.

◆ decode_utf8()

static int decode_utf8 ( const char *  s,
const char **  cp 
)
static

Definition at line 300 of file clp.c.

References U_REPLACEMENT.

Referenced by compare_options(), long_as_short(), and utf8_charlen().

◆ do_error()

static void do_error ( Clp_Parser clp,
Clp_BuildString bs 
)
static

Definition at line 2203 of file clp.c.

References Clp_BuildString::data, and Clp_BuildString::pos.

◆ encode_utf8()

static char* encode_utf8 ( char *  s,
int  n,
int  c 
)
static

Definition at line 276 of file clp.c.

References U_REPLACEMENT.

◆ find_long()

static int find_long ( Clp_Parser clp,
const char *  arg 
)
static

Definition at line 1707 of file clp.c.

◆ find_prefix_opt()

static int find_prefix_opt ( Clp_Parser clp,
const char *  arg,
int  nopt,
const Clp_Option opt,
const Clp_InternOption iopt,
int *  ambiguous,
int *  ambiguous_values 
)
static

Definition at line 947 of file clp.c.

Referenced by parse_string_list().

◆ find_short()

static int find_short ( Clp_Parser clp,
const char *  text 
)
static

Definition at line 1769 of file clp.c.

References clp, Clp_Parser::have_val, and Clp_Parser::vstr.

◆ finish_string_list()

static int finish_string_list ( Clp_Parser clp,
int  val_type,
int  flags,
Clp_Option items,
int  nitems,
int  itemscap 
)
static

Definition at line 1219 of file clp.c.

◆ get_oclass()

static int get_oclass ( Clp_Parser clp,
const char *  text,
int *  ocharskip 
)
static

Definition at line 1545 of file clp.c.

References Clp_OptionCharsSize, Clp_Internal::option_chars, and Clp_Internal::xtext.

◆ long_as_short()

static int long_as_short ( const Clp_Internal cli,
const Clp_Option o,
Clp_InternOption io,
int  failure 
)
static

◆ min_different_chars()

static int min_different_chars ( const char *  s,
const char *  t 
)
static

Definition at line 367 of file clp.c.

Referenced by compare_options().

◆ next_argument()

static int next_argument ( Clp_Parser clp,
int  want_argument 
)
static

if not in short options, move to the next argument

Definition at line 1560 of file clp.c.

Referenced by Clp_Shift().

◆ parse_bool()

static int parse_bool ( Clp_Parser clp,
const char *  arg,
int  complain,
void *  user_data 
)
static

Definition at line 1146 of file clp.c.

◆ parse_double()

static int parse_double ( Clp_Parser clp,
const char *  arg,
int  complain,
void *  user_data 
)
static

Definition at line 1128 of file clp.c.

◆ parse_int()

static int parse_int ( Clp_Parser clp,
const char *  arg,
int  complain,
void *  user_data 
)
static

Definition at line 1093 of file clp.c.

Referenced by parse_string_list().

◆ parse_string()

static int parse_string ( Clp_Parser clp,
const char *  arg,
int  complain,
void *  user_data 
)
static

Definition at line 1085 of file clp.c.

◆ parse_string_list()

◆ set_option_text()

static void set_option_text ( Clp_Internal cli,
const char *  text,
int  n_option_chars 
)
static

Definition at line 1536 of file clp.c.

◆ switch_to_short_argument()

static void switch_to_short_argument ( Clp_Parser clp)
static

Definition at line 1694 of file clp.c.

◆ utf8_charlen()

static int utf8_charlen ( const char *  s)
static

Definition at line 349 of file clp.c.

References decode_utf8().

Referenced by clp_utf8_charlen().

◆ val_type_binsearch()

static int val_type_binsearch ( Clp_Internal cli,
int  val_type 
)
static

Definition at line 994 of file clp.c.

References Clp_Internal::nvaltype, Clp_ValType::val_type, and Clp_Internal::valtype.

Variable Documentation

◆ clp_option_sentinel

const Clp_Option clp_option_sentinel[]
static
Initial value:
= {
{"", 0, Clp_NotOption, 0, 0},
{"", 0, Clp_Done, 0, 0},
{"", 0, Clp_BadOption, 0, 0},
{"", 0, Clp_Error, 0, 0}
}

Definition at line 251 of file clp.c.

Clp_AddStringListType
int Clp_AddStringListType(Clp_Parser *clp, int val_type, int flags,...) CLP_SENTINEL
Define a new string list value type for clp.
Definition: clp.c:1285
Clp_Done
#define Clp_Done
Clp_Next value: there are no more arguments.
Definition: clp.h:194
Clp_Error
#define Clp_Error
Clp_Next value: internal CLP error.
Definition: clp.h:200
Clp_NotOption
#define Clp_NotOption
Clp_Next value: argument was not an option.
Definition: clp.h:191
clp
static Clp_Parser * clp
Definition: gifdiff.c:58
Clp_AddStringListTypeVec
int Clp_AddStringListTypeVec(Clp_Parser *clp, int val_type, int flags, int nstrs, const char *const *strs, const int *vals)
Define a new string list value type for clp.
Definition: clp.c:1374
Clp_BadOption
#define Clp_BadOption
Clp_Next value: argument was an erroneous option.
Definition: clp.h:197
Clp_AllowNumbers
#define Clp_AllowNumbers
String list flag: allow explicit numbers.
Definition: clp.h:327
build_string_grow
static int build_string_grow(Clp_BuildString *bs, size_t want)
Definition: clp.c:2021