cfg_private.h 3.95 KB
Newer Older
1
/*
2
 * Copyright (c) 2015, 2017 Moritz Grimm <mgrimm@mrsserver.net>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef __CFG_PRIVATE_H__
#define __CFG_PRIVATE_H__

#include "cfg.h"

22
23
#include <sys/queue.h>

24
25
26
27
28
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

29
30
31
32
33
34
35
36
37
38
39
40
#define UCREDS_SIZE	256
#define CSUITE_SIZE	2048

struct cfg_program {
	char			 name[PATH_MAX];
	enum cfg_config_type	 config_type;
	char			 config_file[PATH_MAX];
	char			 pid_file[PATH_MAX];
	int			 quiet_stderr;
	int			 rtstatus_output;
	unsigned int		 verbosity;
};
41
42

struct cfg {
43
44
45
	cfg_decoder_list_t		 decoders;
	cfg_encoder_list_t		 encoders;
	cfg_intake_list_t		 intakes;
46
47
	cfg_server_list_t		 servers;
	cfg_stream_list_t		 streams;
48
49
50
	struct metadata {
		char			 program[PATH_MAX];
		char			*format_str;
51
		int			 refresh_interval;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
		int			 normalize_strings;
		int			 no_updates;
	} metadata;
};

#define SET_STRLCPY(t, s, e)	do {		\
	if (!(s) || !(s)[0]) {			\
		if ((e))			\
			*(e) = "empty"; 	\
		return (-1);			\
	}					\
	if (sizeof((t)) <=			\
	    strlcpy((t), (s), sizeof((t)))) {	\
		if ((e))			\
			*(e) = "too long";	\
		return (-1);			\
	}					\
} while (0)

71
72
73
74
75
76
#define SET_XSTRDUP(t, s, e)	do {		\
	if (!(s) || !(s)[0]) {			\
		if ((e))			\
			*(e) = "empty"; 	\
		return (-1);			\
	}					\
77
	xfree((t));				\
78
79
80
	(t) = xstrdup((s));			\
} while (0)

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#define SET_BOOLEAN(t, s, e)	do {			\
	int	val;					\
	if (!(s) || !(s)[0]) {				\
		if ((e))				\
			*(e) = "empty"; 		\
		return (-1);				\
	}						\
	if (0 == strcasecmp((s), "true") ||		\
	    0 == strcasecmp((s), "yes") ||		\
	    0 == strcasecmp((s), "1")) {		\
		val = 1;				\
	} else if (0 == strcasecmp((s), "false") ||	\
	    0 == strcasecmp((s), "no") ||		\
	    0 == strcasecmp((s), "0")) {		\
		val = 0;				\
	} else {					\
		if ((e))				\
			*(e) = "invalid";		\
		return (-1);				\
	}						\
	(t) = val;					\
} while (0)

Moritz Grimm's avatar
Moritz Grimm committed
104
105
106
107
108
109
110
111
112
113
#define SET_UINTNUM(t, s, e)	do {			\
	const char	*errstr;			\
	unsigned int	 num;				\
							\
	if (!(s) || !(s)[0]) {				\
		if ((e))				\
			*(e) = "empty"; 		\
		return (-1);				\
	}						\
							\
114
	num = (unsigned int)strtonum((s), 0, UINT_MAX, &errstr); \
Moritz Grimm's avatar
Moritz Grimm committed
115
116
117
118
119
120
121
122
	if (errstr) {					\
		if ((e))				\
			*(e) = errstr;			\
		return (-1);				\
	}						\
	(t) = num;					\
} while (0)

123
124
#define SET_INTNUM(t, s, e)	do {			\
	const char	*errstr;			\
125
	int		 num;				\
126
127
128
129
130
131
132
							\
	if (!(s) || !(s)[0]) {				\
		if ((e))				\
			*(e) = "empty"; 		\
		return (-1);				\
	}						\
							\
133
	num = (int)strtonum((s), INT_MIN, INT_MAX, &errstr); \
134
135
136
137
138
139
140
141
	if (errstr) {					\
		if ((e))				\
			*(e) = errstr;			\
		return (-1);				\
	}						\
	(t) = num;					\
} while (0)

142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#define CHECKPH_PROHIBITED(s, p)	do {				\
	if (NULL != strstr((s), (p))) { 				\
		if (errstrp)						\
			*errstrp = "prohibited placeholder " p; 	\
		return (-1);						\
	}								\
} while (0)

#define CHECKPH_DUPLICATE(s, p)	do {				\
	char	*c;							\
	if (NULL != (c = strstr((s), (p)))) {				\
		c += strlen((p));					\
		if (NULL != strstr(c, (p))) {				\
			if (errstrp)					\
				*errstrp = "duplicate placeholder " p;	\
			return (-1);					\
		}							\
	}								\
} while (0)

#define CHECKPH_REQUIRED(s, p)	do {				\
	if (NULL == strstr((s), (p))) { 				\
		if (errstrp)						\
			*errstrp = "missing placeholder " p;		\
		return (-1);						\
	}								\
} while (0)

#endif /* __CFG_PRIVATE_H__ */