Make mpg123 behave more nicely
[asterisk/asterisk.git] / include / asterisk / utils.h
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Utility functions
5  *
6  * Copyright (C) 2004 - 2005, Digium, Inc.
7  *
8  * This program is free software, distributed under the terms of
9  * the GNU General Public License
10  */
11
12 #ifndef _ASTERISK_UTIL_H
13 #define _ASTERISK_UTIL_H
14
15 #include <netinet/in.h>
16 #include <netdb.h>
17 #include <pthread.h>
18 #include <asterisk/lock.h>
19 #include <limits.h>
20
21 /* Note:
22    It is very important to use only unsigned variables to hold
23    bit flags, as otherwise you can fall prey to the compiler's
24    sign-extension antics if you try to use the top two bits in
25    your variable.
26
27    The flag macros below use a set of compiler tricks to verify
28    that the caller is using an "unsigned int" variable to hold
29    the flags, and nothing else. If the caller uses any other
30    type of variable, a warning message similar to this:
31
32    warning: comparison of distinct pointer types lacks cast
33
34    will be generated.
35
36    The "dummy" variable below is used to make these comparisons.
37
38    Also note that at -O2 or above, this type-safety checking
39    does _not_ produce any additional object code at all.
40 */
41
42 extern unsigned int __unsigned_int_flags_dummy;
43
44 #define ast_test_flag(p,flag)           ({ \
45                                         typeof ((p)->flags) __p = (p)->flags; \
46                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
47                                         (void) (&__p == &__x); \
48                                         ((p)->flags & (flag)); \
49                                         })
50
51 #define ast_set_flag(p,flag)            do { \
52                                         typeof ((p)->flags) __p = (p)->flags; \
53                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
54                                         (void) (&__p == &__x); \
55                                         ((p)->flags |= (flag)); \
56                                         } while(0)
57
58 #define ast_clear_flag(p,flag)          do { \
59                                         typeof ((p)->flags) __p = (p)->flags; \
60                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
61                                         (void) (&__p == &__x); \
62                                         ((p)->flags &= ~(flag)); \
63                                         } while(0)
64
65 #define ast_copy_flags(dest,src,flagz)  do { \
66                                         typeof ((dest)->flags) __d = (dest)->flags; \
67                                         typeof ((src)->flags) __s = (src)->flags; \
68                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
69                                         (void) (&__d == &__x); \
70                                         (void) (&__s == &__x); \
71                                         (dest)->flags &= ~(flagz); \
72                                         (dest)->flags |= ((src)->flags & (flagz)); \
73                                         } while (0)
74
75 #define ast_set2_flag(p,value,flag)     do { \
76                                         typeof ((p)->flags) __p = (p)->flags; \
77                                         typeof (__unsigned_int_flags_dummy) __x = 0; \
78                                         (void) (&__p == &__x); \
79                                         if (value) \
80                                                 (p)->flags |= (flag); \
81                                         else \
82                                                 (p)->flags &= ~(flag); \
83                                         } while (0)
84
85 /* Non-type checking variations for non-unsigned int flags.  You
86    should only use non-unsigned int flags where required by 
87    protocol etc and if you know what you're doing :)  */
88 #define ast_test_flag_nonstd(p,flag)            ({ \
89                                         ((p)->flags & (flag)); \
90                                         })
91
92 #define ast_set_flag_nonstd(p,flag)             do { \
93                                         ((p)->flags |= (flag)); \
94                                         } while(0)
95
96 #define ast_clear_flag_nonstd(p,flag)           do { \
97                                         ((p)->flags &= ~(flag)); \
98                                         } while(0)
99
100 #define ast_copy_flags_nonstd(dest,src,flagz)   do { \
101                                         (dest)->flags &= ~(flagz); \
102                                         (dest)->flags |= ((src)->flags & (flagz)); \
103                                         } while (0)
104
105 #define ast_set2_flag_nonstd(p,value,flag)      do { \
106                                         if (value) \
107                                                 (p)->flags |= (flag); \
108                                         else \
109                                                 (p)->flags &= ~(flag); \
110                                         } while (0)
111
112 #define AST_FLAGS_ALL UINT_MAX
113
114 struct ast_flags {
115         unsigned int flags;
116 };
117
118 static inline int ast_strlen_zero(const char *s)
119 {
120         return (*s == '\0');
121 }
122
123 struct ast_hostent {
124         struct hostent hp;
125         char buf[1024];
126 };
127
128 extern char *ast_strip(char *buf);
129 extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
130 /* ast_md5_hash: Produces MD5 hash based on input string */
131 extern void ast_md5_hash(char *output, char *input);
132 extern int ast_base64encode(char *dst, unsigned char *src, int srclen, int max);
133 extern int ast_base64decode(unsigned char *dst, char *src, int max);
134
135 extern int test_for_thread_safety(void);
136
137 extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia);
138 extern int ast_utils_init(void);
139 extern int ast_wait_for_input(int fd, int ms);
140
141 /* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
142
143 struct ast_realloca {
144         char *ptr;
145         int alloclen;
146 };
147
148 #define ast_restrdupa(ra, s) \
149         ({ \
150                 if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \
151                         strcpy((ra)->ptr, s); \
152                 } else { \
153                         (ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \
154                         if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \
155                 } \
156                 (ra)->ptr; \
157         })
158
159 #ifdef inet_ntoa
160 #undef inet_ntoa
161 #endif
162 #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
163
164 #define AST_STACKSIZE 256 * 1024
165 #define ast_pthread_create(a,b,c,d) ast_pthread_create_stack(a,b,c,d,0)
166 #ifdef __linux__
167 #define ast_strcasestr strcasestr
168 #else
169 extern char *ast_strcasestr(const char *, const char *);
170 #endif /* __linux__ */
171 extern int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize);
172 #endif