machinatrix
A very silly Matrix bot written in C
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
5#include <stdbool.h>
6#include <stdio.h>
7
8#include <sys/types.h>
9
11#define MTRIX_MAX_PATH ((size_t)1024U)
12
14#define MTRIX_MAX_UNIX_PATH ((size_t)108U)
15
17#define MTRIX_MAX_URL_LEN ((size_t)1024U)
18
20#define MTRIX_MAX_ARGS ((size_t)2U)
21
23#define BUILD_URL(url, ...) build_url(url, (const char *[]){__VA_ARGS__, NULL})
24
26extern const char *PROG_NAME;
27
29extern const char *CMD_NAME;
30
32FILE *log_set(FILE *f);
33
37 char *p;
39 size_t n;
40};
41
46void log_err(const char *fmt, ...);
47
49void log_errv(const char *fmt, va_list argp);
50
56void log_errno(const char *fmt, ...);
57
59static const char *strchrnul(const char *s, int c);
60
62static size_t strlcpy(char *restrict dst, const char *restrict src, size_t n);
63
68char *is_prefix(const char *prefix, const char *s);
69
74bool copy_arg(const char *name, struct mtrix_buffer dst, const char *src);
75
77char *join_path(char v[static MTRIX_MAX_PATH], int n, ...);
78
80FILE *open_or_create(const char *path, const char *flags);
81
86bool read_all(int fd, void *p, size_t n);
87
92bool write_all(int fd, const void *p, size_t n);
93
103bool exec(const char *const *argv, int fin, int fout, int ferr);
104
106bool wait_n(size_t n, const pid_t *p);
107
109void join_lines(unsigned char *b, unsigned char *e);
110
113 const char *p, size_t size, size_t n, struct mtrix_buffer *b);
114
116bool build_url(char *url, const char *const *v);
117
124bool request(const char *url, struct mtrix_buffer *b, bool verbose);
125
127typedef struct {
129 const char *url;
131 size_t data_len;
133 const char *data;
135
142bool post(post_request r, bool verbose, struct mtrix_buffer *b);
143
144static inline const char *strchrnul(const char *s, int c) {
145 while(*s && *s != c)
146 ++s;
147 return s;
148}
149
150static inline size_t strlcpy(
151 char *restrict dst, const char *restrict src, size_t n)
152{
153 const char *const p = dst;
154 while(n && (*dst++ = *src++))
155 --n;
156 return (size_t)(dst - p);
157}
Resizable buffer used by several functions.
Definition utils.h:35
char * p
Owning pointer to the dynamically-allocated data.
Definition utils.h:37
size_t n
Size of the buffer pointed to by p.
Definition utils.h:39
Data used for POST requests.
Definition utils.h:127
const char * url
Target URL.
Definition utils.h:129
size_t data_len
Length of data.
Definition utils.h:131
const char * data
POST payload.
Definition utils.h:133
#define MTRIX_MAX_PATH
Maximum path length, arbitrarily chosen.
Definition utils.h:11
void log_err(const char *fmt,...)
Writes the printf-style message to the error output.
Definition utils.c:35
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.
Definition utils.c:184
bool post(post_request r, bool verbose, struct mtrix_buffer *b)
Performs a POST request.
Definition utils.c:255
char * is_prefix(const char *prefix, const char *s)
Checks if a string has a certain prefix.
Definition utils.c:51
bool wait_n(size_t n, const pid_t *p)
Waits for n child processes to exit.
Definition utils.c:149
bool build_url(char *url, const char *const *v)
Joins several URL parts into one, limited to MTRIX_MAX_URL_LEN.
Definition utils.c:208
FILE * open_or_create(const char *path, const char *flags)
Performs an open(2)s with O_CREAT followed by fopen.
Definition utils.c:94
FILE * log_set(FILE *f)
Sets the output stream used by log functions and returns previous value.
Definition utils.c:21
void log_errv(const char *fmt, va_list argp)
Definition utils.c:27
bool copy_arg(const char *name, struct mtrix_buffer dst, const char *src)
Copies a value from an argv-style array.
Definition utils.c:58
const char * CMD_NAME
Command name, used as a second prefix for log messages if non-NULL.
Definition main.c:56
char * join_path(char v[static MTRIX_MAX_PATH], int n,...)
Concatenate n segments, with length checking.
Definition utils.c:74
bool request(const char *url, struct mtrix_buffer *b, bool verbose)
Performs a GET request.
Definition utils.c:240
void join_lines(unsigned char *b, unsigned char *e)
Replaces new-line characters with spaces.
Definition utils.c:171
void log_errno(const char *fmt,...)
Similar to log_err, but also logs strerror(errno).
Definition utils.c:42
static size_t strlcpy(char *restrict dst, const char *restrict src, size_t n)
See libbsd's function.
Definition utils.h:150
static const char * strchrnul(const char *s, int c)
See glibc's function.
Definition utils.h:144
bool exec(const char *const *argv, int fin, int fout, int ferr)
Executes a command with optional input/output/error redirection.
Definition utils.c:136
bool read_all(int fd, void *p, size_t n)
Repeatedly calls read(2) until all data are read.
Definition utils.c:105
const char * PROG_NAME
Program name, used as a prefix for log messages if non-NULL.
Definition main.c:53
bool write_all(int fd, const void *p, size_t n)
Repeatedly calls write(2) until all data are written.
Definition utils.c:117