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