Merged revisions 167545 via svnmerge from
[asterisk/asterisk.git] / main / file.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Generic File Format Support.
22  *
23  * \author Mark Spencer <markster@digium.com> 
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <math.h>
33
34 #include "asterisk/_private.h"  /* declare ast_file_init() */
35 #include "asterisk/paths.h"     /* use ast_config_AST_DATA_DIR */
36 #include "asterisk/mod_format.h"
37 #include "asterisk/cli.h"
38 #include "asterisk/channel.h"
39 #include "asterisk/sched.h"
40 #include "asterisk/translate.h"
41 #include "asterisk/utils.h"
42 #include "asterisk/lock.h"
43 #include "asterisk/app.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/linkedlists.h"
46 #include "asterisk/module.h"
47 #include "asterisk/astobj2.h"
48
49 /*
50  * The following variable controls the layout of localized sound files.
51  * If 0, use the historical layout with prefix just before the filename
52  * (i.e. digits/en/1.gsm , digits/it/1.gsm or default to digits/1.gsm),
53  * if 1 put the prefix at the beginning of the filename
54  * (i.e. en/digits/1.gsm, it/digits/1.gsm or default to digits/1.gsm).
55  * The latter permits a language to be entirely in one directory.
56  */
57 int ast_language_is_prefix = 1;
58
59 static AST_RWLIST_HEAD_STATIC(formats, ast_format);
60
61 int __ast_format_register(const struct ast_format *f, struct ast_module *mod)
62 {
63         struct ast_format *tmp;
64
65         AST_RWLIST_WRLOCK(&formats);
66         AST_RWLIST_TRAVERSE(&formats, tmp, list) {
67                 if (!strcasecmp(f->name, tmp->name)) {
68                         AST_RWLIST_UNLOCK(&formats);
69                         ast_log(LOG_WARNING, "Tried to register '%s' format, already registered\n", f->name);
70                         return -1;
71                 }
72         }
73         if (!(tmp = ast_calloc(1, sizeof(*tmp)))) {
74                 AST_RWLIST_UNLOCK(&formats);
75                 return -1;
76         }
77         *tmp = *f;
78         tmp->module = mod;
79         if (tmp->buf_size) {
80                 /*
81                  * Align buf_size properly, rounding up to the machine-specific
82                  * alignment for pointers.
83                  */
84                 struct _test_align { void *a, *b; } p;
85                 int align = (char *)&p.b - (char *)&p.a;
86                 tmp->buf_size = ((f->buf_size + align - 1) / align) * align;
87         }
88         
89         memset(&tmp->list, 0, sizeof(tmp->list));
90
91         AST_RWLIST_INSERT_HEAD(&formats, tmp, list);
92         AST_RWLIST_UNLOCK(&formats);
93         ast_verb(2, "Registered file format %s, extension(s) %s\n", f->name, f->exts);
94
95         return 0;
96 }
97
98 int ast_format_unregister(const char *name)
99 {
100         struct ast_format *tmp;
101         int res = -1;
102
103         AST_RWLIST_WRLOCK(&formats);
104         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&formats, tmp, list) {
105                 if (!strcasecmp(name, tmp->name)) {
106                         AST_RWLIST_REMOVE_CURRENT(list);
107                         ast_free(tmp);
108                         res = 0;
109                 }
110         }
111         AST_RWLIST_TRAVERSE_SAFE_END;
112         AST_RWLIST_UNLOCK(&formats);
113
114         if (!res)
115                 ast_verb(2, "Unregistered format %s\n", name);
116         else
117                 ast_log(LOG_WARNING, "Tried to unregister format %s, already unregistered\n", name);
118
119         return res;
120 }
121
122 int ast_stopstream(struct ast_channel *tmp)
123 {
124         ast_channel_lock(tmp);
125
126         /* Stop a running stream if there is one */
127         if (tmp->stream) {
128                 ast_closestream(tmp->stream);
129                 tmp->stream = NULL;
130                 if (tmp->oldwriteformat && ast_set_write_format(tmp, tmp->oldwriteformat))
131                         ast_log(LOG_WARNING, "Unable to restore format back to %d\n", tmp->oldwriteformat);
132         }
133         /* Stop the video stream too */
134         if (tmp->vstream != NULL) {
135                 ast_closestream(tmp->vstream);
136                 tmp->vstream = NULL;
137         }
138
139         ast_channel_unlock(tmp);
140
141         return 0;
142 }
143
144 int ast_writestream(struct ast_filestream *fs, struct ast_frame *f)
145 {
146         int res = -1;
147         int alt = 0;
148         if (f->frametype == AST_FRAME_VIDEO) {
149                 if (fs->fmt->format & AST_FORMAT_AUDIO_MASK) {
150                         /* This is the audio portion.  Call the video one... */
151                         if (!fs->vfs && fs->filename) {
152                                 const char *type = ast_getformatname(f->subclass & ~0x1);
153                                 fs->vfs = ast_writefile(fs->filename, type, NULL, fs->flags, 0, fs->mode);
154                                 ast_debug(1, "Opened video output file\n");
155                         }
156                         if (fs->vfs)
157                                 return ast_writestream(fs->vfs, f);
158                         /* else ignore */
159                         return 0;                               
160                 } else {
161                         /* Might / might not have mark set */
162                         alt = 1;
163                 }
164         } else if (f->frametype != AST_FRAME_VOICE) {
165                 ast_log(LOG_WARNING, "Tried to write non-voice frame\n");
166                 return -1;
167         }
168         if (((fs->fmt->format | alt) & f->subclass) == f->subclass) {
169                 res =  fs->fmt->write(fs, f);
170                 if (res < 0) 
171                         ast_log(LOG_WARNING, "Natural write failed\n");
172                 else if (res > 0)
173                         ast_log(LOG_WARNING, "Huh??\n");
174         } else {
175                 /* XXX If they try to send us a type of frame that isn't the normal frame, and isn't
176                        the one we've setup a translator for, we do the "wrong thing" XXX */
177                 if (fs->trans && f->subclass != fs->lastwriteformat) {
178                         ast_translator_free_path(fs->trans);
179                         fs->trans = NULL;
180                 }
181                 if (!fs->trans) 
182                         fs->trans = ast_translator_build_path(fs->fmt->format, f->subclass);
183                 if (!fs->trans)
184                         ast_log(LOG_WARNING, "Unable to translate to format %s, source format %s\n",
185                                 fs->fmt->name, ast_getformatname(f->subclass));
186                 else {
187                         struct ast_frame *trf;
188                         fs->lastwriteformat = f->subclass;
189                         /* Get the translated frame but don't consume the original in case they're using it on another stream */
190                         trf = ast_translate(fs->trans, f, 0);
191                         if (trf) {
192                                 res = fs->fmt->write(fs, trf);
193                                 ast_frfree(trf);
194                                 if (res) 
195                                         ast_log(LOG_WARNING, "Translated frame write failed\n");
196                         } else
197                                 res = 0;
198                 }
199         }
200         return res;
201 }
202
203 static int copy(const char *infile, const char *outfile)
204 {
205         int ifd, ofd, len;
206         char buf[4096]; /* XXX make it lerger. */
207
208         if ((ifd = open(infile, O_RDONLY)) < 0) {
209                 ast_log(LOG_WARNING, "Unable to open %s in read-only mode\n", infile);
210                 return -1;
211         }
212         if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, AST_FILE_MODE)) < 0) {
213                 ast_log(LOG_WARNING, "Unable to open %s in write-only mode\n", outfile);
214                 close(ifd);
215                 return -1;
216         }
217         while ( (len = read(ifd, buf, sizeof(buf)) ) ) {
218                 int res;
219                 if (len < 0) {
220                         ast_log(LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
221                         break;
222                 }
223                 /* XXX handle partial writes */
224                 res = write(ofd, buf, len);
225                 if (res != len) {
226                         ast_log(LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
227                         len = -1; /* error marker */
228                         break;
229                 }
230         }
231         close(ifd);
232         close(ofd);
233         if (len < 0) {
234                 unlink(outfile);
235                 return -1; /* error */
236         }
237         return 0;       /* success */
238 }
239
240 /*!
241  * \brief construct a filename. Absolute pathnames are preserved,
242  * relative names are prefixed by the sounds/ directory.
243  * The wav49 suffix is replaced by 'WAV'.
244  * Returns a malloc'ed string to be freed by the caller.
245  */
246 static char *build_filename(const char *filename, const char *ext)
247 {
248         char *fn = NULL;
249
250         if (!strcmp(ext, "wav49"))
251                 ext = "WAV";
252
253         if (filename[0] == '/') {
254                 if (asprintf(&fn, "%s.%s", filename, ext) < 0) {
255                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
256                         fn = NULL;
257                 }
258         } else {
259                 if (asprintf(&fn, "%s/sounds/%s.%s",
260                              ast_config_AST_DATA_DIR, filename, ext) < 0) {
261                         ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
262                         fn = NULL;
263                 }
264         }
265         return fn;
266 }
267
268 /* compare type against the list 'exts' */
269 /* XXX need a better algorithm */
270 static int exts_compare(const char *exts, const char *type)
271 {
272         char tmp[256];
273         char *stringp = tmp, *ext;
274
275         ast_copy_string(tmp, exts, sizeof(tmp));
276         while ((ext = strsep(&stringp, "|"))) {
277                 if (!strcmp(ext, type))
278                         return 1;
279         }
280
281         return 0;
282 }
283
284 static void filestream_destructor(void *arg)
285 {
286         char *cmd = NULL;
287         size_t size = 0;
288         struct ast_filestream *f = arg;
289
290         /* Stop a running stream if there is one */
291         if (f->owner) {
292                 if (f->fmt->format < AST_FORMAT_AUDIO_MASK) {
293                         f->owner->stream = NULL;
294                         AST_SCHED_DEL(f->owner->sched, f->owner->streamid);
295 #ifdef HAVE_DAHDI
296                         ast_settimeout(f->owner, 0, NULL, NULL);
297 #endif                  
298                 } else {
299                         f->owner->vstream = NULL;
300                         AST_SCHED_DEL(f->owner->sched, f->owner->vstreamid);
301                 }
302         }
303         /* destroy the translator on exit */
304         if (f->trans)
305                 ast_translator_free_path(f->trans);
306
307         if (f->realfilename && f->filename) {
308                         size = strlen(f->filename) + strlen(f->realfilename) + 15;
309                         cmd = alloca(size);
310                         memset(cmd,0,size);
311                         snprintf(cmd,size,"/bin/mv -f %s %s",f->filename,f->realfilename);
312                         ast_safe_system(cmd);
313         }
314
315         if (f->filename)
316                 free(f->filename);
317         if (f->realfilename)
318                 free(f->realfilename);
319         if (f->fmt->close)
320                 f->fmt->close(f);
321         if (f->f)
322                 fclose(f->f);
323         if (f->vfs)
324                 ast_closestream(f->vfs);
325         if (f->orig_chan_name)
326                 free((void *) f->orig_chan_name);
327         ast_module_unref(f->fmt->module);
328 }
329
330 static struct ast_filestream *get_filestream(struct ast_format *fmt, FILE *bfile)
331 {
332         struct ast_filestream *s;
333
334         int l = sizeof(*s) + fmt->buf_size + fmt->desc_size;    /* total allocation size */
335         if ( (s = ao2_alloc(l, filestream_destructor)) == NULL)
336                 return NULL;
337         s->fmt = fmt;
338         s->f = bfile;
339
340         if (fmt->desc_size)
341                 s->_private = ((char *)(s + 1)) + fmt->buf_size;
342         if (fmt->buf_size)
343                 s->buf = (char *)(s + 1);
344         s->fr.src = fmt->name;
345         return s;
346 }
347
348 /*
349  * Default implementations of open and rewrite.
350  * Only use them if you don't have expensive stuff to do.
351  */
352 enum wrap_fn { WRAP_OPEN, WRAP_REWRITE };
353
354 static int fn_wrapper(struct ast_filestream *s, const char *comment, enum wrap_fn mode)
355 {
356         struct ast_format *f = s->fmt;
357         int ret = -1;
358
359         if (mode == WRAP_OPEN && f->open && f->open(s))
360                 ast_log(LOG_WARNING, "Unable to open format %s\n", f->name);
361         else if (mode == WRAP_REWRITE && f->rewrite && f->rewrite(s, comment))
362                 ast_log(LOG_WARNING, "Unable to rewrite format %s\n", f->name);
363         else {
364                 /* preliminary checks succeed. update usecount */
365                 ast_module_ref(f->module);
366                 ret = 0;
367         }
368         return ret;
369 }
370
371 static int rewrite_wrapper(struct ast_filestream *s, const char *comment)
372 {
373         return fn_wrapper(s, comment, WRAP_REWRITE);
374 }
375
376 static int open_wrapper(struct ast_filestream *s)
377 {
378         return fn_wrapper(s, NULL, WRAP_OPEN);
379 }
380
381 enum file_action {
382         ACTION_EXISTS = 1, /* return matching format if file exists, 0 otherwise */
383         ACTION_DELETE,  /* delete file, return 0 on success, -1 on error */
384         ACTION_RENAME,  /* rename file. return 0 on success, -1 on error */
385         ACTION_OPEN,
386         ACTION_COPY     /* copy file. return 0 on success, -1 on error */
387 };
388
389 /*!
390  * \brief perform various actions on a file. Second argument
391  * arg2 depends on the command:
392  *      unused for EXISTS and DELETE
393  *      destination file name (const char *) for COPY and RENAME
394  *      struct ast_channel * for OPEN
395  * if fmt is NULL, OPEN will return the first matching entry,
396  * whereas other functions will run on all matching entries.
397  */
398 static int ast_filehelper(const char *filename, const void *arg2, const char *fmt, const enum file_action action)
399 {
400         struct ast_format *f;
401         int res = (action == ACTION_EXISTS) ? 0 : -1;
402
403         AST_RWLIST_RDLOCK(&formats);
404         /* Check for a specific format */
405         AST_RWLIST_TRAVERSE(&formats, f, list) {
406                 char *stringp, *ext = NULL;
407
408                 if (fmt && !exts_compare(f->exts, fmt))
409                         continue;
410
411                 /* Look for a file matching the supported extensions.
412                  * The file must exist, and for OPEN, must match
413                  * one of the formats supported by the channel.
414                  */
415                 stringp = ast_strdupa(f->exts); /* this is in the stack so does not need to be freed */
416                 while ( (ext = strsep(&stringp, "|")) ) {
417                         struct stat st;
418                         char *fn = build_filename(filename, ext);
419
420                         if (fn == NULL)
421                                 continue;
422
423                         if ( stat(fn, &st) ) { /* file not existent */
424                                 ast_free(fn);
425                                 continue;
426                         }
427                         /* for 'OPEN' we need to be sure that the format matches
428                          * what the channel can process
429                          */
430                         if (action == ACTION_OPEN) {
431                                 struct ast_channel *chan = (struct ast_channel *)arg2;
432                                 FILE *bfile;
433                                 struct ast_filestream *s;
434
435                                 if ( !(chan->writeformat & f->format) &&
436                                      !((f->format & AST_FORMAT_AUDIO_MASK && fmt) ||
437                                           (f->format & AST_FORMAT_VIDEO_MASK && fmt))) {
438                                         ast_free(fn);
439                                         continue;       /* not a supported format */
440                                 }
441                                 if ( (bfile = fopen(fn, "r")) == NULL) {
442                                         ast_free(fn);
443                                         continue;       /* cannot open file */
444                                 }
445                                 s = get_filestream(f, bfile);
446                                 if (!s) {
447                                         fclose(bfile);
448                                         ast_free(fn);   /* cannot allocate descriptor */
449                                         continue;
450                                 }
451                                 if (open_wrapper(s)) {
452                                         fclose(bfile);
453                                         ast_free(fn);
454                                         ast_closestream(s);
455                                         continue;       /* cannot run open on file */
456                                 }
457                                 if (st.st_size == 0) {
458                                         ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
459                                 }
460                                 /* ok this is good for OPEN */
461                                 res = 1;        /* found */
462                                 s->lasttimeout = -1;
463                                 s->fmt = f;
464                                 s->trans = NULL;
465                                 s->filename = NULL;
466                                 if (s->fmt->format & AST_FORMAT_AUDIO_MASK) {
467                                         if (chan->stream)
468                                                 ast_closestream(chan->stream);
469                                         chan->stream = s;
470                                 } else {
471                                         if (chan->vstream)
472                                                 ast_closestream(chan->vstream);
473                                         chan->vstream = s;
474                                 }
475                                 ast_free(fn);
476                                 break;
477                         }
478                         switch (action) {
479                         case ACTION_OPEN:
480                                 break;  /* will never get here */
481
482                         case ACTION_EXISTS:     /* return the matching format */
483                                 res |= f->format;
484                                 break;
485
486                         case ACTION_DELETE:
487                                 if ( (res = unlink(fn)) )
488                                         ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
489                                 break;
490
491                         case ACTION_RENAME:
492                         case ACTION_COPY: {
493                                 char *nfn = build_filename((const char *)arg2, ext);
494                                 if (!nfn)
495                                         ast_log(LOG_WARNING, "Out of memory\n");
496                                 else {
497                                         res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
498                                         if (res)
499                                                 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
500                                                         action == ACTION_COPY ? "copy" : "rename",
501                                                          fn, nfn, strerror(errno));
502                                         ast_free(nfn);
503                                 }
504                             }
505                                 break;
506
507                         default:
508                                 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
509                         }
510                         ast_free(fn);
511                 }
512         }
513         AST_RWLIST_UNLOCK(&formats);
514         return res;
515 }
516
517 static int is_absolute_path(const char *filename)
518 {
519         return filename[0] == '/';
520 }
521
522 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
523                            char *buf, int buflen)
524 {
525         if (buf == NULL) {
526                 return -1;
527         }
528
529         if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
530                 if (lang) {
531                         snprintf(buf, buflen, "%s/%s", lang, filename);
532                 } else {
533                         snprintf(buf, buflen, "%s", filename);
534                 }
535         } else { /* old layout */
536                 strcpy(buf, filename);  /* first copy the full string */
537                 if (lang) {
538                         /* insert the language and suffix if needed */
539                         const char *c = strrchr(filename, '/');
540                         int offset = c ? c - filename + 1 : 0;  /* points right after the last '/' */
541                         snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
542                 }
543         }
544
545         return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
546 }
547
548 /*!
549  * \brief helper routine to locate a file with a given format
550  * and language preference.
551  * Try preflang, preflang with stripped '_' suffix, or NULL.
552  * In the standard asterisk, language goes just before the last component.
553  * In an alternative configuration, the language should be a prefix
554  * to the actual filename.
555  *
556  * The last parameter(s) point to a buffer of sufficient size,
557  * which on success is filled with the matching filename.
558  */
559 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
560                            char *buf, int buflen)
561 {
562         int res = -1;
563         char *lang = NULL;
564
565         if (buf == NULL) {
566                 return -1;
567         }
568
569         /* We try languages in the following order:
570          *    preflang (may include dialect)
571          *    lang (preflang without dialect - if any)
572          *    <none>
573          *    default (unless the same as preflang or lang without dialect)
574          */
575
576         /* Try preferred language */
577         if (!ast_strlen_zero(preflang)) {
578                 /* try the preflang exactly as it was requested */
579                 if ((res = fileexists_test(filename, fmt, preflang, buf, buflen)) > 0) {
580                         return res;
581                 } else {
582                         /* try without a dialect */
583                         char *postfix = NULL;
584                         postfix = lang = ast_strdupa(preflang);
585
586                         strsep(&postfix, "_");
587                         if (postfix) {
588                                 if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
589                                         return res;
590                                 }
591                         }
592                 }
593         }
594
595         /* Try without any language */
596         if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
597                 return res;
598         }
599
600         /* Finally try the default language unless it was already tried before */
601         if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
602                 if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
603                         return res;
604                 }
605         }
606
607         return 0;
608 }
609
610 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
611 {
612         return ast_openstream_full(chan, filename, preflang, 0);
613 }
614
615 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
616 {
617         /* 
618          * Use fileexists_core() to find a file in a compatible
619          * language and format, set up a suitable translator,
620          * and open the stream.
621          */
622         int fmts, res, buflen;
623         char *buf;
624
625         if (!asis) {
626                 /* do this first, otherwise we detect the wrong writeformat */
627                 ast_stopstream(chan);
628                 if (chan->generator)
629                         ast_deactivate_generator(chan);
630         }
631         if (preflang == NULL)
632                 preflang = "";
633         buflen = strlen(preflang) + strlen(filename) + 4;
634         buf = alloca(buflen);
635         if (buf == NULL)
636                 return NULL;
637         fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
638         if (fmts > 0)
639                 fmts &= AST_FORMAT_AUDIO_MASK;
640         if (fmts < 1) {
641                 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
642                 return NULL;
643         }
644         chan->oldwriteformat = chan->writeformat;
645         /* Set the channel to a format we can work with */
646         res = ast_set_write_format(chan, fmts);
647         res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
648         if (res >= 0)
649                 return chan->stream;
650         return NULL;
651 }
652
653 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
654 {
655         /* As above, but for video. But here we don't have translators
656          * so we must enforce a format.
657          */
658         unsigned int format;
659         char *buf;
660         int buflen;
661
662         if (preflang == NULL)
663                 preflang = "";
664         buflen = strlen(preflang) + strlen(filename) + 4;
665         buf = alloca(buflen);
666         if (buf == NULL)
667                 return NULL;
668
669         for (format = AST_FORMAT_AUDIO_MASK + 1; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
670                 int fd;
671                 const char *fmt;
672
673                 if (!(chan->nativeformats & format))
674                         continue;
675                 fmt = ast_getformatname(format);
676                 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
677                         continue;
678                 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
679                 if (fd >= 0)
680                         return chan->vstream;
681                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
682         }
683         return NULL;
684 }
685
686 struct ast_frame *ast_readframe(struct ast_filestream *s)
687 {
688         struct ast_frame *f = NULL;
689         int whennext = 0;       
690         if (s && s->fmt)
691                 f = s->fmt->read(s, &whennext);
692         if (f) {
693                 ast_set_flag(f, AST_FRFLAG_FROM_FILESTREAM);
694                 ao2_ref(s, +1);
695         }
696         return f;
697 }
698
699 enum fsread_res {
700         FSREAD_FAILURE,
701         FSREAD_SUCCESS_SCHED,
702         FSREAD_SUCCESS_NOSCHED,
703 };
704
705 static int ast_fsread_audio(const void *data);
706
707 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
708 {
709         int whennext = 0;
710
711         while (!whennext) {
712                 struct ast_frame *fr;
713                 
714                 if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
715                         goto return_failure;
716                 
717                 fr = s->fmt->read(s, &whennext);
718                 if (fr) {
719                         ast_set_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
720                         ao2_ref(s, +1);
721                 }
722                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
723                         if (fr)
724                                 ast_log(LOG_WARNING, "Failed to write frame\n");
725                         goto return_failure;
726                 }
727         }
728         if (whennext != s->lasttimeout) {
729                 if (s->owner->timingfd > -1) {
730                         float samp_rate = (float) ast_format_rate(s->fmt->format);
731                         unsigned int rate;
732
733                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
734
735                         ast_settimeout(s->owner, rate, ast_fsread_audio, s);
736                 } else {
737                         s->owner->streamid = ast_sched_add(s->owner->sched, 
738                                 whennext / (ast_format_rate(s->fmt->format) / 1000), ast_fsread_audio, s);
739                 }
740                 s->lasttimeout = whennext;
741                 return FSREAD_SUCCESS_NOSCHED;
742         }
743         return FSREAD_SUCCESS_SCHED;
744
745 return_failure:
746         s->owner->streamid = -1;
747         ast_settimeout(s->owner, 0, NULL, NULL);
748         return FSREAD_FAILURE;
749 }
750
751 static int ast_fsread_audio(const void *data)
752 {
753         struct ast_filestream *fs = (struct ast_filestream *)data;
754         enum fsread_res res;
755
756         res = ast_readaudio_callback(fs);
757
758         if (res == FSREAD_SUCCESS_SCHED)
759                 return 1;
760         
761         return 0;
762 }
763
764 static int ast_fsread_video(const void *data);
765
766 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
767 {
768         int whennext = 0;
769
770         while (!whennext) {
771                 struct ast_frame *fr = s->fmt->read(s, &whennext);
772                 if (fr) {
773                         ast_set_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
774                         ao2_ref(s, +1);
775                 }
776                 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
777                         if (fr)
778                                 ast_log(LOG_WARNING, "Failed to write frame\n");
779                         s->owner->vstreamid = -1;
780                         return FSREAD_FAILURE;
781                 }
782         }
783
784         if (whennext != s->lasttimeout) {
785                 s->owner->vstreamid = ast_sched_add(s->owner->sched, 
786                         whennext / (ast_format_rate(s->fmt->format) / 1000), 
787                         ast_fsread_video, s);
788                 s->lasttimeout = whennext;
789                 return FSREAD_SUCCESS_NOSCHED;
790         }
791
792         return FSREAD_SUCCESS_SCHED;
793 }
794
795 static int ast_fsread_video(const void *data)
796 {
797         struct ast_filestream *fs = (struct ast_filestream *)data;
798         enum fsread_res res;
799
800         res = ast_readvideo_callback(fs);
801
802         if (res == FSREAD_SUCCESS_SCHED)
803                 return 1;
804         
805         return 0;
806 }
807
808 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
809 {
810         s->owner = chan;
811         return 0;
812 }
813
814 int ast_playstream(struct ast_filestream *s)
815 {
816         enum fsread_res res;
817
818         if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
819                 res = ast_readaudio_callback(s);
820         else
821                 res = ast_readvideo_callback(s);
822
823         return (res == FSREAD_FAILURE) ? -1 : 0;
824 }
825
826 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
827 {
828         return fs->fmt->seek(fs, sample_offset, whence);
829 }
830
831 int ast_truncstream(struct ast_filestream *fs)
832 {
833         return fs->fmt->trunc(fs);
834 }
835
836 off_t ast_tellstream(struct ast_filestream *fs)
837 {
838         return fs->fmt->tell(fs);
839 }
840
841 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
842 {
843         return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
844 }
845
846 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
847 {
848         return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
849 }
850
851 int ast_closestream(struct ast_filestream *f)
852 {
853         if (ast_test_flag(&f->fr, AST_FRFLAG_FROM_FILESTREAM)) {
854                 /* If this flag is still set, it essentially means that the reference
855                  * count of f is non-zero. We can't destroy this filestream until
856                  * whatever is using the filestream's frame has finished.
857                  *
858                  * Since this was called, however, we need to remove the reference from
859                  * when this filestream was first allocated. That way, when the embedded
860                  * frame is freed, the refcount will reach 0 and we can finish destroying
861                  * this filestream properly.
862                  */
863                 ao2_ref(f, -1);
864                 return 0;
865         }
866         
867         ao2_ref(f, -1);
868         return 0;
869 }
870
871
872 /*
873  * Look the various language-specific places where a file could exist.
874  */
875 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
876 {
877         char *buf;
878         int buflen;
879
880         if (preflang == NULL)
881                 preflang = "";
882         buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
883         buf = alloca(buflen);
884         if (buf == NULL)
885                 return 0;
886         return fileexists_core(filename, fmt, preflang, buf, buflen);
887 }
888
889 int ast_filedelete(const char *filename, const char *fmt)
890 {
891         return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
892 }
893
894 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
895 {
896         return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
897 }
898
899 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
900 {
901         return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
902 }
903
904 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
905 {
906         struct ast_filestream *fs;
907         struct ast_filestream *vfs=NULL;
908         char fmt[256];
909         int seekattempt;
910         int res;
911
912         fs = ast_openstream(chan, filename, preflang);
913         if (!fs) {
914                 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
915                 return -1;
916         }
917
918         /* check to see if there is any data present (not a zero length file),
919          * done this way because there is no where for ast_openstream_full to
920          * return the file had no data. */
921         seekattempt = fseek(fs->f, -1, SEEK_END);
922         if (!seekattempt)
923                 ast_seekstream(fs, 0, SEEK_SET);
924         else
925                 return 0;
926
927         vfs = ast_openvstream(chan, filename, preflang);
928         if (vfs) {
929                 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
930         }
931
932         if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
933                 fs->orig_chan_name = ast_strdup(chan->name);
934         if (ast_applystream(chan, fs))
935                 return -1;
936         if (vfs && ast_applystream(chan, vfs))
937                 return -1;
938         res = ast_playstream(fs);
939         if (!res && vfs)
940                 res = ast_playstream(vfs);
941         ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
942
943         return res;
944 }
945
946 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
947 {
948         FILE *bfile;
949         struct ast_format *f;
950         struct ast_filestream *fs = NULL;
951         char *fn;
952         int format_found = 0;   
953
954         AST_RWLIST_RDLOCK(&formats);
955
956         AST_RWLIST_TRAVERSE(&formats, f, list) {
957                 fs = NULL;
958                 if (!exts_compare(f->exts, type))
959                         continue;
960                 else 
961                         format_found = 1;
962
963                 fn = build_filename(filename, type);
964                 errno = 0;
965                 bfile = fopen(fn, "r");
966
967                 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
968                         ast_log(LOG_WARNING, "Unable to open %s\n", fn);
969                         if (fs)
970                                 ast_free(fs);
971                         fs = NULL;
972                         if (bfile)
973                                 fclose(bfile);
974                         ast_free(fn);
975                         break;                          
976                 }
977                 /* found it */
978                 fs->trans = NULL;
979                 fs->fmt = f;
980                 fs->flags = flags;
981                 fs->mode = mode;
982                 fs->filename = ast_strdup(filename);
983                 fs->vfs = NULL;
984                 break;
985         }
986
987         AST_RWLIST_UNLOCK(&formats);
988         if (!format_found)
989                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
990
991         return fs;
992 }
993
994 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
995 {
996         int fd, myflags = 0;
997         /* compiler claims this variable can be used before initialization... */
998         FILE *bfile = NULL;
999         struct ast_format *f;
1000         struct ast_filestream *fs = NULL;
1001         char *buf = NULL;
1002         size_t size = 0;
1003         int format_found = 0;
1004
1005         AST_RWLIST_RDLOCK(&formats);
1006
1007         /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1008         /* We really can't use O_APPEND as it will break WAV header updates */
1009         if (flags & O_APPEND) { 
1010                 flags &= ~O_APPEND;
1011         } else {
1012                 myflags = O_TRUNC;
1013         }
1014         
1015         myflags |= O_WRONLY | O_CREAT;
1016
1017         /* XXX need to fix this - we should just do the fopen,
1018          * not open followed by fdopen()
1019          */
1020         AST_RWLIST_TRAVERSE(&formats, f, list) {
1021                 char *fn, *orig_fn = NULL;
1022                 if (fs)
1023                         break;
1024
1025                 if (!exts_compare(f->exts, type))
1026                         continue;
1027                 else
1028                         format_found = 1;
1029
1030                 fn = build_filename(filename, type);
1031                 fd = open(fn, flags | myflags, mode);
1032                 if (fd > -1) {
1033                         /* fdopen() the resulting file stream */
1034                         bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1035                         if (!bfile) {
1036                                 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1037                                 close(fd);
1038                                 fd = -1;
1039                         }
1040                 }
1041                 
1042                 if (ast_opt_cache_record_files && (fd > -1)) {
1043                         char *c;
1044
1045                         fclose(bfile);  /* this also closes fd */
1046                         /*
1047                           We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1048                           What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1049                         */
1050                         orig_fn = ast_strdupa(fn);
1051                         for (c = fn; *c; c++)
1052                                 if (*c == '/')
1053                                         *c = '_';
1054
1055                         size = strlen(fn) + strlen(record_cache_dir) + 2;
1056                         buf = alloca(size);
1057                         strcpy(buf, record_cache_dir);
1058                         strcat(buf, "/");
1059                         strcat(buf, fn);
1060                         ast_free(fn);
1061                         fn = buf;
1062                         fd = open(fn, flags | myflags, mode);
1063                         if (fd > -1) {
1064                                 /* fdopen() the resulting file stream */
1065                                 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1066                                 if (!bfile) {
1067                                         ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1068                                         close(fd);
1069                                         fd = -1;
1070                                 }
1071                         }
1072                 }
1073                 if (fd > -1) {
1074                         errno = 0;
1075                         fs = get_filestream(f, bfile);
1076                         if (!fs || rewrite_wrapper(fs, comment)) {
1077                                 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1078                                 close(fd);
1079                                 if (orig_fn) {
1080                                         unlink(fn);
1081                                         unlink(orig_fn);
1082                                 }
1083                                 if (fs)
1084                                         ast_free(fs);
1085                                 fs = NULL;
1086                                 continue;
1087                         }
1088                         fs->trans = NULL;
1089                         fs->fmt = f;
1090                         fs->flags = flags;
1091                         fs->mode = mode;
1092                         if (orig_fn) {
1093                                 fs->realfilename = ast_strdup(orig_fn);
1094                                 fs->filename = ast_strdup(fn);
1095                         } else {
1096                                 fs->realfilename = NULL;
1097                                 fs->filename = ast_strdup(filename);
1098                         }
1099                         fs->vfs = NULL;
1100                         /* If truncated, we'll be at the beginning; if not truncated, then append */
1101
1102                         if ((fs->write_buffer = ast_malloc(32768))){
1103                                 setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1104                         }
1105
1106                         f->seek(fs, 0, SEEK_END);
1107                 } else if (errno != EEXIST) {
1108                         ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1109                         if (orig_fn)
1110                                 unlink(orig_fn);
1111                 }
1112                 /* if buf != NULL then fn is already free and pointing to it */
1113                 if (!buf)
1114                         ast_free(fn);
1115         }
1116
1117         AST_RWLIST_UNLOCK(&formats);
1118
1119         if (!format_found)
1120                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1121
1122         return fs;
1123 }
1124
1125 /*!
1126  * \brief the core of all waitstream() functions
1127  */
1128 static int waitstream_core(struct ast_channel *c, const char *breakon,
1129         const char *forward, const char *reverse, int skip_ms,
1130         int audiofd, int cmdfd,  const char *context)
1131 {
1132         const char *orig_chan_name = NULL;
1133         int err = 0;
1134
1135         if (!breakon)
1136                 breakon = "";
1137         if (!forward)
1138                 forward = "";
1139         if (!reverse)
1140                 reverse = "";
1141
1142         /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1143         ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
1144
1145         if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
1146                 orig_chan_name = ast_strdupa(c->name);
1147
1148         while (c->stream) {
1149                 int res;
1150                 int ms;
1151
1152                 if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
1153                         ast_stopstream(c);
1154                         err = 1;
1155                         break;
1156                 }
1157
1158                 ms = ast_sched_wait(c->sched);
1159
1160                 if (ms < 0 && !c->timingfunc) {
1161                         ast_stopstream(c);
1162                         break;
1163                 }
1164                 if (ms < 0)
1165                         ms = 1000;
1166                 if (cmdfd < 0) {
1167                         res = ast_waitfor(c, ms);
1168                         if (res < 0) {
1169                                 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1170                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1171                                 return res;
1172                         }
1173                 } else {
1174                         int outfd;
1175                         struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1176                         if (!rchan && (outfd < 0) && (ms)) {
1177                                 /* Continue */
1178                                 if (errno == EINTR)
1179                                         continue;
1180                                 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1181                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1182                                 return -1;
1183                         } else if (outfd > -1) { /* this requires cmdfd set */
1184                                 /* The FD we were watching has something waiting */
1185                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1186                                 return 1;
1187                         }
1188                         /* if rchan is set, it is 'c' */
1189                         res = rchan ? 1 : 0; /* map into 'res' values */
1190                 }
1191                 if (res > 0) {
1192                         struct ast_frame *fr = ast_read(c);
1193                         if (!fr) {
1194                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1195                                 return -1;
1196                         }
1197                         switch (fr->frametype) {
1198                         case AST_FRAME_DTMF_END:
1199                                 if (context) {
1200                                         const char exten[2] = { fr->subclass, '\0' };
1201                                         if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1202                                                 res = fr->subclass;
1203                                                 ast_frfree(fr);
1204                                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1205                                                 return res;
1206                                         }
1207                                 } else {
1208                                         res = fr->subclass;
1209                                         if (strchr(forward, res)) {
1210                                                 ast_stream_fastforward(c->stream, skip_ms);
1211                                         } else if (strchr(reverse, res)) {
1212                                                 ast_stream_rewind(c->stream, skip_ms);
1213                                         } else if (strchr(breakon, res)) {
1214                                                 ast_frfree(fr);
1215                                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1216                                                 return res;
1217                                         }                                       
1218                                 }
1219                                 break;
1220                         case AST_FRAME_CONTROL:
1221                                 switch (fr->subclass) {
1222                                 case AST_CONTROL_HANGUP:
1223                                 case AST_CONTROL_BUSY:
1224                                 case AST_CONTROL_CONGESTION:
1225                                         ast_frfree(fr);
1226                                         ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1227                                         return -1;
1228                                 case AST_CONTROL_RINGING:
1229                                 case AST_CONTROL_ANSWER:
1230                                 case AST_CONTROL_VIDUPDATE:
1231                                 case AST_CONTROL_SRCUPDATE:
1232                                 case AST_CONTROL_HOLD:
1233                                 case AST_CONTROL_UNHOLD:
1234                                         /* Unimportant */
1235                                         break;
1236                                 default:
1237                                         ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1238                                 }
1239                                 break;
1240                         case AST_FRAME_VOICE:
1241                                 /* Write audio if appropriate */
1242                                 if (audiofd > -1) {
1243                                         if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1244                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1245                                         }
1246                                 }
1247                         default:
1248                                 /* Ignore all others */
1249                                 break;
1250                         }
1251                         ast_frfree(fr);
1252                 }
1253                 ast_sched_runq(c->sched);
1254         }
1255
1256         ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1257
1258         return (err || c->_softhangup) ? -1 : 0;
1259 }
1260
1261 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1262 {
1263         return waitstream_core(c, breakon, forward, reverse, ms,
1264                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1265 }
1266
1267 int ast_waitstream(struct ast_channel *c, const char *breakon)
1268 {
1269         return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1270 }
1271
1272 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1273 {
1274         return waitstream_core(c, breakon, NULL, NULL, 0,
1275                 audiofd, cmdfd, NULL /* no context */);
1276 }
1277
1278 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1279 {
1280         /* Waitstream, with return in the case of a valid 1 digit extension */
1281         /* in the current or specified context being pressed */
1282
1283         if (!context)
1284                 context = c->context;
1285         return waitstream_core(c, NULL, NULL, NULL, 0,
1286                 -1, -1, context);
1287 }
1288
1289 void ast_filestream_frame_freed(struct ast_frame *fr)
1290 {
1291         struct ast_filestream *fs;
1292
1293         ast_clear_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
1294
1295         fs = (struct ast_filestream *) (((char *) fr) - offsetof(struct ast_filestream, fr));
1296
1297         ao2_ref(fs, -1);
1298 }
1299
1300 /*
1301  * if the file name is non-empty, try to play it.
1302  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1303  * If digits == "" then we can simply check for non-zero.
1304  */
1305 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1306 {
1307         int res = 0;
1308         if (!ast_strlen_zero(file)) {
1309                 res = ast_streamfile(chan, file, chan->language);
1310                 if (!res) {
1311                         res = ast_waitstream(chan, digits);
1312                 }
1313         }
1314         return res;
1315
1316
1317 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1318 {
1319 #define FORMAT "%-10s %-10s %-20s\n"
1320 #define FORMAT2 "%-10s %-10s %-20s\n"
1321         struct ast_format *f;
1322         int count_fmt = 0;
1323
1324         switch (cmd) {
1325         case CLI_INIT:
1326                 e->command = "core show file formats";
1327                 e->usage =
1328                         "Usage: core show file formats\n"
1329                         "       Displays currently registered file formats (if any).\n";
1330                 return NULL;
1331         case CLI_GENERATE:
1332                 return NULL;
1333         }
1334
1335         if (a->argc != 4)
1336                 return CLI_SHOWUSAGE;
1337
1338         ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1339         ast_cli(a->fd, FORMAT, "------", "----", "----------");
1340
1341         AST_RWLIST_RDLOCK(&formats);
1342         AST_RWLIST_TRAVERSE(&formats, f, list) {
1343                 ast_cli(a->fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1344                 count_fmt++;
1345         }
1346         AST_RWLIST_UNLOCK(&formats);
1347         ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1348         return CLI_SUCCESS;
1349 #undef FORMAT
1350 #undef FORMAT2
1351 }
1352
1353 struct ast_cli_entry cli_file[] = {
1354         AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1355 };
1356
1357 int ast_file_init(void)
1358 {
1359         ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
1360         return 0;
1361 }