| 1 | /* |
| 2 | * Copyright (c) 2015, 2017 Moritz Grimm <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #ifndef __CFG_PRIVATE_H__ |
| 18 | #define __CFG_PRIVATE_H__ |
| 19 | |
| 20 | #include "cfg.h" |
| 21 | |
| 22 | #include <sys/queue.h> |
| 23 | |
| 24 | #include <limits.h> |
| 25 | #include <netdb.h> |
| 26 | #include <stdio.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #define UCREDS_SIZE 256 |
| 30 | #define CSUITE_SIZE 2048 |
| 31 | |
| 32 | struct cfg_program { |
| 33 | char name[PATH_MAX]; |
| 34 | enum cfg_config_type config_type; |
| 35 | char config_file[PATH_MAX]; |
| 36 | char pid_file[PATH_MAX]; |
| 37 | int quiet_stderr; |
| 38 | int rtstatus_output; |
| 39 | unsigned int verbosity; |
| 40 | }; |
| 41 | |
| 42 | struct cfg { |
| 43 | cfg_decoder_list_t decoders; |
| 44 | cfg_encoder_list_t encoders; |
| 45 | cfg_intake_list_t intakes; |
| 46 | cfg_server_list_t servers; |
| 47 | cfg_stream_list_t streams; |
| 48 | struct metadata { |
| 49 | char program[PATH_MAX]; |
| 50 | char *format_str; |
| 51 | int refresh_interval; |
| 52 | int normalize_strings; |
| 53 | int no_updates; |
| 54 | } metadata; |
| 55 | }; |
| 56 | |
| 57 | #define SET_STRLCPY(t, s, e) do { \ |
| 58 | if (!(s) || !(s)[0]) { \ |
| 59 | if ((e)) \ |
| 60 | *(e) = "empty"; \ |
| 61 | return (-1); \ |
| 62 | } \ |
| 63 | if (sizeof((t)) <= \ |
| 64 | strlcpy((t), (s), sizeof((t)))) { \ |
| 65 | if ((e)) \ |
| 66 | *(e) = "too long"; \ |
| 67 | return (-1); \ |
| 68 | } \ |
| 69 | } while (0) |
| 70 | |
| 71 | #define SET_XSTRDUP(t, s, e) do { \ |
| 72 | if (!(s) || !(s)[0]) { \ |
| 73 | if ((e)) \ |
| 74 | *(e) = "empty"; \ |
| 75 | return (-1); \ |
| 76 | } \ |
| 77 | xfree((t)); \ |
| 78 | (t) = xstrdup((s)); \ |
| 79 | } while (0) |
| 80 | |
| 81 | #define SET_BOOLEAN(t, s, e) do { \ |
| 82 | int val; \ |
| 83 | if (!(s) || !(s)[0]) { \ |
| 84 | if ((e)) \ |
| 85 | *(e) = "empty"; \ |
| 86 | return (-1); \ |
| 87 | } \ |
| 88 | if (0 == strcasecmp((s), "true") || \ |
| 89 | 0 == strcasecmp((s), "yes") || \ |
| 90 | 0 == strcasecmp((s), "1")) { \ |
| 91 | val = 1; \ |
| 92 | } else if (0 == strcasecmp((s), "false") || \ |
| 93 | 0 == strcasecmp((s), "no") || \ |
| 94 | 0 == strcasecmp((s), "0")) { \ |
| 95 | val = 0; \ |
| 96 | } else { \ |
| 97 | if ((e)) \ |
| 98 | *(e) = "invalid"; \ |
| 99 | return (-1); \ |
| 100 | } \ |
| 101 | (t) = val; \ |
| 102 | } while (0) |
| 103 | |
| 104 | #define SET_UINTNUM(t, s, e) do { \ |
| 105 | const char *errstr; \ |
| 106 | unsigned int num; \ |
| 107 | \ |
| 108 | if (!(s) || !(s)[0]) { \ |
| 109 | if ((e)) \ |
| 110 | *(e) = "empty"; \ |
| 111 | return (-1); \ |
| 112 | } \ |
| 113 | \ |
| 114 | num = (unsigned int)strtonum((s), 0, UINT_MAX, &errstr); \ |
| 115 | if (errstr) { \ |
| 116 | if ((e)) \ |
| 117 | *(e) = errstr; \ |
| 118 | return (-1); \ |
| 119 | } \ |
| 120 | (t) = num; \ |
| 121 | } while (0) |
| 122 | |
| 123 | #define SET_INTNUM(t, s, e) do { \ |
| 124 | const char *errstr; \ |
| 125 | int num; \ |
| 126 | \ |
| 127 | if (!(s) || !(s)[0]) { \ |
| 128 | if ((e)) \ |
| 129 | *(e) = "empty"; \ |
| 130 | return (-1); \ |
| 131 | } \ |
| 132 | \ |
| 133 | num = (int)strtonum((s), INT_MIN, INT_MAX, &errstr); \ |
| 134 | if (errstr) { \ |
| 135 | if ((e)) \ |
| 136 | *(e) = errstr; \ |
| 137 | return (-1); \ |
| 138 | } \ |
| 139 | (t) = num; \ |
| 140 | } while (0) |
| 141 | |
| 142 | #define CHECKPH_PROHIBITED(s, p) do { \ |
| 143 | if (NULL != strstr((s), (p))) { \ |
| 144 | if (errstrp) \ |
| 145 | *errstrp = "prohibited placeholder " p; \ |
| 146 | return (-1); \ |
| 147 | } \ |
| 148 | } while (0) |
| 149 | |
| 150 | #define CHECKPH_DUPLICATE(s, p) do { \ |
| 151 | char *c; \ |
| 152 | if (NULL != (c = strstr((s), (p)))) { \ |
| 153 | c += strlen((p)); \ |
| 154 | if (NULL != strstr(c, (p))) { \ |
| 155 | if (errstrp) \ |
| 156 | *errstrp = "duplicate placeholder " p; \ |
| 157 | return (-1); \ |
| 158 | } \ |
| 159 | } \ |
| 160 | } while (0) |
| 161 | |
| 162 | #define CHECKPH_REQUIRED(s, p) do { \ |
| 163 | if (NULL == strstr((s), (p))) { \ |
| 164 | if (errstrp) \ |
| 165 | *errstrp = "missing placeholder " p; \ |
| 166 | return (-1); \ |
| 167 | } \ |
| 168 | } while (0) |
| 169 | |
| 170 | #endif /* __CFG_PRIVATE_H__ */ |