Fix a problem where file playback would cause fds to remain open forever
[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                                         ast_free(fn);
453                                         ast_closestream(s);
454                                         continue;       /* cannot run open on file */
455                                 }
456                                 if (st.st_size == 0) {
457                                         ast_log(LOG_WARNING, "File %s detected to have zero size.\n", fn);
458                                 }
459                                 /* ok this is good for OPEN */
460                                 res = 1;        /* found */
461                                 s->lasttimeout = -1;
462                                 s->fmt = f;
463                                 s->trans = NULL;
464                                 s->filename = NULL;
465                                 if (s->fmt->format & AST_FORMAT_AUDIO_MASK) {
466                                         if (chan->stream)
467                                                 ast_closestream(chan->stream);
468                                         chan->stream = s;
469                                 } else {
470                                         if (chan->vstream)
471                                                 ast_closestream(chan->vstream);
472                                         chan->vstream = s;
473                                 }
474                                 ast_free(fn);
475                                 break;
476                         }
477                         switch (action) {
478                         case ACTION_OPEN:
479                                 break;  /* will never get here */
480
481                         case ACTION_EXISTS:     /* return the matching format */
482                                 res |= f->format;
483                                 break;
484
485                         case ACTION_DELETE:
486                                 if ( (res = unlink(fn)) )
487                                         ast_log(LOG_WARNING, "unlink(%s) failed: %s\n", fn, strerror(errno));
488                                 break;
489
490                         case ACTION_RENAME:
491                         case ACTION_COPY: {
492                                 char *nfn = build_filename((const char *)arg2, ext);
493                                 if (!nfn)
494                                         ast_log(LOG_WARNING, "Out of memory\n");
495                                 else {
496                                         res = action == ACTION_COPY ? copy(fn, nfn) : rename(fn, nfn);
497                                         if (res)
498                                                 ast_log(LOG_WARNING, "%s(%s,%s) failed: %s\n",
499                                                         action == ACTION_COPY ? "copy" : "rename",
500                                                          fn, nfn, strerror(errno));
501                                         ast_free(nfn);
502                                 }
503                             }
504                                 break;
505
506                         default:
507                                 ast_log(LOG_WARNING, "Unknown helper %d\n", action);
508                         }
509                         ast_free(fn);
510                 }
511         }
512         AST_RWLIST_UNLOCK(&formats);
513         return res;
514 }
515
516 static int is_absolute_path(const char *filename)
517 {
518         return filename[0] == '/';
519 }
520
521 static int fileexists_test(const char *filename, const char *fmt, const char *lang,
522                            char *buf, int buflen)
523 {
524         if (buf == NULL) {
525                 return -1;
526         }
527
528         if (ast_language_is_prefix && !is_absolute_path(filename)) { /* new layout */
529                 if (lang) {
530                         snprintf(buf, buflen, "%s/%s", lang, filename);
531                 } else {
532                         snprintf(buf, buflen, "%s", filename);
533                 }
534         } else { /* old layout */
535                 strcpy(buf, filename);  /* first copy the full string */
536                 if (lang) {
537                         /* insert the language and suffix if needed */
538                         const char *c = strrchr(filename, '/');
539                         int offset = c ? c - filename + 1 : 0;  /* points right after the last '/' */
540                         snprintf(buf + offset, buflen - offset, "%s/%s", lang, filename + offset);
541                 }
542         }
543
544         return ast_filehelper(buf, NULL, fmt, ACTION_EXISTS);
545 }
546
547 /*!
548  * \brief helper routine to locate a file with a given format
549  * and language preference.
550  * Try preflang, preflang with stripped '_' suffix, or NULL.
551  * In the standard asterisk, language goes just before the last component.
552  * In an alternative configuration, the language should be a prefix
553  * to the actual filename.
554  *
555  * The last parameter(s) point to a buffer of sufficient size,
556  * which on success is filled with the matching filename.
557  */
558 static int fileexists_core(const char *filename, const char *fmt, const char *preflang,
559                            char *buf, int buflen)
560 {
561         int res = -1;
562         char *lang = NULL;
563
564         if (buf == NULL) {
565                 return -1;
566         }
567
568         /* We try languages in the following order:
569          *    preflang (may include dialect)
570          *    lang (preflang without dialect - if any)
571          *    <none>
572          *    default (unless the same as preflang or lang without dialect)
573          */
574
575         /* Try preferred language */
576         if (!ast_strlen_zero(preflang)) {
577                 /* try the preflang exactly as it was requested */
578                 if ((res = fileexists_test(filename, fmt, preflang, buf, buflen)) > 0) {
579                         return res;
580                 } else {
581                         /* try without a dialect */
582                         char *postfix = NULL;
583                         postfix = lang = ast_strdupa(preflang);
584
585                         strsep(&postfix, "_");
586                         if (postfix) {
587                                 if ((res = fileexists_test(filename, fmt, lang, buf, buflen)) > 0) {
588                                         return res;
589                                 }
590                         }
591                 }
592         }
593
594         /* Try without any language */
595         if ((res = fileexists_test(filename, fmt, NULL, buf, buflen)) > 0) {
596                 return res;
597         }
598
599         /* Finally try the default language unless it was already tried before */
600         if ((ast_strlen_zero(preflang) || strcmp(preflang, DEFAULT_LANGUAGE)) && (ast_strlen_zero(lang) || strcmp(lang, DEFAULT_LANGUAGE))) {
601                 if ((res = fileexists_test(filename, fmt, DEFAULT_LANGUAGE, buf, buflen)) > 0) {
602                         return res;
603                 }
604         }
605
606         return 0;
607 }
608
609 struct ast_filestream *ast_openstream(struct ast_channel *chan, const char *filename, const char *preflang)
610 {
611         return ast_openstream_full(chan, filename, preflang, 0);
612 }
613
614 struct ast_filestream *ast_openstream_full(struct ast_channel *chan, const char *filename, const char *preflang, int asis)
615 {
616         /* 
617          * Use fileexists_core() to find a file in a compatible
618          * language and format, set up a suitable translator,
619          * and open the stream.
620          */
621         int fmts, res, buflen;
622         char *buf;
623
624         if (!asis) {
625                 /* do this first, otherwise we detect the wrong writeformat */
626                 ast_stopstream(chan);
627                 if (chan->generator)
628                         ast_deactivate_generator(chan);
629         }
630         if (preflang == NULL)
631                 preflang = "";
632         buflen = strlen(preflang) + strlen(filename) + 4;
633         buf = alloca(buflen);
634         if (buf == NULL)
635                 return NULL;
636         fmts = fileexists_core(filename, NULL, preflang, buf, buflen);
637         if (fmts > 0)
638                 fmts &= AST_FORMAT_AUDIO_MASK;
639         if (fmts < 1) {
640                 ast_log(LOG_WARNING, "File %s does not exist in any format\n", filename);
641                 return NULL;
642         }
643         chan->oldwriteformat = chan->writeformat;
644         /* Set the channel to a format we can work with */
645         res = ast_set_write_format(chan, fmts);
646         res = ast_filehelper(buf, chan, NULL, ACTION_OPEN);
647         if (res >= 0)
648                 return chan->stream;
649         return NULL;
650 }
651
652 struct ast_filestream *ast_openvstream(struct ast_channel *chan, const char *filename, const char *preflang)
653 {
654         /* As above, but for video. But here we don't have translators
655          * so we must enforce a format.
656          */
657         unsigned int format;
658         char *buf;
659         int buflen;
660
661         if (preflang == NULL)
662                 preflang = "";
663         buflen = strlen(preflang) + strlen(filename) + 4;
664         buf = alloca(buflen);
665         if (buf == NULL)
666                 return NULL;
667
668         for (format = AST_FORMAT_AUDIO_MASK + 1; format <= AST_FORMAT_VIDEO_MASK; format = format << 1) {
669                 int fd;
670                 const char *fmt;
671
672                 if (!(chan->nativeformats & format))
673                         continue;
674                 fmt = ast_getformatname(format);
675                 if ( fileexists_core(filename, fmt, preflang, buf, buflen) < 1) /* no valid format */
676                         continue;
677                 fd = ast_filehelper(buf, chan, fmt, ACTION_OPEN);
678                 if (fd >= 0)
679                         return chan->vstream;
680                 ast_log(LOG_WARNING, "File %s has video but couldn't be opened\n", filename);
681         }
682         return NULL;
683 }
684
685 struct ast_frame *ast_readframe(struct ast_filestream *s)
686 {
687         struct ast_frame *f = NULL;
688         int whennext = 0;       
689         if (s && s->fmt)
690                 f = s->fmt->read(s, &whennext);
691         if (f) {
692                 ast_set_flag(f, AST_FRFLAG_FROM_FILESTREAM);
693                 ao2_ref(s, +1);
694         }
695         return f;
696 }
697
698 enum fsread_res {
699         FSREAD_FAILURE,
700         FSREAD_SUCCESS_SCHED,
701         FSREAD_SUCCESS_NOSCHED,
702 };
703
704 static int ast_fsread_audio(const void *data);
705
706 static enum fsread_res ast_readaudio_callback(struct ast_filestream *s)
707 {
708         int whennext = 0;
709
710         while (!whennext) {
711                 struct ast_frame *fr;
712                 
713                 if (s->orig_chan_name && strcasecmp(s->owner->name, s->orig_chan_name))
714                         goto return_failure;
715                 
716                 fr = s->fmt->read(s, &whennext);
717                 if (fr) {
718                         ast_set_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
719                         ao2_ref(s, +1);
720                 }
721                 if (!fr /* stream complete */ || ast_write(s->owner, fr) /* error writing */) {
722                         if (fr) {
723                                 ast_log(LOG_WARNING, "Failed to write frame\n");
724                                 ast_frfree(fr);
725                         }
726                         goto return_failure;
727                 } 
728                 if (fr) {
729                         ast_frfree(fr);
730                 }
731         }
732         if (whennext != s->lasttimeout) {
733                 if (s->owner->timingfd > -1) {
734                         float samp_rate = (float) ast_format_rate(s->fmt->format);
735                         unsigned int rate;
736
737                         rate = (unsigned int) roundf(samp_rate / ((float) whennext));
738
739                         ast_settimeout(s->owner, rate, ast_fsread_audio, s);
740                 } else {
741                         s->owner->streamid = ast_sched_add(s->owner->sched, 
742                                 whennext / (ast_format_rate(s->fmt->format) / 1000), ast_fsread_audio, s);
743                 }
744                 s->lasttimeout = whennext;
745                 return FSREAD_SUCCESS_NOSCHED;
746         }
747         return FSREAD_SUCCESS_SCHED;
748
749 return_failure:
750         s->owner->streamid = -1;
751         ast_settimeout(s->owner, 0, NULL, NULL);
752         return FSREAD_FAILURE;
753 }
754
755 static int ast_fsread_audio(const void *data)
756 {
757         struct ast_filestream *fs = (struct ast_filestream *)data;
758         enum fsread_res res;
759
760         res = ast_readaudio_callback(fs);
761
762         if (res == FSREAD_SUCCESS_SCHED)
763                 return 1;
764         
765         return 0;
766 }
767
768 static int ast_fsread_video(const void *data);
769
770 static enum fsread_res ast_readvideo_callback(struct ast_filestream *s)
771 {
772         int whennext = 0;
773
774         while (!whennext) {
775                 struct ast_frame *fr = s->fmt->read(s, &whennext);
776                 if (fr) {
777                         ast_set_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
778                         ao2_ref(s, +1);
779                 }
780                 if (!fr || ast_write(s->owner, fr)) { /* no stream or error, as above */
781                         if (fr)
782                                 ast_log(LOG_WARNING, "Failed to write frame\n");
783                         s->owner->vstreamid = -1;
784                         return FSREAD_FAILURE;
785                 }
786         }
787
788         if (whennext != s->lasttimeout) {
789                 s->owner->vstreamid = ast_sched_add(s->owner->sched, 
790                         whennext / (ast_format_rate(s->fmt->format) / 1000), 
791                         ast_fsread_video, s);
792                 s->lasttimeout = whennext;
793                 return FSREAD_SUCCESS_NOSCHED;
794         }
795
796         return FSREAD_SUCCESS_SCHED;
797 }
798
799 static int ast_fsread_video(const void *data)
800 {
801         struct ast_filestream *fs = (struct ast_filestream *)data;
802         enum fsread_res res;
803
804         res = ast_readvideo_callback(fs);
805
806         if (res == FSREAD_SUCCESS_SCHED)
807                 return 1;
808         
809         return 0;
810 }
811
812 int ast_applystream(struct ast_channel *chan, struct ast_filestream *s)
813 {
814         s->owner = chan;
815         return 0;
816 }
817
818 int ast_playstream(struct ast_filestream *s)
819 {
820         enum fsread_res res;
821
822         if (s->fmt->format & AST_FORMAT_AUDIO_MASK)
823                 res = ast_readaudio_callback(s);
824         else
825                 res = ast_readvideo_callback(s);
826
827         return (res == FSREAD_FAILURE) ? -1 : 0;
828 }
829
830 int ast_seekstream(struct ast_filestream *fs, off_t sample_offset, int whence)
831 {
832         return fs->fmt->seek(fs, sample_offset, whence);
833 }
834
835 int ast_truncstream(struct ast_filestream *fs)
836 {
837         return fs->fmt->trunc(fs);
838 }
839
840 off_t ast_tellstream(struct ast_filestream *fs)
841 {
842         return fs->fmt->tell(fs);
843 }
844
845 int ast_stream_fastforward(struct ast_filestream *fs, off_t ms)
846 {
847         return ast_seekstream(fs, ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
848 }
849
850 int ast_stream_rewind(struct ast_filestream *fs, off_t ms)
851 {
852         return ast_seekstream(fs, -ms * DEFAULT_SAMPLES_PER_MS, SEEK_CUR);
853 }
854
855 int ast_closestream(struct ast_filestream *f)
856 {
857         if (ast_test_flag(&f->fr, AST_FRFLAG_FROM_FILESTREAM)) {
858                 /* If this flag is still set, it essentially means that the reference
859                  * count of f is non-zero. We can't destroy this filestream until
860                  * whatever is using the filestream's frame has finished.
861                  *
862                  * Since this was called, however, we need to remove the reference from
863                  * when this filestream was first allocated. That way, when the embedded
864                  * frame is freed, the refcount will reach 0 and we can finish destroying
865                  * this filestream properly.
866                  */
867                 ao2_ref(f, -1);
868                 return 0;
869         }
870         
871         ao2_ref(f, -1);
872         return 0;
873 }
874
875
876 /*
877  * Look the various language-specific places where a file could exist.
878  */
879 int ast_fileexists(const char *filename, const char *fmt, const char *preflang)
880 {
881         char *buf;
882         int buflen;
883
884         if (preflang == NULL)
885                 preflang = "";
886         buflen = strlen(preflang) + strlen(filename) + 4;       /* room for everything */
887         buf = alloca(buflen);
888         if (buf == NULL)
889                 return 0;
890         return fileexists_core(filename, fmt, preflang, buf, buflen);
891 }
892
893 int ast_filedelete(const char *filename, const char *fmt)
894 {
895         return ast_filehelper(filename, NULL, fmt, ACTION_DELETE);
896 }
897
898 int ast_filerename(const char *filename, const char *filename2, const char *fmt)
899 {
900         return ast_filehelper(filename, filename2, fmt, ACTION_RENAME);
901 }
902
903 int ast_filecopy(const char *filename, const char *filename2, const char *fmt)
904 {
905         return ast_filehelper(filename, filename2, fmt, ACTION_COPY);
906 }
907
908 int ast_streamfile(struct ast_channel *chan, const char *filename, const char *preflang)
909 {
910         struct ast_filestream *fs;
911         struct ast_filestream *vfs=NULL;
912         char fmt[256];
913         int seekattempt;
914         int res;
915
916         fs = ast_openstream(chan, filename, preflang);
917         if (!fs) {
918                 ast_log(LOG_WARNING, "Unable to open %s (format %s): %s\n", filename, ast_getformatname_multiple(fmt, sizeof(fmt), chan->nativeformats), strerror(errno));
919                 return -1;
920         }
921
922         /* check to see if there is any data present (not a zero length file),
923          * done this way because there is no where for ast_openstream_full to
924          * return the file had no data. */
925         seekattempt = fseek(fs->f, -1, SEEK_END);
926         if (!seekattempt)
927                 ast_seekstream(fs, 0, SEEK_SET);
928         else
929                 return 0;
930
931         vfs = ast_openvstream(chan, filename, preflang);
932         if (vfs) {
933                 ast_debug(1, "Ooh, found a video stream, too, format %s\n", ast_getformatname(vfs->fmt->format));
934         }
935
936         if (ast_test_flag(chan, AST_FLAG_MASQ_NOSTREAM))
937                 fs->orig_chan_name = ast_strdup(chan->name);
938         if (ast_applystream(chan, fs))
939                 return -1;
940         if (vfs && ast_applystream(chan, vfs))
941                 return -1;
942         res = ast_playstream(fs);
943         if (!res && vfs)
944                 res = ast_playstream(vfs);
945         ast_verb(3, "<%s> Playing '%s.%s' (language '%s')\n", chan->name, filename, ast_getformatname(chan->writeformat), preflang ? preflang : "default");
946
947         return res;
948 }
949
950 struct ast_filestream *ast_readfile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
951 {
952         FILE *bfile;
953         struct ast_format *f;
954         struct ast_filestream *fs = NULL;
955         char *fn;
956         int format_found = 0;   
957
958         AST_RWLIST_RDLOCK(&formats);
959
960         AST_RWLIST_TRAVERSE(&formats, f, list) {
961                 fs = NULL;
962                 if (!exts_compare(f->exts, type))
963                         continue;
964                 else 
965                         format_found = 1;
966
967                 fn = build_filename(filename, type);
968                 errno = 0;
969                 bfile = fopen(fn, "r");
970
971                 if (!bfile || (fs = get_filestream(f, bfile)) == NULL || open_wrapper(fs) ) {
972                         ast_log(LOG_WARNING, "Unable to open %s\n", fn);
973                         if (fs) {
974                                 ast_closestream(fs);
975                         }
976                         fs = NULL;
977                         bfile = NULL;
978                         ast_free(fn);
979                         break;                          
980                 }
981                 /* found it */
982                 fs->trans = NULL;
983                 fs->fmt = f;
984                 fs->flags = flags;
985                 fs->mode = mode;
986                 fs->filename = ast_strdup(filename);
987                 fs->vfs = NULL;
988                 break;
989         }
990
991         AST_RWLIST_UNLOCK(&formats);
992         if (!format_found)
993                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
994
995         return fs;
996 }
997
998 struct ast_filestream *ast_writefile(const char *filename, const char *type, const char *comment, int flags, int check, mode_t mode)
999 {
1000         int fd, myflags = 0;
1001         /* compiler claims this variable can be used before initialization... */
1002         FILE *bfile = NULL;
1003         struct ast_format *f;
1004         struct ast_filestream *fs = NULL;
1005         char *buf = NULL;
1006         size_t size = 0;
1007         int format_found = 0;
1008
1009         AST_RWLIST_RDLOCK(&formats);
1010
1011         /* set the O_TRUNC flag if and only if there is no O_APPEND specified */
1012         /* We really can't use O_APPEND as it will break WAV header updates */
1013         if (flags & O_APPEND) { 
1014                 flags &= ~O_APPEND;
1015         } else {
1016                 myflags = O_TRUNC;
1017         }
1018         
1019         myflags |= O_WRONLY | O_CREAT;
1020
1021         /* XXX need to fix this - we should just do the fopen,
1022          * not open followed by fdopen()
1023          */
1024         AST_RWLIST_TRAVERSE(&formats, f, list) {
1025                 char *fn, *orig_fn = NULL;
1026                 if (fs)
1027                         break;
1028
1029                 if (!exts_compare(f->exts, type))
1030                         continue;
1031                 else
1032                         format_found = 1;
1033
1034                 fn = build_filename(filename, type);
1035                 fd = open(fn, flags | myflags, mode);
1036                 if (fd > -1) {
1037                         /* fdopen() the resulting file stream */
1038                         bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1039                         if (!bfile) {
1040                                 ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1041                                 close(fd);
1042                                 fd = -1;
1043                         }
1044                 }
1045                 
1046                 if (ast_opt_cache_record_files && (fd > -1)) {
1047                         char *c;
1048
1049                         fclose(bfile);  /* this also closes fd */
1050                         /*
1051                           We touch orig_fn just as a place-holder so other things (like vmail) see the file is there.
1052                           What we are really doing is writing to record_cache_dir until we are done then we will mv the file into place.
1053                         */
1054                         orig_fn = ast_strdupa(fn);
1055                         for (c = fn; *c; c++)
1056                                 if (*c == '/')
1057                                         *c = '_';
1058
1059                         size = strlen(fn) + strlen(record_cache_dir) + 2;
1060                         buf = alloca(size);
1061                         strcpy(buf, record_cache_dir);
1062                         strcat(buf, "/");
1063                         strcat(buf, fn);
1064                         ast_free(fn);
1065                         fn = buf;
1066                         fd = open(fn, flags | myflags, mode);
1067                         if (fd > -1) {
1068                                 /* fdopen() the resulting file stream */
1069                                 bfile = fdopen(fd, ((flags | myflags) & O_RDWR) ? "w+" : "w");
1070                                 if (!bfile) {
1071                                         ast_log(LOG_WARNING, "Whoa, fdopen failed: %s!\n", strerror(errno));
1072                                         close(fd);
1073                                         fd = -1;
1074                                 }
1075                         }
1076                 }
1077                 if (fd > -1) {
1078                         errno = 0;
1079                         fs = get_filestream(f, bfile);
1080                         if (!fs || rewrite_wrapper(fs, comment)) {
1081                                 ast_log(LOG_WARNING, "Unable to rewrite %s\n", fn);
1082                                 close(fd);
1083                                 if (orig_fn) {
1084                                         unlink(fn);
1085                                         unlink(orig_fn);
1086                                 }
1087                                 if (fs) {
1088                                         ast_closestream(fs);
1089                                         fs = NULL;
1090                                 }
1091                                 continue;
1092                         }
1093                         fs->trans = NULL;
1094                         fs->fmt = f;
1095                         fs->flags = flags;
1096                         fs->mode = mode;
1097                         if (orig_fn) {
1098                                 fs->realfilename = ast_strdup(orig_fn);
1099                                 fs->filename = ast_strdup(fn);
1100                         } else {
1101                                 fs->realfilename = NULL;
1102                                 fs->filename = ast_strdup(filename);
1103                         }
1104                         fs->vfs = NULL;
1105                         /* If truncated, we'll be at the beginning; if not truncated, then append */
1106
1107                         if ((fs->write_buffer = ast_malloc(32768))){
1108                                 setvbuf(fs->f, fs->write_buffer, _IOFBF, 32768);
1109                         }
1110
1111                         f->seek(fs, 0, SEEK_END);
1112                 } else if (errno != EEXIST) {
1113                         ast_log(LOG_WARNING, "Unable to open file %s: %s\n", fn, strerror(errno));
1114                         if (orig_fn)
1115                                 unlink(orig_fn);
1116                 }
1117                 /* if buf != NULL then fn is already free and pointing to it */
1118                 if (!buf)
1119                         ast_free(fn);
1120         }
1121
1122         AST_RWLIST_UNLOCK(&formats);
1123
1124         if (!format_found)
1125                 ast_log(LOG_WARNING, "No such format '%s'\n", type);
1126
1127         return fs;
1128 }
1129
1130 /*!
1131  * \brief the core of all waitstream() functions
1132  */
1133 static int waitstream_core(struct ast_channel *c, const char *breakon,
1134         const char *forward, const char *reverse, int skip_ms,
1135         int audiofd, int cmdfd,  const char *context)
1136 {
1137         const char *orig_chan_name = NULL;
1138         int err = 0;
1139
1140         if (!breakon)
1141                 breakon = "";
1142         if (!forward)
1143                 forward = "";
1144         if (!reverse)
1145                 reverse = "";
1146
1147         /* Switch the channel to end DTMF frame only. waitstream_core doesn't care about the start of DTMF. */
1148         ast_set_flag(c, AST_FLAG_END_DTMF_ONLY);
1149
1150         if (ast_test_flag(c, AST_FLAG_MASQ_NOSTREAM))
1151                 orig_chan_name = ast_strdupa(c->name);
1152
1153         while (c->stream) {
1154                 int res;
1155                 int ms;
1156
1157                 if (orig_chan_name && strcasecmp(orig_chan_name, c->name)) {
1158                         ast_stopstream(c);
1159                         err = 1;
1160                         break;
1161                 }
1162
1163                 ms = ast_sched_wait(c->sched);
1164
1165                 if (ms < 0 && !c->timingfunc) {
1166                         ast_stopstream(c);
1167                         break;
1168                 }
1169                 if (ms < 0)
1170                         ms = 1000;
1171                 if (cmdfd < 0) {
1172                         res = ast_waitfor(c, ms);
1173                         if (res < 0) {
1174                                 ast_log(LOG_WARNING, "Select failed (%s)\n", strerror(errno));
1175                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1176                                 return res;
1177                         }
1178                 } else {
1179                         int outfd;
1180                         struct ast_channel *rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
1181                         if (!rchan && (outfd < 0) && (ms)) {
1182                                 /* Continue */
1183                                 if (errno == EINTR)
1184                                         continue;
1185                                 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
1186                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1187                                 return -1;
1188                         } else if (outfd > -1) { /* this requires cmdfd set */
1189                                 /* The FD we were watching has something waiting */
1190                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1191                                 return 1;
1192                         }
1193                         /* if rchan is set, it is 'c' */
1194                         res = rchan ? 1 : 0; /* map into 'res' values */
1195                 }
1196                 if (res > 0) {
1197                         struct ast_frame *fr = ast_read(c);
1198                         if (!fr) {
1199                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1200                                 return -1;
1201                         }
1202                         switch (fr->frametype) {
1203                         case AST_FRAME_DTMF_END:
1204                                 if (context) {
1205                                         const char exten[2] = { fr->subclass, '\0' };
1206                                         if (ast_exists_extension(c, context, exten, 1, c->cid.cid_num)) {
1207                                                 res = fr->subclass;
1208                                                 ast_frfree(fr);
1209                                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1210                                                 return res;
1211                                         }
1212                                 } else {
1213                                         res = fr->subclass;
1214                                         if (strchr(forward, res)) {
1215                                                 ast_stream_fastforward(c->stream, skip_ms);
1216                                         } else if (strchr(reverse, res)) {
1217                                                 ast_stream_rewind(c->stream, skip_ms);
1218                                         } else if (strchr(breakon, res)) {
1219                                                 ast_frfree(fr);
1220                                                 ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1221                                                 return res;
1222                                         }                                       
1223                                 }
1224                                 break;
1225                         case AST_FRAME_CONTROL:
1226                                 switch (fr->subclass) {
1227                                 case AST_CONTROL_HANGUP:
1228                                 case AST_CONTROL_BUSY:
1229                                 case AST_CONTROL_CONGESTION:
1230                                         ast_frfree(fr);
1231                                         ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1232                                         return -1;
1233                                 case AST_CONTROL_RINGING:
1234                                 case AST_CONTROL_ANSWER:
1235                                 case AST_CONTROL_VIDUPDATE:
1236                                 case AST_CONTROL_SRCUPDATE:
1237                                 case AST_CONTROL_HOLD:
1238                                 case AST_CONTROL_UNHOLD:
1239                                         /* Unimportant */
1240                                         break;
1241                                 default:
1242                                         ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", fr->subclass);
1243                                 }
1244                                 break;
1245                         case AST_FRAME_VOICE:
1246                                 /* Write audio if appropriate */
1247                                 if (audiofd > -1) {
1248                                         if (write(audiofd, fr->data.ptr, fr->datalen) < 0) {
1249                                                 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
1250                                         }
1251                                 }
1252                         default:
1253                                 /* Ignore all others */
1254                                 break;
1255                         }
1256                         ast_frfree(fr);
1257                 }
1258                 ast_sched_runq(c->sched);
1259         }
1260
1261         ast_clear_flag(c, AST_FLAG_END_DTMF_ONLY);
1262
1263         return (err || c->_softhangup) ? -1 : 0;
1264 }
1265
1266 int ast_waitstream_fr(struct ast_channel *c, const char *breakon, const char *forward, const char *reverse, int ms)
1267 {
1268         return waitstream_core(c, breakon, forward, reverse, ms,
1269                 -1 /* no audiofd */, -1 /* no cmdfd */, NULL /* no context */);
1270 }
1271
1272 int ast_waitstream(struct ast_channel *c, const char *breakon)
1273 {
1274         return waitstream_core(c, breakon, NULL, NULL, 0, -1, -1, NULL);
1275 }
1276
1277 int ast_waitstream_full(struct ast_channel *c, const char *breakon, int audiofd, int cmdfd)
1278 {
1279         return waitstream_core(c, breakon, NULL, NULL, 0,
1280                 audiofd, cmdfd, NULL /* no context */);
1281 }
1282
1283 int ast_waitstream_exten(struct ast_channel *c, const char *context)
1284 {
1285         /* Waitstream, with return in the case of a valid 1 digit extension */
1286         /* in the current or specified context being pressed */
1287
1288         if (!context)
1289                 context = c->context;
1290         return waitstream_core(c, NULL, NULL, NULL, 0,
1291                 -1, -1, context);
1292 }
1293
1294 void ast_filestream_frame_freed(struct ast_frame *fr)
1295 {
1296         struct ast_filestream *fs;
1297
1298         ast_clear_flag(fr, AST_FRFLAG_FROM_FILESTREAM);
1299
1300         fs = (struct ast_filestream *) (((char *) fr) - offsetof(struct ast_filestream, fr));
1301
1302         ao2_ref(fs, -1);
1303 }
1304
1305 /*
1306  * if the file name is non-empty, try to play it.
1307  * Return 0 if success, -1 if error, digit if interrupted by a digit.
1308  * If digits == "" then we can simply check for non-zero.
1309  */
1310 int ast_stream_and_wait(struct ast_channel *chan, const char *file, const char *digits)
1311 {
1312         int res = 0;
1313         if (!ast_strlen_zero(file)) {
1314                 res = ast_streamfile(chan, file, chan->language);
1315                 if (!res) {
1316                         res = ast_waitstream(chan, digits);
1317                 }
1318         }
1319         return res;
1320
1321
1322 static char *handle_cli_core_show_file_formats(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
1323 {
1324 #define FORMAT "%-10s %-10s %-20s\n"
1325 #define FORMAT2 "%-10s %-10s %-20s\n"
1326         struct ast_format *f;
1327         int count_fmt = 0;
1328
1329         switch (cmd) {
1330         case CLI_INIT:
1331                 e->command = "core show file formats";
1332                 e->usage =
1333                         "Usage: core show file formats\n"
1334                         "       Displays currently registered file formats (if any).\n";
1335                 return NULL;
1336         case CLI_GENERATE:
1337                 return NULL;
1338         }
1339
1340         if (a->argc != 4)
1341                 return CLI_SHOWUSAGE;
1342
1343         ast_cli(a->fd, FORMAT, "Format", "Name", "Extensions");
1344         ast_cli(a->fd, FORMAT, "------", "----", "----------");
1345
1346         AST_RWLIST_RDLOCK(&formats);
1347         AST_RWLIST_TRAVERSE(&formats, f, list) {
1348                 ast_cli(a->fd, FORMAT2, ast_getformatname(f->format), f->name, f->exts);
1349                 count_fmt++;
1350         }
1351         AST_RWLIST_UNLOCK(&formats);
1352         ast_cli(a->fd, "%d file formats registered.\n", count_fmt);
1353         return CLI_SUCCESS;
1354 #undef FORMAT
1355 #undef FORMAT2
1356 }
1357
1358 struct ast_cli_entry cli_file[] = {
1359         AST_CLI_DEFINE(handle_cli_core_show_file_formats, "Displays file formats")
1360 };
1361
1362 int ast_file_init(void)
1363 {
1364         ast_cli_register_multiple(cli_file, ARRAY_LEN(cli_file));
1365         return 0;
1366 }