summaryrefslogtreecommitdiff
path: root/string.h
blob: 766469bce71f0c11ea7e12370451ca7fa8cd17fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* SPDX-License-Identifier: GPL-2.0 */

/** \file string.h exported symbols from string.c */

/** Flags that change how content is printed into the buffer. */
enum para_buffer_flags {
	/** Prefix each buffer with its length. */
	PBF_SIZE_PREFIX = 1,
};

/** A string buffer used for para_printf(). */
struct para_buffer {
	/** The buffer. May be \p NULL. */
	char *buf;
	/** The size of \a buf. */
	size_t size;
	/** The maximal size this buffer may grow. Zero means unlimited. */
	size_t max_size;
	/** \sa \ref para_buffer_flags. */
	unsigned flags;
	/** The next para_printf() will write at this offset. */
	size_t offset;
	/**
	 * If an attempt to print into this buffer is made that would need to
	 * grow \a buf to a size larger than \a max_size, then this function is
	 * called.
	 */
	int (*max_size_handler)(char *buf, size_t size, void *private_data);
	/** Passed to max_size_handler(). */
	void *private_data;
};

/**
 * Controls the behavior of for_each_line().
 *
 * \sa \ref for_each_line().
 */
enum for_each_line_flags {
	/** Activate read-only mode. */
	FELF_READ_ONLY = 1 << 0,
	/** Don't call line handler for the first input line. */
	FELF_DISCARD_FIRST = 1 << 1,
};

/** Used for \ref for_each_line(). */
typedef int line_handler_t(char *, void *);
int for_each_line(unsigned flags, char *buf, size_t size,
		line_handler_t *line_handler, void *private_data);

/**
 * Write the contents of a status item to a para_buffer.
 *
 * \param b The para_buffer.
 * \param n The number of the status item.
 * \param f A format string.
 *
 * \return The return value of the underlying call to \ref para_printf().
 */
#define WRITE_STATUS_ITEM(b, n, f, ...) (\
{ \
	if ((b)->flags & PBF_SIZE_PREFIX) { \
		para_printf((b), "%02x:" f, (unsigned)n, ## __VA_ARGS__); \
	} else { \
		para_printf((b), "%s: " f, status_item_list[(n)], \
			## __VA_ARGS__); \
	} \
} \
)

__must_check void *arr_realloc(void *ptr, size_t nmemb, size_t size);
__must_check void *para_realloc(void *p, size_t size);
__must_check __malloc void *alloc(size_t size);
__must_check __malloc void *zalloc(size_t size);
__must_check __malloc void *arr_alloc(size_t nmemb, size_t size);
__must_check __malloc void *arr_zalloc(size_t nmemb, size_t size);
__must_check __malloc char *para_strdup(const char *s);

__printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap);
__printf_2_3 unsigned xasprintf(char **result, const char *fmt, ...);
__must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...);
__must_check __malloc char *para_logname(void);
__must_check __malloc char *para_homedir(void);
const char *para_hostname(void);
__printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...);
int para_atoi64(const char *str, int64_t *result);
int para_atoi32(const char *str, int32_t *value);
int create_argv(const char *buf, const char *delim, char ***result);
int create_shifted_argv(const char *buf, const char *delim, char ***result);
void free_argv(char **argv);
int para_regcomp(regex_t *preg, const char *regex, int cflags);
void freep(void *arg);
int compute_word_num(const char *buf, const char *delim, int offset);
char *safe_strdup(const char *src, size_t len);
char *key_value_copy(const char *src, size_t len, const char *key);
int skip_cells(const char *s, size_t cells_to_skip, size_t *result);
__must_check int strwidth(const char *s, size_t *result);
__must_check int sanitize_str(const char *src, size_t max_width,
		char **result, size_t *width);
/**
 * Return the single line git version string.
 *
 * This is what the version subcommands of para_server and para_audiod output.
 * The function is defined in an auto-generated .c file which gets updated by
 * the build system when the git version changes.
 *
 * \return A pointer to a constant string. Don't free!
 */
const char *paraslash_version(void);

/**
 * Return multi-line version information (license, links, etc.).
 *
 * All paraslash executables output this when run with -V. Also the version
 * subcommands of para_server and para_audiod send this string to the client if
 * the verbose flag was given. The function is defined in the same
 * auto-generated file as \ref paraslash_version().
 *
 * \return A pointer to a constant string. Don't free!
 */
const char *paraslash_info(void);

const char *version_single_line(const char *pfx);
const char *version_text(const char *pfx);
void version_handle_flag(const char *pfx, bool flag);