]> git.sesse.net Git - vlc/blob - src/config/help.c
Android vout: reference files used
[vlc] / src / config / help.c
1 /*****************************************************************************
2  * help.c: command line help
3  *****************************************************************************
4  * Copyright (C) 1998-2011 VLC authors and VideoLAN
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <wchar.h>
29 #include <wctype.h>
30
31 #include <vlc_common.h>
32 #include <vlc_modules.h>
33 #include <vlc_plugin.h>
34 #include "modules/modules.h"
35 #include "config/configuration.h"
36 #include "libvlc.h"
37
38 #if defined( _WIN32 )
39 # include <vlc_charset.h>
40 # define wcwidth(cp) (cp, 1) /* LOL */
41 #else
42 # include <unistd.h>
43 # include <termios.h>
44 # include <sys/ioctl.h>
45 #endif
46
47 #if defined( _WIN32 ) && !VLC_WINSTORE_APP
48 static void ShowConsole (void);
49 static void PauseConsole (void);
50 #else
51 # define ShowConsole() (void)0
52 # define PauseConsole() (void)0
53 #endif
54
55 static void Help (vlc_object_t *, const char *);
56 static void Usage (vlc_object_t *, const char *);
57 static void Version (void);
58 static void ListModules (vlc_object_t *, bool);
59
60 /**
61  * Returns the console width or a best guess.
62  */
63 static unsigned ConsoleWidth(void)
64 {
65 #ifdef TIOCGWINSZ
66     struct winsize ws;
67
68     if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
69         return ws.ws_col;
70 #endif
71 #ifdef WIOCGETD
72     struct uwdata uw;
73
74     if (ioctl(STDOUT_FILENO, WIOCGETD, &uw) == 0)
75         return uw.uw_height / uw.uw_vs;
76 #endif
77 #if defined (_WIN32) && !VLC_WINSTORE_APP
78     CONSOLE_SCREEN_BUFFER_INFO buf;
79
80     if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &buf))
81         return buf.dwSize.X;
82 #endif
83     return 80;
84 }
85
86 /**
87  * Checks for help command line options such as --help or --version.
88  * If one is found, print the corresponding text.
89  * \return true if a command line options caused some help message to be
90  * printed, false otherwise. 
91  */
92 bool config_PrintHelp (vlc_object_t *obj)
93 {
94     char *str;
95
96     /* Check for short help option */
97     if (var_InheritBool (obj, "help"))
98     {
99         Help (obj, "help");
100         return true;
101     }
102
103     /* Check for version option */
104     if (var_InheritBool (obj, "version"))
105     {
106         Version();
107         return true;
108     }
109
110     /* Check for help on modules */
111     str = var_InheritString (obj, "module");
112     if (str != NULL)
113     {
114         Help (obj, str);
115         free (str);
116         return true;
117     }
118
119     /* Check for full help option */
120     if (var_InheritBool (obj, "full-help"))
121     {
122         var_Create (obj, "advanced", VLC_VAR_BOOL);
123         var_SetBool (obj, "advanced", true);
124         var_Create (obj, "help-verbose", VLC_VAR_BOOL);
125         var_SetBool (obj, "help-verbose", true);
126         Help (obj, "full-help");
127         return true;
128     }
129
130     /* Check for long help option */
131     if (var_InheritBool (obj, "longhelp"))
132     {
133         Help (obj, "longhelp");
134         return true;
135     }
136
137     /* Check for module list option */
138     if (var_InheritBool (obj, "list"))
139     {
140         ListModules (obj, false );
141         return true;
142     }
143
144     if (var_InheritBool (obj, "list-verbose"))
145     {
146         ListModules (obj, true);
147         return true;
148     }
149
150     return false;
151 }
152
153 /*****************************************************************************
154  * Help: print program help
155  *****************************************************************************
156  * Print a short inline help. Message interface is initialized at this stage.
157  *****************************************************************************/
158 static inline void print_help_on_full_help( void )
159 {
160     putchar('\n');
161     puts(_("To get exhaustive help, use '-H'."));
162 }
163
164 static const char vlc_usage[] = N_(
165   "Usage: %s [options] [stream] ...\n"
166   "You can specify multiple streams on the commandline.\n"
167   "They will be enqueued in the playlist.\n"
168   "The first item specified will be played first.\n"
169   "\n"
170   "Options-styles:\n"
171   "  --option  A global option that is set for the duration of the program.\n"
172   "   -option  A single letter version of a global --option.\n"
173   "   :option  An option that only applies to the stream directly before it\n"
174   "            and that overrides previous settings.\n"
175   "\n"
176   "Stream MRL syntax:\n"
177   "  [[access][/demux]://]URL[#[title][:chapter][-[title][:chapter]]]\n"
178   "  [:option=value ...]\n"
179   "\n"
180   "  Many of the global --options can also be used as MRL specific :options.\n"
181   "  Multiple :option=value pairs can be specified.\n"
182   "\n"
183   "URL syntax:\n"
184   "  file:///path/file              Plain media file\n"
185   "  http://host[:port]/file        HTTP URL\n"
186   "  ftp://host[:port]/file         FTP URL\n"
187   "  mms://host[:port]/file         MMS URL\n"
188   "  screen://                      Screen capture\n"
189   "  dvd://[device]                 DVD device\n"
190   "  vcd://[device]                 VCD device\n"
191   "  cdda://[device]                Audio CD device\n"
192   "  udp://[[<source address>]@[<bind address>][:<bind port>]]\n"
193   "                                 UDP stream sent by a streaming server\n"
194   "  vlc://pause:<seconds>          Pause the playlist for a certain time\n"
195   "  vlc://quit                     Special item to quit VLC\n"
196   "\n");
197
198 static void Help (vlc_object_t *p_this, char const *psz_help_name)
199 {
200     ShowConsole();
201
202     if( psz_help_name && !strcmp( psz_help_name, "help" ) )
203     {
204         printf(_(vlc_usage), "vlc");
205         Usage( p_this, "=core" );
206         print_help_on_full_help();
207     }
208     else if( psz_help_name && !strcmp( psz_help_name, "longhelp" ) )
209     {
210         printf(_(vlc_usage), "vlc");
211         Usage( p_this, NULL );
212         print_help_on_full_help();
213     }
214     else if( psz_help_name && !strcmp( psz_help_name, "full-help" ) )
215     {
216         printf(_(vlc_usage), "vlc");
217         Usage( p_this, NULL );
218     }
219     else if( psz_help_name )
220     {
221         Usage( p_this, psz_help_name );
222     }
223
224     PauseConsole();
225 }
226
227 /*****************************************************************************
228  * Usage: print module usage
229  *****************************************************************************
230  * Print a short inline help. Message interface is initialized at this stage.
231  *****************************************************************************/
232 #   define COL(x)  "\033[" #x ";1m"
233 #   define RED     COL(31)
234 #   define GREEN   COL(32)
235 #   define YELLOW  COL(33)
236 #   define BLUE    COL(34)
237 #   define MAGENTA COL(35)
238 #   define CYAN    COL(36)
239 #   define WHITE   COL(0)
240 #   define GRAY    "\033[0m"
241 #   define LINE_START      8
242 #   define PADDING_SPACES 25
243
244 static void print_section(const module_t *m, const module_config_t **sect,
245                           bool color, bool desc)
246 {
247     const module_config_t *item = *sect;
248
249     if (item == NULL)
250         return;
251     *sect = NULL;
252
253     printf(color ? RED"   %s:\n"GRAY : "   %s:\n",
254            module_gettext(m, item->psz_text));
255     if (desc && item->psz_longtext != NULL)
256         printf(color ? MAGENTA"   %s\n"GRAY : "   %s\n",
257                module_gettext(m, item->psz_longtext));
258 }
259
260 static void print_desc(const char *str, unsigned margin, bool color)
261 {
262     unsigned width = ConsoleWidth() - margin;
263
264     if (color)
265         fputs(BLUE, stdout);
266
267     const char *word = str;
268     int wordlen = 0, wordwidth = 0;
269     unsigned offset = 0;
270     bool newline = true;
271
272     while (str[0])
273     {
274         uint32_t cp;
275         size_t charlen = vlc_towc(str, &cp);
276         if (unlikely(charlen == (size_t)-1))
277             break;
278
279         int charwidth = wcwidth(cp);
280         if (charwidth < 0)
281             charwidth = 0;
282
283         str += charlen;
284
285         if (iswspace(cp))
286         {
287             if (!newline)
288             {
289                 putchar(' '); /* insert space */
290                 charwidth = 1;
291             }
292             fwrite(word, 1, wordlen, stdout); /* write complete word */
293             word = str;
294             wordlen = 0;
295             wordwidth = 0;
296             newline = false;
297         }
298         else
299         {
300             wordlen += charlen;
301             wordwidth += charwidth;
302         }
303
304         offset += charwidth;
305         if (offset >= width)
306         {
307             if (newline)
308             {   /* overflow (word wider than line) */
309                 fwrite(word, 1, wordlen - charlen, stdout);
310                 word = str - charlen;
311                 wordlen = charlen;
312                 wordwidth = charwidth;
313             }
314             printf("\n%*s", margin, ""); /* new line */
315             offset = wordwidth;
316             newline = true;
317         }
318     }
319
320     if (!newline)
321         putchar(' ');
322     printf(color ? "%s\n"GRAY : "%s\n", word);
323 }
324
325 static int vlc_swidth(const char *str)
326 {
327     for (int total = 0;;)
328     {
329         uint32_t cp;
330         size_t charlen = vlc_towc(str, &cp);
331
332         if (charlen == 0)
333             return total;
334         if (charlen == (size_t)-1)
335             return -1;
336         str += charlen;
337
338         int w = wcwidth(cp);
339         if (w == -1)
340             return -1;
341         total += w;
342     }
343 }
344
345 static void print_item(const module_t *m, const module_config_t *item,
346                        const module_config_t **section, bool color, bool desc)
347 {
348 #ifndef _WIN32
349 # define OPTION_VALUE_SEP " "
350 #else
351 # define OPTION_VALUE_SEP "="
352 #endif
353     const char *bra = OPTION_VALUE_SEP "<", *type, *ket = ">";
354     const char *prefix = NULL, *suffix = NULL;
355     char *typebuf = NULL;
356
357     switch (CONFIG_CLASS(item->i_type))
358     {
359         case 0: // hint class
360             switch (item->i_type)
361             {
362                 case CONFIG_HINT_CATEGORY:
363                 case CONFIG_HINT_USAGE:
364                     printf(color ? GREEN "\n %s\n" GRAY : "\n %s\n",
365                            module_gettext(m, item->psz_text));
366
367                     if (desc && item->psz_longtext != NULL)
368                         printf(color ? CYAN " %s\n" GRAY : " %s\n",
369                                module_gettext(m, item->psz_longtext));
370                     break;
371
372                 case CONFIG_SECTION:
373                     *section = item;
374                     break;
375             }
376             return;
377
378         case CONFIG_ITEM_STRING:
379             type = _("string");
380             if (item->list_count > 0)
381             {
382                 size_t len = 0;
383
384                 for (unsigned i = 0; i < item->list_count; i++)
385                     len += strlen(item->list.psz[i]) + 1;
386
387                 typebuf = malloc(len);
388                 if (typebuf == NULL)
389                     break;
390
391                 bra = OPTION_VALUE_SEP "{";
392                 type = typebuf;
393                 ket = "}";
394
395                 *typebuf = 0;
396                 for (unsigned i = 0; i < item->list_count; i++)
397                 {
398                     if (i > 0)
399                         strcat(typebuf, ",");
400                     strcat(typebuf, item->list.psz[i]);
401                 }
402             }
403             break;
404
405         case CONFIG_ITEM_INTEGER:
406             type = _("integer");
407
408             if (item->list_count > 0)
409             {
410                 size_t len = 0;
411
412                 for (unsigned i = 0; i < item->list_count; i++)
413                     len += strlen(item->list_text[i]) + 4 * sizeof (int) + 5;
414
415                 typebuf = malloc(len);
416                 if (typebuf == NULL)
417                     break;
418
419                 bra = OPTION_VALUE_SEP "{";
420                 type = typebuf;
421                 ket = "}";
422
423                 *typebuf = 0;
424                 for (unsigned i = 0; i < item->list_count; i++)
425                 {
426                     if (i != 0)
427                         strcat(typebuf, ", ");
428                     sprintf(typebuf + strlen(typebuf), "%i (%s)",
429                             item->list.i[i],
430                             module_gettext(m, item->list_text[i]));
431                 }
432             }
433             else if (item->min.i != 0 || item->max.i != 0)
434             {
435                 if (asprintf(&typebuf, "%s [%"PRId64" .. %"PRId64"]",
436                              type, item->min.i, item->max.i) >= 0)
437                     type = typebuf;
438                 else
439                     typebuf = NULL;
440             }
441             break;
442
443         case CONFIG_ITEM_FLOAT:
444             type = _("float");
445             if (item->min.f != 0.f || item->max.f != 0.f)
446             {
447                 if (asprintf(&typebuf, "%s [%f .. %f]", type,
448                              item->min.f, item->max.f) >= 0)
449                     type = typebuf;
450                 else
451                     typebuf = NULL;
452             }
453             break;
454
455         case CONFIG_ITEM_BOOL:
456             bra = type = ket = "";
457             prefix = ", --no-";
458             suffix = item->value.i ? _("(default enabled)")
459                                    : _("(default disabled)");
460             break;
461        default:
462             return;
463     }
464
465     print_section(m, section, color, desc);
466
467     /* Add short option if any */
468     char shortopt[4];
469     if (item->i_short != '\0')
470         sprintf(shortopt, "-%c,", item->i_short);
471     else
472         strcpy(shortopt, "   ");
473
474     if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
475         printf(color ? WHITE"  %s --%s"      "%s%s%s%s%s "GRAY
476                      : "  %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
477                prefix, item->psz_name, bra, type, ket);
478     else
479         printf(color ? WHITE"  %s --%s"YELLOW"%s%s%s%s%s "GRAY
480                      : "  %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
481                "", "",  /* XXX */      bra, type, ket);
482
483     /* Wrap description */
484     int offset = PADDING_SPACES - strlen(item->psz_name)
485                - strlen(bra) - vlc_swidth(type) - strlen(ket) - 1;
486     if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
487         offset -= strlen(item->psz_name) + vlc_swidth(prefix);
488     if (offset < 0)
489     {
490         putchar('\n');
491         offset = PADDING_SPACES + LINE_START;
492     }
493
494     printf("%*s", offset, "");
495     print_desc(module_gettext(m, item->psz_longtext),
496                PADDING_SPACES + LINE_START, color);
497
498     if (suffix != NULL)
499     {
500         printf("%*s", offset, "");
501         print_desc(suffix, PADDING_SPACES + LINE_START, color);
502     }
503
504     if (desc && (item->psz_longtext != NULL && item->psz_longtext[0]))
505     {   /* Wrap long description */
506         printf("%*s", LINE_START + 2, "");
507         print_desc(module_gettext(m, item->psz_longtext),
508                    LINE_START + 2, false);
509     }
510
511     free(typebuf);
512 }
513
514 static bool module_match(const module_t *m, const char *pattern, bool strict)
515 {
516     if (pattern == NULL)
517         return true;
518
519     const char *objname = module_get_object(m);
520
521     if (strict ? (strcmp(objname, pattern) == 0)
522                : (strstr(objname, pattern) != NULL))
523         return true;
524
525     for (unsigned i = 0; i < m->i_shortcuts; i++)
526     {
527         const char *shortcut = m->pp_shortcuts[i];
528
529         if (strict ? (strcmp(shortcut, pattern) == 0)
530                    : (strstr(shortcut, pattern) != NULL))
531             return true;
532     }
533     return false;
534 }
535
536 static bool module_show(const module_t *m, bool advanced)
537 {
538     for (size_t i = 0; i < m->confsize; i++)
539     {
540         const module_config_t *item = m->p_config + i;
541
542         if (!CONFIG_ITEM(item->i_type))
543             continue;
544         if (item->b_removed)
545             continue;
546         if ((!advanced) && item->b_advanced)
547             continue;
548         return true;
549     }
550     return false;
551 }
552
553 static void Usage (vlc_object_t *p_this, char const *psz_search)
554 {
555     bool b_has_advanced = false;
556     bool found = false;
557     unsigned i_only_advanced = 0; /* Number of modules ignored because they
558                                * only have advanced options */
559     bool strict = false;
560     if (psz_search != NULL && psz_search[0] == '=')
561     {
562         strict = true;
563         psz_search++;
564     }
565
566     bool color = false;
567 #ifndef _WIN32
568     if (isatty(STDOUT_FILENO))
569         color = var_InheritBool(p_this, "color");
570 #endif
571
572     const bool desc = var_InheritBool(p_this, "help-verbose");
573     const bool advanced = var_InheritBool(p_this, "advanced");
574
575     /* List all modules */
576     size_t count;
577     module_t **list = module_list_get (&count);
578
579     /* Enumerate the config for each module */
580     for (size_t i = 0; i < count; i++)
581     {
582         const module_t *m = list[i];
583         const module_config_t *section = NULL;
584         const char *objname = module_get_object(m);
585
586         if (m->i_config_items == 0)
587             continue; /* Ignore modules without config options */
588         if (!module_match(m, psz_search, strict))
589             continue;
590         found = true;
591
592         if (!module_show(m, advanced))
593         {   /* Ignore modules with only advanced config options if requested */
594             i_only_advanced++;
595             continue;
596         }
597
598         /* Print name of module */
599         printf(color ? "\n " GREEN "%s" GRAY " (%s)\n" : "\n %s (%s)\n",
600                module_gettext(m, m->psz_longname), objname);
601         if (m->psz_help != NULL)
602             printf(color ? CYAN" %s\n"GRAY : " %s\n",
603                    module_gettext(m, m->psz_help));
604
605         /* Print module options */
606         for (size_t j = 0; j < m->confsize; j++)
607         {
608             const module_config_t *item = m->p_config + j;
609
610             if (item->b_removed)
611                 continue; /* Skip removed options */
612             if (item->b_advanced && !advanced)
613             {   /* Skip advanced options unless requested */
614                 b_has_advanced = true;
615                 continue;
616             }
617             print_item(m, item, &section, color, desc);
618         }
619     }
620
621     if( b_has_advanced )
622         printf(color ? "\n" WHITE "%s" GRAY " %s\n"
623                      : "\n%s %s\n", _( "Note:" ), _( "add --advanced to your "
624                                      "command line to see advanced options."));
625     if( i_only_advanced > 0 )
626     {
627         printf(color ? "\n" WHITE "%s" GRAY " " : "\n%s ", _( "Note:" ) );
628         printf(vlc_ngettext("%u module was not displayed because it only has "
629                "advanced options.\n", "%u modules were not displayed because "
630                "they only have advanced options.\n", i_only_advanced),
631                i_only_advanced);
632     }
633     else if (!found)
634         printf(color ? "\n" WHITE "%s" GRAY "\n" : "\n%s\n",
635                _("No matching module found. Use --list or "
636                  "--list-verbose to list available modules."));
637
638     /* Release the module list */
639     module_list_free (list);
640 }
641
642 /*****************************************************************************
643  * ListModules: list the available modules with their description
644  *****************************************************************************
645  * Print a list of all available modules (builtins and plugins) and a short
646  * description for each one.
647  *****************************************************************************/
648 static void ListModules (vlc_object_t *p_this, bool b_verbose)
649 {
650     bool color = false;
651
652     ShowConsole();
653 #ifndef _WIN32
654     if (isatty(STDOUT_FILENO))
655         color = var_InheritBool(p_this, "color");
656 #else
657     (void) p_this;
658 #endif
659
660     /* List all modules */
661     size_t count;
662     module_t **list = module_list_get (&count);
663
664     /* Enumerate each module */
665     for (size_t j = 0; j < count; j++)
666     {
667         module_t *p_parser = list[j];
668         const char *objname = module_get_object (p_parser);
669         printf(color ? GREEN"  %-22s "WHITE"%s\n"GRAY : "  %-22s %s\n",
670                objname, module_gettext(p_parser, p_parser->psz_longname));
671
672         if( b_verbose )
673         {
674             char *const *pp_shortcuts = p_parser->pp_shortcuts;
675             for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
676                 if( strcmp( pp_shortcuts[i], objname ) )
677                     printf(color ? CYAN"   s %s\n"GRAY : "   s %s\n",
678                            pp_shortcuts[i]);
679             if (p_parser->psz_capability != NULL)
680                 printf(color ? MAGENTA"   c %s (%d)\n"GRAY : "   c %s (%d)\n",
681                        p_parser->psz_capability, p_parser->i_score);
682         }
683     }
684     module_list_free (list);
685     PauseConsole();
686 }
687
688 /*****************************************************************************
689  * Version: print complete program version
690  *****************************************************************************
691  * Print complete program version and build number.
692  *****************************************************************************/
693 static void Version( void )
694 {
695     ShowConsole();
696     printf(_("VLC version %s (%s)\n"), VERSION_MESSAGE, psz_vlc_changeset);
697     printf(_("Compiled by %s on %s (%s)\n"), VLC_CompileBy(),
698            VLC_CompileHost(), __DATE__" "__TIME__ );
699     printf(_("Compiler: %s\n"), VLC_Compiler());
700     fputs(LICENSE_MSG, stdout);
701     PauseConsole();
702 }
703
704 #if defined( _WIN32 ) && !VLC_WINSTORE_APP
705 /*****************************************************************************
706  * ShowConsole: On Win32, create an output console for debug messages
707  *****************************************************************************
708  * This function is useful only on Win32.
709  *****************************************************************************/
710 static void ShowConsole( void )
711 {
712     if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
713
714     if( !AllocConsole() ) return;
715
716     /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
717      * Unicode/locale subsystem. By default, we have the obsolecent OEM code
718      * page (e.g. CP437 or CP850). */
719     SetConsoleOutputCP (GetACP ());
720     SetConsoleTitle (TEXT("VLC media player version ") TEXT(PACKAGE_VERSION));
721
722     freopen( "CONOUT$", "w", stderr );
723     freopen( "CONIN$", "r", stdin );
724
725     if( freopen( "vlc-help.txt", "wt", stdout ) != NULL )
726     {
727         fputs( "\xEF\xBB\xBF", stdout );
728         fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
729     }
730     else
731         freopen( "CONOUT$", "w", stdout );
732 }
733
734 /*****************************************************************************
735  * PauseConsole: On Win32, wait for a key press before closing the console
736  *****************************************************************************
737  * This function is useful only on Win32.
738  *****************************************************************************/
739 static void PauseConsole( void )
740 {
741     if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
742
743     utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
744     getchar();
745     fclose( stdout );
746 }
747 #endif