Newer
Older
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2006, Digium, Inc.
*
* Steve Murphy <murf@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*
*
* A condensation of the pbx_config stuff, to read into exensions.conf, and provide an interface to the data there,
* for operations outside of asterisk. A huge, awful hack.
*
*/
#include "asterisk/compat.h"
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <stdarg.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>
#if !defined(SOLARIS) && !defined(__CYGWIN__)
#include <err.h>
#endif
#include <regex.h>
#include <limits.h>
#include <pthread.h>
#include <netdb.h>
#include <sys/param.h>
#define ASINCLUDE_GLOB 1
#ifdef AST_INCLUDE_GLOB
#if defined(__Darwin__) || defined(__CYGWIN__)
#define GLOB_ABORTED GLOB_ABEND
#endif
# include <glob.h>
#endif
static const char ast_config_AST_CONFIG_DIR[PATH_MAX] = {"/etc/asterisk"};
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#define AST_API_MODULE 1 /* gimme the inline defs! */
struct ast_channel
{
char x; /* basically empty! */
};
#include "asterisk/inline_api.h"
#include "asterisk/endian.h"
#include "asterisk/ast_expr.h"
#include "asterisk/ael_structs.h"
#include "asterisk/pval.h"
/* logger.h */
#define EVENTLOG "event_log"
#define QUEUELOG "queue_log"
#define DEBUG_M(a) { \
a; \
}
#define VERBOSE_PREFIX_1 " "
#define VERBOSE_PREFIX_2 " == "
#define VERBOSE_PREFIX_3 " -- "
#define VERBOSE_PREFIX_4 " > "
/* IN CONFLICT: void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...)
__attribute__ ((format (printf, 5, 6))); */
static void ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf,5,6)));
void ast_backtrace(void);
void ast_queue_log(const char *queuename, const char *callid, const char *agent, const char *event, const char *fmt, ...)
__attribute__ ((format (printf, 5, 6)));
/* IN CONFLICT: void ast_verbose(const char *fmt, ...)
__attribute__ ((format (printf, 1, 2))); */
int ast_register_verbose(void (*verboser)(const char *string));
int ast_unregister_verbose(void (*verboser)(const char *string));
void ast_console_puts(const char *string);
void ast_console_puts_mutable(const char *string);
void ast_console_toggle_mute(int fd);
#define _A_ __FILE__, __LINE__, __PRETTY_FUNCTION__
#ifdef LOG_DEBUG
#undef LOG_DEBUG
#endif
#define __LOG_DEBUG 0
#define LOG_DEBUG __LOG_DEBUG, _A_
#ifdef LOG_EVENT
#undef LOG_EVENT
#endif
#define __LOG_EVENT 1
#define LOG_EVENT __LOG_EVENT, _A_
#ifdef LOG_NOTICE
#undef LOG_NOTICE
#endif
#define __LOG_NOTICE 2
#define LOG_NOTICE __LOG_NOTICE, _A_
#ifdef LOG_WARNING
#undef LOG_WARNING
#endif
#define __LOG_WARNING 3
#define LOG_WARNING __LOG_WARNING, _A_
#ifdef LOG_ERROR
#undef LOG_ERROR
#endif
#define __LOG_ERROR 4
#define LOG_ERROR __LOG_ERROR, _A_
#ifdef LOG_VERBOSE
#undef LOG_VERBOSE
#endif
#define __LOG_VERBOSE 5
#define LOG_VERBOSE __LOG_VERBOSE, _A_
#ifdef LOG_DTMF
#undef LOG_DTMF
#endif
#define __LOG_DTMF 6
#define LOG_DTMF __LOG_DTMF, _A_
/* from utils.h */
static unsigned int __unsigned_int_flags_dummy;
struct ast_flags { /* stolen from utils.h */
unsigned int flags;
};
#define ast_test_flag(p,flag) ({ \
typeof ((p)->flags) __p = (p)->flags; \
typeof (__unsigned_int_flags_dummy) __x = 0; \
(void) (&__p == &__x); \
((p)->flags & (flag)); \
})
#define ast_set2_flag(p,value,flag) do { \
typeof ((p)->flags) __p = (p)->flags; \
typeof (__unsigned_int_flags_dummy) __x = 0; \
(void) (&__p == &__x); \
if (value) \
(p)->flags |= (flag); \
else \
(p)->flags &= ~(flag); \
} while (0)
#ifdef __AST_DEBUG_MALLOC
static void ast_free(void *ptr) attribute_unused;
static void ast_free(void *ptr)
{
free(ptr);
}
#else
#define ast_free free
#endif
#ifndef __AST_DEBUG_MALLOC
#define MALLOC_FAILURE_MSG \
ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);
/*!
* \brief A wrapper for malloc()
*
* ast_malloc() is a wrapper for malloc() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* The argument and return value are the same as malloc()
*/
#define ast_malloc(len) \
_ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
AST_INLINE_API(
void * attribute_malloc _ast_malloc(size_t len, const char *file, int lineno, const char *func),
{
void *p;
if (!(p = malloc(len)))
MALLOC_FAILURE_MSG;
return p;
}
)
/*!
* \brief A wrapper for calloc()
*
* ast_calloc() is a wrapper for calloc() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* The arguments and return value are the same as calloc()
*/
#define ast_calloc(num, len) \
_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
AST_INLINE_API(
void * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func),
{
void *p;
if (!(p = calloc(num, len)))
MALLOC_FAILURE_MSG;
return p;
}
)
/*!
* \brief A wrapper for calloc() for use in cache pools
*
* ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log
* message in the case that the allocation fails. When memory debugging is in use,
* the memory allocated by this function will be marked as 'cache' so it can be
* distinguished from normal memory allocations.
*
* The arguments and return value are the same as calloc()
*/
#define ast_calloc_cache(num, len) \
_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
/*!
* \brief A wrapper for realloc()
*
* ast_realloc() is a wrapper for realloc() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* The arguments and return value are the same as realloc()
*/
#define ast_realloc(p, len) \
_ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
AST_INLINE_API(
void * attribute_malloc _ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func),
{
void *newp;
if (!(newp = realloc(p, len)))
MALLOC_FAILURE_MSG;
return newp;
}
)
/*!
* \brief A wrapper for strdup()
*
* ast_strdup() is a wrapper for strdup() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* ast_strdup(), unlike strdup(), can safely accept a NULL argument. If a NULL
* argument is provided, ast_strdup will return NULL without generating any
* kind of error log message.
*
* The argument and return value are the same as strdup()
*/
#define ast_strdup(str) \
_ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
AST_INLINE_API(
char * attribute_malloc _ast_strdup(const char *str, const char *file, int lineno, const char *func),
{
char *newstr = NULL;
if (str) {
if (!(newstr = strdup(str)))
MALLOC_FAILURE_MSG;
}
return newstr;
}
)
/*!
* \brief A wrapper for strndup()
*
* ast_strndup() is a wrapper for strndup() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* ast_strndup(), unlike strndup(), can safely accept a NULL argument for the
* string to duplicate. If a NULL argument is provided, ast_strdup will return
* NULL without generating any kind of error log message.
*
* The arguments and return value are the same as strndup()
*/
#define ast_strndup(str, len) \
_ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
AST_INLINE_API(
char * attribute_malloc _ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),
{
char *newstr = NULL;
if (str) {
if (!(newstr = strndup(str, len)))
MALLOC_FAILURE_MSG;
}
return newstr;
}
)
/*!
* \brief A wrapper for asprintf()
*
* ast_asprintf() is a wrapper for asprintf() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* The arguments and return value are the same as asprintf()
*/
#define ast_asprintf(ret, fmt, ...) \
_ast_asprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, __VA_ARGS__)
AST_INLINE_API(
int _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...),
{
int res;
va_list ap;
va_start(ap, fmt);
if ((res = vasprintf(ret, fmt, ap)) == -1)
MALLOC_FAILURE_MSG;
va_end(ap);
return res;
}
)
/*!
* \brief A wrapper for vasprintf()
*
* ast_vasprintf() is a wrapper for vasprintf() that will generate an Asterisk log
* message in the case that the allocation fails.
*
* The arguments and return value are the same as vasprintf()
*/
#define ast_vasprintf(ret, fmt, ap) \
_ast_vasprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, (fmt), (ap))
AST_INLINE_API(
int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, va_list ap),
{
int res;
if ((res = vasprintf(ret, fmt, ap)) == -1)
MALLOC_FAILURE_MSG;
return res;
}
)
#else
/* If astmm is in use, let it handle these. Otherwise, it will report that
all allocations are coming from this header file */
#define ast_malloc(a) malloc(a)
#define ast_calloc(a,b) calloc(a,b)
#define ast_realloc(a,b) realloc(a,b)
#define ast_strdup(a) strdup(a)
#define ast_strndup(a,b) strndup(a,b)
#define ast_asprintf(a,b,c) asprintf(a,b,c)
#define ast_vasprintf(a,b,c) vasprintf(a,b,c)
#endif /* AST_DEBUG_MALLOC */
#if !defined(ast_strdupa) && defined(__GNUC__)
/*!
\brief duplicate a string in memory from the stack
\param s The string to duplicate
This macro will duplicate the given string. It returns a pointer to the stack
allocatted memory for the new string.
*/
#define ast_strdupa(s) \
(__extension__ \
({ \
const char *__old = (s); \
size_t __len = strlen(__old) + 1; \
char *__new = __builtin_alloca(__len); \
memcpy (__new, __old, __len); \
__new; \
}))
#endif
/* from config.c */
#define MAX_NESTED_COMMENTS 128
#define COMMENT_START ";--"
#define COMMENT_END "--;"
#define COMMENT_META ';'
#define COMMENT_TAG '-'
static char *extconfig_conf = "extconfig.conf";
/*! Growable string buffer */
static char *comment_buffer; /*!< this will be a comment collector.*/
static int comment_buffer_size; /*!< the amount of storage so far alloc'd for the comment_buffer */
static char *lline_buffer; /*!< A buffer for stuff behind the ; */
static int lline_buffer_size;
#define CB_INCR 250
struct ast_comment {
struct ast_comment *next;
char cmt[0];
};
static void CB_INIT(void)
{
if (!comment_buffer) {
comment_buffer = ast_malloc(CB_INCR);
if (!comment_buffer)
return;
comment_buffer[0] = 0;
comment_buffer_size = CB_INCR;
lline_buffer = ast_malloc(CB_INCR);
if (!lline_buffer)
return;
lline_buffer[0] = 0;
lline_buffer_size = CB_INCR;
} else {
comment_buffer[0] = 0;
lline_buffer[0] = 0;
}
}
static void CB_ADD(char *str)
{
int rem = comment_buffer_size - strlen(comment_buffer) - 1;
int siz = strlen(str);
if (rem < siz+1) {
comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + siz + 1);
if (!comment_buffer)
return;
comment_buffer_size += CB_INCR+siz+1;
}
strcat(comment_buffer,str);
}
static void CB_ADD_LEN(char *str, int len)
{
int cbl = strlen(comment_buffer) + 1;
int rem = comment_buffer_size - cbl;
if (rem < len+1) {
comment_buffer = ast_realloc(comment_buffer, comment_buffer_size + CB_INCR + len + 1);
if (!comment_buffer)
return;
comment_buffer_size += CB_INCR+len+1;
}
strncat(comment_buffer,str,len);
comment_buffer[cbl+len-1] = 0;
}
static void LLB_ADD(char *str)
{
int rem = lline_buffer_size - strlen(lline_buffer) - 1;
int siz = strlen(str);
if (rem < siz+1) {
lline_buffer = ast_realloc(lline_buffer, lline_buffer_size + CB_INCR + siz + 1);
if (!lline_buffer)
return;
lline_buffer_size += CB_INCR + siz + 1;
}
strcat(lline_buffer,str);
}
static void CB_RESET(void )
{
comment_buffer[0] = 0;
lline_buffer[0] = 0;
}
/*! \brief Keep track of how many threads are currently trying to wait*() on
* a child process */
static unsigned int safe_system_level = 0;
static void *safe_system_prev_handler;
/*! \brief NULL handler so we can collect the child exit status */
static void null_sig_handler(int signal)
{
}
void ast_replace_sigchld(void);
void ast_replace_sigchld(void)
{
unsigned int level;
level = safe_system_level++;
/* only replace the handler if it has not already been done */
if (level == 0)
safe_system_prev_handler = signal(SIGCHLD, null_sig_handler);
}
void ast_unreplace_sigchld(void);
void ast_unreplace_sigchld(void)
{
unsigned int level;
level = --safe_system_level;
/* only restore the handler if we are the last one */
if (level == 0)
signal(SIGCHLD, safe_system_prev_handler);
}
int ast_safe_system(const char *s);
int ast_safe_system(const char *s)
{
pid_t pid;
#ifdef HAVE_WORKING_FORK
int x;
#endif
int res;
struct rusage rusage;
int status;
#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
ast_replace_sigchld();
#ifdef HAVE_WORKING_FORK
pid = fork();
#else
pid = vfork();
#endif
if (pid == 0) {
#ifdef HAVE_WORKING_FORK
/* Close file descriptors and launch system command */
for (x = STDERR_FILENO + 1; x < 4096; x++)
close(x);
#endif
execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
_exit(1);
} else if (pid > 0) {
for(;;) {
res = wait4(pid, &status, 0, &rusage);
if (res > -1) {
res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
break;
} else if (errno != EINTR)
break;
}
} else {
ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
res = -1;
}
ast_unreplace_sigchld();
#else
res = -1;
#endif
return res;
}
static struct ast_comment *ALLOC_COMMENT(const char *buffer)
{
struct ast_comment *x = ast_calloc(1,sizeof(struct ast_comment)+strlen(buffer)+1);
strcpy(x->cmt, buffer);
return x;
}
static struct ast_config_map {
struct ast_config_map *next;
char *name;
char *driver;
char *database;
char *table;
char stuff[0];
} *config_maps = NULL;
static struct ast_config_engine *config_engine_list;
#define MAX_INCLUDE_LEVEL 10
struct ast_category {
char name[80];
int ignored; /*!< do not let user of the config see this category */
int include_level;
Steve Murphy
committed
char *file; /*!< the file name from whence this declaration was read */
int lineno;
struct ast_comment *precomments;
struct ast_comment *sameline;
struct ast_variable *root;
struct ast_variable *last;
struct ast_category *next;
};
struct ast_config {
struct ast_category *root;
struct ast_category *last;
struct ast_category *current;
struct ast_category *last_browse; /*!< used to cache the last category supplied via category_browse */
int include_level;
int max_include_level;
Steve Murphy
committed
struct ast_config_include *includes; /*!< a list of inclusions, which should describe the entire tree */
};
Steve Murphy
committed
struct ast_config_include {
char *include_location_file; /*!< file name in which the include occurs */
int include_location_lineno; /*!< lineno where include occurred */
int exec; /*!< set to non-zero if itsa #exec statement */
char *exec_file; /*!< if it's an exec, you'll have both the /var/tmp to read, and the original script */
char *included_file; /*!< file name included */
int inclusion_count; /*!< if the file is included more than once, a running count thereof -- but, worry not,
we explode the instances and will include those-- so all entries will be unique */
int output; /*!< a flag to indicate if the inclusion has been output */
struct ast_config_include *next; /*!< ptr to next inclusion in the list */
};
typedef struct ast_config *config_load_func(const char *database, const char *table, const char *configfile, struct ast_config *config, int withcomments, const char *suggested_include_file);
typedef struct ast_variable *realtime_var_get(const char *database, const char *table, va_list ap);
typedef struct ast_config *realtime_multi_get(const char *database, const char *table, va_list ap);
typedef int realtime_update(const char *database, const char *table, const char *keyfield, const char *entity, va_list ap);
/*! \brief Configuration engine structure, used to define realtime drivers */
struct ast_config_engine {
char *name;
config_load_func *load_func;
realtime_var_get *realtime_func;
realtime_multi_get *realtime_multi_func;
realtime_update *update_func;
struct ast_config_engine *next;
};
static struct ast_config_engine *config_engine_list;
Steve Murphy
committed
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* taken from strings.h */
static force_inline int ast_strlen_zero(const char *s)
{
return (!s || (*s == '\0'));
}
#define S_OR(a, b) (!ast_strlen_zero(a) ? (a) : (b))
AST_INLINE_API(
void ast_copy_string(char *dst, const char *src, size_t size),
{
while (*src && size) {
*dst++ = *src++;
size--;
}
if (__builtin_expect(!size, 0))
dst--;
*dst = '\0';
}
)
AST_INLINE_API(
char *ast_skip_blanks(const char *str),
{
while (*str && *str < 33)
str++;
return (char *)str;
}
)
/*!
\brief Trims trailing whitespace characters from a string.
\param ast_trim_blanks function being used
\param str the input string
\return a pointer to the modified string
*/
AST_INLINE_API(
char *ast_trim_blanks(char *str),
{
char *work = str;
if (work) {
work += strlen(work) - 1;
/* It's tempting to only want to erase after we exit this loop,
but since ast_trim_blanks *could* receive a constant string
(which we presumably wouldn't have to touch), we shouldn't
actually set anything unless we must, and it's easier just
to set each position to \0 than to keep track of a variable
for it */
while ((work >= str) && *work < 33)
*(work--) = '\0';
}
return str;
}
)
/*!
\brief Strip leading/trailing whitespace from a string.
\param s The string to be stripped (will be modified).
\return The stripped string.
This functions strips all leading and trailing whitespace
characters from the input string, and returns a pointer to
the resulting string. The string is modified in place.
*/
AST_INLINE_API(
char *ast_strip(char *s),
{
s = ast_skip_blanks(s);
if (s)
ast_trim_blanks(s);
return s;
}
)
/* from config.h */
struct ast_variable {
char *name;
char *value;
Steve Murphy
committed
char *file;
int lineno;
int object; /*!< 0 for variable, 1 for object */
int blanklines; /*!< Number of blanklines following entry */
struct ast_comment *precomments;
struct ast_comment *sameline;
struct ast_variable *next;
char stuff[0];
};
static const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable);
Steve Murphy
committed
static struct ast_config *config_text_file_load(const char *database, const char *table, const char *filename, struct ast_config *cfg, int withcomments, const char *suggested_include_file);
struct ast_config *localized_config_load_with_comments(const char *filename);
static char *ast_category_browse(struct ast_config *config, const char *prev);
static struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category);
static void ast_variables_destroy(struct ast_variable *v);
static void ast_config_destroy(struct ast_config *cfg);
Steve Murphy
committed
static struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size);
static struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file);
void localized_ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file);
Steve Murphy
committed
static struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename);
Steve Murphy
committed
static struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename)
{
struct ast_variable *variable;
int name_len = strlen(name) + 1;
Steve Murphy
committed
if ((variable = ast_calloc(1, name_len + strlen(value) + 1 + strlen(filename) + 1 + sizeof(*variable)))) {
variable->name = variable->stuff;
variable->value = variable->stuff + name_len;
Steve Murphy
committed
variable->file = variable->value + strlen(value) + 1;
strcpy(variable->name,name);
strcpy(variable->value,value);
Steve Murphy
committed
strcpy(variable->file,filename);
}
return variable;
}
Steve Murphy
committed
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
static struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size)
{
/* a file should be included ONCE. Otherwise, if one of the instances is changed,
then all be changed. -- how do we know to include it? -- Handling modified
instances is possible, I'd have
to create a new master for each instance. */
struct ast_config_include *inc;
inc = ast_include_find(conf, included_file);
if (inc)
{
inc->inclusion_count++;
snprintf(real_included_file_name, real_included_file_name_size, "%s~~%d", included_file, inc->inclusion_count);
ast_log(LOG_WARNING,"'%s', line %d: Same File included more than once! This data will be saved in %s if saved back to disk.\n", from_file, from_lineno, real_included_file_name);
} else
*real_included_file_name = 0;
inc = ast_calloc(1,sizeof(struct ast_config_include));
inc->include_location_file = ast_strdup(from_file);
inc->include_location_lineno = from_lineno;
if (!ast_strlen_zero(real_included_file_name))
inc->included_file = ast_strdup(real_included_file_name);
else
inc->included_file = ast_strdup(included_file);
inc->exec = is_exec;
if (is_exec)
inc->exec_file = ast_strdup(exec_file);
/* attach this new struct to the conf struct */
inc->next = conf->includes;
conf->includes = inc;
return inc;
}
void localized_ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file)
{
struct ast_config_include *incl;
struct ast_category *cat;
struct ast_variable *v;
int from_len = strlen(from_file);
int to_len = strlen(to_file);
if (strcmp(from_file, to_file) == 0) /* no use wasting time if the name is the same */
return;
/* the manager code allows you to read in one config file, then
write it back out under a different name. But, the new arrangement
ties output lines to the file name. So, before you try to write
the config file to disk, better riffle thru the data and make sure
the file names are changed.
*/
/* file names are on categories, includes (of course), and on variables. So,
traverse all this and swap names */
for (incl = conf->includes; incl; incl=incl->next) {
if (strcmp(incl->include_location_file,from_file) == 0) {
if (from_len >= to_len)
strcpy(incl->include_location_file, to_file);
else {
free(incl->include_location_file);
incl->include_location_file = strdup(to_file);
}
}
}
for (cat = conf->root; cat; cat = cat->next) {
if (strcmp(cat->file,from_file) == 0) {
if (from_len >= to_len)
strcpy(cat->file, to_file);
else {
free(cat->file);
cat->file = strdup(to_file);
}
}
for (v = cat->root; v; v = v->next) {
if (strcmp(v->file,from_file) == 0) {
if (from_len >= to_len)
strcpy(v->file, to_file);
else {
free(v->file);
v->file = strdup(to_file);
}
}
}
}
}
static struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file)
{
struct ast_config_include *x;
for (x=conf->includes;x;x=x->next)
{
if (strcmp(x->included_file,included_file) == 0)
return x;
}
return 0;
}
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
static void ast_variable_append(struct ast_category *category, struct ast_variable *variable);
static void ast_variable_append(struct ast_category *category, struct ast_variable *variable)
{
if (!variable)
return;
if (category->last)
category->last->next = variable;
else
category->root = variable;
category->last = variable;
while (category->last->next)
category->last = category->last->next;
}
static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored);
static struct ast_category *category_get(const struct ast_config *config, const char *category_name, int ignored)
{
struct ast_category *cat;
/* try exact match first, then case-insensitive match */
for (cat = config->root; cat; cat = cat->next) {
if (cat->name == category_name && (ignored || !cat->ignored))
return cat;
}
for (cat = config->root; cat; cat = cat->next) {
if (!strcasecmp(cat->name, category_name) && (ignored || !cat->ignored))
return cat;
}
return NULL;
}
static struct ast_category *ast_category_get(const struct ast_config *config, const char *category_name)
{
return category_get(config, category_name, 0);
}
static struct ast_variable *ast_variable_browse(const struct ast_config *config, const char *category)
{
struct ast_category *cat = NULL;
if (category && config->last_browse && (config->last_browse->name == category))
cat = config->last_browse;
else
cat = ast_category_get(config, category);
return (cat) ? cat->root : NULL;
}
static const char *ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
{
struct ast_variable *v;
if (category) {
for (v = ast_variable_browse(config, category); v; v = v->next) {
if (!strcasecmp(variable, v->name))
return v->value;
}
} else {
struct ast_category *cat;
for (cat = config->root; cat; cat = cat->next)
for (v = cat->root; v; v = v->next)
if (!strcasecmp(variable, v->name))
return v->value;
}
return NULL;
}
static struct ast_variable *variable_clone(const struct ast_variable *old)
{
Steve Murphy
committed
struct ast_variable *new = ast_variable_new(old->name, old->value, old->file);
if (new) {
new->lineno = old->lineno;
new->object = old->object;
new->blanklines = old->blanklines;
/* TODO: clone comments? */
}
return new;
}
static void ast_variables_destroy(struct ast_variable *v)
{
struct ast_variable *vn;
while (v) {
vn = v;
v = v->next;
free(vn);
}
}
Steve Murphy
committed
static void ast_includes_destroy(struct ast_config_include *incls)
{
struct ast_config_include *incl,*inclnext;
for (incl=incls; incl; incl = inclnext) {
inclnext = incl->next;
if (incl->include_location_file)
free(incl->include_location_file);
if (incl->exec_file)
free(incl->exec_file);
if (incl->included_file)
free(incl->included_file);
free(incl);
}