machinatrix
A very silly Matrix bot written in C
Loading...
Searching...
No Matches
utils.h File Reference

Utility functions. More...

#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mtrix_buffer
 Resizable buffer used by several functions. More...
 
struct  post_request
 Data used for POST requests. More...
 

Macros

#define MTRIX_MAX_PATH   ((size_t)1024U)
 Maximum path length, arbitrarily chosen.
 
#define MTRIX_MAX_UNIX_PATH   ((size_t)108U)
 Maximum unix socket path length, based on Linux's maximum.
 
#define MTRIX_MAX_URL_LEN   ((size_t)1024U)
 Maximum length for URLs built by build_url.
 
#define MTRIX_MAX_ARGS   ((size_t)2U)
 Maximum number of command arguments (excluding the command name).
 
#define BUILD_URL(url, ...)   build_url(url, (const char *[]){__VA_ARGS__, NULL})
 Helper macro for build_url that creates a const char * array.
 

Functions

FILE * log_set (FILE *f)
 Sets the output stream used by log functions and returns previous value.
 
void log_err (const char *fmt,...)
 Writes the printf-style message to the error output.
 
void log_errv (const char *fmt, va_list argp)
 
void log_errno (const char *fmt,...)
 Similar to log_err, but also logs strerror(errno).
 
static const char * strchrnul (const char *s, int c)
 See glibc's function.
 
static size_t strlcpy (char *restrict dst, const char *restrict src, size_t n)
 See libbsd's function.
 
char * is_prefix (const char *prefix, const char *s)
 Checks if a string has a certain prefix.
 
bool copy_arg (const char *name, struct mtrix_buffer dst, const char *src)
 Copies a value from an argv-style array.
 
char * join_path (char v[static MTRIX_MAX_PATH], int n,...)
 Concatenate n segments, with length checking.
 
FILE * open_or_create (const char *path, const char *flags)
 Performs an open(2)s with O_CREAT followed by fopen.
 
bool read_all (int fd, void *p, size_t n)
 Repeatedly calls read(2) until all data are read.
 
bool write_all (int fd, const void *p, size_t n)
 Repeatedly calls write(2) until all data are written.
 
bool exec (const char *const *argv, int fin, int fout, int ferr)
 Executes a command with optional input/output/error redirection.
 
bool wait_n (size_t n, const pid_t *p)
 Waits for n child processes to exit.
 
void join_lines (unsigned char *b, unsigned char *e)
 Replaces new-line characters with spaces.
 
size_t mtrix_buffer_append (const char *p, size_t size, size_t n, struct mtrix_buffer *b)
 Copies data to the buffer, reallocating if necessary.
 
bool build_url (char *url, const char *const *v)
 Joins several URL parts into one, limited to MTRIX_MAX_URL_LEN.
 
bool request (const char *url, struct mtrix_buffer *b, bool verbose)
 Performs a GET request.
 
bool post (post_request r, bool verbose, struct mtrix_buffer *b)
 Performs a POST request.
 

Variables

const char * PROG_NAME
 Program name, used as a prefix for log messages if non-NULL.
 
const char * CMD_NAME
 Command name, used as a second prefix for log messages if non-NULL.
 

Detailed Description

Utility functions.

Macro Definition Documentation

◆ BUILD_URL

#define BUILD_URL ( url,
... )   build_url(url, (const char *[]){__VA_ARGS__, NULL})

Helper macro for build_url that creates a const char * array.

◆ MTRIX_MAX_ARGS

#define MTRIX_MAX_ARGS   ((size_t)2U)

Maximum number of command arguments (excluding the command name).

◆ MTRIX_MAX_PATH

#define MTRIX_MAX_PATH   ((size_t)1024U)

Maximum path length, arbitrarily chosen.

◆ MTRIX_MAX_UNIX_PATH

#define MTRIX_MAX_UNIX_PATH   ((size_t)108U)

Maximum unix socket path length, based on Linux's maximum.

◆ MTRIX_MAX_URL_LEN

#define MTRIX_MAX_URL_LEN   ((size_t)1024U)

Maximum length for URLs built by build_url.

Function Documentation

◆ build_url()

bool build_url ( char * url,
const char *const * v )

Joins several URL parts into one, limited to MTRIX_MAX_URL_LEN.

Here is the caller graph for this function:

◆ copy_arg()

bool copy_arg ( const char * name,
struct mtrix_buffer dst,
const char * src )

Copies a value from an argv-style array.

Checks for non-emptiness and length are performed and errors are logged.

Here is the caller graph for this function:

◆ exec()

bool exec ( const char *const * argv,
int fin,
int fout,
int ferr )

Executes a command with optional input/output/error redirection.

If any of the f* parameters are not -1, the corresponding file descriptor is replaced by it before the exec call.

Parameters
argvexecv-style argument list.
finSubstitute for stdin.
foutSubstitute for stdout.
ferrSubstitute for stderr.
Here is the caller graph for this function:

◆ is_prefix()

char * is_prefix ( const char * prefix,
const char * s )

Checks if a string has a certain prefix.

Returns
The first character after the prefix or NULL if not a prefix.
Here is the caller graph for this function:

◆ join_lines()

void join_lines ( unsigned char * b,
unsigned char * e )

Replaces new-line characters with spaces.

Here is the caller graph for this function:

◆ join_path()

char * join_path ( char v[static MTRIX_MAX_PATH],
int n,
... )

Concatenate n segments, with length checking.

Here is the call graph for this function:

◆ log_err()

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

Writes the printf-style message to the error output.

If non-null, PROG_NAME and CMD_NAME are prepended.

Here is the caller graph for this function:

◆ log_errno()

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

Similar to log_err, but also logs strerror(errno).

: s\n is appended, where s is the result of strerror(errno). errno is cleared.

Here is the caller graph for this function:

◆ log_errv()

void log_errv ( const char * fmt,
va_list argp )
See also
log_err
Here is the caller graph for this function:

◆ log_set()

FILE * log_set ( FILE * f)

Sets the output stream used by log functions and returns previous value.

◆ mtrix_buffer_append()

size_t mtrix_buffer_append ( const char * p,
size_t size,
size_t n,
struct mtrix_buffer * b )

Copies data to the buffer, reallocating if necessary.

Here is the caller graph for this function:

◆ open_or_create()

FILE * open_or_create ( const char * path,
const char * flags )

Performs an open(2)s with O_CREAT followed by fopen.

Here is the caller graph for this function:

◆ post()

bool post ( post_request r,
bool verbose,
struct mtrix_buffer * b )

Performs a POST request.

Parameters
rPOST url/payload.
bOutput buffer, resized as required.
verboseEmit debug output.
Here is the caller graph for this function:

◆ read_all()

bool read_all ( int fd,
void * p,
size_t n )

Repeatedly calls read(2) until all data are read.

Returns
true iff n bytes were read without error.

◆ request()

bool request ( const char * url,
struct mtrix_buffer * b,
bool verbose )

Performs a GET request.

Parameters
urlTarget RUL.
bOutput buffer, resized as required.
verboseEmit debug output.
Here is the caller graph for this function:

◆ strchrnul()

static const char * strchrnul ( const char * s,
int c )
inlinestatic

See glibc's function.

Here is the caller graph for this function:

◆ strlcpy()

static size_t strlcpy ( char *restrict dst,
const char *restrict src,
size_t n )
inlinestatic

See libbsd's function.

Here is the caller graph for this function:

◆ wait_n()

bool wait_n ( size_t n,
const pid_t * p )

Waits for n child processes to exit.

Here is the caller graph for this function:

◆ write_all()

bool write_all ( int fd,
const void * p,
size_t n )

Repeatedly calls write(2) until all data are written.

Returns
true iff n bytes were written without error.

Variable Documentation

◆ CMD_NAME

const char* CMD_NAME
extern

Command name, used as a second prefix for log messages if non-NULL.

Command name, used as a second prefix for log messages if non-NULL.

◆ PROG_NAME

const char* PROG_NAME
extern

Program name, used as a prefix for log messages if non-NULL.

Program name, used as a prefix for log messages if non-NULL.