![]() |
Flowgrind
Advanced TCP traffic generator
|
Command line argument parser. More...
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "fg_definitions.h"
#include "fg_argparser.h"
Go to the source code of this file.
Functions | |
static bool | add_error (struct arg_parser *const ap, const char *const msg) |
Add an error message to the arg-parser ap . More... | |
const char * | ap_argument (const struct arg_parser *const ap, const int i) |
Returns the argument of a parsed option. More... | |
int | ap_arguments (const struct arg_parser *const ap) |
Number of arguments parsed (may be different from argc). More... | |
bool | ap_check_mutex (const struct arg_parser *const ap, const struct ap_Mutex_state *const ms, const int i, int *conflict) |
Check a new option record for mutex. More... | |
int | ap_code (const struct arg_parser *const ap, const int i) |
Returns the code of a parsed option with given index. More... | |
const char * | ap_error (const struct arg_parser *const ap) |
Get the string containing errors encountered during parsing. More... | |
void | ap_free (struct arg_parser *const ap) |
Free internal state of arg-parser. More... | |
void | ap_free_mutex_state (struct ap_Mutex_state *const ms) |
Free a mutex context. More... | |
bool | ap_init (struct arg_parser *const ap, const int argc, const char *const argv[], const struct ap_Option options[], const char in_order) |
Initialize the arg-parser given command line and user-defined options. More... | |
bool | ap_init_mutex_state (const struct arg_parser *const ap, struct ap_Mutex_state *const ms) |
Initialize a new mutex state table. More... | |
bool | ap_is_used (const struct arg_parser *const ap, int code) |
Returns true if the option specified by code was given at least once. More... | |
const char * | ap_opt_string (const struct arg_parser *const ap, const int i) |
Get the real command line option string (may be the short or long version). More... | |
const struct ap_Option * | ap_option (const struct arg_parser *const ap, const int i) |
Get the user-defined option for a given record position. More... | |
void | ap_reset_mutex (struct ap_Mutex_state *const ms) |
Reset a mutex context. More... | |
static void * | ap_resize_buffer (void *buf, const int min_size) |
Assure at least a minimum size for buffer buf . More... | |
bool | ap_set_check_mutex (const struct arg_parser *const ap, struct ap_Mutex_state *const ms, const int i, int *conflict) |
Check a new option record for mutex and register it at the same time. More... | |
bool | ap_set_mutex (const struct arg_parser *const ap, struct ap_Mutex_state *const ms, const int i) |
Register an option record in a mutex context. More... | |
static bool | copy_options (struct arg_parser *const ap, const struct ap_Option options[]) |
Copy options into the arg-parser ap . More... | |
static void | free_data (struct arg_parser *const ap) |
Free all space required by the arg-parser ap . More... | |
static int | get_mutex_count (const struct ap_Option options[]) |
Get the number of mutex in the option definitions. More... | |
static int | get_num_options (const struct ap_Option options[]) |
Determines number of options in options . More... | |
static bool | parse_long_option (struct arg_parser *const ap, const char *const opt, const char *const arg, const struct ap_Option options[], int *const argindp) |
Parses a long option and adds it to the record of arg-parser ap . More... | |
static bool | parse_short_option (struct arg_parser *const ap, const char *const opt, const char *const arg, const struct ap_Option options[], int *const argindp) |
Parses a short option and adds it to the record of arg-parser ap . More... | |
static bool | push_back_record (struct arg_parser *const ap, const int option_index, bool long_opt, const char *const argument) |
Store a parsed option in the state of the arg-parser given by ap . More... | |
Command line argument parser.
Definition in file fg_argparser.c.
|
static |
Add an error message to the arg-parser ap
.
[in] | ap | pointer to the arg-parser state |
[in] | msg | error string |
Definition at line 104 of file fg_argparser.c.
const char* ap_argument | ( | const struct arg_parser *const | ap, |
const int | i | ||
) |
Returns the argument of a parsed option.
[in] | ap | pointer to arg-parser state |
[in] | i | index of the parsed option |
i
. If this option has code 0 (non-option), the non-option string is returned Definition at line 478 of file fg_argparser.c.
int ap_arguments | ( | const struct arg_parser *const | ap | ) |
Number of arguments parsed (may be different from argc).
[in] | ap | pointer to arg-parser state |
Definition at line 463 of file fg_argparser.c.
bool ap_check_mutex | ( | const struct arg_parser *const | ap, |
const struct ap_Mutex_state *const | ms, | ||
const int | i, | ||
int * | conflict | ||
) |
Check a new option record for mutex.
[in] | ap | pointer to arg-parser state |
[in] | ms | pointer to an initialized mutex context |
[in] | i | index of the option to check for previous occurrences of mutexed options |
[in] | conflict | pointer to a single integer value. This will contain the conflicting record position, if a conflict has been found |
ms
has occurred, otherwise flase Definition at line 527 of file fg_argparser.c.
int ap_code | ( | const struct arg_parser *const | ap, |
const int | i | ||
) |
Returns the code of a parsed option with given index.
[in] | ap | pointer to arg-parser state |
[in] | i | index of the parsed option |
i
. If this is a non-option, it returns 0 Definition at line 468 of file fg_argparser.c.
const char* ap_error | ( | const struct arg_parser *const | ap | ) |
Get the string containing errors encountered during parsing.
[in] | ap | pointer to arg-parser state |
Definition at line 458 of file fg_argparser.c.
void ap_free | ( | struct arg_parser *const | ap | ) |
Free internal state of arg-parser.
[in] | ap | pointer to arg-parser state |
Definition at line 448 of file fg_argparser.c.
void ap_free_mutex_state | ( | struct ap_Mutex_state *const | ms | ) |
Free a mutex context.
[in] | ms | pointer to an initialized mutex context |
Definition at line 583 of file fg_argparser.c.
bool ap_init | ( | struct arg_parser *const | ap, |
const int | argc, | ||
const char *const | argv[], | ||
const struct ap_Option | options[], | ||
const char | in_order | ||
) |
Initialize the arg-parser given command line and user-defined options.
[in] | ap | pointer to arg-parser state |
[in] | argc | number of command line arguments |
[in] | argv | array of command line argument strings |
[in] | options | array of user-defined options to parse for. The last entry of the array needs to have '0' members. This array will be copied over to the arg-parser state ap . |
[in] | in_order | if set to true, arguments are stored in the order in which they appear. If false, non-option arguments are stored after options |
Definition at line 374 of file fg_argparser.c.
bool ap_init_mutex_state | ( | const struct arg_parser *const | ap, |
struct ap_Mutex_state *const | ms | ||
) |
Initialize a new mutex state table.
This can be seen as a separate context for checking mutex. Thus, by initializing more than one mutex state, mutual exclusions of options may be evaluated in independent contexts.
[in] | ap | pointer to arg-parser state |
[in] | ms | pointer to a new mutex context. It can be used in the following to check and set mutual exclusions |
Definition at line 516 of file fg_argparser.c.
bool ap_is_used | ( | const struct arg_parser *const | ap, |
int | code | ||
) |
Returns true if the option specified by code
was given at least once.
[in] | ap | pointer to arg-parser state |
[in] | code | code of the option to check return return true if the option specified by code has been parsed, otherwise false |
Definition at line 503 of file fg_argparser.c.
const char* ap_opt_string | ( | const struct arg_parser *const | ap, |
const int | i | ||
) |
Get the real command line option string (may be the short or long version).
[in] | ap | pointer to arg-parser state |
[in] | i | index of the parsed option |
Definition at line 486 of file fg_argparser.c.
const struct ap_Option* ap_option | ( | const struct arg_parser *const | ap, |
const int | i | ||
) |
Get the user-defined option for a given record position.
[in] | ap | pointer to arg-parser state |
[in] | i | index of the parsed option |
i
Definition at line 494 of file fg_argparser.c.
void ap_reset_mutex | ( | struct ap_Mutex_state *const | ms | ) |
Reset a mutex context.
[in] | ms | pointer to an initialized mutex context |
Definition at line 578 of file fg_argparser.c.
|
static |
Assure at least a minimum size for buffer buf
.
[in] | buf | pointer to buffer |
[in] | min_size | minimum size buf should hold in bytes |
Definition at line 48 of file fg_argparser.c.
bool ap_set_check_mutex | ( | const struct arg_parser *const | ap, |
struct ap_Mutex_state *const | ms, | ||
const int | i, | ||
int * | conflict | ||
) |
Check a new option record for mutex and register it at the same time.
[in] | ap | pointer to arg-parser state |
[in] | ms | pointer to an initialized mutex context |
[in] | i | index of the option to register in the mutex state ms |
[in] | conflict | pointer to a single integer value. This will contain the conflicting record position, if a conflict has been found |
ms
has occurred, otherwise false Definition at line 569 of file fg_argparser.c.
bool ap_set_mutex | ( | const struct arg_parser *const | ap, |
struct ap_Mutex_state *const | ms, | ||
const int | i | ||
) |
Register an option record in a mutex context.
[in] | ap | pointer to arg-parser state |
[in] | ms | pointer to an initialized mutex context |
[in] | i | index of the option to register in the mutex state ms |
Definition at line 553 of file fg_argparser.c.
|
static |
Copy options
into the arg-parser ap
.
This is a deep copy including strings and arrays.
[in] | ap | arg-parser |
[in] | options | options struct to copy |
Definition at line 341 of file fg_argparser.c.
|
static |
Free all space required by the arg-parser ap
.
[in] | ap | pointer to the arg-parser state |
Definition at line 122 of file fg_argparser.c.
|
static |
Get the number of mutex in the option definitions.
Searching for the greatest mutex ID in all options.
[in] | options | array of user-defined options |
Definition at line 321 of file fg_argparser.c.
|
static |
Determines number of options in options
.
Counting all options until an option with code 0 is found.
[in] | options | array of user-defined options |
options
Definition at line 306 of file fg_argparser.c.
|
static |
Parses a long option and adds it to the record of arg-parser ap
.
[in] | ap | pointer to the arg-parser state |
[in] | opt | long option string |
[in] | arg | option argument string |
[in] | options | array containing all defined options which may be parsed |
[in] | argindp | pointer to the index in the command line argument array. The value will be automatically updated |
Definition at line 153 of file fg_argparser.c.
|
static |
Parses a short option and adds it to the record of arg-parser ap
.
[in] | ap | pointer to the arg-parser state |
[in] | opt | long option string |
[in] | arg | option argument string |
[in] | options | array containing all defined options which may be parsed |
[in] | argindp | pointer to the index in the command line argument array. The value will be automatically updated |
Definition at line 242 of file fg_argparser.c.
|
static |
Store a parsed option in the state of the arg-parser given by ap
.
[in] | ap | pointer to the arg-parser state |
[in] | option_index | index of the option to store |
[in] | long_opt | true if this option was a long option |
[in] | argument | argument string for this option (may be empty) |
Definition at line 66 of file fg_argparser.c.