The ROme OpTimistic Simulator  3.0.0
A General-Purpose Multithreaded Parallel/Distributed Simulation Platform
arg_parse.c File Reference

Command line option parser. More...

#include <core/arg_parse.h>
#include <mm/mm.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
+ Include dependency graph for arg_parse.c:

Go to the source code of this file.

Macros

#define SCREEN_LENGTH   80
 Expected length of the terminal expressed in monospace characters width.
 
#define HELP_INDENT   6
 Number of spaces to indent the --help entries.
 
#define HELP_OPT_LEN_MIN   20
 Minimum indentation of the documentation strings in the --help entries.
 
#define HELP_OPT_LEN_MAX   26
 Maximum indentation of the documentation strings in the --help entries.
 
#define HELP_SPACES_MIN   3
 Minimum spaces between the option and its documentation in the --help text.
 
#define USAGE_INDENT   11
 Number of spaces to indent the --usage text.
 

Enumerations

enum  internal_opt_key { AP_HELP = AP_KEY_FINI + 1, AP_USAGE, AP_VERSION }
 The keys used in the internal struct ap_option to handle base options. More...
 

Functions

static int cmp_opts (const void *a, const void *b)
 Compares two ap_option structs alphabetically. More...
 
static int cmp_sects (const void *a, const void *b)
 Compares two ap_section structs (used to reorder sections alphabetically) More...
 
static void print_indented_string (const char *str, int curr_i, int indent)
 Prints a string in the terminal indenting it and wrapping it. More...
 
static void print_help_option (const char *name, const char *arg, const char *doc)
 Prints the help text for a single option. More...
 
static void print_help (void)
 Prints the help text for the whole argument parsing context.
 
static int print_usage_option (const char *name, const char *arg, int curr_i)
 Prints the help text for a single option. More...
 
static void print_usage (void)
 Prints the usage text for the whole argument parsing context.
 
static void internal_opt_parse (int key, const char *arg)
 The parsing function for the internal options (–help, –usage, –version) More...
 
static void sort_and_setup_settings (void)
 Sets up the ap_section structs and their ap_option structs by sorting them. The internal ap_section struct is also injected.
 
static void undo_setup_settings (void)
 Undos the injection of the internal ap_section struct.
 
static int parse_option (struct ap_section *s, struct ap_option *o, const char *arg, bool arg_explicit)
 Parses a single option by calling the appropriate parser or throwing an error. More...
 
static int process_option (const char *o_name, const char *arg)
 Parses a single option by calling the appropriate parser or throwing an error. More...
 
void arg_parse_run (struct ap_settings *ap_s, char **argv)
 Parses the command line options. More...
 
const char * arg_parse_program_name (void)
 Gets the program name. More...
 
void arg_parse_error (const char *fmt,...)
 Prints a parsing related error message and exits with a bad exit code. More...
 

Variables

static const char * ap_pname
 The program name extracted from the command line.
 
static const struct ap_settingsap_settings
 The currently parsed struct ap_settings.
 
static struct ap_option ap_internal_opts []
 The internal struct ap_option used to handle base options. More...
 

Detailed Description

Command line option parser.

A command line option parser mimicking a subset of GNU argp features.

Definition in file arg_parse.c.

Enumeration Type Documentation

◆ internal_opt_key

The keys used in the internal struct ap_option to handle base options.

Enumerator
AP_HELP 

Identifies the --help option.

AP_USAGE 

Identifies the --usage option.

AP_VERSION 

Identifies the --version option.

Definition at line 39 of file arg_parse.c.

Function Documentation

◆ arg_parse_error()

void arg_parse_error ( const char *  fmt,
  ... 
)

Prints a parsing related error message and exits with a bad exit code.

Parameters
fmtthe printf-style format string
...the arguments required to fill in the format string

Definition at line 432 of file arg_parse.c.

+ Here is the caller graph for this function:

◆ arg_parse_program_name()

const char* arg_parse_program_name ( void  )

Gets the program name.

Returns
a C string containing the program name, or NULL if arg_parse_run hasn't been called yet.

Definition at line 422 of file arg_parse.c.

◆ arg_parse_run()

void arg_parse_run ( struct ap_settings ap_s,
char **  argv 
)

Parses the command line options.

Parameters
ap_sthe parsing settings (see struct ap_settings for more info)
argvthe NULL terminated array of C strings from the command line

Definition at line 382 of file arg_parse.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cmp_opts()

static int cmp_opts ( const void *  a,
const void *  b 
)
static

Compares two ap_option structs alphabetically.

Parameters
athe first ap_option to compare
bthe second ap_option to compare
Returns
a positive or negative value if a is respectively before or after b, 0 otherwise (should never happen with correct options specifications)

Definition at line 63 of file arg_parse.c.

◆ cmp_sects()

static int cmp_sects ( const void *  a,
const void *  b 
)
static

Compares two ap_section structs (used to reorder sections alphabetically)

Parameters
athe first ap_section to compare
bthe second ap_section to compare
Returns
a positive or negative value if a must be considered respectively before or after b, 0 otherwise

Definition at line 76 of file arg_parse.c.

◆ internal_opt_parse()

static void internal_opt_parse ( int  key,
const char *  arg 
)
static

The parsing function for the internal options (–help, –usage, –version)

Parameters
keythe currently parsed key
argthe currently parsed argument

Definition at line 233 of file arg_parse.c.

◆ parse_option()

static int parse_option ( struct ap_section s,
struct ap_option o,
const char *  arg,
bool  arg_explicit 
)
static

Parses a single option by calling the appropriate parser or throwing an error.

Parameters
sthe section of the option currently parsed
othe option currently parsed
argthe argument associated with the option (can be NULL)
arg_explicita flag indicating if the argument has been supplied explicitly (with an option formatted as –option=value)
Returns
1 if a non explicit argument has been consumed during parsing, 0 otherwise

Definition at line 306 of file arg_parse.c.

◆ print_help_option()

static void print_help_option ( const char *  name,
const char *  arg,
const char *  doc 
)
static

Prints the help text for a single option.

Parameters
namethe name of the option
argthe documentation for the argument (if present, else NULL)
docthe documentation for the option (if present, else NULL)

Definition at line 131 of file arg_parse.c.

+ Here is the caller graph for this function:

◆ print_indented_string()

static void print_indented_string ( const char *  str,
int  curr_i,
int  indent 
)
static

Prints a string in the terminal indenting it and wrapping it.

Parameters
strthe string to print
curr_ithe number of characters already present in the current line of the terminal
indentthe desired indentation level for the string express in number of spaces

Definition at line 95 of file arg_parse.c.

◆ print_usage_option()

static int print_usage_option ( const char *  name,
const char *  arg,
int  curr_i 
)
static

Prints the help text for a single option.

Parameters
namethe name of the option
argthe documentation for the argument (if present, else NULL)
curr_ithe number of characters already present in the current line of the terminal

Definition at line 188 of file arg_parse.c.

+ Here is the caller graph for this function:

◆ process_option()

static int process_option ( const char *  o_name,
const char *  arg 
)
static

Parses a single option by calling the appropriate parser or throwing an error.

Parameters
o_namethe option currently parsed
argthe argument associated with the option (can be NULL)
Returns
1 if the argument has been consumed during parsing, 0 otherwise

Definition at line 334 of file arg_parse.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ ap_internal_opts

struct ap_option ap_internal_opts[]
static
Initial value:
= {
{"help", AP_HELP, NULL, "Give this help list"},
{"usage", AP_USAGE, NULL, "Give a short usage message"},
{"version", AP_VERSION, NULL, "Print program version"},
{0}
}

The internal struct ap_option used to handle base options.

Definition at line 49 of file arg_parse.c.

AP_VERSION
@ AP_VERSION
Identifies the --version option.
Definition: arg_parse.c:45
AP_HELP
@ AP_HELP
Identifies the --help option.
Definition: arg_parse.c:41
AP_USAGE
@ AP_USAGE
Identifies the --usage option.
Definition: arg_parse.c:43