]> git.sesse.net Git - vlc/blob - src/config/help.c
help: fix Windows Store build
[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, "=main" );
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 = "";
355     char psz_buffer[10000]; // XXX
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                 bra = OPTION_VALUE_SEP "{";
383                 type = psz_buffer;
384                 psz_buffer[0] = '\0';
385
386                 for (unsigned i = 0; i < item->list_count; i++)
387                 {
388                     if (i > 0)
389                         strcat(psz_buffer, ",");
390                     strcat(psz_buffer, item->list.psz[i]);
391                 }
392                 ket = "}";
393             }
394             break;
395
396         case CONFIG_ITEM_INTEGER:
397             type = _("integer");
398             if (item->min.i != 0 || item->max.i != 0)
399             {
400                 sprintf (psz_buffer, "%s [%"PRId64" .. %"PRId64"]",
401                          type, item->min.i, item->max.i);
402                 type = psz_buffer;
403             }
404             if (item->list_count > 0)
405             {
406                 bra = OPTION_VALUE_SEP "{";
407                 type = psz_buffer;
408                 psz_buffer[0] = '\0';
409
410                 for (unsigned i = 0; i < item->list_count; i++)
411                 {
412                     if (i != 0)
413                         strcat(psz_buffer, ", ");
414                     sprintf(psz_buffer + strlen(psz_buffer), "%i (%s)",
415                             item->list.i[i],
416                             module_gettext(m, item->list_text[i]));
417                 }
418                 ket = "}";
419             }
420             break;
421
422         case CONFIG_ITEM_FLOAT:
423             type = _("float");
424             if (item->min.f != 0.f || item->max.f != 0.f)
425             {
426                 sprintf(psz_buffer, "%s [%f .. %f]", type,
427                         item->min.f, item->max.f);
428                 type = psz_buffer;
429             }
430             break;
431
432         case CONFIG_ITEM_BOOL:
433             bra = type = ket = "";
434             prefix = ", --no-";
435             suffix = item->value.i ? _(" (default enabled)")
436                                    : _(" (default disabled)");
437             break;
438        default:
439             return;
440     }
441
442     print_section(m, section, color, desc);
443
444     /* Add short option if any */
445     char shortopt[4];
446     if (item->i_short != '\0')
447         sprintf(shortopt, "-%c,", item->i_short);
448     else
449         strcpy(shortopt, "   ");
450
451     if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
452         printf(color ? WHITE"  %s --%s"      "%s%s%s%s%s "GRAY
453                      : "  %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
454                prefix, item->psz_name, bra, type, ket);
455     else
456         printf(color ? WHITE"  %s --%s"YELLOW"%s%s%s%s%s "GRAY
457                      : "  %s --%s%s%s%s%s%s ", shortopt, item->psz_name,
458                "", "",  /* XXX */      bra, type, ket);
459
460     /* Wrap description */
461     int offset = PADDING_SPACES - strlen(item->psz_name)
462                - strlen(bra) - vlc_swidth(type) - strlen(ket) - 1;
463     if (CONFIG_CLASS(item->i_type) == CONFIG_ITEM_BOOL)
464         offset -= strlen(item->psz_name) + vlc_swidth(prefix);
465     if (offset < 0)
466     {
467         putchar('\n');
468         offset = PADDING_SPACES + LINE_START;
469     }
470     printf("%*s", offset, "");
471
472     sprintf(psz_buffer, "%s%s", module_gettext(m, item->psz_text), suffix);
473     print_desc(psz_buffer, PADDING_SPACES + LINE_START, color);
474
475     if (desc && (item->psz_longtext != NULL && item->psz_longtext[0]))
476     {   /* Wrap long description */
477         printf("%*s", LINE_START + 2, "");
478         sprintf(psz_buffer, "%s%s", module_gettext(m, item->psz_longtext),
479                 suffix);
480         print_desc(psz_buffer, LINE_START + 2, false);
481     }
482 }
483
484 static bool module_match(const module_t *m, const char *pattern, bool strict)
485 {
486     if (pattern == NULL)
487         return true;
488
489     const char *objname = module_get_object(m);
490
491     if (strict ? (strcmp(objname, pattern) == 0)
492                : (strstr(objname, pattern) != NULL))
493         return true;
494
495     for (unsigned i = 0; i < m->i_shortcuts; i++)
496     {
497         const char *shortcut = m->pp_shortcuts[i];
498
499         if (strict ? (strcmp(shortcut, pattern) == 0)
500                    : (strstr(shortcut, pattern) != NULL))
501             return true;
502     }
503     return false;
504 }
505
506 static bool module_show(const module_t *m, bool advanced)
507 {
508     for (size_t i = 0; i < m->confsize; i++)
509     {
510         const module_config_t *item = m->p_config + i;
511
512         if (!CONFIG_ITEM(item->i_type))
513             continue;
514         if (item->b_removed)
515             continue;
516         if ((!advanced) && item->b_advanced)
517             continue;
518         return true;
519     }
520     return false;
521 }
522
523 static void Usage (vlc_object_t *p_this, char const *psz_search)
524 {
525     bool b_has_advanced = false;
526     bool found = false;
527     unsigned i_only_advanced = 0; /* Number of modules ignored because they
528                                * only have advanced options */
529     bool strict = false;
530     if (psz_search != NULL && psz_search[0] == '=')
531     {
532         strict = true;
533         psz_search++;
534     }
535
536     bool color = false;
537 #ifndef _WIN32
538     if (isatty(STDOUT_FILENO))
539         color = var_InheritBool(p_this, "color");
540 #endif
541
542     const bool desc = var_InheritBool(p_this, "help-verbose");
543     const bool advanced = var_InheritBool(p_this, "advanced");
544
545     /* List all modules */
546     size_t count;
547     module_t **list = module_list_get (&count);
548
549     /* Enumerate the config for each module */
550     for (size_t i = 0; i < count; i++)
551     {
552         const module_t *m = list[i];
553         const module_config_t *section = NULL;
554         const char *objname = module_get_object(m);
555
556         if (m->i_config_items == 0)
557             continue; /* Ignore modules without config options */
558         if (!module_match(m, psz_search, strict))
559             continue;
560         found = true;
561
562         if (!module_show(m, advanced))
563         {   /* Ignore modules with only advanced config options if requested */
564             i_only_advanced++;
565             continue;
566         }
567
568         /* Print name of module */
569         printf(color ? "\n " GREEN "%s" GRAY " (%s)\n" : "\n %s\n",
570                module_gettext(m, m->psz_longname), objname);
571         if (m->psz_help != NULL)
572             printf(color ? CYAN" %s\n"GRAY : " %s\n",
573                    module_gettext(m, m->psz_help));
574
575         /* Print module options */
576         for (size_t i = 0; i < m->confsize; i++)
577         {
578             const module_config_t *item = m->p_config + i;
579
580             if (item->b_removed)
581                 continue; /* Skip removed options */
582             if (item->b_advanced && !advanced)
583             {   /* Skip advanced options unless requested */
584                 b_has_advanced = true;
585                 continue;
586             }
587             print_item(m, item, &section, color, desc);
588         }
589     }
590
591     if( b_has_advanced )
592         printf(color ? "\n" WHITE "%s" GRAY " %s\n"
593                      : "\n%s %s\n", _( "Note:" ), _( "add --advanced to your "
594                                      "command line to see advanced options."));
595     if( i_only_advanced > 0 )
596     {
597         printf(color ? "\n" WHITE "%s" GRAY " " : "\n%s ", _( "Note:" ) );
598         printf(vlc_ngettext("%u module was not displayed because it only has "
599                "advanced options.\n", "%u modules were not displayed because "
600                "they only have advanced options.\n", i_only_advanced),
601                i_only_advanced);
602     }
603     else if (!found)
604         printf(color ? "\n" WHITE "%s" GRAY "\n" : "\n%s\n",
605                _("No matching module found. Use --list or "
606                  "--list-verbose to list available modules."));
607
608     /* Release the module list */
609     module_list_free (list);
610 }
611
612 /*****************************************************************************
613  * ListModules: list the available modules with their description
614  *****************************************************************************
615  * Print a list of all available modules (builtins and plugins) and a short
616  * description for each one.
617  *****************************************************************************/
618 static void ListModules (vlc_object_t *p_this, bool b_verbose)
619 {
620     bool color = false;
621
622     ShowConsole();
623 #ifndef _WIN32
624     if (isatty(STDOUT_FILENO))
625         color = var_InheritBool(p_this, "color");
626 #endif
627
628     /* List all modules */
629     size_t count;
630     module_t **list = module_list_get (&count);
631
632     /* Enumerate each module */
633     for (size_t j = 0; j < count; j++)
634     {
635         module_t *p_parser = list[j];
636         const char *objname = module_get_object (p_parser);
637         printf(color ? GREEN"  %-22s "WHITE"%s\n"GRAY : "  %-22s %s\n",
638                objname, module_gettext(p_parser, p_parser->psz_longname));
639
640         if( b_verbose )
641         {
642             char *const *pp_shortcuts = p_parser->pp_shortcuts;
643             for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
644                 if( strcmp( pp_shortcuts[i], objname ) )
645                     printf(color ? CYAN"   s %s\n"GRAY : "   s %s\n",
646                            pp_shortcuts[i]);
647             if (p_parser->psz_capability != NULL)
648                 printf(color ? MAGENTA"   c %s (%d)\n"GRAY : "   c %s (%d)\n",
649                        p_parser->psz_capability, p_parser->i_score);
650         }
651     }
652     module_list_free (list);
653     PauseConsole();
654 }
655
656 /*****************************************************************************
657  * Version: print complete program version
658  *****************************************************************************
659  * Print complete program version and build number.
660  *****************************************************************************/
661 static void Version( void )
662 {
663     ShowConsole();
664     printf(_("VLC version %s (%s)\n"), VERSION_MESSAGE, psz_vlc_changeset);
665     printf(_("Compiled by %s on %s (%s)\n"), VLC_CompileBy(),
666            VLC_CompileHost(), __DATE__" "__TIME__ );
667     printf(_("Compiler: %s\n"), VLC_Compiler());
668     fputs(LICENSE_MSG, stdout);
669     PauseConsole();
670 }
671
672 #if defined( _WIN32 ) && !VLC_WINSTORE_APP
673 /*****************************************************************************
674  * ShowConsole: On Win32, create an output console for debug messages
675  *****************************************************************************
676  * This function is useful only on Win32.
677  *****************************************************************************/
678 static void ShowConsole( void )
679 {
680     if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
681
682     if( !AllocConsole() ) return;
683
684     /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
685      * Unicode/locale subsystem. By default, we have the obsolecent OEM code
686      * page (e.g. CP437 or CP850). */
687     SetConsoleOutputCP (GetACP ());
688     SetConsoleTitle (TEXT("VLC media player version "PACKAGE_VERSION));
689
690     freopen( "CONOUT$", "w", stderr );
691     freopen( "CONIN$", "r", stdin );
692
693     if( freopen( "vlc-help.txt", "wt", stdout ) != NULL )
694     {
695         fputs( "\xEF\xBB\xBF", stdout );
696         fprintf( stderr, _("\nDumped content to vlc-help.txt file.\n") );
697     }
698     else
699         freopen( "CONOUT$", "w", stdout );
700 }
701
702 /*****************************************************************************
703  * PauseConsole: On Win32, wait for a key press before closing the console
704  *****************************************************************************
705  * This function is useful only on Win32.
706  *****************************************************************************/
707 static void PauseConsole( void )
708 {
709     if( getenv( "PWD" ) ) return; /* Cygwin shell or Wine */
710
711     utf8_fprintf( stderr, _("\nPress the RETURN key to continue...\n") );
712     getchar();
713     fclose( stdout );
714 }
715 #endif