Crinit -- Configurable Rootfs Init
confconv.c File Reference

(2023-08-02, commit: 9434b31)

Implementations of conversion operations from configuration values to structured data. More...

#include "confconv.h"
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
#include "lexers.h"
#include "logio.h"
Include dependency graph for confconv.c:

Macros

#define crinitConfConvToIntegerFnBody(resType, confVal)
 

Functions

static char * crinitCopyEscaped (char *dst, const char *src, const char *end)
 
char ** crinitConfConvToStrArr (int *numElements, const char *confVal, bool doubleQuoting)
 
int crinitConfConvToIntegerI (int *x, const char *confVal, int base)
 
int crinitConfConvToIntegerULL (unsigned long long *x, const char *confVal, int base)
 
int crinitConfConvToBool (bool *b, const char *confVal)
 
int crinitConfConvToIoRedir (crinitIoRedir_t *ior, const char *confVal)
 
int crinitConfConvToEnvSetMember (crinitEnvSet_t *es, const char *confVal)
 

Detailed Description

Implementations of conversion operations from configuration values to structured data.

Macro Definition Documentation

◆ crinitConfConvToIntegerFnBody

#define crinitConfConvToIntegerFnBody (   resType,
  confVal 
)
Value:
do { \
if (*(confVal) == '\0') { \
crinitErrPrint("Could not convert string to int. String is empty."); \
return -1; \
} \
char *endptr = NULL; \
errno = 0; \
(resType) = crinitStrtoGenericInteger(resType, confVal, &endptr, base); \
if (errno != 0) { \
crinitErrnoPrint("Could not convert string to int."); \
return -1; \
} \
if (*endptr != '\0') { \
crinitErrPrint("Could not convert string to int. Non-numeric characters present in string."); \
return -1; \
} \
} while (0)
#define crinitStrtoGenericInteger(resType, str, endptr, base)
Definition: common.h:87

Function body for type-generic string-to-integer functions, used internally to deduplicate code.

Function Documentation

◆ crinitConfConvToBool()

int crinitConfConvToBool ( bool *  b,
const char *  confVal 
)

Converts a string to bool.

String must be equal to either NO (==false) or YES (==true).

Parameters
bOutput pointer to bool variable.
confValThe string to convert.
Returns
0 on success, -1 on error.

◆ crinitConfConvToEnvSetMember()

int crinitConfConvToEnvSetMember ( crinitEnvSet_t es,
const char *  confVal 
)

Parses a single ENV_SET directive and sets the variable in question accordingly.

For details on the syntax, see the relevant section in README.md.

Parameters
esThe environment set to be modified, must be initialized.
confValThe ENV_SET directive to be parsed and "executed" on the set.
Returns
0 on success, -1 otherwise

◆ crinitConfConvToIntegerI()

int crinitConfConvToIntegerI ( int *  x,
const char *  confVal,
int  base 
)

Converts a string to a signed integer, see crinitConfConvToInteger()

◆ crinitConfConvToIntegerULL()

int crinitConfConvToIntegerULL ( unsigned long long *  x,
const char *  confVal,
int  base 
)

Converts a string to an unsigned long long, see crinitConfConvToInteger()

◆ crinitConfConvToIoRedir()

int crinitConfConvToIoRedir ( crinitIoRedir_t ior,
const char *  confVal 
)

Initializes an instance of crinitIoRedir_t from an IO redirection statement in a string.

The string must be of the form

<REDIRECT_FROM> <REDIRECT_TO> [ APPEND | TRUNCATE ] [ OCTAL MODE ]

Where REDIRECT_FROM is one of STDOUT, STDERR, STDIN and REDIRECT_TO may either also be one of those streams or an absolute path to a file. APPEND or TRUNCATE signify whether an existing file at that location should be appended to or truncated. Default ist TRUNCATE. OCTAL MODE sets the permission bits if the file is newly created. Default is 0644.

The function may allocate memory inside the crinitIoRedir_t struct which must be freed using crinitDestroyIoRedir().

Parameters
iorThe crinitIoRedir_t instance to initialize.
confValThe string with the statement to parse.
Returns
0 on success, -1 otherwise

◆ crinitConfConvToStrArr()

char** crinitConfConvToStrArr ( int *  numElements,
const char *  confVal,
bool  doubleQuoting 
)

Extract an array of strings from the value mapped to an indexed key in an crinitConfKvList_t.

Will split confVal along spaces. Will optionally respect quoting using double quotes if doubleQuoting is set to true. A dynamically-allocated array-of-strings is returned. If no longer needed it should be freed using crinitFreeArgvArray(). numElements will contain the number of strings inside the output array and the output array will be additionally NULL-terminated, same as argc/argv in main().

Parameters
numElementsWill contain the number of strings in the output.
confValThe string to split.
doubleQuotingIf true, crinitConfConvToStrArr will respect quoting with double quotes.
Returns
A new dynamically allocated array of the substrings in confVal on success, NULL on error.

◆ crinitCopyEscaped()

static char * crinitCopyEscaped ( char *  dst,
const char *  src,
const char *  end 
)
static

Copies a string while resolving all contained escape sequences.

Parameters
dstDestination string. Must have enough space. Best practice would be "same as \a src" because escape sequence evaluation can only make the string shorter in this case.
srcSource string, potentially containing escape sequences.
endEnd pointer, if src should not be read until the terminating null byte.
Returns
Pointer to end of src on success, NULL on failure.