machinatrix
A very silly Matrix bot written in C
|
Utility functions. More...
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
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 . | |
Utility functions.
#define BUILD_URL | ( | url, | |
... ) build_url(url, (const char *[]){__VA_ARGS__, NULL}) |
Helper macro for build_url that creates a const char *
array.
#define MTRIX_MAX_ARGS ((size_t)2U) |
Maximum number of command arguments (excluding the command name).
#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.
bool build_url | ( | char * | url, |
const char *const * | v ) |
Joins several URL parts into one, limited to MTRIX_MAX_URL_LEN.
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.
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.
argv | execv -style argument list. |
fin | Substitute for stdin . |
fout | Substitute for stdout . |
ferr | Substitute for stderr . |
char * is_prefix | ( | const char * | prefix, |
const char * | s ) |
Checks if a string has a certain prefix.
NULL
if not a prefix. void join_lines | ( | unsigned char * | b, |
unsigned char * | e ) |
Replaces new-line characters with spaces.
char * join_path | ( | char | v[static MTRIX_MAX_PATH], |
int | n, | ||
... ) |
Concatenate n
segments, with length checking.
void log_err | ( | const char * | fmt, |
... ) |
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.
void log_errv | ( | const char * | fmt, |
va_list | argp ) |
FILE * log_set | ( | FILE * | f | ) |
Sets the output stream used by log functions and returns previous value.
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.
FILE * open_or_create | ( | const char * | path, |
const char * | flags ) |
Performs an open(2)s
with O_CREAT
followed by fopen
.
bool post | ( | post_request | r, |
bool | verbose, | ||
struct mtrix_buffer * | b ) |
Performs a POST
request.
r | POST url/payload. |
b | Output buffer, resized as required. |
verbose | Emit debug output. |
bool read_all | ( | int | fd, |
void * | p, | ||
size_t | n ) |
Repeatedly calls read(2)
until all data are read.
true
iff n
bytes were read without error. bool request | ( | const char * | url, |
struct mtrix_buffer * | b, | ||
bool | verbose ) |
Performs a GET
request.
url | Target RUL. |
b | Output buffer, resized as required. |
verbose | Emit debug output. |
|
inlinestatic |
See glibc's function.
|
inlinestatic |
See libbsd's function.
bool wait_n | ( | size_t | n, |
const pid_t * | p ) |
Waits for n
child processes to exit.
bool write_all | ( | int | fd, |
const void * | p, | ||
size_t | n ) |
Repeatedly calls write(2)
until all data are written.
true
iff n
bytes were written without error.
|
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
.
|
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
.