Release 1.5.29.
[wine] / programs / dxdiag / main.c
1 /*
2  * DxDiag Implementation
3  *
4  * Copyright 2009 Austin English
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23 #include <dxdiag.h>
24
25 #include "wine/debug.h"
26 #include "wine/unicode.h"
27
28 #include "dxdiag_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);
31
32 HINSTANCE hInstance;
33
34 struct command_line_info
35 {
36     WCHAR outfile[MAX_PATH];
37     enum output_type output_type;
38     BOOL whql_check;
39 };
40
41 static void usage(void)
42 {
43     WCHAR title[MAX_STRING_LEN];
44     WCHAR usage[MAX_STRING_LEN];
45
46     LoadStringW(hInstance, STRING_DXDIAG_TOOL, title, sizeof(title)/sizeof(WCHAR));
47     LoadStringW(hInstance, STRING_USAGE, usage, sizeof(usage)/sizeof(WCHAR));
48
49     MessageBoxW(NULL, usage, title, MB_OK | MB_ICONWARNING);
50
51     ExitProcess(0);
52 }
53
54 static BOOL process_file_name(const WCHAR *cmdline, enum output_type output_type, WCHAR *filename, size_t filename_len)
55 {
56     const WCHAR *endptr;
57     size_t len;
58
59     /* Skip any intervening spaces. */
60     while (*cmdline == ' ')
61         cmdline++;
62
63     /* Ignore filename quoting, if any. */
64     if (*cmdline == '"' && (endptr = strrchrW(cmdline, '"')))
65     {
66         /* Reject a string with only one quote. */
67         if (cmdline == endptr)
68             return FALSE;
69
70         cmdline++;
71     }
72     else
73         endptr = cmdline + strlenW(cmdline);
74
75     len = endptr - cmdline;
76     if (len == 0 || len >= filename_len)
77         return FALSE;
78
79     memcpy(filename, cmdline, len * sizeof(WCHAR));
80     filename[len] = '\0';
81
82     /* Append an extension appropriate for the output type if the filename does not have one. */
83     if (!strrchrW(filename, '.'))
84     {
85         const WCHAR *filename_ext = get_output_extension(output_type);
86
87         if (len + strlenW(filename_ext) >= filename_len)
88             return FALSE;
89
90         strcatW(filename, filename_ext);
91     }
92
93     return TRUE;
94 }
95
96 /*
97     Process options [/WHQL:ON|OFF][/X outfile|/T outfile]
98     Returns TRUE if options were present, FALSE otherwise
99     Only one of /X and /T is allowed, /WHQL must come before /X and /T,
100     and the rest of the command line after /X or /T is interpreted as a
101     filename. If a non-option portion of the command line is encountered,
102     dxdiag assumes that the string is a filename for the /T option.
103
104     Native does not interpret quotes, but quotes are parsed here because of how
105     Wine handles the command line.
106 */
107
108 static BOOL process_command_line(const WCHAR *cmdline, struct command_line_info *info)
109 {
110     static const WCHAR whql_colonW[] = {'w','h','q','l',':',0};
111     static const WCHAR offW[] = {'o','f','f',0};
112     static const WCHAR onW[] = {'o','n',0};
113
114     info->whql_check = FALSE;
115     info->output_type = OUTPUT_NONE;
116
117     while (*cmdline)
118     {
119         /* Skip whitespace before arg */
120         while (*cmdline == ' ')
121             cmdline++;
122
123         /* If no option is specified, treat the command line as a filename. */
124         if (*cmdline != '-' && *cmdline != '/')
125         {
126             info->output_type = OUTPUT_TEXT;
127             return process_file_name(cmdline, OUTPUT_TEXT, info->outfile,
128                                      sizeof(info->outfile)/sizeof(WCHAR));
129         }
130
131         cmdline++;
132
133         switch (*cmdline)
134         {
135         case 'T':
136         case 't':
137             info->output_type = OUTPUT_TEXT;
138             return process_file_name(cmdline + 1, OUTPUT_TEXT, info->outfile,
139                                      sizeof(info->outfile)/sizeof(WCHAR));
140         case 'X':
141         case 'x':
142             info->output_type = OUTPUT_XML;
143             return process_file_name(cmdline + 1, OUTPUT_XML, info->outfile,
144                                      sizeof(info->outfile)/sizeof(WCHAR));
145         case 'W':
146         case 'w':
147             if (strncmpiW(cmdline, whql_colonW, 5))
148                 return FALSE;
149
150             cmdline += 5;
151
152             if (!strncmpiW(cmdline, offW, 3))
153             {
154                 info->whql_check = FALSE;
155                 cmdline += 2;
156             }
157             else if (!strncmpiW(cmdline, onW, 2))
158             {
159                 info->whql_check = TRUE;
160                 cmdline++;
161             }
162             else
163                 return FALSE;
164
165             break;
166         default:
167             return FALSE;
168         }
169
170         cmdline++;
171     }
172
173     return TRUE;
174 }
175
176 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
177 {
178     struct command_line_info info;
179     struct dxdiag_information *dxdiag_info;
180
181     hInstance = hInst;
182
183     if (!process_command_line(cmdline, &info))
184         usage();
185
186     WINE_TRACE("WHQL check: %s\n", info.whql_check ? "TRUE" : "FALSE");
187     WINE_TRACE("Output type: %d\n", info.output_type);
188     if (info.output_type != OUTPUT_NONE)
189         WINE_TRACE("Output filename: %s\n", debugstr_output_type(info.output_type));
190
191     CoInitialize(NULL);
192
193     dxdiag_info = collect_dxdiag_information(info.whql_check);
194     if (!dxdiag_info)
195     {
196         WINE_ERR("DxDiag information collection failed\n");
197         CoUninitialize();
198         return 1;
199     }
200
201     if (info.output_type != OUTPUT_NONE)
202         output_dxdiag_information(dxdiag_info, info.outfile, info.output_type);
203     else
204         WINE_FIXME("Information dialog is not implemented\n");
205
206     free_dxdiag_information(dxdiag_info);
207
208     CoUninitialize();
209     return 0;
210 }