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