make sure text operations are not queried by (numeric) value
[LibreOffice.git] / solenv / wsl / wsl-lo-helper.cpp
blob1a90580b6f19fc2566e5fd4bb637202672dc8ab3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
11 * This is a tool that will be useful for various tasks if/when we build LO on WSL
13 * It is a Win32 program, not a Linux (WSL) one.
15 * Compile as: cl -MD wsl-lo-helper.cpp advapi32.lib
18 #include <cstdio>
19 #include <cstring>
21 #include <Windows.h>
23 static void print_result(const wchar_t* argv0, const wchar_t* result)
25 char output[1000];
26 if (WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, result, -1, output, sizeof(output), NULL,
27 NULL)
28 == 0)
30 fprintf(stderr, "%S --8.3: Could not convert result to UTF-8.\n", argv0);
31 exit(1);
34 // Intentionally output no newline at end
35 printf("%s", output);
36 fflush(stdout);
39 static REGSAM parse_bitness(const wchar_t* argv0, const wchar_t* command, const wchar_t* bitness)
41 if (wcscmp(bitness, L"32") == 0)
42 return KEY_WOW64_32KEY;
43 else if (wcscmp(bitness, L"64") == 0)
44 return KEY_WOW64_64KEY;
45 else
47 fprintf(stderr, "%S %S: Use 32 or 64.\n", argv0, command);
48 exit(1);
52 static void parse_hive(const wchar_t* argv0, const wchar_t* path, HKEY* hivep,
53 const wchar_t** hive_namep)
55 if (wcscmp(path, L"HKEY_CLASSES_ROOT") == 0)
57 *hivep = HKEY_CLASSES_ROOT;
58 *hive_namep = L"HKEY_CLASSES_ROOT";
60 else if (wcscmp(path, L"HKEY_CURRENT_CONFIG") == 0)
62 *hivep = HKEY_CURRENT_CONFIG;
63 *hive_namep = L"HKEY_CURRENT_CONFIG";
65 else if (wcscmp(path, L"HKEY_CURRENT_USER") == 0)
67 *hivep = HKEY_CURRENT_USER;
68 *hive_namep = L"HKEY_CURRENT_USER";
70 else if (wcscmp(path, L"HKEY_LOCAL_MACHINE") == 0)
72 *hivep = HKEY_LOCAL_MACHINE;
73 *hive_namep = L"HKEY_LOCAL_MACHINE";
75 else if (wcscmp(path, L"HKEY_USERS") == 0)
77 *hivep = HKEY_USERS;
78 *hive_namep = L"HKEY_USERS";
80 else
82 fprintf(stderr, "%S: Invalid Registry hive %S.\n", argv0, path);
83 exit(1);
87 int wmain(int argc, wchar_t** argv)
89 if (argc <= 1)
91 fprintf(stderr, "%S: Usage:\n", argv[0]);
92 fprintf(stderr, "%S --8.3 <windows-pathname>\n", argv[0]);
93 fprintf(stderr, " Print the 8.3 form of a Windows pathnam. Fail it not present.\n");
94 fprintf(stderr, "%S --read-registry [32|64] <path>\n", argv[0]);
95 fprintf(stderr, " Read a string value from the Registry and print it.\n");
96 fprintf(stderr, "%S --list-registry [32|64] <path>\n", argv[0]);
97 fprintf(stderr, " List subkeys of a key in the Registry.\n");
98 exit(1);
101 if (wcscmp(argv[1], L"--8.3") == 0)
103 if (argc != 3)
105 fprintf(stderr, "%S --8.3: One pathname argument expected\n", argv[0]);
106 exit(1);
109 // The argument should be a pathname in Windows format. The
110 // output will be the 8.3 pathname if present. If not present,
111 // return failure.
113 wchar_t woutput[1000];
114 if (GetShortPathNameW(argv[2], woutput, sizeof(woutput) / sizeof(woutput[0])) == 0)
116 fprintf(stderr, "%S --8.3: Could not get short pathname of %S.\n", argv[0], argv[2]);
117 exit(1);
120 print_result(argv[0], woutput);
122 else if (wcscmp(argv[1], L"--read-registry") == 0)
124 if (argc != 4)
126 fprintf(stderr, "%S --read-registry: Bitness and path arguments expected.\n", argv[0]);
127 exit(1);
130 REGSAM sam = parse_bitness(argv[0], L"--read-registry", argv[2]);
132 wchar_t* path = wcsdup(argv[3]);
134 for (wchar_t* p = path; *p != L'\0'; p++)
135 if (*p == '/')
136 *p = '\\';
138 wchar_t* const first_backslash = wcschr(path, L'\\');
139 if (first_backslash == NULL)
141 fprintf(stderr, "%S: Invalid path %S to value in the Registry.\n", argv[0], path);
142 exit(1);
145 *first_backslash = L'\0';
146 wchar_t* const key_path = first_backslash + 1;
148 HKEY hive;
149 const wchar_t* hive_name;
150 parse_hive(argv[0], path, &hive, &hive_name);
152 DWORD type;
153 wchar_t result[1000];
154 DWORD count = sizeof(result);
156 wchar_t* last_backslash = wcsrchr(key_path, L'\\');
157 if (last_backslash == NULL)
159 fprintf(stderr, "%S: Invalid path %S\\%S to value in the Registry.\n", argv[0],
160 hive_name, key_path);
161 exit(1);
164 *last_backslash = L'\0';
165 wchar_t* value = last_backslash + 1;
167 HKEY key;
168 if (RegOpenKeyExW(hive, key_path, 0, KEY_QUERY_VALUE | sam, &key) != ERROR_SUCCESS)
170 fprintf(stderr, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv[0],
171 hive_name, key_path, argv[2]);
172 exit(1);
175 if (RegQueryValueExW(key, value, NULL, &type, (LPBYTE)result, &count) != ERROR_SUCCESS)
177 fprintf(stderr, "%S: Reading value %S\\%S\\%S from %S-bit Registry failed.\n", argv[0],
178 hive_name, key_path, value, argv[2]);
179 exit(1);
181 if (type != REG_SZ)
183 fprintf(stderr, "%S: Value %S\\%S\\%S is not a string.\n", argv[0], hive_name, key_path,
184 value);
185 exit(1);
187 print_result(argv[0], result);
189 else if (wcscmp(argv[1], L"--list-registry") == 0)
191 if (argc != 4)
193 fprintf(stderr, "%S --list-registry: Bitness and path arguments expected.\n", argv[0]);
194 exit(1);
197 REGSAM sam = parse_bitness(argv[0], L"--list-registry", argv[2]);
199 wchar_t* path = wcsdup(argv[3]);
201 for (wchar_t* p = path; *p != L'\0'; p++)
202 if (*p == '/')
203 *p = '\\';
205 wchar_t* const first_backslash = wcschr(path, L'\\');
206 if (first_backslash == NULL)
208 fprintf(stderr, "%S: Invalid path %S to key in the Registry.\n", argv[0], path);
209 exit(1);
212 *first_backslash = L'\0';
213 wchar_t* const key_path = first_backslash + 1;
215 HKEY hive;
216 const wchar_t* hive_name;
217 parse_hive(argv[0], path, &hive, &hive_name);
219 HKEY key;
220 if (RegOpenKeyExW(hive, key_path, 0, KEY_ENUMERATE_SUB_KEYS | sam, &key) != ERROR_SUCCESS)
222 fprintf(stderr, "%S: Opening key %S\\%S in %S-bit Registry failed.\n", argv[0],
223 hive_name, key_path, argv[2]);
224 exit(1);
227 DWORD index = 0;
228 wchar_t name[256];
229 DWORD namelen = sizeof(name) / sizeof(name[0]);
230 while (RegEnumKeyExW(key, index, name, &namelen, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
232 printf("%S\n", name);
233 index++;
234 namelen = sizeof(name) / sizeof(name[0]);
237 else
239 fprintf(stderr, "%S: Unrecognized sub-command %S.\n", argv[0], argv[1]);
240 exit(1);
243 return 0;
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */