Skip to content
Snippets Groups Projects
extconf.c 181 KiB
Newer Older
  • Learn to ignore specific revisions
  • 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641
      \param type This is the type of each list entry.
    
      This macro creates a structure definition that can be used
      to hold a list of the entries of type \a type. It does not actually
      declare (allocate) a structure; to do that, either follow this
      macro with the desired name of the instance you wish to declare,
      or use the specified \a name to declare instances elsewhere.
    
      Example usage:
      \code
      static AST_LIST_HEAD_NOLOCK(entry_list, entry) entries;
      \endcode
    
      This would define \c struct \c entry_list, and declare an instance of it named
      \a entries, all intended to hold a list of type \c struct \c entry.
    */
    #define AST_LIST_HEAD_NOLOCK(name, type)				\
    struct name {								\
    	struct type *first;						\
    	struct type *last;						\
    }
    
    /*!
      \brief Defines initial values for a declaration of AST_LIST_HEAD
    */
    #define AST_LIST_HEAD_INIT_VALUE	{		\
    	.first = NULL,					\
    	.last = NULL,					\
    	.lock = AST_MUTEX_INIT_VALUE,			\
    	}
    
    /*!
      \brief Defines initial values for a declaration of AST_RWLIST_HEAD
    */
    #define AST_RWLIST_HEAD_INIT_VALUE      {               \
            .first = NULL,                                  \
            .last = NULL,                                   \
            .lock = AST_RWLOCK_INIT_VALUE,                  \
            }
    
    /*!
      \brief Defines initial values for a declaration of AST_LIST_HEAD_NOLOCK
    */
    #define AST_LIST_HEAD_NOLOCK_INIT_VALUE	{	\
    	.first = NULL,					\
    	.last = NULL,					\
    	}
    
    /*!
      \brief Defines a structure to be used to hold a list of specified type, statically initialized.
      \param name This will be the name of the defined structure.
      \param type This is the type of each list entry.
    
      This macro creates a structure definition that can be used
      to hold a list of the entries of type \a type, and allocates an instance
      of it, initialized to be empty.
    
      Example usage:
      \code
      static AST_LIST_HEAD_STATIC(entry_list, entry);
      \endcode
    
      This would define \c struct \c entry_list, intended to hold a list of
      type \c struct \c entry.
    */
    #if defined(AST_MUTEX_INIT_W_CONSTRUCTORS)
    #define AST_LIST_HEAD_STATIC(name, type)				\
    struct name {								\
    	struct type *first;						\
    	struct type *last;						\
    	ast_mutex_t lock;						\
    } name;									\
    static void  __attribute__ ((constructor)) init_##name(void)		\
    {									\
            AST_LIST_HEAD_INIT(&name);					\
    }									\
    static void  __attribute__ ((destructor)) fini_##name(void)		\
    {									\
            AST_LIST_HEAD_DESTROY(&name);					\
    }									\
    struct __dummy_##name
    #else
    #define AST_LIST_HEAD_STATIC(name, type)				\
    struct name {								\
    	struct type *first;						\
    	struct type *last;						\
    	ast_mutex_t lock;						\
    } name = AST_LIST_HEAD_INIT_VALUE
    #endif
    
    /*!
      \brief Defines a structure to be used to hold a read/write list of specified type, statically initialized.
      \param name This will be the name of the defined structure.
      \param type This is the type of each list entry.
    
      This macro creates a structure definition that can be used
      to hold a list of the entries of type \a type, and allocates an instance
      of it, initialized to be empty.
    
      Example usage:
      \code
      static AST_RWLIST_HEAD_STATIC(entry_list, entry);
      \endcode
    
      This would define \c struct \c entry_list, intended to hold a list of
      type \c struct \c entry.
    */
    #ifndef AST_RWLOCK_INIT_VALUE
    #define AST_RWLIST_HEAD_STATIC(name, type)                              \
    struct name {                                                           \
            struct type *first;                                             \
            struct type *last;                                              \
            ast_rwlock_t lock;                                              \
    } name;                                                                 \
    static void  __attribute__ ((constructor)) init_##name(void)            \
    {                                                                       \
            AST_RWLIST_HEAD_INIT(&name);                                    \
    }                                                                       \
    static void  __attribute__ ((destructor)) fini_##name(void)             \
    {                                                                       \
            AST_RWLIST_HEAD_DESTROY(&name);                                 \
    }                                                                       \
    struct __dummy_##name
    #else
    #define AST_RWLIST_HEAD_STATIC(name, type)                              \
    struct name {                                                           \
            struct type *first;                                             \
            struct type *last;                                              \
            ast_rwlock_t lock;                                              \
    } name = AST_RWLIST_HEAD_INIT_VALUE
    #endif
    
    /*!
      \brief Defines a structure to be used to hold a list of specified type, statically initialized.
    
      This is the same as AST_LIST_HEAD_STATIC, except without the lock included.
    */
    #define AST_LIST_HEAD_NOLOCK_STATIC(name, type)				\
    struct name {								\
    	struct type *first;						\
    	struct type *last;						\
    } name = AST_LIST_HEAD_NOLOCK_INIT_VALUE
    
    /*!
      \brief Initializes a list head structure with a specified first entry.
      \param head This is a pointer to the list head structure
      \param entry pointer to the list entry that will become the head of the list
    
      This macro initializes a list head structure by setting the head
      entry to the supplied value and recreating the embedded lock.
    */
    #define AST_LIST_HEAD_SET(head, entry) do {				\
    	(head)->first = (entry);					\
    	(head)->last = (entry);						\
    	ast_mutex_init(&(head)->lock);					\
    } while (0)
    
    /*!
      \brief Initializes an rwlist head structure with a specified first entry.
      \param head This is a pointer to the list head structure
      \param entry pointer to the list entry that will become the head of the list
    
      This macro initializes a list head structure by setting the head
      entry to the supplied value and recreating the embedded lock.
    */
    #define AST_RWLIST_HEAD_SET(head, entry) do {                           \
            (head)->first = (entry);                                        \
            (head)->last = (entry);                                         \
            ast_rwlock_init(&(head)->lock);                                 \
    } while (0)
    
    /*!
      \brief Initializes a list head structure with a specified first entry.
      \param head This is a pointer to the list head structure
      \param entry pointer to the list entry that will become the head of the list
    
      This macro initializes a list head structure by setting the head
      entry to the supplied value.
    */
    #define AST_LIST_HEAD_SET_NOLOCK(head, entry) do {			\
    	(head)->first = (entry);					\
    	(head)->last = (entry);						\
    } while (0)
    
    /*!
      \brief Declare a forward link structure inside a list entry.
      \param type This is the type of each list entry.
    
      This macro declares a structure to be used to link list entries together.
      It must be used inside the definition of the structure named in
      \a type, as follows:
    
      \code
      struct list_entry {
      	...
      	AST_LIST_ENTRY(list_entry) list;
      }
      \endcode
    
      The field name \a list here is arbitrary, and can be anything you wish.
    */
    #define AST_LIST_ENTRY(type)						\
    struct {								\
    	struct type *next;						\
    }
    
    #define AST_RWLIST_ENTRY AST_LIST_ENTRY
     
    /*!
      \brief Returns the first entry contained in a list.
      \param head This is a pointer to the list head structure
     */
    #define	AST_LIST_FIRST(head)	((head)->first)
    
    #define AST_RWLIST_FIRST AST_LIST_FIRST
    
    /*!
      \brief Returns the last entry contained in a list.
      \param head This is a pointer to the list head structure
     */
    #define	AST_LIST_LAST(head)	((head)->last)
    
    #define AST_RWLIST_LAST AST_LIST_LAST
    
    /*!
      \brief Returns the next entry in the list after the given entry.
      \param elm This is a pointer to the current entry.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    */
    #define AST_LIST_NEXT(elm, field)	((elm)->field.next)
    
    #define AST_RWLIST_NEXT AST_LIST_NEXT
    
    /*!
      \brief Checks whether the specified list contains any entries.
      \param head This is a pointer to the list head structure
    
      Returns non-zero if the list has entries, zero if not.
     */
    #define	AST_LIST_EMPTY(head)	(AST_LIST_FIRST(head) == NULL)
    
    #define AST_RWLIST_EMPTY AST_LIST_EMPTY
    
    /*!
      \brief Loops over (traverses) the entries in a list.
      \param head This is a pointer to the list head structure
      \param var This is the name of the variable that will hold a pointer to the
      current list entry on each iteration. It must be declared before calling
      this macro.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    
      This macro is use to loop over (traverse) the entries in a list. It uses a
      \a for loop, and supplies the enclosed code with a pointer to each list
      entry as it loops. It is typically used as follows:
      \code
      static AST_LIST_HEAD(entry_list, list_entry) entries;
      ...
      struct list_entry {
      	...
      	AST_LIST_ENTRY(list_entry) list;
      }
      ...
      struct list_entry *current;
      ...
      AST_LIST_TRAVERSE(&entries, current, list) {
         (do something with current here)
      }
      \endcode
      \warning If you modify the forward-link pointer contained in the \a current entry while
      inside the loop, the behavior will be unpredictable. At a minimum, the following
      macros will modify the forward-link pointer, and should not be used inside
      AST_LIST_TRAVERSE() against the entry pointed to by the \a current pointer without
      careful consideration of their consequences:
      \li AST_LIST_NEXT() (when used as an lvalue)
      \li AST_LIST_INSERT_AFTER()
      \li AST_LIST_INSERT_HEAD()
      \li AST_LIST_INSERT_TAIL()
    */
    #define AST_LIST_TRAVERSE(head,var,field) 				\
    	for((var) = (head)->first; (var); (var) = (var)->field.next)
    
    #define AST_RWLIST_TRAVERSE AST_LIST_TRAVERSE
    
    /*!
      \brief Loops safely over (traverses) the entries in a list.
      \param head This is a pointer to the list head structure
      \param var This is the name of the variable that will hold a pointer to the
      current list entry on each iteration. It must be declared before calling
      this macro.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    
      This macro is used to safely loop over (traverse) the entries in a list. It
      uses a \a for loop, and supplies the enclosed code with a pointer to each list
      entry as it loops. It is typically used as follows:
    
      \code
      static AST_LIST_HEAD(entry_list, list_entry) entries;
      ...
      struct list_entry {
      	...
      	AST_LIST_ENTRY(list_entry) list;
      }
      ...
      struct list_entry *current;
      ...
      AST_LIST_TRAVERSE_SAFE_BEGIN(&entries, current, list) {
         (do something with current here)
      }
      AST_LIST_TRAVERSE_SAFE_END;
      \endcode
    
      It differs from AST_LIST_TRAVERSE() in that the code inside the loop can modify
      (or even free, after calling AST_LIST_REMOVE_CURRENT()) the entry pointed to by
      the \a current pointer without affecting the loop traversal.
    */
    #define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field) {				\
    	typeof((head)->first) __list_next;						\
    	typeof((head)->first) __list_prev = NULL;					\
    	typeof((head)->first) __new_prev = NULL;					\
    	for ((var) = (head)->first, __new_prev = (var),					\
    	      __list_next = (var) ? (var)->field.next : NULL;				\
    	     (var);									\
    	     __list_prev = __new_prev, (var) = __list_next,				\
    	     __new_prev = (var),							\
    	     __list_next = (var) ? (var)->field.next : NULL				\
    	    )
    
    #define AST_RWLIST_TRAVERSE_SAFE_BEGIN AST_LIST_TRAVERSE_SAFE_BEGIN
    
    /*!
      \brief Removes the \a current entry from a list during a traversal.
      \param head This is a pointer to the list head structure
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    
      \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
      block; it is used to unlink the current entry from the list without affecting
      the list traversal (and without having to re-traverse the list to modify the
      previous entry, if any).
     */
    #define AST_LIST_REMOVE_CURRENT(head, field)						\
    	__new_prev->field.next = NULL;							\
    	__new_prev = __list_prev;							\
    	if (__list_prev)								\
    		__list_prev->field.next = __list_next;					\
    	else										\
    		(head)->first = __list_next;						\
    	if (!__list_next)								\
    		(head)->last = __list_prev;
    
    #define AST_RWLIST_REMOVE_CURRENT AST_LIST_REMOVE_CURRENT
    
    /*!
      \brief Inserts a list entry before the current entry during a traversal.
      \param head This is a pointer to the list head structure
      \param elm This is a pointer to the entry to be inserted.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    
      \note This macro can \b only be used inside an AST_LIST_TRAVERSE_SAFE_BEGIN()
      block.
     */
    #define AST_LIST_INSERT_BEFORE_CURRENT(head, elm, field) do {		\
    	if (__list_prev) {						\
    		(elm)->field.next = __list_prev->field.next;		\
    		__list_prev->field.next = elm;				\
    	} else {							\
    		(elm)->field.next = (head)->first;			\
    		(head)->first = (elm);					\
    	}								\
    	__new_prev = (elm);						\
    } while (0)
    
    #define AST_RWLIST_INSERT_BEFORE_CURRENT AST_LIST_INSERT_BEFORE_CURRENT
    
    /*!
      \brief Closes a safe loop traversal block.
     */
    #define AST_LIST_TRAVERSE_SAFE_END  }
    
    #define AST_RWLIST_TRAVERSE_SAFE_END AST_LIST_TRAVERSE_SAFE_END
    
    /*!
      \brief Initializes a list head structure.
      \param head This is a pointer to the list head structure
    
      This macro initializes a list head structure by setting the head
      entry to \a NULL (empty list) and recreating the embedded lock.
    */
    #define AST_LIST_HEAD_INIT(head) {					\
    	(head)->first = NULL;						\
    	(head)->last = NULL;						\
    	ast_mutex_init(&(head)->lock);					\
    }
    
    /*!
      \brief Initializes an rwlist head structure.
      \param head This is a pointer to the list head structure
    
      This macro initializes a list head structure by setting the head
      entry to \a NULL (empty list) and recreating the embedded lock.
    */
    #define AST_RWLIST_HEAD_INIT(head) {                                    \
            (head)->first = NULL;                                           \
            (head)->last = NULL;                                            \
            ast_rwlock_init(&(head)->lock);                                 \
    }
    
    /*!
      \brief Destroys a list head structure.
      \param head This is a pointer to the list head structure
    
      This macro destroys a list head structure by setting the head
      entry to \a NULL (empty list) and destroying the embedded lock.
      It does not free the structure from memory.
    */
    #define AST_LIST_HEAD_DESTROY(head) {					\
    	(head)->first = NULL;						\
    	(head)->last = NULL;						\
    	ast_mutex_destroy(&(head)->lock);				\
    }
    
    /*!
      \brief Destroys an rwlist head structure.
      \param head This is a pointer to the list head structure
    
      This macro destroys a list head structure by setting the head
      entry to \a NULL (empty list) and destroying the embedded lock.
      It does not free the structure from memory.
    */
    #define AST_RWLIST_HEAD_DESTROY(head) {                                 \
            (head)->first = NULL;                                           \
            (head)->last = NULL;                                            \
            ast_rwlock_destroy(&(head)->lock);                              \
    }
    
    /*!
      \brief Initializes a list head structure.
      \param head This is a pointer to the list head structure
    
      This macro initializes a list head structure by setting the head
      entry to \a NULL (empty list). There is no embedded lock handling
      with this macro.
    */
    #define AST_LIST_HEAD_INIT_NOLOCK(head) {				\
    	(head)->first = NULL;						\
    	(head)->last = NULL;						\
    }
    
    /*!
      \brief Inserts a list entry after a given entry.
      \param head This is a pointer to the list head structure
      \param listelm This is a pointer to the entry after which the new entry should
      be inserted.
      \param elm This is a pointer to the entry to be inserted.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
     */
    #define AST_LIST_INSERT_AFTER(head, listelm, elm, field) do {		\
    	(elm)->field.next = (listelm)->field.next;			\
    	(listelm)->field.next = (elm);					\
    	if ((head)->last == (listelm))					\
    		(head)->last = (elm);					\
    } while (0)
    
    #define AST_RWLIST_INSERT_AFTER AST_LIST_INSERT_AFTER
    
    /*!
      \brief Inserts a list entry at the head of a list.
      \param head This is a pointer to the list head structure
      \param elm This is a pointer to the entry to be inserted.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
     */
    #define AST_LIST_INSERT_HEAD(head, elm, field) do {			\
    		(elm)->field.next = (head)->first;			\
    		(head)->first = (elm);					\
    		if (!(head)->last)					\
    			(head)->last = (elm);				\
    } while (0)
    
    #define AST_RWLIST_INSERT_HEAD AST_LIST_INSERT_HEAD
    
    /*!
      \brief Appends a list entry to the tail of a list.
      \param head This is a pointer to the list head structure
      \param elm This is a pointer to the entry to be appended.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    
      Note: The link field in the appended entry is \b not modified, so if it is
      actually the head of a list itself, the entire list will be appended
      temporarily (until the next AST_LIST_INSERT_TAIL is performed).
     */
    #define AST_LIST_INSERT_TAIL(head, elm, field) do {			\
          if (!(head)->first) {						\
    		(head)->first = (elm);					\
    		(head)->last = (elm);					\
          } else {								\
    		(head)->last->field.next = (elm);			\
    		(head)->last = (elm);					\
          }									\
    } while (0)
    
    #define AST_RWLIST_INSERT_TAIL AST_LIST_INSERT_TAIL
    
    /*!
      \brief Appends a whole list to the tail of a list.
      \param head This is a pointer to the list head structure
      \param list This is a pointer to the list to be appended.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
     */
    #define AST_LIST_APPEND_LIST(head, list, field) do {			\
          if (!(head)->first) {						\
    		(head)->first = (list)->first;				\
    		(head)->last = (list)->last;				\
          } else {								\
    		(head)->last->field.next = (list)->first;		\
    		(head)->last = (list)->last;				\
          }									\
    } while (0)
    
    #define AST_RWLIST_APPEND_LIST AST_LIST_APPEND_LIST
    
    /*!
      \brief Removes and returns the head entry from a list.
      \param head This is a pointer to the list head structure
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
    
      Removes the head entry from the list, and returns a pointer to it.
      This macro is safe to call on an empty list.
     */
    #define AST_LIST_REMOVE_HEAD(head, field) ({				\
    		typeof((head)->first) cur = (head)->first;		\
    		if (cur) {						\
    			(head)->first = cur->field.next;		\
    			cur->field.next = NULL;				\
    			if ((head)->last == cur)			\
    				(head)->last = NULL;			\
    		}							\
    		cur;							\
    	})
    
    #define AST_RWLIST_REMOVE_HEAD AST_LIST_REMOVE_HEAD
    
    /*!
      \brief Removes a specific entry from a list.
      \param head This is a pointer to the list head structure
      \param elm This is a pointer to the entry to be removed.
      \param field This is the name of the field (declared using AST_LIST_ENTRY())
      used to link entries of this list together.
      \warning The removed entry is \b not freed nor modified in any way.
     */
    #define AST_LIST_REMOVE(head, elm, field) do {			        \
    	if ((head)->first == (elm)) {					\
    		(head)->first = (elm)->field.next;			\
    		if ((head)->last == (elm))			\
    			(head)->last = NULL;			\
    	} else {								\
    		typeof(elm) curelm = (head)->first;			\
    		while (curelm && (curelm->field.next != (elm)))			\
    			curelm = curelm->field.next;			\
    		if (curelm) { \
    			curelm->field.next = (elm)->field.next;			\
    			if ((head)->last == (elm))				\
    				(head)->last = curelm;				\
    		} \
    	}								\
            (elm)->field.next = NULL;                                       \
    } while (0)
    
    #define AST_RWLIST_REMOVE AST_LIST_REMOVE
    
    /* chanvars.h */
    
    struct ast_var_t {
    	AST_LIST_ENTRY(ast_var_t) entries;
    	char *value;
    	char name[0];
    };
    
    AST_LIST_HEAD_NOLOCK(varshead, ast_var_t);
    
    AST_RWLOCK_DEFINE_STATIC(globalslock);
    static struct varshead globals = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
    
    
    /* IN CONFLICT: struct ast_var_t *ast_var_assign(const char *name, const char *value); */
    
    static struct ast_var_t *ast_var_assign(const char *name, const char *value);
    
    static void ast_var_delete(struct ast_var_t *var);
    
    /*from channel.h */
    #define AST_MAX_EXTENSION  80      /*!< Max length of an extension */
    
    
    /* from pbx.h */
    #define PRIORITY_HINT	-1	/*!< Special Priority for a hint */
    
    enum ast_extension_states {
    	AST_EXTENSION_REMOVED = -2,	/*!< Extension removed */
    	AST_EXTENSION_DEACTIVATED = -1,	/*!< Extension hint removed */
    	AST_EXTENSION_NOT_INUSE = 0,	/*!< No device INUSE or BUSY  */
    	AST_EXTENSION_INUSE = 1 << 0,	/*!< One or more devices INUSE */
    	AST_EXTENSION_BUSY = 1 << 1,	/*!< All devices BUSY */
    	AST_EXTENSION_UNAVAILABLE = 1 << 2, /*!< All devices UNAVAILABLE/UNREGISTERED */
    	AST_EXTENSION_RINGING = 1 << 3,	/*!< All devices RINGING */
    	AST_EXTENSION_ONHOLD = 1 << 4,	/*!< All devices ONHOLD */
    };
    
    struct ast_custom_function {
    	const char *name;		/*!< Name */
    	const char *synopsis;		/*!< Short description for "show functions" */
    	const char *desc;		/*!< Help text that explains it all */
    	const char *syntax;		/*!< Syntax description */
    	int (*read)(struct ast_channel *, const char *, char *, char *, size_t);	/*!< Read function, if read is supported */
    	int (*write)(struct ast_channel *, const char *, char *, const char *);		/*!< Write function, if write is supported */
    	AST_RWLIST_ENTRY(ast_custom_function) acflist;
    };
    
    typedef int (ast_switch_f)(struct ast_channel *chan, const char *context,
    	const char *exten, int priority, const char *callerid, const char *data);
    
    struct ast_switch {
    	AST_LIST_ENTRY(ast_switch) list;
    	const char *name;			/*!< Name of the switch */
    	const char *description;		/*!< Description of the switch */
    	
    	ast_switch_f *exists;
    	ast_switch_f *canmatch;
    	ast_switch_f *exec;
    	ast_switch_f *matchmore;
    };
    
    
    
    static char *config_filename = "extensions.conf";
    static char *global_registrar = "conf2ael";
    
    static char userscontext[AST_MAX_EXTENSION] = "default";
    static int static_config = 0;
    static int write_protect_config = 1;
    static int autofallthrough_config = 0;
    static int clearglobalvars_config = 0;
    /*! Go no deeper than this through includes (not counting loops) */
    #define AST_PBX_MAX_STACK	128
    static void pbx_substitute_variables_helper(struct ast_channel *c,const char *cp1,char *cp2,int count);
    
    
    /* stolen from callerid.c */
    
    /*! \brief Clean up phone string
     * remove '(', ' ', ')', non-trailing '.', and '-' not in square brackets.
     * Basically, remove anything that could be invalid in a pattern.
     */
    static void ast_shrink_phone_number(char *n)
    {
    	int x, y=0;
    	int bracketed = 0;
    
    	for (x=0; n[x]; x++) {
    		switch(n[x]) {
    		case '[':
    			bracketed++;
    			n[y++] = n[x];
    			break;
    		case ']':
    			bracketed--;
    			n[y++] = n[x];
    			break;
    		case '-':
    			if (bracketed)
    				n[y++] = n[x];
    			break;
    		case '.':
    			if (!n[x+1])
    				n[y++] = n[x];
    			break;
    		default:
    			if (!strchr("()", n[x]))
    				n[y++] = n[x];
    		}
    	}
    	n[y] = '\0';
    }
    
    
    /* stolen from chanvars.c */
    
    static const char *ast_var_name(const struct ast_var_t *var)
    {
    	const char *name;
    
    	if (var == NULL || (name = var->name) == NULL)
    		return NULL;
    	/* Return the name without the initial underscores */
    	if (name[0] == '_') {
    		name++;
    		if (name[0] == '_')
    			name++;
    	}
    	return name;
    }
    
    
    /* stolen from asterisk.c */
    
    static struct ast_flags ast_options = { AST_DEFAULT_OPTIONS };
    static int option_verbose = 0;                         /*!< Verbosity level */
    static int option_debug = 0;                           /*!< Debug level */
    
    
    /* experiment 1: see if it's easier just to use existing config code
     *               to read in the extensions.conf file. In this scenario, 
                     I have to rip/copy code from other modules, because they
                     are staticly declared as-is. A solution would be to move
                     the ripped code to another location and make them available
                     to other modules and standalones */
    
    /* Our own version of ast_log, since the expr parser uses it. -- stolen from utils/check_expr.c */
    
    static void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
    {
    	va_list vars;
    	va_start(vars,fmt);
    	
    	printf("LOG: lev:%d file:%s  line:%d func: %s  ",
    		   level, file, line, function);
    	vprintf(fmt, vars);
    	fflush(stdout);
    	va_end(vars);
    }
    
    
    static void __attribute__((format (printf, 1, 2))) ast_verbose(const char *fmt, ...)
    
    {
    	va_list vars;
    	va_start(vars,fmt);
    	
    	printf("VERBOSE: ");
    	vprintf(fmt, vars);
    	fflush(stdout);
    	va_end(vars);
    }
    
    /* stolen from main/utils.c */
    static char *ast_process_quotes_and_slashes(char *start, char find, char replace_with)
    {
     	char *dataPut = start;
    	int inEscape = 0;
    	int inQuotes = 0;
    
    	for (; *start; start++) {
    		if (inEscape) {
    			*dataPut++ = *start;       /* Always goes verbatim */
    			inEscape = 0;
    		} else {
    			if (*start == '\\') {
    				inEscape = 1;      /* Do not copy \ into the data */
    			} else if (*start == '\'') {
    				inQuotes = 1 - inQuotes;   /* Do not copy ' into the data */
    			} else {
    				/* Replace , with |, unless in quotes */
    				*dataPut++ = inQuotes ? *start : ((*start == find) ? replace_with : *start);
    			}
    		}
    	}
    	if (start != dataPut)
    		*dataPut = 0;
    	return dataPut;
    }
    
    static int ast_true(const char *s)
    {
    	if (ast_strlen_zero(s))
    		return 0;
    
    	/* Determine if this is a true value */
    	if (!strcasecmp(s, "yes") ||
    	    !strcasecmp(s, "true") ||
    	    !strcasecmp(s, "y") ||
    	    !strcasecmp(s, "t") ||
    	    !strcasecmp(s, "1") ||
    	    !strcasecmp(s, "on"))
    		return -1;
    
    	return 0;
    }
    
    /* stolen from pbx.c */
    #define VAR_BUF_SIZE 4096
    
    #define	VAR_NORMAL		1
    #define	VAR_SOFTTRAN	2
    #define	VAR_HARDTRAN	3
    
    #define BACKGROUND_SKIP		(1 << 0)
    #define BACKGROUND_NOANSWER	(1 << 1)
    #define BACKGROUND_MATCHEXTEN	(1 << 2)
    #define BACKGROUND_PLAYBACK	(1 << 3)
    
    /*!
       \brief ast_exten: An extension
    	The dialplan is saved as a linked list with each context
    	having it's own linked list of extensions - one item per
    	priority.
    */
    struct ast_exten {
    	char *exten;			/*!< Extension name */
    	int matchcid;			/*!< Match caller id ? */
    	const char *cidmatch;		/*!< Caller id to match for this extension */
    	int priority;			/*!< Priority */
    	const char *label;		/*!< Label */
    	struct ast_context *parent;	/*!< The context this extension belongs to  */
    	const char *app; 		/*!< Application to execute */
    	struct ast_app *cached_app;     /*!< Cached location of application */
    	void *data;			/*!< Data to use (arguments) */
    	void (*datad)(void *);		/*!< Data destructor */
    	struct ast_exten *peer;		/*!< Next higher priority with our extension */
    	const char *registrar;		/*!< Registrar */
    	struct ast_exten *next;		/*!< Extension with a greater ID */
    	char stuff[0];
    };
    /* from pbx.h */
    typedef int (*ast_state_cb_type)(char *context, char* id, enum ast_extension_states state, void *data);
    struct ast_timing {
    	int hastime;				/*!< If time construct exists */
    	unsigned int monthmask;			/*!< Mask for month */
    	unsigned int daymask;			/*!< Mask for date */
    	unsigned int dowmask;			/*!< Mask for day of week (mon-sun) */
    	unsigned int minmask[24];		/*!< Mask for minute */
    };
    /* end of pbx.h */
    /*! \brief ast_include: include= support in extensions.conf */
    struct ast_include {
    	const char *name;
    	const char *rname;			/*!< Context to include */
    	const char *registrar;			/*!< Registrar */
    	int hastime;				/*!< If time construct exists */
    	struct ast_timing timing;               /*!< time construct */
    	struct ast_include *next;		/*!< Link them together */
    	char stuff[0];
    };
    
    /*! \brief ast_sw: Switch statement in extensions.conf */
    struct ast_sw {
    	char *name;
    	const char *registrar;			/*!< Registrar */
    	char *data;				/*!< Data load */
    	int eval;
    	AST_LIST_ENTRY(ast_sw) list;
    	char *tmpdata;
    	char stuff[0];
    };
    
    /*! \brief ast_ignorepat: Ignore patterns in dial plan */
    struct ast_ignorepat {
    	const char *registrar;
    	struct ast_ignorepat *next;
    	const char pattern[0];
    };
    
    /*! \brief ast_context: An extension context */
    struct ast_context {
    	ast_rwlock_t lock; 			/*!< A lock to prevent multiple threads from clobbering the context */
    	struct ast_exten *root;			/*!< The root of the list of extensions */
    	struct ast_context *next;		/*!< Link them together */
    	struct ast_include *includes;		/*!< Include other contexts */
    	struct ast_ignorepat *ignorepats;	/*!< Patterns for which to continue playing dialtone */
    	const char *registrar;			/*!< Registrar */
    	AST_LIST_HEAD_NOLOCK(, ast_sw) alts;	/*!< Alternative switches */
    	ast_mutex_t macrolock;			/*!< A lock to implement "exclusive" macros - held whilst a call is executing in the macro */
    	char name[0];				/*!< Name of the context */
    };
    
    
    /*! \brief ast_app: A registered application */
    struct ast_app {
    	int (*execute)(struct ast_channel *chan, void *data);
    	const char *synopsis;			/*!< Synopsis text for 'show applications' */
    	const char *description;		/*!< Description (help text) for 'show application &lt;name&gt;' */
    	AST_RWLIST_ENTRY(ast_app) list;		/*!< Next app in list */
    	void *module;			/*!< Module this app belongs to */
    	char name[0];				/*!< Name of the application */
    };
    
    
    /*! \brief ast_state_cb: An extension state notify register item */
    struct ast_state_cb {
    	int id;
    	void *data;
    	ast_state_cb_type callback;
    	struct ast_state_cb *next;
    };
    
    /*! \brief Structure for dial plan hints
    
      \note Hints are pointers from an extension in the dialplan to one or
      more devices (tech/name) 
    	- See \ref AstExtState
    */
    struct ast_hint {
    	struct ast_exten *exten;	/*!< Extension */
    	int laststate; 			/*!< Last known state */
    	struct ast_state_cb *callbacks;	/*!< Callback list for this extension */
    	AST_RWLIST_ENTRY(ast_hint) list;/*!< Pointer to next hint in list */
    };
    
    struct store_hint {
    	char *context;
    	char *exten;
    	struct ast_state_cb *callbacks;
    	int laststate;
    	AST_LIST_ENTRY(store_hint) list;
    	char data[1];
    };
    
    AST_LIST_HEAD(store_hints, store_hint);
    
    static const struct cfextension_states {
    	int extension_state;
    	const char * const text;
    } extension_states[] = {
    	{ AST_EXTENSION_NOT_INUSE,                     "Idle" },
    	{ AST_EXTENSION_INUSE,                         "InUse" },
    	{ AST_EXTENSION_BUSY,                          "Busy" },
    	{ AST_EXTENSION_UNAVAILABLE,                   "Unavailable" },
    	{ AST_EXTENSION_RINGING,                       "Ringing" },
    	{ AST_EXTENSION_INUSE | AST_EXTENSION_RINGING, "InUse&Ringing" },
    	{ AST_EXTENSION_ONHOLD,                        "Hold" },
    	{ AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD,  "InUse&Hold" }
    };
    #define STATUS_NO_CONTEXT	1
    #define STATUS_NO_EXTENSION	2
    #define STATUS_NO_PRIORITY	3
    #define STATUS_NO_LABEL		4
    #define STATUS_SUCCESS		5
    
    
    #if defined ( __i386__) && (defined(__FreeBSD__) || defined(linux))
    #if defined(__FreeBSD__)
    #include <machine/cpufunc.h>
    #elif defined(linux)
    static __inline uint64_t
    rdtsc(void)
    { 
    	uint64_t rv;
    
    	__asm __volatile(".byte 0x0f, 0x31" : "=A" (rv));
    	return (rv);
    }
    #endif
    #else	/* supply a dummy function on other platforms */
    static __inline uint64_t
    rdtsc(void)
    {
    	return 0;
    }
    #endif
    
    
    static struct ast_var_t *ast_var_assign(const char *name, const char *value)
    {	
    	struct ast_var_t *var;
    	int name_len = strlen(name) + 1;
    	int value_len = strlen(value) + 1;
    
    	if (!(var = ast_calloc(sizeof(*var) + name_len + value_len, sizeof(char)))) {
    		return NULL;
    	}
    
    	ast_copy_string(var->name, name, name_len);
    	var->value = var->name + name_len;
    	ast_copy_string(var->value, value, value_len);
    	
    	return var;
    }	
    	
    static void ast_var_delete(struct ast_var_t *var)
    {
    	if (var)
    		free(var);
    }
    
    
    /* chopped this one off at the knees! */
    static int ast_func_write(struct ast_channel *chan, const char *function, const char *value)
    {
    
    	/* ast_log(LOG_ERROR, "Function %s not registered\n", function); we are not interested in the details here */
    
    	return -1;
    }
    
    static unsigned int ast_app_separate_args(char *buf, char delim, char **array, int arraylen)
    {
    	int argc;