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