]> git.tuebingen.mpg.de Git - paraslash.git/blob - para.h
Teach command_util to print also completions.
[paraslash.git] / para.h
1 /*
2  * Copyright (C) 1997-2011 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file para.h global paraslash definitions */
8
9 #include "config.h"
10
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <sys/wait.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <time.h> /* time(), localtime() */
17 #include <unistd.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdarg.h>
21 #include <ctype.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24 #include <sys/socket.h>
25 #include <sys/un.h> /* needed by create_pf_socket */
26 #include <string.h>
27 #include <assert.h>
28 #include "gcc-compat.h"
29
30 /** used in various contexts */
31 #define MAXLINE 255
32
33 /** Compute the minimum of \a x and \a y. */
34 #define PARA_MIN(x, y) ({ \
35         typeof(x) _min1 = (x); \
36         typeof(y) _min2 = (y); \
37         (void) (&_min1 == &_min2); \
38         _min1 < _min2 ? _min1 : _min2; })
39
40 /** Compute the maximum of \a x and \a y. */
41 #define PARA_MAX(x, y) ({ \
42         typeof(x) _max1 = (x); \
43         typeof(y) _max2 = (y); \
44         (void) (&_max1 == &_max2); \
45         _max1 < _max2 ? _max2 : _max1; })
46
47 /** Compute the absolute value of \a x. */
48 #define PARA_ABS(x) ({ \
49         typeof(x) _x = (x); \
50         _x > 0? _x : -_x; })
51
52
53 extern __printf_2_3 void (*para_log)(int, const char*, ...);
54 /**
55  * Define a standard log function that always writes to stderr.
56  *
57  * \param funcname The name of the function to be defined.
58  *
59  * \param loglevel_barrier If the loglevel of the current message
60  * is less than that, the message is going to be ignored.
61  *
62  */
63 #define DEFINE_STDERR_LOGGER(funcname, loglevel_barrier) \
64         __printf_2_3 void funcname(int ll, const char* fmt,...) \
65         { \
66                 va_list argp; \
67                 if (ll < loglevel_barrier) \
68                         return; \
69                 va_start(argp, fmt); \
70                 vfprintf(stderr, fmt, argp); \
71                 va_end(argp); \
72         }
73 #define INIT_STDERR_LOGGING(loglevel_barrier) \
74         DEFINE_STDERR_LOGGER(stderr_log, loglevel_barrier); \
75         __printf_2_3 void (*para_log)(int, const char*, ...) = stderr_log;
76
77 /** Sent by para_client to initiate the authentication procedure. */
78 #define AUTH_REQUEST_MSG "auth rsa "
79 /** Sent by para_server for commands that expect a data file. */
80 #define AWAITING_DATA_MSG "\nAwaiting Data."
81 /** Sent by para_server if authentication was successful. */
82 #define PROCEED_MSG "Proceed."
83 /** Length of the \p PROCEED_MSG string. */
84 #define PROCEED_MSG_LEN strlen(PROCEED_MSG)
85 /** Sent by para_client to indicate the end of the command line. */
86 #define EOC_MSG "\nEnd of Command."
87
88 /* exec */
89 int para_exec_cmdline_pid(pid_t *pid, const char *cmdline, int *fds);
90
91 /* time */
92 int tv_diff(const struct timeval *b, const struct timeval *a, struct timeval *diff);
93 long unsigned tv2ms(const struct timeval*);
94 void d2tv(double, struct timeval*);
95 void tv_add(const struct timeval*, const struct timeval *, struct timeval *);
96 void tv_scale(const unsigned long, const struct timeval *, struct timeval *);
97 void tv_divide(const unsigned long divisor, const struct timeval *tv,
98         struct timeval *result);
99 int tv_convex_combination(const long a, const struct timeval *tv1,
100                 const long b, const struct timeval *tv2,
101                 struct timeval *result);
102 void ms2tv(const long unsigned n, struct timeval *tv);
103 void compute_chunk_time(long unsigned chunk_num,
104                 struct timeval *chunk_tv, struct timeval *stream_start,
105                 struct timeval *result);
106
107 /** The enum of all status items. */
108 enum status_items {STATUS_ITEM_ENUM NUM_STAT_ITEMS};
109 extern const char *status_item_list[];
110 /** Loop over each status item. */
111 #define FOR_EACH_STATUS_ITEM(i) for (i = 0; i < NUM_STAT_ITEMS; i++)
112 int for_each_stat_item(char *item_buf, size_t num_bytes,
113         int (*item_handler)(int, char *));
114
115 /**
116  * Write a log message to a dynamically allocated string.
117  *
118  * \param fmt Usual format string.
119  * \param p Result pointer.
120  *
121  * \sa printf(3). */
122 #define PARA_VSPRINTF(fmt, p) \
123 { \
124         int n; \
125         size_t size = 100; \
126         p = para_malloc(size); \
127         while (1) { \
128                 va_list ap; \
129                 /* Try to print in the allocated space. */ \
130                 va_start(ap, fmt); \
131                 n = vsnprintf(p, size, fmt, ap); \
132                 va_end(ap); \
133                 /* If that worked, return the string. */ \
134                 if (n > -1 && n < size) \
135                         break; \
136                 /* Else try again with more space. */ \
137                 if (n > -1) /* glibc 2.1 */ \
138                         size = n + 1; /* precisely what is needed */ \
139                 else /* glibc 2.0 */ \
140                         size *= 2; /* twice the old size */ \
141                 p = para_realloc(p, size); \
142         } \
143 }
144
145 /**
146  *  Return a random non-negative integer in an interval.
147  *
148  * \param max Determines maximal possible return value.
149  *
150  * \return An integer between zero and \p max - 1, inclusively.
151  */
152 _static_inline_ long int para_random(unsigned max)
153 {
154         return ((max + 0.0) * (random() / (RAND_MAX + 1.0)));
155 }
156
157 /** Round up x to next multiple of y. */
158 #define ROUND_UP(x, y) ({ \
159         const typeof(y) _divisor = y; \
160         ((x) + _divisor - 1) / _divisor * _divisor; })
161
162 /** Round down x to multiple of y. */
163 #define ROUND_DOWN(x, y) ({ \
164         const typeof(y) _divisor = y; \
165         (x) / _divisor * _divisor; })
166
167 /** Divide and round up to next integer. */
168 #define DIV_ROUND_UP(x, y) ({ \
169         typeof(y) _divisor = y; \
170         ((x) + _divisor - 1) / _divisor; })
171
172 /**
173  * Assert a build-time dependency, as an expression.
174  *
175  * \param cond The compile-time condition which must be true.
176  *
177  * Compilation will fail if the condition isn't true, or can't be evaluated by
178  * the compiler. This can be used in an expression: its value is "0".
179  *
180  * Taken from ccan.
181  */
182 #define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2 * !(cond)]) - 1)
183
184 /** &a[0] degrades to a pointer: a different type from an array */
185 #define _array_size_chk(arr) EXPR_BUILD_ASSERT(\
186         !__builtin_types_compatible_p(typeof(arr), typeof(&(arr)[0])))
187 /** Get the size of an array */
188 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + _array_size_chk(arr))
189
190 /**
191  * Wrapper for isspace.
192  * NetBSD needs this.
193  */
194 /*
195  * The values should be cast to an unsigned char first, then to int.
196  * Why? Because the isdigit (as do all other is/to functions/macros)
197  * expect a number from 0 upto and including 255 as their (int) argument.
198  * Because char is signed on most systems, casting it to int immediately
199  * gives the functions an argument between -128 and 127 (inclusive),
200  * which they will use as an array index, and which will thus fail
201  * horribly for characters which have their most significant bit set.
202  */
203 #define para_isspace(c) isspace((int)(unsigned char)(c))
204
205 /** Data that indicates an eof-condition for a fec-encoded stream. */
206 #define FEC_EOF_PACKET "\xec\x0d\xcc\xfe\0\0\0\0" \
207         "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0"
208 /** The number of bytes of the \a FEC_EOF_PACKET. */
209 #define FEC_EOF_PACKET_LEN 32
210
211 /** Used to avoid a shortcoming in vim's syntax highlighting. */
212 #define EMBRACE(...) { __VA_ARGS__}
213
214 /** A nice cup of STFU for Mr gcc. */
215 #define do_nothing do {/* nothing */} while (0)
216
217 /**
218  * The sample formats supported by paraslash.
219  *
220  * It may be determined by one of the following sources:
221  *
222  *      1. The decoding filter (para_audiod only). In this case, it is always
223  *      \p SF_S16_LE which is the canonical format used within decoders.
224  *
225  *      2. The wav header (para_write only).
226  *
227  *      3. The --format option of para_write.
228  */
229 #define SAMPLE_FORMATS \
230         SAMPLE_FORMAT(SF_S8, "8 bit signed"), \
231         SAMPLE_FORMAT(SF_U8, "8 bit unsigned"), \
232         SAMPLE_FORMAT(SF_S16_LE, "16 bit signed, little endian"), \
233         SAMPLE_FORMAT(SF_S16_BE, "16 bit signed, big endian"), \
234         SAMPLE_FORMAT(SF_U16_LE, "16 bit unsigned, little endian"), \
235         SAMPLE_FORMAT(SF_U16_BE, "16 bit unsigned, big endian"), \
236
237 /** \cond sample_format */
238 #define SAMPLE_FORMAT(a, b) a
239 enum sample_format {SAMPLE_FORMATS};
240 #undef SAMPLE_FORMAT
241 #define SAMPLE_FORMAT(a, b) b
242 /** \endcond sample_format */
243
244 /** Debug loglevel, gets really noisy. */
245 #define LL_DEBUG 0
246 /** Still noisy, but won't fill your disk. */
247 #define LL_INFO  1
248 /** Normal, but significant event. */
249 #define LL_NOTICE 2
250 /** Unexpected event that can be handled. */
251 #define LL_WARNING 3
252 /** Unhandled error condition. */
253 #define LL_ERROR 4
254 /** System might be unreliable. */
255 #define LL_CRIT 5
256 /** Last message before exit. */
257 #define LL_EMERG 6
258 /** Number of all loglevels. */
259 #define NUM_LOGLEVELS 7
260
261 /** Log messages with lower priority than that will not be compiled in. */
262 #define COMPILE_TIME_LOGLEVEL 0
263
264 /** \cond log */
265 #if LL_DEBUG >= COMPILE_TIME_LOGLEVEL
266 #define PARA_DEBUG_LOG(f,...) para_log(LL_DEBUG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
267 #else
268 #define PARA_DEBUG_LOG(...) do {;} while (0)
269 #endif
270
271 #if LL_INFO >= COMPILE_TIME_LOGLEVEL
272 #define PARA_INFO_LOG(f,...) para_log(LL_INFO, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
273 #else
274 #define PARA_INFO_LOG(...) do {;} while (0)
275 #endif
276
277 #if LL_NOTICE >= COMPILE_TIME_LOGLEVEL
278 #define PARA_NOTICE_LOG(f,...) para_log(LL_NOTICE, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
279 #else
280 #define PARA_NOTICE_LOG(...) do {;} while (0)
281 #endif
282
283 #if LL_WARNING >= COMPILE_TIME_LOGLEVEL
284 #define PARA_WARNING_LOG(f,...) para_log(LL_WARNING, "%s: " f, __FUNCTION__, ##  __VA_ARGS__)
285 #else
286 #define PARA_WARNING_LOG(...) do {;} while (0)
287 #endif
288
289 #if LL_ERROR >= COMPILE_TIME_LOGLEVEL
290 #define PARA_ERROR_LOG(f,...) para_log(LL_ERROR, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
291 #else
292 #define PARA_ERROR_LOG(...) do {;} while (0)
293 #endif
294
295 #if LL_CRIT >= COMPILE_TIME_LOGLEVEL
296 #define PARA_CRIT_LOG(f,...) para_log(LL_CRIT, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
297 #else
298 #define PARA_CRIT_LOG(...) do {;} while (0)
299 #endif
300
301 #if LL_EMERG >= COMPILE_TIME_LOGLEVEL
302 #define PARA_EMERG_LOG(f,...) para_log(LL_EMERG, "%s: " f, __FUNCTION__, ## __VA_ARGS__)
303 #else
304 #define PARA_EMERG_LOG(...)
305 #endif
306 /** \endcond log */