]> git.sesse.net Git - vlc/blob - src/winvlc.c
Simplify Win32 command line parsing
[vlc] / src / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  *
6  * Authors: Vincent Seguin <seguin@via.ecp.fr>
7  *          Samuel Hocevar <sam@zoy.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *          Lots of other people, see the libvlc AUTHORS file
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #define UNICODE
32 #include <vlc/vlc.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <windows.h>
37
38 static int parse_cmdline (char *line, char ***argvp)
39 {
40     char **argv = malloc (sizeof (char *));
41     int argc = 0;
42
43     while (*line != '\0')
44     {
45         char quote = 0;
46
47         /* Skips white spaces */
48         while (strchr ("\t ", *line))
49             line++;
50         if (!*line)
51             break;
52
53         /* Starts a new parameter */
54         argv = realloc (argv, (argc + 2) * sizeof (char *));
55         if (*line == '"')
56         {
57             quote = '"';
58             line++;
59         }
60         argv[argc++] = line;
61
62     more:
63         while (*line && !strchr ("\t ", *line))
64             line++;
65
66         if (line > argv[argc - 1] && line[-1] == quote)
67             /* End of quoted parameter */
68             line[-1] = 0;
69         else
70         if (*line && quote)
71         {
72             /* Space within a quote */
73             line++;
74             goto more;
75         }
76         else
77         /* End of unquoted parameter */
78         if (*line)
79             *line++ = 0;
80     }
81     argv[argc] = NULL;
82     *argvp = argv;
83     return argc;
84 }
85
86 #ifdef UNDER_CE
87 # define wWinMain WinMain
88 #endif
89
90 /*****************************************************************************
91  * wWinMain: parse command line, start interface and spawn threads.
92  *****************************************************************************/
93 int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
94                     LPWSTR lpCmdLine, int nCmdShow )
95 {
96     char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
97     int argc, ret;
98
99     (void)hInstance; (void)hPrevInstance; (void)nCmdShow;
100
101     WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
102                          psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
103
104     argc = parse_cmdline (psz_cmdline, &argv);
105
106     libvlc_exception_t ex;
107     libvlc_exception_init (&ex);
108
109     /* Initialize libvlc */
110     libvlc_instance_t *vlc = libvlc_new (argc, (const char **)argv, &ex);
111     if (vlc != NULL)
112     {
113         libvlc_add_intf (vlc, NULL, &ex);
114         libvlc_wait (vlc);
115         libvlc_release (vlc);
116     }
117     free (argv);
118
119     ret = libvlc_exception_raised (&ex);
120     libvlc_exception_clear (&ex);
121     return ret;
122 }
123
124 #ifndef IF_MINGW_SUPPORTED_UNICODE
125 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
126                     LPSTR args, int nCmdShow)
127 {
128     /* This makes little sense, but at least it links properly */
129     wchar_t lpCmdLine[strlen(args) * 3];
130     MultiByteToWideChar( CP_ACP, 0, args, -1, lpCmdLine, sizeof(lpCmdLine) );
131     return wWinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow);
132 }
133 #endif